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 class TtyNodeTest : public ::testing::Test { |
| 28 public: |
| 29 void SetUp() { |
| 30 ASSERT_EQ(0, fs_.Access(Path("/tty"), R_OK | W_OK)); |
| 31 ASSERT_EQ(EACCES, fs_.Access(Path("/tty"), X_OK)); |
| 32 ASSERT_EQ(0, fs_.Open(Path("/tty"), O_RDWR, &dev_tty_)); |
| 33 ASSERT_NE(NULL_NODE, dev_tty_.get()); |
| 34 } |
| 35 |
| 36 protected: |
| 37 DevFsForTesting fs_; |
| 38 ScopedNode dev_tty_; |
| 39 }; |
| 40 |
27 class TtyTest : public ::testing::Test { | 41 class TtyTest : public ::testing::Test { |
28 public: | 42 public: |
29 void SetUp() { | 43 void SetUp() { |
30 ASSERT_EQ(0, ki_push_state_for_testing()); | 44 ASSERT_EQ(0, ki_push_state_for_testing()); |
31 ASSERT_EQ(0, ki_init(&kp_)); | 45 ASSERT_EQ(0, ki_init(&kp_)); |
32 ASSERT_EQ(0, fs_.Access(Path("/tty"), R_OK | W_OK)); | |
33 ASSERT_EQ(EACCES, fs_.Access(Path("/tty"), X_OK)); | |
34 ASSERT_EQ(0, fs_.Open(Path("/tty"), O_RDWR, &dev_tty_)); | |
35 ASSERT_NE(NULL_NODE, dev_tty_.get()); | |
36 } | 46 } |
37 | 47 |
38 void TearDown() { ki_uninit(); } | 48 void TearDown() { |
| 49 ki_uninit(); |
| 50 } |
39 | 51 |
40 protected: | 52 protected: |
41 KernelProxy kp_; | 53 KernelProxy kp_; |
42 DevFsForTesting fs_; | |
43 ScopedNode dev_tty_; | |
44 }; | 54 }; |
45 | 55 |
46 TEST_F(TtyTest, InvalidIoctl) { | 56 TEST_F(TtyNodeTest, InvalidIoctl) { |
47 // 123 is not a valid ioctl request. | 57 // 123 is not a valid ioctl request. |
48 EXPECT_EQ(EINVAL, dev_tty_->Ioctl(123)); | 58 EXPECT_EQ(EINVAL, dev_tty_->Ioctl(123)); |
49 } | 59 } |
50 | 60 |
51 TEST_F(TtyTest, TtyInput) { | 61 TEST_F(TtyNodeTest, TtyInput) { |
52 // Now let's try sending some data over. | 62 // Now let's try sending some data over. |
53 // First we create the message. | 63 // First we create the message. |
54 std::string message("hello, how are you?\n"); | 64 std::string message("hello, how are you?\n"); |
55 struct tioc_nacl_input_string packaged_message; | 65 struct tioc_nacl_input_string packaged_message; |
56 packaged_message.length = message.size(); | 66 packaged_message.length = message.size(); |
57 packaged_message.buffer = message.data(); | 67 packaged_message.buffer = message.data(); |
58 | 68 |
59 // Now we make buffer we'll read into. | 69 // Now we make buffer we'll read into. |
60 // We fill the buffer and a backup buffer with arbitrary data | 70 // We fill the buffer and a backup buffer with arbitrary data |
61 // and compare them after reading to make sure read doesn't | 71 // and compare them after reading to make sure read doesn't |
(...skipping 30 matching lines...) Expand all Loading... |
92 size_t output_count; | 102 size_t output_count; |
93 }; | 103 }; |
94 | 104 |
95 static ssize_t output_handler(const char* buf, size_t count, void* data) { | 105 static ssize_t output_handler(const char* buf, size_t count, void* data) { |
96 user_data_t* user_data = static_cast<user_data_t*>(data); | 106 user_data_t* user_data = static_cast<user_data_t*>(data); |
97 user_data->output_buf = buf; | 107 user_data->output_buf = buf; |
98 user_data->output_count = count; | 108 user_data->output_count = count; |
99 return count; | 109 return count; |
100 } | 110 } |
101 | 111 |
102 TEST_F(TtyTest, TtyOutput) { | 112 TEST_F(TtyNodeTest, TtyOutput) { |
103 // When no handler is registered then all writes should return EIO | 113 // When no handler is registered then all writes should return EIO |
104 int bytes_written = 10; | 114 int bytes_written = 10; |
105 const char* message = "hello\n"; | 115 const char* message = "hello\n"; |
106 int message_len = strlen(message); | 116 int message_len = strlen(message); |
107 HandleAttr attrs; | 117 HandleAttr attrs; |
108 EXPECT_EQ(EIO, dev_tty_->Write(attrs, message, message_len, &bytes_written)); | 118 EXPECT_EQ(EIO, dev_tty_->Write(attrs, message, message_len, &bytes_written)); |
109 | 119 |
110 // Setup output handler with user_data to record calls. | 120 // Setup output handler with user_data to record calls. |
111 user_data_t user_data; | 121 user_data_t user_data; |
112 user_data.output_buf = NULL; | 122 user_data.output_buf = NULL; |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 ASSERT_EQ(0, IsReadable(tty_fd)); | 248 ASSERT_EQ(0, IsReadable(tty_fd)); |
239 } | 249 } |
240 | 250 |
241 static int g_recieved_signal; | 251 static int g_recieved_signal; |
242 | 252 |
243 static void sighandler(int sig) { g_recieved_signal = sig; } | 253 static void sighandler(int sig) { g_recieved_signal = sig; } |
244 | 254 |
245 TEST_F(TtyTest, WindowSize) { | 255 TEST_F(TtyTest, WindowSize) { |
246 // Get current window size | 256 // Get current window size |
247 struct winsize old_winsize = {0}; | 257 struct winsize old_winsize = {0}; |
248 ASSERT_EQ(0, dev_tty_->Ioctl(TIOCGWINSZ, &old_winsize)); | 258 int tty_fd = ki_open("/dev/tty", O_RDONLY); |
| 259 ASSERT_EQ(0, ki_ioctl_wrapper(tty_fd, TIOCGWINSZ, &old_winsize)); |
249 | 260 |
250 // Install signal handler | 261 // Install signal handler |
251 sighandler_t new_handler = sighandler; | 262 sighandler_t new_handler = sighandler; |
252 sighandler_t old_handler = ki_signal(SIGWINCH, new_handler); | 263 sighandler_t old_handler = ki_signal(SIGWINCH, new_handler); |
253 ASSERT_NE(SIG_ERR, old_handler) << "signal return error: " << errno; | 264 ASSERT_NE(SIG_ERR, old_handler) << "signal return error: " << errno; |
254 | 265 |
255 g_recieved_signal = 0; | 266 g_recieved_signal = 0; |
256 | 267 |
257 // Set a new windows size | 268 // Set a new windows size |
258 struct winsize winsize; | 269 struct winsize winsize; |
259 winsize.ws_col = 100; | 270 winsize.ws_col = 100; |
260 winsize.ws_row = 200; | 271 winsize.ws_row = 200; |
261 EXPECT_EQ(0, dev_tty_->Ioctl(TIOCSWINSZ, &winsize)); | 272 EXPECT_EQ(0, ki_ioctl_wrapper(tty_fd, TIOCSWINSZ, &winsize)); |
262 EXPECT_EQ(SIGWINCH, g_recieved_signal); | 273 EXPECT_EQ(SIGWINCH, g_recieved_signal); |
263 | 274 |
264 // Restore old signal handler | 275 // Restore old signal handler |
265 EXPECT_EQ(new_handler, ki_signal(SIGWINCH, old_handler)); | 276 EXPECT_EQ(new_handler, ki_signal(SIGWINCH, old_handler)); |
266 | 277 |
267 // Verify new window size can be queried correctly. | 278 // Verify new window size can be queried correctly. |
268 winsize.ws_col = 0; | 279 winsize.ws_col = 0; |
269 winsize.ws_row = 0; | 280 winsize.ws_row = 0; |
270 EXPECT_EQ(0, dev_tty_->Ioctl(TIOCGWINSZ, &winsize)); | 281 EXPECT_EQ(0, ki_ioctl_wrapper(tty_fd, TIOCGWINSZ, &winsize)); |
271 EXPECT_EQ(100, winsize.ws_col); | 282 EXPECT_EQ(100, winsize.ws_col); |
272 EXPECT_EQ(200, winsize.ws_row); | 283 EXPECT_EQ(200, winsize.ws_row); |
273 | 284 |
274 // Restore original windows size. | 285 // Restore original windows size. |
275 EXPECT_EQ(0, dev_tty_->Ioctl(TIOCSWINSZ, &old_winsize)); | 286 EXPECT_EQ(0, ki_ioctl_wrapper(tty_fd, TIOCSWINSZ, &old_winsize)); |
276 } | 287 } |
277 | 288 |
278 /* | 289 /* |
279 * Sleep for 50ms then send a resize event to /dev/tty. | 290 * Sleep for 50ms then send a resize event to /dev/tty. |
280 */ | 291 */ |
281 static void* resize_thread_main(void* arg) { | 292 static void* resize_thread_main(void* arg) { |
282 usleep(50 * 1000); | 293 usleep(50 * 1000); |
283 | 294 |
284 int* tty_fd = static_cast<int*>(arg); | 295 int* tty_fd = static_cast<int*>(arg); |
285 struct winsize winsize; | 296 struct winsize winsize; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 int tty_fd = ki_open("/dev/tty", O_RDONLY); | 347 int tty_fd = ki_open("/dev/tty", O_RDONLY); |
337 | 348 |
338 fd_set readfds; | 349 fd_set readfds; |
339 fd_set errorfds; | 350 fd_set errorfds; |
340 FD_ZERO(&readfds); | 351 FD_ZERO(&readfds); |
341 FD_ZERO(&errorfds); | 352 FD_ZERO(&errorfds); |
342 FD_SET(tty_fd, &readfds); | 353 FD_SET(tty_fd, &readfds); |
343 FD_SET(tty_fd, &errorfds); | 354 FD_SET(tty_fd, &errorfds); |
344 | 355 |
345 pthread_t resize_thread; | 356 pthread_t resize_thread; |
346 pthread_create(&resize_thread, NULL, input_thread_main, &dev_tty_); | 357 pthread_create(&resize_thread, NULL, input_thread_main, NULL); |
347 | 358 |
348 struct timeval timeout; | 359 struct timeval timeout; |
349 timeout.tv_sec = 20; | 360 timeout.tv_sec = 20; |
350 timeout.tv_usec = 0; | 361 timeout.tv_usec = 0; |
351 | 362 |
352 int rtn = ki_select(tty_fd + 1, &readfds, NULL, &errorfds, &timeout); | 363 int rtn = ki_select(tty_fd + 1, &readfds, NULL, &errorfds, &timeout); |
353 pthread_join(resize_thread, NULL); | 364 pthread_join(resize_thread, NULL); |
354 | 365 |
355 ASSERT_EQ(1, rtn); | 366 ASSERT_EQ(1, rtn); |
356 } | 367 } |
357 } | 368 } |
OLD | NEW |