Debian Codegraph @

mfbt/tests/gtest/TestTainting.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "gtest/gtest.h"
8 #include <math.h>
9
10 #include "mozilla/Array.h"
11 #include "mozilla/Assertions.h"
12 #include "mozilla/Range.h"
13 #include "mozilla/Tainting.h"
14 #include "nsTHashtable.h"
15 #include "nsHashKeys.h"
16 #include "nsTArray.h"
17 #include <array>
18 #include <deque>
19 #include <forward_list>
20 #include <list>
21 #include <map>
22 #include <set>
23 #include <unordered_map>
24 #include <unordered_set>
25 #include <vector>
26
27 using mozilla::Tainted;
28
29 #define EXPECTED_INT 10
30 #define EXPECTED_CHAR 'z'
31
32 static bool externalFunction(int arg) { return arg > 2; }
33
34 // ==================================================================
35 // MOZ_VALIDATE_AND_GET =============================================
36 TEST(Tainting, moz_validate_and_get)
37 {
38 int bar;
39 int comparisonVariable = 20;
40 Tainted<int> foo = Tainted<int>(EXPECTED_INT);
41
42 bar = MOZ_VALIDATE_AND_GET(foo, foo < 20);
43 ASSERT_EQ(bar, EXPECTED_INT);
44
45 // This test is for comparison to an external variable, testing the
46 // default capture mode of the lambda used inside the macro.
47 bar = MOZ_VALIDATE_AND_GET(foo, foo < comparisonVariable);
48 ASSERT_EQ(bar, EXPECTED_INT);
49
50 bar = MOZ_VALIDATE_AND_GET(
51 foo, foo < 20,
52 "foo must be less than 20 because higher values represent decibel"
53 "levels greater than a a jet engine inside your ear.");
54 ASSERT_EQ(bar, EXPECTED_INT);
55
56 // Test an external variable with a comment.
57 bar = MOZ_VALIDATE_AND_GET(foo, foo < comparisonVariable, "Test comment");
58 ASSERT_EQ(bar, EXPECTED_INT);
59
60 // Test an external function with a comment.
61 bar = MOZ_VALIDATE_AND_GET(foo, externalFunction(foo), "Test comment");
62 ASSERT_EQ(bar, EXPECTED_INT);
63
64 // Lambda Tests
65 bar =
66 MOZ_VALIDATE_AND_GET(foo, ([&foo]() { return externalFunction(foo); }()));
67 ASSERT_EQ(bar, EXPECTED_INT);
68
69 // This test is for the lambda variant with a supplied assertion
70 // string.
71 bar =
72 MOZ_VALIDATE_AND_GET(foo, ([&foo]() { return externalFunction(foo); }()),
73 "This tests a comment");
74 ASSERT_EQ(bar, EXPECTED_INT);
75
76 // This test is for the lambda variant with a captured variable
77 bar = MOZ_VALIDATE_AND_GET(foo, ([&foo, &comparisonVariable] {
78 bool intermediateResult = externalFunction(foo);
79 return intermediateResult ||
80 comparisonVariable < 4;
81 }()),
82 "This tests a comment");
83 ASSERT_EQ(bar, EXPECTED_INT);
84
85 // This test is for the lambda variant with full capture mode
86 bar = MOZ_VALIDATE_AND_GET(foo, ([&] {
87 bool intermediateResult = externalFunction(foo);
88 return intermediateResult ||
89 comparisonVariable < 4;
90 }()),
91 "This tests a comment");
92 ASSERT_EQ(bar, EXPECTED_INT);
93
94 // External lambdas
95 auto lambda1 = [](int foo) { return externalFunction(foo); };
96
97 auto lambda2 = [&](int foo) {
98 bool intermediateResult = externalFunction(foo);
99 return intermediateResult || comparisonVariable < 4;
100 };
101
102 // Test with an explicit capture
103 auto lambda3 = [&comparisonVariable](int foo) {
104 bool intermediateResult = externalFunction(foo);
105 return intermediateResult || comparisonVariable < 4;
106 };
107
108 bar = MOZ_VALIDATE_AND_GET(foo, lambda1(foo));
109 ASSERT_EQ(bar, EXPECTED_INT);
110
111 // Test with a comment
112 bar = MOZ_VALIDATE_AND_GET(foo, lambda1(foo), "Test comment.");
113 ASSERT_EQ(bar, EXPECTED_INT);
114
115 // Test with a default capture mode
116 bar = MOZ_VALIDATE_AND_GET(foo, lambda2(foo), "Test comment.");
117 ASSERT_EQ(bar, EXPECTED_INT);
118
119 bar = MOZ_VALIDATE_AND_GET(foo, lambda3(foo), "Test comment.");
120 ASSERT_EQ(bar, EXPECTED_INT);
121
122 // We can't test MOZ_VALIDATE_AND_GET failing, because that triggers
123 // a release assert.
124 }
125
126 // ==================================================================
127 // MOZ_IS_VALID =====================================================
128 TEST(Tainting, moz_is_valid)
129 {
130 int comparisonVariable = 20;
131 Tainted<int> foo = Tainted<int>(EXPECTED_INT);
132
133 ASSERT_TRUE(MOZ_IS_VALID(foo, foo < 20));
134
135 ASSERT_FALSE(MOZ_IS_VALID(foo, foo > 20));
136
137 ASSERT_TRUE(MOZ_IS_VALID(foo, foo < comparisonVariable));
138
139 ASSERT_TRUE(
140 MOZ_IS_VALID(foo, ([&foo]() { return externalFunction(foo); }())));
141
142 ASSERT_TRUE(MOZ_IS_VALID(foo, ([&foo, &comparisonVariable]() {
143 bool intermediateResult = externalFunction(foo);
144 return intermediateResult ||
145 comparisonVariable < 4;
146 }())));
147
148 // External lambdas
149 auto lambda1 = [](int foo) { return externalFunction(foo); };
150
151 auto lambda2 = [&](int foo) {
152 bool intermediateResult = externalFunction(foo);
153 return intermediateResult || comparisonVariable < 4;
154 };
155
156 // Test with an explicit capture
157 auto lambda3 = [&comparisonVariable](int foo) {
158 bool intermediateResult = externalFunction(foo);
159 return intermediateResult || comparisonVariable < 4;
160 };
161
162 ASSERT_TRUE(MOZ_IS_VALID(foo, lambda1(foo)));
163
164 ASSERT_TRUE(MOZ_IS_VALID(foo, lambda2(foo)));
165
166 ASSERT_TRUE(MOZ_IS_VALID(foo, lambda3(foo)));
167 }
168
169 // ==================================================================
170 // MOZ_VALIDATE_OR ==================================================
171 TEST(Tainting, moz_validate_or)
172 {
173 int result;
174 int comparisonVariable = 20;
175 Tainted<int> foo = Tainted<int>(EXPECTED_INT);
176
177 result = MOZ_VALIDATE_OR(foo, foo < 20, 100);
178 ASSERT_EQ(result, EXPECTED_INT);
179
180 result = MOZ_VALIDATE_OR(foo, foo > 20, 100);
181 ASSERT_EQ(result, 100);
182
183 result = MOZ_VALIDATE_OR(foo, foo < comparisonVariable, 100);
184 ASSERT_EQ(result, EXPECTED_INT);
185
186 // External lambdas
187 auto lambda1 = [](int foo) { return externalFunction(foo); };
188
189 auto lambda2 = [&](int foo) {
190 bool intermediateResult = externalFunction(foo);
191 return intermediateResult || comparisonVariable < 4;
192 };
193
194 // Test with an explicit capture
195 auto lambda3 = [&comparisonVariable](int foo) {
196 bool intermediateResult = externalFunction(foo);
197 return intermediateResult || comparisonVariable < 4;
198 };
199
200 result = MOZ_VALIDATE_OR(foo, lambda1(foo), 100);
201 ASSERT_EQ(result, EXPECTED_INT);
202
203 result = MOZ_VALIDATE_OR(foo, lambda2(foo), 100);
204 ASSERT_EQ(result, EXPECTED_INT);
205
206 result = MOZ_VALIDATE_OR(foo, lambda3(foo), 100);
207 ASSERT_EQ(result, EXPECTED_INT);
208
209 result =
210 MOZ_VALIDATE_OR(foo, ([&foo]() { return externalFunction(foo); }()), 100);
211 ASSERT_EQ(result, EXPECTED_INT);
212
213 // This test is for the lambda variant with a supplied assertion
214 // string.
215 result =
216 MOZ_VALIDATE_OR(foo, ([&foo] { return externalFunction(foo); }()), 100);
217 ASSERT_EQ(result, EXPECTED_INT);
218
219 // This test is for the lambda variant with a captured variable
220 result =
221 MOZ_VALIDATE_OR(foo, ([&foo, &comparisonVariable] {
222 bool intermediateResult = externalFunction(foo);
223 return intermediateResult || comparisonVariable < 4;
224 }()),
225 100);
226 ASSERT_EQ(result, EXPECTED_INT);
227
228 // This test is for the lambda variant with full capture mode
229 result =
230 MOZ_VALIDATE_OR(foo, ([&] {
231 bool intermediateResult = externalFunction(foo);
232 return intermediateResult || comparisonVariable < 4;
233 }()),
234 100);
235 ASSERT_EQ(result, EXPECTED_INT);
236 }
237
238 // ==================================================================
239 // MOZ_FIND_AND_VALIDATE ============================================
240 TEST(Tainting, moz_find_and_validate)
241 {
242 Tainted<int> foo = Tainted<int>(EXPECTED_INT);
243 Tainted<char> baz = Tainted<char>(EXPECTED_CHAR);
244
245 //-------------------------------
246 const mozilla::Array<int, 6> mozarrayWithFoo(0, 5, EXPECTED_INT, 15, 20, 25);
247 const mozilla::Array<int, 5> mozarrayWithoutFoo(0, 5, 15, 20, 25);
248
249 ASSERT_TRUE(*MOZ_FIND_AND_VALIDATE(foo, list_item == foo, mozarrayWithFoo) ==
250 mozarrayWithFoo[2]);
251
252 ASSERT_TRUE(MOZ_FIND_AND_VALIDATE(foo, list_item == foo,
253 mozarrayWithoutFoo) == nullptr);
254
255 //-------------------------------
256 class TestClass {
257 public:
258 int a;
259 int b;
260
261 TestClass(int a, int b) {
262 this->a = a;
263 this->b = b;
264 }
265
266 bool operator==(const TestClass& other) const {
267 return this->a == other.a && this->b == other.b;
268 }
269 };
270
271 const mozilla::Array<TestClass, 5> mozarrayOfClassesWithFoo(
272 TestClass(0, 1), TestClass(2, 3), TestClass(EXPECTED_INT, EXPECTED_INT),
273 TestClass(4, 5), TestClass(6, 7));
274
275 ASSERT_TRUE(*MOZ_FIND_AND_VALIDATE(
276 foo, foo == list_item.a && foo == list_item.b,
277 mozarrayOfClassesWithFoo) == mozarrayOfClassesWithFoo[2]);
278
279 ASSERT_TRUE(*MOZ_FIND_AND_VALIDATE(
280 foo, (foo == list_item.a && foo == list_item.b),
281 mozarrayOfClassesWithFoo) == mozarrayOfClassesWithFoo[2]);
282
283 ASSERT_TRUE(
284 *MOZ_FIND_AND_VALIDATE(
285 foo,
286 (foo == list_item.a && foo == list_item.b && externalFunction(foo)),
287 mozarrayOfClassesWithFoo) == mozarrayOfClassesWithFoo[2]);
288
289 ASSERT_TRUE(*MOZ_FIND_AND_VALIDATE(
290 foo, ([](int tainted_val, TestClass list_item) {
291 return tainted_val == list_item.a &&
292 tainted_val == list_item.b;
293 }(foo, list_item)),
294 mozarrayOfClassesWithFoo) == mozarrayOfClassesWithFoo[2]);
295
296 auto lambda4 = [](int tainted_val, TestClass list_item) {
297 return tainted_val == list_item.a && tainted_val == list_item.b;
298 };
299
300 ASSERT_TRUE(*MOZ_FIND_AND_VALIDATE(foo, lambda4(foo, list_item),
301 mozarrayOfClassesWithFoo) ==
302 mozarrayOfClassesWithFoo[2]);
303
304 //-------------------------------
305 const char m[] = "m";
306 const char o[] = "o";
307 const char z[] = {EXPECTED_CHAR, '\0'};
308 const char l[] = "l";
309 const char a[] = "a";
310
311 nsTHashtable<nsCharPtrHashKey> hashtableWithBaz;
312 hashtableWithBaz.PutEntry(m);
313 hashtableWithBaz.PutEntry(o);
314 hashtableWithBaz.PutEntry(z);
315 hashtableWithBaz.PutEntry(l);
316 hashtableWithBaz.PutEntry(a);
317 nsTHashtable<nsCharPtrHashKey> hashtableWithoutBaz;
318 hashtableWithoutBaz.PutEntry(m);
319 hashtableWithoutBaz.PutEntry(o);
320 hashtableWithoutBaz.PutEntry(l);
321 hashtableWithoutBaz.PutEntry(a);
322
323 ASSERT_TRUE(MOZ_FIND_AND_VALIDATE(baz, *list_item.GetKey() == baz,
324 hashtableWithBaz) ==
325 hashtableWithBaz.GetEntry(z));
326
327 ASSERT_TRUE(MOZ_FIND_AND_VALIDATE(baz, *list_item.GetKey() == baz,
328 hashtableWithoutBaz) == nullptr);
329
330 //-------------------------------
331 const nsTArray<int> nsTArrayWithFoo = {0, 5, EXPECTED_INT, 15, 20, 25};
332 const nsTArray<int> nsTArrayWithoutFoo = {0, 5, 15, 20, 25};
333
334 ASSERT_TRUE(*MOZ_FIND_AND_VALIDATE(foo, list_item == foo, nsTArrayWithFoo) ==
335 nsTArrayWithFoo[2]);
336
337 ASSERT_TRUE(MOZ_FIND_AND_VALIDATE(foo, list_item == foo,
338 nsTArrayWithoutFoo) == nullptr);
339
340 //-------------------------------
341 const std::array<int, 6> arrayWithFoo{0, 5, EXPECTED_INT, 15, 20, 25};
342 const std::array<int, 5> arrayWithoutFoo{0, 5, 15, 20, 25};
343
344 ASSERT_TRUE(*MOZ_FIND_AND_VALIDATE(foo, list_item == foo, arrayWithFoo) ==
345 arrayWithFoo[2]);
346
347 ASSERT_TRUE(MOZ_FIND_AND_VALIDATE(foo, list_item == foo, arrayWithoutFoo) ==
348 nullptr);
349
350 //-------------------------------
351 const std::deque<int> dequeWithFoo{0, 5, EXPECTED_INT, 15, 20, 25};
352 const std::deque<int> dequeWithoutFoo{0, 5, 15, 20, 25};
353
354 ASSERT_TRUE(*MOZ_FIND_AND_VALIDATE(foo, list_item == foo, dequeWithFoo) ==
355 dequeWithFoo[2]);
356
357 ASSERT_TRUE(MOZ_FIND_AND_VALIDATE(foo, list_item == foo, dequeWithoutFoo) ==
358 nullptr);
359
360 //-------------------------------
361 const std::forward_list<int> forwardWithFoo{0, 5, EXPECTED_INT, 15, 20, 25};
362 const std::forward_list<int> forwardWithoutFoo{0, 5, 15, 20, 25};
363
364 auto forwardListIt = forwardWithFoo.begin();
365 std::advance(forwardListIt, 2);
366
367 ASSERT_TRUE(*MOZ_FIND_AND_VALIDATE(foo, list_item == foo, forwardWithFoo) ==
368 *forwardListIt);
369
370 ASSERT_TRUE(MOZ_FIND_AND_VALIDATE(foo, list_item == foo, forwardWithoutFoo) ==
371 nullptr);
372
373 //-------------------------------
374 const std::list<int> listWithFoo{0, 5, EXPECTED_INT, 15, 20, 25};
375 const std::list<int> listWithoutFoo{0, 5, 15, 20, 25};
376
377 auto listIt = listWithFoo.begin();
378 std::advance(listIt, 2);
379
380 ASSERT_TRUE(*MOZ_FIND_AND_VALIDATE(foo, list_item == foo, listWithFoo) ==
381 *listIt);
382
383 ASSERT_TRUE(MOZ_FIND_AND_VALIDATE(foo, list_item == foo, listWithoutFoo) ==
384 nullptr);
385
386 //-------------------------------
387 const std::map<std::string, int> mapWithFoo{{
388 {"zero", 0},
389 {"five", 5},
390 {"ten", EXPECTED_INT},
391 {"fifteen", 15},
392 {"twenty", 20},
393 {"twenty-five", 25},
394 }};
395 const std::map<std::string, int> mapWithoutFoo{{
396 {"zero", 0},
397 {"five", 5},
398 {"fifteen", 15},
399 {"twenty", 20},
400 {"twenty-five", 25},
401 }};
402
403 const auto map_it = mapWithFoo.find("ten");
404
405 ASSERT_TRUE(
406 MOZ_FIND_AND_VALIDATE(foo, list_item.second == foo, mapWithFoo)->second ==
407 map_it->second);
408
409 ASSERT_TRUE(MOZ_FIND_AND_VALIDATE(foo, list_item.second == foo,
410 mapWithoutFoo) == nullptr);
411
412 //-------------------------------
413 const std::set<int> setWithFoo{0, 5, EXPECTED_INT, 15, 20, 25};
414 const std::set<int> setWithoutFoo{0, 5, 15, 20, 25};
415
416 auto setIt = setWithFoo.find(EXPECTED_INT);
417
418 ASSERT_TRUE(*MOZ_FIND_AND_VALIDATE(foo, list_item == foo, setWithFoo) ==
419 *setIt);
420
421 ASSERT_TRUE(MOZ_FIND_AND_VALIDATE(foo, list_item == foo, setWithoutFoo) ==
422 nullptr);
423
424 //-------------------------------
425 const std::unordered_map<std::string, int> unordermapWithFoo = {
426 {"zero", 0}, {"five", 5}, {"ten", EXPECTED_INT},
427 {"fifteen", 15}, {"twenty", 20}, {"twenty-five", 25},
428 };
429 const std::unordered_map<std::string, int> unordermapWithoutFoo{{
430 {"zero", 0},
431 {"five", 5},
432 {"fifteen", 15},
433 {"twenty", 20},
434 {"twenty-five", 25},
435 }};
436
437 auto unorderedMapIt = unordermapWithFoo.find("ten");
438
439 ASSERT_TRUE(
440 MOZ_FIND_AND_VALIDATE(foo, list_item.second == foo, unordermapWithFoo)
441 ->second == unorderedMapIt->second);
442
443 ASSERT_TRUE(MOZ_FIND_AND_VALIDATE(foo, list_item.second == foo,
444 unordermapWithoutFoo) == nullptr);
445
446 //-------------------------------
447 const std::unordered_set<int> unorderedsetWithFoo{0, 5, EXPECTED_INT,
448 15, 20, 25};
449 const std::unordered_set<int> unorderedsetWithoutFoo{0, 5, 15, 20, 25};
450
451 auto unorderedSetIt = unorderedsetWithFoo.find(EXPECTED_INT);
452
453 ASSERT_TRUE(*MOZ_FIND_AND_VALIDATE(foo, list_item == foo,
454 unorderedsetWithFoo) == *unorderedSetIt);
455
456 ASSERT_TRUE(MOZ_FIND_AND_VALIDATE(foo, list_item == foo,
457 unorderedsetWithoutFoo) == nullptr);
458
459 //-------------------------------
460 const std::vector<int> vectorWithFoo{0, 5, EXPECTED_INT, 15, 20, 25};
461 const std::vector<int> vectorWithoutFoo{0, 5, 15, 20, 25};
462
463 ASSERT_TRUE(*MOZ_FIND_AND_VALIDATE(foo, list_item == foo, vectorWithFoo) ==
464 vectorWithFoo[2]);
465
466 ASSERT_TRUE(MOZ_FIND_AND_VALIDATE(foo, list_item == foo, vectorWithoutFoo) ==
467 nullptr);
468 }
469
470 // ==================================================================
471 // MOZ_NO_VALIDATE ==================================================
472 TEST(Tainting, moz_no_validate)
473 {
474 int result;
475 Tainted<int> foo = Tainted<int>(EXPECTED_INT);
476
477 result = MOZ_NO_VALIDATE(
478 foo,
479 "Value is used to match against a dictionary key in the parent."
480 "If there's no key present, there won't be a match."
481 "There is no risk of grabbing a cross-origin value from the dictionary,"
482 "because the IPC actor is instatiated per-content-process and the "
483 "dictionary is not shared between actors.");
484 ASSERT_TRUE(result == EXPECTED_INT);
485 }