blob: 2245588d97cae6c82c9a3a72345e2a2e71992e8f [file] [log] [blame]
Alexey Samsonovb7dd3292014-07-09 19:40:08 +00001//===- SpecialCaseListTest.cpp - Unit tests for SpecialCaseList -----------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alexey Samsonovb7dd3292014-07-09 19:40:08 +00006//
7//===----------------------------------------------------------------------===//
8
Chandler Carruth9a67b072017-06-06 11:06:56 +00009#include "llvm/Support/SpecialCaseList.h"
Alexey Samsonovb9b80272015-02-04 17:39:48 +000010#include "llvm/Support/FileSystem.h"
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000011#include "llvm/Support/MemoryBuffer.h"
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000012#include "gtest/gtest.h"
13
14using namespace llvm;
15
16namespace {
17
18class SpecialCaseListTest : public ::testing::Test {
19protected:
David Blaikie15913f42014-09-02 18:13:54 +000020 std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List,
21 std::string &Error) {
David Blaikiedfbe3d62014-08-27 20:14:18 +000022 std::unique_ptr<MemoryBuffer> MB = MemoryBuffer::getMemBuffer(List);
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000023 return SpecialCaseList::create(MB.get(), Error);
24 }
25
David Blaikie15913f42014-09-02 18:13:54 +000026 std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List) {
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000027 std::string Error;
David Blaikie15913f42014-09-02 18:13:54 +000028 auto SCL = makeSpecialCaseList(List, Error);
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000029 assert(SCL);
30 assert(Error == "");
31 return SCL;
32 }
Alexey Samsonovb9b80272015-02-04 17:39:48 +000033
34 std::string makeSpecialCaseListFile(StringRef Contents) {
35 int FD;
36 SmallString<64> Path;
37 sys::fs::createTemporaryFile("SpecialCaseListTest", "temp", FD, Path);
38 raw_fd_ostream OF(FD, true, true);
39 OF << Contents;
40 OF.close();
41 return Path.str();
42 }
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000043};
44
45TEST_F(SpecialCaseListTest, Basic) {
David Blaikie15913f42014-09-02 18:13:54 +000046 std::unique_ptr<SpecialCaseList> SCL =
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000047 makeSpecialCaseList("# This is a comment.\n"
48 "\n"
49 "src:hello\n"
50 "src:bye\n"
51 "src:hi=category\n"
David Blaikie15913f42014-09-02 18:13:54 +000052 "src:z*=category\n");
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +000053 EXPECT_TRUE(SCL->inSection("", "src", "hello"));
54 EXPECT_TRUE(SCL->inSection("", "src", "bye"));
55 EXPECT_TRUE(SCL->inSection("", "src", "hi", "category"));
56 EXPECT_TRUE(SCL->inSection("", "src", "zzzz", "category"));
57 EXPECT_FALSE(SCL->inSection("", "src", "hi"));
58 EXPECT_FALSE(SCL->inSection("", "fun", "hello"));
59 EXPECT_FALSE(SCL->inSection("", "src", "hello", "category"));
Mitch Phillips40d66632017-11-07 21:16:46 +000060
61 EXPECT_EQ(3u, SCL->inSectionBlame("", "src", "hello"));
62 EXPECT_EQ(4u, SCL->inSectionBlame("", "src", "bye"));
63 EXPECT_EQ(5u, SCL->inSectionBlame("", "src", "hi", "category"));
64 EXPECT_EQ(6u, SCL->inSectionBlame("", "src", "zzzz", "category"));
65 EXPECT_EQ(0u, SCL->inSectionBlame("", "src", "hi"));
66 EXPECT_EQ(0u, SCL->inSectionBlame("", "fun", "hello"));
67 EXPECT_EQ(0u, SCL->inSectionBlame("", "src", "hello", "category"));
68}
69
70TEST_F(SpecialCaseListTest, CorrectErrorLineNumberWithBlankLine) {
71 std::string Error;
72 EXPECT_EQ(nullptr, makeSpecialCaseList("# This is a comment.\n"
73 "\n"
74 "[not valid\n",
75 Error));
76 EXPECT_TRUE(
77 ((StringRef)Error).startswith("malformed section header on line 3:"));
78
79 EXPECT_EQ(nullptr, makeSpecialCaseList("\n\n\n"
80 "[not valid\n",
81 Error));
82 EXPECT_TRUE(
83 ((StringRef)Error).startswith("malformed section header on line 4:"));
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +000084}
85
86TEST_F(SpecialCaseListTest, SectionRegexErrorHandling) {
87 std::string Error;
88 EXPECT_EQ(makeSpecialCaseList("[address", Error), nullptr);
89 EXPECT_TRUE(((StringRef)Error).startswith("malformed section header "));
90
91 EXPECT_EQ(makeSpecialCaseList("[[]", Error), nullptr);
92 EXPECT_TRUE(((StringRef)Error).startswith("malformed regex for section [: "));
Mitch Phillipsfa2eda82017-10-24 23:56:12 +000093
94 EXPECT_EQ(makeSpecialCaseList("src:=", Error), nullptr);
95 EXPECT_TRUE(((StringRef)Error).endswith("Supplied regexp was blank"));
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +000096}
97
98TEST_F(SpecialCaseListTest, Section) {
99 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:global\n"
100 "[sect1|sect2]\n"
101 "src:test1\n"
102 "[sect3*]\n"
103 "src:test2\n");
104 EXPECT_TRUE(SCL->inSection("arbitrary", "src", "global"));
105 EXPECT_TRUE(SCL->inSection("", "src", "global"));
106 EXPECT_TRUE(SCL->inSection("sect1", "src", "test1"));
107 EXPECT_FALSE(SCL->inSection("sect1-arbitrary", "src", "test1"));
108 EXPECT_FALSE(SCL->inSection("sect", "src", "test1"));
109 EXPECT_FALSE(SCL->inSection("sect1", "src", "test2"));
110 EXPECT_TRUE(SCL->inSection("sect2", "src", "test1"));
111 EXPECT_TRUE(SCL->inSection("sect3", "src", "test2"));
112 EXPECT_TRUE(SCL->inSection("sect3-arbitrary", "src", "test2"));
113 EXPECT_FALSE(SCL->inSection("", "src", "test1"));
114 EXPECT_FALSE(SCL->inSection("", "src", "test2"));
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000115}
116
Alexey Samsonovcfb97aa2014-11-20 01:27:19 +0000117TEST_F(SpecialCaseListTest, GlobalInit) {
David Blaikie15913f42014-09-02 18:13:54 +0000118 std::unique_ptr<SpecialCaseList> SCL =
119 makeSpecialCaseList("global:foo=init\n");
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000120 EXPECT_FALSE(SCL->inSection("", "global", "foo"));
121 EXPECT_FALSE(SCL->inSection("", "global", "bar"));
122 EXPECT_TRUE(SCL->inSection("", "global", "foo", "init"));
123 EXPECT_FALSE(SCL->inSection("", "global", "bar", "init"));
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000124
David Blaikie15913f42014-09-02 18:13:54 +0000125 SCL = makeSpecialCaseList("type:t2=init\n");
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000126 EXPECT_FALSE(SCL->inSection("", "type", "t1"));
127 EXPECT_FALSE(SCL->inSection("", "type", "t2"));
128 EXPECT_FALSE(SCL->inSection("", "type", "t1", "init"));
129 EXPECT_TRUE(SCL->inSection("", "type", "t2", "init"));
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000130
David Blaikie15913f42014-09-02 18:13:54 +0000131 SCL = makeSpecialCaseList("src:hello=init\n");
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000132 EXPECT_FALSE(SCL->inSection("", "src", "hello"));
133 EXPECT_FALSE(SCL->inSection("", "src", "bye"));
134 EXPECT_TRUE(SCL->inSection("", "src", "hello", "init"));
135 EXPECT_FALSE(SCL->inSection("", "src", "bye", "init"));
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000136}
137
138TEST_F(SpecialCaseListTest, Substring) {
David Blaikie15913f42014-09-02 18:13:54 +0000139 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:hello\n"
140 "fun:foo\n"
141 "global:bar\n");
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000142 EXPECT_FALSE(SCL->inSection("", "src", "othello"));
143 EXPECT_FALSE(SCL->inSection("", "fun", "tomfoolery"));
144 EXPECT_FALSE(SCL->inSection("", "global", "bartender"));
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000145
David Blaikie15913f42014-09-02 18:13:54 +0000146 SCL = makeSpecialCaseList("fun:*foo*\n");
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000147 EXPECT_TRUE(SCL->inSection("", "fun", "tomfoolery"));
148 EXPECT_TRUE(SCL->inSection("", "fun", "foobar"));
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000149}
150
151TEST_F(SpecialCaseListTest, InvalidSpecialCaseList) {
152 std::string Error;
153 EXPECT_EQ(nullptr, makeSpecialCaseList("badline", Error));
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000154 EXPECT_EQ("malformed line 1: 'badline'", Error);
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000155 EXPECT_EQ(nullptr, makeSpecialCaseList("src:bad[a-", Error));
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000156 EXPECT_EQ("malformed regex in line 1: 'bad[a-': invalid character range",
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000157 Error);
158 EXPECT_EQ(nullptr, makeSpecialCaseList("src:a.c\n"
159 "fun:fun(a\n",
160 Error));
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000161 EXPECT_EQ("malformed regex in line 2: 'fun(a': parentheses not balanced",
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000162 Error);
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000163 std::vector<std::string> Files(1, "unexisting");
164 EXPECT_EQ(nullptr, SpecialCaseList::create(Files, Error));
165 EXPECT_EQ(0U, Error.find("can't open file 'unexisting':"));
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000166}
167
168TEST_F(SpecialCaseListTest, EmptySpecialCaseList) {
David Blaikie15913f42014-09-02 18:13:54 +0000169 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("");
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000170 EXPECT_FALSE(SCL->inSection("", "foo", "bar"));
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000171}
172
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000173TEST_F(SpecialCaseListTest, MultipleBlacklists) {
174 std::vector<std::string> Files;
175 Files.push_back(makeSpecialCaseListFile("src:bar\n"
176 "src:*foo*\n"
177 "src:ban=init\n"));
178 Files.push_back(makeSpecialCaseListFile("src:baz\n"
179 "src:*fog*\n"));
180 auto SCL = SpecialCaseList::createOrDie(Files);
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000181 EXPECT_TRUE(SCL->inSection("", "src", "bar"));
182 EXPECT_TRUE(SCL->inSection("", "src", "baz"));
183 EXPECT_FALSE(SCL->inSection("", "src", "ban"));
184 EXPECT_TRUE(SCL->inSection("", "src", "ban", "init"));
185 EXPECT_TRUE(SCL->inSection("", "src", "tomfoolery"));
186 EXPECT_TRUE(SCL->inSection("", "src", "tomfoglery"));
Reid Kleckner75e557f2016-09-02 00:51:34 +0000187 for (auto &Path : Files)
188 sys::fs::remove(Path);
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000189}
190
Ivan Krasin3dade412016-12-01 02:54:54 +0000191TEST_F(SpecialCaseListTest, NoTrigramsInRules) {
192 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:b.r\n"
193 "fun:za*az\n");
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000194 EXPECT_TRUE(SCL->inSection("", "fun", "bar"));
195 EXPECT_FALSE(SCL->inSection("", "fun", "baz"));
196 EXPECT_TRUE(SCL->inSection("", "fun", "zakaz"));
197 EXPECT_FALSE(SCL->inSection("", "fun", "zaraza"));
Ivan Krasin3dade412016-12-01 02:54:54 +0000198}
199
200TEST_F(SpecialCaseListTest, NoTrigramsInARule) {
201 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:*bar*\n"
202 "fun:za*az\n");
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000203 EXPECT_TRUE(SCL->inSection("", "fun", "abara"));
204 EXPECT_FALSE(SCL->inSection("", "fun", "bor"));
205 EXPECT_TRUE(SCL->inSection("", "fun", "zakaz"));
206 EXPECT_FALSE(SCL->inSection("", "fun", "zaraza"));
Ivan Krasin3dade412016-12-01 02:54:54 +0000207}
208
209TEST_F(SpecialCaseListTest, RepetitiveRule) {
210 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:*bar*bar*bar*bar*\n"
211 "fun:bar*\n");
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000212 EXPECT_TRUE(SCL->inSection("", "fun", "bara"));
213 EXPECT_FALSE(SCL->inSection("", "fun", "abara"));
214 EXPECT_TRUE(SCL->inSection("", "fun", "barbarbarbar"));
215 EXPECT_TRUE(SCL->inSection("", "fun", "abarbarbarbar"));
216 EXPECT_FALSE(SCL->inSection("", "fun", "abarbarbar"));
Ivan Krasin3dade412016-12-01 02:54:54 +0000217}
218
219TEST_F(SpecialCaseListTest, SpecialSymbolRule) {
220 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:*c\\+\\+abi*\n");
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000221 EXPECT_TRUE(SCL->inSection("", "src", "c++abi"));
222 EXPECT_FALSE(SCL->inSection("", "src", "c\\+\\+abi"));
Ivan Krasin3dade412016-12-01 02:54:54 +0000223}
224
225TEST_F(SpecialCaseListTest, PopularTrigram) {
226 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:*aaaaaa*\n"
227 "fun:*aaaaa*\n"
228 "fun:*aaaa*\n"
229 "fun:*aaa*\n");
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000230 EXPECT_TRUE(SCL->inSection("", "fun", "aaa"));
231 EXPECT_TRUE(SCL->inSection("", "fun", "aaaa"));
232 EXPECT_TRUE(SCL->inSection("", "fun", "aaaabbbaaa"));
Ivan Krasin3dade412016-12-01 02:54:54 +0000233}
234
Ivan Krasin75453b02016-12-02 23:30:16 +0000235TEST_F(SpecialCaseListTest, EscapedSymbols) {
236 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:*c\\+\\+abi*\n"
237 "src:*hello\\\\world*\n");
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000238 EXPECT_TRUE(SCL->inSection("", "src", "dir/c++abi"));
239 EXPECT_FALSE(SCL->inSection("", "src", "dir/c\\+\\+abi"));
240 EXPECT_FALSE(SCL->inSection("", "src", "c\\+\\+abi"));
241 EXPECT_TRUE(SCL->inSection("", "src", "C:\\hello\\world"));
242 EXPECT_TRUE(SCL->inSection("", "src", "hello\\world"));
243 EXPECT_FALSE(SCL->inSection("", "src", "hello\\\\world"));
Ivan Krasin75453b02016-12-02 23:30:16 +0000244}
245
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000246}