Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / sandbox / linux / seccomp-bpf / errorcode_unittest.cc
blobf3b7748335522ea87a5e35200f9fc1d4b35b243d
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
3 // found in the LICENSE file.
5 #include <errno.h>
7 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h"
8 #include "sandbox/linux/tests/unit_tests.h"
10 namespace sandbox {
12 namespace {
14 SANDBOX_TEST(ErrorCode, ErrnoConstructor) {
15 ErrorCode e0;
16 SANDBOX_ASSERT(e0.err() == SECCOMP_RET_INVALID);
18 ErrorCode e1(ErrorCode::ERR_ALLOWED);
19 SANDBOX_ASSERT(e1.err() == SECCOMP_RET_ALLOW);
21 ErrorCode e2(EPERM);
22 SANDBOX_ASSERT(e2.err() == SECCOMP_RET_ERRNO + EPERM);
24 SandboxBPF sandbox;
25 ErrorCode e3 = sandbox.Trap(NULL, NULL);
26 SANDBOX_ASSERT((e3.err() & SECCOMP_RET_ACTION) == SECCOMP_RET_TRAP);
28 uint16_t data = 0xdead;
29 ErrorCode e4(ErrorCode::ERR_TRACE + data);
30 SANDBOX_ASSERT(e4.err() == SECCOMP_RET_TRACE + data);
33 SANDBOX_DEATH_TEST(ErrorCode,
34 InvalidSeccompRetTrace,
35 DEATH_MESSAGE("Invalid use of ErrorCode object")) {
36 // Should die if the trace data does not fit in 16 bits.
37 ErrorCode e(ErrorCode::ERR_TRACE + (1 << 16));
40 SANDBOX_TEST(ErrorCode, Trap) {
41 SandboxBPF sandbox;
42 ErrorCode e0 = sandbox.Trap(NULL, "a");
43 ErrorCode e1 = sandbox.Trap(NULL, "b");
44 SANDBOX_ASSERT((e0.err() & SECCOMP_RET_DATA) + 1 ==
45 (e1.err() & SECCOMP_RET_DATA));
47 ErrorCode e2 = sandbox.Trap(NULL, "a");
48 SANDBOX_ASSERT((e0.err() & SECCOMP_RET_DATA) ==
49 (e2.err() & SECCOMP_RET_DATA));
52 SANDBOX_TEST(ErrorCode, Equals) {
53 ErrorCode e1(ErrorCode::ERR_ALLOWED);
54 ErrorCode e2(ErrorCode::ERR_ALLOWED);
55 SANDBOX_ASSERT(e1.Equals(e1));
56 SANDBOX_ASSERT(e1.Equals(e2));
57 SANDBOX_ASSERT(e2.Equals(e1));
59 ErrorCode e3(EPERM);
60 SANDBOX_ASSERT(!e1.Equals(e3));
62 SandboxBPF sandbox;
63 ErrorCode e4 = sandbox.Trap(NULL, "a");
64 ErrorCode e5 = sandbox.Trap(NULL, "b");
65 ErrorCode e6 = sandbox.Trap(NULL, "a");
66 SANDBOX_ASSERT(!e1.Equals(e4));
67 SANDBOX_ASSERT(!e3.Equals(e4));
68 SANDBOX_ASSERT(!e5.Equals(e4));
69 SANDBOX_ASSERT( e6.Equals(e4));
72 SANDBOX_TEST(ErrorCode, LessThan) {
73 ErrorCode e1(ErrorCode::ERR_ALLOWED);
74 ErrorCode e2(ErrorCode::ERR_ALLOWED);
75 SANDBOX_ASSERT(!e1.LessThan(e1));
76 SANDBOX_ASSERT(!e1.LessThan(e2));
77 SANDBOX_ASSERT(!e2.LessThan(e1));
79 ErrorCode e3(EPERM);
80 SANDBOX_ASSERT(!e1.LessThan(e3));
81 SANDBOX_ASSERT( e3.LessThan(e1));
83 SandboxBPF sandbox;
84 ErrorCode e4 = sandbox.Trap(NULL, "a");
85 ErrorCode e5 = sandbox.Trap(NULL, "b");
86 ErrorCode e6 = sandbox.Trap(NULL, "a");
87 SANDBOX_ASSERT(e1.LessThan(e4));
88 SANDBOX_ASSERT(e3.LessThan(e4));
89 SANDBOX_ASSERT(e4.LessThan(e5));
90 SANDBOX_ASSERT(!e4.LessThan(e6));
91 SANDBOX_ASSERT(!e6.LessThan(e4));
94 } // namespace
96 } // namespace sandbox