blob: 54ded5e8d643070d6319b279b2196fcf5469a08a [file] [log] [blame]
Chris Masone4463bea2011-04-27 08:22:40 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Will Drewry4dd0e662010-01-19 14:43:50 -08002// 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 Masone4463bea2011-04-27 08:22:40 -07006#include "options.h"
7
Will Drewry4dd0e662010-01-19 14:43:50 -08008#include "mock_env.h"
9#include "mock_options.h"
10#include <gmock/gmock.h>
11#include <gtest/gtest.h>
12
13namespace chromeos {
14
15using ::testing::_; // wildcard mock matcher
16using ::testing::AtLeast; // Times modifier
17using ::testing::Invoke; // Allow concrete call redirection
18using ::testing::DefaultValue; // allow for easy default return value change
19using ::testing::Return; // mock Return action
20
21class 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
34TEST_F(OptionsDepsTest, NothingToCorrect) {
35 // Since all options default to false, this should just work.
Chris Masone4463bea2011-04-27 08:22:40 -070036 minijail::Options options;
37 EXPECT_TRUE(options.FixUpDependencies());
Will Drewry4dd0e662010-01-19 14:43:50 -080038}
39
40TEST_F(OptionsDepsTest, MountsWithoutVfs) {
Chris Masone4463bea2011-04-27 08:22:40 -070041 EXPECT_CALL(*(options_.get()), FixUpDependencies())
42 .Times(1);
43 EXPECT_CALL(*options_, enforce_syscalls_benchmark())
44 .WillOnce(Return(false));
Will Drewry4dd0e662010-01-19 14:43:50 -080045 // Set up the case in need of correction
46 EXPECT_CALL(*options_, add_readonly_mounts())
Will Drewry4dd0e662010-01-19 14:43:50 -080047 .WillOnce(Return(true));
48 EXPECT_CALL(*options_, namespace_vfs())
Will Drewry4dd0e662010-01-19 14:43:50 -080049 .WillOnce(Return(false));
50 EXPECT_CALL(*options_, set_namespace_vfs(true)) // Proof of correction
51 .Times(1);
52 EXPECT_TRUE(options_->FixUpDependencies());
53}
54
55TEST_F(OptionsDepsTest, MountsWithVfs) {
Chris Masone4463bea2011-04-27 08:22:40 -070056 EXPECT_CALL(*(options_.get()), FixUpDependencies())
57 .Times(1);
58 EXPECT_CALL(*options_, enforce_syscalls_benchmark())
59 .WillOnce(Return(false));
Will Drewry4dd0e662010-01-19 14:43:50 -080060 // Setup case which should be untouched
61 EXPECT_CALL(*options_, add_readonly_mounts())
Will Drewry4dd0e662010-01-19 14:43:50 -080062 .WillOnce(Return(true));
63 EXPECT_CALL(*options_, namespace_vfs())
Will Drewry4dd0e662010-01-19 14:43:50 -080064 .WillOnce(Return(true));
65 EXPECT_CALL(*options_, set_namespace_vfs(_)) // Proof of correction
66 .Times(0);
67 EXPECT_TRUE(options_->FixUpDependencies());
68}
69
70TEST_F(OptionsDepsTest, NoMounts) {
Chris Masone4463bea2011-04-27 08:22:40 -070071 EXPECT_CALL(*(options_.get()), FixUpDependencies())
72 .Times(1);
73 EXPECT_CALL(*options_, enforce_syscalls_benchmark())
74 .WillOnce(Return(false));
Will Drewry4dd0e662010-01-19 14:43:50 -080075 // Setup case which should be untouched
76 EXPECT_CALL(*options_, add_readonly_mounts())
Will Drewry4dd0e662010-01-19 14:43:50 -080077 .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
86TEST_F(OptionsDepsTest, BothSyscallEnforcements) {
Chris Masone4463bea2011-04-27 08:22:40 -070087 EXPECT_CALL(*(options_.get()), FixUpDependencies())
88 .Times(1);
Will Drewry4dd0e662010-01-19 14:43:50 -080089 // Case which fails
Chris Masone4463bea2011-04-27 08:22:40 -070090 EXPECT_CALL(*options_, add_readonly_mounts())
91 .WillOnce(Return(false));
Will Drewry4dd0e662010-01-19 14:43:50 -080092 EXPECT_CALL(*options_, enforce_syscalls_benchmark())
Will Drewry4dd0e662010-01-19 14:43:50 -080093 .WillOnce(Return(true));
94 EXPECT_CALL(*options_, enforce_syscalls_by_source())
Will Drewry4dd0e662010-01-19 14:43:50 -080095 .WillOnce(Return(true));
96 EXPECT_FALSE(options_->FixUpDependencies());
97}
98
99TEST_F(OptionsDepsTest, SyscallBenchmarkOnly) {
Chris Masone4463bea2011-04-27 08:22:40 -0700100 EXPECT_CALL(*(options_.get()), FixUpDependencies())
101 .Times(1);
102 EXPECT_CALL(*options_, add_readonly_mounts())
103 .WillOnce(Return(false));
Will Drewry4dd0e662010-01-19 14:43:50 -0800104 EXPECT_CALL(*options_, enforce_syscalls_benchmark())
Will Drewry4dd0e662010-01-19 14:43:50 -0800105 .WillOnce(Return(true));
106 EXPECT_CALL(*options_, enforce_syscalls_by_source())
Will Drewry4dd0e662010-01-19 14:43:50 -0800107 .WillOnce(Return(false));
108 EXPECT_TRUE(options_->FixUpDependencies());
109}
110
111TEST_F(OptionsDepsTest, SyscallNoBenchmark) {
Chris Masone4463bea2011-04-27 08:22:40 -0700112 EXPECT_CALL(*(options_.get()), FixUpDependencies())
113 .Times(1);
114 EXPECT_CALL(*options_, add_readonly_mounts())
115 .WillOnce(Return(false));
Will Drewry4dd0e662010-01-19 14:43:50 -0800116 EXPECT_CALL(*options_, enforce_syscalls_benchmark())
Will Drewry4dd0e662010-01-19 14:43:50 -0800117 .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