| 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> |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 return -3; // error | 181 return -3; // error |
| 182 return 1; // readable | 182 return 1; // readable |
| 183 } | 183 } |
| 184 | 184 |
| 185 TEST_F(TtyTest, TtySelect) { | 185 TEST_F(TtyTest, TtySelect) { |
| 186 struct timeval timeout; | 186 struct timeval timeout; |
| 187 fd_set readfds; | 187 fd_set readfds; |
| 188 fd_set writefds; | 188 fd_set writefds; |
| 189 fd_set errorfds; | 189 fd_set errorfds; |
| 190 | 190 |
| 191 int tty_fd = ki_open("/dev/tty", O_RDONLY); | 191 int tty_fd = ki_open("/dev/tty", O_RDONLY, 0); |
| 192 ASSERT_GT(tty_fd, 0) << "tty open failed: " << errno; | 192 ASSERT_GT(tty_fd, 0) << "tty open failed: " << errno; |
| 193 | 193 |
| 194 FD_ZERO(&readfds); | 194 FD_ZERO(&readfds); |
| 195 FD_ZERO(&errorfds); | 195 FD_ZERO(&errorfds); |
| 196 FD_SET(tty_fd, &readfds); | 196 FD_SET(tty_fd, &readfds); |
| 197 FD_SET(tty_fd, &errorfds); | 197 FD_SET(tty_fd, &errorfds); |
| 198 // 10 millisecond timeout | 198 // 10 millisecond timeout |
| 199 timeout.tv_sec = 0; | 199 timeout.tv_sec = 0; |
| 200 timeout.tv_usec = 10 * 1000; | 200 timeout.tv_usec = 10 * 1000; |
| 201 // Should timeout when no input is available. | 201 // Should timeout when no input is available. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 224 ASSERT_EQ(IsReadable(tty_fd), 0); | 224 ASSERT_EQ(IsReadable(tty_fd), 0); |
| 225 ASSERT_EQ(0, TtyWrite(tty_fd, "input:\n")); | 225 ASSERT_EQ(0, TtyWrite(tty_fd, "input:\n")); |
| 226 | 226 |
| 227 // TTY should now be readable | 227 // TTY should now be readable |
| 228 ASSERT_EQ(1, IsReadable(tty_fd)); | 228 ASSERT_EQ(1, IsReadable(tty_fd)); |
| 229 | 229 |
| 230 ki_close(tty_fd); | 230 ki_close(tty_fd); |
| 231 } | 231 } |
| 232 | 232 |
| 233 TEST_F(TtyTest, TtyICANON) { | 233 TEST_F(TtyTest, TtyICANON) { |
| 234 int tty_fd = ki_open("/dev/tty", O_RDONLY); | 234 int tty_fd = ki_open("/dev/tty", O_RDONLY, 0); |
| 235 | 235 |
| 236 ASSERT_EQ(0, IsReadable(tty_fd)); | 236 ASSERT_EQ(0, IsReadable(tty_fd)); |
| 237 | 237 |
| 238 struct termios tattr; | 238 struct termios tattr; |
| 239 ki_tcgetattr(tty_fd, &tattr); | 239 ki_tcgetattr(tty_fd, &tattr); |
| 240 tattr.c_lflag &= ~(ICANON | ECHO); /* Clear ICANON and ECHO. */ | 240 tattr.c_lflag &= ~(ICANON | ECHO); /* Clear ICANON and ECHO. */ |
| 241 ki_tcsetattr(tty_fd, TCSAFLUSH, &tattr); | 241 ki_tcsetattr(tty_fd, TCSAFLUSH, &tattr); |
| 242 | 242 |
| 243 ASSERT_EQ(0, IsReadable(tty_fd)); | 243 ASSERT_EQ(0, IsReadable(tty_fd)); |
| 244 | 244 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 257 ASSERT_EQ(0, IsReadable(tty_fd)); | 257 ASSERT_EQ(0, IsReadable(tty_fd)); |
| 258 } | 258 } |
| 259 | 259 |
| 260 static int g_received_signal; | 260 static int g_received_signal; |
| 261 | 261 |
| 262 static void sighandler(int sig) { g_received_signal = sig; } | 262 static void sighandler(int sig) { g_received_signal = sig; } |
| 263 | 263 |
| 264 TEST_F(TtyTest, WindowSize) { | 264 TEST_F(TtyTest, WindowSize) { |
| 265 // Get current window size | 265 // Get current window size |
| 266 struct winsize old_winsize = {0}; | 266 struct winsize old_winsize = {0}; |
| 267 int tty_fd = ki_open("/dev/tty", O_RDONLY); | 267 int tty_fd = ki_open("/dev/tty", O_RDONLY, 0); |
| 268 ASSERT_EQ(0, ki_ioctl_wrapper(tty_fd, TIOCGWINSZ, &old_winsize)); | 268 ASSERT_EQ(0, ki_ioctl_wrapper(tty_fd, TIOCGWINSZ, &old_winsize)); |
| 269 | 269 |
| 270 // Install signal handler | 270 // Install signal handler |
| 271 sighandler_t new_handler = sighandler; | 271 sighandler_t new_handler = sighandler; |
| 272 sighandler_t old_handler = ki_signal(SIGWINCH, new_handler); | 272 sighandler_t old_handler = ki_signal(SIGWINCH, new_handler); |
| 273 ASSERT_NE(SIG_ERR, old_handler) << "signal return error: " << errno; | 273 ASSERT_NE(SIG_ERR, old_handler) << "signal return error: " << errno; |
| 274 | 274 |
| 275 g_received_signal = 0; | 275 g_received_signal = 0; |
| 276 | 276 |
| 277 // Set a new windows size | 277 // Set a new windows size |
| (...skipping 27 matching lines...) Expand all Loading... |
| 305 struct winsize winsize; | 305 struct winsize winsize; |
| 306 winsize.ws_col = 100; | 306 winsize.ws_col = 100; |
| 307 winsize.ws_row = 200; | 307 winsize.ws_row = 200; |
| 308 ki_ioctl_wrapper(*tty_fd, TIOCSWINSZ, &winsize); | 308 ki_ioctl_wrapper(*tty_fd, TIOCSWINSZ, &winsize); |
| 309 return NULL; | 309 return NULL; |
| 310 } | 310 } |
| 311 | 311 |
| 312 TEST_F(TtyTest, ResizeDuringSelect) { | 312 TEST_F(TtyTest, ResizeDuringSelect) { |
| 313 // Test that a window resize during a call | 313 // Test that a window resize during a call |
| 314 // to select(3) will cause it to fail with EINTR. | 314 // to select(3) will cause it to fail with EINTR. |
| 315 int tty_fd = ki_open("/dev/tty", O_RDONLY); | 315 int tty_fd = ki_open("/dev/tty", O_RDONLY, 0); |
| 316 | 316 |
| 317 fd_set readfds; | 317 fd_set readfds; |
| 318 fd_set errorfds; | 318 fd_set errorfds; |
| 319 FD_ZERO(&readfds); | 319 FD_ZERO(&readfds); |
| 320 FD_ZERO(&errorfds); | 320 FD_ZERO(&errorfds); |
| 321 FD_SET(tty_fd, &readfds); | 321 FD_SET(tty_fd, &readfds); |
| 322 FD_SET(tty_fd, &errorfds); | 322 FD_SET(tty_fd, &errorfds); |
| 323 | 323 |
| 324 pthread_t resize_thread; | 324 pthread_t resize_thread; |
| 325 pthread_create(&resize_thread, NULL, resize_thread_main, &tty_fd); | 325 pthread_create(&resize_thread, NULL, resize_thread_main, &tty_fd); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 340 } | 340 } |
| 341 | 341 |
| 342 /* | 342 /* |
| 343 * Sleep for 50ms then send some input to the /dev/tty. | 343 * Sleep for 50ms then send some input to the /dev/tty. |
| 344 */ | 344 */ |
| 345 static void* input_thread_main(void* arg) { | 345 static void* input_thread_main(void* arg) { |
| 346 TtyTest* thiz = static_cast<TtyTest*>(arg); | 346 TtyTest* thiz = static_cast<TtyTest*>(arg); |
| 347 | 347 |
| 348 usleep(50 * 1000); | 348 usleep(50 * 1000); |
| 349 | 349 |
| 350 int fd = ki_open("/dev/tty", O_RDONLY); | 350 int fd = ki_open("/dev/tty", O_RDONLY, 0); |
| 351 thiz->TtyWrite(fd, "test\n"); | 351 thiz->TtyWrite(fd, "test\n"); |
| 352 return NULL; | 352 return NULL; |
| 353 } | 353 } |
| 354 | 354 |
| 355 TEST_F(TtyTest, InputDuringSelect) { | 355 TEST_F(TtyTest, InputDuringSelect) { |
| 356 // Test that input which occurs while in select causes | 356 // Test that input which occurs while in select causes |
| 357 // select to return. | 357 // select to return. |
| 358 int tty_fd = ki_open("/dev/tty", O_RDONLY); | 358 int tty_fd = ki_open("/dev/tty", O_RDONLY, 0); |
| 359 | 359 |
| 360 fd_set readfds; | 360 fd_set readfds; |
| 361 fd_set errorfds; | 361 fd_set errorfds; |
| 362 FD_ZERO(&readfds); | 362 FD_ZERO(&readfds); |
| 363 FD_ZERO(&errorfds); | 363 FD_ZERO(&errorfds); |
| 364 FD_SET(tty_fd, &readfds); | 364 FD_SET(tty_fd, &readfds); |
| 365 FD_SET(tty_fd, &errorfds); | 365 FD_SET(tty_fd, &errorfds); |
| 366 | 366 |
| 367 pthread_t resize_thread; | 367 pthread_t resize_thread; |
| 368 pthread_create(&resize_thread, NULL, input_thread_main, this); | 368 pthread_create(&resize_thread, NULL, input_thread_main, this); |
| 369 | 369 |
| 370 struct timeval timeout; | 370 struct timeval timeout; |
| 371 timeout.tv_sec = 20; | 371 timeout.tv_sec = 20; |
| 372 timeout.tv_usec = 0; | 372 timeout.tv_usec = 0; |
| 373 | 373 |
| 374 int rtn = ki_select(tty_fd + 1, &readfds, NULL, &errorfds, &timeout); | 374 int rtn = ki_select(tty_fd + 1, &readfds, NULL, &errorfds, &timeout); |
| 375 pthread_join(resize_thread, NULL); | 375 pthread_join(resize_thread, NULL); |
| 376 | 376 |
| 377 ASSERT_EQ(1, rtn); | 377 ASSERT_EQ(1, rtn); |
| 378 } | 378 } |
| 379 | 379 |
| 380 } // namespace | 380 } // namespace |
| OLD | NEW |