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

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

Issue 938223004: Linux sandbox: better APIs with /proc/ arguments (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix invalid proc_fd_ usage. Created 5 years, 10 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
« no previous file with comments | « sandbox/linux/services/proc_util.cc ('k') | sandbox/linux/services/thread_helpers.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "sandbox/linux/services/proc_util.h" 5 #include "sandbox/linux/services/proc_util.h"
6 6
7 #include <fcntl.h> 7 #include <fcntl.h>
8 #include <unistd.h> 8 #include <unistd.h>
9 9
10 #include "base/files/scoped_file.h" 10 #include "base/files/scoped_file.h"
11 #include "base/posix/eintr_wrapper.h" 11 #include "base/posix/eintr_wrapper.h"
12 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 13
14 namespace sandbox { 14 namespace sandbox {
15 15
16 TEST(ProcUtil, CountOpenFds) { 16 TEST(ProcUtil, CountOpenFds) {
17 base::ScopedFD proc_fd(open("/proc", O_RDONLY | O_DIRECTORY)); 17 base::ScopedFD proc_fd(open("/proc/", O_RDONLY | O_DIRECTORY));
18 ASSERT_TRUE(proc_fd.is_valid()); 18 ASSERT_TRUE(proc_fd.is_valid());
19 int fd_count = ProcUtil::CountOpenFds(proc_fd.get()); 19 int fd_count = ProcUtil::CountOpenFds(proc_fd.get());
20 int fd = open("/dev/null", O_RDONLY); 20 int fd = open("/dev/null", O_RDONLY);
21 ASSERT_LE(0, fd); 21 ASSERT_LE(0, fd);
22 EXPECT_EQ(fd_count + 1, ProcUtil::CountOpenFds(proc_fd.get())); 22 EXPECT_EQ(fd_count + 1, ProcUtil::CountOpenFds(proc_fd.get()));
23 ASSERT_EQ(0, IGNORE_EINTR(close(fd))); 23 ASSERT_EQ(0, IGNORE_EINTR(close(fd)));
24 EXPECT_EQ(fd_count, ProcUtil::CountOpenFds(proc_fd.get())); 24 EXPECT_EQ(fd_count, ProcUtil::CountOpenFds(proc_fd.get()));
25 } 25 }
26 26
27 TEST(ProcUtil, HasOpenDirectory) { 27 TEST(ProcUtil, HasOpenDirectory) {
28 // No open directory should exist at startup. 28 // No open directory should exist at startup.
29 EXPECT_FALSE(ProcUtil::HasOpenDirectory(-1)); 29 EXPECT_FALSE(ProcUtil::HasOpenDirectory());
30 { 30 {
31 // Have a "/proc" file descriptor around. 31 // Have a "/proc" file descriptor around.
32 int proc_fd = open("/proc", O_RDONLY | O_DIRECTORY); 32 int proc_fd = open("/proc/", O_RDONLY | O_DIRECTORY);
33 base::ScopedFD proc_fd_closer(proc_fd); 33 base::ScopedFD proc_fd_closer(proc_fd);
34 EXPECT_TRUE(ProcUtil::HasOpenDirectory(-1)); 34 EXPECT_TRUE(ProcUtil::HasOpenDirectory());
35 } 35 }
36 EXPECT_FALSE(ProcUtil::HasOpenDirectory(-1)); 36 EXPECT_FALSE(ProcUtil::HasOpenDirectory());
37 } 37 }
38 38
39 TEST(ProcUtil, HasOpenDirectoryWithFD) { 39 TEST(ProcUtil, HasOpenDirectoryWithFD) {
40 int proc_fd = open("/proc", O_RDONLY | O_DIRECTORY); 40 int proc_fd = open("/proc/", O_RDONLY | O_DIRECTORY);
41 base::ScopedFD proc_fd_closer(proc_fd); 41 base::ScopedFD proc_fd_closer(proc_fd);
42 ASSERT_LE(0, proc_fd); 42 ASSERT_LE(0, proc_fd);
43 43
44 // Don't pass |proc_fd|, an open directory (proc_fd) should 44 // Don't pass |proc_fd|, an open directory (proc_fd) should
45 // be detected. 45 // be detected.
46 EXPECT_TRUE(ProcUtil::HasOpenDirectory(-1)); 46 EXPECT_TRUE(ProcUtil::HasOpenDirectory());
47 // Pass |proc_fd| and no open directory should be detected. 47 // Pass |proc_fd| and no open directory should be detected.
48 EXPECT_FALSE(ProcUtil::HasOpenDirectory(proc_fd)); 48 EXPECT_FALSE(ProcUtil::HasOpenDirectory(proc_fd));
49 49
50 { 50 {
51 // Have a directory file descriptor around. 51 // Have a directory file descriptor around.
52 int open_directory_fd = open("/proc/self", O_RDONLY | O_DIRECTORY); 52 int open_directory_fd = open("/proc/self/", O_RDONLY | O_DIRECTORY);
53 base::ScopedFD open_directory_fd_closer(open_directory_fd); 53 base::ScopedFD open_directory_fd_closer(open_directory_fd);
54 EXPECT_TRUE(ProcUtil::HasOpenDirectory(proc_fd)); 54 EXPECT_TRUE(ProcUtil::HasOpenDirectory(proc_fd));
55 } 55 }
56 56
57 // The "/proc/self" file descriptor should now be closed, |proc_fd| is the 57 // The "/proc/" file descriptor should now be closed, |proc_fd| is the
58 // only directory file descriptor open. 58 // only directory file descriptor open.
59 EXPECT_FALSE(ProcUtil::HasOpenDirectory(proc_fd)); 59 EXPECT_FALSE(ProcUtil::HasOpenDirectory(proc_fd));
60 } 60 }
61 61
62 } // namespace sandbox 62 } // namespace sandbox
OLDNEW
« no previous file with comments | « sandbox/linux/services/proc_util.cc ('k') | sandbox/linux/services/thread_helpers.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698