forked from cubiclesoft/ultimate-web-scraper
/
test_suite.php
204 lines (173 loc) · 5.8 KB
/
test_suite.php
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
// Test suite.
// (C) 2015 CubicleSoft. All Rights Reserved.
if (!isset($_SERVER["argc"]) || !$_SERVER["argc"])
{
echo "This file is intended to be run from the command-line.";
exit();
}
// Temporary root.
$rootpath = str_replace("\\", "/", dirname(__FILE__));
require_once $rootpath . "/support/web_browser.php";
require_once $rootpath . "/support/http.php";
require_once $rootpath . "/support/crc32_stream.php";
require_once $rootpath . "/support/deflate_stream.php";
require_once $rootpath . "/support/simple_html_dom.php";
require_once $rootpath . "/support/tag_filter.php";
require_once $rootpath . "/support/multi_async_helper.php";
function TestHTMLTagFilter($stack, &$content, $open, $tagname, &$attrs, $options)
{
if ($open)
{
if ($tagname === "script") return array("keep_tag" => false, "keep_interior" => false);
else if ($tagname === "style") return array("keep_tag" => false, "keep_interior" => false);
else if ($tagname === "img")
{
if (!isset($attrs["src"]) || substr($attrs["src"], 0, 11) === "javascript:") return array("keep_tag" => false);
foreach ($attrs as $key => $val)
{
if ($key !== "src") unset($attrs[$key]);
}
}
else if ($tagname === "a")
{
if (!isset($attrs["href"]) || substr($attrs["href"], 0, 11) === "javascript:") return array("keep_tag" => false);
foreach ($attrs as $key => $val)
{
if ($key !== "href") unset($attrs[$key]);
}
}
else if ($tagname === "p")
{
foreach ($attrs as $key => $val)
{
if ($key !== "class") unset($attrs[$key]);
else
{
foreach ($val as $class)
{
if ($class !== "allowedclass") unset($val[$class]);
}
$attrs["class"] = $val;
if (!count($attrs["class"])) unset($attrs["class"]);
}
}
}
else if ($tagname === "br" || $tagname === "b" || $tagname === "strong" || $tagname === "i" || $tagname === "ul" || $tagname === "ol" || $tagname === "li")
{
// Remove all attributes.
$attrs = array();
}
else
{
return array("keep_tag" => false);
}
}
else
{
if ($tagname === "/b" || $tagname === "/strong" || $tagname === "/i" || $tagname === "/ul" || $tagname === "/ol" || $tagname === "/li")
{
if (trim($content) === "") return array("keep_tag" => false);
}
}
return array();
}
$options = TagFilter::GetHTMLOptions();
$options["tag_callback"] = "TestHTMLTagFilter";
echo "Testing XSS removal\n";
echo "-------------------\n";
$testfile = file_get_contents("test_xss.txt");
$pos = strpos($testfile, "@EXIT@");
if ($pos === false) $pos = strlen($testfile);
$testfile = substr($testfile, 0, $pos);
$result = TagFilter::Run($testfile, $options);
echo $result . "\n\n";
echo "-------------------\n\n";
echo "Testing Word HTML cleanup\n";
echo "-------------------------\n";
$testfile = file_get_contents("test_word.txt");
$pos = strpos($testfile, "@EXIT@");
if ($pos === false) $pos = strlen($testfile);
$testfile = substr($testfile, 0, $pos);
$result = TagFilter::Run($testfile, $options);
echo $result . "\n\n";
echo "-------------------------\n\n";
$html = new simple_html_dom();
$web = new WebBrowser();
$result = $web->Process("http://www.barebonescms.com/");
if (!$result["success"]) echo "[FAIL] An error occurred. " . $result["error"] . "\n";
else if ($result["response"]["code"] != 200) echo "[FAIL] An unexpected response code was returned. " . $result["response"]["line"] . "\n";
else
{
echo "[PASS] The expected response was returned.\n";
$html->load($result["body"]);
$rows = $html->find('a[href]');
foreach ($rows as $row)
{
echo "\t" . HTTP::ConvertRelativeToAbsoluteURL($result["url"], $row->href) . "\n";
}
}
$result = $web->Process("https://www.barebonescms.com/");
if (!$result["success"]) echo "[FAIL] An error occurred. " . $result["error"] . "\n";
else if ($result["response"]["code"] != 200) echo "[FAIL] An unexpected response code was returned. " . $result["response"]["line"] . "\n";
else
{
echo "[PASS] The expected response was returned.\n";
$html->load($result["body"]);
$rows = $html->find('a[href]');
foreach ($rows as $row)
{
echo "\t" . HTTP::ConvertRelativeToAbsoluteURL($result["url"], $row->href) . "\n";
}
}
// Test asynchronous access.
$urls = array(
"http://www.barebonescms.com/",
"http://www.cubiclesoft.com/",
);
// Build the queue.
$helper = new MultiAsyncHelper();
$pages = array();
foreach ($urls as $url)
{
$pages[$url] = new WebBrowser();
$pages[$url]->ProcessAsync($helper, $url, NULL, $url);
}
// Mix in another file handle type for fun.
$fp = fopen(__FILE__, "rb");
stream_set_blocking($fp, 0);
$helper->Set("__fp", $fp, "MultiAsyncHelper::ReadOnly");
// Run the main loop.
$result = $helper->Wait(2);
while ($result["success"])
{
// Process the file handle if it is ready for reading.
if (isset($result["read"]["__fp"]))
{
$fp = $result["read"]["__fp"];
$data = fread($fp, 500);
if ($data === false || feof($fp))
{
echo "[PASS] File read in successfully.\n";
$helper->Remove("__fp");
}
}
// Process everything else.
foreach ($result["removed"] as $key => $info)
{
if ($key === "__fp") continue;
if (!$info["result"]["success"]) echo "[FAIL] Error retrieving URL (" . $key . "). " . $info["result"]["error"] . "\n";
else if ($info["result"]["response"]["code"] != 200) echo "[FAIL] Error retrieving URL (" . $key . "). Server returned: " . $info["result"]["response"]["line"] . "\n";
else
{
echo "[PASS] The expected response was returned (" . $key . ").\n";
}
unset($pages[$key]);
}
// Break out of the loop when nothing is left.
if ($result["numleft"] < 1) break;
$result = $helper->Wait(2);
}
// An error occurred.
if (!$result["success"]) echo "[FAIL] Error in Wait(). " . $result["error"] . "\n";
?>