1 //===- SpecialCaseListTest.cpp - Unit tests for SpecialCaseList -----------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/Support/SpecialCaseList.h" 11 #include "llvm/Support/FileSystem.h" 12 #include "llvm/Support/MemoryBuffer.h" 13 #include "gtest/gtest.h" 14 15 using namespace llvm; 16 17 namespace { 18 19 class SpecialCaseListTest : public ::testing::Test { 20 protected: 21 std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List, 22 std::string &Error) { 23 std::unique_ptr<MemoryBuffer> MB = MemoryBuffer::getMemBuffer(List); 24 return SpecialCaseList::create(MB.get(), Error); 25 } 26 27 std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List) { 28 std::string Error; 29 auto SCL = makeSpecialCaseList(List, Error); 30 assert(SCL); 31 assert(Error == ""); 32 return SCL; 33 } 34 35 std::string makeSpecialCaseListFile(StringRef Contents) { 36 int FD; 37 SmallString<64> Path; 38 sys::fs::createTemporaryFile("SpecialCaseListTest", "temp", FD, Path); 39 raw_fd_ostream OF(FD, true, true); 40 OF << Contents; 41 OF.close(); 42 return Path.str(); 43 } 44 }; 45 46 TEST_F(SpecialCaseListTest, Basic) { 47 std::unique_ptr<SpecialCaseList> SCL = 48 makeSpecialCaseList("# This is a comment.\n" 49 "\n" 50 "src:hello\n" 51 "src:bye\n" 52 "src:hi=category\n" 53 "src:z*=category\n"); 54 EXPECT_TRUE(SCL->inSection("", "src", "hello")); 55 EXPECT_TRUE(SCL->inSection("", "src", "bye")); 56 EXPECT_TRUE(SCL->inSection("", "src", "hi", "category")); 57 EXPECT_TRUE(SCL->inSection("", "src", "zzzz", "category")); 58 EXPECT_FALSE(SCL->inSection("", "src", "hi")); 59 EXPECT_FALSE(SCL->inSection("", "fun", "hello")); 60 EXPECT_FALSE(SCL->inSection("", "src", "hello", "category")); 61 } 62 63 TEST_F(SpecialCaseListTest, SectionRegexErrorHandling) { 64 std::string Error; 65 EXPECT_EQ(makeSpecialCaseList("[address", Error), nullptr); 66 EXPECT_TRUE(((StringRef)Error).startswith("malformed section header ")); 67 68 EXPECT_EQ(makeSpecialCaseList("[[]", Error), nullptr); 69 EXPECT_TRUE(((StringRef)Error).startswith("malformed regex for section [: ")); 70 71 EXPECT_EQ(makeSpecialCaseList("src:=", Error), nullptr); 72 EXPECT_TRUE(((StringRef)Error).endswith("Supplied regexp was blank")); 73 } 74 75 TEST_F(SpecialCaseListTest, Section) { 76 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:global\n" 77 "[sect1|sect2]\n" 78 "src:test1\n" 79 "[sect3*]\n" 80 "src:test2\n"); 81 EXPECT_TRUE(SCL->inSection("arbitrary", "src", "global")); 82 EXPECT_TRUE(SCL->inSection("", "src", "global")); 83 EXPECT_TRUE(SCL->inSection("sect1", "src", "test1")); 84 EXPECT_FALSE(SCL->inSection("sect1-arbitrary", "src", "test1")); 85 EXPECT_FALSE(SCL->inSection("sect", "src", "test1")); 86 EXPECT_FALSE(SCL->inSection("sect1", "src", "test2")); 87 EXPECT_TRUE(SCL->inSection("sect2", "src", "test1")); 88 EXPECT_TRUE(SCL->inSection("sect3", "src", "test2")); 89 EXPECT_TRUE(SCL->inSection("sect3-arbitrary", "src", "test2")); 90 EXPECT_FALSE(SCL->inSection("", "src", "test1")); 91 EXPECT_FALSE(SCL->inSection("", "src", "test2")); 92 } 93 94 TEST_F(SpecialCaseListTest, GlobalInit) { 95 std::unique_ptr<SpecialCaseList> SCL = 96 makeSpecialCaseList("global:foo=init\n"); 97 EXPECT_FALSE(SCL->inSection("", "global", "foo")); 98 EXPECT_FALSE(SCL->inSection("", "global", "bar")); 99 EXPECT_TRUE(SCL->inSection("", "global", "foo", "init")); 100 EXPECT_FALSE(SCL->inSection("", "global", "bar", "init")); 101 102 SCL = makeSpecialCaseList("type:t2=init\n"); 103 EXPECT_FALSE(SCL->inSection("", "type", "t1")); 104 EXPECT_FALSE(SCL->inSection("", "type", "t2")); 105 EXPECT_FALSE(SCL->inSection("", "type", "t1", "init")); 106 EXPECT_TRUE(SCL->inSection("", "type", "t2", "init")); 107 108 SCL = makeSpecialCaseList("src:hello=init\n"); 109 EXPECT_FALSE(SCL->inSection("", "src", "hello")); 110 EXPECT_FALSE(SCL->inSection("", "src", "bye")); 111 EXPECT_TRUE(SCL->inSection("", "src", "hello", "init")); 112 EXPECT_FALSE(SCL->inSection("", "src", "bye", "init")); 113 } 114 115 TEST_F(SpecialCaseListTest, Substring) { 116 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:hello\n" 117 "fun:foo\n" 118 "global:bar\n"); 119 EXPECT_FALSE(SCL->inSection("", "src", "othello")); 120 EXPECT_FALSE(SCL->inSection("", "fun", "tomfoolery")); 121 EXPECT_FALSE(SCL->inSection("", "global", "bartender")); 122 123 SCL = makeSpecialCaseList("fun:*foo*\n"); 124 EXPECT_TRUE(SCL->inSection("", "fun", "tomfoolery")); 125 EXPECT_TRUE(SCL->inSection("", "fun", "foobar")); 126 } 127 128 TEST_F(SpecialCaseListTest, InvalidSpecialCaseList) { 129 std::string Error; 130 EXPECT_EQ(nullptr, makeSpecialCaseList("badline", Error)); 131 EXPECT_EQ("malformed line 1: 'badline'", Error); 132 EXPECT_EQ(nullptr, makeSpecialCaseList("src:bad[a-", Error)); 133 EXPECT_EQ("malformed regex in line 1: 'bad[a-': invalid character range", 134 Error); 135 EXPECT_EQ(nullptr, makeSpecialCaseList("src:a.c\n" 136 "fun:fun(a\n", 137 Error)); 138 EXPECT_EQ("malformed regex in line 2: 'fun(a': parentheses not balanced", 139 Error); 140 std::vector<std::string> Files(1, "unexisting"); 141 EXPECT_EQ(nullptr, SpecialCaseList::create(Files, Error)); 142 EXPECT_EQ(0U, Error.find("can't open file 'unexisting':")); 143 } 144 145 TEST_F(SpecialCaseListTest, EmptySpecialCaseList) { 146 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList(""); 147 EXPECT_FALSE(SCL->inSection("", "foo", "bar")); 148 } 149 150 TEST_F(SpecialCaseListTest, MultipleBlacklists) { 151 std::vector<std::string> Files; 152 Files.push_back(makeSpecialCaseListFile("src:bar\n" 153 "src:*foo*\n" 154 "src:ban=init\n")); 155 Files.push_back(makeSpecialCaseListFile("src:baz\n" 156 "src:*fog*\n")); 157 auto SCL = SpecialCaseList::createOrDie(Files); 158 EXPECT_TRUE(SCL->inSection("", "src", "bar")); 159 EXPECT_TRUE(SCL->inSection("", "src", "baz")); 160 EXPECT_FALSE(SCL->inSection("", "src", "ban")); 161 EXPECT_TRUE(SCL->inSection("", "src", "ban", "init")); 162 EXPECT_TRUE(SCL->inSection("", "src", "tomfoolery")); 163 EXPECT_TRUE(SCL->inSection("", "src", "tomfoglery")); 164 for (auto &Path : Files) 165 sys::fs::remove(Path); 166 } 167 168 TEST_F(SpecialCaseListTest, NoTrigramsInRules) { 169 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:b.r\n" 170 "fun:za*az\n"); 171 EXPECT_TRUE(SCL->inSection("", "fun", "bar")); 172 EXPECT_FALSE(SCL->inSection("", "fun", "baz")); 173 EXPECT_TRUE(SCL->inSection("", "fun", "zakaz")); 174 EXPECT_FALSE(SCL->inSection("", "fun", "zaraza")); 175 } 176 177 TEST_F(SpecialCaseListTest, NoTrigramsInARule) { 178 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:*bar*\n" 179 "fun:za*az\n"); 180 EXPECT_TRUE(SCL->inSection("", "fun", "abara")); 181 EXPECT_FALSE(SCL->inSection("", "fun", "bor")); 182 EXPECT_TRUE(SCL->inSection("", "fun", "zakaz")); 183 EXPECT_FALSE(SCL->inSection("", "fun", "zaraza")); 184 } 185 186 TEST_F(SpecialCaseListTest, RepetitiveRule) { 187 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:*bar*bar*bar*bar*\n" 188 "fun:bar*\n"); 189 EXPECT_TRUE(SCL->inSection("", "fun", "bara")); 190 EXPECT_FALSE(SCL->inSection("", "fun", "abara")); 191 EXPECT_TRUE(SCL->inSection("", "fun", "barbarbarbar")); 192 EXPECT_TRUE(SCL->inSection("", "fun", "abarbarbarbar")); 193 EXPECT_FALSE(SCL->inSection("", "fun", "abarbarbar")); 194 } 195 196 TEST_F(SpecialCaseListTest, SpecialSymbolRule) { 197 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:*c\\+\\+abi*\n"); 198 EXPECT_TRUE(SCL->inSection("", "src", "c++abi")); 199 EXPECT_FALSE(SCL->inSection("", "src", "c\\+\\+abi")); 200 } 201 202 TEST_F(SpecialCaseListTest, PopularTrigram) { 203 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:*aaaaaa*\n" 204 "fun:*aaaaa*\n" 205 "fun:*aaaa*\n" 206 "fun:*aaa*\n"); 207 EXPECT_TRUE(SCL->inSection("", "fun", "aaa")); 208 EXPECT_TRUE(SCL->inSection("", "fun", "aaaa")); 209 EXPECT_TRUE(SCL->inSection("", "fun", "aaaabbbaaa")); 210 } 211 212 TEST_F(SpecialCaseListTest, EscapedSymbols) { 213 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:*c\\+\\+abi*\n" 214 "src:*hello\\\\world*\n"); 215 EXPECT_TRUE(SCL->inSection("", "src", "dir/c++abi")); 216 EXPECT_FALSE(SCL->inSection("", "src", "dir/c\\+\\+abi")); 217 EXPECT_FALSE(SCL->inSection("", "src", "c\\+\\+abi")); 218 EXPECT_TRUE(SCL->inSection("", "src", "C:\\hello\\world")); 219 EXPECT_TRUE(SCL->inSection("", "src", "hello\\world")); 220 EXPECT_FALSE(SCL->inSection("", "src", "hello\\\\world")); 221 } 222 223 } 224