|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace RattfieldNz\SafeUrls; |
|
6
|
|
|
|
|
7
|
|
|
use RattfieldNz\SafeUrls\Tests\TestCase; |
|
8
|
|
|
|
|
9
|
|
|
class SafeUrlsTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var SafeUrls */ |
|
12
|
|
|
private $safeUrls; |
|
13
|
|
|
|
|
14
|
|
|
private $urlsToTest; |
|
15
|
|
|
|
|
16
|
|
|
protected function setUp():void |
|
17
|
|
|
{ |
|
18
|
|
|
parent::setUp(); |
|
19
|
|
|
|
|
20
|
|
|
$this->safeUrls = new SafeUrls(); |
|
21
|
|
|
$this->urlsToTest = [ |
|
22
|
|
|
'https://testsafebrowsing.appspot.com/s/phishing.html', |
|
23
|
|
|
'https://testsafebrowsing.appspot.com/s/malware.html', |
|
24
|
|
|
'http://testsafebrowsing.appspot.com/apiv4/ANY_PLATFORM/MALWARE/URL/', |
|
25
|
|
|
'http://testsafebrowsing.appspot.com/apiv4/ANY_PLATFORM/SOCIAL_ENGINEERING/URL/', |
|
26
|
|
|
'http://malware.testing.google.test/testing/malware/', |
|
27
|
|
|
]; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function testAddUrls() |
|
31
|
|
|
{ |
|
32
|
|
|
$this->safeUrls->add($this->urlsToTest); |
|
33
|
|
|
|
|
34
|
|
|
$expected = $this->urlsToTest; |
|
35
|
|
|
$actual = $this->safeUrls->getCurrentUrls(true); |
|
36
|
|
|
|
|
37
|
|
|
$this->assertEquals($expected, $actual); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testRemoveUrls() |
|
41
|
|
|
{ |
|
42
|
|
|
$this->safeUrls->add($this->urlsToTest); |
|
43
|
|
|
$urlToRemove = 'https://testsafebrowsing.appspot.com/s/phishing.html'; |
|
44
|
|
|
|
|
45
|
|
|
$expected = [ |
|
46
|
|
|
$this->urlsToTest[1], |
|
47
|
|
|
$this->urlsToTest[2], |
|
48
|
|
|
$this->urlsToTest[3], |
|
49
|
|
|
$this->urlsToTest[4] |
|
50
|
|
|
]; |
|
51
|
|
|
$this->safeUrls->remove([$urlToRemove]); |
|
52
|
|
|
|
|
53
|
|
|
$actual = $this->safeUrls->getCurrentUrls(true); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertEquals($expected, $actual); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function testRemoveAllUrls(){ |
|
59
|
|
|
$this->safeUrls->add($this->urlsToTest); |
|
60
|
|
|
|
|
61
|
|
|
$expected = []; |
|
62
|
|
|
$this->safeUrls->remove($this->urlsToTest); |
|
63
|
|
|
$actual = $this->safeUrls->getCurrentUrls(true); |
|
64
|
|
|
|
|
65
|
|
|
$this->assertEquals($expected, $actual); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testExecute(){ |
|
69
|
|
|
|
|
70
|
|
|
// Only testing with returned HTTP status, as Google Safebrowsing API not working as |
|
71
|
|
|
// documented. See tests\Libraries\Curl\CurlTest testMalwareSocialEngineeringAnyPlatformUrl(). |
|
72
|
|
|
|
|
73
|
|
|
$this->safeUrls->add($this->urlsToTest); |
|
74
|
|
|
|
|
75
|
|
|
$expected = '{ "status": 200, "response": { "matches": [ { "threatType": "SOCIAL_ENGINEERING", "platformType": "ANY_PLATFORM", "threat": { "url": "https://testsafebrowsing.appspot.com/s/phishing.html" }, "cacheDuration": "300s", "threatEntryType": "URL" }, { "threatType": "MALWARE", "platformType": "ANY_PLATFORM", "threat": { "url": "https://testsafebrowsing.appspot.com/s/malware.html" }, "cacheDuration": "300s", "threatEntryType": "URL" }, { "threatType": "MALWARE", "platformType": "ANY_PLATFORM", "threat": { "url": "http://testsafebrowsing.appspot.com/apiv4/ANY_PLATFORM/MALWARE/URL/" }, "cacheDuration": "300s", "threatEntryType": "URL" }, { "threatType": "SOCIAL_ENGINEERING", "platformType": "ANY_PLATFORM", "threat": { "url": "http://testsafebrowsing.appspot.com/apiv4/ANY_PLATFORM/SOCIAL_ENGINEERING/URL/" }, "cacheDuration": "300s", "threatEntryType": "URL" }, { "threatType": "MALWARE", "platformType": "ANY_PLATFORM", "threat": { "url": "http://malware.testing.google.test/testing/malware/" }, "cacheDuration": "300s", "threatEntryType": "URL" }, { "threatType": "SOCIAL_ENGINEERING", "platformType": "ANY_PLATFORM", "threat": { "url": "http://malware.testing.google.test/testing/malware/" }, "cacheDuration": "300s", "threatEntryType": "URL" } ] } }'; |
|
76
|
|
|
|
|
77
|
|
|
$expected = json_decode($expected, true)["status"]; |
|
78
|
|
|
$actual = json_decode((string)$this->safeUrls->execute()->getResults(), true)["status"]; |
|
79
|
|
|
|
|
80
|
|
|
$this->assertEquals($expected, $actual); |
|
81
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function testGetResults(){ |
|
85
|
|
|
|
|
86
|
|
|
$this->testExecute(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function testCheck(){ |
|
90
|
|
|
|
|
91
|
|
|
$expected = '{ "status": 200, "response": { "matches": [ { "threatType": "SOCIAL_ENGINEERING", "platformType": "ANY_PLATFORM", "threat": { "url": "https://testsafebrowsing.appspot.com/s/phishing.html" }, "cacheDuration": "300s", "threatEntryType": "URL" }, { "threatType": "MALWARE", "platformType": "ANY_PLATFORM", "threat": { "url": "https://testsafebrowsing.appspot.com/s/malware.html" }, "cacheDuration": "300s", "threatEntryType": "URL" }, { "threatType": "MALWARE", "platformType": "ANY_PLATFORM", "threat": { "url": "http://testsafebrowsing.appspot.com/apiv4/ANY_PLATFORM/MALWARE/URL/" }, "cacheDuration": "300s", "threatEntryType": "URL" }, { "threatType": "SOCIAL_ENGINEERING", "platformType": "ANY_PLATFORM", "threat": { "url": "http://testsafebrowsing.appspot.com/apiv4/ANY_PLATFORM/SOCIAL_ENGINEERING/URL/" }, "cacheDuration": "300s", "threatEntryType": "URL" }, { "threatType": "MALWARE", "platformType": "ANY_PLATFORM", "threat": { "url": "http://malware.testing.google.test/testing/malware/" }, "cacheDuration": "300s", "threatEntryType": "URL" }, { "threatType": "SOCIAL_ENGINEERING", "platformType": "ANY_PLATFORM", "threat": { "url": "http://malware.testing.google.test/testing/malware/" }, "cacheDuration": "300s", "threatEntryType": "URL" } ] } }'; |
|
92
|
|
|
$expected = json_decode($expected, true)["status"]; |
|
93
|
|
|
|
|
94
|
|
|
$actual = json_decode($this->safeUrls::check($this->urlsToTest), true)["status"]; |
|
95
|
|
|
|
|
96
|
|
|
$this->assertEquals($expected, $actual); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @expectedException \ErrorException |
|
101
|
|
|
*/ |
|
102
|
|
|
public function testCheckThrowErrorException(){ |
|
103
|
|
|
$this->safeUrls->add($this->urlsToTest); |
|
104
|
|
|
|
|
105
|
|
|
$safeUrlsMock = $this->getMockBuilder(SafeUrls::class) |
|
106
|
|
|
->setMethods(['callStatic']) |
|
107
|
|
|
->getMock(); |
|
108
|
|
|
|
|
109
|
|
|
$safeUrlsMock->method("callStatic") |
|
|
|
|
|
|
110
|
|
|
->with(SafeUrls::class, 'check', $this->urlsToTest) |
|
111
|
|
|
->willThrowException(new \ErrorException('Expected Exception was thrown')); |
|
112
|
|
|
|
|
113
|
|
|
$safeUrlsMock->checkCallStatic($this->urlsToTest); |
|
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function testIsDangerous(){ |
|
118
|
|
|
$this->safeUrls->add($this->urlsToTest); |
|
119
|
|
|
$this->safeUrls->execute(); |
|
120
|
|
|
|
|
121
|
|
|
$dangerousUrl = "http://malware.testing.google.test/testing/malware/"; |
|
122
|
|
|
|
|
123
|
|
|
// See tests\Libraries\Curl\CurlTest testMalwareSocialEngineeringAnyPlatformUrl(). |
|
124
|
|
|
$expected = true; |
|
125
|
|
|
$actual = !$this->safeUrls->isDangerous($dangerousUrl); |
|
126
|
|
|
|
|
127
|
|
|
$this->assertEquals($expected, $actual); |
|
128
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.