File: test_line_intelligence.cc

package info (click to toggle)
logserver 1.13.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 720 kB
  • sloc: cpp: 10,815; makefile: 3
file content (334 lines) | stat: -rw-r--r-- 10,053 bytes parent folder | download
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "test.h"

#include "line_intelligence.h"

#include <random>

using namespace std;

TEST_CASE("tabs") {
	CHECK(LineIntelligence::make_tabs(0) == "");
	CHECK(LineIntelligence::make_tabs(1) == "    ");
	CHECK(LineIntelligence::make_tabs(2) == "        ");
}

TEST_CASE("code") {
	list<string> out;
	list<string> expected = {
		"func(){",
		"    i;",
		"    j;",
		"    while(true){",
		"        k;",
		"    }",
		"}"};

	string in = "func(){i;j;while(true){k;}}";
		LineIntelligence::process_code(in, &out);
	CHECK(out == expected);
}

TEST_CASE("code quote") {
	list<string> out;
	list<string> expected = {
		"func(){",
		"    i;",
		"    j = \"}{{{\";",
		"    while(true){",
		"        k;",
		"    }",
		"}"};

	string in = "func(){i;j = \"}{{{\";while(true){k;}}";
	LineIntelligence::process_code(in, &out);
	CHECK(out == expected);
}

TEST_CASE("json") {
	list<string> out;
	list<string> expected = {
		"{",
		"    \"key\":\"value\",",
		"    \"obj\":{",
		"        \"a\":2,",
		"        \"b\":3",
		"    }",
		"}"};

	string in = "{\"key\":\"value\", \"obj\":{\"a\":2, \"b\":3}}";
	map<string, size_t> counts;
	LineIntelligence::count_occurrences(in, &counts);
	CHECK(LineIntelligence::heuristic_json(in, counts));
	CHECK(!LineIntelligence::heuristic_code(in, counts));
	CHECK(!LineIntelligence::heuristic_newlines(in, counts));
	CHECK(!LineIntelligence::heuristic_pipe(in, counts));
	CHECK(!LineIntelligence::heuristic_httparg(in, counts));

	LineIntelligence::process_json(in, &out);
	CHECK(out == expected);
}

TEST_CASE("json array") {
	list<string> out;
	list<string> expected = {
		"[",
		"    3,",
		"    \"hello, there\",",
		"    true",
		"]"};

	string in = "[3, \"hello, there\", true]";
	LineIntelligence::process_json(in, &out);
	CHECK(out == expected);
}

template <typename T, typename R>
bool equal(const T& lhs, const R& rhs) {
	auto lit = lhs.begin();
	auto rit = rhs.begin();
	while (lit != lhs.end()) {
		if (rit == rhs.end()) return false;
		if (*lit != *rit) return false;
		++lit;
		++rit;
	}
	if (rit != rhs.end()) return false;
	return true;
}

TEST_CASE("split with empty") {
	string data = "one,two,,four";
	list<string> expected = {"one", "two", "", "four"};
	list<string_view> tokens;
	LineIntelligence::split_with_empty(data, ",", &tokens);
	CHECK(equal(tokens, expected));
}

TEST_CASE("split with empty end") {
	string data = "one,two,,";
	list<string> expected = {"one", "two", "", ""};
	list<string_view> tokens;
	LineIntelligence::split_with_empty(data, ",", &tokens);
	CHECK(equal(tokens, expected));
}

TEST_CASE("split with empty start") {
	string data = "-one-two";
	list<string> expected = {"", "one", "two"};
	list<string_view> tokens;
	LineIntelligence::split_with_empty(data, "-", &tokens);
	CHECK(equal(tokens, expected));
}

TEST_CASE("no split") {
	string data = "one-two";
	list<string> expected = {"one-two"};
	list<string_view> tokens;
	LineIntelligence::split_with_empty(data, ".", &tokens);
	CHECK(equal(tokens, expected));
}

TEST_CASE("empty split") {
	string data = "";
	list<string> expected = {""};
	list<string_view> tokens;
	LineIntelligence::split_with_empty(data, ",", &tokens);
	CHECK(equal(tokens, expected));
}

TEST_CASE("replace multi") {
	string data = "hello, there, world";
	CHECK(LineIntelligence::replace(data, ", ", ".")
	      == "hello.there.world");
}

TEST_CASE("replace char") {
	string data = "hello there";
	CHECK(LineIntelligence::replace(data, "t", ".")
	      == "hello .here");
}

TEST_CASE("replace none") {
	string data = "hello there";
	CHECK(LineIntelligence::replace(data, "why", "what")
	      == data);
}

TEST_CASE("rewind") {
	string data = "hello there wherecanwefind a good break?";
	CHECK(LineIntelligence::rewind(data, 4, 2) == 4);
	CHECK(LineIntelligence::rewind(data, 7, 5) == 5);
	CHECK(LineIntelligence::rewind(data, 24, 2) == 24);
	CHECK(LineIntelligence::rewind(data, 24, 15) == 11);
}

TEST_CASE("rewind punctuation") {
	string data = "a=b&c=d";
	CHECK(LineIntelligence::rewind(data, 2, 1) == 2);
	CHECK(LineIntelligence::rewind(data, 3, 2) == 3);
	CHECK(LineIntelligence::rewind(data, 4, 1) == 4);
	CHECK(LineIntelligence::rewind(data, 4, 2) == 3);
	CHECK(LineIntelligence::rewind(data, 4, 3) == 3);
	CHECK(LineIntelligence::rewind(data, 5, 1) == 5);
	CHECK(LineIntelligence::rewind(data, 5, 2) == 5);
	CHECK(LineIntelligence::rewind(data, 5, 3) == 3);
}

TEST_CASE("percent printable") {
	CHECK(LineIntelligence::percent_printable("abcd", false) == 100);
	CHECK(LineIntelligence::percent_printable("abcd okay", false) == 100);
	CHECK(LineIntelligence::percent_printable("", false) == 100);

	CHECK(LineIntelligence::percent_printable("ab\4\4", false) == 50);
	CHECK(LineIntelligence::percent_printable("ab\4\2ccdd", false) == 75);
	CHECK(LineIntelligence::percent_printable("\bcd\xff", false) == 50);
}

TEST_CASE("count occurences") {
	CHECK(LineIntelligence::count_occurrences("abcd", 'a') == 1);
	CHECK(LineIntelligence::count_occurrences("abba", 'a') == 2);
	CHECK(LineIntelligence::count_occurrences("ab....c.d", '.') == 5);
	CHECK(LineIntelligence::count_occurrences("abcd", '.') == 0);
	CHECK(LineIntelligence::count_occurrences("abcd", "a") == 1);
	CHECK(LineIntelligence::count_occurrences("abba", "a") == 2);
	CHECK(LineIntelligence::count_occurrences("abbab", "ab") == 2);
	CHECK(LineIntelligence::count_occurrences("ab....c.d", ".") == 5);
	CHECK(LineIntelligence::count_occurrences("abcd", ".") == 0);
	CHECK(LineIntelligence::count_occurrences("abcd", "abcd") == 1);
	CHECK(LineIntelligence::count_occurrences("ababababab", "abab") == 2);
	CHECK(LineIntelligence::count_occurrences("bbbaabbbabbb", "bbb") == 3);
}

TEST_CASE("heuristic timestamp") {
	string line = "the current time is 1762620592 seconds past epoch";
	optional<string> ret = LineIntelligence::apply_heuristics(line, 0);
	CHECK(ret);
	// in case timezone effect timestamp
	CHECK(ret->find(" 2025 seconds") != string::npos);
}

TEST_CASE("heuristic only timestamp") {
	string line = "1762620592";
	optional<string> ret = LineIntelligence::apply_heuristics(line, 0);
	CHECK(ret);
	// in case timezone effect timestamp
	CHECK(ret->ends_with(" 2025"));
}

TEST_CASE("heuristic only timestamp millis") {
	string line = "1762620592123";
	optional<string> ret = LineIntelligence::apply_heuristics(line, 0);
	CHECK(ret);
	// in case timezone effect timestamp
	CHECK(ret->ends_with(" 2025"));
}

TEST_CASE("base64") {
	string in = "dmVyeSBnb29kIHN0cmluZw==";
	optional<string> ret = LineIntelligence::apply_heuristics(in, 0);
	CHECK(ret);
	CHECK(*ret == "very good string");
	in[9] = '+';
	in[10] = '+';
	in[11] = '+';
	ret = LineIntelligence::apply_heuristics(in, 0);
	CHECK(!ret);
	ret = LineIntelligence::apply_heuristics(in, 1);
	CHECK(!ret);
	ret = LineIntelligence::apply_heuristics(in, 2);
	CHECK(ret);
	CHECK(ret->starts_with("very go"));
	CHECK(ret->ends_with(" string"));
}

TEST_CASE("subbase64") {
	string in = "parameter=dmVyeSBnb29kIHN0cmluZw==";
        optional<string> ret = LineIntelligence::apply_heuristics(in, 0);
        CHECK(ret);
        CHECK(*ret == "parameter=very good string");
}

TEST_CASE("hex") {
	string in = "6f6b61792074686572652068656c6c6f";
	        optional<string> ret = LineIntelligence::apply_heuristics(in, 0);
        CHECK(ret);
        CHECK(*ret == "okay there hello");
}

TEST_CASE("hex split") {
	string in = "6f6b6179 and also 7468657265 are present";
	        optional<string> ret = LineIntelligence::apply_heuristics(in, 0);
        CHECK(ret);
        CHECK(*ret == "okay and also there are present");
}

TEST_CASE("apply matches") {
	string in = "here is a string with some matches some repeat";
	map<string, string> replace;
	CHECK(LineIntelligence::apply_matches(in, replace) == nullopt);
	replace["string"] = "sentence";
	CHECK(*LineIntelligence::apply_matches(in, replace) ==
	      "here is a sentence with some matches some repeat");
	replace.clear();
	replace["some"] = "a bunch of";
	replace["here"] = "this";
	CHECK(*LineIntelligence::apply_matches(in, replace) ==
	      "this is a string with a bunch of matches a bunch of repeat");
}

TEST_CASE("newline heuristic") {
	string in = "this line has a bunch\\nof newlines at various\\npositions "
		"along the string\\nwhich can be used to indicate\\nit should "
		"break at those positions.";
	map<string, size_t> counts;
	LineIntelligence::count_occurrences(in, &counts);
	CHECK(!LineIntelligence::heuristic_json(in, counts));
	CHECK(!LineIntelligence::heuristic_code(in, counts));
	CHECK(!LineIntelligence::heuristic_httparg(in, counts));
	CHECK(LineIntelligence::heuristic_newlines(in, counts));

	auto lines = LineIntelligence::split(in, '\0');
	CHECK(lines.size() == 5);
	CHECK(lines.front()->get() == "this line has a bunch");
	CHECK(lines.back()->get() == "it should break at those positions.");
}

TEST_CASE("httparg heuristic") {
	string in = "query=string&formatted=data&x=3&y&enable&data=eyJ";
	map<string, size_t> counts;
	LineIntelligence::count_occurrences(in, &counts);
	CHECK(!LineIntelligence::heuristic_json(in, counts));
	CHECK(!LineIntelligence::heuristic_code(in, counts));
	CHECK(LineIntelligence::heuristic_httparg(in, counts));
	CHECK(!LineIntelligence::heuristic_newlines(in, counts));

	auto lines = LineIntelligence::split(in, '\0');
	CHECK(lines.size() == 6);
	CHECK(lines.front()->get() == "query=string");
	CHECK(lines.back()->get() == "data=eyJ");
}

TEST_CASE("random data") {
	mt19937 rng(random_device{}());
	uniform_int_distribution<unsigned char> chardist(33, 126);
	uniform_int_distribution<size_t> lendist(1, 1000);

	/* throw random strings at line intelligence to make sure it doesn't
	 * crash on bad input */
	for (int i = 0; i < 1000; ++i) {
		string s;
		size_t len = lendist(rng);
		while (len) {
			s += chardist(rng);
			--len;
		}
		optional<string> ret = LineIntelligence::apply_heuristics(s, 0);
	}
}

TEST_CASE("percent encoding") {
	CHECK(LineIntelligence::hex_unescape("%2F") == "/");
	CHECK(LineIntelligence::hex_unescape("%2F ") == "/ ");
	CHECK(LineIntelligence::hex_unescape(" %2F") == " /");
}