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