summaryrefslogtreecommitdiff
path: root/libs/jsonrpc/tests/Validator
diff options
context:
space:
mode:
Diffstat (limited to 'libs/jsonrpc/tests/Validator')
-rw-r--r--libs/jsonrpc/tests/Validator/HostValidatorTest.php32
-rw-r--r--libs/jsonrpc/tests/Validator/JsonEncodingValidatorTest.php22
-rw-r--r--libs/jsonrpc/tests/Validator/JsonFormatValidatorTest.php19
-rw-r--r--libs/jsonrpc/tests/Validator/RpcFormatValidatorTest.php48
-rw-r--r--libs/jsonrpc/tests/Validator/UserValidatorTest.php24
5 files changed, 145 insertions, 0 deletions
diff --git a/libs/jsonrpc/tests/Validator/HostValidatorTest.php b/libs/jsonrpc/tests/Validator/HostValidatorTest.php
new file mode 100644
index 00000000..a5fed7e0
--- /dev/null
+++ b/libs/jsonrpc/tests/Validator/HostValidatorTest.php
@@ -0,0 +1,32 @@
+<?php
+
+use JsonRPC\Validator\HostValidator;
+
+require_once __DIR__.'/../../../../vendor/autoload.php';
+
+class HostValidatorTest extends PHPUnit_Framework_TestCase
+{
+ public function testWithEmptyHosts()
+ {
+ $this->assertNull(HostValidator::validate(array(), '127.0.0.1', '127.0.0.1'));
+ }
+
+ public function testWithValidHosts()
+ {
+ $this->assertNull(HostValidator::validate(array('127.0.0.1'), '127.0.0.1', '127.0.0.1'));
+ }
+
+ public function testWithValidNetwork()
+ {
+ $this->assertNull(HostValidator::validate(array('192.168.10.1/24'), '192.168.10.1'),'test ip match');
+ $this->assertNull(HostValidator::validate(array('192.168.10.1/24'), '192.168.10.250'),'test ip match');
+ $this->setExpectedException('\JsonRPC\Exception\AccessDeniedException');
+ HostValidator::validate(array('192.168.10.1/24'), '192.168.11.1');
+ }
+
+ public function testWithNotAuthorizedHosts()
+ {
+ $this->setExpectedException('\JsonRPC\Exception\AccessDeniedException');
+ HostValidator::validate(array('192.168.1.1'), '127.0.0.1', '127.0.0.1');
+ }
+}
diff --git a/libs/jsonrpc/tests/Validator/JsonEncodingValidatorTest.php b/libs/jsonrpc/tests/Validator/JsonEncodingValidatorTest.php
new file mode 100644
index 00000000..a1b2b80e
--- /dev/null
+++ b/libs/jsonrpc/tests/Validator/JsonEncodingValidatorTest.php
@@ -0,0 +1,22 @@
+<?php
+
+use JsonRPC\Validator\JsonEncodingValidator;
+
+require_once __DIR__.'/../../../../vendor/autoload.php';
+
+class JsonEncodingValidatorTest extends PHPUnit_Framework_TestCase
+{
+ public function testWithValidJson()
+ {
+ json_encode('{"foo": "bar"}');
+ $this->assertNull(JsonEncodingValidator::validate());
+ }
+
+ public function testWithJsonError()
+ {
+ json_encode("\xB1\x31");
+
+ $this->setExpectedException('\JsonRPC\Exception\ResponseEncodingFailureException');
+ JsonEncodingValidator::validate();
+ }
+}
diff --git a/libs/jsonrpc/tests/Validator/JsonFormatValidatorTest.php b/libs/jsonrpc/tests/Validator/JsonFormatValidatorTest.php
new file mode 100644
index 00000000..a838ada9
--- /dev/null
+++ b/libs/jsonrpc/tests/Validator/JsonFormatValidatorTest.php
@@ -0,0 +1,19 @@
+<?php
+
+use JsonRPC\Validator\JsonFormatValidator;
+
+require_once __DIR__.'/../../../../vendor/autoload.php';
+
+class JsonFormatValidatorTest extends PHPUnit_Framework_TestCase
+{
+ public function testJsonParsedCorrectly()
+ {
+ $this->assertNull(JsonFormatValidator::validate(array('foobar')));
+ }
+
+ public function testJsonNotParsedCorrectly()
+ {
+ $this->setExpectedException('\JsonRPC\Exception\InvalidJsonFormatException');
+ JsonFormatValidator::validate('');
+ }
+}
diff --git a/libs/jsonrpc/tests/Validator/RpcFormatValidatorTest.php b/libs/jsonrpc/tests/Validator/RpcFormatValidatorTest.php
new file mode 100644
index 00000000..3e6ba8bc
--- /dev/null
+++ b/libs/jsonrpc/tests/Validator/RpcFormatValidatorTest.php
@@ -0,0 +1,48 @@
+<?php
+
+use JsonRPC\Validator\RpcFormatValidator;
+
+require_once __DIR__.'/../../../../vendor/autoload.php';
+
+class RpcFormatValidatorTest extends PHPUnit_Framework_TestCase
+{
+ public function testWithMinimumRequirement()
+ {
+ $this->assertNull(RpcFormatValidator::validate(array('jsonrpc' => '2.0', 'method' => 'foobar')));
+ }
+
+ public function testWithNoVersion()
+ {
+ $this->setExpectedException('\JsonRPC\Exception\InvalidJsonRpcFormatException');
+ RpcFormatValidator::validate(array('method' => 'foobar'));
+ }
+
+ public function testWithNoMethod()
+ {
+ $this->setExpectedException('\JsonRPC\Exception\InvalidJsonRpcFormatException');
+ RpcFormatValidator::validate(array('jsonrpc' => '2.0'));
+ }
+
+ public function testWithMethodNotString()
+ {
+ $this->setExpectedException('\JsonRPC\Exception\InvalidJsonRpcFormatException');
+ RpcFormatValidator::validate(array('jsonrpc' => '2.0', 'method' => array()));
+ }
+
+ public function testWithBadVersion()
+ {
+ $this->setExpectedException('\JsonRPC\Exception\InvalidJsonRpcFormatException');
+ RpcFormatValidator::validate(array('jsonrpc' => '1.0', 'method' => 'abc'));
+ }
+
+ public function testWithBadParams()
+ {
+ $this->setExpectedException('\JsonRPC\Exception\InvalidJsonRpcFormatException');
+ RpcFormatValidator::validate(array('jsonrpc' => '2.0', 'method' => 'abc', 'params' => 'foobar'));
+ }
+
+ public function testWithParams()
+ {
+ $this->assertNull(RpcFormatValidator::validate(array('jsonrpc' => '2.0', 'method' => 'abc', 'params' => array(1, 2))));
+ }
+}
diff --git a/libs/jsonrpc/tests/Validator/UserValidatorTest.php b/libs/jsonrpc/tests/Validator/UserValidatorTest.php
new file mode 100644
index 00000000..e514c105
--- /dev/null
+++ b/libs/jsonrpc/tests/Validator/UserValidatorTest.php
@@ -0,0 +1,24 @@
+<?php
+
+use JsonRPC\Validator\UserValidator;
+
+require_once __DIR__.'/../../../../vendor/autoload.php';
+
+class UserValidatorTest extends PHPUnit_Framework_TestCase
+{
+ public function testWithEmptyHosts()
+ {
+ $this->assertNull(UserValidator::validate(array(), 'user', 'pass'));
+ }
+
+ public function testWithValidHosts()
+ {
+ $this->assertNull(UserValidator::validate(array('user' => 'pass'), 'user', 'pass'));
+ }
+
+ public function testWithNotAuthorizedHosts()
+ {
+ $this->setExpectedException('\JsonRPC\Exception\AuthenticationFailureException');
+ UserValidator::validate(array('user' => 'pass'), 'user', 'wrong password');
+ }
+}