| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/stat.h> | 8 #include <sys/stat.h> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "gtest/gtest.h" | 11 #include "gtest/gtest.h" |
| 12 #include "mount_dev_mock.h" |
| 12 #include "nacl_io/ioctl.h" | 13 #include "nacl_io/ioctl.h" |
| 13 #include "nacl_io/mount.h" | 14 #include "nacl_io/mount.h" |
| 14 #include "nacl_io/mount_dev.h" | |
| 15 #include "nacl_io/mount_mem.h" | 15 #include "nacl_io/mount_mem.h" |
| 16 #include "nacl_io/osdirent.h" | 16 #include "nacl_io/osdirent.h" |
| 17 #include "nacl_io/osunistd.h" | 17 #include "nacl_io/osunistd.h" |
| 18 | 18 |
| 19 using namespace nacl_io; | 19 using namespace nacl_io; |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 class MountMemMock : public MountMem { | 23 class MountMemMock : public MountMem { |
| 24 public: | 24 public: |
| 25 MountMemMock() { | 25 MountMemMock() { |
| 26 StringMap_t map; | 26 StringMap_t map; |
| 27 EXPECT_EQ(0, Init(1, map, NULL)); | 27 EXPECT_EQ(0, Init(1, map, NULL)); |
| 28 } | 28 } |
| 29 | 29 |
| 30 int num_nodes() { return (int) inode_pool_.size(); } | 30 int num_nodes() { return (int) inode_pool_.size(); } |
| 31 }; | 31 }; |
| 32 | 32 |
| 33 class MountDevMock : public MountDev { | |
| 34 public: | |
| 35 MountDevMock() { | |
| 36 StringMap_t map; | |
| 37 Init(1, map, NULL); | |
| 38 } | |
| 39 int num_nodes() { return (int) inode_pool_.size(); } | |
| 40 }; | |
| 41 | |
| 42 } // namespace | 33 } // namespace |
| 43 | 34 |
| 44 #define NULL_NODE ((MountNode*) NULL) | |
| 45 | |
| 46 TEST(MountTest, Sanity) { | 35 TEST(MountTest, Sanity) { |
| 47 MountMemMock mnt; | 36 MountMemMock mnt; |
| 48 | 37 |
| 49 ScopedMountNode file; | 38 ScopedMountNode file; |
| 50 ScopedMountNode root; | 39 ScopedMountNode root; |
| 51 ScopedMountNode result_node; | 40 ScopedMountNode result_node; |
| 52 | 41 |
| 53 size_t result_size = 0; | 42 size_t result_size = 0; |
| 54 int result_bytes = 0; | 43 int result_bytes = 0; |
| 55 char buf1[1024]; | 44 char buf1[1024]; |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 double chi_squared = 0; | 284 double chi_squared = 0; |
| 296 for (int i = 0; i < 256; ++i) { | 285 for (int i = 0; i < 256; ++i) { |
| 297 double difference = byte_count[i] - expected_count; | 286 double difference = byte_count[i] - expected_count; |
| 298 chi_squared += difference * difference / expected_count; | 287 chi_squared += difference * difference / expected_count; |
| 299 } | 288 } |
| 300 | 289 |
| 301 // Approximate chi-squared value for p-value 0.05, 255 degrees-of-freedom. | 290 // Approximate chi-squared value for p-value 0.05, 255 degrees-of-freedom. |
| 302 EXPECT_LE(chi_squared, 293.24); | 291 EXPECT_LE(chi_squared, 293.24); |
| 303 } | 292 } |
| 304 | 293 |
| 305 class TtyTest : public ::testing::Test { | |
| 306 public: | |
| 307 void SetUp() { | |
| 308 ASSERT_EQ(0, mnt_.Access(Path("/tty"), R_OK | W_OK)); | |
| 309 ASSERT_EQ(EACCES, mnt_.Access(Path("/tty"), X_OK)); | |
| 310 ASSERT_EQ(0, mnt_.Open(Path("/tty"), O_RDWR, &dev_tty_)); | |
| 311 ASSERT_NE(NULL_NODE, dev_tty_.get()); | |
| 312 } | |
| 313 | |
| 314 protected: | |
| 315 MountDevMock mnt_; | |
| 316 ScopedMountNode dev_tty_; | |
| 317 }; | |
| 318 | |
| 319 TEST_F(TtyTest, DevTty) { | |
| 320 // 123 is not a valid ioctl request. | |
| 321 EXPECT_EQ(EINVAL, dev_tty_->Ioctl(123, NULL)); | |
| 322 | |
| 323 // TIOCNACLPREFIX is, it should set the prefix. | |
| 324 std::string prefix("__my_awesome_prefix__"); | |
| 325 EXPECT_EQ(0, dev_tty_->Ioctl(TIOCNACLPREFIX, | |
| 326 const_cast<char*>(prefix.c_str()))); | |
| 327 | |
| 328 // Now let's try sending some data over. | |
| 329 // First we create the message. | |
| 330 std::string content("hello, how are you?\n"); | |
| 331 std::string message = prefix.append(content); | |
| 332 struct tioc_nacl_input_string packaged_message; | |
| 333 packaged_message.length = message.size(); | |
| 334 packaged_message.buffer = message.data(); | |
| 335 | |
| 336 // Now we make buffer we'll read into. | |
| 337 // We fill the buffer and a backup buffer with arbitrary data | |
| 338 // and compare them after reading to make sure read doesn't | |
| 339 // clobber parts of the buffer it shouldn't. | |
| 340 int bytes_read; | |
| 341 char buffer[100]; | |
| 342 char backup_buffer[100]; | |
| 343 memset(buffer, 'a', 100); | |
| 344 memset(backup_buffer, 'a', 100); | |
| 345 | |
| 346 // Now we actually send the data | |
| 347 EXPECT_EQ(0, dev_tty_->Ioctl(TIOCNACLINPUT, | |
| 348 reinterpret_cast<char*>(&packaged_message))); | |
| 349 | |
| 350 // We read a small chunk first to ensure it doesn't give us | |
| 351 // more than we ask for. | |
| 352 EXPECT_EQ(0, dev_tty_->Read(0, buffer, 5, &bytes_read)); | |
| 353 EXPECT_EQ(bytes_read, 5); | |
| 354 EXPECT_EQ(0, memcmp(content.data(), buffer, 5)); | |
| 355 EXPECT_EQ(0, memcmp(buffer + 5, backup_buffer + 5, 95)); | |
| 356 | |
| 357 // Now we ask for more data than is left in the tty, to ensure | |
| 358 // it doesn't give us more than is there. | |
| 359 EXPECT_EQ(0, dev_tty_->Read(0, buffer + 5, 95, &bytes_read)); | |
| 360 EXPECT_EQ(bytes_read, content.size() - 5); | |
| 361 EXPECT_EQ(0, memcmp(content.data(), buffer, content.size())); | |
| 362 EXPECT_EQ(0, memcmp(buffer + content.size(), | |
| 363 backup_buffer + content.size(), | |
| 364 100 - content.size())); | |
| 365 | |
| 366 // Now we try to send something with an invalid prefix | |
| 367 std::string bogus_message("Woah there, this message has no valid prefix"); | |
| 368 struct tioc_nacl_input_string bogus_pack; | |
| 369 bogus_pack.length = bogus_message.size(); | |
| 370 bogus_pack.buffer = bogus_message.data(); | |
| 371 EXPECT_EQ(ENOTTY, dev_tty_->Ioctl(TIOCNACLINPUT, | |
| 372 reinterpret_cast<char*>(&bogus_pack))); | |
| 373 } | |
| OLD | NEW |