1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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'], $this->valid['domain']], TRUE, 'isuser with valid username');
}
function TestUserBad() {
$this->assert(['isuser', '123456789', $this->valid['domain']], FALSE, 'isuser with bad username');
}
function TestAuthGood() {
$this->assert(['auth', $this->valid['user'], $this->valid['domain'], $this->valid['password']], TRUE, 'auth with valid password');
}
function TestAuthBadUser() {
$this->assert(['auth', '123456789', $this->valid['domain'], '123456789'], FALSE, 'auth with bad username');
}
function TestAuthBadPass() {
$this->assert(['auth', $this->valid['user'], $this->valid['domain'], '123456789'], FALSE, 'auth with bad password');
}
function TestSetPass() {
$this->assert(['setpass', '123456789', $this->valid['domain'], '123456789'], FALSE, 'attempt to set password (fail)');
}
function TestRegister() {
$this->assert(['tryregister', '123456789', $this->valid['domain'], '123456789'], FALSE, 'attempt to create account (fail)');
}
function TestRemove() {
$this->assert(['removeuser', '123456789', $this->valid['domain'], '123456789'], FALSE, 'attempt to delete account (fail)');
}
function TestRemove3() {
$this->assert(['removeuser3', '123456789', $this->valid['domain'], '123456789'], FALSE, 'attempt to login and delete account (fail)');
}
}
|