summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristoph Burschka2013-12-30 13:13:33 +0100
committerChristoph Burschka2013-12-30 13:13:33 +0100
commit0c59589d478b11fbc9ae71391e7754c00ae2427b (patch)
tree8a022168c31fdb1f2db022e9a491cc949416dc4b
parentOnly make exceptions for those plugins that actually duplicate code. (diff)
downloadejabberd-auth-php-0c59589d478b11fbc9ae71391e7754c00ae2427b.tar.gz
Rewrote Unit tests to explain successes and failures more clearly.
-rwxr-xr-xtests/test11
-rwxr-xr-x[-rw-r--r--]tests/test.php38
-rw-r--r--tests/test.successfulbin32 -> 0 bytes
-rw-r--r--tests/unit_test.php85
4 files changed, 100 insertions, 34 deletions
diff --git a/tests/test b/tests/test
deleted file mode 100755
index 01662c6..0000000
--- a/tests/test
+++ /dev/null
@@ -1,11 +0,0 @@
-p=`dirname $0`
-php $p/test.php > $p/test.in
-php $p/../main.php <$p/test.in>$p/test.out
-if [ -z "$(diff $p/test.out $p/test.successful)" ]
-then
- echo "Test successful."
- rm $p/test.in $p/test.out
-else
- echo "Test unsuccessful. Check activity logs."
-fi
-
diff --git a/tests/test.php b/tests/test.php
index ec6d35b..91b2dbb 100644..100755
--- a/tests/test.php
+++ b/tests/test.php
@@ -1,26 +1,18 @@
+#!/usr/bin/env php
<?php
-$stderr = fopen('php://stderr', 'w');
-$in = fopen('php://stdin', 'r');
-fwrite($stderr, "Enter a valid username: ");
-$user = trim(fgets($in));
-fwrite($stderr, "Enter the password: ");
-$password = trim(fgets($in));
-$str = array(
- array('isuser', $user, 'localhost'),
- array('isuser', '123456789', 'localhost'),
- array('auth', $user, 'localhost', $password),
- array('auth', $user, 'localhost', '123456789'),
-
- // These should all fail cleanly.
- array('setpass', '123456789', 'localhost', '123456789'),
- array('tryregister', '123456789', 'localhost', '123456789'),
- array('removeuser', '123456789', 'localhost', '123456789'),
- array('removeuser3', '123456789', 'localhost', '123456789'),
-);
-
-foreach ($str as $command) {
- $command = implode(':', $command);
- print pack('n', strlen($command));
- print $command;
+if ($_SERVER['argc'] != 3) {
+ die(
+'Usage:
+ test.php <valid-user> <valid-pass>
+');
}
+
+$valid_account = [
+ 'user' => $_SERVER['argv'][1],
+ 'password' => $_SERVER['argv'][2],
+];
+
+require_once __DIR__ . '/unit_test.php';
+
+(new UnitTest($valid_account))->run();
diff --git a/tests/test.successful b/tests/test.successful
deleted file mode 100644
index 128aea6..0000000
--- a/tests/test.successful
+++ /dev/null
Binary files differ
diff --git a/tests/unit_test.php b/tests/unit_test.php
new file mode 100644
index 0000000..86fff61
--- /dev/null
+++ b/tests/unit_test.php
@@ -0,0 +1,85 @@
+<?php
+
+class UnitTest {
+ function __construct($valid_account) {
+ $spec = [
+ 0 => ['pipe', 'r'], // stdin is a pipe that the child will read from
+ 1 => ['pipe', 'w'], // stdout is a pipe that the child will write to
+ 2 => STDERR, // forward stderr directly
+ ];
+ $cmd = __DIR__ . '/../main.php';
+ $this->_process = proc_open($cmd, $spec, $this->pipes);
+ $this->_input = $this->pipes[0];
+ $this->_output = $this->pipes[1];
+ $this->valid = $valid_account;
+ $this->successes = 0;
+ $this->failures = 0;
+ $this->cases = 0;
+ }
+
+ function run() {
+ foreach (get_class_methods($this) as $method) {
+ if (strpos($method, 'Test') === 0) {
+ $this->$method();
+ }
+ }
+ printf("%d tests, %d passed, %d failed\n", $this->cases, $this->successes, $this->failures);
+ }
+
+ function _send_request($request) {
+ $request = implode(':', $request);
+ fwrite($this->_input, pack('n', strlen($request)));
+ fwrite($this->_input, $request);
+ $result = unpack('n2x', fread($this->_output, 4));
+ return [$result['x1'], $result['x2']];
+ }
+
+ function assert($request, $success, $comment) {
+ $result = $this->_send_request($request);
+ if ($result[0] == 2 and $result[1] == $success) {
+ $this->successes++;
+ printf("\033[0;32mPASS #%d: %s\033[0m\n", 1+$this->cases, $comment);
+ }
+ else {
+ $this->failures++;
+ printf("\033[0;31mFAIL #%d: %s\033[0m\n", 1+$this->cases, $comment);
+ }
+ $this->cases++;
+ }
+
+ function TestUserGood() {
+ $this->assert(['isuser', $this->valid['user'], 'localhost'], TRUE, 'isuser with valid username');
+ }
+
+ function TestUserBad() {
+ $this->assert(['isuser', '123456789', 'localhost'], FALSE, 'isuser with bad username');
+ }
+
+ function TestAuthGood() {
+ $this->assert(['auth', $this->valid['user'], 'localhost', $this->valid['password']], TRUE, 'auth with valid password');
+ }
+
+ function TestAuthBadUser() {
+ $this->assert(['auth', '123456789', 'localhost', '123456789'], FALSE, 'auth with bad username');
+ }
+
+ function TestAuthBadPass() {
+ $this->assert(['auth', $this->valid['user'], 'localhost', '123456789'], FALSE, 'auth with bad password');
+ }
+
+ function TestSetPass() {
+ $this->assert(['setpass', '123456789', 'localhost', '123456789'], FALSE, 'attempt to set password (fail)');
+ }
+
+ function TestRegister() {
+ $this->assert(['tryregister', '123456789', 'localhost', '123456789'], FALSE, 'attempt to create account (fail)');
+ }
+
+ function TestRemove() {
+ $this->assert(['removeuser', '123456789', 'localhost', '123456789'], FALSE, 'attempt to delete account (fail)');
+ }
+
+ function TestRemove3() {
+ $this->assert(['removeuser3', '123456789', 'localhost', '123456789'], FALSE, 'attempt to login and delete account (fail)');
+ }
+}