OLD | NEW |
---|---|
(Empty) | |
1 #include "sandbox/linux/services/broker_process.h" | |
2 | |
3 #include <errno.h> | |
4 #include <fcntl.h> | |
5 #include <sys/stat.h> | |
6 #include <sys/types.h> | |
7 #include <sys/wait.h> | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/logging.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace sandbox { | |
15 | |
16 TEST(BrokerProcess, CreateAndDestroy) { | |
17 std::vector<std::string> file_whitelist; | |
18 file_whitelist.push_back("/proc/cpuinfo"); | |
19 | |
20 BrokerProcess* open_broker = new BrokerProcess(file_whitelist); | |
21 EXPECT_TRUE(open_broker->Init(NULL)); | |
22 pid_t broker_pid = open_broker->broker_pid(); | |
23 delete(open_broker); | |
24 | |
25 // Now we check that the broker has exited properly. | |
26 int status = 0; | |
27 EXPECT_EQ(waitpid(broker_pid, &status, 0), broker_pid); | |
28 EXPECT_TRUE(WIFEXITED(status)); | |
29 EXPECT_EQ(WEXITSTATUS(status), 0); | |
30 } | |
31 | |
32 void TestOpenFile(bool fast_check_in_client) { | |
33 std::vector<std::string> file_whitelist; | |
34 const char kFileCpuInfo[] = "/proc/cpuinfo"; | |
35 const char kDoesNotExistWhitelisted[] = "/proc/DOESNOTEXIST"; | |
36 const char kDoesNotExist2[] = "/proc/DOESNOTEXIST2"; | |
37 file_whitelist.push_back(kFileCpuInfo); | |
38 file_whitelist.push_back(kDoesNotExistWhitelisted); | |
39 | |
40 BrokerProcess* open_broker = new BrokerProcess(file_whitelist, | |
41 fast_check_in_client); | |
42 EXPECT_TRUE(open_broker->Init(NULL)); | |
43 pid_t broker_pid = open_broker->broker_pid(); | |
44 | |
45 int fd = -1; | |
46 // This file is not whitelisted. | |
47 fd = open_broker->Open(kDoesNotExist2, O_RDONLY); | |
48 EXPECT_EQ(fd, -EPERM); | |
49 // This file is whitelisted. | |
50 fd = open_broker->Open(kDoesNotExistWhitelisted, O_RDONLY); | |
51 EXPECT_EQ(fd, -ENOENT); | |
52 fd = open_broker->Open(kFileCpuInfo, O_RDWR); | |
53 EXPECT_EQ(fd, -EPERM); | |
54 | |
55 // Open cpuinfo via the broker. | |
56 int cpuinfo_fd = open_broker->Open(kFileCpuInfo, O_RDONLY); | |
57 ASSERT_GE(cpuinfo_fd, 0); | |
58 char buf[3]; | |
59 memset(buf, 0, sizeof(buf)); | |
60 int read_len1 = read(cpuinfo_fd, buf, sizeof(buf)); | |
61 EXPECT_GT(read_len1, 0); | |
62 | |
63 // Open cpuinfo directly. | |
64 int cpuinfo_fd2 = open(kFileCpuInfo, O_RDONLY); | |
65 ASSERT_GE(cpuinfo_fd2, 0); | |
66 char buf2[3]; | |
67 memset(buf2, 1, sizeof(buf2)); | |
68 int read_len2 = read(cpuinfo_fd2, buf2, sizeof(buf2)); | |
69 EXPECT_GT(read_len1, 0); | |
70 | |
71 // The following is not guaranteed true, but will be in practice. | |
72 EXPECT_EQ(read_len1, read_len2); | |
73 // Compare the broker's cpuinfo fd with the real one. | |
Jorge Lucangeli Obes
2012/12/13 02:14:54
This comment is confusing as you are comparing the
jln (very slow on Chromium)
2012/12/13 02:33:14
Done.
| |
74 EXPECT_EQ(memcmp(buf, buf2, read_len1), 0); | |
75 | |
76 if (fd >= 0) | |
77 close(fd); | |
78 if (cpuinfo_fd >= 0) | |
79 close(cpuinfo_fd); | |
80 if (cpuinfo_fd2 >= 0) | |
81 close(cpuinfo_fd); | |
82 | |
83 delete(open_broker); | |
84 | |
85 // Now we check that the broker has exited properly. | |
86 int status = 0; | |
87 EXPECT_EQ(waitpid(broker_pid, &status, 0), broker_pid); | |
88 EXPECT_TRUE(WIFEXITED(status)); | |
89 EXPECT_EQ(WEXITSTATUS(status), 0); | |
90 } | |
91 | |
92 // Run the same thing twice. The second time, we make sure that no security | |
93 // check is performed on the client. | |
94 TEST(BrokerProcess, OpenFileWithClientCheck) { | |
95 TestOpenFile(true /* fast_check_in_client */); | |
96 } | |
97 | |
98 TEST(BrokerProcess, OpenFileNoClientCheck) { | |
99 TestOpenFile(false /* fast_check_in_client */); | |
100 } | |
101 | |
102 } // namespace sandbox | |
OLD | NEW |