Skip to content

Commit d2340b5

Browse files
committed
Completely refactored the unit testing process;
Added docs for $crypto; Improved performance of Util::escapeString() by making the callback operate on sequences of non-alphanumeric characters instead of one-by-one such characters;
1 parent 5ee9c8b commit d2340b5

39 files changed

+974
-254
lines changed

src/PEAR2/Net/RouterOS/Client.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
*/
2626
use PEAR2\Net\Transmitter\Stream as S;
2727

28+
/**
29+
* Refers to the cryptography constants.
30+
*/
31+
use PEAR2\Net\Transmitter\NetworkStream AS N;
32+
2833
/**
2934
* A RouterOS client.
3035
*
@@ -96,11 +101,18 @@ class Client
96101
* @param string $host Hostname (IP or domain) of the RouterOS server.
97102
* @param string $username The RouterOS username.
98103
* @param string $password The RouterOS password.
99-
* @param int $port The port on which the RouterOS server provides
100-
* the API service.
104+
* @param int|null $port The port on which the RouterOS server provides
105+
* the API service. You can also specify NULL, in which case the port
106+
* will automatically be chosen between 8728 and 8729, depending on your
107+
* encryption setting.
101108
* @param bool $persist Whether or not the connection should be a
102109
* persistent one.
103110
* @param float $timeout The timeout for the connection.
111+
* @param string $crypto The encryption for this connection. Must be one
112+
* of the PEAR2\Net\Transmitter\NetworkStream::CRYPTO_* constants. Off
113+
* by default. For the sake of simplicity, if you specify an encryption,
114+
* don't specify a context and your default context uses the value
115+
* "DEFAULT" for ciphers, "ADH" will be automatically added to the list.
104116
* @param resource $context A context for the socket.
105117
*
106118
* @see sendSync()
@@ -113,6 +125,7 @@ public function __construct(
113125
$port = 8728,
114126
$persist = false,
115127
$timeout = null,
128+
$crypto = N::CRYPTO_OFF,
116129
$context = null
117130
) {
118131
$this->com = new Communicator(
@@ -121,6 +134,7 @@ public function __construct(
121134
$persist,
122135
$timeout,
123136
$username . '/' . $password,
137+
$crypto,
124138
$context
125139
);
126140
$timeout = null == $timeout
@@ -695,7 +709,8 @@ public function close()
695709
$result = $response->getType() === Response::TYPE_FATAL;
696710
$result = $result && $this->com->close();
697711
} catch (SocketException $e) {
698-
$result = $e->getCode() === 40900;
712+
$result
713+
= $e->getCode() === SocketException::CODE_UNACCEPTING_REQEUST;
699714
if (null !== $this->registry) {
700715
$this->registry->setTaglessMode(false);
701716
}

src/PEAR2/Net/RouterOS/Communicator.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,20 @@ class Communicator
9090
* Creates a new connection with the specified options.
9191
*
9292
* @param string $host Hostname (IP or domain) of the RouterOS server.
93-
* @param int $port The port on which the RouterOS server provides
94-
* the API service.
93+
* @param int|null $port The port on which the RouterOS server provides
94+
* the API service. You can also specify NULL, in which case the port
95+
* will automatically be chosen between 8728 and 8729, depending on your
96+
* encryption setting.
9597
* @param bool $persist Whether or not the connection should be a
9698
* persistent one.
9799
* @param float $timeout The timeout for the connection.
98100
* @param string $key A string that uniquely identifies the
99101
* connection.
102+
* @param string $crypto The encryption for this connection. Must be one
103+
* of the PEAR2\Net\Transmitter\NetworkStream::CRYPTO_* constants. Off
104+
* by default. For the sake of simplicity, if you specify an encryption,
105+
* don't specify a context and your default context uses the value
106+
* "DEFAULT" for ciphers, "ADH" will be automatically added to the list.
100107
* @param resource $context A context for the socket.
101108
*
102109
* @see sendWord()
@@ -107,15 +114,31 @@ public function __construct(
107114
$persist = false,
108115
$timeout = null,
109116
$key = '',
117+
$crypto = T\NetworkStream::CRYPTO_OFF,
110118
$context = null
111119
) {
120+
$isUnencrypted = T\NetworkStream::CRYPTO_OFF === $crypto;
121+
if (($context === null) && !$isUnencrypted) {
122+
$context = stream_context_get_default();
123+
$opts = stream_context_get_options($context);
124+
if (!isset($opts['ssl']['ciphers'])
125+
|| 'DEFAULT' === $opts['ssl']['ciphers']
126+
) {
127+
stream_context_set_option($context, 'ssl', 'ciphers', 'ADH');
128+
}
129+
}
130+
if (null === $port) {
131+
$port = $isUnencrypted ? 8728 : 8729;
132+
}
133+
112134
try {
113135
$this->trans = new T\TcpClient(
114136
$host,
115137
$port,
116138
$persist,
117139
$timeout,
118140
$key,
141+
$crypto,
119142
$context
120143
);
121144
} catch (T\Exception $e) {

src/PEAR2/Net/RouterOS/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class Response extends Message
7878
* @param int $timeout_s If a response is not immediatly
7979
* available, wait this many seconds. If NULL, wait indefinetly.
8080
* @param int $timeout_us Microseconds to add to the waiting time.
81-
* @param Registry $reg An optional registry to sync the
81+
* @param Registry $reg An optional registry to sync the
8282
* response with.
8383
*
8484
* @see getType()

src/PEAR2/Net/RouterOS/Util.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ public static function escapeString($value)
151151
* Escapes a character for a RouterOS scripting context. Intended to only be
152152
* called for non-alphanumeric characters.
153153
*
154-
* @param string $char The character to be escaped.
154+
* @param string $chars The matches array, expected to contain exactly one
155+
* member, in which is the whole string to be escaped.
155156
*
156157
* @return string The escaped character.
157158
*/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace PEAR2\Net\RouterOS\Client\Test\Safe\NonPersistent;
4+
5+
use PEAR2\Net\RouterOS\Client;
6+
use PEAR2\Net\RouterOS\Client\Test\Safe\NonPersistentTest;
7+
use PEAR2\Net\Transmitter\NetworkStream;
8+
9+
require_once __DIR__ . '/../NonPersistentTest.php';
10+
11+
/**
12+
* ~
13+
*
14+
* @group Safe
15+
* @group NonPersistent
16+
* @group Encrypted
17+
*
18+
* @category Net
19+
* @package PEAR2_Net_RouterOS
20+
* @author Vasil Rangelov <boen.robot@gmail.com>
21+
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
22+
* @link http://pear2.php.net/PEAR2_Net_RouterOS
23+
*/
24+
class EncryptedTest extends NonPersistentTest
25+
{
26+
protected function setUp()
27+
{
28+
$this->object = new Client(
29+
\HOSTNAME,
30+
USERNAME,
31+
PASSWORD,
32+
ENC_PORT,
33+
false,
34+
null,
35+
NetworkStream::CRYPTO_TLS
36+
);
37+
}
38+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace PEAR2\Net\RouterOS\Client\Test\Safe\NonPersistent;
4+
5+
use PEAR2\Net\RouterOS\Client;
6+
use PEAR2\Net\RouterOS\Client\Test\Safe\NonPersistentTest;
7+
8+
require_once __DIR__ . '/../NonPersistentTest.php';
9+
10+
/**
11+
* ~
12+
*
13+
* @group Safe
14+
* @group NonPersistent
15+
* @group Unencrypted
16+
*
17+
* @category Net
18+
* @package PEAR2_Net_RouterOS
19+
* @author Vasil Rangelov <boen.robot@gmail.com>
20+
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
21+
* @link http://pear2.php.net/PEAR2_Net_RouterOS
22+
*/
23+
class UnencryptedTest extends NonPersistentTest
24+
{
25+
protected function setUp()
26+
{
27+
$this->object = new Client(\HOSTNAME, USERNAME, PASSWORD, PORT);
28+
}
29+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace PEAR2\Net\RouterOS\Client\Test\Safe;
4+
5+
use PEAR2\Net\RouterOS\Client\Test\SafeTest;
6+
7+
require_once __DIR__ . '/../SafeTest.php';
8+
9+
abstract class NonPersistentTest extends SafeTest
10+
{
11+
protected function tearDown()
12+
{
13+
unset($this->object);
14+
}
15+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace PEAR2\Net\RouterOS\Client\Test\Safe\Persistent;
4+
5+
use PEAR2\Net\RouterOS\Client;
6+
use PEAR2\Net\RouterOS\Client\Test\Safe\PersistentTest;
7+
use PEAR2\Net\Transmitter\NetworkStream;
8+
9+
require_once __DIR__ . '/../PersistentTest.php';
10+
11+
/**
12+
* ~
13+
*
14+
* @group Safe
15+
* @group Persistent
16+
* @group Encrypted
17+
*
18+
* @category Net
19+
* @package PEAR2_Net_RouterOS
20+
* @author Vasil Rangelov <boen.robot@gmail.com>
21+
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
22+
* @link http://pear2.php.net/PEAR2_Net_RouterOS
23+
*/
24+
class EncryptedTest extends PersistentTest
25+
{
26+
protected function setUp()
27+
{
28+
$this->object = new Client(
29+
\HOSTNAME,
30+
USERNAME,
31+
PASSWORD,
32+
ENC_PORT,
33+
true,
34+
null,
35+
NetworkStream::CRYPTO_TLS
36+
);
37+
}
38+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace PEAR2\Net\RouterOS\Client\Test\Safe\Persistent;
4+
5+
use PEAR2\Net\RouterOS\Client;
6+
use PEAR2\Net\RouterOS\Client\Test\Safe\PersistentTest;
7+
8+
require_once __DIR__ . '/../PersistentTest.php';
9+
10+
/**
11+
* ~
12+
*
13+
* @group Safe
14+
* @group Persistent
15+
* @group Unencrypted
16+
*
17+
* @category Net
18+
* @package PEAR2_Net_RouterOS
19+
* @author Vasil Rangelov <boen.robot@gmail.com>
20+
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
21+
* @link http://pear2.php.net/PEAR2_Net_RouterOS
22+
*/
23+
class UnencryptedTest extends PersistentTest
24+
{
25+
protected function setUp()
26+
{
27+
$this->object = new Client(\HOSTNAME, USERNAME, PASSWORD, PORT, true);
28+
}
29+
}

tests/ClientPersistentFeaturesTest.php renamed to tests/Client/Safe/PersistentTest.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
<?php
2-
namespace PEAR2\Net\RouterOS;
32

4-
require_once 'ClientFeaturesTest.php';
3+
namespace PEAR2\Net\RouterOS\Client\Test\Safe;
54

6-
class ClientPersistentFeaturesTest extends ClientFeaturesTest
5+
use PEAR2\Net\RouterOS\Client;
6+
use PEAR2\Net\RouterOS\Client\Test;
7+
use PEAR2\Net\RouterOS\Client\Test\SafeTest;
8+
use PEAR2\Net\RouterOS\Request;
9+
10+
require_once __DIR__ . '/../SafeTest.php';
11+
12+
abstract class PersistentTest extends SafeTest
713
{
814

9-
protected function setUp()
10-
{
11-
$this->object = new Client(\HOSTNAME, USERNAME, PASSWORD, PORT, true);
12-
}
13-
1415
protected function tearDown()
1516
{
1617
$this->object->close();
@@ -21,7 +22,7 @@ public function testCancellingSeparation()
2122
{
2223
$client = new Client(\HOSTNAME, USERNAME, PASSWORD, PORT, true);
2324
$pingRequest = new Request('/ping', null, 'ping');
24-
$pingRequest->setArgument('address', HOSTNAME);
25+
$pingRequest->setArgument('address', Test\HOSTNAME);
2526
$this->object->sendAsync($pingRequest);
2627
$client->sendAsync($pingRequest);
2728
$client->loop(2);

0 commit comments

Comments
 (0)