Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
redactor-config-parser-test.cc
Go to the documentation of this file.
1 // Copyright 2015 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "redactor.cc"
16 
17 #include <gtest/gtest.h>
18 
19 #include "redactor-test-utils.h"
20 
21 namespace impala {
22 
23 using std::string;
24 
25 TEST(ParserTest, FileNotFound) {
26  TempRulesFile rules_file("");
27  rules_file.Delete();
28  string error = SetRedactionRulesFromFile(rules_file.name());
29  ASSERT_ERROR_MESSAGE_CONTAINS(error, "No such file");
30 }
31 
32 TEST(ParserTest, EmptyFile) {
33  TempRulesFile rules_file("");
34  string error = SetRedactionRulesFromFile(rules_file.name());
35  ASSERT_EQ("", error);
36  ASSERT_EQ(0, g_rules->size());
37  ASSERT_UNREDACTED("foo33");
38 
39  rules_file.OverwriteContents(" \t\n ");
40  error = SetRedactionRulesFromFile(rules_file.name());
41  ASSERT_ERROR_MESSAGE_CONTAINS(error, "Text only contains white space");
42 }
43 
44 TEST(ParserTest, DescriptionPropertyIgnored) {
45  TempRulesFile rules_file(
46  "{"
47  " \"version\": 1,"
48  " \"rules\": ["
49  " {\"search\": \"foo\", \"replace\": \"bar\", \"description\": \"def\"}"
50  " ]"
51  "}");
52  string error = SetRedactionRulesFromFile(rules_file.name());
53  ASSERT_EQ("", error);
54  ASSERT_REDACTED_EQ("foo", "bar");
55 }
56 
57 TEST(ParserTest, InvalidJson) {
58  TempRulesFile rules_file(
59  "\"version\": 100,"
60  "\"rules\": ["
61  " {\"search\": \"[0-9]\", \"replace\": \"#\"}"
62  "]");
63  string error = SetRedactionRulesFromFile(rules_file.name());
64  ASSERT_ERROR_MESSAGE_CONTAINS(error, "either an object or array at root");
65 
66  rules_file.OverwriteContents(
67  "[{"
68  " \"version\": 1.0,"
69  " \"rules\": ["
70  " {\"search\": \"[0-9]\", \"replace\": \"#\"}"
71  " ]"
72  "}]");
73  error = SetRedactionRulesFromFile(rules_file.name());
74  ASSERT_ERROR_MESSAGE_CONTAINS(error, "root element must be a JSON Object");
75 
76  rules_file.OverwriteContents(
77  "{"
78  " \"version\": 1,"
79  " \"ules\": ["
80  " {\"search\": \"[0-9]\", \"replace\": \"#\"}"
81  " ]"
82  "}");
83  error = SetRedactionRulesFromFile(rules_file.name());
84  ASSERT_ERROR_MESSAGE_CONTAINS(error, "unexpected property 'ules'");
85 
86  rules_file.OverwriteContents(
87  "{"
88  " \"version\": 1,"
89  " \"rules\": ["
90  " {\"earch\": \"[0-9]\", \"replace\": \"#\"}"
91  " ]"
92  "}");
93  error = SetRedactionRulesFromFile(rules_file.name());
94  ASSERT_ERROR_MESSAGE_CONTAINS(error, "unexpected property 'earch'");
95 
96  rules_file.OverwriteContents("{!@#$}");
97  error = SetRedactionRulesFromFile(rules_file.name());
98  ASSERT_ERROR_MESSAGE_CONTAINS(error, "Name of an object member must be a string");
99 }
100 
101 TEST(ParserTest, BadVersion) {
102  TempRulesFile rules_file(
103  "{"
104  " \"version\": 100,"
105  " \"rules\": ["
106  " {\"search\": \"[0-9]\", \"replace\": \"#\"}"
107  " ]"
108  "}");
109  string error = SetRedactionRulesFromFile(rules_file.name());
110  ASSERT_ERROR_MESSAGE_CONTAINS(error, "only version 1");
111 
112  int bad_value_count = 6;
113  string bad_values[][2] = {
114  {"1.0", "Float"},
115  {"true", "Bool"},
116  {"false", "Bool"},
117  {"\"string\"", "String"},
118  {"[]", "Array"},
119  {"{}", "Object"},
120  };
121  ASSERT_EQ(sizeof(string) * bad_value_count * 2, sizeof(bad_values));
122  for (int i = 0; i < bad_value_count; ++i) {
123  rules_file.OverwriteContents(Substitute("{ \"version\": $0 }", bad_values[i][0]));
124  error = SetRedactionRulesFromFile(rules_file.name());
126  error, Substitute("must be an Integer but is a $0", bad_values[i][1]).c_str());
127  }
128 
129  rules_file.OverwriteContents(
130  "{"
131  " \"rules\": ["
132  " {\"search\": \"[0-9]\", \"replace\": \"#\"}"
133  " ]"
134  "}");
135  error = SetRedactionRulesFromFile(rules_file.name());
136  ASSERT_ERROR_MESSAGE_CONTAINS(error, "version is required");
137 }
138 
139 TEST(ParserTest, BadRegex) {
140  TempRulesFile rules_file(
141  "{"
142  " \"version\": 1,"
143  " \"rules\": ["
144  " {\"search\": \"[0-9\", \"replace\": \"#\"}"
145  " ]"
146  "}");
147  string error = SetRedactionRulesFromFile(rules_file.name());
148  ASSERT_ERROR_MESSAGE_CONTAINS(error, "missing ]");
149 }
150 
151 TEST(ParserTest, BadCaseSensitivtyValue) {
152  TempRulesFile rules_file(
153  "{"
154  " \"version\": 1,"
155  " \"rules\": ["
156  " {\"search\": \"[0-9\", \"replace\": \"#\", \"caseSensitive\": 1}"
157  " ]"
158  "}");
159  string error = SetRedactionRulesFromFile(rules_file.name());
160  ASSERT_ERROR_MESSAGE_CONTAINS(error, "must be of type Bool");
161 }
162 
163 TEST(ParserTest, RuleNumberInErrorMessage) {
164  TempRulesFile rules_file(
165  "{"
166  " \"version\": 1,"
167  " \"rules\": ["
168  " {\"search\": \"[0-9]\", \"replace\": \"#\"},"
169  " {\"search\": \"[0-\", \"replace\": \"error\"},"
170  " {\"search\": \"[A-Z]\", \"replace\": \"_\"}"
171  " ]"
172  "}");
173  string error = SetRedactionRulesFromFile(rules_file.name());
175  "Error parsing redaction rule #2; search regex is invalid; missing ]");
176 }
177 
178 }
179 
180 int main(int argc, char **argv) {
181  // Disabled under ASAN, see IMPALA-1918
182 #ifndef ADDRESS_SANITIZER
183  ::testing::InitGoogleTest(&argc, argv);
184  return RUN_ALL_TESTS();
185 #endif
186 }
const char * name() const
Returns the absolute path to the file.
TEST(AtomicTest, Basic)
Definition: atomic-test.cc:28
static Rules * g_rules
Definition: redactor.cc:93
string SetRedactionRulesFromFile(const string &rules_file_path)
Definition: redactor.cc:260
int main(int argc, char **argv)
#define ASSERT_UNREDACTED(string)
void OverwriteContents(const std::string &contents)
#define ASSERT_REDACTED_EQ(actual, expected)
#define ASSERT_ERROR_MESSAGE_CONTAINS(error, expected)