blob: 21f889ee8839bacfe74a4892fa5fe06b89e397a1 [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "sandbox/linux/seccomp-bpf/sandbox_bpf.h"
#include "sandbox/linux/tests/unit_tests.h"
using namespace playground2;
namespace {
SANDBOX_TEST(ErrorCode, ErrnoConstructor) {
ErrorCode e0;
SANDBOX_ASSERT(e0.err() == SECCOMP_RET_INVALID);
ErrorCode e1(ErrorCode::ERR_ALLOWED);
SANDBOX_ASSERT(e1.err() == SECCOMP_RET_ALLOW);
ErrorCode e2(EPERM);
SANDBOX_ASSERT(e2.err() == SECCOMP_RET_ERRNO + EPERM);
ErrorCode e3 = Sandbox::Trap(NULL, NULL);
SANDBOX_ASSERT((e3.err() & SECCOMP_RET_ACTION) == SECCOMP_RET_TRAP);
}
SANDBOX_TEST(ErrorCode, Trap) {
ErrorCode e0 = Sandbox::Trap(NULL, "a");
ErrorCode e1 = Sandbox::Trap(NULL, "b");
SANDBOX_ASSERT((e0.err() & SECCOMP_RET_DATA) + 1 ==
(e1.err() & SECCOMP_RET_DATA));
ErrorCode e2 = Sandbox::Trap(NULL, "a");
SANDBOX_ASSERT((e0.err() & SECCOMP_RET_DATA) ==
(e2.err() & SECCOMP_RET_DATA));
}
SANDBOX_TEST(ErrorCode, Equals) {
ErrorCode e1(ErrorCode::ERR_ALLOWED);
ErrorCode e2(ErrorCode::ERR_ALLOWED);
SANDBOX_ASSERT(e1.Equals(e1));
SANDBOX_ASSERT(e1.Equals(e2));
SANDBOX_ASSERT(e2.Equals(e1));
ErrorCode e3(EPERM);
SANDBOX_ASSERT(!e1.Equals(e3));
ErrorCode e4 = Sandbox::Trap(NULL, "a");
ErrorCode e5 = Sandbox::Trap(NULL, "b");
ErrorCode e6 = Sandbox::Trap(NULL, "a");
SANDBOX_ASSERT(!e1.Equals(e4));
SANDBOX_ASSERT(!e3.Equals(e4));
SANDBOX_ASSERT(!e5.Equals(e4));
SANDBOX_ASSERT( e6.Equals(e4));
}
SANDBOX_TEST(ErrorCode, LessThan) {
ErrorCode e1(ErrorCode::ERR_ALLOWED);
ErrorCode e2(ErrorCode::ERR_ALLOWED);
SANDBOX_ASSERT(!e1.LessThan(e1));
SANDBOX_ASSERT(!e1.LessThan(e2));
SANDBOX_ASSERT(!e2.LessThan(e1));
ErrorCode e3(EPERM);
SANDBOX_ASSERT(!e1.LessThan(e3));
SANDBOX_ASSERT( e3.LessThan(e1));
ErrorCode e4 = Sandbox::Trap(NULL, "a");
ErrorCode e5 = Sandbox::Trap(NULL, "b");
ErrorCode e6 = Sandbox::Trap(NULL, "a");
SANDBOX_ASSERT(e1.LessThan(e4));
SANDBOX_ASSERT(e3.LessThan(e4));
SANDBOX_ASSERT(e4.LessThan(e5));
SANDBOX_ASSERT(!e4.LessThan(e6));
SANDBOX_ASSERT(!e6.LessThan(e4));
}
} // namespace