From bd31680f7c0fe281938452e939682c69689a347d Mon Sep 17 00:00:00 2001 From: Paul Rock Date: Fri, 24 Aug 2018 23:40:24 +0300 Subject: [PATCH] new test was added --- .travis.yml | 8 ++-- composer.json | 4 +- examples/system_package_print.php | 2 +- preconf.sh | 3 -- preconf.tcl | 23 ++++++++++ src/Client.php | 31 +++++--------- src/Exceptions/Exception.php | 10 ----- src/Interfaces/ClientInterface.php | 9 +--- tests/ClientTest.php | 86 ++++++++++++++++++++++++++++++++++++++ tests/ConfigTest.php | 15 +++---- 10 files changed, 135 insertions(+), 56 deletions(-) delete mode 100755 preconf.sh create mode 100755 preconf.tcl create mode 100644 tests/ClientTest.php diff --git a/.travis.yml b/.travis.yml index f3483b5..95b3186 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,12 +21,12 @@ before_script: - sudo apt-get install -y expect - docker pull evilfreelancer/docker-routeros:6.42 - docker pull evilfreelancer/docker-routeros:latest -- docker run -d -p 12222:22 -p 18728:8728 -p 18729:8729 -ti evilfreelancer/docker-routeros:6.42 -- docker run -d -p 2222:22 -p 8728:8728 -p 8729:8729 -ti evilfreelancer/docker-routeros:latest +- docker run -d -p 12223:23 -p 18728:8728 -p 18729:8729 -ti evilfreelancer/docker-routeros:6.42 +- docker run -d -p 2223:23 -p 8728:8728 -p 8729:8729 -ti evilfreelancer/docker-routeros:latest - docker ps -a - sleep 30 -- ./preconfig.sh 12222 || true -- ./preconfig.sh 2222 || true +- ./preconf.tcl 12223 > /dev/null || true +- ./preconf.tcl 2223 > /dev/null || true - composer self-update - composer install --prefer-source --no-interaction --dev diff --git a/composer.json b/composer.json index 1579358..69c7bf2 100644 --- a/composer.json +++ b/composer.json @@ -30,6 +30,8 @@ "ext-sockets": "*" }, "require-dev": { - "phpunit/phpunit": "^6.0" + "phpunit/phpunit": "^6.0", + "graze/telnet-client": "^2.1", + "miyahan/telnet": "^1.1" } } diff --git a/examples/system_package_print.php b/examples/system_package_print.php index 074679a..b8e7a8b 100644 --- a/examples/system_package_print.php +++ b/examples/system_package_print.php @@ -10,7 +10,7 @@ use \RouterOS\Query; // Create config object with parameters $config = (new Config()) - ->set('host', '192.168.1.3') + ->set('host', '192.168.1.31') ->set('user', 'admin') ->set('pass', 'admin'); diff --git a/preconf.sh b/preconf.sh deleted file mode 100755 index f915620..0000000 --- a/preconf.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash - -ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=nos -p "$1" -l admin localhost "/user set admin password=admin; /quit;" -p "$1" diff --git a/preconf.tcl b/preconf.tcl new file mode 100755 index 0000000..6789196 --- /dev/null +++ b/preconf.tcl @@ -0,0 +1,23 @@ +#!/usr/bin/expect + +set timeout 10 + +set port [lindex $argv 0] + +spawn telnet localhost $port + +expect "Login: " +send "admin+c\n" +expect "Password: " +send "\n" +expect "]:" +send "n\r\n" +expect ">\ " +send "?\r\n" +expect ">\ " +send "\r\n" +expect ">\ " +send "/user set admin password=admin\r\n" +expect ">" +send "quit\n" +expect eof diff --git a/src/Client.php b/src/Client.php index 18763fe..b08f5f5 100644 --- a/src/Client.php +++ b/src/Client.php @@ -3,10 +3,9 @@ namespace RouterOS; use RouterOS\Exceptions\ClientException; -use RouterOS\Exceptions\ConfigException; -use RouterOS\Exceptions\Exception; use RouterOS\Interfaces\ClientInterface; use RouterOS\Interfaces\ConfigInterface; +use RouterOS\Exceptions\ConfigException; use RouterOS\Interfaces\QueryInterface; /** @@ -310,7 +309,7 @@ class Client implements Interfaces\ClientInterface * @return bool * @throws ClientException */ - public function connect(): bool + private function connect(): bool { // By default we not connected $connected = false; @@ -345,37 +344,29 @@ class Client implements Interfaces\ClientInterface /** * Save socket resource to static variable * - * @param resource|null $socket - * @return bool + * @param resource $socket */ - private function setSocket($socket): bool + private function setSocket($socket) { - if (\is_resource($socket)) { - $this->_socket = $socket; - return true; - } - return false; + $this->_socket = $socket; } /** * Return socket resource if is exist * - * @return bool|resource + * @return resource */ public function getSocket() { - return \is_resource($this->_socket) - ? $this->_socket - : false; + return $this->_socket; } /** * Initiate socket session * - * @return bool * @throws ClientException */ - private function openSocket(): bool + private function openSocket() { // Default: Context for ssl $context = stream_context_create([ @@ -390,7 +381,7 @@ class Client implements Interfaces\ClientInterface $proto = $this->config('ssl') ? 'ssl://' : ''; // Initiate socket client - $socket = stream_socket_client( + $socket = @stream_socket_client( $proto . $this->config('host') . ':' . $this->config('port'), $this->_socket_err_num, $this->_socket_err_str, @@ -400,8 +391,8 @@ class Client implements Interfaces\ClientInterface ); // Throw error is socket is not initiated - if (false === $socket) { - throw new ClientException('stream_socket_client() failed: code: ' . $this->_socket_err_num . ' reason: ' . $this->_socket_err_str); + if (!$socket) { + throw new ClientException('Unable to establish socket session, ' . $this->_socket_err_str); } // Save socket to static variable diff --git a/src/Exceptions/Exception.php b/src/Exceptions/Exception.php index e0cae14..401b664 100644 --- a/src/Exceptions/Exception.php +++ b/src/Exceptions/Exception.php @@ -9,14 +9,4 @@ namespace RouterOS\Exceptions; */ class Exception extends \Exception { - public function __construct(string $message = '', int $code = 0, \Throwable $previous = null) - { - parent::__construct($message, $code, $previous); - - error_log( - 'Uncaught Error: ' . $this->getMessage() . ' in ' . $this->getFile() . ':' . $this->getLine() . "\n" - . "Stack trace:\n" . $this->getTraceAsString() . "\n" - . ' thrown in ' . $this->getFile() . ' on line ' . $this->getLine() - ); - } } diff --git a/src/Interfaces/ClientInterface.php b/src/Interfaces/ClientInterface.php index 84bedad..c648d36 100644 --- a/src/Interfaces/ClientInterface.php +++ b/src/Interfaces/ClientInterface.php @@ -47,18 +47,11 @@ interface ClientInterface /** * Return socket resource if is exist * - * @return bool|resource + * @return resource */ public function getSocket(); /** - * Connect to socket server - * - * @return bool - */ - public function connect(): bool; - - /** * Read answer from server after query was executed * * @param bool $parse diff --git a/tests/ClientTest.php b/tests/ClientTest.php new file mode 100644 index 0000000..4ff573a --- /dev/null +++ b/tests/ClientTest.php @@ -0,0 +1,86 @@ +set('user', 'admin')->set('pass', 'admin')->set('host', '127.0.0.1'); + $obj = new Client($config); + $this->assertInternalType('object', $obj); + $socket = $obj->getSocket(); + $this->assertInternalType('resource', $socket); + } catch (\Exception $e) { + $this->assertContains('Must be initialized ', $e->getMessage()); + } + } + +// public function test__constructLegacy() +// { +// try { +// $config = new Config(); +// $config->set('user', 'admin')->set('pass', 'admin') +// ->set('host', '127.0.0.1')->set('port', 18728)->set('legacy', true); +// $obj = new Client($config); +// $this->assertInternalType('object', $obj); +// } catch (\Exception $e) { +// $this->assertContains('Must be initialized ', $e->getMessage()); +// } +// } + + public function test__constructWrongPass() + { + $this->expectException(ClientException::class); + + $config = (new Config())->set('attempts', 2); + $config->set('user', 'admin')->set('pass', 'admin2')->set('host', '127.0.0.1'); + $obj = new Client($config); + } + + /** + * @expectedException ClientException + */ + public function test__constructWrongNet() + { + $this->expectException(ClientException::class); + + $config = new Config(); + $config->set('user', 'admin')->set('pass', 'admin')->set('host', '127.0.0.1')->set('port', 11111); + $obj = new Client($config); + } + + public function testWriteRead() + { + $config = new Config(); + $config->set('user', 'admin')->set('pass', 'admin')->set('host', '127.0.0.1'); + $obj = new Client($config); + + $query = new Query('/ip/address/print'); + $readRaw = $obj->write($query)->read(false); + $this->assertCount(10, $readRaw); + $this->assertEquals('=.id=*1', $readRaw[1]); + + $query = new Query('/interface/getall'); + $read = $obj->write($query)->read(); + $this->assertCount(1, $read); + $this->assertEquals('*1', $read[0]['.id']); + + $query = new Query('/interface'); + $readTrap = $obj->write($query)->read(); + $this->assertCount(3, $readTrap); + $this->assertEquals('!trap', $readTrap[0]); + } + +} diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 8be748b..dad884b 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -4,7 +4,7 @@ namespace RouterOS\Tests; use PHPUnit\Framework\TestCase; use RouterOS\Config; -use RouterOS\Exceptions\Exception; +use RouterOS\Exceptions\ConfigException; class ConfigTest extends TestCase { @@ -51,18 +51,15 @@ class ConfigTest extends TestCase public function testDeleteEx() { - $this->expectException(Exception::class); + $this->expectException(ConfigException::class); $obj = new Config(); $obj->delete('wrong'); } - /** - * @throws Exception - */ public function testSetEx1() { - $this->expectException(Exception::class); + $this->expectException(ConfigException::class); $obj = new Config(); $obj->set('delay', 'some string'); @@ -70,7 +67,7 @@ class ConfigTest extends TestCase public function testSetEx2() { - $this->expectException(Exception::class); + $this->expectException(ConfigException::class); $obj = new Config(); $obj->set('wrong', 'some string'); @@ -98,9 +95,9 @@ class ConfigTest extends TestCase public function testGetEx() { - $this->expectException(Exception::class); + $this->expectException(ConfigException::class); $obj = new Config(); - $test = $obj->get('wrong'); + $obj->get('wrong'); } }