Make FileUitls::Canonicalize return the empty string if it encounters a path with...
[hiphop-php.git] / hphp / test / ext / test_util.cpp
blob691acc091d134575a7adcb29f44c7d9d636a492d
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2016 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_util.h"
18 #include "hphp/util/logger.h"
19 #include "hphp/runtime/base/file-util.h"
20 #include "hphp/runtime/base/shared-string.h"
21 #include "hphp/runtime/base/zend-string.h"
22 #include "hphp/runtime/base/config.h"
24 #define VERIFY_DUMP(map, exp) \
25 if (!(exp)) { \
26 printf("%s:%d: [" #exp "] is false\n", __FILE__, __LINE__); \
27 map.dump(); \
28 return Count(false); \
29 } \
31 ///////////////////////////////////////////////////////////////////////////////
33 TestUtil::TestUtil() {
36 ///////////////////////////////////////////////////////////////////////////////
38 bool TestUtil::RunTests(const std::string &which) {
39 bool ret = true;
40 RUN_TEST(TestSharedString);
41 RUN_TEST(TestCanonicalize);
42 RUN_TEST(TestHDF);
43 return ret;
46 ///////////////////////////////////////////////////////////////////////////////
47 // data types
49 struct testhash {
50 size_t operator()(const String& s) const {
51 return hash_string_unsafe(s.data(), s.size());
55 struct testeqstr {
56 bool operator()(const String& s1, const String& s2) const {
57 return string_strcmp(s1.data(), s1.size(), s2.data(), s2.size()) == 0;
61 struct IHash {
62 size_t operator()(int i) const { return i; }
64 struct IEq {
65 bool operator()(int i, int j) const { return i == j; }
68 bool TestUtil::TestSharedString() {
70 SharedString foo = "foo";
71 SharedString bah("bah");
72 VERIFY(bah->getString() == "bah");
74 SharedString bah2 = bah;
75 VERIFY(bah2->getString() == "bah");
76 VERIFY(bah.get() == bah2.get());
77 SharedString bah3 = "bah";
78 VERIFY(bah.get() == bah3.get());
80 VERIFY(bah->getString() == "bah");
81 VERIFY(foo->getString() == "foo");
84 hphp_shared_string_map<int64_t> map;
85 for (int i = 0; i < 100; i++) {
86 std::string k("key");
87 k += i;
88 map[k] = i;
90 for (int i = 0; i < 100; i++) {
91 std::string k("key");
92 k += i;
93 auto const it = map.find(k);
94 VERIFY(it != map.end());
95 VERIFY(it->second == i);
99 return Count(true);
102 bool TestUtil::TestCanonicalize() {
103 VERIFY(FileUtil::canonicalize(String("foo")) == String("foo"));
104 VERIFY(FileUtil::canonicalize(String("/foo")) == String("/foo"));
105 VERIFY(FileUtil::canonicalize(String("foo/bar")) == String("foo/bar"));
106 VERIFY(FileUtil::canonicalize(String("foo/////bar")) == String("foo/bar"));
107 VERIFY(FileUtil::canonicalize(String("foo/bar/")) == String("foo/bar/"));
108 VERIFY(FileUtil::canonicalize(String("./foo")) == String("foo"));
109 VERIFY(FileUtil::canonicalize(String(".")) == String("."));
110 VERIFY(FileUtil::canonicalize(String("./")) == String("./"));
111 VERIFY(FileUtil::canonicalize(String("././")) == String("./"));
112 VERIFY(FileUtil::canonicalize(String("foo/./")) == String("foo/"));
113 VERIFY(FileUtil::canonicalize(String("foo/../bar")) == String("bar"));
114 VERIFY(FileUtil::canonicalize(String("./foo/../bar")) == String("bar"));
115 VERIFY(FileUtil::canonicalize(String(".////foo/xyz////..////../bar"))
116 == String("bar"));
117 VERIFY(FileUtil::canonicalize(String("a/foo../bar"))
118 == String("a/foo../bar"));
119 VERIFY(FileUtil::canonicalize(String("a./foo/./bar"))
120 == String("a./foo/bar"));
121 VERIFY(FileUtil::canonicalize(String("////a/foo")) == String("/a/foo"));
122 VERIFY(FileUtil::canonicalize(String("../foo")) == String("../foo"));
123 VERIFY(FileUtil::canonicalize(String("foo/../../bar")) == String("../bar"));
124 VERIFY(FileUtil::canonicalize(String("./../../")) == String("../../"));
125 VERIFY(FileUtil::canonicalize(String("/test\0", 6, CopyString))
126 == String(""));
127 VERIFY(FileUtil::canonicalize(String("/test\0test", 10, CopyString))
128 == String(""));
129 return Count(true);
132 bool TestUtil::TestHDF() {
133 // This was causing a crash
135 Hdf doc, node;
136 node = doc["Node"];
140 IniSetting::Map ini = IniSetting::Map::object;
141 Hdf doc;
142 doc.fromString(
143 "node.* {\n"
144 " name = value\n"
145 "}");
146 VS(Config::GetString(ini, doc, "node.0.name"), "value");
149 return Count(true);