Permalink
Browse files

Added support for non ASCII passwords.

  • Loading branch information...
boenrobot committed Nov 6, 2011
1 parent 2960aaf commit c55a38c89c64addb8f7481274cd36f95d2a8d819
Showing with 72 additions and 24 deletions.
  1. +10 −0 src/PEAR2/Net/RouterOS/Client.php
  2. +24 −24 tests/ClientFeaturesTest.php
  3. +21 −0 tests/ConnectionTest.php
  4. +17 −0 tests/phpunit.xml
@@ -151,6 +151,16 @@ public function __invoke($arg = null)
*/
public static function login(Communicator $com, $username, $password = '')
{
+ if (null !== ($remoteCharset = $com->getCharset($com::CHARSET_REMOTE))
+ && null !== ($localCharset = $com->getCharset($com::CHARSET_LOCAL))
+ ) {
+ $password = iconv(
+ $localCharset,
+ $remoteCharset . '//IGNORE//TRANSLIT',
+ $password
+ );
+ }
+
try {
$request = new Request('/login');
$request->send($com);
@@ -120,30 +120,6 @@ public function testSendAsyncUniqueTagRequirement()
$this->assertEquals(103, $e->getCode(), 'Improper exception code.');
}
}
-
- public function testClientInvokability()
- {
- $obj = $this->object;
- $this->assertEquals(0, $obj->getPendingRequestsCount());
- $obj(new Request('/ping address=' . HOSTNAME, 'ping'));
- $this->assertEquals(1, $obj->getPendingRequestsCount());
- $obj(new Request('/ip/arp/print', 'arp'));
- $this->assertEquals(2, $obj->getPendingRequestsCount());
- $obj(4);
- $pingResponses = $obj->extractNewResponses('ping');
- $this->assertGreaterThan(0, count($pingResponses));
- $obj->cancelRequest('ping');
- $arpResponses1 = $obj('arp');
- $this->assertEquals(0, $obj->getPendingRequestsCount());
- $this->assertGreaterThan(0, count($arpResponses1));
- $obj(new Request('/ip/arp/print', 'arp'));
- $this->assertEquals(1, $obj->getPendingRequestsCount());
- $obj();
- $arpResponses2 = $obj('arp');
- $this->assertGreaterThan(0, count($arpResponses2));
- $this->assertEquals(count($arpResponses1), count($arpResponses2));
- $this->assertInstanceOf(__NAMESPACE__ . '\Response', $arpResponses1(0));
- }
public function testSendAsyncWithCallbackAndTempLoop()
{
@@ -553,6 +529,30 @@ public function testSendAsyncWithoutCallbackAndLoop()
);
$this->object->cancelRequest('ping');
}
+
+ public function testClientInvokability()
+ {
+ $obj = $this->object;
+ $this->assertEquals(0, $obj->getPendingRequestsCount());
+ $obj(new Request('/ping address=' . HOSTNAME, 'ping'));
+ $this->assertEquals(1, $obj->getPendingRequestsCount());
+ $obj(new Request('/ip/arp/print', 'arp'));
+ $this->assertEquals(2, $obj->getPendingRequestsCount());
+ $obj(4);
+ $pingResponses = $obj->extractNewResponses('ping');
+ $this->assertGreaterThan(0, count($pingResponses));
+ $obj->cancelRequest('ping');
+ $arpResponses1 = $obj('arp');
+ $this->assertEquals(0, $obj->getPendingRequestsCount());
+ $this->assertGreaterThan(0, count($arpResponses1));
+ $obj(new Request('/ip/arp/print', 'arp'));
+ $this->assertEquals(1, $obj->getPendingRequestsCount());
+ $obj();
+ $arpResponses2 = $obj('arp');
+ $this->assertGreaterThan(0, count($arpResponses2));
+ $this->assertEquals(count($arpResponses1), count($arpResponses2));
+ $this->assertInstanceOf(__NAMESPACE__ . '\Response', $arpResponses1(0));
+ }
public function testStreamEquality()
{
View
@@ -89,6 +89,27 @@ public function testMultiplePersistentConnection()
}
}
+ public function testNormalUnicodeConnection()
+ {
+ $oldCharsets = Communicator::setDefaultCharset(array(
+ Communicator::CHARSET_LOCAL => 'UTF-8',
+ Communicator::CHARSET_REMOTE => UNICODE_PASSWORD_CHARSET
+ ));
+ try {
+ $routerOS = new Client(
+ HOSTNAME, UNICODE_USERNAME, UNICODE_PASSWORD, PORT
+ );
+ $this->assertInstanceOf(
+ __NAMESPACE__ . '\Client', $routerOS,
+ 'Object initialization failed.'
+ );
+ Communicator::setDefaultCharset($oldCharsets);
+ } catch (Exception $e) {
+ Communicator::setDefaultCharset($oldCharsets);
+ $this->fail('Unable to connect normally:' . (string) $e);
+ }
+ }
+
public function testNormalContextConnection()
{
try {
View
@@ -58,6 +58,23 @@
<const name="USERNAME2" value="api" />
<const name="PASSWORD2" value="api" />
+ <!--
+ A RouterOS username and password that uses non ASCII characters.
+ Does not need to have any privileges beyond accessing the router.
+
+ RouterOS forbids non ASCII usernames, but does not forbid non ASCII
+ passwords, which is why these credentials are required for a special
+ test.
+ -->
+ <const name="UNICODE_USERNAME" value="api-unicode" />
+ <const name="UNICODE_PASSWORD" value="апи" />
+
+ <!--
+ The ANSI charset of the password. Should match the charset for the
+ locale of the device the password was defined from.
+ -->
+ <const name="UNICODE_PASSWORD_CHARSET" value="windows-1251" />
+
<!--
A RouterOS username that doesn't exist.
-->

0 comments on commit c55a38c

Please sign in to comment.