blob: e8c30d07e78cee5274bb909e04362c0f844d651b [file] [log] [blame]
Sergey Matveeva52e5c62013-06-26 15:37:14 +00001//===-- sanitizer_suppressions_test.cc ------------------------------------===//
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// This file is a part of ThreadSanitizer/AddressSanitizer runtime.
11//
12//===----------------------------------------------------------------------===//
13#include "sanitizer_common/sanitizer_suppressions.h"
14#include "gtest/gtest.h"
15
16#include <string.h>
17
18namespace __sanitizer {
19
20static bool MyMatch(const char *templ, const char *func) {
21 char tmp[1024];
22 strcpy(tmp, templ); // NOLINT
23 return TemplateMatch(tmp, func);
24}
25
26TEST(Suppressions, Match) {
Dmitry Vyukov55e6f3f2013-07-16 16:44:15 +000027 EXPECT_TRUE(MyMatch("foobar$", "foobar"));
28
Sergey Matveeva52e5c62013-06-26 15:37:14 +000029 EXPECT_TRUE(MyMatch("foobar", "foobar"));
Dmitry Vyukov55e6f3f2013-07-16 16:44:15 +000030 EXPECT_TRUE(MyMatch("*foobar*", "foobar"));
Sergey Matveeva52e5c62013-06-26 15:37:14 +000031 EXPECT_TRUE(MyMatch("foobar", "prefix_foobar_postfix"));
32 EXPECT_TRUE(MyMatch("*foobar*", "prefix_foobar_postfix"));
33 EXPECT_TRUE(MyMatch("foo*bar", "foo_middle_bar"));
34 EXPECT_TRUE(MyMatch("foo*bar", "foobar"));
35 EXPECT_TRUE(MyMatch("foo*bar*baz", "foo_middle_bar_another_baz"));
36 EXPECT_TRUE(MyMatch("foo*bar*baz", "foo_middle_barbaz"));
Dmitry Vyukov55e6f3f2013-07-16 16:44:15 +000037 EXPECT_TRUE(MyMatch("^foobar", "foobar"));
38 EXPECT_TRUE(MyMatch("^foobar", "foobar_postfix"));
39 EXPECT_TRUE(MyMatch("^*foobar", "foobar"));
40 EXPECT_TRUE(MyMatch("^*foobar", "prefix_foobar"));
41 EXPECT_TRUE(MyMatch("foobar$", "foobar"));
42 EXPECT_TRUE(MyMatch("foobar$", "prefix_foobar"));
43 EXPECT_TRUE(MyMatch("*foobar*$", "foobar"));
44 EXPECT_TRUE(MyMatch("*foobar*$", "foobar_postfix"));
45 EXPECT_TRUE(MyMatch("^foobar$", "foobar"));
Sergey Matveeva52e5c62013-06-26 15:37:14 +000046
47 EXPECT_FALSE(MyMatch("foo", "baz"));
48 EXPECT_FALSE(MyMatch("foobarbaz", "foobar"));
49 EXPECT_FALSE(MyMatch("foobarbaz", "barbaz"));
50 EXPECT_FALSE(MyMatch("foo*bar", "foobaz"));
51 EXPECT_FALSE(MyMatch("foo*bar", "foo_baz"));
Dmitry Vyukov55e6f3f2013-07-16 16:44:15 +000052 EXPECT_FALSE(MyMatch("^foobar", "prefix_foobar"));
53 EXPECT_FALSE(MyMatch("foobar$", "foobar_postfix"));
54 EXPECT_FALSE(MyMatch("^foobar$", "prefix_foobar"));
55 EXPECT_FALSE(MyMatch("^foobar$", "foobar_postfix"));
56 EXPECT_FALSE(MyMatch("foo^bar", "foobar"));
57 EXPECT_FALSE(MyMatch("foo$bar", "foobar"));
58 EXPECT_FALSE(MyMatch("foo$^bar", "foobar"));
Sergey Matveeva52e5c62013-06-26 15:37:14 +000059}
60
Stephen Hines86277eb2015-03-23 12:06:32 -070061static const char *kTestSuppressionTypes[] = {"race", "thread", "mutex",
62 "signal"};
Sergey Matveeva52e5c62013-06-26 15:37:14 +000063
64class SuppressionContextTest : public ::testing::Test {
65 public:
Stephen Hines86277eb2015-03-23 12:06:32 -070066 SuppressionContextTest()
67 : ctx_(kTestSuppressionTypes, ARRAY_SIZE(kTestSuppressionTypes)) {}
Sergey Matveeva52e5c62013-06-26 15:37:14 +000068
69 protected:
Stephen Hines86277eb2015-03-23 12:06:32 -070070 SuppressionContext ctx_;
71
72 void CheckSuppressions(unsigned count, std::vector<const char *> types,
73 std::vector<const char *> templs) const {
74 EXPECT_EQ(count, ctx_.SuppressionCount());
75 for (unsigned i = 0; i < count; i++) {
76 const Suppression *s = ctx_.SuppressionAt(i);
77 EXPECT_STREQ(types[i], s->type);
78 EXPECT_STREQ(templs[i], s->templ);
79 }
Sergey Matveeva52e5c62013-06-26 15:37:14 +000080 }
Sergey Matveeva52e5c62013-06-26 15:37:14 +000081};
82
83TEST_F(SuppressionContextTest, Parse) {
Stephen Hines86277eb2015-03-23 12:06:32 -070084 ctx_.Parse("race:foo\n"
85 " race:bar\n" // NOLINT
86 "race:baz \n" // NOLINT
87 "# a comment\n"
88 "race:quz\n"); // NOLINT
89 CheckSuppressions(4, {"race", "race", "race", "race"},
90 {"foo", "bar", "baz", "quz"});
Sergey Matveeva52e5c62013-06-26 15:37:14 +000091}
92
93TEST_F(SuppressionContextTest, Parse2) {
Stephen Hines86277eb2015-03-23 12:06:32 -070094 ctx_.Parse(
Sergey Matveeva52e5c62013-06-26 15:37:14 +000095 " # first line comment\n" // NOLINT
96 " race:bar \n" // NOLINT
97 "race:baz* *baz\n"
98 "# a comment\n"
99 "# last line comment\n"
100 ); // NOLINT
Stephen Hines86277eb2015-03-23 12:06:32 -0700101 CheckSuppressions(2, {"race", "race"}, {"bar", "baz* *baz"});
Sergey Matveeva52e5c62013-06-26 15:37:14 +0000102}
103
104TEST_F(SuppressionContextTest, Parse3) {
Stephen Hines86277eb2015-03-23 12:06:32 -0700105 ctx_.Parse(
Sergey Matveeva52e5c62013-06-26 15:37:14 +0000106 "# last suppression w/o line-feed\n"
107 "race:foo\n"
108 "race:bar"
109 ); // NOLINT
Stephen Hines86277eb2015-03-23 12:06:32 -0700110 CheckSuppressions(2, {"race", "race"}, {"foo", "bar"});
Sergey Matveeva52e5c62013-06-26 15:37:14 +0000111}
112
113TEST_F(SuppressionContextTest, ParseType) {
Stephen Hines86277eb2015-03-23 12:06:32 -0700114 ctx_.Parse(
Sergey Matveeva52e5c62013-06-26 15:37:14 +0000115 "race:foo\n"
116 "thread:bar\n"
117 "mutex:baz\n"
118 "signal:quz\n"
119 ); // NOLINT
Stephen Hines86277eb2015-03-23 12:06:32 -0700120 CheckSuppressions(4, {"race", "thread", "mutex", "signal"},
121 {"foo", "bar", "baz", "quz"});
122}
123
124TEST_F(SuppressionContextTest, HasSuppressionType) {
125 ctx_.Parse(
126 "race:foo\n"
127 "thread:bar\n");
128 EXPECT_TRUE(ctx_.HasSuppressionType("race"));
129 EXPECT_TRUE(ctx_.HasSuppressionType("thread"));
130 EXPECT_FALSE(ctx_.HasSuppressionType("mutex"));
131 EXPECT_FALSE(ctx_.HasSuppressionType("signal"));
Sergey Matveeva52e5c62013-06-26 15:37:14 +0000132}
133
134} // namespace __sanitizer