Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | 9a67b07 | 2017-06-06 11:06:56 +0000 | [diff] [blame] | 10 | #include "llvm/Support/SpecialCaseList.h" |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame] | 11 | #include "llvm/Support/FileSystem.h" |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 12 | #include "llvm/Support/MemoryBuffer.h" |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 13 | #include "gtest/gtest.h" |
| 14 | |
| 15 | using namespace llvm; |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | class SpecialCaseListTest : public ::testing::Test { |
| 20 | protected: |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 21 | std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List, |
| 22 | std::string &Error) { |
David Blaikie | dfbe3d6 | 2014-08-27 20:14:18 +0000 | [diff] [blame] | 23 | std::unique_ptr<MemoryBuffer> MB = MemoryBuffer::getMemBuffer(List); |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 24 | return SpecialCaseList::create(MB.get(), Error); |
| 25 | } |
| 26 | |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 27 | std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List) { |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 28 | std::string Error; |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 29 | auto SCL = makeSpecialCaseList(List, Error); |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 30 | assert(SCL); |
| 31 | assert(Error == ""); |
| 32 | return SCL; |
| 33 | } |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame] | 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 | } |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | TEST_F(SpecialCaseListTest, Basic) { |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 47 | std::unique_ptr<SpecialCaseList> SCL = |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 48 | makeSpecialCaseList("# This is a comment.\n" |
| 49 | "\n" |
| 50 | "src:hello\n" |
| 51 | "src:bye\n" |
| 52 | "src:hi=category\n" |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 53 | "src:z*=category\n"); |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame^] | 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 | |
| 72 | TEST_F(SpecialCaseListTest, Section) { |
| 73 | std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:global\n" |
| 74 | "[sect1|sect2]\n" |
| 75 | "src:test1\n" |
| 76 | "[sect3*]\n" |
| 77 | "src:test2\n"); |
| 78 | EXPECT_TRUE(SCL->inSection("arbitrary", "src", "global")); |
| 79 | EXPECT_TRUE(SCL->inSection("", "src", "global")); |
| 80 | EXPECT_TRUE(SCL->inSection("sect1", "src", "test1")); |
| 81 | EXPECT_FALSE(SCL->inSection("sect1-arbitrary", "src", "test1")); |
| 82 | EXPECT_FALSE(SCL->inSection("sect", "src", "test1")); |
| 83 | EXPECT_FALSE(SCL->inSection("sect1", "src", "test2")); |
| 84 | EXPECT_TRUE(SCL->inSection("sect2", "src", "test1")); |
| 85 | EXPECT_TRUE(SCL->inSection("sect3", "src", "test2")); |
| 86 | EXPECT_TRUE(SCL->inSection("sect3-arbitrary", "src", "test2")); |
| 87 | EXPECT_FALSE(SCL->inSection("", "src", "test1")); |
| 88 | EXPECT_FALSE(SCL->inSection("", "src", "test2")); |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Alexey Samsonov | cfb97aa | 2014-11-20 01:27:19 +0000 | [diff] [blame] | 91 | TEST_F(SpecialCaseListTest, GlobalInit) { |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 92 | std::unique_ptr<SpecialCaseList> SCL = |
| 93 | makeSpecialCaseList("global:foo=init\n"); |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame^] | 94 | EXPECT_FALSE(SCL->inSection("", "global", "foo")); |
| 95 | EXPECT_FALSE(SCL->inSection("", "global", "bar")); |
| 96 | EXPECT_TRUE(SCL->inSection("", "global", "foo", "init")); |
| 97 | EXPECT_FALSE(SCL->inSection("", "global", "bar", "init")); |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 98 | |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 99 | SCL = makeSpecialCaseList("type:t2=init\n"); |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame^] | 100 | EXPECT_FALSE(SCL->inSection("", "type", "t1")); |
| 101 | EXPECT_FALSE(SCL->inSection("", "type", "t2")); |
| 102 | EXPECT_FALSE(SCL->inSection("", "type", "t1", "init")); |
| 103 | EXPECT_TRUE(SCL->inSection("", "type", "t2", "init")); |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 104 | |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 105 | SCL = makeSpecialCaseList("src:hello=init\n"); |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame^] | 106 | EXPECT_FALSE(SCL->inSection("", "src", "hello")); |
| 107 | EXPECT_FALSE(SCL->inSection("", "src", "bye")); |
| 108 | EXPECT_TRUE(SCL->inSection("", "src", "hello", "init")); |
| 109 | EXPECT_FALSE(SCL->inSection("", "src", "bye", "init")); |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | TEST_F(SpecialCaseListTest, Substring) { |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 113 | std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:hello\n" |
| 114 | "fun:foo\n" |
| 115 | "global:bar\n"); |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame^] | 116 | EXPECT_FALSE(SCL->inSection("", "src", "othello")); |
| 117 | EXPECT_FALSE(SCL->inSection("", "fun", "tomfoolery")); |
| 118 | EXPECT_FALSE(SCL->inSection("", "global", "bartender")); |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 119 | |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 120 | SCL = makeSpecialCaseList("fun:*foo*\n"); |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame^] | 121 | EXPECT_TRUE(SCL->inSection("", "fun", "tomfoolery")); |
| 122 | EXPECT_TRUE(SCL->inSection("", "fun", "foobar")); |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | TEST_F(SpecialCaseListTest, InvalidSpecialCaseList) { |
| 126 | std::string Error; |
| 127 | EXPECT_EQ(nullptr, makeSpecialCaseList("badline", Error)); |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame] | 128 | EXPECT_EQ("malformed line 1: 'badline'", Error); |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 129 | EXPECT_EQ(nullptr, makeSpecialCaseList("src:bad[a-", Error)); |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame] | 130 | EXPECT_EQ("malformed regex in line 1: 'bad[a-': invalid character range", |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 131 | Error); |
| 132 | EXPECT_EQ(nullptr, makeSpecialCaseList("src:a.c\n" |
| 133 | "fun:fun(a\n", |
| 134 | Error)); |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame] | 135 | EXPECT_EQ("malformed regex in line 2: 'fun(a': parentheses not balanced", |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 136 | Error); |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame] | 137 | std::vector<std::string> Files(1, "unexisting"); |
| 138 | EXPECT_EQ(nullptr, SpecialCaseList::create(Files, Error)); |
| 139 | EXPECT_EQ(0U, Error.find("can't open file 'unexisting':")); |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | TEST_F(SpecialCaseListTest, EmptySpecialCaseList) { |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 143 | std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList(""); |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame^] | 144 | EXPECT_FALSE(SCL->inSection("", "foo", "bar")); |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame] | 147 | TEST_F(SpecialCaseListTest, MultipleBlacklists) { |
| 148 | std::vector<std::string> Files; |
| 149 | Files.push_back(makeSpecialCaseListFile("src:bar\n" |
| 150 | "src:*foo*\n" |
| 151 | "src:ban=init\n")); |
| 152 | Files.push_back(makeSpecialCaseListFile("src:baz\n" |
| 153 | "src:*fog*\n")); |
| 154 | auto SCL = SpecialCaseList::createOrDie(Files); |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame^] | 155 | EXPECT_TRUE(SCL->inSection("", "src", "bar")); |
| 156 | EXPECT_TRUE(SCL->inSection("", "src", "baz")); |
| 157 | EXPECT_FALSE(SCL->inSection("", "src", "ban")); |
| 158 | EXPECT_TRUE(SCL->inSection("", "src", "ban", "init")); |
| 159 | EXPECT_TRUE(SCL->inSection("", "src", "tomfoolery")); |
| 160 | EXPECT_TRUE(SCL->inSection("", "src", "tomfoglery")); |
Reid Kleckner | 75e557f | 2016-09-02 00:51:34 +0000 | [diff] [blame] | 161 | for (auto &Path : Files) |
| 162 | sys::fs::remove(Path); |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Ivan Krasin | 3dade41 | 2016-12-01 02:54:54 +0000 | [diff] [blame] | 165 | TEST_F(SpecialCaseListTest, NoTrigramsInRules) { |
| 166 | std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:b.r\n" |
| 167 | "fun:za*az\n"); |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame^] | 168 | EXPECT_TRUE(SCL->inSection("", "fun", "bar")); |
| 169 | EXPECT_FALSE(SCL->inSection("", "fun", "baz")); |
| 170 | EXPECT_TRUE(SCL->inSection("", "fun", "zakaz")); |
| 171 | EXPECT_FALSE(SCL->inSection("", "fun", "zaraza")); |
Ivan Krasin | 3dade41 | 2016-12-01 02:54:54 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | TEST_F(SpecialCaseListTest, NoTrigramsInARule) { |
| 175 | std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:*bar*\n" |
| 176 | "fun:za*az\n"); |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame^] | 177 | EXPECT_TRUE(SCL->inSection("", "fun", "abara")); |
| 178 | EXPECT_FALSE(SCL->inSection("", "fun", "bor")); |
| 179 | EXPECT_TRUE(SCL->inSection("", "fun", "zakaz")); |
| 180 | EXPECT_FALSE(SCL->inSection("", "fun", "zaraza")); |
Ivan Krasin | 3dade41 | 2016-12-01 02:54:54 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | TEST_F(SpecialCaseListTest, RepetitiveRule) { |
| 184 | std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:*bar*bar*bar*bar*\n" |
| 185 | "fun:bar*\n"); |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame^] | 186 | EXPECT_TRUE(SCL->inSection("", "fun", "bara")); |
| 187 | EXPECT_FALSE(SCL->inSection("", "fun", "abara")); |
| 188 | EXPECT_TRUE(SCL->inSection("", "fun", "barbarbarbar")); |
| 189 | EXPECT_TRUE(SCL->inSection("", "fun", "abarbarbarbar")); |
| 190 | EXPECT_FALSE(SCL->inSection("", "fun", "abarbarbar")); |
Ivan Krasin | 3dade41 | 2016-12-01 02:54:54 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | TEST_F(SpecialCaseListTest, SpecialSymbolRule) { |
| 194 | std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:*c\\+\\+abi*\n"); |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame^] | 195 | EXPECT_TRUE(SCL->inSection("", "src", "c++abi")); |
| 196 | EXPECT_FALSE(SCL->inSection("", "src", "c\\+\\+abi")); |
Ivan Krasin | 3dade41 | 2016-12-01 02:54:54 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | TEST_F(SpecialCaseListTest, PopularTrigram) { |
| 200 | std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:*aaaaaa*\n" |
| 201 | "fun:*aaaaa*\n" |
| 202 | "fun:*aaaa*\n" |
| 203 | "fun:*aaa*\n"); |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame^] | 204 | EXPECT_TRUE(SCL->inSection("", "fun", "aaa")); |
| 205 | EXPECT_TRUE(SCL->inSection("", "fun", "aaaa")); |
| 206 | EXPECT_TRUE(SCL->inSection("", "fun", "aaaabbbaaa")); |
Ivan Krasin | 3dade41 | 2016-12-01 02:54:54 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Ivan Krasin | 75453b0 | 2016-12-02 23:30:16 +0000 | [diff] [blame] | 209 | TEST_F(SpecialCaseListTest, EscapedSymbols) { |
| 210 | std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:*c\\+\\+abi*\n" |
| 211 | "src:*hello\\\\world*\n"); |
Vlad Tsyrklevich | 998b220 | 2017-09-25 22:11:11 +0000 | [diff] [blame^] | 212 | EXPECT_TRUE(SCL->inSection("", "src", "dir/c++abi")); |
| 213 | EXPECT_FALSE(SCL->inSection("", "src", "dir/c\\+\\+abi")); |
| 214 | EXPECT_FALSE(SCL->inSection("", "src", "c\\+\\+abi")); |
| 215 | EXPECT_TRUE(SCL->inSection("", "src", "C:\\hello\\world")); |
| 216 | EXPECT_TRUE(SCL->inSection("", "src", "hello\\world")); |
| 217 | EXPECT_FALSE(SCL->inSection("", "src", "hello\\\\world")); |
Ivan Krasin | 75453b0 | 2016-12-02 23:30:16 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame] | 220 | } |