Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(150)

Unified Diff: src/platform/minijail/minijail_unittest.cc

Issue 542124: Update minijail tests with real mocks and packaging testing deps (Closed)
Patch Set: integrate notes from cmasone Created 10 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/platform/minijail/minijail_testrunner.cc ('k') | src/platform/minijail/mock_env.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/platform/minijail/minijail_unittest.cc
diff --git a/src/platform/minijail/minijail_unittest.cc b/src/platform/minijail/minijail_unittest.cc
index 70c712e9e35364b05f4535a623f1d1185598419c..bd7d004b203cd56923171d49331f436b8c90599a 100644
--- a/src/platform/minijail/minijail_unittest.cc
+++ b/src/platform/minijail/minijail_unittest.cc
@@ -1,47 +1,126 @@
-// Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
+// Copyright (c) 2009-2010 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Some portions Copyright (c) 2009 The Chromium Authors.
//
// Tests for MiniJail
-#include "env.h"
+#include "mock_env.h"
+#include "mock_options.h"
#include "minijail.h"
+#include <gmock/gmock.h>
#include <gtest/gtest.h>
namespace chromeos {
-// TODO: pull in gmock to make this non-crazy for testing.
-class TrueEnv : public minijail::Env {
+using ::testing::_; // wildcard mock matcher
+using ::testing::AtLeast; // Times modifier
+using ::testing::DefaultValue; // allow for easy default return value change
+using ::testing::Return; // mock Return action
+
+class MiniJailTest : public ::testing::Test {
public:
- TrueEnv() { }
- ~TrueEnv() { }
- bool DisableTracing() const { return true; }
- bool KeepRootCapabilities() const { return true; }
- bool DisableDefaultRootPrivileges() const { return true; }
- bool ChangeUser(uid_t uid, gid_t gid) const { return true; }
- bool SanitizeBoundingSet(uint64 capmask) const { return true; }
- bool EnterNamespace(int namespaces) const { return true; }
- bool FilterSyscallsBySource() const { return true; }
- bool Mount() const { return true; }
- bool SanitizeCapabilities(uint64 eff_capmask) const { return true; }
- bool Run(const char *path,
- char * const *argv,
- char * const *envp) const { return true; }
+ static const char kDummyPath[];
+ void SetUp() {
+ env_.reset(new minijail::MockEnv);
+ options_.reset(new minijail::MockOptions);
+ // Setup options to return the mock env
+ EXPECT_CALL(*options_, env())
+ .Times(AtLeast(1))
+ .WillRepeatedly(Return(env_.get()));
+ }
+ void TearDown() {
+ }
+ protected:
+ scoped_ptr<minijail::MockEnv> env_;
+ scoped_ptr<minijail::MockOptions> options_;
};
-class MiniJailTest : public ::testing::Test { };
+const char MiniJailTest::kDummyPath[] = "/path/to/target/binary";
+
+TEST_F(MiniJailTest, RunGetsPath) {
+ MiniJail jail;
+ jail.Initialize(options_.get());
+
+ // This will be a relative no-op since all the options are defaulting
+ // to false.
+ EXPECT_TRUE(jail.Jail());
+ // Ensure the pre-configured dummy path is propagated via Run().
+ EXPECT_CALL(*env_, Run(kDummyPath, NULL, NULL))
+ .Times(1)
+ .WillOnce(Return(true));
+ // Setup executable_path to return a dummy
+ EXPECT_CALL(*options_, executable_path())
+ .Times(2)
+ .WillRepeatedly(Return(kDummyPath));
+ EXPECT_TRUE(jail.Run());
+}
-TEST(MiniJailTest, TrueJail) {
- TrueEnv *env = new TrueEnv;
- MiniJailOptions options;
- options.set_env(env); // takes ownership
- options.set_executable_path("/no/where");
+TEST_F(MiniJailTest, DefaultTrueEnvAndOptions) {
+ // Make all default mock calls return true
+ DefaultValue<bool>::Set(true);
MiniJail jail;
- jail.Initialize(&options);
- // This does basically nothing since the options default to false.
- // Only ChangeUser is actually called.
+ jail.Initialize(options_.get());
EXPECT_TRUE(jail.Jail());
+ // Setup executable_path to return a dummy
+ EXPECT_CALL(*options_, executable_path())
+ .Times(2)
+ .WillRepeatedly(Return(kDummyPath));
EXPECT_TRUE(jail.Run());
+ DefaultValue<bool>::Clear();
+}
+
+TEST_F(MiniJailTest, NamespaceFlagsPidOnly) {
+ MiniJail jail;
+ jail.Initialize(options_.get());
+
+ EXPECT_CALL(*options_, namespace_pid())
+ .Times(1)
+ .WillOnce(Return(true));
+ EXPECT_CALL(*options_, namespace_vfs())
+ .Times(2)
+ .WillOnce(Return(false))
+ .WillOnce(Return(false));
+ EXPECT_CALL(*env_, EnterNamespace(CLONE_NEWPID))
+ .Times(1)
+ .WillOnce(Return(true));
+ EXPECT_TRUE(jail.Jail());
}
+TEST_F(MiniJailTest, NamespaceFlagsVfsOnly) {
+ MiniJail jail;
+ jail.Initialize(options_.get());
+
+ EXPECT_CALL(*options_, namespace_pid())
+ .Times(1)
+ .WillOnce(Return(false));
+ EXPECT_CALL(*options_, namespace_vfs())
+ .Times(2)
+ .WillOnce(Return(true))
+ .WillOnce(Return(true));
+ EXPECT_CALL(*env_, EnterNamespace(CLONE_NEWNS))
+ .Times(1)
+ .WillOnce(Return(true));
+ EXPECT_TRUE(jail.Jail());
+}
+
+TEST_F(MiniJailTest, NamespaceFlagsAll) {
+ MiniJail jail;
+ jail.Initialize(options_.get());
+
+ EXPECT_CALL(*options_, namespace_pid())
+ .Times(1)
+ .WillOnce(Return(true));
+ EXPECT_CALL(*options_, namespace_vfs())
+ .Times(2)
+ .WillOnce(Return(true))
+ .WillOnce(Return(true));
+ EXPECT_CALL(*env_, EnterNamespace(CLONE_NEWNS|CLONE_NEWPID))
+ .Times(1)
+ .WillOnce(Return(true));
+ EXPECT_TRUE(jail.Jail()); // all works on first call
+}
+
+// TODO(wad) finish up test cases for each conditional
+
+
} // namespace chromeos
« no previous file with comments | « src/platform/minijail/minijail_testrunner.cc ('k') | src/platform/minijail/mock_env.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698