Permalink
Browse files

All tests pass

  • Loading branch information...
1 parent c6262a8 commit 36b4aea01fc760f191d09430eed7550ace3f951f @boenrobot boenrobot committed Jun 5, 2012
Showing with 137 additions and 32 deletions.
  1. +14 −16 src/PEAR2/Net/RouterOS/Client.php
  2. +11 −16 tests/ClientFeaturesTest.php
  3. +112 −0 tests/ConnectionTest.php
@@ -23,7 +23,7 @@
/**
* Refers to transmitter direction constants.
*/
-use PEAR2\Net\Transmitter as T;
+use PEAR2\Net\Transmitter\Stream as S;
/**
* Uses shared memory to keep responses in when using persistent connections.
@@ -115,11 +115,12 @@ public function __construct($host, $username, $password = '', $port = 8728,
$persist = false, $timeout = null, $context = null
) {
$this->com = new Communicator(
- $host, $port, $persist, $timeout, $username, $context
+ $host, $port, $persist, $timeout, $username . '/' . $password,
+ $context
);
//Login the user if necessary
if ((!$persist
- || 0 == $this->com->getTransmitter()->lock(T\Stream::DIRECTION_ALL))
+ || !($old = $this->com->getTransmitter()->lock(S::DIRECTION_ALL)))
&& $this->com->getTransmitter()->isFresh()
) {
if (!static::login($this->com, $username, $password)) {
@@ -128,7 +129,10 @@ public function __construct($host, $username, $password = '', $port = 8728,
'Invalid username or password supplied.', 10000
);
}
- $this->com->getTransmitter()->lock(T\Stream::DIRECTION_NONE, true);
+ }
+
+ if (isset($old)) {
+ $this->com->getTransmitter()->lock($old, true);
}
if ($persist) {
@@ -187,12 +191,12 @@ public static function login(Communicator $com, $username, $password = '')
$old = null;
try {
if ($com->getTransmitter()->isPersistent()) {
- $old = $com->getTransmitter()->lock(T\Stream::DIRECTION_ALL);
- $result = self::_performLogin($com, $username, $password);
+ $old = $com->getTransmitter()->lock(S::DIRECTION_ALL);
+ $result = self::_login($com, $username, $password);
$com->getTransmitter()->lock($old, true);
return $result;
}
- return self::_performLogin($com, $username, $password);
+ return self::_login($com, $username, $password);
} catch (\Exception $e) {
if ($com->getTransmitter()->isPersistent() && null !== $old) {
$com->getTransmitter()->lock($old, true);
@@ -218,7 +222,7 @@ public static function login(Communicator $com, $username, $password = '')
*
* @return bool TRUE on success, FALSE on failure.
*/
- private static function _performLogin(
+ private static function _login(
Communicator $com, $username, $password
) {
$request = new Request('/login');
@@ -567,9 +571,7 @@ public function cancelRequest($tag = null)
$this->completeRequest($tag);
}
} else {
- $this->responseBuffer = array();
- $this->callbacks = array();
- $this->pendingRequestsCount = 0;
+ $this->loop();
}
return $this;
}
@@ -618,7 +620,7 @@ public function isStreamingResponses()
*/
public function close()
{
- $result = true;
+ $result = false;
try {
if (0 !== $this->pendingRequestsCount) {
if (null !== $this->registry) {
@@ -630,15 +632,11 @@ public function close()
}
$result = $response->getType() === Response::TYPE_FATAL;
}
- $result = $result && $this->com->close();
} catch (SocketException $e) {
$result = $e->getCode() === 205;
}
$this->callbacks = array();
$this->pendingRequestsCount = 0;
- //if (null !== $this->registry) {
- // $this->registry->close();
- //}
return $result;
}
@@ -196,22 +196,17 @@ public function testSendAsyncAndFullCancel()
$this->object->sendAsync($ping2);
$this->object->loop(2);
$this->object->cancelRequest();
- try {
- $this->object->extractNewResponses('ping1');
- $this->fail('The call had to fail.');
- } catch (DataFlowException $e) {
- $this->assertEquals(
- 10900, $e->getCode(), 'Improper exception code.'
- );
- }
- try {
- $this->object->extractNewResponses('ping2');
- $this->fail('The call had to fail.');
- } catch (DataFlowException $e) {
- $this->assertEquals(
- 10900, $e->getCode(), 'Improper exception code.'
- );
- }
+
+ $ping1responses = $this->object->extractNewResponses('ping1');
+
+ $ping1responses->end();
+ $ping1responses->prev();
+ $this->assertEquals(Response::TYPE_ERROR, $ping1responses->getType());
+
+ $ping2responses = $this->object->extractNewResponses('ping2');
+ $ping2responses->end();
+ $ping2responses->prev();
+ $this->assertEquals(Response::TYPE_ERROR, $ping2responses->getType());
}
public function testInvalidCancel()
@@ -144,6 +144,22 @@ public function testInvalidUsername()
10000, $e->getCode(), 'Improper exception code.'
);
}
+
+ try {
+ $routerOS = new Client(
+ \HOSTNAME, USERNAME_INVALID, PASSWORD, PORT, true
+ );
+
+ $this->fail(
+ 'No proper connection with the username "'
+ . USERNAME_INVALID
+ . '" should be available.'
+ );
+ } catch (DataFlowException $e) {
+ $this->assertEquals(
+ 10000, $e->getCode(), 'Improper exception code.'
+ );
+ }
}
public function testInvalidPassword()
@@ -161,6 +177,22 @@ public function testInvalidPassword()
10000, $e->getCode(), 'Improper exception code.'
);
}
+
+ try {
+ $routerOS = new Client(
+ \HOSTNAME, USERNAME, PASSWORD_INVALID, PORT, true
+ );
+
+ $this->fail(
+ 'No proper connection with the password "'
+ . PASSWORD_INVALID
+ . '" should be available.'
+ );
+ } catch (DataFlowException $e) {
+ $this->assertEquals(
+ 10000, $e->getCode(), 'Improper exception code.'
+ );
+ }
}
public function testInvalidUsernameAndPassword()
@@ -182,6 +214,24 @@ public function testInvalidUsernameAndPassword()
10000, $e->getCode(), 'Improper exception code.'
);
}
+
+ try {
+ $routerOS = new Client(
+ \HOSTNAME, USERNAME_INVALID, PASSWORD_INVALID, PORT, true
+ );
+
+ $this->fail(
+ 'No proper connection with the username "'
+ . USERNAME_INVALID
+ . '" and password "'
+ . PASSWORD_INVALID
+ . '" should be available.'
+ );
+ } catch (DataFlowException $e) {
+ $this->assertEquals(
+ 10000, $e->getCode(), 'Improper exception code.'
+ );
+ }
}
public function testInvalidHost()
@@ -199,6 +249,22 @@ public function testInvalidHost()
10200, $e->getCode(), 'Improper exception code.'
);
}
+
+ try {
+ $routerOS = new Client(
+ \HOSTNAME_INVALID, USERNAME, PASSWORD, PORT, true
+ );
+
+ $this->fail(
+ 'No proper connection over hostname "'
+ . \HOSTNAME_INVALID
+ . '" should be available.'
+ );
+ } catch (SocketException $e) {
+ $this->assertEquals(
+ 10200, $e->getCode(), 'Improper exception code.'
+ );
+ }
}
public function testSilentHost()
@@ -215,6 +281,21 @@ public function testSilentHost()
$this->assertEquals(7, $e->getCode());
$this->assertEquals(10060, $e->getSocketErrorNumber());
}
+
+ try {
+ $routerOS = new Client(
+ \HOSTNAME_SILENT, USERNAME, PASSWORD, PORT, true
+ );
+
+ $this->fail(
+ 'No proper connection over hostname "'
+ . \HOSTNAME_SILENT
+ . '" should be available.'
+ );
+ } catch (T\SocketException $e) {
+ $this->assertEquals(7, $e->getCode());
+ $this->assertEquals(10060, $e->getSocketErrorNumber());
+ }
}
public function testInvalidPort()
@@ -232,6 +313,22 @@ public function testInvalidPort()
10200, $e->getCode(), 'Improper exception code.'
);
}
+
+ try {
+ $routerOS = new Client(
+ \HOSTNAME, USERNAME, PASSWORD, PORT_INVALID, true
+ );
+
+ $this->fail(
+ 'No proper connection over port "'
+ . PORT_INVALID
+ . '" should be available.'
+ );
+ } catch (SocketException $e) {
+ $this->assertEquals(
+ 10200, $e->getCode(), 'Improper exception code.'
+ );
+ }
}
public function testSilentPort()
@@ -248,6 +345,21 @@ public function testSilentPort()
$this->assertEquals(7, $e->getCode());
$this->assertEquals(10061, $e->getSocketErrorNumber());
}
+
+ try {
+ $routerOS = new Client(
+ \HOSTNAME, USERNAME, PASSWORD, PORT_SILENT, true
+ );
+
+ $this->fail(
+ 'No proper connection over port "'
+ . PORT_SILENT
+ . '" should be available.'
+ );
+ } catch (T\SocketException $e) {
+ $this->assertEquals(7, $e->getCode());
+ $this->assertEquals(10061, $e->getSocketErrorNumber());
+ }
}
public function testInvalidTimeout()

0 comments on commit 36b4aea

Please sign in to comment.