OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <errno.h> | 5 #include <errno.h> |
6 #include <fcntl.h> | 6 #include <fcntl.h> |
7 #include <string.h> | 7 #include <string.h> |
8 #include <sys/ioctl.h> | 8 #include <sys/ioctl.h> |
9 #include <sys/select.h> | 9 #include <sys/select.h> |
10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
11 #include <sys/time.h> | 11 #include <sys/time.h> |
12 #include <string> | 12 #include <string> |
13 | 13 |
14 #include "dev_fs_for_testing.h" | 14 #include "dev_fs_for_testing.h" |
15 #include "gtest/gtest.h" | 15 #include "gtest/gtest.h" |
16 #include "nacl_io/devfs/dev_fs.h" | 16 #include "nacl_io/devfs/dev_fs.h" |
17 #include "nacl_io/filesystem.h" | 17 #include "nacl_io/filesystem.h" |
18 #include "nacl_io/ioctl.h" | 18 #include "nacl_io/ioctl.h" |
19 #include "nacl_io/kernel_intercept.h" | 19 #include "nacl_io/kernel_intercept.h" |
20 #include "nacl_io/kernel_proxy.h" | 20 #include "nacl_io/kernel_proxy.h" |
21 #include "nacl_io/osdirent.h" | 21 #include "nacl_io/osdirent.h" |
22 | 22 |
23 using namespace nacl_io; | 23 using namespace nacl_io; |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
| 27 static int ki_ioctl_wrapper(int fd, int request, ...) { |
| 28 va_list ap; |
| 29 va_start(ap, request); |
| 30 int rtn = ki_ioctl(fd, request, ap); |
| 31 va_end(ap); |
| 32 return rtn; |
| 33 } |
| 34 |
27 class TtyNodeTest : public ::testing::Test { | 35 class TtyNodeTest : public ::testing::Test { |
28 public: | 36 public: |
29 TtyNodeTest() : fs_(&pepper_) {} | 37 TtyNodeTest() : fs_(&ppapi_) {} |
30 | 38 |
31 void SetUp() { | 39 void SetUp() { |
32 ASSERT_EQ(0, fs_.Access(Path("/tty"), R_OK | W_OK)); | 40 ASSERT_EQ(0, fs_.Access(Path("/tty"), R_OK | W_OK)); |
33 ASSERT_EQ(EACCES, fs_.Access(Path("/tty"), X_OK)); | 41 ASSERT_EQ(EACCES, fs_.Access(Path("/tty"), X_OK)); |
34 ASSERT_EQ(0, fs_.Open(Path("/tty"), O_RDWR, &dev_tty_)); | 42 ASSERT_EQ(0, fs_.Open(Path("/tty"), O_RDWR, &dev_tty_)); |
35 ASSERT_NE(NULL_NODE, dev_tty_.get()); | 43 ASSERT_NE(NULL_NODE, dev_tty_.get()); |
36 } | 44 } |
37 | 45 |
38 protected: | 46 protected: |
39 FakePepperInterface pepper_; | 47 FakePepperInterface ppapi_; |
40 DevFsForTesting fs_; | 48 DevFsForTesting fs_; |
41 ScopedNode dev_tty_; | 49 ScopedNode dev_tty_; |
42 }; | 50 }; |
43 | 51 |
44 class TtyTest : public ::testing::Test { | 52 class TtyTest : public ::testing::Test { |
45 public: | 53 public: |
46 void SetUp() { | 54 void SetUp() { |
47 ASSERT_EQ(0, ki_push_state_for_testing()); | 55 ASSERT_EQ(0, ki_push_state_for_testing()); |
48 ASSERT_EQ(0, ki_init(&kp_)); | 56 ASSERT_EQ(0, ki_init_interface(&kp_, &ppapi_)); |
| 57 |
| 58 var_iface_ = ppapi_.GetVarInterface(); |
49 } | 59 } |
50 | 60 |
51 void TearDown() { | 61 void TearDown() { |
52 ki_uninit(); | 62 ki_uninit(); |
53 } | 63 } |
54 | 64 |
| 65 int TtyWrite(int fd, const char* string) { |
| 66 PP_Var message_var = var_iface_->VarFromUtf8(string, strlen(string)); |
| 67 int result = ki_ioctl_wrapper(fd, NACL_IOC_HANDLEMESSAGE, &message_var); |
| 68 var_iface_->Release(message_var); |
| 69 return result; |
| 70 } |
| 71 |
55 protected: | 72 protected: |
| 73 FakePepperInterface ppapi_; |
56 KernelProxy kp_; | 74 KernelProxy kp_; |
| 75 VarInterface* var_iface_; |
57 }; | 76 }; |
58 | 77 |
59 TEST_F(TtyNodeTest, InvalidIoctl) { | 78 TEST_F(TtyNodeTest, InvalidIoctl) { |
60 // 123 is not a valid ioctl request. | 79 // 123 is not a valid ioctl request. |
61 EXPECT_EQ(EINVAL, dev_tty_->Ioctl(123)); | 80 EXPECT_EQ(EINVAL, dev_tty_->Ioctl(123)); |
62 } | 81 } |
63 | 82 |
64 TEST_F(TtyNodeTest, TtyInput) { | 83 TEST_F(TtyNodeTest, TtyInput) { |
65 // Now let's try sending some data over. | 84 // Now let's try sending some data over. |
66 // First we create the message. | 85 // First we create the message. |
67 std::string message("hello, how are you?\n"); | 86 std::string message("hello, how are you?\n"); |
68 struct tioc_nacl_input_string packaged_message; | 87 VarInterface* var_iface = ppapi_.GetVarInterface(); |
69 packaged_message.length = message.size(); | 88 PP_Var message_var = var_iface->VarFromUtf8(message.data(), message.size()); |
70 packaged_message.buffer = message.data(); | |
71 | 89 |
72 // Now we make buffer we'll read into. | 90 // Now we make buffer we'll read into. |
73 // We fill the buffer and a backup buffer with arbitrary data | 91 // We fill the buffer and a backup buffer with arbitrary data |
74 // and compare them after reading to make sure read doesn't | 92 // and compare them after reading to make sure read doesn't |
75 // clobber parts of the buffer it shouldn't. | 93 // clobber parts of the buffer it shouldn't. |
76 int bytes_read; | 94 int bytes_read; |
77 char buffer[100]; | 95 char buffer[100]; |
78 char backup_buffer[100]; | 96 char backup_buffer[100]; |
79 memset(buffer, 'a', 100); | 97 memset(buffer, 'a', 100); |
80 memset(backup_buffer, 'a', 100); | 98 memset(backup_buffer, 'a', 100); |
81 | 99 |
82 // Now we actually send the data | 100 // Now we actually send the data |
83 EXPECT_EQ(0, dev_tty_->Ioctl(TIOCNACLINPUT, &packaged_message)); | 101 EXPECT_EQ(0, dev_tty_->Ioctl(NACL_IOC_HANDLEMESSAGE, &message_var)); |
| 102 |
| 103 var_iface->Release(message_var); |
84 | 104 |
85 // We read a small chunk first to ensure it doesn't give us | 105 // We read a small chunk first to ensure it doesn't give us |
86 // more than we ask for. | 106 // more than we ask for. |
87 HandleAttr attrs; | 107 HandleAttr attrs; |
88 EXPECT_EQ(0, dev_tty_->Read(attrs, buffer, 5, &bytes_read)); | 108 EXPECT_EQ(0, dev_tty_->Read(attrs, buffer, 5, &bytes_read)); |
89 EXPECT_EQ(5, bytes_read); | 109 EXPECT_EQ(5, bytes_read); |
90 EXPECT_EQ(0, memcmp(message.data(), buffer, 5)); | 110 EXPECT_EQ(0, memcmp(message.data(), buffer, 5)); |
91 EXPECT_EQ(0, memcmp(buffer + 5, backup_buffer + 5, 95)); | 111 EXPECT_EQ(0, memcmp(buffer + 5, backup_buffer + 5, 95)); |
92 | 112 |
93 // Now we ask for more data than is left in the tty, to ensure | 113 // Now we ask for more data than is left in the tty, to ensure |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 handler.user_data = &user_data; | 150 handler.user_data = &user_data; |
131 | 151 |
132 EXPECT_EQ(0, dev_tty_->Ioctl(TIOCNACLOUTPUT, &handler)); | 152 EXPECT_EQ(0, dev_tty_->Ioctl(TIOCNACLOUTPUT, &handler)); |
133 | 153 |
134 EXPECT_EQ(0, dev_tty_->Write(attrs, message, message_len, &bytes_written)); | 154 EXPECT_EQ(0, dev_tty_->Write(attrs, message, message_len, &bytes_written)); |
135 EXPECT_EQ(message_len, bytes_written); | 155 EXPECT_EQ(message_len, bytes_written); |
136 EXPECT_EQ(message_len, user_data.output_count); | 156 EXPECT_EQ(message_len, user_data.output_count); |
137 EXPECT_EQ(0, strncmp(user_data.output_buf, message, message_len)); | 157 EXPECT_EQ(0, strncmp(user_data.output_buf, message, message_len)); |
138 } | 158 } |
139 | 159 |
140 static int ki_ioctl_wrapper(int fd, int request, ...) { | |
141 va_list ap; | |
142 va_start(ap, request); | |
143 int rtn = ki_ioctl(fd, request, ap); | |
144 va_end(ap); | |
145 return rtn; | |
146 } | |
147 | |
148 static int TtyWrite(int fd, const char* string) { | |
149 struct tioc_nacl_input_string input; | |
150 input.buffer = string; | |
151 input.length = strlen(input.buffer); | |
152 return ki_ioctl_wrapper(fd, TIOCNACLINPUT, &input); | |
153 } | |
154 | |
155 // Returns: | 160 // Returns: |
156 // 0 -> Not readable | 161 // 0 -> Not readable |
157 // 1 -> Readable | 162 // 1 -> Readable |
158 // -1 -> Error occured | 163 // -1 -> Error occured |
159 static int IsReadable(int fd) { | 164 static int IsReadable(int fd) { |
160 struct timeval timeout = {0, 0}; | 165 struct timeval timeout = {0, 0}; |
161 fd_set readfds; | 166 fd_set readfds; |
162 fd_set errorfds; | 167 fd_set errorfds; |
163 FD_ZERO(&readfds); | 168 FD_ZERO(&readfds); |
164 FD_ZERO(&errorfds); | 169 FD_ZERO(&errorfds); |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 pthread_join(resize_thread, NULL); | 335 pthread_join(resize_thread, NULL); |
331 ASSERT_EQ(-1, rtn); | 336 ASSERT_EQ(-1, rtn); |
332 ASSERT_EQ(EINTR, errno); | 337 ASSERT_EQ(EINTR, errno); |
333 ASSERT_EQ(0, IsReadable(tty_fd)); | 338 ASSERT_EQ(0, IsReadable(tty_fd)); |
334 } | 339 } |
335 | 340 |
336 /* | 341 /* |
337 * Sleep for 50ms then send some input to the /dev/tty. | 342 * Sleep for 50ms then send some input to the /dev/tty. |
338 */ | 343 */ |
339 static void* input_thread_main(void* arg) { | 344 static void* input_thread_main(void* arg) { |
| 345 TtyTest* thiz = static_cast<TtyTest*>(arg); |
| 346 |
340 usleep(50 * 1000); | 347 usleep(50 * 1000); |
341 | 348 |
342 int fd = ki_open("/dev/tty", O_RDONLY); | 349 int fd = ki_open("/dev/tty", O_RDONLY); |
343 TtyWrite(fd, "test\n"); | 350 thiz->TtyWrite(fd, "test\n"); |
344 return NULL; | 351 return NULL; |
345 } | 352 } |
346 | 353 |
347 TEST_F(TtyTest, InputDuringSelect) { | 354 TEST_F(TtyTest, InputDuringSelect) { |
348 // Test that input which occurs while in select causes | 355 // Test that input which occurs while in select causes |
349 // select to return. | 356 // select to return. |
350 int tty_fd = ki_open("/dev/tty", O_RDONLY); | 357 int tty_fd = ki_open("/dev/tty", O_RDONLY); |
351 | 358 |
352 fd_set readfds; | 359 fd_set readfds; |
353 fd_set errorfds; | 360 fd_set errorfds; |
354 FD_ZERO(&readfds); | 361 FD_ZERO(&readfds); |
355 FD_ZERO(&errorfds); | 362 FD_ZERO(&errorfds); |
356 FD_SET(tty_fd, &readfds); | 363 FD_SET(tty_fd, &readfds); |
357 FD_SET(tty_fd, &errorfds); | 364 FD_SET(tty_fd, &errorfds); |
358 | 365 |
359 pthread_t resize_thread; | 366 pthread_t resize_thread; |
360 pthread_create(&resize_thread, NULL, input_thread_main, NULL); | 367 pthread_create(&resize_thread, NULL, input_thread_main, this); |
361 | 368 |
362 struct timeval timeout; | 369 struct timeval timeout; |
363 timeout.tv_sec = 20; | 370 timeout.tv_sec = 20; |
364 timeout.tv_usec = 0; | 371 timeout.tv_usec = 0; |
365 | 372 |
366 int rtn = ki_select(tty_fd + 1, &readfds, NULL, &errorfds, &timeout); | 373 int rtn = ki_select(tty_fd + 1, &readfds, NULL, &errorfds, &timeout); |
367 pthread_join(resize_thread, NULL); | 374 pthread_join(resize_thread, NULL); |
368 | 375 |
369 ASSERT_EQ(1, rtn); | 376 ASSERT_EQ(1, rtn); |
370 } | 377 } |
371 } | 378 |
| 379 } // namespace |
OLD | NEW |