-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscrubber_test.cc
259 lines (232 loc) · 6.86 KB
/
scrubber_test.cc
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright 2017 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <time.h>
#include <string>
#include <base/files/file_path.h>
#include <base/files/file_util.h>
#include <base/macros.h>
#include <base/strings/string_util.h>
#include <gtest/gtest.h>
#include <vm_protos/proto_bindings/vm_host.pb.h>
#include "vm_tools/syslog/scrubber.h"
using std::string;
namespace vm_tools {
namespace syslog {
namespace {
// NOLINT(whitespace/braces)
constexpr struct SeverityTestCase {
vm_tools::LogSeverity severity;
const char* result;
} kSeverityTests[] = {
{
.severity = vm_tools::EMERGENCY,
.result = "<8>",
},
{
.severity = vm_tools::ALERT,
.result = "<9>",
},
{
.severity = vm_tools::CRITICAL,
.result = "<10>",
},
{
.severity = vm_tools::ERROR,
.result = "<11>",
},
{
.severity = vm_tools::WARNING,
.result = "<12>",
},
{
.severity = vm_tools::NOTICE,
.result = "<13>",
},
{
.severity = vm_tools::INFO,
.result = "<14>",
},
{
.severity = vm_tools::DEBUG,
.result = "<15>",
},
{
.severity = vm_tools::MISSING,
.result = "<13>",
},
{
.severity = static_cast<vm_tools::LogSeverity>(18),
.result = "<13>",
},
};
class SeverityTest : public ::testing::TestWithParam<SeverityTestCase> {};
// NOLINT(whitespace/braces)
constexpr struct TimestampTestCase {
struct tm tm;
const char* result;
} kTimestampTests[] = {
{
// clang-format off
.tm = {
.tm_sec = 11,
.tm_min = 54,
.tm_hour = 23,
.tm_mday = 17,
.tm_mon = 0,
.tm_year = 125,
},
// clang-format on
.result = "Jan 17 23:54:11",
},
{
// clang-format off
.tm = {
.tm_sec = 58,
.tm_min = 33,
.tm_hour = 18,
.tm_mday = 24,
.tm_mon = 11,
.tm_year = 6,
},
// clang-format on
.result = "Dec 24 18:33:58",
},
{
// clang-format off
.tm = {
.tm_sec = 0,
.tm_min = 0,
.tm_hour = 0,
.tm_mday = 1,
.tm_mon = 0,
.tm_year = 70,
},
// clang-format on
.result = "Jan 1 00:00:00",
},
{
// clang-format off
.tm = {
.tm_sec = 47,
.tm_min = 15,
.tm_hour = 17,
.tm_mday = 2,
.tm_mon = 5,
.tm_year = 112,
},
// clang-format on
.result = "Jun 2 17:15:47",
},
{
// clang-format off
.tm = {
.tm_sec = 47,
.tm_min = 15,
.tm_hour = 17,
.tm_mday = 2,
.tm_mon = 5,
.tm_year = 57,
},
// clang-format on
.result = "Jun 2 17:15:47",
},
};
class TimestampTest : public ::testing::TestWithParam<TimestampTestCase> {};
// NOLINT(whitespace/braces)
constexpr struct ContentTestCase {
const char* input;
const char* output;
} kContentTests[] = {
{
.input = "",
.output = "",
},
{
.input = "Contains only ASCII characters",
.output = u8"Contains only ASCII characters",
},
{
.input = u8"网页 图片 资讯更多 »",
.output = u8"网页 图片 资讯更多 »",
},
{
.input = u8"Παγκόσμιος Ιστός",
.output = u8"Παγκόσμιος Ιστός",
},
{
.input = u8"Поиск страниц на русском",
.output = u8"Поиск страниц на русском",
},
{
// "Embedded (U+008c) control (U+0007) characters"
.input = "Embedded \xC2\x8C control \x07 characters",
.output = u8"Embedded #214 control #007 characters",
},
{
// "Invalid(U+dead) code(U+12ffff) points"
.input = "Invalid\xED\xBA\xAD code\xF4\xAF\xBF\xBF points",
// "Invalid��� code��� points" NOLINT(readability/utf8)
.output =
u8"Invalid\xEF\xBF\xBD\xEF\xBF\xBD\xEF\xBF\xBD "
u8"code\xEF\xBF\xBD\xEF\xBF\xBD\xEF\xBF\xBD\xEF\xBF\xBD points",
},
{
// "Non-(U+fffe) character (U+fde1) code points"
.input = "Non-\xEF\xBF\xBE character \xEF\xB7\xA1 code points",
.output = u8"Non-#177776 character #176741 code points",
},
{
// "Mix of(U+0091) val(U+001c)id, invalid(U+daaa), 전체Παγκόσμιος网页на
// русском, non(U+1dffff)-character, and(U+fffe) control (U+fdea) code
// points"
.input = "Mix of\xC2\x91 val\x1Cid, invalid\xED\xAA\xAA, "
"전체Παγκόσμιος网页на русском, non\xF7\x9F\xBF\xBF-character, "
"and\xEF\xBF\xBE control \xEF\xB7\xAA code points",
.output =
u8"Mix of#221 val#034id, "
u8"invalid\xEF\xBF\xBD\xEF\xBF\xBD\xEF\xBF\xBD, "
u8"전체Παγκόσμιος网页на русском, "
u8"non\xEF\xBF\xBD\xEF\xBF\xBD\xEF\xBF\xBD\xEF\xBF\xBD-character, "
u8"and#177776 control #176752 code points",
},
};
class ContentTest : public ::testing::TestWithParam<ContentTestCase> {};
} // namespace
TEST_P(SeverityTest, ParsesCorrectly) {
struct SeverityTestCase param = GetParam();
EXPECT_EQ(ParseProtoSeverity(param.severity), string(param.result));
}
INSTANTIATE_TEST_SUITE_P(Scrubber,
SeverityTest,
::testing::ValuesIn(kSeverityTests));
TEST_P(TimestampTest, ParsesCorrectly) {
struct TimestampTestCase param = GetParam();
vm_tools::Timestamp timestamp;
timestamp.set_seconds(mktime(¶m.tm));
ASSERT_NE(timestamp.seconds(), -1);
EXPECT_EQ(ParseProtoTimestamp(timestamp), string(param.result));
}
INSTANTIATE_TEST_SUITE_P(Scrubber,
TimestampTest,
::testing::ValuesIn(kTimestampTests));
TEST_P(ContentTest, ScrubsCleanly) {
struct ContentTestCase param = GetParam();
EXPECT_EQ(ScrubProtoContent(param.input), string(param.output));
}
INSTANTIATE_TEST_SUITE_P(Scrubber,
ContentTest,
::testing::ValuesIn(kContentTests));
TEST(Content, StressTest) {
base::FilePath src(getenv("PWD"));
ASSERT_TRUE(base::PathExists(src));
base::FilePath stress_test = src.Append("syslog").Append("UTF8_test.txt");
ASSERT_TRUE(base::PathExists(stress_test));
string content;
ASSERT_TRUE(base::ReadFileToString(stress_test, &content));
EXPECT_FALSE(base::IsStringUTF8(content));
string result = ScrubProtoContent(content);
EXPECT_TRUE(base::IsStringUTF8(result));
}
} // namespace syslog
} // namespace vm_tools