 Chromium Code Reviews
 Chromium Code Reviews Issue 849893004:
  Move a couple of utility functions to a new namespace_utils class.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 849893004:
  Move a couple of utility functions to a new namespace_utils class.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| 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..a7d33700fe74842bf4233516e90137bb17ff3a1f | 
| --- /dev/null | 
| +++ b/sandbox/linux/services/namespace_utils_unittest.cc | 
| @@ -0,0 +1,71 @@ | 
| +// 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/launch.h" | 
| +#include "sandbox/linux/tests/unit_tests.h" | 
| +#include "testing/gtest/include/gtest/gtest.h" | 
| + | 
| +namespace sandbox { | 
| + | 
| +namespace { | 
| + | 
| +SANDBOX_TEST(NamespaceUtils, SupportsUnprivilegedNamespace) { | 
| + bool supports_user_ns = | 
| + NamespaceUtils::SupportsUnprivilegedNamespace(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); | 
| 
jln (very slow on Chromium)
2015/01/23 02:48:23
This will break from inside a chroot. fork_succeed
 
rickyz (no longer on Chrome)
2015/01/23 23:59:37
Done.
 | 
| +} | 
| + | 
| +SANDBOX_TEST(NamespaceUtils, WriteToIdMapFile) { | 
| + if (!NamespaceUtils::SupportsUnprivilegedNamespace(CLONE_NEWUSER)) { | 
| + return; | 
| + } | 
| + | 
| + pid_t pid = base::ForkWithFlags(CLONE_NEWUSER, nullptr, nullptr); | 
| + ASSERT_NE(-1, pid); | 
| 
jln (very slow on Chromium)
2015/01/23 02:48:23
Same remark, this will fail inside a chroot().
 
rickyz (no longer on Chrome)
2015/01/23 23:59:37
Done.
 | 
| + | 
| + 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. |