Skip to content

Commit 36b4aea

Browse files
committed
All tests pass
1 parent c6262a8 commit 36b4aea

File tree

3 files changed

+137
-32
lines changed

3 files changed

+137
-32
lines changed

src/PEAR2/Net/RouterOS/Client.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
/**
2424
* Refers to transmitter direction constants.
2525
*/
26-
use PEAR2\Net\Transmitter as T;
26+
use PEAR2\Net\Transmitter\Stream as S;
2727

2828
/**
2929
* Uses shared memory to keep responses in when using persistent connections.
@@ -115,11 +115,12 @@ public function __construct($host, $username, $password = '', $port = 8728,
115115
$persist = false, $timeout = null, $context = null
116116
) {
117117
$this->com = new Communicator(
118-
$host, $port, $persist, $timeout, $username, $context
118+
$host, $port, $persist, $timeout, $username . '/' . $password,
119+
$context
119120
);
120121
//Login the user if necessary
121122
if ((!$persist
122-
|| 0 == $this->com->getTransmitter()->lock(T\Stream::DIRECTION_ALL))
123+
|| !($old = $this->com->getTransmitter()->lock(S::DIRECTION_ALL)))
123124
&& $this->com->getTransmitter()->isFresh()
124125
) {
125126
if (!static::login($this->com, $username, $password)) {
@@ -128,7 +129,10 @@ public function __construct($host, $username, $password = '', $port = 8728,
128129
'Invalid username or password supplied.', 10000
129130
);
130131
}
131-
$this->com->getTransmitter()->lock(T\Stream::DIRECTION_NONE, true);
132+
}
133+
134+
if (isset($old)) {
135+
$this->com->getTransmitter()->lock($old, true);
132136
}
133137

134138
if ($persist) {
@@ -187,12 +191,12 @@ public static function login(Communicator $com, $username, $password = '')
187191
$old = null;
188192
try {
189193
if ($com->getTransmitter()->isPersistent()) {
190-
$old = $com->getTransmitter()->lock(T\Stream::DIRECTION_ALL);
191-
$result = self::_performLogin($com, $username, $password);
194+
$old = $com->getTransmitter()->lock(S::DIRECTION_ALL);
195+
$result = self::_login($com, $username, $password);
192196
$com->getTransmitter()->lock($old, true);
193197
return $result;
194198
}
195-
return self::_performLogin($com, $username, $password);
199+
return self::_login($com, $username, $password);
196200
} catch (\Exception $e) {
197201
if ($com->getTransmitter()->isPersistent() && null !== $old) {
198202
$com->getTransmitter()->lock($old, true);
@@ -218,7 +222,7 @@ public static function login(Communicator $com, $username, $password = '')
218222
*
219223
* @return bool TRUE on success, FALSE on failure.
220224
*/
221-
private static function _performLogin(
225+
private static function _login(
222226
Communicator $com, $username, $password
223227
) {
224228
$request = new Request('/login');
@@ -567,9 +571,7 @@ public function cancelRequest($tag = null)
567571
$this->completeRequest($tag);
568572
}
569573
} else {
570-
$this->responseBuffer = array();
571-
$this->callbacks = array();
572-
$this->pendingRequestsCount = 0;
574+
$this->loop();
573575
}
574576
return $this;
575577
}
@@ -618,7 +620,7 @@ public function isStreamingResponses()
618620
*/
619621
public function close()
620622
{
621-
$result = true;
623+
$result = false;
622624
try {
623625
if (0 !== $this->pendingRequestsCount) {
624626
if (null !== $this->registry) {
@@ -630,15 +632,11 @@ public function close()
630632
}
631633
$result = $response->getType() === Response::TYPE_FATAL;
632634
}
633-
$result = $result && $this->com->close();
634635
} catch (SocketException $e) {
635636
$result = $e->getCode() === 205;
636637
}
637638
$this->callbacks = array();
638639
$this->pendingRequestsCount = 0;
639-
//if (null !== $this->registry) {
640-
// $this->registry->close();
641-
//}
642640
return $result;
643641
}
644642

tests/ClientFeaturesTest.php

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -196,22 +196,17 @@ public function testSendAsyncAndFullCancel()
196196
$this->object->sendAsync($ping2);
197197
$this->object->loop(2);
198198
$this->object->cancelRequest();
199-
try {
200-
$this->object->extractNewResponses('ping1');
201-
$this->fail('The call had to fail.');
202-
} catch (DataFlowException $e) {
203-
$this->assertEquals(
204-
10900, $e->getCode(), 'Improper exception code.'
205-
);
206-
}
207-
try {
208-
$this->object->extractNewResponses('ping2');
209-
$this->fail('The call had to fail.');
210-
} catch (DataFlowException $e) {
211-
$this->assertEquals(
212-
10900, $e->getCode(), 'Improper exception code.'
213-
);
214-
}
199+
200+
$ping1responses = $this->object->extractNewResponses('ping1');
201+
202+
$ping1responses->end();
203+
$ping1responses->prev();
204+
$this->assertEquals(Response::TYPE_ERROR, $ping1responses->getType());
205+
206+
$ping2responses = $this->object->extractNewResponses('ping2');
207+
$ping2responses->end();
208+
$ping2responses->prev();
209+
$this->assertEquals(Response::TYPE_ERROR, $ping2responses->getType());
215210
}
216211

217212
public function testInvalidCancel()

tests/ConnectionTest.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,22 @@ public function testInvalidUsername()
144144
10000, $e->getCode(), 'Improper exception code.'
145145
);
146146
}
147+
148+
try {
149+
$routerOS = new Client(
150+
\HOSTNAME, USERNAME_INVALID, PASSWORD, PORT, true
151+
);
152+
153+
$this->fail(
154+
'No proper connection with the username "'
155+
. USERNAME_INVALID
156+
. '" should be available.'
157+
);
158+
} catch (DataFlowException $e) {
159+
$this->assertEquals(
160+
10000, $e->getCode(), 'Improper exception code.'
161+
);
162+
}
147163
}
148164

149165
public function testInvalidPassword()
@@ -161,6 +177,22 @@ public function testInvalidPassword()
161177
10000, $e->getCode(), 'Improper exception code.'
162178
);
163179
}
180+
181+
try {
182+
$routerOS = new Client(
183+
\HOSTNAME, USERNAME, PASSWORD_INVALID, PORT, true
184+
);
185+
186+
$this->fail(
187+
'No proper connection with the password "'
188+
. PASSWORD_INVALID
189+
. '" should be available.'
190+
);
191+
} catch (DataFlowException $e) {
192+
$this->assertEquals(
193+
10000, $e->getCode(), 'Improper exception code.'
194+
);
195+
}
164196
}
165197

166198
public function testInvalidUsernameAndPassword()
@@ -182,6 +214,24 @@ public function testInvalidUsernameAndPassword()
182214
10000, $e->getCode(), 'Improper exception code.'
183215
);
184216
}
217+
218+
try {
219+
$routerOS = new Client(
220+
\HOSTNAME, USERNAME_INVALID, PASSWORD_INVALID, PORT, true
221+
);
222+
223+
$this->fail(
224+
'No proper connection with the username "'
225+
. USERNAME_INVALID
226+
. '" and password "'
227+
. PASSWORD_INVALID
228+
. '" should be available.'
229+
);
230+
} catch (DataFlowException $e) {
231+
$this->assertEquals(
232+
10000, $e->getCode(), 'Improper exception code.'
233+
);
234+
}
185235
}
186236

187237
public function testInvalidHost()
@@ -199,6 +249,22 @@ public function testInvalidHost()
199249
10200, $e->getCode(), 'Improper exception code.'
200250
);
201251
}
252+
253+
try {
254+
$routerOS = new Client(
255+
\HOSTNAME_INVALID, USERNAME, PASSWORD, PORT, true
256+
);
257+
258+
$this->fail(
259+
'No proper connection over hostname "'
260+
. \HOSTNAME_INVALID
261+
. '" should be available.'
262+
);
263+
} catch (SocketException $e) {
264+
$this->assertEquals(
265+
10200, $e->getCode(), 'Improper exception code.'
266+
);
267+
}
202268
}
203269

204270
public function testSilentHost()
@@ -215,6 +281,21 @@ public function testSilentHost()
215281
$this->assertEquals(7, $e->getCode());
216282
$this->assertEquals(10060, $e->getSocketErrorNumber());
217283
}
284+
285+
try {
286+
$routerOS = new Client(
287+
\HOSTNAME_SILENT, USERNAME, PASSWORD, PORT, true
288+
);
289+
290+
$this->fail(
291+
'No proper connection over hostname "'
292+
. \HOSTNAME_SILENT
293+
. '" should be available.'
294+
);
295+
} catch (T\SocketException $e) {
296+
$this->assertEquals(7, $e->getCode());
297+
$this->assertEquals(10060, $e->getSocketErrorNumber());
298+
}
218299
}
219300

220301
public function testInvalidPort()
@@ -232,6 +313,22 @@ public function testInvalidPort()
232313
10200, $e->getCode(), 'Improper exception code.'
233314
);
234315
}
316+
317+
try {
318+
$routerOS = new Client(
319+
\HOSTNAME, USERNAME, PASSWORD, PORT_INVALID, true
320+
);
321+
322+
$this->fail(
323+
'No proper connection over port "'
324+
. PORT_INVALID
325+
. '" should be available.'
326+
);
327+
} catch (SocketException $e) {
328+
$this->assertEquals(
329+
10200, $e->getCode(), 'Improper exception code.'
330+
);
331+
}
235332
}
236333

237334
public function testSilentPort()
@@ -248,6 +345,21 @@ public function testSilentPort()
248345
$this->assertEquals(7, $e->getCode());
249346
$this->assertEquals(10061, $e->getSocketErrorNumber());
250347
}
348+
349+
try {
350+
$routerOS = new Client(
351+
\HOSTNAME, USERNAME, PASSWORD, PORT_SILENT, true
352+
);
353+
354+
$this->fail(
355+
'No proper connection over port "'
356+
. PORT_SILENT
357+
. '" should be available.'
358+
);
359+
} catch (T\SocketException $e) {
360+
$this->assertEquals(7, $e->getCode());
361+
$this->assertEquals(10061, $e->getSocketErrorNumber());
362+
}
251363
}
252364

253365
public function testInvalidTimeout()

0 commit comments

Comments
 (0)