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

Unified Diff: native_client_sdk/src/libraries/nacl_io_test/mount_node_tty_test.cc

Issue 23075013: [NaCl SDK] Enable select/poll on TTY nodes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: native_client_sdk/src/libraries/nacl_io_test/mount_node_tty_test.cc
diff --git a/native_client_sdk/src/libraries/nacl_io_test/mount_node_tty_test.cc b/native_client_sdk/src/libraries/nacl_io_test/mount_node_tty_test.cc
new file mode 100644
index 0000000000000000000000000000000000000000..1fe02f2c48d6f071cc2f7761abb83ee79cec3edb
--- /dev/null
+++ b/native_client_sdk/src/libraries/nacl_io_test/mount_node_tty_test.cc
@@ -0,0 +1,113 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <errno.h>
+#include <fcntl.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <string>
+
+#include "gtest/gtest.h"
+#include "mount_dev_mock.h"
+#include "nacl_io/ioctl.h"
+#include "nacl_io/kernel_intercept.h"
+#include "nacl_io/kernel_proxy.h"
+#include "nacl_io/mount.h"
+#include "nacl_io/mount_dev.h"
+#include "nacl_io/mount_mem.h"
+#include "nacl_io/osdirent.h"
+#include "nacl_io/osunistd.h"
+
+using namespace nacl_io;
+
+namespace {
+
+class TtyTest : public ::testing::Test {
+ public:
+ void SetUp() {
+ ki_init(&kp_);
+ ASSERT_EQ(0, mnt_.Access(Path("/tty"), R_OK | W_OK));
+ ASSERT_EQ(EACCES, mnt_.Access(Path("/tty"), X_OK));
+ ASSERT_EQ(0, mnt_.Open(Path("/tty"), O_RDWR, &dev_tty_));
+ ASSERT_NE(NULL_NODE, dev_tty_.get());
+ }
+
+ void TearDown() {
+ ki_uninit();
+ }
+
+ protected:
+ KernelProxy kp_;
+ MountDevMock mnt_;
+ ScopedMountNode dev_tty_;
+};
+
+TEST_F(TtyTest, InvalidIoctl) {
+ // 123 is not a valid ioctl request.
+ EXPECT_EQ(EINVAL, dev_tty_->Ioctl(123, NULL));
+}
+
+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);
+ struct tioc_nacl_input_string packaged_message;
+ packaged_message.length = message.size();
+ packaged_message.buffer = message.data();
+
+ // Now we make buffer we'll read into.
+ // We fill the buffer and a backup buffer with arbitrary data
+ // and compare them after reading to make sure read doesn't
+ // clobber parts of the buffer it shouldn't.
+ int bytes_read;
+ char buffer[100];
+ char backup_buffer[100];
+ memset(buffer, 'a', 100);
+ memset(backup_buffer, 'a', 100);
+
+ // Now we actually send the data
+ EXPECT_EQ(0, dev_tty_->Ioctl(TIOCNACLINPUT,
+ reinterpret_cast<char*>(&packaged_message)));
+
+ // We read a small chunk first to ensure it doesn't give us
+ // 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(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()));
+}
+
+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)));
+}
+
+TEST_F(TtyTest, TtySelect) {
binji 2013/08/20 18:26:49 empty?
Sam Clegg 2013/08/20 19:09:36 Done.
+}
+
+}

Powered by Google App Engine
This is Rietveld 408576698