Index: sandbox/linux/services/namespace_utils_unittest.cc |
diff --git a/sandbox/linux/services/namespace_utils_unittest.cc b/sandbox/linux/services/namespace_utils_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e153183540cb79d0176db94cf584939dc580dd6d |
--- /dev/null |
+++ b/sandbox/linux/services/namespace_utils_unittest.cc |
@@ -0,0 +1,70 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "sandbox/linux/services/namespace_utils.h" |
+ |
+#include <errno.h> |
+#include <sched.h> |
+#include <sys/types.h> |
+#include <sys/wait.h> |
+ |
+#include "base/posix/eintr_wrapper.h" |
+#include "base/process/process.h" |
+#include "sandbox/linux/tests/unit_tests.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace sandbox { |
+ |
+namespace { |
+ |
+SANDBOX_TEST(NamespaceUtils, SupportsLinuxNamespace) { |
+ bool supports_user_ns = NamespaceUtils::SupportsLinuxNamespace(CLONE_NEWUSER); |
+ errno = 0; |
+ pid_t pid = base::ForkWithFlags(CLONE_NEWUSER, nullptr, nullptr); |
+ if (pid == 0) { |
+ _exit(0); |
+ } |
+ |
+ bool fork_succeeded = pid > 0; |
+ if (fork_succeeded) { |
+ int status; |
+ EXPECT_EQ(pid, HANDLE_EINTR(waitpid(pid, &status, 0))); |
+ EXPECT_EQ(0, status); |
+ } else { |
+ EXPECT_EQ(EINTR, errno); |
+ } |
+ |
+ EXPECT_EQ(supports_user_ns, fork_succeeded); |
+} |
+ |
+SANDBOX_TEST(NamespaceUtils, WriteToIdMapFile) { |
+ if (!NamespaceUtils::SupportsLinuxNamespace(CLONE_NEWUSER)) { |
+ return; |
+ } |
+ |
+ pid_t pid = base::ForkWithFlags(CLONE_NEWUSER, nullptr, nullptr); |
+ ASSERT_NE(-1, pid); |
+ |
+ uid_t uid = getuid(); |
+ gid_t gid = getgid(); |
+ if (pid == 0) { |
+ EXPECT_NE(uid, getuid()); |
+ NamespaceUtils::WriteToIdMapFile("/proc/self/uid_map", uid); |
+ EXPECT_EQ(uid, getuid()); |
+ |
+ EXPECT_NE(gid, getgid()); |
+ NamespaceUtils::WriteToIdMapFile("/proc/self/gid_map", gid); |
+ EXPECT_EQ(gid, getgid()); |
+ |
+ _exit(0); |
+ } |
+ |
+ int status; |
+ EXPECT_EQ(pid, HANDLE_EINTR(waitpid(pid, &status, 0))); |
+ EXPECT_EQ(0, status); |
+} |
+ |
+} // namespace. |
+ |
+} // namespace sandbox. |