summaryrefslogtreecommitdiff
path: root/wrappers/php/tests/DiscoveryTest.php
blob: 9a3e14c2bdd6870035bd1b191778d88ec2723175 (plain)
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
<?php declare(strict_types=1);

use EduVpn\Common\Discovery;
use EduVpn\Common\InvalidSignatureException;
use EduVpn\Common\InvalidSignatureUnknownKeyException;
use EduVpn\Common\SignatureTooOldException;
use PHPUnit\Framework\TestCase;

class DiscoveryTest extends TestCase
{
	private const TEST_DATA_DIR = '../../src/test_data';

	public static function setUpBeforeClass(): void
	{
		preg_match('/[\r\n](\S+)\s*/', file_get_contents(self::TEST_DATA_DIR . '/public.key'), $matches);
		Discovery::insecureTestingSetExtraKey($matches[1]);
	}

	public function testValid(): void
	{
		$this->expectNotToPerformAssertions();
		Discovery::verify(file_get_contents(self::TEST_DATA_DIR . '/server_list.json.minisig'),
			  file_get_contents(self::TEST_DATA_DIR . '/server_list.json'),
			  'server_list.json', 10);
	}

	public function testInvalidSignature(): void
	{
		$this->expectException(InvalidSignatureException::class);
		Discovery::verify(file_get_contents(self::TEST_DATA_DIR . '/random.txt'),
			  file_get_contents(self::TEST_DATA_DIR . '/server_list.json'),
			  'server_list.json', 0);
	}

	public function testWrongKey(): void
	{
		$this->expectException(InvalidSignatureUnknownKeyException::class);
		Discovery::verify(file_get_contents(self::TEST_DATA_DIR . '/server_list.json.wrong_key.minisig'),
			  file_get_contents(self::TEST_DATA_DIR . '/server_list.json'),
			  'server_list.json', 0);
	}

	public function testOldSignature(): void
	{
		$this->expectException(SignatureTooOldException::class);
		Discovery::verify(file_get_contents(self::TEST_DATA_DIR . '/server_list.json.minisig'),
			  file_get_contents(self::TEST_DATA_DIR . '/server_list.json'),
			  'server_list.json', 11);
	}

	public function testUnknownExpectedFileName(): void
	{
		$this->expectException(InvalidArgumentException::class);
		Discovery::verify(file_get_contents(self::TEST_DATA_DIR . '/other_list.json.minisig'),
			  file_get_contents(self::TEST_DATA_DIR . '/other_list.json'),
			  'other_list.json', 0);
	}
}