Skip to content

Commit 5bcd2d2

Browse files
committed
add first batch of unit tests
1 parent 0b55556 commit 5bcd2d2

File tree

8 files changed

+390
-0
lines changed

8 files changed

+390
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":1,"defects":{"Pdsinterop\\PhpSolid\\Tests\\ClientRegistrationTest::testSimpleCreationg":8,"Pdsinterop\\PhpSolid\\ClientRegistrationTest::testSimpleCreationg":8,"Pdsinterop\\PhpSolid\\Tests\\ClientRegistrationTest::testSimpleCreation":5,"Pdsinterop\\PhpSolid\\Tests\\ClientRegistrationTest::testGetRegistration":8,"Pdsinterop\\PhpSolid\\Tests\\ClientRegistrationTest::testClientNameAutofill":7,"Pdsinterop\\PhpSolid\\Tests\\IpAttemptsTest::testGetAttemptsCountZero":8,"Pdsinterop\\PhpSolid\\Tests\\IpAttemptsTest::testGetAttemptsCountOne":8,"Pdsinterop\\PhpSolid\\Tests\\IpAttemptsTest::testGetAttemptsCountTwo":7,"Pdsinterop\\PhpSolid\\Tests\\IpAttemptsTest::testGetAttemptsCountExpired":7,"Pdsinterop\\PhpSolid\\Tests\\IpAttemptsTest::testCleanup":7,"Pdsinterop\\PhpSolid\\Tests\\UtilTest::testBase64EncodeDecode":7,"Pdsinterop\\PhpSolid\\Tests\\JtiTest::testNonExistingJti":8,"Pdsinterop\\PhpSolid\\Tests\\JtiTest::testExistingJti":8,"Pdsinterop\\PhpSolid\\Tests\\JtiTest::testExpiredJti":8,"Pdsinterop\\PhpSolid\\Tests\\JtiTest::testCleanup":8,"Pdsinterop\\PhpSolid\\Tests\\JtiStoreTest::testExpiredJti":8,"Pdsinterop\\PhpSolid\\Tests\\JtiStoreTest::testCleanup":7},"times":{"Pdsinterop\\PhpSolid\\Tests\\BaseTest::testBaseLower":0.001,"Pdsinterop\\PhpSolid\\Tests\\BaseTest::testBaseUpper":0,"Pdsinterop\\PhpSolid\\Tests\\BaseTest::testBaseNumbers":0,"Pdsinterop\\PhpSolid\\Tests\\BaseTest::testBaseSpecial":0,"Pdsinterop\\PhpSolid\\Tests\\BaseTest::testBaseUpperAndLower":0,"Pdsinterop\\PhpSolid\\Tests\\EntropyTest::testSimplyEntropy":0,"Pdsinterop\\PhpSolid\\Tests\\LengthTest::testLengthLower":0,"Pdsinterop\\PhpSolid\\Tests\\LengthTest::testLengthUpper":0,"Pdsinterop\\PhpSolid\\Tests\\LengthTest::testLengthNumbers":0,"Pdsinterop\\PhpSolid\\Tests\\LengthTest::testLengthSpecial":0,"Pdsinterop\\PhpSolid\\Tests\\LengthTest::testLengthUpperAndLower":0,"Pdsinterop\\PhpSolid\\Tests\\PasswordValidatorTest::testBaseLower":0,"Pdsinterop\\PhpSolid\\Tests\\PasswordValidatorTest::testBaseUpper":0,"Pdsinterop\\PhpSolid\\Tests\\PasswordValidatorTest::testBaseNumbers":0,"Pdsinterop\\PhpSolid\\Tests\\PasswordValidatorTest::testBaseSpecial":0,"Pdsinterop\\PhpSolid\\Tests\\PasswordValidatorTest::testBaseUpperAndLower":0,"Pdsinterop\\PhpSolid\\Tests\\PasswordValidatorTest::testLengthLower":0,"Pdsinterop\\PhpSolid\\Tests\\PasswordValidatorTest::testLengthUpper":0,"Pdsinterop\\PhpSolid\\Tests\\PasswordValidatorTest::testLengthNumbers":0,"Pdsinterop\\PhpSolid\\Tests\\PasswordValidatorTest::testLengthSpecial":0,"Pdsinterop\\PhpSolid\\Tests\\PasswordValidatorTest::testLengthUpperAndLower":0,"Pdsinterop\\PhpSolid\\Tests\\PasswordValidatorTest::testSimplyEntropy":0,"Pdsinterop\\PhpSolid\\Tests\\ClientRegistrationTest::testSimpleCreationg":0.002,"Pdsinterop\\PhpSolid\\ClientRegistrationTest::testSimpleCreationg":0.002,"Pdsinterop\\PhpSolid\\Tests\\ClientRegistrationTest::testSimpleCreation":0.001,"Pdsinterop\\PhpSolid\\Tests\\ClientRegistrationTest::testGetRegistration":0,"Pdsinterop\\PhpSolid\\Tests\\ClientRegistrationTest::testClientNameAutofill":0,"Pdsinterop\\PhpSolid\\Tests\\ClientRegistrationTest::testClientByOrigin":0,"Pdsinterop\\PhpSolid\\Tests\\ClientRegistrationTest::testClientByDuplicateOrigin":0,"Pdsinterop\\PhpSolid\\Tests\\IpAttemptsTest::testGetAttemptsCountZero":0.001,"Pdsinterop\\PhpSolid\\Tests\\IpAttemptsTest::testGetAttemptsCountOne":0,"Pdsinterop\\PhpSolid\\Tests\\IpAttemptsTest::testGetAttemptsCountTwo":0,"Pdsinterop\\PhpSolid\\Tests\\IpAttemptsTest::testGetAttemptsCountExpired":0,"Pdsinterop\\PhpSolid\\Tests\\IpAttemptsTest::testGetAttemptsCountOneExpired":0,"Pdsinterop\\PhpSolid\\Tests\\IpAttemptsTest::testCleanup":0,"Pdsinterop\\PhpSolid\\Tests\\IpAttemptsTest::testTrustedIpGetAttemptsCountTwo":0,"Pdsinterop\\PhpSolid\\Tests\\IpAttemptsTest::testTrustedIpGetAttempts":0,"Pdsinterop\\PhpSolid\\Tests\\IpAttemptsTest::testTrustedIpGetAttemptsSkipsDb":0,"Pdsinterop\\PhpSolid\\Tests\\UtilTest::testBase64EncodeDecode":0,"Pdsinterop\\PhpSolid\\Tests\\JtiTest::testNonExistingJti":0.002,"Pdsinterop\\PhpSolid\\Tests\\JtiTest::testExistingJti":0,"Pdsinterop\\PhpSolid\\Tests\\JtiTest::testExpiredJti":0,"Pdsinterop\\PhpSolid\\Tests\\JtiTest::testCleanup":0,"Pdsinterop\\PhpSolid\\Tests\\JtiStoreTest::testNonExistingJti":0,"Pdsinterop\\PhpSolid\\Tests\\JtiStoreTest::testExistingJti":0,"Pdsinterop\\PhpSolid\\Tests\\JtiStoreTest::testExpiredJti":0,"Pdsinterop\\PhpSolid\\Tests\\JtiStoreTest::testCleanup":0}}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
namespace Pdsinterop\PhpSolid\Tests;
3+
4+
require_once(__DIR__ . "/test-config.php");
5+
6+
use Pdsinterop\PhpSolid\ClientRegistration;
7+
use Pdsinterop\PhpSolid\Db;
8+
9+
class ClientRegistrationTest extends \PHPUnit\Framework\TestCase
10+
{
11+
protected function setUp(): void
12+
{
13+
$statements = [
14+
'DROP TABLE IF EXISTS clients',
15+
'CREATE TABLE clients (
16+
clientId VARCHAR(255) NOT NULL PRIMARY KEY,
17+
origin TEXT NOT NULL,
18+
clientData TEXT NOT NULL
19+
)'
20+
];
21+
22+
Db::connect();
23+
try {
24+
// create tables
25+
foreach($statements as $statement){
26+
Db::$pdo->exec($statement);
27+
}
28+
} catch(\PDOException $e) {
29+
echo $e->getMessage();
30+
}
31+
32+
ClientRegistration::saveClientRegistration([
33+
"client_id" => "12345",
34+
"origin" => "https://example.com",
35+
"client_name" => "Client name"
36+
]);
37+
38+
ClientRegistration::saveClientRegistration([
39+
"client_id" => "23456",
40+
"origin" => "https://example2.com"
41+
]);
42+
43+
ClientRegistration::saveClientRegistration([
44+
"client_id" => "34567",
45+
"origin" => "https://example2.com"
46+
]);
47+
}
48+
49+
public function testGetRegistration() {
50+
$storedData = ClientRegistration::getRegistration("12345");
51+
$this->assertEquals("12345", $storedData['client_id']);
52+
$this->assertEquals("https://example.com", $storedData['origin']);
53+
$this->assertEquals("Client name", $storedData['client_name']);
54+
}
55+
56+
public function testClientNameAutofill() {
57+
$storedData = ClientRegistration::getRegistration("23456");
58+
$this->assertEquals("23456", $storedData['client_id']);
59+
$this->assertEquals("https://example2.com", $storedData['origin']);
60+
$this->assertEquals("https://example2.com", $storedData['client_name']);
61+
}
62+
63+
public function testClientByOrigin() {
64+
$storedData = ClientRegistration::getClientByOrigin("https://example.com");
65+
$this->assertEquals("12345", $storedData['client_id']);
66+
$this->assertEquals("https://example.com", $storedData['origin']);
67+
$this->assertEquals("Client name", $storedData['client_name']);
68+
}
69+
70+
public function testClientByDuplicateOrigin() {
71+
$storedData = ClientRegistration::getClientByOrigin("https://example2.com");
72+
$this->assertFalse($storedData); // false because we have 2 clients with the same origin
73+
}
74+
}

tests/phpunit/IpAttemptsTest.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
namespace Pdsinterop\PhpSolid\Tests;
3+
4+
require_once(__DIR__ . "/test-config.php");
5+
6+
use Pdsinterop\PhpSolid\IpAttempts;
7+
use Pdsinterop\PhpSolid\Db;
8+
9+
class IpAttemptsTest extends \PHPUnit\Framework\TestCase
10+
{
11+
protected function setUp(): void
12+
{
13+
$statements = [
14+
'DROP TABLE IF EXISTS ipAttempts',
15+
'CREATE TABLE IF NOT EXISTS ipAttempts (
16+
ip VARCHAR(255) NOT NULL,
17+
type VARCHAR(255) NOT NULL,
18+
expires NOT NULL
19+
)'
20+
];
21+
22+
Db::connect();
23+
try {
24+
// create tables
25+
foreach($statements as $statement){
26+
Db::$pdo->exec($statement);
27+
}
28+
} catch(\PDOException $e) {
29+
echo $e->getMessage();
30+
}
31+
}
32+
33+
public function testGetAttemptsCountZero() {
34+
$ip = "10.0.0.1";
35+
$count = IpAttempts::getAttemptsCount($ip, "test");
36+
$this->assertEquals(0, $count);
37+
}
38+
39+
public function testGetAttemptsCountOne() {
40+
$ip = "10.0.0.1";
41+
42+
IpAttempts::logFailedAttempt($ip, "test", time() + 3600);
43+
$count = IpAttempts::getAttemptsCount($ip, "test");
44+
$this->assertEquals(1, $count);
45+
}
46+
47+
public function testGetAttemptsCountTwo() {
48+
$ip = "10.0.0.1";
49+
50+
IpAttempts::logFailedAttempt($ip, "test", time() + 3600);
51+
IpAttempts::logFailedAttempt($ip, "test", time() + 3600);
52+
53+
$count = IpAttempts::getAttemptsCount($ip, "test");
54+
$this->assertEquals(2, $count);
55+
}
56+
57+
public function testGetAttemptsCountExpired() {
58+
$ip = "10.0.0.1";
59+
60+
IpAttempts::logFailedAttempt($ip, "test", time() - 1);
61+
IpAttempts::logFailedAttempt($ip, "test", time() - 1);
62+
63+
$count = IpAttempts::getAttemptsCount($ip, "test");
64+
$this->assertEquals(0, $count);
65+
}
66+
67+
public function testGetAttemptsCountOneExpired() {
68+
$ip = "10.0.0.1";
69+
70+
IpAttempts::logFailedAttempt($ip, "test", time() + 10);
71+
IpAttempts::logFailedAttempt($ip, "test", time() - 1);
72+
73+
$count = IpAttempts::getAttemptsCount($ip, "test");
74+
$this->assertEquals(1, $count);
75+
}
76+
77+
public function testCleanup() {
78+
$ip = "10.0.0.1";
79+
80+
IpAttempts::logFailedAttempt($ip, "test", time() - 1);
81+
IpAttempts::logFailedAttempt($ip, "test", time() - 1);
82+
IpAttempts::logFailedAttempt($ip, "test", time() + 3600);
83+
IpAttempts::logFailedAttempt($ip, "test", time() + 3600);
84+
85+
$query = Db::$pdo->prepare('SELECT count(*) AS count FROM ipAttempts');
86+
$query->execute();
87+
$result = $query->fetchAll();
88+
$beforeCleanup = $result[0]['count'];
89+
90+
$this->assertEquals(4, $beforeCleanup);
91+
92+
IpAttempts::cleanupAttempts();
93+
$query = Db::$pdo->prepare('SELECT count(*) AS count FROM ipAttempts');
94+
$query->execute();
95+
$result = $query->fetchAll();
96+
$afterCleanup = $result[0]['count'];
97+
98+
$this->assertEquals(2, $afterCleanup);
99+
}
100+
101+
public function testTrustedIpGetAttempts() {
102+
$ip = "127.0.0.100"; // trusted IP
103+
104+
IpAttempts::logFailedAttempt($ip, "test", time() + 3600);
105+
IpAttempts::logFailedAttempt($ip, "test", time() + 3600);
106+
107+
$count = IpAttempts::getAttemptsCount($ip, "test");
108+
$this->assertEquals(0, $count);
109+
}
110+
111+
public function testTrustedIpGetAttemptsSkipsDb() {
112+
$ip = "127.0.0.100"; // trusted IP
113+
114+
IpAttempts::logFailedAttempt($ip, "test", time() + 3600);
115+
IpAttempts::logFailedAttempt($ip, "test", time() + 3600);
116+
117+
$query = Db::$pdo->prepare('SELECT count(*) AS count FROM ipAttempts');
118+
$query->execute();
119+
$result = $query->fetchAll();
120+
$count = $result[0]['count'];
121+
$this->assertEquals(0, $count);
122+
}
123+
}

tests/phpunit/JtiStoreTest.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
namespace Pdsinterop\PhpSolid\Tests;
3+
4+
require_once(__DIR__ . "/test-config.php");
5+
6+
use Pdsinterop\PhpSolid\JtiStore;
7+
use Pdsinterop\PhpSolid\Db;
8+
9+
class JtiStoreTest extends \PHPUnit\Framework\TestCase
10+
{
11+
protected function setUp(): void
12+
{
13+
$statements = [
14+
'DROP TABLE IF EXISTS jti',
15+
'CREATE TABLE IF NOT EXISTS jti (
16+
jti VARCHAR(255) NOT NULL PRIMARY KEY,
17+
expires TEXT
18+
)'
19+
];
20+
21+
Db::connect();
22+
try {
23+
// create tables
24+
foreach($statements as $statement){
25+
Db::$pdo->exec($statement);
26+
}
27+
} catch(\PDOException $e) {
28+
echo $e->getMessage();
29+
}
30+
}
31+
32+
public function testNonExistingJti() {
33+
$jti = "123";
34+
$found = JtiStore::hasJti($jti);
35+
$this->assertFalse($found);
36+
}
37+
38+
public function testExistingJti() {
39+
$jti = "123";
40+
JtiStore::saveJti($jti, time() + 3600);
41+
$found = JtiStore::hasJti($jti);
42+
$this->assertTrue($found);
43+
}
44+
45+
public function testExpiredJti() {
46+
$jti = "123";
47+
JtiStore::saveJti($jti);
48+
$query = Db::$pdo->prepare('UPDATE jti SET expires=:expires WHERE jti=:jti');
49+
$query->execute([
50+
'expires' => time() - 10,
51+
'jti' => $jti
52+
]);
53+
$found = JtiStore::hasJti($jti);
54+
$this->assertFalse($found);
55+
}
56+
57+
public function testCleanup() {
58+
JtiStore::saveJti("123");
59+
JtiStore::saveJti("234");
60+
61+
$query = Db::$pdo->prepare('UPDATE jti SET expires=:expires WHERE jti=:jti');
62+
$query->execute([
63+
'expires' => time() - 10,
64+
'jti' => "123"
65+
]);
66+
$query = Db::$pdo->prepare('UPDATE jti SET expires=:expires WHERE jti=:jti');
67+
$query->execute([
68+
'expires' => time() - 10,
69+
'jti' => "234"
70+
]);
71+
72+
$query = Db::$pdo->prepare('SELECT count(*) AS count FROM jti');
73+
$query->execute();
74+
$result = $query->fetchAll();
75+
$beforeCleanup = $result[0]['count'];
76+
$this->assertEquals(2, $beforeCleanup);
77+
78+
79+
JtiStore::cleanupJti();
80+
$query = Db::$pdo->prepare('SELECT count(*) AS count FROM jti');
81+
$query->execute();
82+
$result = $query->fetchAll();
83+
$afterCleanup = $result[0]['count'];
84+
85+
$this->assertEquals(0, $afterCleanup);
86+
}
87+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
namespace Pdsinterop\PhpSolid\Tests;
3+
4+
use Pdsinterop\PhpSolid\PasswordValidator;
5+
6+
class PasswordValidatorTest extends \PHPUnit\Framework\TestCase
7+
{
8+
public function testBaseLower()
9+
{
10+
$password = "aaa";
11+
$this->assertEquals(26, PasswordValidator::getBase($password));
12+
}
13+
14+
public function testBaseUpper()
15+
{
16+
$password = "AAA";
17+
$this->assertEquals(26, PasswordValidator::getBase($password));
18+
}
19+
20+
public function testBaseNumbers()
21+
{
22+
$password = "123";
23+
$this->assertEquals(10, PasswordValidator::getBase($password));
24+
}
25+
26+
public function testBaseSpecial()
27+
{
28+
$password = "!@#$";
29+
$this->assertEquals(32, PasswordValidator::getBase($password));
30+
}
31+
32+
public function testBaseUpperAndLower()
33+
{
34+
$password = "aaaAAA";
35+
$this->assertEquals(52, PasswordValidator::getBase($password));
36+
}
37+
38+
public function testLengthLower()
39+
{
40+
$password = "aaa";
41+
$this->assertEquals(2, PasswordValidator::getLength($password));
42+
}
43+
44+
public function testLengthUpper()
45+
{
46+
$password = "AAA";
47+
$this->assertEquals(2, PasswordValidator::getLength($password));
48+
}
49+
50+
public function testLengthNumbers()
51+
{
52+
$password = "123";
53+
$this->assertEquals(3, PasswordValidator::getLength($password));
54+
}
55+
56+
public function testLengthSpecial()
57+
{
58+
$password = "!@#$";
59+
$this->assertEquals(4, PasswordValidator::getLength($password));
60+
}
61+
62+
public function testLengthUpperAndLower()
63+
{
64+
$password = "aaaAAA";
65+
$this->assertEquals(4, PasswordValidator::getLength($password));
66+
}
67+
68+
public function testSimplyEntropy()
69+
{
70+
$values = [
71+
["password" => "aaa", "expected" => 6.52],
72+
["password" => "abc", "expected" => 9.77]
73+
];
74+
75+
foreach ($values as $value) {
76+
$this->assertEquals($value['expected'], PasswordValidator::getEntropy($value['password']));
77+
}
78+
}
79+
}

tests/phpunit/UtilTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
namespace Pdsinterop\PhpSolid\Tests;
3+
4+
use Pdsinterop\PhpSolid\Util;
5+
6+
class UtilTest extends \PHPUnit\Framework\TestCase
7+
{
8+
public function testBase64EncodeDecode() {
9+
$string = "this is a test string with more stuffing";
10+
$encoded = Util::base64_url_encode($string);
11+
$decoded = Util::base64_url_decode($encoded);
12+
$this->assertEquals($string, $decoded);
13+
}
14+
}

tests/phpunit/phpunit.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<phpunit colors="true">
2+
<testsuites>
3+
<testsuite name="unit">
4+
<directory>.</directory>
5+
</testsuite>
6+
</testsuites>
7+
</phpunit>

tests/phpunit/test-config.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
namespace Pdsinterop\PhpSolid;
3+
4+
const DBPATH = ":memory:";
5+
const TRUSTED_IPS = ['127.0.0.100'];

0 commit comments

Comments
 (0)