Index: native_client_sdk/src/tests/nacl_io_test/googledrivefs_test.cc |
diff --git a/native_client_sdk/src/tests/nacl_io_test/googledrivefs_test.cc b/native_client_sdk/src/tests/nacl_io_test/googledrivefs_test.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0e6f7cb0a620c658cc5a3fcc87b4308e03e1b360 |
--- /dev/null |
+++ b/native_client_sdk/src/tests/nacl_io_test/googledrivefs_test.cc |
@@ -0,0 +1,776 @@ |
+// Copyright 2016 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 <set> |
+#include <string> |
+ |
+#include <gmock/gmock.h> |
+ |
+#include "nacl_io/googledrivefs/googledrivefs.h" |
+#include "nacl_io/kernel_handle.h" |
+#include "nacl_io/osdirent.h" |
+#include "nacl_io/pepper_interface_delegate.h" |
+#include "sdk_util/scoped_ref.h" |
+ |
+#include "fake_ppapi/fake_pepper_interface_googledrivefs.h" |
+#include "fake_ppapi/fake_util.h" |
+#include "mock_util.h" |
+ |
+using namespace nacl_io; |
+using namespace sdk_util; |
+ |
+using ::testing::_; |
+using ::testing::DoAll; |
+using ::testing::Invoke; |
+using ::testing::Return; |
+ |
+namespace { |
+ |
+class GoogleDriveFsForTesting : public GoogleDriveFs { |
+ public: |
+ GoogleDriveFsForTesting(StringMap_t& string_map, PepperInterface* ppapi) { |
+ FsInitArgs args; |
+ args.string_map = string_map; |
+ args.ppapi = ppapi; |
+ Error error = Init(args); |
+ EXPECT_EQ(0, error); |
+ } |
+}; |
+ |
+class GoogleDriveFsTest : public ::testing::Test { |
+ public: |
+ GoogleDriveFsTest(); |
+ |
+ virtual void SetUp() { map_["token"] = "some token value"; } |
+ |
+ protected: |
+ FakePepperInterfaceGoogleDriveFs ppapi_googledrivefs_; |
+ PepperInterfaceDelegate ppapi_; |
+ StringMap_t map_; |
+}; |
+ |
+GoogleDriveFsTest::GoogleDriveFsTest() |
+ : ppapi_(ppapi_googledrivefs_.GetInstance()) { |
+ // Default delegation to the googledrivefs pepper interface. |
+ ppapi_.SetCoreInterfaceDelegate(ppapi_googledrivefs_.GetCoreInterface()); |
+ ppapi_.SetURLLoaderInterfaceDelegate( |
+ ppapi_googledrivefs_.GetURLLoaderInterface()); |
+ ppapi_.SetURLRequestInfoInterfaceDelegate( |
+ ppapi_googledrivefs_.GetURLRequestInfoInterface()); |
+ ppapi_.SetURLResponseInfoInterfaceDelegate( |
+ ppapi_googledrivefs_.GetURLResponseInfoInterface()); |
+ ppapi_.SetFileIoInterfaceDelegate(ppapi_googledrivefs_.GetFileIoInterface()); |
+ ppapi_.SetVarInterfaceDelegate(ppapi_googledrivefs_.GetVarInterface()); |
+} |
+ |
+} // namespace |
+ |
+TEST_F(GoogleDriveFsTest, Mkdir) { |
+ ScopedRef<GoogleDriveFsForTesting> fs( |
+ new GoogleDriveFsForTesting(map_, &ppapi_)); |
+ |
+ // |
+ // mkdir at the root should return EEXIST, not EACCES. |
+ // |
+ EXPECT_EQ(EEXIST, fs->Mkdir(Path("/"), 0644)); |
+ |
+ // |
+ // mkdir on a directory that has not been existed. |
+ // |
+ Path path("/foo"); |
+ |
+ ASSERT_EQ(0, fs->Mkdir(path, 0644)); |
+ |
+ ScopedNode node; |
+ ASSERT_EQ(0, fs->Open(path, O_RDONLY, &node)); |
+ |
+ EXPECT_TRUE(node->IsaDir()); |
+ |
+ // |
+ // mkdir on a directory that has already been existed. |
+ // |
+ EXPECT_EQ(EEXIST, fs->Mkdir(path, 0644)); |
+ |
+ // |
+ // mkdir on an existing file. |
+ // |
+ ScopedNode file_node; |
+ Path file_path("/filename"); |
+ ASSERT_EQ(0, fs->Open(file_path, O_WRONLY | O_CREAT | O_TRUNC, &file_node)); |
+ |
+ EXPECT_EQ(EEXIST, fs->Mkdir(file_path, 0644)); |
+} |
+ |
+TEST_F(GoogleDriveFsTest, Rmdir) { |
+ ScopedRef<GoogleDriveFsForTesting> fs( |
+ new GoogleDriveFsForTesting(map_, &ppapi_)); |
+ // |
+ // rmdir at the root returns EEXIST, as returned in nacl_io_demo. |
+ // |
+ EXPECT_EQ(EEXIST, fs->Rmdir(Path("/"))); |
+ |
+ // |
+ // rmdir on an existing dir. |
+ // |
+ Path dir_path("/dir"); |
+ |
+ ASSERT_EQ(0, fs->Mkdir(dir_path, 0644)); |
+ EXPECT_EQ(0, fs->Rmdir(dir_path)); |
+ |
+ // |
+ // rmdir on a non-existing dir. |
+ // |
+ EXPECT_EQ(ENOENT, fs->Rmdir(dir_path)); |
+ |
+ // |
+ // rmdir on an existing file. |
+ // |
+ ScopedNode file_node; |
+ Path file_path("/filename"); |
+ ASSERT_EQ(0, fs->Open(file_path, O_RDWR | O_CREAT | O_TRUNC, &file_node)); |
+ |
+ ASSERT_EQ(ENOTDIR, fs->Rmdir(file_path)); |
+ |
+ // |
+ // rmdir on an existing dir that has a file. |
+ // |
+ Path nonempty_dir_path("/nonempty_dir"); |
+ ASSERT_EQ(0, fs->Mkdir(nonempty_dir_path, 0644)); |
+ |
+ ScopedNode file_in_nonempty_dir_node; |
+ Path file_in_nonempty_dir_path("/nonempty_dir/file"); |
+ ASSERT_EQ(0, fs->Open(file_in_nonempty_dir_path, O_RDWR | O_CREAT | O_TRUNC, |
+ &file_in_nonempty_dir_node)); |
+ |
+ EXPECT_EQ(ENOTEMPTY, fs->Rmdir(nonempty_dir_path)); |
+} |
+ |
+TEST_F(GoogleDriveFsTest, OpenNonExistingFile) { |
+ ScopedRef<GoogleDriveFsForTesting> fs( |
+ new GoogleDriveFsForTesting(map_, &ppapi_)); |
+ |
+ // |
+ // open a non-existing file in r mode. |
+ // |
+ Path nonexisting_file_path("/file"); |
+ ScopedNode nonexisting_file_node; |
+ EXPECT_EQ(ENOENT, |
+ fs->Open(nonexisting_file_path, O_RDONLY, &nonexisting_file_node)); |
+ |
+ // |
+ // open a non-existing file in r+ mode. |
+ // |
+ EXPECT_EQ(ENOENT, |
+ fs->Open(nonexisting_file_path, O_RDWR, &nonexisting_file_node)); |
+ |
+ // |
+ // open a non-existing file in w mode. |
+ // |
+ Path nonexisting_file_w_mode_path("/nonexisting_file_w_mode"); |
+ ScopedNode nonexisting_file_w_mode_node; |
+ EXPECT_EQ(0, |
+ fs->Open(nonexisting_file_w_mode_path, O_WRONLY | O_CREAT | O_TRUNC, |
+ &nonexisting_file_w_mode_node)); |
+ |
+ // |
+ // open a non-existing file in w+ mode. |
+ // |
+ Path nonexisting_file_w_plus_mode_path("/nonexisting_file_w_plus_mode"); |
+ ScopedNode nonexisting_file_w_plus_mode_node; |
+ EXPECT_EQ( |
+ 0, fs->Open(nonexisting_file_w_plus_mode_path, O_RDWR | O_CREAT | O_TRUNC, |
+ &nonexisting_file_w_plus_mode_node)); |
+ |
+ // |
+ // open a non-existing file in a mode. |
+ // |
+ Path nonexisting_file_a_mode_path("/nonexisting_file_a_mode"); |
+ ScopedNode nonexisting_file_a_mode_node; |
+ EXPECT_EQ( |
+ 0, fs->Open(nonexisting_file_a_mode_path, O_WRONLY | O_CREAT | O_APPEND, |
+ &nonexisting_file_a_mode_node)); |
+ |
+ // |
+ // open a non-existing file in a+ mode. |
+ // |
+ Path nonexisting_file_a_plus_mode_path("/nonexisting_file_a_plus_mode"); |
+ ScopedNode nonexisting_file_a_plus_mode_node; |
+ EXPECT_EQ(0, fs->Open(nonexisting_file_a_plus_mode_path, |
+ O_RDWR | O_CREAT | O_APPEND, |
+ &nonexisting_file_a_plus_mode_node)); |
+} |
+ |
+TEST_F(GoogleDriveFsTest, OpenExistingFile) { |
+ ScopedRef<GoogleDriveFsForTesting> fs( |
+ new GoogleDriveFsForTesting(map_, &ppapi_)); |
+ |
+ Path existing_file_path("/file"); |
+ ScopedNode existing_file_node; |
+ EXPECT_EQ(0, fs->Open(existing_file_path, O_WRONLY | O_CREAT | O_TRUNC, |
+ &existing_file_node)); |
+ |
+ // |
+ // open an existing file in r mode. |
+ // |
+ ScopedNode existing_file_r_mode_node; |
+ EXPECT_EQ(0, |
+ fs->Open(existing_file_path, O_RDONLY, &existing_file_r_mode_node)); |
+ |
+ // |
+ // open an existing file in r+ mode. |
+ // |
+ ScopedNode existing_file_r_plus_mode_node; |
+ EXPECT_EQ( |
+ 0, fs->Open(existing_file_path, O_RDWR, &existing_file_r_plus_mode_node)); |
+ |
+ // |
+ // open an existing file in w mode. |
+ // |
+ ScopedNode existing_file_w_mode_node; |
+ EXPECT_EQ(0, fs->Open(existing_file_path, O_WRONLY | O_CREAT | O_TRUNC, |
+ &existing_file_w_mode_node)); |
+ |
+ // |
+ // open an existing file in w+ mode. |
+ // |
+ ScopedNode existing_file_w_plus_mode_node; |
+ EXPECT_EQ(0, fs->Open(existing_file_path, O_RDWR | O_CREAT | O_TRUNC, |
+ &existing_file_w_plus_mode_node)); |
+ |
+ // |
+ // open an existing file in a mode. |
+ // |
+ ScopedNode existing_file_a_mode_node; |
+ EXPECT_EQ(0, fs->Open(existing_file_path, O_WRONLY | O_CREAT | O_APPEND, |
+ &existing_file_a_mode_node)); |
+ |
+ // |
+ // open an existing file in a+ mode. |
+ // |
+ ScopedNode existing_file_a_plus_mode_node; |
+ EXPECT_EQ(0, fs->Open(existing_file_path, O_RDWR | O_CREAT | O_APPEND, |
+ &existing_file_a_plus_mode_node)); |
+} |
+ |
+TEST_F(GoogleDriveFsTest, OpenOtherCases) { |
+ ScopedRef<GoogleDriveFsForTesting> fs( |
+ new GoogleDriveFsForTesting(map_, &ppapi_)); |
+ |
+ // |
+ // A file opened without O_TRUNC flag does not truncate data. |
+ // |
+ Path file_path("/file"); |
+ ScopedNode file_node; |
+ EXPECT_EQ(0, fs->Open(file_path, O_WRONLY | O_CREAT | O_APPEND, &file_node)); |
+ |
+ // Write some data. |
+ const char* contents = "contents"; |
+ int bytes_written; |
+ EXPECT_EQ(0, file_node->Write(HandleAttr(), contents, strlen(contents), |
+ &bytes_written)); |
+ EXPECT_EQ(strlen(contents), bytes_written); |
+ |
+ // Create again. |
+ EXPECT_EQ(0, fs->Open(file_path, O_WRONLY | O_CREAT | O_APPEND, &file_node)); |
+ |
+ // Check that the file still has data. |
+ off_t size; |
+ EXPECT_EQ(0, file_node->GetSize(&size)); |
+ EXPECT_EQ(strlen(contents), size); |
+ |
+ // |
+ // A file opened with O_TRUNC flag truncates data. |
+ // |
+ // Create again. |
+ EXPECT_EQ(0, fs->Open(file_path, O_WRONLY | O_CREAT | O_TRUNC, &file_node)); |
+ |
+ // File should be empty. |
+ EXPECT_EQ(0, file_node->GetSize(&size)); |
+ EXPECT_EQ(0, size); |
+} |
+ |
+TEST_F(GoogleDriveFsTest, ReadContents) { |
+ ScopedRef<GoogleDriveFsForTesting> fs( |
+ new GoogleDriveFsForTesting(map_, &ppapi_)); |
+ |
+ Path file_path("/file"); |
+ ScopedNode file_node; |
+ EXPECT_EQ(0, fs->Open(file_path, O_WRONLY | O_CREAT | O_APPEND, &file_node)); |
+ |
+ const char* contents = "contents"; |
+ int bytes_written; |
+ EXPECT_EQ(0, file_node->Write(HandleAttr(), contents, strlen(contents), |
+ &bytes_written)); |
+ EXPECT_EQ(strlen(contents), bytes_written); |
+ |
+ ScopedNode node; |
+ ASSERT_EQ(0, fs->Open(file_path, O_RDONLY, &node)); |
+ |
+ // |
+ // Read nothing past the end of the file. |
+ // |
+ char buffer[10] = {0}; |
+ int bytes_read; |
+ HandleAttr attr; |
+ attr.offs = 100; |
+ ASSERT_EQ(0, node->Read(attr, &buffer[0], sizeof(buffer), &bytes_read)); |
+ ASSERT_EQ(0, bytes_read); |
+ |
+ // |
+ // Read from the beginning of the file. Buffer size > file size. |
+ // |
+ attr.offs = 0; |
+ ASSERT_EQ(0, node->Read(attr, &buffer[0], sizeof(buffer), &bytes_read)); |
+ ASSERT_EQ(strlen(contents), bytes_read); |
+ ASSERT_STREQ(contents, buffer); |
+ |
+ // |
+ // Read from the middle of the file. Buffer size > file size. |
+ // |
+ attr.offs = 4; |
+ ASSERT_EQ(0, node->Read(attr, &buffer[0], sizeof(buffer), &bytes_read)); |
+ ASSERT_EQ(strlen(contents) - 4, bytes_read); |
+ buffer[bytes_read] = 0; |
+ ASSERT_STREQ("ents", buffer); |
+ |
+ // |
+ // Read from the beginning of the file. Buffer size < file size. |
+ // |
+ char buffer_smaller_than_file[5]; |
+ attr.offs = 0; |
+ ASSERT_EQ(0, node->Read(attr, &buffer_smaller_than_file[0], |
+ sizeof(buffer_smaller_than_file), &bytes_read)); |
+ ASSERT_EQ(sizeof(buffer_smaller_than_file), bytes_read); |
+ ASSERT_EQ('c', buffer_smaller_than_file[0]); |
+ ASSERT_EQ('o', buffer_smaller_than_file[1]); |
+ ASSERT_EQ('n', buffer_smaller_than_file[2]); |
+ ASSERT_EQ('t', buffer_smaller_than_file[3]); |
+ ASSERT_EQ('e', buffer_smaller_than_file[4]); |
+ |
+ // |
+ // Read from the middle of the file. Buffer size < file size. |
+ // |
+ attr.offs = 1; |
+ ASSERT_EQ(0, node->Read(attr, &buffer_smaller_than_file[0], |
+ sizeof(buffer_smaller_than_file), &bytes_read)); |
+ ASSERT_EQ(sizeof(buffer_smaller_than_file), bytes_read); |
+ ASSERT_EQ('o', buffer_smaller_than_file[0]); |
+ ASSERT_EQ('n', buffer_smaller_than_file[1]); |
+ ASSERT_EQ('t', buffer_smaller_than_file[2]); |
+ ASSERT_EQ('e', buffer_smaller_than_file[3]); |
+ ASSERT_EQ('n', buffer_smaller_than_file[4]); |
+ |
+ // |
+ // Read from the beginning of the file. Buffer size == file size. |
+ // |
+ char equal_buffer_size[8]; |
+ attr.offs = 0; |
+ ASSERT_EQ(0, node->Read(attr, &equal_buffer_size[0], |
+ sizeof(equal_buffer_size), &bytes_read)); |
+ ASSERT_EQ(sizeof(equal_buffer_size), bytes_read); |
+ ASSERT_EQ('c', equal_buffer_size[0]); |
+ ASSERT_EQ('o', equal_buffer_size[1]); |
+ ASSERT_EQ('n', equal_buffer_size[2]); |
+ ASSERT_EQ('t', equal_buffer_size[3]); |
+ ASSERT_EQ('e', equal_buffer_size[4]); |
+ ASSERT_EQ('n', equal_buffer_size[5]); |
+ ASSERT_EQ('t', equal_buffer_size[6]); |
+ ASSERT_EQ('s', equal_buffer_size[7]); |
+} |
+ |
+TEST_F(GoogleDriveFsTest, ReadFailureCases) { |
+ ScopedRef<GoogleDriveFsForTesting> fs( |
+ new GoogleDriveFsForTesting(map_, &ppapi_)); |
+ |
+ Path file_path("/file"); |
+ ScopedNode file_node; |
+ EXPECT_EQ(0, fs->Open(file_path, O_WRONLY | O_CREAT | O_APPEND, &file_node)); |
+ |
+ const char* contents = "contents"; |
+ int bytes_written; |
+ EXPECT_EQ(0, file_node->Write(HandleAttr(), contents, strlen(contents), |
+ &bytes_written)); |
+ EXPECT_EQ(strlen(contents), bytes_written); |
+ |
+ ScopedNode node; |
+ ASSERT_EQ(0, fs->Open(file_path, O_WRONLY | O_CREAT | O_APPEND, &node)); |
+ |
+ // |
+ // Reading from a file opened as write-only should fail. |
+ // |
+ char buffer[10]; |
+ int bytes_read = 1; // Set to a non-zero value. |
+ HandleAttr attr; |
+ EXPECT_EQ(EACCES, node->Read(attr, &buffer[0], sizeof(buffer), &bytes_read)); |
+ EXPECT_EQ(0, bytes_read); |
+ |
+ // mkdir on a directory that has not been existed. |
+ Path dir_path("/dir"); |
+ ASSERT_EQ(0, fs->Mkdir(dir_path, 0644)); |
+ |
+ // |
+ // Reading from a directory should fail. |
+ // |
+ ScopedNode dir_node; |
+ ASSERT_EQ(0, fs->Open(dir_path, O_RDONLY, &dir_node)); |
+ ASSERT_EQ(EISDIR, |
+ dir_node->Read(attr, &buffer[0], sizeof(buffer), &bytes_read)); |
+} |
+ |
+TEST_F(GoogleDriveFsTest, WriteContents) { |
+ ScopedRef<GoogleDriveFsForTesting> fs( |
+ new GoogleDriveFsForTesting(map_, &ppapi_)); |
+ |
+ // |
+ // Write from the beginning of the file. |
+ // |
+ Path file_path("/file"); |
+ ScopedNode file_node; |
+ EXPECT_EQ(0, fs->Open(file_path, O_WRONLY | O_CREAT | O_APPEND, &file_node)); |
+ |
+ const char* contents = "contents"; |
+ int bytes_written; |
+ EXPECT_EQ(0, file_node->Write(HandleAttr(), contents, strlen(contents), |
+ &bytes_written)); |
+ ASSERT_EQ(strlen(contents), bytes_written); |
+ |
+ // Open as read-write. |
+ ScopedNode node; |
+ ASSERT_EQ(0, fs->Open(file_path, O_RDWR, &node)); |
+ |
+ // |
+ // Write from the middle of the file. |
+ // |
+ bytes_written = 1; // Set to a non-zero value. |
+ HandleAttr attr; |
+ attr.offs = 3; |
+ EXPECT_EQ(0, node->Write(attr, "struct", 6, &bytes_written)); |
+ EXPECT_EQ(6, bytes_written); |
+ |
+ char buffer[10]; |
+ attr.offs = 0; |
+ int bytes_read; |
+ EXPECT_EQ(0, node->Read(attr, &buffer[0], sizeof(buffer), &bytes_read)); |
+ EXPECT_EQ(9, bytes_read); |
+ buffer[bytes_read] = 0; |
+ EXPECT_STREQ("construct", buffer); |
+} |
+ |
+TEST_F(GoogleDriveFsTest, WriteFailureCases) { |
+ ScopedRef<GoogleDriveFsForTesting> fs( |
+ new GoogleDriveFsForTesting(map_, &ppapi_)); |
+ |
+ Path file_path("/file"); |
+ ScopedNode file_node; |
+ EXPECT_EQ(0, fs->Open(file_path, O_WRONLY | O_CREAT | O_APPEND, &file_node)); |
+ |
+ const char* contents = "contents"; |
+ int bytes_written; |
+ EXPECT_EQ(0, file_node->Write(HandleAttr(), contents, strlen(contents), |
+ &bytes_written)); |
+ EXPECT_EQ(strlen(contents), bytes_written); |
+ |
+ ScopedNode node; |
+ ASSERT_EQ(0, fs->Open(file_path, O_RDONLY, &node)); |
+ |
+ // |
+ // Writing from a file opened as read-only should fail. |
+ // |
+ bytes_written = 1; // Set to a non-zero value. |
+ HandleAttr attr; |
+ attr.offs = 0; |
+ char buffer[10]; |
+ ASSERT_EQ(EACCES, |
+ node->Write(attr, &buffer[0], sizeof(buffer), &bytes_written)); |
+ ASSERT_EQ(0, bytes_written); |
+ |
+ // mkdir on a directory that has not been existed. |
+ Path dir_path("/dir"); |
+ ASSERT_EQ(0, fs->Mkdir(dir_path, 0644)); |
+ |
+ // |
+ // Writing to a directory should fail. |
+ // |
+ EXPECT_EQ(0, fs->Open(dir_path, O_RDONLY, &node)); |
+ int bytes_read; |
+ EXPECT_EQ(EISDIR, node->Write(attr, &buffer[0], sizeof(buffer), &bytes_read)); |
+} |
+ |
+TEST_F(GoogleDriveFsTest, GetStat) { |
+ ScopedRef<GoogleDriveFsForTesting> fs( |
+ new GoogleDriveFsForTesting(map_, &ppapi_)); |
+ |
+ // |
+ // GetStat on a file. |
+ // |
+ ScopedNode node; |
+ ASSERT_EQ(0, fs->Open(Path("/file"), O_WRONLY | O_CREAT | O_APPEND, &node)); |
+ |
+ const char* contents = "contents"; |
+ int bytes_written; |
+ EXPECT_EQ( |
+ 0, node->Write(HandleAttr(), contents, strlen(contents), &bytes_written)); |
+ ASSERT_EQ(strlen(contents), bytes_written); |
+ |
+ struct stat statbuf; |
+ EXPECT_EQ(0, node->GetStat(&statbuf)); |
+ EXPECT_TRUE(S_ISREG(statbuf.st_mode)); |
+ EXPECT_EQ(S_IWRITE, statbuf.st_mode & S_MODEBITS); |
+ EXPECT_EQ(strlen(contents), statbuf.st_size); |
+ EXPECT_EQ(0, statbuf.st_atime); |
+ EXPECT_EQ(0, statbuf.st_ctime); |
+ |
+ // UTC time "2016-07-24T08:21:28.940Z" specified at each file |
+ // in FakeGoogleDriveServer is equal to the UNIX timestamp 1469348488 |
+ EXPECT_EQ(1469348488, statbuf.st_mtime); |
+ |
+ // Test Get* and Isa* methods. |
+ off_t size; |
+ EXPECT_EQ(0, node->GetSize(&size)); |
+ EXPECT_EQ(strlen(contents), size); |
+ EXPECT_FALSE(node->IsaDir()); |
+ EXPECT_TRUE(node->IsaFile()); |
+ EXPECT_EQ(ENOTTY, node->Isatty()); |
+ |
+ Path path("/dir"); |
+ ASSERT_EQ(0, fs->Mkdir(path, 0644)); |
+ |
+ // |
+ // GetStat on a directory. |
+ // |
+ EXPECT_EQ(0, fs->Open(path, O_RDONLY, &node)); |
+ EXPECT_EQ(0, node->GetStat(&statbuf)); |
+ EXPECT_TRUE(S_ISDIR(statbuf.st_mode)); |
+ EXPECT_EQ(S_IREAD, statbuf.st_mode & S_MODEBITS); |
+ EXPECT_EQ(0, statbuf.st_size); |
+ EXPECT_EQ(0, statbuf.st_atime); |
+ EXPECT_EQ(0, statbuf.st_ctime); |
+ |
+ // UTC time "2016-07-24T08:21:28.940Z" specified at each directory |
+ // in FakeGoogleDriveServer is equal to the UNIX timestamp 1469348488; |
+ EXPECT_EQ(1469348488, statbuf.st_mtime); |
+ |
+ // Test Get* and Isa* methods. |
+ EXPECT_EQ(0, node->GetSize(&size)); |
+ EXPECT_EQ(0, size); |
+ EXPECT_TRUE(node->IsaDir()); |
+ EXPECT_FALSE(node->IsaFile()); |
+ EXPECT_EQ(ENOTTY, node->Isatty()); |
+} |
+ |
+TEST_F(GoogleDriveFsTest, FTruncate) { |
+ ScopedRef<GoogleDriveFsForTesting> fs( |
+ new GoogleDriveFsForTesting(map_, &ppapi_)); |
+ |
+ ScopedNode node; |
+ ASSERT_EQ(0, fs->Open(Path("/file"), O_RDWR | O_CREAT | O_TRUNC, &node)); |
+ const char* contents = "contents"; |
+ int bytes_written; |
+ EXPECT_EQ( |
+ 0, node->Write(HandleAttr(), contents, strlen(contents), &bytes_written)); |
+ ASSERT_EQ(strlen(contents), bytes_written); |
+ |
+ HandleAttr attr; |
+ char buffer[10] = {0}; |
+ int bytes_read; |
+ |
+ // |
+ // First make the file shorter... |
+ // |
+ EXPECT_EQ(0, node->FTruncate(4)); |
+ EXPECT_EQ(0, node->Read(attr, &buffer[0], sizeof(buffer), &bytes_read)); |
+ EXPECT_EQ(4, bytes_read); |
+ buffer[bytes_read] = 0; |
+ EXPECT_STREQ("cont", buffer); |
+ |
+ // |
+ // Now make the file longer... |
+ // |
+ EXPECT_EQ(0, node->FTruncate(8)); |
+ EXPECT_EQ(0, node->Read(attr, &buffer[0], sizeof(buffer), &bytes_read)); |
+ EXPECT_EQ(8, bytes_read); |
+ buffer[bytes_read] = 0; |
+ EXPECT_STREQ("cont\0\0\0\0", buffer); |
+ |
+ Path path("/dir"); |
+ ASSERT_EQ(0, fs->Mkdir(path, 0644)); |
+ |
+ // |
+ // Ftruncate should fail for a directory. |
+ // |
+ EXPECT_EQ(0, fs->Open(path, O_RDONLY, &node)); |
+ EXPECT_EQ(EISDIR, node->FTruncate(4)); |
+} |
+ |
+TEST_F(GoogleDriveFsTest, OpenDirectory) { |
+ ScopedRef<GoogleDriveFsForTesting> fs( |
+ new GoogleDriveFsForTesting(map_, &ppapi_)); |
+ |
+ Path dir_path("/dir"); |
+ ASSERT_EQ(0, fs->Mkdir(dir_path, 0644)); |
+ |
+ // |
+ // Open a directory with r mode should succeed. |
+ // |
+ ScopedNode node; |
+ ASSERT_EQ(0, fs->Open(dir_path, O_RDONLY, &node)); |
+ |
+ // |
+ // Open a directory with r+, w, w+, a, and a+ mode should fail. |
+ // |
+ EXPECT_EQ(EPERM, fs->Open(dir_path, O_RDWR, &node)); |
+ EXPECT_EQ(EPERM, fs->Open(dir_path, O_WRONLY | O_CREAT | O_TRUNC, &node)); |
+ EXPECT_EQ(EPERM, fs->Open(dir_path, O_RDWR | O_CREAT | O_TRUNC, &node)); |
+ EXPECT_EQ(EPERM, fs->Open(dir_path, O_WRONLY | O_CREAT | O_APPEND, &node)); |
+ EXPECT_EQ(EPERM, fs->Open(dir_path, O_RDWR | O_CREAT | O_APPEND, &node)); |
+ |
+ // |
+ // Opening a non-existing directory should fail. |
+ // |
+ EXPECT_EQ(ENOENT, fs->Open(Path("/nonexistingdir"), O_RDONLY, &node)); |
+} |
+ |
+TEST_F(GoogleDriveFsTest, GetDents) { |
+ ScopedRef<GoogleDriveFsForTesting> fs( |
+ new GoogleDriveFsForTesting(map_, &ppapi_)); |
+ |
+ ScopedNode node; |
+ Path file_path("/file"); |
+ ASSERT_EQ(0, fs->Open(file_path, O_WRONLY | O_CREAT | O_TRUNC, &node)); |
+ const char* contents = "contents"; |
+ int bytes_written; |
+ EXPECT_EQ( |
+ 0, node->Write(HandleAttr(), contents, strlen(contents), &bytes_written)); |
+ ASSERT_EQ(strlen(contents), bytes_written); |
+ |
+ ScopedNode root; |
+ ASSERT_EQ(0, fs->Open(Path("/"), O_RDONLY, &root)); |
+ |
+ struct stat stat; |
+ ASSERT_EQ(0, node->GetStat(&stat)); |
+ |
+ // |
+ // Should fail for regular files. |
+ // |
+ const size_t kMaxDirents = 5; |
+ dirent dirents[kMaxDirents]; |
+ int bytes_read = 1; // Set to a non-zero value. |
+ |
+ memset(&dirents[0], 0, sizeof(dirents)); |
+ EXPECT_EQ(ENOTDIR, |
+ node->GetDents(0, &dirents[0], sizeof(dirents), &bytes_read)); |
+ EXPECT_EQ(0, bytes_read); |
+ |
+ // |
+ // Should work with root directory. |
+ // |
+ // +2 to test a size that is not a multiple of sizeof(dirent). |
+ // Expect it to round down. |
+ memset(&dirents[0], 0, sizeof(dirents)); |
+ EXPECT_EQ( |
+ 0, root->GetDents(0, &dirents[0], sizeof(dirent) * 3 + 2, &bytes_read)); |
+ |
+ { |
+ size_t num_dirents = bytes_read / sizeof(dirent); |
+ EXPECT_EQ(3, num_dirents); |
+ EXPECT_EQ(sizeof(dirent) * num_dirents, bytes_read); |
+ |
+ std::multiset<std::string> dirnames; |
+ for (size_t i = 0; i < num_dirents; ++i) { |
+ EXPECT_EQ(sizeof(dirent), dirents[i].d_reclen); |
+ dirnames.insert(dirents[i].d_name); |
+ } |
+ |
+ EXPECT_EQ(1, dirnames.count("file")); |
+ EXPECT_EQ(1, dirnames.count(".")); |
+ EXPECT_EQ(1, dirnames.count("..")); |
+ } |
+ |
+ // Add another file... |
+ ASSERT_EQ(0, fs->Open(Path("/file2"), O_WRONLY | O_CREAT | O_TRUNC, &node)); |
+ ASSERT_EQ(0, node->GetStat(&stat)); |
+ |
+ // |
+ // Read the root directory again after adding another file. |
+ // |
+ memset(&dirents[0], 0, sizeof(dirents)); |
+ EXPECT_EQ(0, root->GetDents(0, &dirents[0], sizeof(dirents), &bytes_read)); |
+ |
+ { |
+ size_t num_dirents = bytes_read / sizeof(dirent); |
+ EXPECT_EQ(4, num_dirents); |
+ EXPECT_EQ(sizeof(dirent) * num_dirents, bytes_read); |
+ |
+ std::multiset<std::string> dirnames; |
+ for (size_t i = 0; i < num_dirents; ++i) { |
+ EXPECT_EQ(sizeof(dirent), dirents[i].d_reclen); |
+ dirnames.insert(dirents[i].d_name); |
+ } |
+ |
+ EXPECT_EQ(1, dirnames.count("file")); |
+ EXPECT_EQ(1, dirnames.count("file2")); |
+ EXPECT_EQ(1, dirnames.count(".")); |
+ EXPECT_EQ(1, dirnames.count("..")); |
+ } |
+ |
+ // |
+ // Read >100 entries. Multiple API requests are sent to request >100 entries. |
+ // |
+ const size_t kMaxDirectsMultipleRequests = 105; |
+ std::vector<dirent> direntsMultipleRequests; |
+ direntsMultipleRequests.resize(kMaxDirectsMultipleRequests); |
+ |
+ // Add 100 files... |
+ for (size_t i = 0; i < 100; ++i) { |
+ char buffer[4]; |
+ int char_written = snprintf(buffer, sizeof(buffer), "%u", i); |
+ |
+ // cast signed int on sizeof(..) but not signed long long int on |
chanpatorikku
2016/08/29 18:10:39
Sorry. The comments were not meant to be in the pa
|
+ // both operands because sizeof(buffer) <= INT_MAX. |
+ ASSERT_TRUE(char_written >= 0 && char_written < (signed int)sizeof(buffer)); |
+ |
+ ASSERT_EQ(0, fs->Open(Path("/anotherfile" + std::string(buffer)), |
+ O_WRONLY | O_CREAT | O_TRUNC, &node)); |
+ ASSERT_EQ(0, node->GetStat(&stat)); |
+ } |
+ |
+ EXPECT_EQ(0, root->GetDents(0, &direntsMultipleRequests[0], |
+ sizeof(dirent) * direntsMultipleRequests.size(), |
+ &bytes_read)); |
+ { |
+ size_t num_dirents = bytes_read / sizeof(dirent); |
+ EXPECT_EQ(104, num_dirents); |
+ EXPECT_EQ(sizeof(dirent) * num_dirents, bytes_read); |
+ |
+ std::multiset<std::string> dirnames; |
+ for (size_t i = 0; i < num_dirents; ++i) { |
+ EXPECT_EQ(sizeof(dirent), direntsMultipleRequests[i].d_reclen); |
+ dirnames.insert(direntsMultipleRequests[i].d_name); |
+ } |
+ |
+ for (size_t i = 0; i < 100; ++i) { |
+ char buffer[4]; |
+ int char_written = snprintf(buffer, sizeof(buffer), "%u", i); |
+ |
+ // cast signed int on sizeof(..) but not signed long long int on |
chanpatorikku
2016/08/29 18:10:39
Sorry. The comments were not meant to be in the pa
|
+ // both operands because sizeof(buffer) <= INT_MAX. |
+ ASSERT_TRUE(char_written >= 0 && |
+ char_written < (signed int)sizeof(buffer)); |
+ |
+ EXPECT_EQ(1, dirnames.count("anotherfile" + std::string(buffer))); |
+ } |
+ |
+ EXPECT_EQ(1, dirnames.count("file")); |
+ EXPECT_EQ(1, dirnames.count("file2")); |
+ EXPECT_EQ(1, dirnames.count(".")); |
+ EXPECT_EQ(1, dirnames.count("..")); |
+ } |
+} |
+ |
+// TODO: Add tests after Remove becomes supported. |
+// TODO: Add tests after Unlink becomes supported. |
+// TODO: Add tests after Rename becomes supported. |