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

Side by Side Diff: sandbox/linux/services/namespace_utils_unittest.cc

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
Patch Set: Respond to comments Created 5 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
jln (very slow on Chromium) 2015/01/27 00:06:44 New file, so 2015 and no '(c)'
rickyz (no longer on Chrome) 2015/01/27 01:08:04 Done.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "sandbox/linux/services/namespace_utils.h"
6
7 #include <errno.h>
8 #include <sched.h>
9 #include <sys/types.h>
10 #include <sys/wait.h>
11
12 #include "base/posix/eintr_wrapper.h"
13 #include "base/process/launch.h"
14 #include "sandbox/linux/services/credentials.h"
15 #include "sandbox/linux/tests/unit_tests.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace sandbox {
19
20 namespace {
21
22 SANDBOX_TEST(NamespaceUtils, KernelSupportsUnprivilegedNamespace) {
23 const bool can_create_user_ns = Credentials::CanCreateProcessInNewUserNS();
24 const bool supports_user_ns =
25 NamespaceUtils::KernelSupportsUnprivilegedNamespace(CLONE_NEWUSER);
26 // can_create_user_ns implies supports_user_ns, but the converse is not
27 // necessarily true, as creating a user namespace can fail for various
28 // reasons.
29 if (can_create_user_ns) {
30 EXPECT_TRUE(supports_user_ns);
jln (very slow on Chromium) 2015/01/27 00:06:44 SANDBOX_ASSERT in this SANDBOX_TEST.
rickyz (no longer on Chrome) 2015/01/27 01:08:03 Done.
31 }
32 }
33
34 SANDBOX_TEST(NamespaceUtils, WriteToIdMapFile) {
35 if (!Credentials::CanCreateProcessInNewUserNS()) {
36 return;
37 }
38
39 pid_t pid = base::ForkWithFlags(CLONE_NEWUSER, nullptr, nullptr);
40 ASSERT_NE(-1, pid);
41
42 uid_t uid = getuid();
jln (very slow on Chromium) 2015/01/27 00:06:44 const
rickyz (no longer on Chrome) 2015/01/27 01:08:03 Done.
43 gid_t gid = getgid();
jln (very slow on Chromium) 2015/01/27 00:06:44 const
rickyz (no longer on Chrome) 2015/01/27 01:08:04 Done.
44 if (pid == 0) {
45 EXPECT_NE(uid, getuid());
jln (very slow on Chromium) 2015/01/27 00:06:44 You're in a child, it needs to _exit() and not exi
rickyz (no longer on Chrome) 2015/01/27 01:08:03 Gah, I keep forgetting this, sorry
46 NamespaceUtils::WriteToIdMapFile("/proc/self/uid_map", uid);
47 EXPECT_EQ(uid, getuid());
48
49 EXPECT_NE(gid, getgid());
50 NamespaceUtils::WriteToIdMapFile("/proc/self/gid_map", gid);
51 EXPECT_EQ(gid, getgid());
52
53 _exit(0);
54 }
55
56 int status;
57 EXPECT_EQ(pid, HANDLE_EINTR(waitpid(pid, &status, 0)));
jln (very slow on Chromium) 2015/01/27 00:06:44 This is a SANDBOX_TEST so use SANDBOX_ASSERT_EQ()
rickyz (no longer on Chrome) 2015/01/27 01:08:03 Nice catch with the asserts - using the right asse
58 EXPECT_EQ(0, status);
59 }
60
61 } // namespace.
62
63 } // namespace sandbox.
OLDNEW
« sandbox/linux/services/namespace_utils.cc ('K') | « sandbox/linux/services/namespace_utils.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698