Don't rely on shallow_class decls being available in NastInitCheck
[hiphop-php.git] / hphp / test / ext / test_cpp_base.cpp
blobc6c69426065f9450fa00a2d81a901be449ca872a
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #include "hphp/test/ext/test_cpp_base.h"
18 #include "hphp/runtime/ext/extension.h"
19 #include "hphp/util/logger.h"
20 #include "hphp/runtime/base/memory-manager.h"
21 #include "hphp/runtime/base/builtin-functions.h"
22 #include "hphp/runtime/ext/apc/ext_apc.h"
23 #include "hphp/runtime/ext/string/ext_string.h"
24 #include "hphp/runtime/ext/std/ext_std_variable.h"
25 #include "hphp/runtime/base/runtime-option.h"
26 #include "hphp/runtime/server/ip-block-map.h"
27 #include "hphp/runtime/server/virtual-host.h"
28 #include "hphp/runtime/server/satellite-server.h"
29 #include "hphp/system/systemlib.h"
31 ///////////////////////////////////////////////////////////////////////////////
33 TestCppBase::TestCppBase() {
36 ///////////////////////////////////////////////////////////////////////////////
38 bool TestCppBase::RunTests(const std::string &which) {
39 bool ret = true;
40 RUN_TEST(TestIpBlockMap);
41 RUN_TEST(TestIpBlockMapIni);
42 RUN_TEST(TestSatelliteServer);
43 RUN_TEST(TestSatelliteServerIni);
44 RUN_TEST(TestVirtualHost);
45 RUN_TEST(TestVirtualHostIni);
46 RUN_TEST(TestCollectionHdf);
47 RUN_TEST(TestCollectionIni);
48 return ret;
51 ///////////////////////////////////////////////////////////////////////////////
52 // building blocks
54 /* Pull 32bit Big Endian words from an in6_addr */
55 static inline long in6addrWord(struct in6_addr addr, char wordNo) {
56 return ((addr.s6_addr[(wordNo*4)+0] << 24) |
57 (addr.s6_addr[(wordNo*4)+1] << 16) |
58 (addr.s6_addr[(wordNo*4)+2] << 8) |
59 (addr.s6_addr[(wordNo*4)+3] << 0)) & 0xFFFFFFFF;
62 bool TestCppBase::TestIpBlockMap() {
63 struct in6_addr addr;
64 int bits;
66 VERIFY(IpBlockMap::ReadIPv6Address("204.15.21.0/22", &addr, bits));
67 VS(bits, 118);
68 VS(in6addrWord(addr, 0), 0x00000000L);
69 VS(in6addrWord(addr, 1), 0x00000000L);
70 VS(in6addrWord(addr, 2), 0x0000FFFFL);
71 VS(in6addrWord(addr, 3), 0xCC0F1500L);
73 VERIFY(IpBlockMap::ReadIPv6Address("127.0.0.1", &addr, bits));
74 VS(bits, 128);
75 VS(in6addrWord(addr, 0), 0x00000000L);
76 VS(in6addrWord(addr, 1), 0x00000000L);
77 VS(in6addrWord(addr, 2), 0x0000FFFFL);
78 VS(in6addrWord(addr, 3), 0x7F000001L);
80 VERIFY(IpBlockMap::ReadIPv6Address(
81 "1111:2222:3333:4444:5555:6666:789a:bcde", &addr, bits));
82 VS(bits, 128);
83 VS(in6addrWord(addr, 0), 0x11112222L);
84 VS(in6addrWord(addr, 1), 0x33334444L);
85 VS(in6addrWord(addr, 2), 0x55556666L);
86 VS(in6addrWord(addr, 3), 0x789abcdeL);
88 VERIFY(IpBlockMap::ReadIPv6Address(
89 "1111:2222:3333:4444:5555:6666:789a:bcde/68", &addr, bits));
90 VS(bits, 68);
91 VS(in6addrWord(addr, 0), 0x11112222L);
92 VS(in6addrWord(addr, 1), 0x33334444L);
93 VS(in6addrWord(addr, 2), 0x55556666L);
94 VS(in6addrWord(addr, 3), 0x789abcdeL);
96 IpBlockMap::BinaryPrefixTrie root(true);
97 unsigned char value[16];
99 // Default value with no additional nodes
100 memset(value, 0, 16);
101 VERIFY(root.isAllowed(value, 1));
102 value[0] = 0x80;
103 VERIFY(root.isAllowed(value));
105 // Inheritance of parent allow value through multiple levels of new nodes
106 IpBlockMap::BinaryPrefixTrie::InsertNewPrefix(&root, value, 1, false);
107 value[0] = 0xf0;
108 IpBlockMap::BinaryPrefixTrie::InsertNewPrefix(&root, value, 4, true);
109 VERIFY(root.isAllowed(value));
110 value[0] = 0xe0;
111 VERIFY(!root.isAllowed(value));
112 value[0] = 0xc0;
113 VERIFY(!root.isAllowed(value));
114 value[0] = 0x80;
115 VERIFY(!root.isAllowed(value));
116 value[0] = 0;
117 VERIFY(root.isAllowed(value));
119 // > 1 byte in address
120 value[2] = 0xff;
121 IpBlockMap::BinaryPrefixTrie::InsertNewPrefix(&root, value, 24, false);
122 VERIFY(!root.isAllowed(value));
123 value[3] = 0xff;
124 VERIFY(!root.isAllowed(value));
125 value[2] = 0xfe;
126 VERIFY(root.isAllowed(value));
128 // Exact address match
129 value[2] = 0xff;
130 value[15] = 1;
131 IpBlockMap::BinaryPrefixTrie::InsertNewPrefix(&root, value, 128, true);
132 VERIFY(root.isAllowed(value));
134 IniSetting::Map ini = IniSetting::Map::object;
135 Hdf hdf;
136 hdf.fromString(
137 "IpBlockMap {\n"
138 " 0 {\n"
139 " Location = /test\n"
140 " AllowFirst = true\n"
141 " Ip {\n"
142 " Allow {\n"
143 " * = 127.0.0.1\n"
144 " }\n"
145 " Deny {\n"
146 " * = 8.32.0.0/24\n"
147 " * = aaaa:bbbb:cccc:dddd:eeee:ffff:1111::/80\n"
148 " }\n"
149 " }\n"
150 " }\n"
151 "}\n"
154 IpBlockMap ibm(ini, hdf);
155 VERIFY(!ibm.isBlocking("test/blah.php", "127.0.0.1"));
156 VERIFY(ibm.isBlocking("test/blah.php", "8.32.0.104"));
157 VERIFY(ibm.isBlocking("test/blah.php",
158 "aaaa:bbbb:cccc:dddd:eeee:9999:8888:7777"));
159 VERIFY(!ibm.isBlocking("test/blah.php",
160 "aaaa:bbbb:cccc:dddd:eee3:4444:3333:2222"));
162 return Count(true);
165 bool TestCppBase::TestIpBlockMapIni() {
166 struct in6_addr addr;
167 int bits;
169 VERIFY(IpBlockMap::ReadIPv6Address("204.15.21.0/22", &addr, bits));
170 VS(bits, 118);
171 VS(in6addrWord(addr, 0), 0x00000000L);
172 VS(in6addrWord(addr, 1), 0x00000000L);
173 VS(in6addrWord(addr, 2), 0x0000FFFFL);
174 VS(in6addrWord(addr, 3), 0xCC0F1500L);
176 VERIFY(IpBlockMap::ReadIPv6Address("127.0.0.1", &addr, bits));
177 VS(bits, 128);
178 VS(in6addrWord(addr, 0), 0x00000000L);
179 VS(in6addrWord(addr, 1), 0x00000000L);
180 VS(in6addrWord(addr, 2), 0x0000FFFFL);
181 VS(in6addrWord(addr, 3), 0x7F000001L);
183 VERIFY(IpBlockMap::ReadIPv6Address(
184 "1111:2222:3333:4444:5555:6666:789a:bcde", &addr, bits));
185 VS(bits, 128);
186 VS(in6addrWord(addr, 0), 0x11112222L);
187 VS(in6addrWord(addr, 1), 0x33334444L);
188 VS(in6addrWord(addr, 2), 0x55556666L);
189 VS(in6addrWord(addr, 3), 0x789abcdeL);
191 VERIFY(IpBlockMap::ReadIPv6Address(
192 "1111:2222:3333:4444:5555:6666:789a:bcde/68", &addr, bits));
193 VS(bits, 68);
194 VS(in6addrWord(addr, 0), 0x11112222L);
195 VS(in6addrWord(addr, 1), 0x33334444L);
196 VS(in6addrWord(addr, 2), 0x55556666L);
197 VS(in6addrWord(addr, 3), 0x789abcdeL);
199 IpBlockMap::BinaryPrefixTrie root(true);
200 unsigned char value[16];
202 // Default value with no additional nodes
203 memset(value, 0, 16);
204 VERIFY(root.isAllowed(value, 1));
205 value[0] = 0x80;
206 VERIFY(root.isAllowed(value));
208 // Inheritance of parent allow value through multiple levels of new nodes
209 IpBlockMap::BinaryPrefixTrie::InsertNewPrefix(&root, value, 1, false);
210 value[0] = 0xf0;
211 IpBlockMap::BinaryPrefixTrie::InsertNewPrefix(&root, value, 4, true);
212 VERIFY(root.isAllowed(value));
213 value[0] = 0xe0;
214 VERIFY(!root.isAllowed(value));
215 value[0] = 0xc0;
216 VERIFY(!root.isAllowed(value));
217 value[0] = 0x80;
218 VERIFY(!root.isAllowed(value));
219 value[0] = 0;
220 VERIFY(root.isAllowed(value));
222 // > 1 byte in address
223 value[2] = 0xff;
224 IpBlockMap::BinaryPrefixTrie::InsertNewPrefix(&root, value, 24, false);
225 VERIFY(!root.isAllowed(value));
226 value[3] = 0xff;
227 VERIFY(!root.isAllowed(value));
228 value[2] = 0xfe;
229 VERIFY(root.isAllowed(value));
231 // Exact address match
232 value[2] = 0xff;
233 value[15] = 1;
234 IpBlockMap::BinaryPrefixTrie::InsertNewPrefix(&root, value, 128, true);
235 VERIFY(root.isAllowed(value));
237 std::string inistr =
238 "hhvm.ip_block_map[0][location] = /test\n"
239 "hhvm.ip_block_map[0][allow_first] = true\n"
240 "hhvm.ip_block_map[0][ip][allow][0] = 127.0.0.1\n"
241 "hhvm.ip_block_map[0][ip][deny][0] = 8.32.0.0/24\n"
242 "hhvm.ip_block_map[0][ip][deny][1] = "
243 "aaaa:bbbb:cccc:dddd:eeee:ffff:1111::/80\n";
245 IniSetting::Map ini = IniSetting::Map::object;
246 Hdf empty;
248 Config::ParseIniString(inistr, ini);
250 IpBlockMap ibm(ini, empty);
252 VERIFY(!ibm.isBlocking("test/blah.php", "127.0.0.1"));
253 VERIFY(ibm.isBlocking("test/blah.php", "8.32.0.104"));
254 VERIFY(ibm.isBlocking("test/blah.php",
255 "aaaa:bbbb:cccc:dddd:eeee:9999:8888:7777"));
256 // allow first
257 VERIFY(!ibm.isBlocking("test/blah.php",
258 "aaaa:bbbb:cccc:dddd:eee3:4444:3333:2222"));
260 return Count(true);
263 bool TestCppBase::TestSatelliteServer() {
264 IniSetting::Map ini = IniSetting::Map::object;
265 Hdf hdf;
266 hdf.fromString(
267 "Satellites {\n"
268 " rpc {\n"
269 " Type = RPCServer\n"
270 " Port = 9999\n"
271 " RequestInitDocument = my/rpc/rpc.php\n"
272 " RequestInitFunction = init_me\n"
273 " Password = abcd0987\n"
274 " Passwords {\n"
275 " * = abcd0987\n"
276 " }\n"
277 " }\n"
278 " ips {\n"
279 " Type = InternalPageServer\n"
280 " BlockMainServer = false\n"
281 " }\n"
282 "}\n"
286 std::vector<std::shared_ptr<SatelliteServerInfo>> infos;
287 RuntimeOption::ReadSatelliteInfo(ini, hdf, infos,
288 RuntimeOption::XboxPassword,
289 RuntimeOption::XboxPasswords);
290 for (auto& info_ptr : infos) {
291 auto info = info_ptr.get();
292 auto name = info->getName();
293 if (name == "rpc") {
294 VERIFY(info->getType() == SatelliteServer::Type::KindOfRPCServer);
295 VERIFY(info->getPort() == 9999);
296 VERIFY(info->getThreadCount() == 5);
297 VERIFY(info->getTimeoutSeconds() ==
298 std::chrono::seconds(RuntimeOption::RequestTimeoutSeconds));
299 VERIFY(info->getURLs().size() == 0);
300 VERIFY(info->getMaxRequest() == 500);
301 VERIFY(info->getMaxDuration() == 120);
302 VERIFY(info->getReqInitFunc() == "init_me");
303 VERIFY(info->getReqInitDoc() == "my/rpc/rpc.php");
304 VERIFY(info->getPassword() == "abcd0987");
305 VERIFY(info->getPasswords().size() == 1);
306 VERIFY(info->getPasswords().find("abcd0987") !=
307 info->getPasswords().end());
308 VERIFY(info->alwaysReset() == false);
309 VERIFY(RuntimeOption::XboxPassword == "abcd0987");
310 } else if (name == "ips") {
311 VERIFY(info->getType() ==
312 SatelliteServer::Type::KindOfInternalPageServer);
313 VERIFY(info->getURLs().size() == 0);
316 return Count(true);
319 bool TestCppBase::TestSatelliteServerIni() {
320 std::string iniStr =
321 "hhvm.satellites[rpc][type] = RPCServer\n"
322 "hhvm.satellites[rpc][port] = 9999\n"
323 "hhvm.satellites[rpc][request_init_document] = my/rpc/rpc.php\n"
324 "hhvm.satellites[rpc][request_init_function] = init_me\n"
325 "hhvm.satellites[rpc][password] = abcd0987\n"
326 "hhvm.satellites[rpc][passwords][] = abcd0987\n"
327 "hhvm.satellites[ips][type] = InternalPageServer\n"
328 "hhvm.satellites[ips][block_main_server] = false\n";
330 IniSettingMap ini = IniSetting::Map::object;
331 Hdf empty;
332 Config::ParseIniString(iniStr, ini);
334 std::vector<std::shared_ptr<SatelliteServerInfo>> infos;
335 RuntimeOption::ReadSatelliteInfo(ini, empty, infos,
336 RuntimeOption::XboxPassword,
337 RuntimeOption::XboxPasswords);
338 for (auto& info_ptr : infos) {
339 auto info = info_ptr.get();
340 auto name = info->getName();
341 if (name == "rpc") {
342 VERIFY(info->getType() == SatelliteServer::Type::KindOfRPCServer);
343 VERIFY(info->getPort() == 9999);
344 VERIFY(info->getThreadCount() == 5);
345 VERIFY(info->getTimeoutSeconds() ==
346 std::chrono::seconds(RuntimeOption::RequestTimeoutSeconds));
347 VERIFY(info->getURLs().size() == 0);
348 VERIFY(info->getMaxRequest() == 500);
349 VERIFY(info->getMaxDuration() == 120);
350 VERIFY(info->getReqInitFunc() == "init_me");
351 VERIFY(info->getReqInitDoc() == "my/rpc/rpc.php");
352 VERIFY(info->getPassword() == "abcd0987");
353 VERIFY(info->getPasswords().size() == 1);
354 VERIFY(info->getPasswords().find("abcd0987") !=
355 info->getPasswords().end());
356 VERIFY(info->alwaysReset() == false);
357 VERIFY(RuntimeOption::XboxPassword == "abcd0987");
358 } else if (name == "ips") {
359 VERIFY(info->getType() ==
360 SatelliteServer::Type::KindOfInternalPageServer);
361 VERIFY(info->getURLs().size() == 0);
364 return Count(true);
367 bool TestCppBase::TestVirtualHost() {
368 IniSetting::Map ini = IniSetting::Map::object;
369 Hdf hdf;
370 hdf.fromString(
371 " Server {\n"
372 " AllowedDirectories.* = /var/www\n"
373 " AllowedDirectories.* = /usr/bin\n"
374 " }\n"
375 " VirtualHost {\n"
376 " flibtest {\n"
377 " Prefix = flibtest.\n"
378 " PathTranslation = flib/_bin\n"
379 " ServerName = flibtest.facebook.com\n"
380 " }\n"
381 " upload {\n"
382 " Prefix = upload.\n"
383 " ServerVariables {\n"
384 " TFBENV = 8{\n"
385 " }\n"
386 " overwrite {\n"
387 " Server {\n"
388 " AllowedDirectories.* = /var/www\n"
389 " AllowedDirectories.* = /mnt\n"
390 " AllowedDirectories.* = /tmp\n"
391 " AllowedDirectories.* = /var/tmp/tao\n"
392 " }\n"
393 " MaxPostSize = 100MB\n"
394 " UploadMaxFileSize = 100MB\n"
395 " RequestTimeoutSeconds = 120\n"
396 " }\n"
397 " PathTranslation = html\n"
398 " }\n"
399 " default {\n"
400 " LogFilters {\n"
401 " * {\n"
402 " url = /method/searchme\n"
403 " params {\n"
404 " * = q\n"
405 " * = s\n"
406 " * = atoken\n"
407 " * = otoken\n"
408 " }\n"
409 " value = REMOVED\n"
410 " }\n"
411 " }\n"
412 " RewriteRules {\n"
413 " common {\n"
414 " pattern = /html/common/\n"
415 " to = http://3v4l.org\n"
416 " qsa = true\n"
417 " redirect = 301\n"
418 " }\n"
419 " }\n"
420 " PathTranslation = htm\n"
421 " }\n"
422 " }\n"
425 // reset RuntimeOption::AllowedDirectories to empty because if the INI
426 // version of this test is run at the same time, we don't want to append
427 // the same directories to it. We want to start fresh.
428 RuntimeOption::AllowedDirectories.clear();
429 std::vector<VirtualHost> hosts;
430 RuntimeOption::AllowedDirectories =
431 Config::GetStrVector(ini, hdf, "Server.AllowedDirectories");
432 auto cb = [&] (const IniSetting::Map &ini_cb, const Hdf &hdf_cb,
433 const std::string &host) {
434 if (VirtualHost::IsDefault(ini_cb, hdf_cb, host)) {
435 VirtualHost::GetDefault().init(ini_cb, hdf_cb, host);
436 VirtualHost::GetDefault().
437 addAllowedDirectories(RuntimeOption::AllowedDirectories);
438 } else {
439 auto vh = VirtualHost(ini_cb, hdf_cb, host);
440 // These will be added
441 // " AllowedDirectories.* = /var/www\n"
442 // " AllowedDirectories.* = /usr/bin\n"
443 vh.addAllowedDirectories(RuntimeOption::AllowedDirectories);
444 hosts.push_back(vh);
447 Config::Iterate(cb, ini, hdf, "VirtualHost");
449 for (auto& host : hosts) {
450 VirtualHost::SetCurrent(&host);
451 auto name = host.getName();
452 if (name == "flibtest") {
453 VERIFY(host.getPathTranslation() == "flib/_bin/"); // the / is added
454 VERIFY(host.getDocumentRoot() ==
455 RuntimeOption::SourceRoot + "flib/_bin");
456 VERIFY(host.getServerVars().size() == 0);
457 VERIFY(VirtualHost::GetAllowedDirectories().size() == 2);
458 VERIFY(host.valid() == true);
459 VERIFY(host.hasLogFilter() == false);
460 } else if (name == "upload") {
461 VERIFY(host.getPathTranslation() == "html/"); // the / is added
462 VERIFY(host.getDocumentRoot() ==
463 RuntimeOption::SourceRoot + "html");
464 // SortALlowedDirectories might add something and remove
465 // duplicates. In this case, /var/releases/continuous_www_scripts4
466 // was added and the duplicate /var/www was removed.s
467 VERIFY(VirtualHost::GetAllowedDirectories().size() == 6);
468 VERIFY(host.valid() == true);
469 VERIFY(host.hasLogFilter() == false);
470 VERIFY(VirtualHost::GetMaxPostSize() == 104857600);
474 VERIFY(VirtualHost::GetDefault().getPathTranslation() == "htm/");
475 VERIFY(VirtualHost::GetDefault().hasLogFilter() == true);
476 String rw("/html/common/");
477 String h("default");
478 bool q = false;
479 int rd = 0;
480 VirtualHost::GetDefault().rewriteURL(h, rw, q, rd);
481 VERIFY(rw.toCppString() == "http://3v4l.org");
482 VERIFY(rd == 301);
483 return Count(true);
486 bool TestCppBase::TestVirtualHostIni() {
487 std::string inistr =
488 "hhvm.server.allowed_directories[] = /var/www\n"
489 "hhvm.server.allowed_directories[] = /usr/bin\n"
490 "hhvm.virtual_host[flibtest][prefix] = flibtest.\n"
491 "hhvm.virtual_host[flibtest][path_translation] = flib/_bin\n"
492 "hhvm.virtual_host[flibtest][server_name] = flibtest.facebook.com\n"\
493 "hhvm.virtual_host[flibtest][log_filters][0][url] = function/searchme\n"
494 "hhvm.virtual_host[flibtest][log_filters][0][params][0] = v\n"
495 "hhvm.virtual_host[flibtest][log_filters][0][params][1] = t\n"
496 "hhvm.virtual_host[flibtest][log_filters][0][params][2] = btoken\n"
497 "hhvm.virtual_host[flibtest][log_filters][0][params][3] = ptoken\n"
498 "hhvm.virtual_host[flibtest][log_filters][0][value] = INSERTED\n"
499 "hhvm.virtual_host[flibtest][log_filters][1][url] = property/searchme\n"
500 "hhvm.virtual_host[flibtest][log_filters][1][pattern] = #thePattern#\n"
501 "hhvm.virtual_host[flibtest][log_filters][1][value] = BETWEEN\n"
502 "hhvm.virtual_host[upload][prefix] = upload.\n"
503 "hhvm.virtual_host[upload][server_variables][TFBENV] = 8\n"
504 "hhvm.virtual_host[upload][overwrite][server][allowed_directories][0] = "
505 "/var/www\n"
506 "hhvm.virtual_host[upload][overwrite][server][allowed_directories][1] = "
507 "/mnt\n"
508 "hhvm.virtual_host[upload][overwrite][server][allowed_directories][2] = "
509 "/tmp\n"
510 "hhvm.virtual_host[upload][overwrite][server][allowed_directories][3] = "
511 "/var/tmp/tao\n"
512 "hhvm.virtual_host[upload][overwrite][server][max_post_size] = 100MB\n"
513 "hhvm.virtual_host[upload][overwrite][server][upload]"
514 "[upload_max_file_size] = 100MB\n"
515 "hhvm.virtual_host[upload][overwrite][server]"
516 "[request_timeout_seconds] = 120\n"
517 "hhvm.virtual_host[upload][path_translation] = html\n"
518 "hhvm.virtual_host[default][path_translation] = htm\n"
519 "hhvm.virtual_host[default][log_filters][0][url] = method/searchme\n"
520 "hhvm.virtual_host[default][log_filters][0][params][0] = q\n"
521 "hhvm.virtual_host[default][log_filters][0][params][1] = s\n"
522 "hhvm.virtual_host[default][log_filters][0][params][2] = atoken\n"
523 "hhvm.virtual_host[default][log_filters][0][params][3] = otoken\n"
524 "hhvm.virtual_host[default][log_filters][0][value] = REMOVED\n"
525 "hhvm.virtual_host[default][rewrite_rules][common][pattern] = "
526 "/html/common/\n"
527 "hhvm.virtual_host[default][rewrite_rules][common][to] = "
528 "http://3v4l.org\n"
529 "hhvm.virtual_host[default][rewrite_rules][common][qsa] = true\n"
530 "hhvm.virtual_host[default][rewrite_rules][common][redirect] = 301\n";
532 IniSetting::Map ini = IniSetting::Map::object;
533 Hdf empty;
534 std::vector<VirtualHost> hosts;
536 // reset RuntimeOption::AllowedDirectories to empty because if the Hdf
537 // version of this test is run at the same time, we don't want to append
538 // the same directories to it. We want to start fresh.
539 RuntimeOption::AllowedDirectories.clear();
541 Config::ParseIniString(inistr, ini);
542 RuntimeOption::AllowedDirectories =
543 Config::GetStrVector(ini, empty, "Server.AllowedDirectories");
544 auto cb = [&] (const IniSetting::Map &ini_cb,
545 const Hdf &hdf_cb,
546 const std::string &host){
547 if (VirtualHost::IsDefault(ini_cb, hdf_cb, host)) {
548 VirtualHost::GetDefault().init(ini_cb, hdf_cb, host);
549 VirtualHost::GetDefault().
550 addAllowedDirectories(RuntimeOption::AllowedDirectories);
551 } else {
552 auto vh = VirtualHost(ini_cb, hdf_cb, host);
553 // These will be added
554 // "hhvm.server.allowed_directories[] = /var/www\n"
555 // "hhvm.server.allowed_directories[] = /usr/bin\n"
556 vh.addAllowedDirectories(RuntimeOption::AllowedDirectories);
557 hosts.push_back(vh);
560 Config::Iterate(cb, ini, empty, "VirtualHost");
562 for (auto& host : hosts) {
563 VirtualHost::SetCurrent(&host);
564 auto name = host.getName();
565 if (name == "flibtest") {
566 VERIFY(host.getPathTranslation() == "flib/_bin/"); // the / is added
567 VERIFY(host.getDocumentRoot() ==
568 RuntimeOption::SourceRoot + "flib/_bin");
569 VERIFY(host.getServerVars().size() == 0);
570 VERIFY(VirtualHost::GetAllowedDirectories().size() == 2);
571 VERIFY(host.valid() == true);
572 VERIFY(host.hasLogFilter() == true);
573 VERIFY(host.filterUrl("http://function/searchme/bar?ptoken=baz") ==
574 "http://function/searchme/bar?ptoken=INSERTED");
575 VERIFY(host.filterUrl(
576 "http://function/searchme/thePattern?ptoken=baz") ==
577 "http://function/searchme/thePattern?ptoken=INSERTED"
579 VERIFY(host.filterUrl("http://property/searchme/foothePattern") ==
580 "http://property/searchme/foo=BETWEEN");
581 VERIFY(host.filterUrl("foo") == "foo");
582 } else if (name == "upload") {
583 VERIFY(host.getPathTranslation() == "html/"); // the / is added
584 VERIFY(host.getDocumentRoot() ==
585 RuntimeOption::SourceRoot + "html");
586 // SortALlowedDirectories might add something and remove
587 // duplicates. In this case, /var/releases/continuous_www_scripts4
588 // was added and the duplicate /var/www was removed.s
589 VERIFY(VirtualHost::GetAllowedDirectories().size() == 6);
590 VERIFY(host.valid() == true);
591 VERIFY(host.hasLogFilter() == false);
592 VERIFY(VirtualHost::GetMaxPostSize() == 104857600);
596 VERIFY(VirtualHost::GetDefault().getPathTranslation() == "htm/");
597 VERIFY(VirtualHost::GetDefault().hasLogFilter() == true);
598 String rw("/html/common/");
599 String h("default");
600 bool q = false;
601 int rd = 0;
602 VirtualHost::GetDefault().rewriteURL(h, rw, q, rd);
603 VERIFY(rw.toCppString() == "http://3v4l.org");
604 VERIFY(rd == 301);
606 return Count(true);
609 bool TestCppBase::TestCollectionHdf() {
610 IniSetting::Map ini = IniSetting::Map::object;
611 Hdf hdf;
612 hdf.fromString(
613 " Server {\n"
614 " AllowedDirectories.* = /var/www\n"
615 " AllowedDirectories.* = /usr/bin\n"
616 " HighPriorityEndPoints.* = /end\n"
617 " HighPriorityEndPoints.* = /point\n"
618 " HighPriorityEndPoints.* = /power\n"
619 " }\n"
621 RuntimeOption::AllowedDirectories.clear();
623 Config::Bind(RuntimeOption::AllowedDirectories, ini,
624 hdf, "Server.AllowedDirectories");
625 VERIFY(RuntimeOption::AllowedDirectories.size() == 2);
626 std::vector<std::string> ad =
627 Config::GetStrVector(ini, hdf, "Server.AllowedDirectories",
628 RuntimeOption::AllowedDirectories);
629 VERIFY(RuntimeOption::AllowedDirectories.size() == 2);
630 VERIFY(ad.size() == 2);
631 Config::Bind(RuntimeOption::ServerHighPriorityEndPoints, ini,
632 hdf, "Server.HighPriorityEndPoints");
633 VERIFY(RuntimeOption::ServerHighPriorityEndPoints.size() == 3);
634 return Count(true);
637 bool TestCppBase::TestCollectionIni() {
638 std::string inistr =
639 "hhvm.server.allowed_directories[] = /var/www\n"
640 "hhvm.server.allowed_directories[] = /usr/bin\n"
641 "hhvm.server.high_priority_end_points[] = /end\n"
642 "hhvm.server.high_priority_end_points[] = /point\n"
643 "hhvm.server.high_priority_end_points[] = /power\n"
644 "hhvm.server.high_priority_end_points[] = /word\n";
646 IniSetting::Map ini = IniSetting::Map::object;
647 Hdf empty;
649 // Ensure we have no residuals left over from the HDF run.
650 RuntimeOption::AllowedDirectories.clear();
651 RuntimeOption::ServerHighPriorityEndPoints.clear();
653 Config::ParseIniString(inistr, ini);
654 Config::Bind(RuntimeOption::AllowedDirectories, ini, empty,
655 "Server.AllowedDirectories"); // Test converting name
656 VERIFY(RuntimeOption::AllowedDirectories.size() == 2);
657 std::vector<std::string> ad =
658 Config::GetStrVector(ini, empty, "Server.AllowedDirectories",
659 RuntimeOption::AllowedDirectories, false);
660 // This should still be 2. In other words, Get shouldn't append
661 // values.
662 VERIFY(RuntimeOption::AllowedDirectories.size() == 2);
663 VERIFY(ad.size() == 2);
664 Config::Bind(RuntimeOption::ServerHighPriorityEndPoints, ini, empty,
665 "Server.HighPriorityEndPoints",
666 RuntimeOption::ServerHighPriorityEndPoints, false);
667 VERIFY(RuntimeOption::ServerHighPriorityEndPoints.size() == 4);
668 return Count(true);