1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use PHPUnit\Framework\TestCase; |
4
|
|
|
use tinymeng\tools\Encryption; |
5
|
|
|
|
6
|
|
|
class EncryptionTest extends TestCase |
7
|
|
|
{ |
8
|
|
|
public function testAuthcodeEncode() |
9
|
|
|
{ |
10
|
|
|
$originalString = 'Hello, World!'; |
11
|
|
|
$key = 'tinymeng'; |
12
|
|
|
$encodedString = Encryption::authcode($originalString, 'encode', $key); |
13
|
|
|
$this->assertNotEquals($originalString, $encodedString); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function testAuthcodeDecode() |
17
|
|
|
{ |
18
|
|
|
$originalString = 'Hello, World!'; |
19
|
|
|
$key = 'tinymeng'; |
20
|
|
|
$encodedString = Encryption::authcode($originalString, 'encode', $key); |
21
|
|
|
$decodedString = Encryption::authcode($encodedString, 'decode', $key); |
22
|
|
|
$this->assertEquals($originalString, $decodedString); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testAuthcodeWithExpiry() |
26
|
|
|
{ |
27
|
|
|
$originalString = 'Hello, World!'; |
28
|
|
|
$key = 'tinymeng'; |
29
|
|
|
$expiry = 10; // 10 seconds |
30
|
|
|
$encodedString = Encryption::authcode($originalString, 'encode', $key, $expiry); |
31
|
|
|
sleep(11); // Wait for the encoded string to expire |
32
|
|
|
$decodedString = Encryption::authcode($encodedString, 'decode', $key); |
33
|
|
|
$this->assertEquals('', $decodedString); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testAuthcodeWithoutExpiry() |
37
|
|
|
{ |
38
|
|
|
$originalString = 'Hello, World!'; |
39
|
|
|
$key = 'tinymeng'; |
40
|
|
|
$encodedString = Encryption::authcode($originalString, 'encode', $key); |
41
|
|
|
$decodedString = Encryption::authcode($encodedString, 'decode', $key); |
42
|
|
|
$this->assertEquals($originalString, $decodedString); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|