Chromium Code Reviews| Index: native_client_sdk/src/tests/nacl_io_test/mount_node_tty_test.cc |
| diff --git a/native_client_sdk/src/tests/nacl_io_test/mount_node_tty_test.cc b/native_client_sdk/src/tests/nacl_io_test/mount_node_tty_test.cc |
| index 9aa1ee74d60e9b6eb1d527e91863f71017aa6c1a..82f8565b1c7eec1a1e9ea505b5a6239dd5324acf 100644 |
| --- a/native_client_sdk/src/tests/nacl_io_test/mount_node_tty_test.cc |
| +++ b/native_client_sdk/src/tests/nacl_io_test/mount_node_tty_test.cc |
| @@ -5,6 +5,7 @@ |
| #include <errno.h> |
| #include <fcntl.h> |
| #include <string.h> |
| +#include <sys/ioctl.h> |
| #include <sys/select.h> |
| #include <sys/stat.h> |
| #include <sys/time.h> |
| @@ -50,15 +51,9 @@ TEST_F(TtyTest, InvalidIoctl) { |
| } |
| TEST_F(TtyTest, TtyInput) { |
| - // TIOCNACLPREFIX is, it should set the prefix. |
| - std::string prefix("__my_awesome_prefix__"); |
| - EXPECT_EQ(0, dev_tty_->Ioctl(TIOCNACLPREFIX, |
| - const_cast<char*>(prefix.c_str()))); |
| - |
| // Now let's try sending some data over. |
| // First we create the message. |
| - std::string content("hello, how are you?\n"); |
| - std::string message = prefix.append(content); |
| + std::string message("hello, how are you?\n"); |
| struct tioc_nacl_input_string packaged_message; |
| packaged_message.length = message.size(); |
| packaged_message.buffer = message.data(); |
| @@ -81,31 +76,44 @@ TEST_F(TtyTest, TtyInput) { |
| // more than we ask for. |
| EXPECT_EQ(0, dev_tty_->Read(0, buffer, 5, &bytes_read)); |
| EXPECT_EQ(bytes_read, 5); |
| - EXPECT_EQ(0, memcmp(content.data(), buffer, 5)); |
| + EXPECT_EQ(0, memcmp(message.data(), buffer, 5)); |
| EXPECT_EQ(0, memcmp(buffer + 5, backup_buffer + 5, 95)); |
| // Now we ask for more data than is left in the tty, to ensure |
| // it doesn't give us more than is there. |
| EXPECT_EQ(0, dev_tty_->Read(0, buffer + 5, 95, &bytes_read)); |
| - EXPECT_EQ(bytes_read, content.size() - 5); |
| - EXPECT_EQ(0, memcmp(content.data(), buffer, content.size())); |
| - EXPECT_EQ(0, memcmp(buffer + content.size(), |
| - backup_buffer + content.size(), |
| - 100 - content.size())); |
| + EXPECT_EQ(bytes_read, message.size() - 5); |
| + EXPECT_EQ(0, memcmp(message.data(), buffer, message.size())); |
| + EXPECT_EQ(0, memcmp(buffer + message.size(), |
| + backup_buffer + message.size(), |
| + 100 - message.size())); |
| } |
| -TEST_F(TtyTest, InvalidPrefix) { |
| - std::string prefix("__my_awesome_prefix__"); |
| - ASSERT_EQ(0, dev_tty_->Ioctl(TIOCNACLPREFIX, |
| - const_cast<char*>(prefix.c_str()))); |
| - |
| - // Now we try to send something with an invalid prefix |
| - std::string bogus_message("Woah there, this message has no valid prefix"); |
| - struct tioc_nacl_input_string bogus_pack; |
| - bogus_pack.length = bogus_message.size(); |
| - bogus_pack.buffer = bogus_message.data(); |
| - ASSERT_EQ(ENOTTY, dev_tty_->Ioctl(TIOCNACLINPUT, |
| - reinterpret_cast<char*>(&bogus_pack))); |
| +static const char* g_output_buf; |
| +static int g_output_count; |
| + |
| +static int output_handler(const char* buf, int count) { |
| + g_output_buf = buf; |
|
binji
2013/08/22 18:09:36
won't need a global if you add user_data to the ou
Sam Clegg
2013/08/22 19:52:25
Done.
|
| + g_output_count = count; |
| + return count; |
| +} |
| + |
| +TEST_F(TtyTest, TtyOutput) { |
| + // When no handler is registered then all writes should return EIO |
| + int bytes_written = 10; |
| + const char* message = "hello\n"; |
| + int message_len = strlen(message); |
| + EXPECT_EQ(EIO, dev_tty_->Write(0, message, message_len, &bytes_written)); |
| + |
| + g_output_buf = NULL; |
| + g_output_count = 0; |
| + EXPECT_EQ(0, dev_tty_->Ioctl(TIOCNACLOUTPUT, |
| + reinterpret_cast<char*>(output_handler))); |
| + |
| + EXPECT_EQ(0, dev_tty_->Write(0, message, message_len, &bytes_written)); |
| + EXPECT_EQ(message_len, bytes_written); |
| + EXPECT_EQ(message_len, g_output_count); |
| + EXPECT_EQ(0, strncmp(g_output_buf, message, message_len)); |
| } |
| // Returns: |
| @@ -169,16 +177,16 @@ TEST_F(TtyTest, TtySelect) { |
| // Send 4 bytes to TTY input |
| struct tioc_nacl_input_string input; |
| - input.buffer = "test"; |
| - input.length = 4; |
| + input.buffer = "input:test"; |
| + input.length = strlen(input.buffer); |
| char* ioctl_arg = reinterpret_cast<char*>(&input); |
| ASSERT_EQ(0, ki_ioctl(tty_fd, TIOCNACLINPUT, ioctl_arg)); |
| // TTY should not be readable until newline in written |
| ASSERT_EQ(IsReadable(tty_fd), 0); |
| - input.buffer = "\n"; |
| - input.length = 1; |
| + input.buffer = "input:\n"; |
| + input.length = strlen(input.buffer); |
| ASSERT_EQ(0, ki_ioctl(tty_fd, TIOCNACLINPUT, ioctl_arg)); |
| // TTY should now be readable |
| @@ -187,4 +195,46 @@ TEST_F(TtyTest, TtySelect) { |
| ki_close(tty_fd); |
| } |
| +int g_recieved_signal = 0; |
| + |
| +void sighandler(int sig) { |
| + g_recieved_signal = sig; |
| +} |
| + |
| +TEST_F(TtyTest, WindowSize) { |
| + // Get current window size |
| + struct winsize old_winsize = { 0 }; |
| + ASSERT_EQ(0, dev_tty_->Ioctl(TIOCGWINSZ, |
| + reinterpret_cast<char*>(&old_winsize))); |
| + |
| + // Install signal handler |
| + sighandler_t new_handler = sighandler; |
| + sighandler_t old_handler = ki_signal(SIGWINCH, new_handler); |
| + ASSERT_NE(old_handler, SIG_ERR) << "signal return error: " << errno; |
| + |
| + // Set a new windows size |
| + struct winsize winsize; |
| + winsize.ws_col = 100; |
| + winsize.ws_row = 200; |
| + EXPECT_EQ(0, dev_tty_->Ioctl(TIOCSWINSZ, |
| + reinterpret_cast<char*>(&winsize))); |
| + EXPECT_EQ(g_recieved_signal, SIGWINCH); |
| + |
| + // Restore old signal handler |
| + EXPECT_EQ(new_handler, ki_signal(SIGWINCH, old_handler)); |
| + |
| + // Verify new window size can be queried correctly. |
| + winsize.ws_col = 0; |
| + winsize.ws_row = 0; |
| + EXPECT_EQ(0, dev_tty_->Ioctl(TIOCGWINSZ, |
| + reinterpret_cast<char*>(&winsize))); |
| + EXPECT_EQ(winsize.ws_col, 100); |
| + EXPECT_EQ(winsize.ws_row, 200); |
| + |
| + // Restore original windows size. |
| + EXPECT_EQ(0, dev_tty_->Ioctl(TIOCSWINSZ, |
| + reinterpret_cast<char*>(&old_winsize))); |
| + |
| +} |
| + |
| } |