blob: bd7d004b203cd56923171d49331f436b8c90599a [file] [log] [blame]
Will Drewry4dd0e662010-01-19 14:43:50 -08001// Copyright (c) 2009-2010 The Chromium OS Authors. All rights reserved.
drewry@google.combd940e92009-12-07 19:13:27 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4// Some portions Copyright (c) 2009 The Chromium Authors.
5//
6// Tests for MiniJail
Will Drewry4dd0e662010-01-19 14:43:50 -08007#include "mock_env.h"
8#include "mock_options.h"
drewry@google.combd940e92009-12-07 19:13:27 +00009#include "minijail.h"
Will Drewry4dd0e662010-01-19 14:43:50 -080010#include <gmock/gmock.h>
drewry@google.combd940e92009-12-07 19:13:27 +000011#include <gtest/gtest.h>
12
13namespace chromeos {
14
Will Drewry4dd0e662010-01-19 14:43:50 -080015using ::testing::_; // wildcard mock matcher
16using ::testing::AtLeast; // Times modifier
17using ::testing::DefaultValue; // allow for easy default return value change
18using ::testing::Return; // mock Return action
19
20class MiniJailTest : public ::testing::Test {
drewry@google.combd940e92009-12-07 19:13:27 +000021 public:
Will Drewry4dd0e662010-01-19 14:43:50 -080022 static const char kDummyPath[];
23 void SetUp() {
24 env_.reset(new minijail::MockEnv);
25 options_.reset(new minijail::MockOptions);
26 // Setup options to return the mock env
27 EXPECT_CALL(*options_, env())
28 .Times(AtLeast(1))
29 .WillRepeatedly(Return(env_.get()));
30 }
31 void TearDown() {
32 }
33 protected:
34 scoped_ptr<minijail::MockEnv> env_;
35 scoped_ptr<minijail::MockOptions> options_;
drewry@google.combd940e92009-12-07 19:13:27 +000036};
37
Will Drewry4dd0e662010-01-19 14:43:50 -080038const char MiniJailTest::kDummyPath[] = "/path/to/target/binary";
drewry@google.combd940e92009-12-07 19:13:27 +000039
Will Drewry4dd0e662010-01-19 14:43:50 -080040TEST_F(MiniJailTest, RunGetsPath) {
drewry@google.combd940e92009-12-07 19:13:27 +000041 MiniJail jail;
Will Drewry4dd0e662010-01-19 14:43:50 -080042 jail.Initialize(options_.get());
43
44 // This will be a relative no-op since all the options are defaulting
45 // to false.
drewry@google.combd940e92009-12-07 19:13:27 +000046 EXPECT_TRUE(jail.Jail());
Will Drewry4dd0e662010-01-19 14:43:50 -080047 // Ensure the pre-configured dummy path is propagated via Run().
48 EXPECT_CALL(*env_, Run(kDummyPath, NULL, NULL))
49 .Times(1)
50 .WillOnce(Return(true));
51 // Setup executable_path to return a dummy
52 EXPECT_CALL(*options_, executable_path())
53 .Times(2)
54 .WillRepeatedly(Return(kDummyPath));
drewry@google.combd940e92009-12-07 19:13:27 +000055 EXPECT_TRUE(jail.Run());
56}
57
Will Drewry4dd0e662010-01-19 14:43:50 -080058TEST_F(MiniJailTest, DefaultTrueEnvAndOptions) {
59 // Make all default mock calls return true
60 DefaultValue<bool>::Set(true);
61 MiniJail jail;
62 jail.Initialize(options_.get());
63 EXPECT_TRUE(jail.Jail());
64 // Setup executable_path to return a dummy
65 EXPECT_CALL(*options_, executable_path())
66 .Times(2)
67 .WillRepeatedly(Return(kDummyPath));
68 EXPECT_TRUE(jail.Run());
69 DefaultValue<bool>::Clear();
70}
71
72TEST_F(MiniJailTest, NamespaceFlagsPidOnly) {
73 MiniJail jail;
74 jail.Initialize(options_.get());
75
76 EXPECT_CALL(*options_, namespace_pid())
77 .Times(1)
78 .WillOnce(Return(true));
79 EXPECT_CALL(*options_, namespace_vfs())
80 .Times(2)
81 .WillOnce(Return(false))
82 .WillOnce(Return(false));
83 EXPECT_CALL(*env_, EnterNamespace(CLONE_NEWPID))
84 .Times(1)
85 .WillOnce(Return(true));
86 EXPECT_TRUE(jail.Jail());
87}
88
89TEST_F(MiniJailTest, NamespaceFlagsVfsOnly) {
90 MiniJail jail;
91 jail.Initialize(options_.get());
92
93 EXPECT_CALL(*options_, namespace_pid())
94 .Times(1)
95 .WillOnce(Return(false));
96 EXPECT_CALL(*options_, namespace_vfs())
97 .Times(2)
98 .WillOnce(Return(true))
99 .WillOnce(Return(true));
100 EXPECT_CALL(*env_, EnterNamespace(CLONE_NEWNS))
101 .Times(1)
102 .WillOnce(Return(true));
103 EXPECT_TRUE(jail.Jail());
104}
105
106TEST_F(MiniJailTest, NamespaceFlagsAll) {
107 MiniJail jail;
108 jail.Initialize(options_.get());
109
110 EXPECT_CALL(*options_, namespace_pid())
111 .Times(1)
112 .WillOnce(Return(true));
113 EXPECT_CALL(*options_, namespace_vfs())
114 .Times(2)
115 .WillOnce(Return(true))
116 .WillOnce(Return(true));
117 EXPECT_CALL(*env_, EnterNamespace(CLONE_NEWNS|CLONE_NEWPID))
118 .Times(1)
119 .WillOnce(Return(true));
120 EXPECT_TRUE(jail.Jail()); // all works on first call
121}
122
123// TODO(wad) finish up test cases for each conditional
124
125
drewry@google.combd940e92009-12-07 19:13:27 +0000126} // namespace chromeos