Chris Masone | 4463bea | 2011-04-27 08:22:40 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
Will Drewry | 4dd0e66 | 2010-01-19 14:43:50 -0800 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | // |
| 5 | // Tests for minijail::Options |
Chris Masone | 4463bea | 2011-04-27 08:22:40 -0700 | [diff] [blame] | 6 | #include "options.h" |
| 7 | |
Will Drewry | 4dd0e66 | 2010-01-19 14:43:50 -0800 | [diff] [blame] | 8 | #include "mock_env.h" |
| 9 | #include "mock_options.h" |
| 10 | #include <gmock/gmock.h> |
| 11 | #include <gtest/gtest.h> |
| 12 | |
| 13 | namespace chromeos { |
| 14 | |
| 15 | using ::testing::_; // wildcard mock matcher |
| 16 | using ::testing::AtLeast; // Times modifier |
| 17 | using ::testing::Invoke; // Allow concrete call redirection |
| 18 | using ::testing::DefaultValue; // allow for easy default return value change |
| 19 | using ::testing::Return; // mock Return action |
| 20 | |
| 21 | class OptionsDepsTest : public ::testing::Test { |
| 22 | public: |
| 23 | OptionsDepsTest() : options_(new minijail::MockOptions) { } |
| 24 | ~OptionsDepsTest() { } |
| 25 | void SetUp() { |
| 26 | ON_CALL(*options_, FixUpDependencies()) |
| 27 | .WillByDefault(Invoke(options_.get(), |
| 28 | &minijail::MockOptions::OptionsFixUpDependencies)); |
| 29 | } |
| 30 | protected: |
| 31 | scoped_ptr<minijail::MockOptions> options_; |
| 32 | }; |
| 33 | |
| 34 | TEST_F(OptionsDepsTest, NothingToCorrect) { |
| 35 | // Since all options default to false, this should just work. |
Chris Masone | 4463bea | 2011-04-27 08:22:40 -0700 | [diff] [blame] | 36 | minijail::Options options; |
| 37 | EXPECT_TRUE(options.FixUpDependencies()); |
Will Drewry | 4dd0e66 | 2010-01-19 14:43:50 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | TEST_F(OptionsDepsTest, MountsWithoutVfs) { |
Chris Masone | 4463bea | 2011-04-27 08:22:40 -0700 | [diff] [blame] | 41 | EXPECT_CALL(*(options_.get()), FixUpDependencies()) |
| 42 | .Times(1); |
| 43 | EXPECT_CALL(*options_, enforce_syscalls_benchmark()) |
| 44 | .WillOnce(Return(false)); |
Will Drewry | 4dd0e66 | 2010-01-19 14:43:50 -0800 | [diff] [blame] | 45 | // Set up the case in need of correction |
| 46 | EXPECT_CALL(*options_, add_readonly_mounts()) |
Will Drewry | 4dd0e66 | 2010-01-19 14:43:50 -0800 | [diff] [blame] | 47 | .WillOnce(Return(true)); |
| 48 | EXPECT_CALL(*options_, namespace_vfs()) |
Will Drewry | 4dd0e66 | 2010-01-19 14:43:50 -0800 | [diff] [blame] | 49 | .WillOnce(Return(false)); |
| 50 | EXPECT_CALL(*options_, set_namespace_vfs(true)) // Proof of correction |
| 51 | .Times(1); |
| 52 | EXPECT_TRUE(options_->FixUpDependencies()); |
| 53 | } |
| 54 | |
| 55 | TEST_F(OptionsDepsTest, MountsWithVfs) { |
Chris Masone | 4463bea | 2011-04-27 08:22:40 -0700 | [diff] [blame] | 56 | EXPECT_CALL(*(options_.get()), FixUpDependencies()) |
| 57 | .Times(1); |
| 58 | EXPECT_CALL(*options_, enforce_syscalls_benchmark()) |
| 59 | .WillOnce(Return(false)); |
Will Drewry | 4dd0e66 | 2010-01-19 14:43:50 -0800 | [diff] [blame] | 60 | // Setup case which should be untouched |
| 61 | EXPECT_CALL(*options_, add_readonly_mounts()) |
Will Drewry | 4dd0e66 | 2010-01-19 14:43:50 -0800 | [diff] [blame] | 62 | .WillOnce(Return(true)); |
| 63 | EXPECT_CALL(*options_, namespace_vfs()) |
Will Drewry | 4dd0e66 | 2010-01-19 14:43:50 -0800 | [diff] [blame] | 64 | .WillOnce(Return(true)); |
| 65 | EXPECT_CALL(*options_, set_namespace_vfs(_)) // Proof of correction |
| 66 | .Times(0); |
| 67 | EXPECT_TRUE(options_->FixUpDependencies()); |
| 68 | } |
| 69 | |
| 70 | TEST_F(OptionsDepsTest, NoMounts) { |
Chris Masone | 4463bea | 2011-04-27 08:22:40 -0700 | [diff] [blame] | 71 | EXPECT_CALL(*(options_.get()), FixUpDependencies()) |
| 72 | .Times(1); |
| 73 | EXPECT_CALL(*options_, enforce_syscalls_benchmark()) |
| 74 | .WillOnce(Return(false)); |
Will Drewry | 4dd0e66 | 2010-01-19 14:43:50 -0800 | [diff] [blame] | 75 | // Setup case which should be untouched |
| 76 | EXPECT_CALL(*options_, add_readonly_mounts()) |
Will Drewry | 4dd0e66 | 2010-01-19 14:43:50 -0800 | [diff] [blame] | 77 | .WillOnce(Return(false)); |
| 78 | // VFS check should never be run since the conditional short circuits |
| 79 | EXPECT_CALL(*options_, namespace_vfs()) |
| 80 | .Times(0); |
| 81 | EXPECT_CALL(*options_, set_namespace_vfs(_)) |
| 82 | .Times(0); |
| 83 | EXPECT_TRUE(options_->FixUpDependencies()); |
| 84 | } |
| 85 | |
| 86 | TEST_F(OptionsDepsTest, BothSyscallEnforcements) { |
Chris Masone | 4463bea | 2011-04-27 08:22:40 -0700 | [diff] [blame] | 87 | EXPECT_CALL(*(options_.get()), FixUpDependencies()) |
| 88 | .Times(1); |
Will Drewry | 4dd0e66 | 2010-01-19 14:43:50 -0800 | [diff] [blame] | 89 | // Case which fails |
Chris Masone | 4463bea | 2011-04-27 08:22:40 -0700 | [diff] [blame] | 90 | EXPECT_CALL(*options_, add_readonly_mounts()) |
| 91 | .WillOnce(Return(false)); |
Will Drewry | 4dd0e66 | 2010-01-19 14:43:50 -0800 | [diff] [blame] | 92 | EXPECT_CALL(*options_, enforce_syscalls_benchmark()) |
Will Drewry | 4dd0e66 | 2010-01-19 14:43:50 -0800 | [diff] [blame] | 93 | .WillOnce(Return(true)); |
| 94 | EXPECT_CALL(*options_, enforce_syscalls_by_source()) |
Will Drewry | 4dd0e66 | 2010-01-19 14:43:50 -0800 | [diff] [blame] | 95 | .WillOnce(Return(true)); |
| 96 | EXPECT_FALSE(options_->FixUpDependencies()); |
| 97 | } |
| 98 | |
| 99 | TEST_F(OptionsDepsTest, SyscallBenchmarkOnly) { |
Chris Masone | 4463bea | 2011-04-27 08:22:40 -0700 | [diff] [blame] | 100 | EXPECT_CALL(*(options_.get()), FixUpDependencies()) |
| 101 | .Times(1); |
| 102 | EXPECT_CALL(*options_, add_readonly_mounts()) |
| 103 | .WillOnce(Return(false)); |
Will Drewry | 4dd0e66 | 2010-01-19 14:43:50 -0800 | [diff] [blame] | 104 | EXPECT_CALL(*options_, enforce_syscalls_benchmark()) |
Will Drewry | 4dd0e66 | 2010-01-19 14:43:50 -0800 | [diff] [blame] | 105 | .WillOnce(Return(true)); |
| 106 | EXPECT_CALL(*options_, enforce_syscalls_by_source()) |
Will Drewry | 4dd0e66 | 2010-01-19 14:43:50 -0800 | [diff] [blame] | 107 | .WillOnce(Return(false)); |
| 108 | EXPECT_TRUE(options_->FixUpDependencies()); |
| 109 | } |
| 110 | |
| 111 | TEST_F(OptionsDepsTest, SyscallNoBenchmark) { |
Chris Masone | 4463bea | 2011-04-27 08:22:40 -0700 | [diff] [blame] | 112 | EXPECT_CALL(*(options_.get()), FixUpDependencies()) |
| 113 | .Times(1); |
| 114 | EXPECT_CALL(*options_, add_readonly_mounts()) |
| 115 | .WillOnce(Return(false)); |
Will Drewry | 4dd0e66 | 2010-01-19 14:43:50 -0800 | [diff] [blame] | 116 | EXPECT_CALL(*options_, enforce_syscalls_benchmark()) |
Will Drewry | 4dd0e66 | 2010-01-19 14:43:50 -0800 | [diff] [blame] | 117 | .WillOnce(Return(false)); |
| 118 | EXPECT_CALL(*options_, enforce_syscalls_by_source()) |
| 119 | .Times(0); |
| 120 | EXPECT_TRUE(options_->FixUpDependencies()); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | } // namespace chromeos |