SpecialCaseListTest.cpp 10 KB

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