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 EXPECT_EQ(3u, SCL->inSectionBlame("", "src", "hello")); 63 EXPECT_EQ(4u, SCL->inSectionBlame("", "src", "bye")); 64 EXPECT_EQ(5u, SCL->inSectionBlame("", "src", "hi", "category")); 65 EXPECT_EQ(6u, SCL->inSectionBlame("", "src", "zzzz", "category")); 66 EXPECT_EQ(0u, SCL->inSectionBlame("", "src", "hi")); 67 EXPECT_EQ(0u, SCL->inSectionBlame("", "fun", "hello")); 68 EXPECT_EQ(0u, SCL->inSectionBlame("", "src", "hello", "category")); 69 } 70 71 TEST_F(SpecialCaseListTest, CorrectErrorLineNumberWithBlankLine) { 72 std::string Error; 73 EXPECT_EQ(nullptr, makeSpecialCaseList("# This is a comment.\n" 74 "\n" 75 "[not valid\n", 76 Error)); 77 EXPECT_TRUE( 78 ((StringRef)Error).startswith("malformed section header on line 3:")); 79 80 EXPECT_EQ(nullptr, makeSpecialCaseList("\n\n\n" 81 "[not valid\n", 82 Error)); 83 EXPECT_TRUE( 84 ((StringRef)Error).startswith("malformed section header on line 4:")); 85 } 86 87 TEST_F(SpecialCaseListTest, SectionRegexErrorHandling) { 88 std::string Error; 89 EXPECT_EQ(makeSpecialCaseList("[address", Error), nullptr); 90 EXPECT_TRUE(((StringRef)Error).startswith("malformed section header ")); 91 92 EXPECT_EQ(makeSpecialCaseList("[[]", Error), nullptr); 93 EXPECT_TRUE(((StringRef)Error).startswith("malformed regex for section [: ")); 94 95 EXPECT_EQ(makeSpecialCaseList("src:=", Error), nullptr); 96 EXPECT_TRUE(((StringRef)Error).endswith("Supplied regexp was blank")); 97 } 98 99 TEST_F(SpecialCaseListTest, Section) { 100 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:global\n" 101 "[sect1|sect2]\n" 102 "src:test1\n" 103 "[sect3*]\n" 104 "src:test2\n"); 105 EXPECT_TRUE(SCL->inSection("arbitrary", "src", "global")); 106 EXPECT_TRUE(SCL->inSection("", "src", "global")); 107 EXPECT_TRUE(SCL->inSection("sect1", "src", "test1")); 108 EXPECT_FALSE(SCL->inSection("sect1-arbitrary", "src", "test1")); 109 EXPECT_FALSE(SCL->inSection("sect", "src", "test1")); 110 EXPECT_FALSE(SCL->inSection("sect1", "src", "test2")); 111 EXPECT_TRUE(SCL->inSection("sect2", "src", "test1")); 112 EXPECT_TRUE(SCL->inSection("sect3", "src", "test2")); 113 EXPECT_TRUE(SCL->inSection("sect3-arbitrary", "src", "test2")); 114 EXPECT_FALSE(SCL->inSection("", "src", "test1")); 115 EXPECT_FALSE(SCL->inSection("", "src", "test2")); 116 } 117 118 TEST_F(SpecialCaseListTest, GlobalInit) { 119 std::unique_ptr<SpecialCaseList> SCL = 120 makeSpecialCaseList("global:foo=init\n"); 121 EXPECT_FALSE(SCL->inSection("", "global", "foo")); 122 EXPECT_FALSE(SCL->inSection("", "global", "bar")); 123 EXPECT_TRUE(SCL->inSection("", "global", "foo", "init")); 124 EXPECT_FALSE(SCL->inSection("", "global", "bar", "init")); 125 126 SCL = makeSpecialCaseList("type:t2=init\n"); 127 EXPECT_FALSE(SCL->inSection("", "type", "t1")); 128 EXPECT_FALSE(SCL->inSection("", "type", "t2")); 129 EXPECT_FALSE(SCL->inSection("", "type", "t1", "init")); 130 EXPECT_TRUE(SCL->inSection("", "type", "t2", "init")); 131 132 SCL = makeSpecialCaseList("src:hello=init\n"); 133 EXPECT_FALSE(SCL->inSection("", "src", "hello")); 134 EXPECT_FALSE(SCL->inSection("", "src", "bye")); 135 EXPECT_TRUE(SCL->inSection("", "src", "hello", "init")); 136 EXPECT_FALSE(SCL->inSection("", "src", "bye", "init")); 137 } 138 139 TEST_F(SpecialCaseListTest, Substring) { 140 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:hello\n" 141 "fun:foo\n" 142 "global:bar\n"); 143 EXPECT_FALSE(SCL->inSection("", "src", "othello")); 144 EXPECT_FALSE(SCL->inSection("", "fun", "tomfoolery")); 145 EXPECT_FALSE(SCL->inSection("", "global", "bartender")); 146 147 SCL = makeSpecialCaseList("fun:*foo*\n"); 148 EXPECT_TRUE(SCL->inSection("", "fun", "tomfoolery")); 149 EXPECT_TRUE(SCL->inSection("", "fun", "foobar")); 150 } 151 152 TEST_F(SpecialCaseListTest, InvalidSpecialCaseList) { 153 std::string Error; 154 EXPECT_EQ(nullptr, makeSpecialCaseList("badline", Error)); 155 EXPECT_EQ("malformed line 1: 'badline'", Error); 156 EXPECT_EQ(nullptr, makeSpecialCaseList("src:bad[a-", Error)); 157 EXPECT_EQ("malformed regex in line 1: 'bad[a-': invalid character range", 158 Error); 159 EXPECT_EQ(nullptr, makeSpecialCaseList("src:a.c\n" 160 "fun:fun(a\n", 161 Error)); 162 EXPECT_EQ("malformed regex in line 2: 'fun(a': parentheses not balanced", 163 Error); 164 std::vector<std::string> Files(1, "unexisting"); 165 EXPECT_EQ(nullptr, SpecialCaseList::create(Files, Error)); 166 EXPECT_EQ(0U, Error.find("can't open file 'unexisting':")); 167 } 168 169 TEST_F(SpecialCaseListTest, EmptySpecialCaseList) { 170 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList(""); 171 EXPECT_FALSE(SCL->inSection("", "foo", "bar")); 172 } 173 174 TEST_F(SpecialCaseListTest, MultipleBlacklists) { 175 std::vector<std::string> Files; 176 Files.push_back(makeSpecialCaseListFile("src:bar\n" 177 "src:*foo*\n" 178 "src:ban=init\n")); 179 Files.push_back(makeSpecialCaseListFile("src:baz\n" 180 "src:*fog*\n")); 181 auto SCL = SpecialCaseList::createOrDie(Files); 182 EXPECT_TRUE(SCL->inSection("", "src", "bar")); 183 EXPECT_TRUE(SCL->inSection("", "src", "baz")); 184 EXPECT_FALSE(SCL->inSection("", "src", "ban")); 185 EXPECT_TRUE(SCL->inSection("", "src", "ban", "init")); 186 EXPECT_TRUE(SCL->inSection("", "src", "tomfoolery")); 187 EXPECT_TRUE(SCL->inSection("", "src", "tomfoglery")); 188 for (auto &Path : Files) 189 sys::fs::remove(Path); 190 } 191 192 TEST_F(SpecialCaseListTest, NoTrigramsInRules) { 193 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:b.r\n" 194 "fun:za*az\n"); 195 EXPECT_TRUE(SCL->inSection("", "fun", "bar")); 196 EXPECT_FALSE(SCL->inSection("", "fun", "baz")); 197 EXPECT_TRUE(SCL->inSection("", "fun", "zakaz")); 198 EXPECT_FALSE(SCL->inSection("", "fun", "zaraza")); 199 } 200 201 TEST_F(SpecialCaseListTest, NoTrigramsInARule) { 202 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:*bar*\n" 203 "fun:za*az\n"); 204 EXPECT_TRUE(SCL->inSection("", "fun", "abara")); 205 EXPECT_FALSE(SCL->inSection("", "fun", "bor")); 206 EXPECT_TRUE(SCL->inSection("", "fun", "zakaz")); 207 EXPECT_FALSE(SCL->inSection("", "fun", "zaraza")); 208 } 209 210 TEST_F(SpecialCaseListTest, RepetitiveRule) { 211 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:*bar*bar*bar*bar*\n" 212 "fun:bar*\n"); 213 EXPECT_TRUE(SCL->inSection("", "fun", "bara")); 214 EXPECT_FALSE(SCL->inSection("", "fun", "abara")); 215 EXPECT_TRUE(SCL->inSection("", "fun", "barbarbarbar")); 216 EXPECT_TRUE(SCL->inSection("", "fun", "abarbarbarbar")); 217 EXPECT_FALSE(SCL->inSection("", "fun", "abarbarbar")); 218 } 219 220 TEST_F(SpecialCaseListTest, SpecialSymbolRule) { 221 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:*c\\+\\+abi*\n"); 222 EXPECT_TRUE(SCL->inSection("", "src", "c++abi")); 223 EXPECT_FALSE(SCL->inSection("", "src", "c\\+\\+abi")); 224 } 225 226 TEST_F(SpecialCaseListTest, PopularTrigram) { 227 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:*aaaaaa*\n" 228 "fun:*aaaaa*\n" 229 "fun:*aaaa*\n" 230 "fun:*aaa*\n"); 231 EXPECT_TRUE(SCL->inSection("", "fun", "aaa")); 232 EXPECT_TRUE(SCL->inSection("", "fun", "aaaa")); 233 EXPECT_TRUE(SCL->inSection("", "fun", "aaaabbbaaa")); 234 } 235 236 TEST_F(SpecialCaseListTest, EscapedSymbols) { 237 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:*c\\+\\+abi*\n" 238 "src:*hello\\\\world*\n"); 239 EXPECT_TRUE(SCL->inSection("", "src", "dir/c++abi")); 240 EXPECT_FALSE(SCL->inSection("", "src", "dir/c\\+\\+abi")); 241 EXPECT_FALSE(SCL->inSection("", "src", "c\\+\\+abi")); 242 EXPECT_TRUE(SCL->inSection("", "src", "C:\\hello\\world")); 243 EXPECT_TRUE(SCL->inSection("", "src", "hello\\world")); 244 EXPECT_FALSE(SCL->inSection("", "src", "hello\\\\world")); 245 } 246 247 } 248