Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(291)

Side by Side Diff: sandbox/linux/seccomp-bpf/syscall_iterator_unittest.cc

Issue 659723002: SyscallIterator: support C++11 range-based for loops (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add 'explicit' Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sandbox/linux/seccomp-bpf/syscall_iterator.cc ('k') | sandbox/linux/seccomp-bpf/verifier.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "sandbox/linux/seccomp-bpf/syscall_iterator.h" 5 #include "sandbox/linux/seccomp-bpf/syscall_iterator.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include "sandbox/linux/seccomp-bpf/linux_seccomp.h" 9 #include "sandbox/linux/seccomp-bpf/linux_seccomp.h"
10 #include "sandbox/linux/tests/unit_tests.h" 10 #include "sandbox/linux/tests/unit_tests.h"
11 11
12 namespace sandbox { 12 namespace sandbox {
13 13
14 namespace { 14 namespace {
15 15
16 const bool kFalseTrue[] = {false, true}; 16 const SyscallSet kSyscallSets[] = {
17 SyscallSet::All(),
18 SyscallSet::InvalidOnly(),
19 };
17 20
18 SANDBOX_TEST(SyscallIterator, Monotonous) { 21 SANDBOX_TEST(SyscallIterator, Monotonous) {
19 for (bool invalid_only : kFalseTrue) { 22 for (const SyscallSet& set : kSyscallSets) {
20 uint32_t prev = 0; 23 uint32_t prev = 0;
21 bool have_prev = false; 24 bool have_prev = false;
22 for (SyscallIterator iter(invalid_only); !iter.Done();) { 25 for (uint32_t sysnum : set) {
23 uint32_t sysnum = iter.Next();
24
25 if (have_prev) { 26 if (have_prev) {
26 SANDBOX_ASSERT(sysnum > prev); 27 SANDBOX_ASSERT(sysnum > prev);
27 } else if (!invalid_only) { 28 } else if (set == SyscallSet::All()) {
28 // The iterator should start at 0. 29 // The iterator should start at 0.
29 SANDBOX_ASSERT(sysnum == 0); 30 SANDBOX_ASSERT(sysnum == 0);
30 } 31 }
31 32
32 prev = sysnum; 33 prev = sysnum;
33 have_prev = true; 34 have_prev = true;
34 } 35 }
35 36
36 // The iterator should always return 0xFFFFFFFFu as the last value. 37 // The iterator should always return 0xFFFFFFFFu as the last value.
37 SANDBOX_ASSERT(have_prev); 38 SANDBOX_ASSERT(have_prev);
38 SANDBOX_ASSERT(prev == 0xFFFFFFFFu); 39 SANDBOX_ASSERT(prev == 0xFFFFFFFFu);
39 } 40 }
40 } 41 }
41 42
42 // AssertRange checks that SyscallIterator produces all system call 43 // AssertRange checks that SyscallIterator produces all system call
43 // numbers in the inclusive range [min, max]. 44 // numbers in the inclusive range [min, max].
44 void AssertRange(uint32_t min, uint32_t max) { 45 void AssertRange(uint32_t min, uint32_t max) {
45 SANDBOX_ASSERT(min < max); 46 SANDBOX_ASSERT(min < max);
46 uint32_t prev = min - 1; 47 uint32_t prev = min - 1;
47 for (SyscallIterator iter(false); !iter.Done();) { 48 for (uint32_t sysnum : SyscallSet::All()) {
48 uint32_t sysnum = iter.Next();
49 if (sysnum >= min && sysnum <= max) { 49 if (sysnum >= min && sysnum <= max) {
50 SANDBOX_ASSERT(prev == sysnum - 1); 50 SANDBOX_ASSERT(prev == sysnum - 1);
51 prev = sysnum; 51 prev = sysnum;
52 } 52 }
53 } 53 }
54 SANDBOX_ASSERT(prev == max); 54 SANDBOX_ASSERT(prev == max);
55 } 55 }
56 56
57 SANDBOX_TEST(SyscallIterator, ValidSyscallRanges) { 57 SANDBOX_TEST(SyscallIterator, ValidSyscallRanges) {
58 AssertRange(MIN_SYSCALL, MAX_PUBLIC_SYSCALL); 58 AssertRange(MIN_SYSCALL, MAX_PUBLIC_SYSCALL);
(...skipping 14 matching lines...) Expand all
73 MIN_PRIVATE_SYSCALL - 1, 73 MIN_PRIVATE_SYSCALL - 1,
74 MAX_PRIVATE_SYSCALL + 1, 74 MAX_PRIVATE_SYSCALL + 1,
75 MIN_GHOST_SYSCALL - 1, 75 MIN_GHOST_SYSCALL - 1,
76 MAX_SYSCALL + 1, 76 MAX_SYSCALL + 1,
77 #endif 77 #endif
78 0x7FFFFFFFu, 78 0x7FFFFFFFu,
79 0x80000000u, 79 0x80000000u,
80 0xFFFFFFFFu, 80 0xFFFFFFFFu,
81 }; 81 };
82 82
83 for (bool invalid_only : kFalseTrue) { 83 for (const SyscallSet& set : kSyscallSets) {
84 size_t i = 0; 84 size_t i = 0;
85 for (SyscallIterator iter(invalid_only); !iter.Done();) { 85 for (uint32_t sysnum : set) {
86 uint32_t sysnum = iter.Next(); 86 if (!SyscallSet::IsValid(sysnum)) {
87 if (!SyscallIterator::IsValid(sysnum)) {
88 SANDBOX_ASSERT(i < arraysize(kExpected)); 87 SANDBOX_ASSERT(i < arraysize(kExpected));
89 SANDBOX_ASSERT(kExpected[i] == sysnum); 88 SANDBOX_ASSERT(kExpected[i] == sysnum);
90 ++i; 89 ++i;
91 } 90 }
92 } 91 }
93 SANDBOX_ASSERT(i == arraysize(kExpected)); 92 SANDBOX_ASSERT(i == arraysize(kExpected));
94 } 93 }
95 } 94 }
96 95
96 SANDBOX_TEST(SyscallIterator, InvalidOnlyIsOnlyInvalid) {
97 for (uint32_t sysnum : SyscallSet::InvalidOnly()) {
98 SANDBOX_ASSERT(!SyscallSet::IsValid(sysnum));
99 }
100 }
101
97 } // namespace 102 } // namespace
98 103
99 } // namespace sandbox 104 } // namespace sandbox
OLDNEW
« no previous file with comments | « sandbox/linux/seccomp-bpf/syscall_iterator.cc ('k') | sandbox/linux/seccomp-bpf/verifier.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698