Home | History | Annotate | Download | only in seccomp-bpf
      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 <errno.h>
      6 
      7 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h"
      8 #include "sandbox/linux/tests/unit_tests.h"
      9 
     10 namespace sandbox {
     11 
     12 namespace {
     13 
     14 SANDBOX_TEST(ErrorCode, ErrnoConstructor) {
     15   ErrorCode e0;
     16   SANDBOX_ASSERT(e0.err() == SECCOMP_RET_INVALID);
     17 
     18   ErrorCode e1(ErrorCode::ERR_ALLOWED);
     19   SANDBOX_ASSERT(e1.err() == SECCOMP_RET_ALLOW);
     20 
     21   ErrorCode e2(EPERM);
     22   SANDBOX_ASSERT(e2.err() == SECCOMP_RET_ERRNO + EPERM);
     23 
     24   SandboxBPF sandbox;
     25   ErrorCode e3 = sandbox.Trap(NULL, NULL);
     26   SANDBOX_ASSERT((e3.err() & SECCOMP_RET_ACTION)  == SECCOMP_RET_TRAP);
     27 }
     28 
     29 SANDBOX_TEST(ErrorCode, Trap) {
     30   SandboxBPF sandbox;
     31   ErrorCode e0 = sandbox.Trap(NULL, "a");
     32   ErrorCode e1 = sandbox.Trap(NULL, "b");
     33   SANDBOX_ASSERT((e0.err() & SECCOMP_RET_DATA) + 1 ==
     34                  (e1.err() & SECCOMP_RET_DATA));
     35 
     36   ErrorCode e2 = sandbox.Trap(NULL, "a");
     37   SANDBOX_ASSERT((e0.err() & SECCOMP_RET_DATA) ==
     38                  (e2.err() & SECCOMP_RET_DATA));
     39 }
     40 
     41 SANDBOX_TEST(ErrorCode, Equals) {
     42   ErrorCode e1(ErrorCode::ERR_ALLOWED);
     43   ErrorCode e2(ErrorCode::ERR_ALLOWED);
     44   SANDBOX_ASSERT(e1.Equals(e1));
     45   SANDBOX_ASSERT(e1.Equals(e2));
     46   SANDBOX_ASSERT(e2.Equals(e1));
     47 
     48   ErrorCode e3(EPERM);
     49   SANDBOX_ASSERT(!e1.Equals(e3));
     50 
     51   SandboxBPF sandbox;
     52   ErrorCode e4 = sandbox.Trap(NULL, "a");
     53   ErrorCode e5 = sandbox.Trap(NULL, "b");
     54   ErrorCode e6 = sandbox.Trap(NULL, "a");
     55   SANDBOX_ASSERT(!e1.Equals(e4));
     56   SANDBOX_ASSERT(!e3.Equals(e4));
     57   SANDBOX_ASSERT(!e5.Equals(e4));
     58   SANDBOX_ASSERT( e6.Equals(e4));
     59 }
     60 
     61 SANDBOX_TEST(ErrorCode, LessThan) {
     62   ErrorCode e1(ErrorCode::ERR_ALLOWED);
     63   ErrorCode e2(ErrorCode::ERR_ALLOWED);
     64   SANDBOX_ASSERT(!e1.LessThan(e1));
     65   SANDBOX_ASSERT(!e1.LessThan(e2));
     66   SANDBOX_ASSERT(!e2.LessThan(e1));
     67 
     68   ErrorCode e3(EPERM);
     69   SANDBOX_ASSERT(!e1.LessThan(e3));
     70   SANDBOX_ASSERT( e3.LessThan(e1));
     71 
     72   SandboxBPF sandbox;
     73   ErrorCode e4 = sandbox.Trap(NULL, "a");
     74   ErrorCode e5 = sandbox.Trap(NULL, "b");
     75   ErrorCode e6 = sandbox.Trap(NULL, "a");
     76   SANDBOX_ASSERT(e1.LessThan(e4));
     77   SANDBOX_ASSERT(e3.LessThan(e4));
     78   SANDBOX_ASSERT(e4.LessThan(e5));
     79   SANDBOX_ASSERT(!e4.LessThan(e6));
     80   SANDBOX_ASSERT(!e6.LessThan(e4));
     81 }
     82 
     83 }  // namespace
     84 
     85 }  // namespace sandbox
     86