OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
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 "chrome/test/nacl_security_tests/commands_posix.h" | |
6 | |
7 #include <errno.h> | |
8 #include <fcntl.h> | |
9 #include <netdb.h> | |
10 #include <stdio.h> | |
11 #include <string.h> | |
12 #include <sys/socket.h> | |
13 #include <sys/types.h> | |
14 #include <sys/wait.h> | |
15 #include <unistd.h> | |
16 | |
17 #include <string> | |
18 | |
19 // Sandbox access tests (mimic'ing "sandbox/tests/validation_tests/commands.h") | |
20 | |
21 namespace sandbox { | |
22 | |
23 // Permissions for the user to read & write and others to read. | |
24 const mode_t kCreatePermissions = 0644; | |
25 | |
26 SboxTestResult TestOpenReadFile(const char *path) { | |
27 int fd = open(path, O_RDONLY | O_CREAT, kCreatePermissions); | |
28 if (-1 == fd) { | |
29 return SBOX_TEST_DENIED; | |
30 } else { | |
31 close(fd); | |
32 return SBOX_TEST_SUCCEEDED; | |
33 } | |
34 } | |
35 | |
36 SboxTestResult TestOpenWriteFile(const char *path) { | |
37 int fd = open(path, O_WRONLY | O_CREAT, kCreatePermissions); | |
38 if (-1 == fd) { | |
39 return SBOX_TEST_DENIED; | |
40 } else { | |
41 close(fd); | |
42 return SBOX_TEST_SUCCEEDED; | |
43 } | |
44 } | |
45 | |
46 SboxTestResult TestCreateProcess(const char *path) { | |
47 pid_t pid; | |
48 int exec_res; | |
49 | |
50 pid = fork(); | |
51 if (0 == pid) { | |
52 exec_res = execl(path, path, NULL); | |
53 if (exec_res) { | |
54 return SBOX_TEST_DENIED; | |
55 } else { | |
56 return SBOX_TEST_SUCCEEDED; | |
57 } | |
58 return SBOX_TEST_SUCCEEDED; | |
59 } else if (0 < pid) { | |
60 pid_t w_pid; | |
61 do { | |
62 w_pid = waitpid(pid, NULL, WNOHANG); | |
63 } while (w_pid != -1 && errno != EINTR); | |
64 return SBOX_TEST_SUCCEEDED; | |
65 } else { | |
66 return SBOX_TEST_DENIED; | |
67 } | |
68 } | |
69 | |
70 SboxTestResult TestConnect(const char *url) { | |
71 int conn_sock; | |
72 struct addrinfo hints, *servinfo, *p; | |
73 int rv; | |
74 | |
75 memset(&hints, 0, sizeof(hints)); | |
76 hints.ai_family = AF_UNSPEC; | |
77 hints.ai_socktype = SOCK_STREAM; | |
78 rv = getaddrinfo(url, "http", &hints, &servinfo); | |
79 if (0 != rv) { | |
80 return SBOX_TEST_DENIED; | |
81 } | |
82 | |
83 p = servinfo; | |
84 // Just try the first entry. | |
85 conn_sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol); | |
86 if (-1 == conn_sock) { | |
87 perror("socket"); | |
88 freeaddrinfo(servinfo); | |
89 return SBOX_TEST_DENIED; | |
90 } | |
91 | |
92 if (-1 == connect(conn_sock, p->ai_addr, p->ai_addrlen)) { | |
93 close(conn_sock); | |
94 freeaddrinfo(servinfo); | |
95 return SBOX_TEST_DENIED; | |
96 } | |
97 | |
98 shutdown(conn_sock, SHUT_RDWR); | |
99 close(conn_sock); | |
100 freeaddrinfo(servinfo); | |
101 return SBOX_TEST_SUCCEEDED; | |
102 } | |
103 | |
104 // TODO(jvoung): test more: e.g., bind and accept. | |
105 // chmod, unlink, symlink, ... if guaranteed a test file that would normally | |
106 // allow us to do such things (i.e., we want the test operations to be | |
107 // context-independent, yet leave no traces). | |
108 | |
109 SboxTestResult TestDummyFails() { | |
110 return SBOX_TEST_SUCCEEDED; | |
111 } | |
112 | |
113 } // namespace sandbox | |
OLD | NEW |