-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_unittest.cc
360 lines (300 loc) · 10.3 KB
/
util_unittest.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/* Copyright 2018 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.
*
* Test util.[ch] module code using gtest.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <gtest/gtest.h>
#include "bpf.h"
#include "util.h"
namespace {
std::string dump_env(const char *const *env) {
std::string result;
for (; *env; ++env) {
result += *env;
result += "\n";
}
return result;
}
} // namespace
// Sanity check for the strip func.
TEST(strip, basic) {
char str[] = " foo\t";
ASSERT_EQ("foo", std::string(strip(str)));
}
// Make sure we don't crash with various "null"-like inputs.
TEST(tokenize, null_stringp) {
ASSERT_EQ(nullptr, tokenize(nullptr, nullptr));
ASSERT_EQ(nullptr, tokenize(nullptr, ""));
ASSERT_EQ(nullptr, tokenize(nullptr, ","));
char *p = nullptr;
ASSERT_EQ(nullptr, tokenize(&p, nullptr));
}
// Make sure we don't crash with various "null"-like inputs.
TEST(tokenize, null_delim) {
char str[] = "a,b,c";
char *p = str;
ASSERT_EQ(str, tokenize(&p, nullptr));
ASSERT_EQ(nullptr, p);
ASSERT_EQ(str, std::string("a,b,c"));
p = str;
ASSERT_EQ(str, tokenize(&p, ""));
ASSERT_EQ(nullptr, p);
ASSERT_EQ(str, std::string("a,b,c"));
}
// Sanity check for the tokenize func.
TEST(tokenize, basic) {
char str[] = "a,b,c";
char *p = str;
ASSERT_EQ("a", std::string(tokenize(&p, ",")));
ASSERT_EQ("b", std::string(tokenize(&p, ",")));
ASSERT_EQ("c", std::string(tokenize(&p, ",")));
ASSERT_EQ(nullptr, p);
ASSERT_EQ(nullptr, tokenize(&p, ","));
}
// Check edge case with an empty string.
TEST(tokenize, empty_string) {
char str[] = "";
char *p = str;
ASSERT_EQ("", std::string(tokenize(&p, ",")));
ASSERT_EQ(nullptr, p);
ASSERT_EQ(nullptr, tokenize(&p, ","));
}
// Check behavior with empty tokens at the start/middle/end.
TEST(tokenize, empty_tokens) {
char str[] = ",,a,b,,,c,,";
char *p = str;
ASSERT_EQ("", std::string(tokenize(&p, ",")));
ASSERT_EQ("", std::string(tokenize(&p, ",")));
ASSERT_EQ("a", std::string(tokenize(&p, ",")));
ASSERT_EQ("b", std::string(tokenize(&p, ",")));
ASSERT_EQ("", std::string(tokenize(&p, ",")));
ASSERT_EQ("", std::string(tokenize(&p, ",")));
ASSERT_EQ("c", std::string(tokenize(&p, ",")));
ASSERT_EQ("", std::string(tokenize(&p, ",")));
ASSERT_EQ("", std::string(tokenize(&p, ",")));
ASSERT_EQ(nullptr, p);
ASSERT_EQ(nullptr, tokenize(&p, ","));
}
// Check environment manipulation functions.
TEST(environment, copy_and_modify) {
minijail_free_env(nullptr);
char **env = minijail_copy_env(nullptr);
EXPECT_EQ("", dump_env(env));
minijail_free_env(env);
const char *const kConstEnv[] = {
"val1=1",
"val2=2",
"dup=1",
"dup=2",
"empty=",
nullptr,
};
// libc unfortunately uses char* const[] as the type for the environment, and
// we match that. It's actually missing a const-ness of the chars making up
// the environment strings, but we need that to initialize the |kEnv|
// constant. Hence, do a cast here to force things into alignment...
char* const* kEnv = const_cast<char* const*>(kConstEnv);
env = minijail_copy_env(kEnv);
EXPECT_EQ("val1=1\nval2=2\ndup=1\ndup=2\nempty=\n", dump_env(env));
minijail_free_env(env);
env = minijail_copy_env(kEnv);
EXPECT_EQ("val1=1\nval2=2\ndup=1\ndup=2\nempty=\n", dump_env(env));
EXPECT_EQ(EINVAL, minijail_setenv(nullptr, "val1", "3", 1));
char **env_ret = nullptr;
EXPECT_EQ(EINVAL, minijail_setenv(&env_ret, "val1", "3", 1));
env_ret = env;
EXPECT_EQ(EINVAL, minijail_setenv(&env_ret, nullptr, "3", 1));
EXPECT_EQ(env, env_ret);
EXPECT_EQ(EINVAL, minijail_setenv(&env_ret, "", "3", 1));
EXPECT_EQ(env, env_ret);
EXPECT_EQ(EINVAL, minijail_setenv(&env_ret, "", nullptr, 1));
EXPECT_EQ(env, env_ret);
EXPECT_EQ(0, minijail_setenv(&env, "val1", "3", 0));
EXPECT_EQ("val1=1\nval2=2\ndup=1\ndup=2\nempty=\n", dump_env(env));
EXPECT_EQ(0, minijail_setenv(&env, "val1", "3", 1));
EXPECT_EQ("val1=3\nval2=2\ndup=1\ndup=2\nempty=\n", dump_env(env));
EXPECT_EQ(0, minijail_setenv(&env, "val2", "4", 1));
EXPECT_EQ("val1=3\nval2=4\ndup=1\ndup=2\nempty=\n", dump_env(env));
EXPECT_EQ(0, minijail_setenv(&env, "dup", "5", 1));
EXPECT_EQ("val1=3\nval2=4\ndup=5\ndup=2\nempty=\n", dump_env(env));
EXPECT_EQ(0, minijail_setenv(&env, "empty", "6", 1));
EXPECT_EQ("val1=3\nval2=4\ndup=5\ndup=2\nempty=6\n", dump_env(env));
EXPECT_EQ(0, minijail_setenv(&env, "empty", "", 1));
EXPECT_EQ("val1=3\nval2=4\ndup=5\ndup=2\nempty=\n", dump_env(env));
EXPECT_EQ(0, minijail_setenv(&env, "new1", "7", 0));
EXPECT_EQ("val1=3\nval2=4\ndup=5\ndup=2\nempty=\nnew1=7\n", dump_env(env));
EXPECT_EQ(0, minijail_setenv(&env, "new2", "8", 1));
EXPECT_EQ("val1=3\nval2=4\ndup=5\ndup=2\nempty=\nnew1=7\nnew2=8\n",
dump_env(env));
minijail_free_env(env);
}
TEST(parse_single_constant, formats) {
char *end;
long int c = 0;
std::string constant;
// Check base 10 works.
constant = "1234";
c = parse_constant(const_cast<char*>(constant.data()), &end);
EXPECT_EQ(1234, c);
// Check base 16 works.
constant = "0x1234";
c = parse_constant(const_cast<char*>(constant.data()), &end);
EXPECT_EQ(0x1234, c);
// Check base 8 works.
constant = "01234";
c = parse_constant(const_cast<char*>(constant.data()), &end);
EXPECT_EQ(01234, c);
}
TEST(parse_constant, unsigned) {
char *end;
long int c = 0;
std::string constant;
#if defined(BITS32)
constant = "0x80000000";
c = parse_constant(const_cast<char*>(constant.data()), &end);
EXPECT_EQ(0x80000000U, static_cast<unsigned long int>(c));
#elif defined(BITS64)
constant = "0x8000000000000000";
c = parse_constant(const_cast<char*>(constant.data()), &end);
EXPECT_EQ(0x8000000000000000UL, static_cast<unsigned long int>(c));
#else
# error "unknown bits!"
#endif
}
TEST(parse_constant, unsigned_toobig) {
char *end;
long int c = 0;
std::string constant;
#if defined(BITS32)
constant = "0x100000000"; // Too big for 32-bit unsigned long int.
c = parse_constant(const_cast<char*>(constant.data()), &end);
// Error case should return 0.
EXPECT_EQ(0, c);
#elif defined(BITS64)
constant = "0x10000000000000000";
c = parse_constant(const_cast<char*>(constant.data()), &end);
// Error case should return 0.
EXPECT_EQ(0, c);
#else
# error "unknown bits!"
#endif
}
TEST(parse_constant, signed) {
char *end;
long int c = 0;
std::string constant = "-1";
c = parse_constant(const_cast<char*>(constant.data()), &end);
EXPECT_EQ(-1, c);
}
TEST(parse_constant, signed_toonegative) {
char *end;
long int c = 0;
std::string constant;
#if defined(BITS32)
constant = "-0x80000001";
c = parse_constant(const_cast<char*>(constant.data()), &end);
// Error case should return 0.
EXPECT_EQ(0, c);
#elif defined(BITS64)
constant = "-0x8000000000000001";
c = parse_constant(const_cast<char*>(constant.data()), &end);
// Error case should return 0.
EXPECT_EQ(0, c);
#else
# error "unknown bits!"
#endif
}
TEST(parse_constant, complements) {
char* end;
long int c = 0;
std::string constant;
#if defined(BITS32)
constant = "~0x005AF0FF|~0xFFA50FFF";
c = parse_constant(const_cast<char*>(constant.data()), &end);
EXPECT_EQ(c, 0xFFFFFF00);
constant = "0x0F|~(0x005AF000|0x00A50FFF)|0xF0";
c = parse_constant(const_cast<char*>(constant.data()), &end);
EXPECT_EQ(c, 0xFF0000FF);
#elif defined(BITS64)
constant = "~0x00005A5AF0F0FFFF|~0xFFFFA5A50F0FFFFF";
c = parse_constant(const_cast<char*>(constant.data()), &end);
EXPECT_EQ(c, 0xFFFFFFFFFFFF0000UL);
constant = "0x00FF|~(0x00005A5AF0F00000|0x0000A5A50F0FFFFF)|0xFF00";
c = parse_constant(const_cast<char*>(constant.data()), &end);
EXPECT_EQ(c, 0xFFFF00000000FFFFUL);
#else
# error "unknown bits!"
#endif
}
TEST(parse_constant, parenthesized_expresions) {
char* end;
const std::vector<const char*> bad_expressions = {
"(1", "1)", "(1)1", "|(1)", "(1)|", "()",
"(", "((", "(()", "(()1", "1(0)",
};
for (const auto* expression : bad_expressions) {
std::string mutable_expression = expression;
long int c =
parse_constant(const_cast<char*>(mutable_expression.data()), &end);
EXPECT_EQ(reinterpret_cast<const void*>(end),
reinterpret_cast<const void*>(mutable_expression.data()));
// Error case should return 0.
EXPECT_EQ(c, 0) << "For expression: \"" << expression << "\"";
}
const std::vector<const char*> good_expressions = {
"(3)", "(1)|2", "1|(2)", "(1)|(2)", "((3))", "0|(1|2)", "(0|1|2)",
};
for (const auto* expression : good_expressions) {
std::string mutable_expression = expression;
long int c =
parse_constant(const_cast<char*>(mutable_expression.data()), &end);
EXPECT_EQ(c, 3) << "For expression: \"" << expression << "\"";
}
}
TEST(parse_size, complete) {
size_t size;
ASSERT_EQ(0, parse_size(&size, "42"));
ASSERT_EQ(42U, size);
ASSERT_EQ(0, parse_size(&size, "16K"));
ASSERT_EQ(16384U, size);
ASSERT_EQ(0, parse_size(&size, "1M"));
ASSERT_EQ(1024U * 1024, size);
uint64_t gigabyte = 1024ULL * 1024 * 1024;
ASSERT_EQ(0, parse_size(&size, "3G"));
ASSERT_EQ(3U, size / gigabyte);
ASSERT_EQ(0U, size % gigabyte);
ASSERT_EQ(0, parse_size(&size, "4294967294"));
ASSERT_EQ(3U, size / gigabyte);
ASSERT_EQ(gigabyte - 2, size % gigabyte);
#if __WORDSIZE == 64
uint64_t exabyte = gigabyte * 1024 * 1024 * 1024;
ASSERT_EQ(0, parse_size(&size, "9E"));
ASSERT_EQ(9U, size / exabyte);
ASSERT_EQ(0U, size % exabyte);
ASSERT_EQ(0, parse_size(&size, "15E"));
ASSERT_EQ(15U, size / exabyte);
ASSERT_EQ(0U, size % exabyte);
ASSERT_EQ(0, parse_size(&size, "18446744073709551614"));
ASSERT_EQ(15U, size / exabyte);
ASSERT_EQ(exabyte - 2, size % exabyte);
ASSERT_EQ(-ERANGE, parse_size(&size, "16E"));
ASSERT_EQ(-ERANGE, parse_size(&size, "19E"));
ASSERT_EQ(-EINVAL, parse_size(&size, "7GTPE"));
#elif __WORDSIZE == 32
ASSERT_EQ(-ERANGE, parse_size(&size, "5G"));
ASSERT_EQ(-ERANGE, parse_size(&size, "9G"));
ASSERT_EQ(-ERANGE, parse_size(&size, "9E"));
ASSERT_EQ(-ERANGE, parse_size(&size, "7GTPE"));
#endif
ASSERT_EQ(-EINVAL, parse_size(&size, ""));
ASSERT_EQ(-EINVAL, parse_size(&size, "14u"));
ASSERT_EQ(-EINVAL, parse_size(&size, "14.2G"));
ASSERT_EQ(-EINVAL, parse_size(&size, "-1G"));
ASSERT_EQ(-EINVAL, parse_size(&size, "; /bin/rm -- "));
}