summaryrefslogtreecommitdiff
path: root/lib/sanitizer_common/tests/sanitizer_suppressions_test.cpp
blob: 8bbb11c40db9d3767040142fb5c9fc8c72295a2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//===-- sanitizer_suppressions_test.cpp -----------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file is a part of ThreadSanitizer/AddressSanitizer runtime.
//
//===----------------------------------------------------------------------===//
#include "sanitizer_common/sanitizer_suppressions.h"
#include "gtest/gtest.h"

#include <string.h>

namespace __sanitizer {

static bool MyMatch(const char *templ, const char *func) {
  char tmp[1024];
  strcpy(tmp, templ);  // NOLINT
  return TemplateMatch(tmp, func);
}

TEST(Suppressions, Match) {
  EXPECT_TRUE(MyMatch("foobar$", "foobar"));

  EXPECT_TRUE(MyMatch("foobar", "foobar"));
  EXPECT_TRUE(MyMatch("*foobar*", "foobar"));
  EXPECT_TRUE(MyMatch("foobar", "prefix_foobar_postfix"));
  EXPECT_TRUE(MyMatch("*foobar*", "prefix_foobar_postfix"));
  EXPECT_TRUE(MyMatch("foo*bar", "foo_middle_bar"));
  EXPECT_TRUE(MyMatch("foo*bar", "foobar"));
  EXPECT_TRUE(MyMatch("foo*bar*baz", "foo_middle_bar_another_baz"));
  EXPECT_TRUE(MyMatch("foo*bar*baz", "foo_middle_barbaz"));
  EXPECT_TRUE(MyMatch("^foobar", "foobar"));
  EXPECT_TRUE(MyMatch("^foobar", "foobar_postfix"));
  EXPECT_TRUE(MyMatch("^*foobar", "foobar"));
  EXPECT_TRUE(MyMatch("^*foobar", "prefix_foobar"));
  EXPECT_TRUE(MyMatch("foobar$", "foobar"));
  EXPECT_TRUE(MyMatch("foobar$", "prefix_foobar"));
  EXPECT_TRUE(MyMatch("*foobar*$", "foobar"));
  EXPECT_TRUE(MyMatch("*foobar*$", "foobar_postfix"));
  EXPECT_TRUE(MyMatch("^foobar$", "foobar"));

  EXPECT_FALSE(MyMatch("foo", "baz"));
  EXPECT_FALSE(MyMatch("foobarbaz", "foobar"));
  EXPECT_FALSE(MyMatch("foobarbaz", "barbaz"));
  EXPECT_FALSE(MyMatch("foo*bar", "foobaz"));
  EXPECT_FALSE(MyMatch("foo*bar", "foo_baz"));
  EXPECT_FALSE(MyMatch("^foobar", "prefix_foobar"));
  EXPECT_FALSE(MyMatch("foobar$", "foobar_postfix"));
  EXPECT_FALSE(MyMatch("^foobar$", "prefix_foobar"));
  EXPECT_FALSE(MyMatch("^foobar$", "foobar_postfix"));
  EXPECT_FALSE(MyMatch("foo^bar", "foobar"));
  EXPECT_FALSE(MyMatch("foo$bar", "foobar"));
  EXPECT_FALSE(MyMatch("foo$^bar", "foobar"));
}

static const char *kTestSuppressionTypes[] = {"race", "thread", "mutex",
                                              "signal"};

class SuppressionContextTest : public ::testing::Test {
 public:
  SuppressionContextTest()
      : ctx_(kTestSuppressionTypes, ARRAY_SIZE(kTestSuppressionTypes)) {}

 protected:
  SuppressionContext ctx_;

  void CheckSuppressions(unsigned count, std::vector<const char *> types,
                         std::vector<const char *> templs) const {
    EXPECT_EQ(count, ctx_.SuppressionCount());
    for (unsigned i = 0; i < count; i++) {
      const Suppression *s = ctx_.SuppressionAt(i);
      EXPECT_STREQ(types[i], s->type);
      EXPECT_STREQ(templs[i], s->templ);
    }
  }
};

TEST_F(SuppressionContextTest, Parse) {
  ctx_.Parse("race:foo\n"
             " 	race:bar\n"  // NOLINT
             "race:baz	 \n" // NOLINT
             "# a comment\n"
             "race:quz\n"); // NOLINT
  CheckSuppressions(4, {"race", "race", "race", "race"},
                    {"foo", "bar", "baz", "quz"});
}

TEST_F(SuppressionContextTest, Parse2) {
  ctx_.Parse(
    "  	# first line comment\n"  // NOLINT
    " 	race:bar 	\n"  // NOLINT
    "race:baz* *baz\n"
    "# a comment\n"
    "# last line comment\n"
  );  // NOLINT
  CheckSuppressions(2, {"race", "race"}, {"bar", "baz* *baz"});
}

TEST_F(SuppressionContextTest, Parse3) {
  ctx_.Parse(
    "# last suppression w/o line-feed\n"
    "race:foo\n"
    "race:bar\r\n"
    "race:baz"
  );  // NOLINT
  CheckSuppressions(3, {"race", "race", "race"}, {"foo", "bar", "baz"});
}

TEST_F(SuppressionContextTest, ParseType) {
  ctx_.Parse(
    "race:foo\n"
    "thread:bar\n"
    "mutex:baz\n"
    "signal:quz\n"
  );  // NOLINT
  CheckSuppressions(4, {"race", "thread", "mutex", "signal"},
                    {"foo", "bar", "baz", "quz"});
}

TEST_F(SuppressionContextTest, HasSuppressionType) {
  ctx_.Parse(
    "race:foo\n"
    "thread:bar\n");
  EXPECT_TRUE(ctx_.HasSuppressionType("race"));
  EXPECT_TRUE(ctx_.HasSuppressionType("thread"));
  EXPECT_FALSE(ctx_.HasSuppressionType("mutex"));
  EXPECT_FALSE(ctx_.HasSuppressionType("signal"));
}

TEST_F(SuppressionContextTest, RegressionTestForBufferOverflowInSuppressions) {
  EXPECT_DEATH(ctx_.Parse("race"), "failed to parse suppressions");
  EXPECT_DEATH(ctx_.Parse("foo"), "failed to parse suppressions");
}


}  // namespace __sanitizer