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. 4 5 #include "sandbox/linux/seccomp-bpf/errorcode.h" 6 7 #include <errno.h> 8 9 #include "sandbox/linux/seccomp-bpf/linux_seccomp.h" 10 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h" 11 #include "sandbox/linux/tests/unit_tests.h" 12 13 namespace sandbox { 14 15 namespace { 16 17 SANDBOX_TEST(ErrorCode, ErrnoConstructor) { 18 ErrorCode e0; 19 SANDBOX_ASSERT(e0.err() == SECCOMP_RET_INVALID); 20 21 ErrorCode e1(ErrorCode::ERR_ALLOWED); 22 SANDBOX_ASSERT(e1.err() == SECCOMP_RET_ALLOW); 23 24 ErrorCode e2(EPERM); 25 SANDBOX_ASSERT(e2.err() == SECCOMP_RET_ERRNO + EPERM); 26 27 SandboxBPF sandbox; 28 ErrorCode e3 = sandbox.Trap(NULL, NULL); 29 SANDBOX_ASSERT((e3.err() & SECCOMP_RET_ACTION) == SECCOMP_RET_TRAP); 30 31 uint16_t data = 0xdead; 32 ErrorCode e4(ErrorCode::ERR_TRACE + data); 33 SANDBOX_ASSERT(e4.err() == SECCOMP_RET_TRACE + data); 34 } 35 36 SANDBOX_DEATH_TEST(ErrorCode, 37 InvalidSeccompRetTrace, 38 DEATH_MESSAGE("Invalid use of ErrorCode object")) { 39 // Should die if the trace data does not fit in 16 bits. 40 ErrorCode e(ErrorCode::ERR_TRACE + (1 << 16)); 41 } 42 43 SANDBOX_TEST(ErrorCode, Trap) { 44 SandboxBPF sandbox; 45 ErrorCode e0 = sandbox.Trap(NULL, "a"); 46 ErrorCode e1 = sandbox.Trap(NULL, "b"); 47 SANDBOX_ASSERT((e0.err() & SECCOMP_RET_DATA) + 1 == 48 (e1.err() & SECCOMP_RET_DATA)); 49 50 ErrorCode e2 = sandbox.Trap(NULL, "a"); 51 SANDBOX_ASSERT((e0.err() & SECCOMP_RET_DATA) == 52 (e2.err() & SECCOMP_RET_DATA)); 53 } 54 55 SANDBOX_TEST(ErrorCode, Equals) { 56 ErrorCode e1(ErrorCode::ERR_ALLOWED); 57 ErrorCode e2(ErrorCode::ERR_ALLOWED); 58 SANDBOX_ASSERT(e1.Equals(e1)); 59 SANDBOX_ASSERT(e1.Equals(e2)); 60 SANDBOX_ASSERT(e2.Equals(e1)); 61 62 ErrorCode e3(EPERM); 63 SANDBOX_ASSERT(!e1.Equals(e3)); 64 65 SandboxBPF sandbox; 66 ErrorCode e4 = sandbox.Trap(NULL, "a"); 67 ErrorCode e5 = sandbox.Trap(NULL, "b"); 68 ErrorCode e6 = sandbox.Trap(NULL, "a"); 69 SANDBOX_ASSERT(!e1.Equals(e4)); 70 SANDBOX_ASSERT(!e3.Equals(e4)); 71 SANDBOX_ASSERT(!e5.Equals(e4)); 72 SANDBOX_ASSERT( e6.Equals(e4)); 73 } 74 75 SANDBOX_TEST(ErrorCode, LessThan) { 76 ErrorCode e1(ErrorCode::ERR_ALLOWED); 77 ErrorCode e2(ErrorCode::ERR_ALLOWED); 78 SANDBOX_ASSERT(!e1.LessThan(e1)); 79 SANDBOX_ASSERT(!e1.LessThan(e2)); 80 SANDBOX_ASSERT(!e2.LessThan(e1)); 81 82 ErrorCode e3(EPERM); 83 SANDBOX_ASSERT(!e1.LessThan(e3)); 84 SANDBOX_ASSERT( e3.LessThan(e1)); 85 86 SandboxBPF sandbox; 87 ErrorCode e4 = sandbox.Trap(NULL, "a"); 88 ErrorCode e5 = sandbox.Trap(NULL, "b"); 89 ErrorCode e6 = sandbox.Trap(NULL, "a"); 90 SANDBOX_ASSERT(e1.LessThan(e4)); 91 SANDBOX_ASSERT(e3.LessThan(e4)); 92 SANDBOX_ASSERT(e4.LessThan(e5)); 93 SANDBOX_ASSERT(!e4.LessThan(e6)); 94 SANDBOX_ASSERT(!e6.LessThan(e4)); 95 } 96 97 } // namespace 98 99 } // namespace sandbox 100