| Index: sandbox/linux/syscall_broker/broker_file_permission_unittest.cc
|
| diff --git a/sandbox/linux/syscall_broker/broker_file_permission_unittest.cc b/sandbox/linux/syscall_broker/broker_file_permission_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..503a338bcd9aed0e98f254059e7d944105c8a0d0
|
| --- /dev/null
|
| +++ b/sandbox/linux/syscall_broker/broker_file_permission_unittest.cc
|
| @@ -0,0 +1,200 @@
|
| +// Copyright 2014 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/syscall_broker/broker_file_permission.h"
|
| +
|
| +#include <fcntl.h>
|
| +#include <string.h>
|
| +#include <sys/stat.h>
|
| +#include <sys/types.h>
|
| +
|
| +#include "base/logging.h"
|
| +#include "sandbox/linux/tests/test_utils.h"
|
| +#include "sandbox/linux/tests/unit_tests.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace sandbox {
|
| +
|
| +namespace syscall_broker {
|
| +
|
| +// Creation tests are DEATH tests as a bad permission causes termination.
|
| +SANDBOX_DEATH_TEST(BrokerFilePermission, CreateGood, DEATH_SUCCESS()) {
|
| + const char k_Path[] = "/tmp/good";
|
| + BrokerFilePermission perm = BrokerFilePermission::ReadOnly(k_Path);
|
| +}
|
| +
|
| +SANDBOX_DEATH_TEST(BrokerFilePermission, CreateGoodRecursive, DEATH_SUCCESS()) {
|
| + const char k_Path[] = "/tmp/good/";
|
| + BrokerFilePermission perm = BrokerFilePermission::ReadOnlyRecursive(k_Path);
|
| +}
|
| +
|
| +SANDBOX_DEATH_TEST(BrokerFilePermission, CreateBad, DEATH_MESSAGE("")) {
|
| + const char k_Path[] = "/tmp/bad/";
|
| + BrokerFilePermission perm = BrokerFilePermission::ReadOnly(k_Path);
|
| +}
|
| +
|
| +SANDBOX_DEATH_TEST(BrokerFilePermission,
|
| + CreateBadRecursive,
|
| + DEATH_MESSAGE("")) {
|
| + const char k_Path[] = "/tmp/bad";
|
| + BrokerFilePermission perm = BrokerFilePermission::ReadOnlyRecursive(k_Path);
|
| +}
|
| +
|
| +SANDBOX_DEATH_TEST(BrokerFilePermission, CreateBadNotAbs, DEATH_MESSAGE("")) {
|
| + const char k_Path[] = "tmp/bad";
|
| + BrokerFilePermission perm = BrokerFilePermission::ReadOnly(k_Path);
|
| +}
|
| +
|
| +void CheckPerm(BrokerFilePermission& perm,
|
| + const char* path,
|
| + int access_flags,
|
| + bool create) {
|
| + const char* file_to_open;
|
| +
|
| + // check bad perms
|
| + switch (access_flags) {
|
| + case O_RDONLY:
|
| + ASSERT_TRUE(perm.CheckOpen(path, O_RDONLY, &file_to_open, NULL));
|
| + ASSERT_FALSE(perm.CheckOpen(path, O_WRONLY, &file_to_open, NULL));
|
| + ASSERT_FALSE(perm.CheckOpen(path, O_RDWR, &file_to_open, NULL));
|
| + break;
|
| + case O_WRONLY:
|
| + ASSERT_FALSE(perm.CheckOpen(path, O_RDONLY, &file_to_open, NULL));
|
| + ASSERT_TRUE(perm.CheckOpen(path, O_WRONLY, &file_to_open, NULL));
|
| + ASSERT_FALSE(perm.CheckOpen(path, O_RDWR, &file_to_open, NULL));
|
| + break;
|
| + case O_RDWR:
|
| + ASSERT_TRUE(perm.CheckOpen(path, O_RDONLY, &file_to_open, NULL));
|
| + ASSERT_TRUE(perm.CheckOpen(path, O_WRONLY, &file_to_open, NULL));
|
| + ASSERT_TRUE(perm.CheckOpen(path, O_RDWR, &file_to_open, NULL));
|
| + break;
|
| + default:
|
| + // Bad test case
|
| + NOTREACHED();
|
| + }
|
| +
|
| + // check every possible flag and act accordingly.
|
| + for (int i = 2; i < 32; i++) {
|
| + int flag = 1 << i;
|
| + switch (flag) {
|
| + case O_APPEND:
|
| + case O_ASYNC:
|
| + case O_DIRECT:
|
| + case O_DIRECTORY:
|
| + case O_DSYNC:
|
| + case O_EXCL:
|
| + case O_NOATIME:
|
| + case O_NOCTTY:
|
| + case O_NOFOLLOW:
|
| + case O_NONBLOCK:
|
| +#if (O_NONBLOCK != O_NDELAY)
|
| + case O_NDELAY:
|
| +#endif
|
| + case (O_SYNC & ~O_DSYNC):
|
| + case O_TRUNC:
|
| + ASSERT_TRUE(
|
| + perm.CheckOpen(path, access_flags | flag, &file_to_open, NULL));
|
| + break;
|
| + case O_CLOEXEC:
|
| + case O_CREAT:
|
| + default:
|
| + ASSERT_FALSE(
|
| + perm.CheckOpen(path, access_flags | flag, &file_to_open, NULL));
|
| + }
|
| + }
|
| + if (create) {
|
| + bool unlink;
|
| + ASSERT_TRUE(perm.CheckOpen(path, O_CREAT | O_EXCL | access_flags,
|
| + &file_to_open, &unlink));
|
| + ASSERT_FALSE(unlink);
|
| + } else {
|
| + ASSERT_FALSE(perm.CheckOpen(path, O_CREAT | O_EXCL | access_flags,
|
| + &file_to_open, NULL));
|
| + }
|
| +}
|
| +
|
| +TEST(BrokerFilePermission, ReadOnly) {
|
| + const char k_Path[] = "/tmp/good";
|
| + BrokerFilePermission perm = BrokerFilePermission::ReadOnly(k_Path);
|
| + CheckPerm(perm, k_Path, O_RDONLY, false);
|
| +}
|
| +
|
| +TEST(BrokerFilePermission, ReadOnlyRecursive) {
|
| + const char k_Path[] = "/tmp/good/";
|
| + const char k_PathFile[] = "/tmp/good/file";
|
| + BrokerFilePermission perm = BrokerFilePermission::ReadOnlyRecursive(k_Path);
|
| + CheckPerm(perm, k_PathFile, O_RDONLY, false);
|
| +}
|
| +
|
| +TEST(BrokerFilePermission, WriteOnly) {
|
| + const char k_Path[] = "/tmp/good";
|
| + BrokerFilePermission perm = BrokerFilePermission::WriteOnly(k_Path);
|
| + CheckPerm(perm, k_Path, O_WRONLY, false);
|
| +}
|
| +
|
| +TEST(BrokerFilePermission, ReadWrite) {
|
| + const char k_Path[] = "/tmp/good";
|
| + BrokerFilePermission perm = BrokerFilePermission::ReadWrite(k_Path);
|
| + CheckPerm(perm, k_Path, O_RDWR, false);
|
| +}
|
| +
|
| +TEST(BrokerFilePermission, ReadWriteCreate) {
|
| + const char k_Path[] = "/tmp/good";
|
| + BrokerFilePermission perm = BrokerFilePermission::ReadWriteCreate(k_Path);
|
| + CheckPerm(perm, k_Path, O_RDWR, true);
|
| +}
|
| +
|
| +void CheckUnlink(BrokerFilePermission& perm,
|
| + const char* path,
|
| + int access_flags) {
|
| + bool unlink;
|
| + ASSERT_FALSE(perm.CheckOpen(path, access_flags, NULL, &unlink));
|
| + ASSERT_FALSE(perm.CheckOpen(path, access_flags | O_CREAT, NULL, &unlink));
|
| + ASSERT_TRUE(
|
| + perm.CheckOpen(path, access_flags | O_CREAT | O_EXCL, NULL, &unlink));
|
| + ASSERT_TRUE(unlink);
|
| +}
|
| +
|
| +TEST(BrokerFilePermission, ReadWriteCreateUnlink) {
|
| + const char k_Path[] = "/tmp/good";
|
| + BrokerFilePermission perm =
|
| + BrokerFilePermission::ReadWriteCreateUnlink(k_Path);
|
| + CheckUnlink(perm, k_Path, O_RDWR);
|
| +}
|
| +
|
| +TEST(BrokerFilePermission, ReadWriteCreateUnlinkRecursive) {
|
| + const char k_Path[] = "/tmp/good/";
|
| + const char k_PathFile[] = "/tmp/good/file";
|
| + BrokerFilePermission perm =
|
| + BrokerFilePermission::ReadWriteCreateUnlinkRecursive(k_Path);
|
| + CheckUnlink(perm, k_PathFile, O_RDWR);
|
| +}
|
| +
|
| +class BrokerFilePermissionTester {
|
| + public:
|
| + static bool ValidatePath(const char* path) {
|
| + return BrokerFilePermission::ValidatePath(path);
|
| + }
|
| +
|
| + private:
|
| + DISALLOW_COPY_AND_ASSIGN(BrokerFilePermissionTester);
|
| +};
|
| +
|
| +TEST(BrokerFilePermission, ValidatePath) {
|
| + ASSERT_TRUE(BrokerFilePermissionTester::ValidatePath("/path"));
|
| + ASSERT_TRUE(BrokerFilePermissionTester::ValidatePath("/"));
|
| + ASSERT_TRUE(BrokerFilePermissionTester::ValidatePath("/..path"));
|
| +
|
| + ASSERT_FALSE(BrokerFilePermissionTester::ValidatePath(""));
|
| + ASSERT_FALSE(BrokerFilePermissionTester::ValidatePath("bad"));
|
| + ASSERT_FALSE(BrokerFilePermissionTester::ValidatePath("/bad/"));
|
| + ASSERT_FALSE(BrokerFilePermissionTester::ValidatePath("/bad/"));
|
| + ASSERT_FALSE(BrokerFilePermissionTester::ValidatePath("/bad/.."));
|
| + ASSERT_FALSE(BrokerFilePermissionTester::ValidatePath("/bad/../bad"));
|
| + ASSERT_FALSE(BrokerFilePermissionTester::ValidatePath("/../bad"));
|
| +}
|
| +
|
| +} // namespace syscall_broker
|
| +
|
| +} // namespace sandbox
|
|
|