Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
redactor-test-utils.h
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 <cstdlib> // rand
16 #include <cstdio> // file stuff
17 #include <pthread.h>
18 #include <time.h>
19 
20 #include <gtest/gtest.h>
21 
22 #ifndef IMPALA_REDACTOR_TEST_UTILS_H
23 #define IMPALA_REDACTOR_TEST_UTILS_H
24 
25 namespace impala {
26 
30  public:
31  TempRulesFile(const std::string& contents)
32  : name_("/tmp/rules_XXXXXX"),
33  deleted_(false) {
34  int fd = mkstemp(&name_[0]);
35  if (fd == -1) {
36  std::cout << "Error creating temp file; " << strerror(errno) << std::endl;
37  abort();
38  }
39  if (close(fd) != 0) {
40  std::cout << "Error closing temp file; " << strerror(errno) << std::endl;
41  abort();
42  }
43  OverwriteContents(contents);
44  }
45 
47 
48  void Delete() {
49  if (deleted_) return;
50  deleted_ = true;
51  if (remove(name()) != 0) {
52  std::cout << "Error deleting temp file; " << strerror(errno) << std::endl;
53  abort();
54  }
55  }
56 
57  void OverwriteContents(const std::string& contents) {
58  FILE* handle = fopen(name(), "w");
59  if (handle == NULL) {
60  std::cout << "Error creating temp file; " << strerror(errno) << std::endl;
61  abort();
62  }
63  int status = fputs(contents.c_str(), handle);
64  if (status < 0) {
65  std::cout << "Error writing to temp file; " << strerror(errno) << std::endl;
66  abort();
67  }
68  status = fclose(handle);
69  if (status != 0) {
70  std::cout << "Error closing temp file; " << strerror(errno) << std::endl;
71  abort();
72  }
73  }
74 
76  const char* name() const { return name_.c_str(); }
77 
78  private:
79  string name_;
80  bool deleted_;
81 };
82 
83 unsigned int RandSeed() {
84  struct timespec now;
85  clock_gettime(CLOCK_REALTIME, &now);
86  return now.tv_nsec + pthread_self();
87 }
88 
90 void RandomlyFillString(char* string, const int length) {
91  ASSERT_GT(length, 0);
92  unsigned int rand_seed = RandSeed();
93  int char_count = static_cast<int>('~') - static_cast<int>(' ') + 1;
94  for (int i = 0; i < length - 1; ++i) {
95  string[i] = ' ' + rand_r(&rand_seed) % char_count;
96  }
97  string[length - 1] = '\0';
98 }
99 
100 void AssertErrorMessageContains(const std::string& message, const char* expected) {
101  ASSERT_TRUE(message.find(expected) != std::string::npos)
102  << "Expected substring <<" << expected << ">> is not in <<" << message << ">>";
103 }
104 
105 void AssertRedactedEquals(const char* message, const char* expected) {
106  std::string temp(message);
107  Redact(&temp);
108  ASSERT_EQ(expected, temp);
109 
111  temp = string(message);
112  bool changed;
113  Redact(&temp, &changed);
114  ASSERT_EQ(expected, temp);
115  ASSERT_EQ(temp == message, !changed);
116 }
117 
118 void AssertUnredacted(const char* message) {
119  AssertRedactedEquals(message, message);
120 }
121 
126 #define SCOPED_ASSERT(assertion) { \
127  SCOPED_TRACE(""); \
128  assertion; \
129  if (HasFatalFailure()) return; \
130  }
131 
132 #define ASSERT_ERROR_MESSAGE_CONTAINS(error, expected) \
133  SCOPED_ASSERT(AssertErrorMessageContains(error, expected))
134 
135 #define ASSERT_REDACTED_EQ(actual, expected) \
136  SCOPED_ASSERT(AssertRedactedEquals(actual, expected))
137 
138 #define ASSERT_UNREDACTED(string) SCOPED_ASSERT(AssertUnredacted(string))
139 
140 }
141 
142 #endif
const char * name() const
Returns the absolute path to the file.
void Redact(string *value, bool *changed)
Definition: redactor.cc:309
void AssertRedactedEquals(const char *message, const char *expected)
void AssertUnredacted(const char *message)
unsigned int RandSeed()
void AssertErrorMessageContains(const std::string &message, const char *expected)
TempRulesFile(const std::string &contents)
void OverwriteContents(const std::string &contents)
void RandomlyFillString(char *string, const int length)
Randomly fills the contents of 'string' up to the given length.