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/FileSystem.h" 11 #include "llvm/Support/MemoryBuffer.h" 12 #include "llvm/Support/SpecialCaseList.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, GlobalInit) { 64 std::unique_ptr<SpecialCaseList> SCL = 65 makeSpecialCaseList("global:foo=init\n"); 66 EXPECT_FALSE(SCL->inSection("global", "foo")); 67 EXPECT_FALSE(SCL->inSection("global", "bar")); 68 EXPECT_TRUE(SCL->inSection("global", "foo", "init")); 69 EXPECT_FALSE(SCL->inSection("global", "bar", "init")); 70 71 SCL = makeSpecialCaseList("type:t2=init\n"); 72 EXPECT_FALSE(SCL->inSection("type", "t1")); 73 EXPECT_FALSE(SCL->inSection("type", "t2")); 74 EXPECT_FALSE(SCL->inSection("type", "t1", "init")); 75 EXPECT_TRUE(SCL->inSection("type", "t2", "init")); 76 77 SCL = makeSpecialCaseList("src:hello=init\n"); 78 EXPECT_FALSE(SCL->inSection("src", "hello")); 79 EXPECT_FALSE(SCL->inSection("src", "bye")); 80 EXPECT_TRUE(SCL->inSection("src", "hello", "init")); 81 EXPECT_FALSE(SCL->inSection("src", "bye", "init")); 82 } 83 84 TEST_F(SpecialCaseListTest, Substring) { 85 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:hello\n" 86 "fun:foo\n" 87 "global:bar\n"); 88 EXPECT_FALSE(SCL->inSection("src", "othello")); 89 EXPECT_FALSE(SCL->inSection("fun", "tomfoolery")); 90 EXPECT_FALSE(SCL->inSection("global", "bartender")); 91 92 SCL = makeSpecialCaseList("fun:*foo*\n"); 93 EXPECT_TRUE(SCL->inSection("fun", "tomfoolery")); 94 EXPECT_TRUE(SCL->inSection("fun", "foobar")); 95 } 96 97 TEST_F(SpecialCaseListTest, InvalidSpecialCaseList) { 98 std::string Error; 99 EXPECT_EQ(nullptr, makeSpecialCaseList("badline", Error)); 100 EXPECT_EQ("malformed line 1: 'badline'", Error); 101 EXPECT_EQ(nullptr, makeSpecialCaseList("src:bad[a-", Error)); 102 EXPECT_EQ("malformed regex in line 1: 'bad[a-': invalid character range", 103 Error); 104 EXPECT_EQ(nullptr, makeSpecialCaseList("src:a.c\n" 105 "fun:fun(a\n", 106 Error)); 107 EXPECT_EQ("malformed regex in line 2: 'fun(a': parentheses not balanced", 108 Error); 109 std::vector<std::string> Files(1, "unexisting"); 110 EXPECT_EQ(nullptr, SpecialCaseList::create(Files, Error)); 111 EXPECT_EQ(0U, Error.find("can't open file 'unexisting':")); 112 } 113 114 TEST_F(SpecialCaseListTest, EmptySpecialCaseList) { 115 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList(""); 116 EXPECT_FALSE(SCL->inSection("foo", "bar")); 117 } 118 119 TEST_F(SpecialCaseListTest, MultipleBlacklists) { 120 std::vector<std::string> Files; 121 Files.push_back(makeSpecialCaseListFile("src:bar\n" 122 "src:*foo*\n" 123 "src:ban=init\n")); 124 Files.push_back(makeSpecialCaseListFile("src:baz\n" 125 "src:*fog*\n")); 126 auto SCL = SpecialCaseList::createOrDie(Files); 127 EXPECT_TRUE(SCL->inSection("src", "bar")); 128 EXPECT_TRUE(SCL->inSection("src", "baz")); 129 EXPECT_FALSE(SCL->inSection("src", "ban")); 130 EXPECT_TRUE(SCL->inSection("src", "ban", "init")); 131 EXPECT_TRUE(SCL->inSection("src", "tomfoolery")); 132 EXPECT_TRUE(SCL->inSection("src", "tomfoglery")); 133 for (auto &Path : Files) 134 sys::fs::remove(Path); 135 } 136 137 TEST_F(SpecialCaseListTest, NoTrigramsInRules) { 138 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:b.r\n" 139 "fun:za*az\n"); 140 EXPECT_TRUE(SCL->inSection("fun", "bar")); 141 EXPECT_FALSE(SCL->inSection("fun", "baz")); 142 EXPECT_TRUE(SCL->inSection("fun", "zakaz")); 143 EXPECT_FALSE(SCL->inSection("fun", "zaraza")); 144 } 145 146 TEST_F(SpecialCaseListTest, NoTrigramsInARule) { 147 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:*bar*\n" 148 "fun:za*az\n"); 149 EXPECT_TRUE(SCL->inSection("fun", "abara")); 150 EXPECT_FALSE(SCL->inSection("fun", "bor")); 151 EXPECT_TRUE(SCL->inSection("fun", "zakaz")); 152 EXPECT_FALSE(SCL->inSection("fun", "zaraza")); 153 } 154 155 TEST_F(SpecialCaseListTest, RepetitiveRule) { 156 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:*bar*bar*bar*bar*\n" 157 "fun:bar*\n"); 158 EXPECT_TRUE(SCL->inSection("fun", "bara")); 159 EXPECT_FALSE(SCL->inSection("fun", "abara")); 160 EXPECT_TRUE(SCL->inSection("fun", "barbarbarbar")); 161 EXPECT_TRUE(SCL->inSection("fun", "abarbarbarbar")); 162 EXPECT_FALSE(SCL->inSection("fun", "abarbarbar")); 163 } 164 165 TEST_F(SpecialCaseListTest, SpecialSymbolRule) { 166 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:*c\\+\\+abi*\n"); 167 EXPECT_TRUE(SCL->inSection("src", "c++abi")); 168 EXPECT_FALSE(SCL->inSection("src", "c\\+\\+abi")); 169 } 170 171 TEST_F(SpecialCaseListTest, PopularTrigram) { 172 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:*aaaaaa*\n" 173 "fun:*aaaaa*\n" 174 "fun:*aaaa*\n" 175 "fun:*aaa*\n"); 176 EXPECT_TRUE(SCL->inSection("fun", "aaa")); 177 EXPECT_TRUE(SCL->inSection("fun", "aaaa")); 178 EXPECT_TRUE(SCL->inSection("fun", "aaaabbbaaa")); 179 } 180 181 TEST_F(SpecialCaseListTest, EscapedSymbols) { 182 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:*c\\+\\+abi*\n" 183 "src:*hello\\\\world*\n"); 184 EXPECT_TRUE(SCL->inSection("src", "dir/c++abi")); 185 EXPECT_FALSE(SCL->inSection("src", "dir/c\\+\\+abi")); 186 EXPECT_FALSE(SCL->inSection("src", "c\\+\\+abi")); 187 EXPECT_TRUE(SCL->inSection("src", "C:\\hello\\world")); 188 EXPECT_TRUE(SCL->inSection("src", "hello\\world")); 189 EXPECT_FALSE(SCL->inSection("src", "hello\\\\world")); 190 } 191 192 } 193