Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <errno.h> | |
| 6 #include <fcntl.h> | |
| 7 | |
| 8 #include <set> | |
| 9 #include <string> | |
| 10 | |
| 11 #include <gmock/gmock.h> | |
| 12 | |
| 13 #include "nacl_io/googledrivefs/googledrivefs.h" | |
| 14 #include "nacl_io/kernel_handle.h" | |
| 15 #include "nacl_io/osdirent.h" | |
| 16 #include "nacl_io/pepper_interface_delegate.h" | |
| 17 #include "sdk_util/scoped_ref.h" | |
| 18 | |
| 19 #include "fake_ppapi/fake_pepper_interface_googledrivefs.h" | |
| 20 #include "fake_ppapi/fake_util.h" | |
| 21 #include "mock_util.h" | |
| 22 | |
| 23 using namespace nacl_io; | |
| 24 using namespace sdk_util; | |
| 25 | |
| 26 using ::testing::_; | |
| 27 using ::testing::DoAll; | |
| 28 using ::testing::Invoke; | |
| 29 using ::testing::Return; | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 33 class GoogleDriveFsForTesting : public GoogleDriveFs { | |
| 34 public: | |
| 35 GoogleDriveFsForTesting(StringMap_t& string_map, PepperInterface* ppapi) { | |
| 36 FsInitArgs args; | |
| 37 args.string_map = string_map; | |
| 38 args.ppapi = ppapi; | |
| 39 Error error = Init(args); | |
| 40 EXPECT_EQ(0, error); | |
| 41 } | |
| 42 }; | |
| 43 | |
| 44 class GoogleDriveFsTest : public ::testing::Test { | |
| 45 public: | |
| 46 GoogleDriveFsTest(); | |
| 47 | |
| 48 virtual void SetUp() { map_["token"] = "some token value"; } | |
| 49 | |
| 50 protected: | |
| 51 FakePepperInterfaceGoogleDriveFs ppapi_googledrivefs_; | |
| 52 PepperInterfaceDelegate ppapi_; | |
| 53 StringMap_t map_; | |
| 54 }; | |
| 55 | |
| 56 GoogleDriveFsTest::GoogleDriveFsTest() | |
| 57 : ppapi_(ppapi_googledrivefs_.GetInstance()) { | |
| 58 // Default delegation to the googledrivefs pepper interface. | |
| 59 ppapi_.SetCoreInterfaceDelegate(ppapi_googledrivefs_.GetCoreInterface()); | |
| 60 ppapi_.SetURLLoaderInterfaceDelegate( | |
| 61 ppapi_googledrivefs_.GetURLLoaderInterface()); | |
| 62 ppapi_.SetURLRequestInfoInterfaceDelegate( | |
| 63 ppapi_googledrivefs_.GetURLRequestInfoInterface()); | |
| 64 ppapi_.SetURLResponseInfoInterfaceDelegate( | |
| 65 ppapi_googledrivefs_.GetURLResponseInfoInterface()); | |
| 66 ppapi_.SetFileIoInterfaceDelegate(ppapi_googledrivefs_.GetFileIoInterface()); | |
| 67 ppapi_.SetVarInterfaceDelegate(ppapi_googledrivefs_.GetVarInterface()); | |
| 68 } | |
| 69 | |
| 70 } // namespace | |
| 71 | |
| 72 TEST_F(GoogleDriveFsTest, Mkdir) { | |
| 73 ScopedRef<GoogleDriveFsForTesting> fs( | |
| 74 new GoogleDriveFsForTesting(map_, &ppapi_)); | |
| 75 | |
| 76 // | |
| 77 // mkdir at the root should return EEXIST, not EACCES. | |
| 78 // | |
| 79 EXPECT_EQ(EEXIST, fs->Mkdir(Path("/"), 0644)); | |
| 80 | |
| 81 // | |
| 82 // mkdir on a directory that has not been existed. | |
| 83 // | |
| 84 Path path("/foo"); | |
| 85 | |
| 86 ASSERT_EQ(0, fs->Mkdir(path, 0644)); | |
| 87 | |
| 88 ScopedNode node; | |
| 89 ASSERT_EQ(0, fs->Open(path, O_RDONLY, &node)); | |
| 90 | |
| 91 EXPECT_TRUE(node->IsaDir()); | |
| 92 | |
| 93 // | |
| 94 // mkdir on a directory that has already been existed. | |
| 95 // | |
| 96 EXPECT_EQ(EEXIST, fs->Mkdir(path, 0644)); | |
| 97 | |
| 98 // | |
| 99 // mkdir on an existing file. | |
| 100 // | |
| 101 ScopedNode file_node; | |
| 102 Path file_path("/filename"); | |
| 103 ASSERT_EQ(0, fs->Open(file_path, O_WRONLY | O_CREAT | O_TRUNC, &file_node)); | |
| 104 | |
| 105 EXPECT_EQ(EEXIST, fs->Mkdir(file_path, 0644)); | |
| 106 } | |
| 107 | |
| 108 TEST_F(GoogleDriveFsTest, Rmdir) { | |
| 109 ScopedRef<GoogleDriveFsForTesting> fs( | |
| 110 new GoogleDriveFsForTesting(map_, &ppapi_)); | |
| 111 // | |
| 112 // rmdir at the root returns EEXIST, as returned in nacl_io_demo. | |
| 113 // | |
| 114 EXPECT_EQ(EEXIST, fs->Rmdir(Path("/"))); | |
| 115 | |
| 116 // | |
| 117 // rmdir on an existing dir. | |
| 118 // | |
| 119 Path dir_path("/dir"); | |
| 120 | |
| 121 ASSERT_EQ(0, fs->Mkdir(dir_path, 0644)); | |
| 122 EXPECT_EQ(0, fs->Rmdir(dir_path)); | |
| 123 | |
| 124 // | |
| 125 // rmdir on a non-existing dir. | |
| 126 // | |
| 127 EXPECT_EQ(ENOENT, fs->Rmdir(dir_path)); | |
| 128 | |
| 129 // | |
| 130 // rmdir on an existing file. | |
| 131 // | |
| 132 ScopedNode file_node; | |
| 133 Path file_path("/filename"); | |
| 134 ASSERT_EQ(0, fs->Open(file_path, O_RDWR | O_CREAT | O_TRUNC, &file_node)); | |
| 135 | |
| 136 ASSERT_EQ(ENOTDIR, fs->Rmdir(file_path)); | |
| 137 | |
| 138 // | |
| 139 // rmdir on an existing dir that has a file. | |
| 140 // | |
| 141 Path nonempty_dir_path("/nonempty_dir"); | |
| 142 ASSERT_EQ(0, fs->Mkdir(nonempty_dir_path, 0644)); | |
| 143 | |
| 144 ScopedNode file_in_nonempty_dir_node; | |
| 145 Path file_in_nonempty_dir_path("/nonempty_dir/file"); | |
| 146 ASSERT_EQ(0, fs->Open(file_in_nonempty_dir_path, O_RDWR | O_CREAT | O_TRUNC, | |
| 147 &file_in_nonempty_dir_node)); | |
| 148 | |
| 149 EXPECT_EQ(ENOTEMPTY, fs->Rmdir(nonempty_dir_path)); | |
| 150 } | |
| 151 | |
| 152 TEST_F(GoogleDriveFsTest, OpenNonExistingFile) { | |
| 153 ScopedRef<GoogleDriveFsForTesting> fs( | |
| 154 new GoogleDriveFsForTesting(map_, &ppapi_)); | |
| 155 | |
| 156 // | |
| 157 // open a non-existing file in r mode. | |
| 158 // | |
| 159 Path nonexisting_file_path("/file"); | |
| 160 ScopedNode nonexisting_file_node; | |
| 161 EXPECT_EQ(ENOENT, | |
| 162 fs->Open(nonexisting_file_path, O_RDONLY, &nonexisting_file_node)); | |
| 163 | |
| 164 // | |
| 165 // open a non-existing file in r+ mode. | |
| 166 // | |
| 167 EXPECT_EQ(ENOENT, | |
| 168 fs->Open(nonexisting_file_path, O_RDWR, &nonexisting_file_node)); | |
| 169 | |
| 170 // | |
| 171 // open a non-existing file in w mode. | |
| 172 // | |
| 173 Path nonexisting_file_w_mode_path("/nonexisting_file_w_mode"); | |
| 174 ScopedNode nonexisting_file_w_mode_node; | |
| 175 EXPECT_EQ(0, | |
| 176 fs->Open(nonexisting_file_w_mode_path, O_WRONLY | O_CREAT | O_TRUNC, | |
| 177 &nonexisting_file_w_mode_node)); | |
| 178 | |
| 179 // | |
| 180 // open a non-existing file in w+ mode. | |
| 181 // | |
| 182 Path nonexisting_file_w_plus_mode_path("/nonexisting_file_w_plus_mode"); | |
| 183 ScopedNode nonexisting_file_w_plus_mode_node; | |
| 184 EXPECT_EQ( | |
| 185 0, fs->Open(nonexisting_file_w_plus_mode_path, O_RDWR | O_CREAT | O_TRUNC, | |
| 186 &nonexisting_file_w_plus_mode_node)); | |
| 187 | |
| 188 // | |
| 189 // open a non-existing file in a mode. | |
| 190 // | |
| 191 Path nonexisting_file_a_mode_path("/nonexisting_file_a_mode"); | |
| 192 ScopedNode nonexisting_file_a_mode_node; | |
| 193 EXPECT_EQ( | |
| 194 0, fs->Open(nonexisting_file_a_mode_path, O_WRONLY | O_CREAT | O_APPEND, | |
| 195 &nonexisting_file_a_mode_node)); | |
| 196 | |
| 197 // | |
| 198 // open a non-existing file in a+ mode. | |
| 199 // | |
| 200 Path nonexisting_file_a_plus_mode_path("/nonexisting_file_a_plus_mode"); | |
| 201 ScopedNode nonexisting_file_a_plus_mode_node; | |
| 202 EXPECT_EQ(0, fs->Open(nonexisting_file_a_plus_mode_path, | |
| 203 O_RDWR | O_CREAT | O_APPEND, | |
| 204 &nonexisting_file_a_plus_mode_node)); | |
| 205 } | |
| 206 | |
| 207 TEST_F(GoogleDriveFsTest, OpenExistingFile) { | |
| 208 ScopedRef<GoogleDriveFsForTesting> fs( | |
| 209 new GoogleDriveFsForTesting(map_, &ppapi_)); | |
| 210 | |
| 211 Path existing_file_path("/file"); | |
| 212 ScopedNode existing_file_node; | |
| 213 EXPECT_EQ(0, fs->Open(existing_file_path, O_WRONLY | O_CREAT | O_TRUNC, | |
| 214 &existing_file_node)); | |
| 215 | |
| 216 // | |
| 217 // open an existing file in r mode. | |
| 218 // | |
| 219 ScopedNode existing_file_r_mode_node; | |
| 220 EXPECT_EQ(0, | |
| 221 fs->Open(existing_file_path, O_RDONLY, &existing_file_r_mode_node)); | |
| 222 | |
| 223 // | |
| 224 // open an existing file in r+ mode. | |
| 225 // | |
| 226 ScopedNode existing_file_r_plus_mode_node; | |
| 227 EXPECT_EQ( | |
| 228 0, fs->Open(existing_file_path, O_RDWR, &existing_file_r_plus_mode_node)); | |
| 229 | |
| 230 // | |
| 231 // open an existing file in w mode. | |
| 232 // | |
| 233 ScopedNode existing_file_w_mode_node; | |
| 234 EXPECT_EQ(0, fs->Open(existing_file_path, O_WRONLY | O_CREAT | O_TRUNC, | |
| 235 &existing_file_w_mode_node)); | |
| 236 | |
| 237 // | |
| 238 // open an existing file in w+ mode. | |
| 239 // | |
| 240 ScopedNode existing_file_w_plus_mode_node; | |
| 241 EXPECT_EQ(0, fs->Open(existing_file_path, O_RDWR | O_CREAT | O_TRUNC, | |
| 242 &existing_file_w_plus_mode_node)); | |
| 243 | |
| 244 // | |
| 245 // open an existing file in a mode. | |
| 246 // | |
| 247 ScopedNode existing_file_a_mode_node; | |
| 248 EXPECT_EQ(0, fs->Open(existing_file_path, O_WRONLY | O_CREAT | O_APPEND, | |
| 249 &existing_file_a_mode_node)); | |
| 250 | |
| 251 // | |
| 252 // open an existing file in a+ mode. | |
| 253 // | |
| 254 ScopedNode existing_file_a_plus_mode_node; | |
| 255 EXPECT_EQ(0, fs->Open(existing_file_path, O_RDWR | O_CREAT | O_APPEND, | |
| 256 &existing_file_a_plus_mode_node)); | |
| 257 } | |
| 258 | |
| 259 TEST_F(GoogleDriveFsTest, OpenOtherCases) { | |
| 260 ScopedRef<GoogleDriveFsForTesting> fs( | |
| 261 new GoogleDriveFsForTesting(map_, &ppapi_)); | |
| 262 | |
| 263 // | |
| 264 // A file opened without O_TRUNC flag does not truncate data. | |
| 265 // | |
| 266 Path file_path("/file"); | |
| 267 ScopedNode file_node; | |
| 268 EXPECT_EQ(0, fs->Open(file_path, O_WRONLY | O_CREAT | O_APPEND, &file_node)); | |
| 269 | |
| 270 // Write some data. | |
| 271 const char* contents = "contents"; | |
| 272 int bytes_written; | |
| 273 EXPECT_EQ(0, file_node->Write(HandleAttr(), contents, strlen(contents), | |
| 274 &bytes_written)); | |
| 275 EXPECT_EQ(strlen(contents), bytes_written); | |
| 276 | |
| 277 // Create again. | |
| 278 EXPECT_EQ(0, fs->Open(file_path, O_WRONLY | O_CREAT | O_APPEND, &file_node)); | |
| 279 | |
| 280 // Check that the file still has data. | |
| 281 off_t size; | |
| 282 EXPECT_EQ(0, file_node->GetSize(&size)); | |
| 283 EXPECT_EQ(strlen(contents), size); | |
| 284 | |
| 285 // | |
| 286 // A file opened with O_TRUNC flag truncates data. | |
| 287 // | |
| 288 // Create again. | |
| 289 EXPECT_EQ(0, fs->Open(file_path, O_WRONLY | O_CREAT | O_TRUNC, &file_node)); | |
| 290 | |
| 291 // File should be empty. | |
| 292 EXPECT_EQ(0, file_node->GetSize(&size)); | |
| 293 EXPECT_EQ(0, size); | |
| 294 } | |
| 295 | |
| 296 TEST_F(GoogleDriveFsTest, ReadContents) { | |
| 297 ScopedRef<GoogleDriveFsForTesting> fs( | |
| 298 new GoogleDriveFsForTesting(map_, &ppapi_)); | |
| 299 | |
| 300 Path file_path("/file"); | |
| 301 ScopedNode file_node; | |
| 302 EXPECT_EQ(0, fs->Open(file_path, O_WRONLY | O_CREAT | O_APPEND, &file_node)); | |
| 303 | |
| 304 const char* contents = "contents"; | |
| 305 int bytes_written; | |
| 306 EXPECT_EQ(0, file_node->Write(HandleAttr(), contents, strlen(contents), | |
| 307 &bytes_written)); | |
| 308 EXPECT_EQ(strlen(contents), bytes_written); | |
| 309 | |
| 310 ScopedNode node; | |
| 311 ASSERT_EQ(0, fs->Open(file_path, O_RDONLY, &node)); | |
| 312 | |
| 313 // | |
| 314 // Read nothing past the end of the file. | |
| 315 // | |
| 316 char buffer[10] = {0}; | |
| 317 int bytes_read; | |
| 318 HandleAttr attr; | |
| 319 attr.offs = 100; | |
| 320 ASSERT_EQ(0, node->Read(attr, &buffer[0], sizeof(buffer), &bytes_read)); | |
| 321 ASSERT_EQ(0, bytes_read); | |
| 322 | |
| 323 // | |
| 324 // Read from the beginning of the file. Buffer size > file size. | |
| 325 // | |
| 326 attr.offs = 0; | |
| 327 ASSERT_EQ(0, node->Read(attr, &buffer[0], sizeof(buffer), &bytes_read)); | |
| 328 ASSERT_EQ(strlen(contents), bytes_read); | |
| 329 ASSERT_STREQ(contents, buffer); | |
| 330 | |
| 331 // | |
| 332 // Read from the middle of the file. Buffer size > file size. | |
| 333 // | |
| 334 attr.offs = 4; | |
| 335 ASSERT_EQ(0, node->Read(attr, &buffer[0], sizeof(buffer), &bytes_read)); | |
| 336 ASSERT_EQ(strlen(contents) - 4, bytes_read); | |
| 337 buffer[bytes_read] = 0; | |
| 338 ASSERT_STREQ("ents", buffer); | |
| 339 | |
| 340 // | |
| 341 // Read from the beginning of the file. Buffer size < file size. | |
| 342 // | |
| 343 char buffer_smaller_than_file[5]; | |
| 344 attr.offs = 0; | |
| 345 ASSERT_EQ(0, node->Read(attr, &buffer_smaller_than_file[0], | |
| 346 sizeof(buffer_smaller_than_file), &bytes_read)); | |
| 347 ASSERT_EQ(sizeof(buffer_smaller_than_file), bytes_read); | |
| 348 ASSERT_EQ('c', buffer_smaller_than_file[0]); | |
| 349 ASSERT_EQ('o', buffer_smaller_than_file[1]); | |
| 350 ASSERT_EQ('n', buffer_smaller_than_file[2]); | |
| 351 ASSERT_EQ('t', buffer_smaller_than_file[3]); | |
| 352 ASSERT_EQ('e', buffer_smaller_than_file[4]); | |
| 353 | |
| 354 // | |
| 355 // Read from the middle of the file. Buffer size < file size. | |
| 356 // | |
| 357 attr.offs = 1; | |
| 358 ASSERT_EQ(0, node->Read(attr, &buffer_smaller_than_file[0], | |
| 359 sizeof(buffer_smaller_than_file), &bytes_read)); | |
| 360 ASSERT_EQ(sizeof(buffer_smaller_than_file), bytes_read); | |
| 361 ASSERT_EQ('o', buffer_smaller_than_file[0]); | |
| 362 ASSERT_EQ('n', buffer_smaller_than_file[1]); | |
| 363 ASSERT_EQ('t', buffer_smaller_than_file[2]); | |
| 364 ASSERT_EQ('e', buffer_smaller_than_file[3]); | |
| 365 ASSERT_EQ('n', buffer_smaller_than_file[4]); | |
| 366 | |
| 367 // | |
| 368 // Read from the beginning of the file. Buffer size == file size. | |
| 369 // | |
| 370 char equal_buffer_size[8]; | |
| 371 attr.offs = 0; | |
| 372 ASSERT_EQ(0, node->Read(attr, &equal_buffer_size[0], | |
| 373 sizeof(equal_buffer_size), &bytes_read)); | |
| 374 ASSERT_EQ(sizeof(equal_buffer_size), bytes_read); | |
| 375 ASSERT_EQ('c', equal_buffer_size[0]); | |
| 376 ASSERT_EQ('o', equal_buffer_size[1]); | |
| 377 ASSERT_EQ('n', equal_buffer_size[2]); | |
| 378 ASSERT_EQ('t', equal_buffer_size[3]); | |
| 379 ASSERT_EQ('e', equal_buffer_size[4]); | |
| 380 ASSERT_EQ('n', equal_buffer_size[5]); | |
| 381 ASSERT_EQ('t', equal_buffer_size[6]); | |
| 382 ASSERT_EQ('s', equal_buffer_size[7]); | |
| 383 } | |
| 384 | |
| 385 TEST_F(GoogleDriveFsTest, ReadFailureCases) { | |
| 386 ScopedRef<GoogleDriveFsForTesting> fs( | |
| 387 new GoogleDriveFsForTesting(map_, &ppapi_)); | |
| 388 | |
| 389 Path file_path("/file"); | |
| 390 ScopedNode file_node; | |
| 391 EXPECT_EQ(0, fs->Open(file_path, O_WRONLY | O_CREAT | O_APPEND, &file_node)); | |
| 392 | |
| 393 const char* contents = "contents"; | |
| 394 int bytes_written; | |
| 395 EXPECT_EQ(0, file_node->Write(HandleAttr(), contents, strlen(contents), | |
| 396 &bytes_written)); | |
| 397 EXPECT_EQ(strlen(contents), bytes_written); | |
| 398 | |
| 399 ScopedNode node; | |
| 400 ASSERT_EQ(0, fs->Open(file_path, O_WRONLY | O_CREAT | O_APPEND, &node)); | |
| 401 | |
| 402 // | |
| 403 // Reading from a file opened as write-only should fail. | |
| 404 // | |
| 405 char buffer[10]; | |
| 406 int bytes_read = 1; // Set to a non-zero value. | |
| 407 HandleAttr attr; | |
| 408 EXPECT_EQ(EACCES, node->Read(attr, &buffer[0], sizeof(buffer), &bytes_read)); | |
| 409 EXPECT_EQ(0, bytes_read); | |
| 410 | |
| 411 // mkdir on a directory that has not been existed. | |
| 412 Path dir_path("/dir"); | |
| 413 ASSERT_EQ(0, fs->Mkdir(dir_path, 0644)); | |
| 414 | |
| 415 // | |
| 416 // Reading from a directory should fail. | |
| 417 // | |
| 418 ScopedNode dir_node; | |
| 419 ASSERT_EQ(0, fs->Open(dir_path, O_RDONLY, &dir_node)); | |
| 420 ASSERT_EQ(EISDIR, | |
| 421 dir_node->Read(attr, &buffer[0], sizeof(buffer), &bytes_read)); | |
| 422 } | |
| 423 | |
| 424 TEST_F(GoogleDriveFsTest, WriteContents) { | |
| 425 ScopedRef<GoogleDriveFsForTesting> fs( | |
| 426 new GoogleDriveFsForTesting(map_, &ppapi_)); | |
| 427 | |
| 428 // | |
| 429 // Write from the beginning of the file. | |
| 430 // | |
| 431 Path file_path("/file"); | |
| 432 ScopedNode file_node; | |
| 433 EXPECT_EQ(0, fs->Open(file_path, O_WRONLY | O_CREAT | O_APPEND, &file_node)); | |
| 434 | |
| 435 const char* contents = "contents"; | |
| 436 int bytes_written; | |
| 437 EXPECT_EQ(0, file_node->Write(HandleAttr(), contents, strlen(contents), | |
| 438 &bytes_written)); | |
| 439 ASSERT_EQ(strlen(contents), bytes_written); | |
| 440 | |
| 441 // Open as read-write. | |
| 442 ScopedNode node; | |
| 443 ASSERT_EQ(0, fs->Open(file_path, O_RDWR, &node)); | |
| 444 | |
| 445 // | |
| 446 // Write from the middle of the file. | |
| 447 // | |
| 448 bytes_written = 1; // Set to a non-zero value. | |
| 449 HandleAttr attr; | |
| 450 attr.offs = 3; | |
| 451 EXPECT_EQ(0, node->Write(attr, "struct", 6, &bytes_written)); | |
| 452 EXPECT_EQ(6, bytes_written); | |
| 453 | |
| 454 char buffer[10]; | |
| 455 attr.offs = 0; | |
| 456 int bytes_read; | |
| 457 EXPECT_EQ(0, node->Read(attr, &buffer[0], sizeof(buffer), &bytes_read)); | |
| 458 EXPECT_EQ(9, bytes_read); | |
| 459 buffer[bytes_read] = 0; | |
| 460 EXPECT_STREQ("construct", buffer); | |
| 461 } | |
| 462 | |
| 463 TEST_F(GoogleDriveFsTest, WriteFailureCases) { | |
| 464 ScopedRef<GoogleDriveFsForTesting> fs( | |
| 465 new GoogleDriveFsForTesting(map_, &ppapi_)); | |
| 466 | |
| 467 Path file_path("/file"); | |
| 468 ScopedNode file_node; | |
| 469 EXPECT_EQ(0, fs->Open(file_path, O_WRONLY | O_CREAT | O_APPEND, &file_node)); | |
| 470 | |
| 471 const char* contents = "contents"; | |
| 472 int bytes_written; | |
| 473 EXPECT_EQ(0, file_node->Write(HandleAttr(), contents, strlen(contents), | |
| 474 &bytes_written)); | |
| 475 EXPECT_EQ(strlen(contents), bytes_written); | |
| 476 | |
| 477 ScopedNode node; | |
| 478 ASSERT_EQ(0, fs->Open(file_path, O_RDONLY, &node)); | |
| 479 | |
| 480 // | |
| 481 // Writing from a file opened as read-only should fail. | |
| 482 // | |
| 483 bytes_written = 1; // Set to a non-zero value. | |
| 484 HandleAttr attr; | |
| 485 attr.offs = 0; | |
| 486 char buffer[10]; | |
| 487 ASSERT_EQ(EACCES, | |
| 488 node->Write(attr, &buffer[0], sizeof(buffer), &bytes_written)); | |
| 489 ASSERT_EQ(0, bytes_written); | |
| 490 | |
| 491 // mkdir on a directory that has not been existed. | |
| 492 Path dir_path("/dir"); | |
| 493 ASSERT_EQ(0, fs->Mkdir(dir_path, 0644)); | |
| 494 | |
| 495 // | |
| 496 // Writing to a directory should fail. | |
| 497 // | |
| 498 EXPECT_EQ(0, fs->Open(dir_path, O_RDONLY, &node)); | |
| 499 int bytes_read; | |
| 500 EXPECT_EQ(EISDIR, node->Write(attr, &buffer[0], sizeof(buffer), &bytes_read)); | |
| 501 } | |
| 502 | |
| 503 TEST_F(GoogleDriveFsTest, GetStat) { | |
| 504 ScopedRef<GoogleDriveFsForTesting> fs( | |
| 505 new GoogleDriveFsForTesting(map_, &ppapi_)); | |
| 506 | |
| 507 // | |
| 508 // GetStat on a file. | |
| 509 // | |
| 510 ScopedNode node; | |
| 511 ASSERT_EQ(0, fs->Open(Path("/file"), O_WRONLY | O_CREAT | O_APPEND, &node)); | |
| 512 | |
| 513 const char* contents = "contents"; | |
| 514 int bytes_written; | |
| 515 EXPECT_EQ( | |
| 516 0, node->Write(HandleAttr(), contents, strlen(contents), &bytes_written)); | |
| 517 ASSERT_EQ(strlen(contents), bytes_written); | |
| 518 | |
| 519 struct stat statbuf; | |
| 520 EXPECT_EQ(0, node->GetStat(&statbuf)); | |
| 521 EXPECT_TRUE(S_ISREG(statbuf.st_mode)); | |
| 522 EXPECT_EQ(S_IWRITE, statbuf.st_mode & S_MODEBITS); | |
| 523 EXPECT_EQ(strlen(contents), statbuf.st_size); | |
| 524 EXPECT_EQ(0, statbuf.st_atime); | |
| 525 EXPECT_EQ(0, statbuf.st_ctime); | |
| 526 | |
| 527 // UTC time "2016-07-24T08:21:28.940Z" specified at each file | |
| 528 // in FakeGoogleDriveServer is equal to the UNIX timestamp 1469348488 | |
| 529 EXPECT_EQ(1469348488, statbuf.st_mtime); | |
| 530 | |
| 531 // Test Get* and Isa* methods. | |
| 532 off_t size; | |
| 533 EXPECT_EQ(0, node->GetSize(&size)); | |
| 534 EXPECT_EQ(strlen(contents), size); | |
| 535 EXPECT_FALSE(node->IsaDir()); | |
| 536 EXPECT_TRUE(node->IsaFile()); | |
| 537 EXPECT_EQ(ENOTTY, node->Isatty()); | |
| 538 | |
| 539 Path path("/dir"); | |
| 540 ASSERT_EQ(0, fs->Mkdir(path, 0644)); | |
| 541 | |
| 542 // | |
| 543 // GetStat on a directory. | |
| 544 // | |
| 545 EXPECT_EQ(0, fs->Open(path, O_RDONLY, &node)); | |
| 546 EXPECT_EQ(0, node->GetStat(&statbuf)); | |
| 547 EXPECT_TRUE(S_ISDIR(statbuf.st_mode)); | |
| 548 EXPECT_EQ(S_IREAD, statbuf.st_mode & S_MODEBITS); | |
| 549 EXPECT_EQ(0, statbuf.st_size); | |
| 550 EXPECT_EQ(0, statbuf.st_atime); | |
| 551 EXPECT_EQ(0, statbuf.st_ctime); | |
| 552 | |
| 553 // UTC time "2016-07-24T08:21:28.940Z" specified at each directory | |
| 554 // in FakeGoogleDriveServer is equal to the UNIX timestamp 1469348488; | |
| 555 EXPECT_EQ(1469348488, statbuf.st_mtime); | |
| 556 | |
| 557 // Test Get* and Isa* methods. | |
| 558 EXPECT_EQ(0, node->GetSize(&size)); | |
| 559 EXPECT_EQ(0, size); | |
| 560 EXPECT_TRUE(node->IsaDir()); | |
| 561 EXPECT_FALSE(node->IsaFile()); | |
| 562 EXPECT_EQ(ENOTTY, node->Isatty()); | |
| 563 } | |
| 564 | |
| 565 TEST_F(GoogleDriveFsTest, FTruncate) { | |
| 566 ScopedRef<GoogleDriveFsForTesting> fs( | |
| 567 new GoogleDriveFsForTesting(map_, &ppapi_)); | |
| 568 | |
| 569 ScopedNode node; | |
| 570 ASSERT_EQ(0, fs->Open(Path("/file"), O_RDWR | O_CREAT | O_TRUNC, &node)); | |
| 571 const char* contents = "contents"; | |
| 572 int bytes_written; | |
| 573 EXPECT_EQ( | |
| 574 0, node->Write(HandleAttr(), contents, strlen(contents), &bytes_written)); | |
| 575 ASSERT_EQ(strlen(contents), bytes_written); | |
| 576 | |
| 577 HandleAttr attr; | |
| 578 char buffer[10] = {0}; | |
| 579 int bytes_read; | |
| 580 | |
| 581 // | |
| 582 // First make the file shorter... | |
| 583 // | |
| 584 EXPECT_EQ(0, node->FTruncate(4)); | |
| 585 EXPECT_EQ(0, node->Read(attr, &buffer[0], sizeof(buffer), &bytes_read)); | |
| 586 EXPECT_EQ(4, bytes_read); | |
| 587 buffer[bytes_read] = 0; | |
| 588 EXPECT_STREQ("cont", buffer); | |
| 589 | |
| 590 // | |
| 591 // Now make the file longer... | |
| 592 // | |
| 593 EXPECT_EQ(0, node->FTruncate(8)); | |
| 594 EXPECT_EQ(0, node->Read(attr, &buffer[0], sizeof(buffer), &bytes_read)); | |
| 595 EXPECT_EQ(8, bytes_read); | |
| 596 buffer[bytes_read] = 0; | |
| 597 EXPECT_STREQ("cont\0\0\0\0", buffer); | |
| 598 | |
| 599 Path path("/dir"); | |
| 600 ASSERT_EQ(0, fs->Mkdir(path, 0644)); | |
| 601 | |
| 602 // | |
| 603 // Ftruncate should fail for a directory. | |
| 604 // | |
| 605 EXPECT_EQ(0, fs->Open(path, O_RDONLY, &node)); | |
| 606 EXPECT_EQ(EISDIR, node->FTruncate(4)); | |
| 607 } | |
| 608 | |
| 609 TEST_F(GoogleDriveFsTest, OpenDirectory) { | |
| 610 ScopedRef<GoogleDriveFsForTesting> fs( | |
| 611 new GoogleDriveFsForTesting(map_, &ppapi_)); | |
| 612 | |
| 613 Path dir_path("/dir"); | |
| 614 ASSERT_EQ(0, fs->Mkdir(dir_path, 0644)); | |
| 615 | |
| 616 // | |
| 617 // Open a directory with r mode should succeed. | |
| 618 // | |
| 619 ScopedNode node; | |
| 620 ASSERT_EQ(0, fs->Open(dir_path, O_RDONLY, &node)); | |
| 621 | |
| 622 // | |
| 623 // Open a directory with r+, w, w+, a, and a+ mode should fail. | |
| 624 // | |
| 625 EXPECT_EQ(EPERM, fs->Open(dir_path, O_RDWR, &node)); | |
| 626 EXPECT_EQ(EPERM, fs->Open(dir_path, O_WRONLY | O_CREAT | O_TRUNC, &node)); | |
| 627 EXPECT_EQ(EPERM, fs->Open(dir_path, O_RDWR | O_CREAT | O_TRUNC, &node)); | |
| 628 EXPECT_EQ(EPERM, fs->Open(dir_path, O_WRONLY | O_CREAT | O_APPEND, &node)); | |
| 629 EXPECT_EQ(EPERM, fs->Open(dir_path, O_RDWR | O_CREAT | O_APPEND, &node)); | |
| 630 | |
| 631 // | |
| 632 // Opening a non-existing directory should fail. | |
| 633 // | |
| 634 EXPECT_EQ(ENOENT, fs->Open(Path("/nonexistingdir"), O_RDONLY, &node)); | |
| 635 } | |
| 636 | |
| 637 TEST_F(GoogleDriveFsTest, GetDents) { | |
| 638 ScopedRef<GoogleDriveFsForTesting> fs( | |
| 639 new GoogleDriveFsForTesting(map_, &ppapi_)); | |
| 640 | |
| 641 ScopedNode node; | |
| 642 Path file_path("/file"); | |
| 643 ASSERT_EQ(0, fs->Open(file_path, O_WRONLY | O_CREAT | O_TRUNC, &node)); | |
| 644 const char* contents = "contents"; | |
| 645 int bytes_written; | |
| 646 EXPECT_EQ( | |
| 647 0, node->Write(HandleAttr(), contents, strlen(contents), &bytes_written)); | |
| 648 ASSERT_EQ(strlen(contents), bytes_written); | |
| 649 | |
| 650 ScopedNode root; | |
| 651 ASSERT_EQ(0, fs->Open(Path("/"), O_RDONLY, &root)); | |
| 652 | |
| 653 struct stat stat; | |
| 654 ASSERT_EQ(0, node->GetStat(&stat)); | |
| 655 | |
| 656 // | |
| 657 // Should fail for regular files. | |
| 658 // | |
| 659 const size_t kMaxDirents = 5; | |
| 660 dirent dirents[kMaxDirents]; | |
| 661 int bytes_read = 1; // Set to a non-zero value. | |
| 662 | |
| 663 memset(&dirents[0], 0, sizeof(dirents)); | |
| 664 EXPECT_EQ(ENOTDIR, | |
| 665 node->GetDents(0, &dirents[0], sizeof(dirents), &bytes_read)); | |
| 666 EXPECT_EQ(0, bytes_read); | |
| 667 | |
| 668 // | |
| 669 // Should work with root directory. | |
| 670 // | |
| 671 // +2 to test a size that is not a multiple of sizeof(dirent). | |
| 672 // Expect it to round down. | |
| 673 memset(&dirents[0], 0, sizeof(dirents)); | |
| 674 EXPECT_EQ( | |
| 675 0, root->GetDents(0, &dirents[0], sizeof(dirent) * 3 + 2, &bytes_read)); | |
| 676 | |
| 677 { | |
| 678 size_t num_dirents = bytes_read / sizeof(dirent); | |
| 679 EXPECT_EQ(3, num_dirents); | |
| 680 EXPECT_EQ(sizeof(dirent) * num_dirents, bytes_read); | |
| 681 | |
| 682 std::multiset<std::string> dirnames; | |
| 683 for (size_t i = 0; i < num_dirents; ++i) { | |
| 684 EXPECT_EQ(sizeof(dirent), dirents[i].d_reclen); | |
| 685 dirnames.insert(dirents[i].d_name); | |
| 686 } | |
| 687 | |
| 688 EXPECT_EQ(1, dirnames.count("file")); | |
| 689 EXPECT_EQ(1, dirnames.count(".")); | |
| 690 EXPECT_EQ(1, dirnames.count("..")); | |
| 691 } | |
| 692 | |
| 693 // Add another file... | |
| 694 ASSERT_EQ(0, fs->Open(Path("/file2"), O_WRONLY | O_CREAT | O_TRUNC, &node)); | |
| 695 ASSERT_EQ(0, node->GetStat(&stat)); | |
| 696 | |
| 697 // | |
| 698 // Read the root directory again after adding another file. | |
| 699 // | |
| 700 memset(&dirents[0], 0, sizeof(dirents)); | |
| 701 EXPECT_EQ(0, root->GetDents(0, &dirents[0], sizeof(dirents), &bytes_read)); | |
| 702 | |
| 703 { | |
| 704 size_t num_dirents = bytes_read / sizeof(dirent); | |
| 705 EXPECT_EQ(4, num_dirents); | |
| 706 EXPECT_EQ(sizeof(dirent) * num_dirents, bytes_read); | |
| 707 | |
| 708 std::multiset<std::string> dirnames; | |
| 709 for (size_t i = 0; i < num_dirents; ++i) { | |
| 710 EXPECT_EQ(sizeof(dirent), dirents[i].d_reclen); | |
| 711 dirnames.insert(dirents[i].d_name); | |
| 712 } | |
| 713 | |
| 714 EXPECT_EQ(1, dirnames.count("file")); | |
| 715 EXPECT_EQ(1, dirnames.count("file2")); | |
| 716 EXPECT_EQ(1, dirnames.count(".")); | |
| 717 EXPECT_EQ(1, dirnames.count("..")); | |
| 718 } | |
| 719 | |
| 720 // | |
| 721 // Read >100 entries. Multiple API requests are sent to request >100 entries. | |
| 722 // | |
| 723 const size_t kMaxDirectsMultipleRequests = 105; | |
| 724 std::vector<dirent> direntsMultipleRequests; | |
| 725 direntsMultipleRequests.resize(kMaxDirectsMultipleRequests); | |
| 726 | |
| 727 // Add 100 files... | |
| 728 for (size_t i = 0; i < 100; ++i) { | |
| 729 char buffer[4]; | |
| 730 int char_written = snprintf(buffer, sizeof(buffer), "%u", i); | |
| 731 | |
| 732 // 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
| |
| 733 // both operands because sizeof(buffer) <= INT_MAX. | |
| 734 ASSERT_TRUE(char_written >= 0 && char_written < (signed int)sizeof(buffer)); | |
| 735 | |
| 736 ASSERT_EQ(0, fs->Open(Path("/anotherfile" + std::string(buffer)), | |
| 737 O_WRONLY | O_CREAT | O_TRUNC, &node)); | |
| 738 ASSERT_EQ(0, node->GetStat(&stat)); | |
| 739 } | |
| 740 | |
| 741 EXPECT_EQ(0, root->GetDents(0, &direntsMultipleRequests[0], | |
| 742 sizeof(dirent) * direntsMultipleRequests.size(), | |
| 743 &bytes_read)); | |
| 744 { | |
| 745 size_t num_dirents = bytes_read / sizeof(dirent); | |
| 746 EXPECT_EQ(104, num_dirents); | |
| 747 EXPECT_EQ(sizeof(dirent) * num_dirents, bytes_read); | |
| 748 | |
| 749 std::multiset<std::string> dirnames; | |
| 750 for (size_t i = 0; i < num_dirents; ++i) { | |
| 751 EXPECT_EQ(sizeof(dirent), direntsMultipleRequests[i].d_reclen); | |
| 752 dirnames.insert(direntsMultipleRequests[i].d_name); | |
| 753 } | |
| 754 | |
| 755 for (size_t i = 0; i < 100; ++i) { | |
| 756 char buffer[4]; | |
| 757 int char_written = snprintf(buffer, sizeof(buffer), "%u", i); | |
| 758 | |
| 759 // 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
| |
| 760 // both operands because sizeof(buffer) <= INT_MAX. | |
| 761 ASSERT_TRUE(char_written >= 0 && | |
| 762 char_written < (signed int)sizeof(buffer)); | |
| 763 | |
| 764 EXPECT_EQ(1, dirnames.count("anotherfile" + std::string(buffer))); | |
| 765 } | |
| 766 | |
| 767 EXPECT_EQ(1, dirnames.count("file")); | |
| 768 EXPECT_EQ(1, dirnames.count("file2")); | |
| 769 EXPECT_EQ(1, dirnames.count(".")); | |
| 770 EXPECT_EQ(1, dirnames.count("..")); | |
| 771 } | |
| 772 } | |
| 773 | |
| 774 // TODO: Add tests after Remove becomes supported. | |
| 775 // TODO: Add tests after Unlink becomes supported. | |
| 776 // TODO: Add tests after Rename becomes supported. | |
| OLD | NEW |