Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(416)

Side by Side Diff: native_client_sdk/src/tests/nacl_io_test/tty_test.cc

Issue 334983007: [NaCl SDK] Remove use of TIOCNACLINPUT from nacl_io/ppapi_simple. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 buffer_iface_ = ppapi_.GetVarArrayBufferInterface();
59 var_iface_ = ppapi_.GetVarInterface();
49 } 60 }
50 61
51 void TearDown() { 62 void TearDown() {
52 ki_uninit(); 63 ki_uninit();
53 } 64 }
54 65
66 int TtyWrite(int fd, const char* string) {
67 size_t len = strlen(string);
68 PP_Var message_var = buffer_iface_->Create(strlen(string));
69
70 char* data = static_cast<char*>(buffer_iface_->Map(message_var));
71 EXPECT_NE((char*)NULL, data);
72
73 memcpy(data, string, len);
74 buffer_iface_->Unmap(message_var);
75
76 int result = ki_ioctl_wrapper(fd, NACL_IOC_HANDLEMESSAGE, &message_var);
77
78 var_iface_->Release(message_var);
79 return result;
80 }
81
55 protected: 82 protected:
83 FakePepperInterface ppapi_;
56 KernelProxy kp_; 84 KernelProxy kp_;
85 VarArrayBufferInterface* buffer_iface_;
86 VarInterface* var_iface_;
57 }; 87 };
58 88
59 TEST_F(TtyNodeTest, InvalidIoctl) { 89 TEST_F(TtyNodeTest, InvalidIoctl) {
60 // 123 is not a valid ioctl request. 90 // 123 is not a valid ioctl request.
61 EXPECT_EQ(EINVAL, dev_tty_->Ioctl(123)); 91 EXPECT_EQ(EINVAL, dev_tty_->Ioctl(123));
62 } 92 }
63 93
64 TEST_F(TtyNodeTest, TtyInput) { 94 TEST_F(TtyNodeTest, TtyInput) {
65 // Now let's try sending some data over. 95 // Now let's try sending some data over.
66 // First we create the message. 96 // First we create the message.
67 std::string message("hello, how are you?\n"); 97 std::string message("hello, how are you?\n");
68 struct tioc_nacl_input_string packaged_message; 98 VarArrayBufferInterface* buffer_iface = ppapi_.GetVarArrayBufferInterface();
69 packaged_message.length = message.size(); 99 VarInterface* var_iface = ppapi_.GetVarInterface();
70 packaged_message.buffer = message.data(); 100 PP_Var message_var = buffer_iface->Create(message.size());
101
102 char* data = static_cast<char*>(buffer_iface->Map(message_var));
103 ASSERT_NE((char*)NULL, data);
104
105 memcpy(data, message.data(), message.size());
106 buffer_iface->Unmap(message_var);
71 107
72 // Now we make buffer we'll read into. 108 // Now we make buffer we'll read into.
73 // We fill the buffer and a backup buffer with arbitrary data 109 // We fill the buffer and a backup buffer with arbitrary data
74 // and compare them after reading to make sure read doesn't 110 // and compare them after reading to make sure read doesn't
75 // clobber parts of the buffer it shouldn't. 111 // clobber parts of the buffer it shouldn't.
76 int bytes_read; 112 int bytes_read;
77 char buffer[100]; 113 char buffer[100];
78 char backup_buffer[100]; 114 char backup_buffer[100];
79 memset(buffer, 'a', 100); 115 memset(buffer, 'a', 100);
80 memset(backup_buffer, 'a', 100); 116 memset(backup_buffer, 'a', 100);
81 117
82 // Now we actually send the data 118 // Now we actually send the data
83 EXPECT_EQ(0, dev_tty_->Ioctl(TIOCNACLINPUT, &packaged_message)); 119 EXPECT_EQ(0, dev_tty_->Ioctl(NACL_IOC_HANDLEMESSAGE, &message_var));
120
121 var_iface->Release(message_var);
84 122
85 // We read a small chunk first to ensure it doesn't give us 123 // We read a small chunk first to ensure it doesn't give us
86 // more than we ask for. 124 // more than we ask for.
87 HandleAttr attrs; 125 HandleAttr attrs;
88 EXPECT_EQ(0, dev_tty_->Read(attrs, buffer, 5, &bytes_read)); 126 EXPECT_EQ(0, dev_tty_->Read(attrs, buffer, 5, &bytes_read));
89 EXPECT_EQ(5, bytes_read); 127 EXPECT_EQ(5, bytes_read);
90 EXPECT_EQ(0, memcmp(message.data(), buffer, 5)); 128 EXPECT_EQ(0, memcmp(message.data(), buffer, 5));
91 EXPECT_EQ(0, memcmp(buffer + 5, backup_buffer + 5, 95)); 129 EXPECT_EQ(0, memcmp(buffer + 5, backup_buffer + 5, 95));
92 130
93 // Now we ask for more data than is left in the tty, to ensure 131 // 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
130 handler.user_data = &user_data; 168 handler.user_data = &user_data;
131 169
132 EXPECT_EQ(0, dev_tty_->Ioctl(TIOCNACLOUTPUT, &handler)); 170 EXPECT_EQ(0, dev_tty_->Ioctl(TIOCNACLOUTPUT, &handler));
133 171
134 EXPECT_EQ(0, dev_tty_->Write(attrs, message, message_len, &bytes_written)); 172 EXPECT_EQ(0, dev_tty_->Write(attrs, message, message_len, &bytes_written));
135 EXPECT_EQ(message_len, bytes_written); 173 EXPECT_EQ(message_len, bytes_written);
136 EXPECT_EQ(message_len, user_data.output_count); 174 EXPECT_EQ(message_len, user_data.output_count);
137 EXPECT_EQ(0, strncmp(user_data.output_buf, message, message_len)); 175 EXPECT_EQ(0, strncmp(user_data.output_buf, message, message_len));
138 } 176 }
139 177
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: 178 // Returns:
156 // 0 -> Not readable 179 // 0 -> Not readable
157 // 1 -> Readable 180 // 1 -> Readable
158 // -1 -> Error occured 181 // -1 -> Error occured
159 static int IsReadable(int fd) { 182 static int IsReadable(int fd) {
160 struct timeval timeout = {0, 0}; 183 struct timeval timeout = {0, 0};
161 fd_set readfds; 184 fd_set readfds;
162 fd_set errorfds; 185 fd_set errorfds;
163 FD_ZERO(&readfds); 186 FD_ZERO(&readfds);
164 FD_ZERO(&errorfds); 187 FD_ZERO(&errorfds);
165 FD_SET(fd, &readfds); 188 FD_SET(fd, &readfds);
166 FD_SET(fd, &errorfds); 189 FD_SET(fd, &errorfds);
167 int rtn = ki_select(fd + 1, &readfds, NULL, &errorfds, &timeout); 190 int rtn = ki_select(fd + 1, &readfds, NULL, &errorfds, &timeout);
168 if (rtn == 0) 191 if (rtn == 0)
169 return 0; // not readable 192 return 0; // not readable
170 if (rtn != 1) 193 if (rtn != 1)
171 return -1; // error 194 return -1; // error
172 if (FD_ISSET(fd, &errorfds)) 195 if (FD_ISSET(fd, &errorfds))
173 return -2; // error 196 return -2; // error
174 if (!FD_ISSET(fd, &readfds)) 197 if (!FD_ISSET(fd, &readfds))
175 return -3; // error 198 return -3; // error
176 return 1; // readable 199 return 1; // readable
177 } 200 }
178 201
202 TEST_F(TtyTest, Empty) {
203 }
Sam Clegg 2014/06/17 22:29:29 ?
binji 2014/06/17 22:55:23 Done.
204
179 TEST_F(TtyTest, TtySelect) { 205 TEST_F(TtyTest, TtySelect) {
180 struct timeval timeout; 206 struct timeval timeout;
181 fd_set readfds; 207 fd_set readfds;
182 fd_set writefds; 208 fd_set writefds;
183 fd_set errorfds; 209 fd_set errorfds;
184 210
185 int tty_fd = ki_open("/dev/tty", O_RDONLY); 211 int tty_fd = ki_open("/dev/tty", O_RDONLY);
186 ASSERT_GT(tty_fd, 0) << "tty open failed: " << errno; 212 ASSERT_GT(tty_fd, 0) << "tty open failed: " << errno;
187 213
188 FD_ZERO(&readfds); 214 FD_ZERO(&readfds);
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 pthread_join(resize_thread, NULL); 356 pthread_join(resize_thread, NULL);
331 ASSERT_EQ(-1, rtn); 357 ASSERT_EQ(-1, rtn);
332 ASSERT_EQ(EINTR, errno); 358 ASSERT_EQ(EINTR, errno);
333 ASSERT_EQ(0, IsReadable(tty_fd)); 359 ASSERT_EQ(0, IsReadable(tty_fd));
334 } 360 }
335 361
336 /* 362 /*
337 * Sleep for 50ms then send some input to the /dev/tty. 363 * Sleep for 50ms then send some input to the /dev/tty.
338 */ 364 */
339 static void* input_thread_main(void* arg) { 365 static void* input_thread_main(void* arg) {
366 TtyTest* thiz = static_cast<TtyTest*>(arg);
367
340 usleep(50 * 1000); 368 usleep(50 * 1000);
341 369
342 int fd = ki_open("/dev/tty", O_RDONLY); 370 int fd = ki_open("/dev/tty", O_RDONLY);
343 TtyWrite(fd, "test\n"); 371 thiz->TtyWrite(fd, "test\n");
344 return NULL; 372 return NULL;
345 } 373 }
346 374
347 TEST_F(TtyTest, InputDuringSelect) { 375 TEST_F(TtyTest, InputDuringSelect) {
348 // Test that input which occurs while in select causes 376 // Test that input which occurs while in select causes
349 // select to return. 377 // select to return.
350 int tty_fd = ki_open("/dev/tty", O_RDONLY); 378 int tty_fd = ki_open("/dev/tty", O_RDONLY);
351 379
352 fd_set readfds; 380 fd_set readfds;
353 fd_set errorfds; 381 fd_set errorfds;
354 FD_ZERO(&readfds); 382 FD_ZERO(&readfds);
355 FD_ZERO(&errorfds); 383 FD_ZERO(&errorfds);
356 FD_SET(tty_fd, &readfds); 384 FD_SET(tty_fd, &readfds);
357 FD_SET(tty_fd, &errorfds); 385 FD_SET(tty_fd, &errorfds);
358 386
359 pthread_t resize_thread; 387 pthread_t resize_thread;
360 pthread_create(&resize_thread, NULL, input_thread_main, NULL); 388 pthread_create(&resize_thread, NULL, input_thread_main, this);
361 389
362 struct timeval timeout; 390 struct timeval timeout;
363 timeout.tv_sec = 20; 391 timeout.tv_sec = 20;
364 timeout.tv_usec = 0; 392 timeout.tv_usec = 0;
365 393
366 int rtn = ki_select(tty_fd + 1, &readfds, NULL, &errorfds, &timeout); 394 int rtn = ki_select(tty_fd + 1, &readfds, NULL, &errorfds, &timeout);
367 pthread_join(resize_thread, NULL); 395 pthread_join(resize_thread, NULL);
368 396
369 ASSERT_EQ(1, rtn); 397 ASSERT_EQ(1, rtn);
370 } 398 }
371 } 399
400 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698