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
3 // found in the LICENSE file.
11 #include <gmock/gmock.h>
12 #include <ppapi/c/ppb_file_io.h>
13 #include <ppapi/c/pp_directory_entry.h>
14 #include <ppapi/c/pp_errors.h>
15 #include <ppapi/c/pp_instance.h>
17 #include <windows.h> // For Sleep()
20 #include "fake_ppapi/fake_pepper_interface_html5_fs.h"
21 #include "nacl_io/kernel_handle.h"
22 #include "nacl_io/html5fs/html5_fs.h"
23 #include "nacl_io/osdirent.h"
24 #include "nacl_io/osunistd.h"
25 #include "nacl_io/pepper_interface_delegate.h"
26 #include "sdk_util/scoped_ref.h"
27 #include "mock_util.h"
28 #include "pepper_interface_mock.h"
30 using namespace nacl_io;
31 using namespace sdk_util;
34 using ::testing::DoAll;
35 using ::testing::Invoke;
36 using ::testing::Mock;
37 using ::testing::Return;
41 class Html5FsForTesting : public Html5Fs {
43 Html5FsForTesting(StringMap_t& string_map, PepperInterface* ppapi,
44 int expected_error = 0) {
46 args.string_map = string_map;
48 Error error = Init(args);
49 EXPECT_EQ(expected_error, error);
52 bool Exists(const char* filename) {
54 if (Open(Path(filename), O_RDONLY, &node))
58 return node->GetStat(&buf) == 0;
62 class Html5FsTest : public ::testing::Test {
67 FakePepperInterfaceHtml5Fs ppapi_html5_;
68 PepperInterfaceMock ppapi_mock_;
69 PepperInterfaceDelegate ppapi_;
72 Html5FsTest::Html5FsTest()
73 : ppapi_mock_(ppapi_html5_.GetInstance()),
74 ppapi_(ppapi_html5_.GetInstance()) {
75 // Default delegation to the html5 pepper interface.
76 ppapi_.SetCoreInterfaceDelegate(ppapi_html5_.GetCoreInterface());
77 ppapi_.SetFileSystemInterfaceDelegate(ppapi_html5_.GetFileSystemInterface());
78 ppapi_.SetFileRefInterfaceDelegate(ppapi_html5_.GetFileRefInterface());
79 ppapi_.SetFileIoInterfaceDelegate(ppapi_html5_.GetFileIoInterface());
80 ppapi_.SetVarInterfaceDelegate(ppapi_html5_.GetVarInterface());
85 TEST_F(Html5FsTest, FilesystemType) {
86 const char* filesystem_type_strings[] = {"", "PERSISTENT", "TEMPORARY", NULL};
87 PP_FileSystemType filesystem_type_values[] = {
88 PP_FILESYSTEMTYPE_LOCALPERSISTENT, // Default to persistent.
89 PP_FILESYSTEMTYPE_LOCALPERSISTENT, PP_FILESYSTEMTYPE_LOCALTEMPORARY};
91 const char* expected_size_strings[] = {"100", "12345", NULL};
92 const int expected_size_values[] = {100, 12345};
94 FileSystemInterfaceMock* filesystem_mock =
95 ppapi_mock_.GetFileSystemInterface();
97 FakeFileSystemInterface* filesystem_fake =
98 static_cast<FakeFileSystemInterface*>(
99 ppapi_html5_.GetFileSystemInterface());
101 for (int i = 0; filesystem_type_strings[i] != NULL; ++i) {
102 const char* filesystem_type_string = filesystem_type_strings[i];
103 PP_FileSystemType expected_filesystem_type = filesystem_type_values[i];
105 for (int j = 0; expected_size_strings[j] != NULL; ++j) {
106 const char* expected_size_string = expected_size_strings[j];
107 int64_t expected_expected_size = expected_size_values[j];
109 ppapi_.SetFileSystemInterfaceDelegate(filesystem_mock);
111 ON_CALL(*filesystem_mock, Create(_, _)).WillByDefault(
112 Invoke(filesystem_fake, &FakeFileSystemInterface::Create));
114 EXPECT_CALL(*filesystem_mock,
115 Create(ppapi_.GetInstance(), expected_filesystem_type));
117 EXPECT_CALL(*filesystem_mock, Open(_, expected_expected_size, _))
118 .WillOnce(DoAll(CallCallback<2>(int32_t(PP_OK)),
119 Return(int32_t(PP_OK_COMPLETIONPENDING))));
122 map["type"] = filesystem_type_string;
123 map["expected_size"] = expected_size_string;
124 ScopedRef<Html5FsForTesting> fs(new Html5FsForTesting(map, &ppapi_));
126 Mock::VerifyAndClearExpectations(&filesystem_mock);
131 TEST_F(Html5FsTest, PassFilesystemResource) {
132 // Fail if given a bad resource.
135 map["filesystem_resource"] = "0";
136 ScopedRef<Html5FsForTesting> fs(
137 new Html5FsForTesting(map, &ppapi_, EINVAL));
141 EXPECT_TRUE(ppapi_html5_.filesystem_template()->AddEmptyFile("/foo", NULL));
142 PP_Resource filesystem = ppapi_html5_.GetFileSystemInterface()->Create(
143 ppapi_html5_.GetInstance(), PP_FILESYSTEMTYPE_LOCALPERSISTENT);
145 ASSERT_EQ(int32_t(PP_OK), ppapi_html5_.GetFileSystemInterface()->Open(
146 filesystem, 0, PP_BlockUntilComplete()));
150 snprintf(buffer, 30, "%d", filesystem);
151 map["filesystem_resource"] = buffer;
152 ScopedRef<Html5FsForTesting> fs(
153 new Html5FsForTesting(map, &ppapi_));
155 ASSERT_TRUE(fs->Exists("/foo"));
157 ppapi_html5_.GetCoreInterface()->ReleaseResource(filesystem);
161 TEST_F(Html5FsTest, MountSubtree) {
162 EXPECT_TRUE(ppapi_html5_.filesystem_template()->AddEmptyFile("/foo/bar",
165 map["SOURCE"] = "/foo";
166 ScopedRef<Html5FsForTesting> fs(new Html5FsForTesting(map, &ppapi_));
168 ASSERT_TRUE(fs->Exists("/bar"));
169 ASSERT_FALSE(fs->Exists("/foo/bar"));
172 TEST_F(Html5FsTest, Mkdir) {
174 ScopedRef<Html5FsForTesting> fs(new Html5FsForTesting(map, &ppapi_));
176 // mkdir at the root should return EEXIST, not EACCES.
177 EXPECT_EQ(EEXIST, fs->Mkdir(Path("/"), 0644));
180 ASSERT_FALSE(fs->Exists("/foo"));
181 ASSERT_EQ(0, fs->Mkdir(path, 0644));
185 ASSERT_EQ(0, fs->Open(path, O_RDONLY, &node));
186 EXPECT_EQ(0, node->GetStat(&stat));
187 EXPECT_EQ(S_IFDIR, stat.st_mode & S_IFDIR);
190 TEST_F(Html5FsTest, Remove) {
191 const char* kPath = "/foo";
192 EXPECT_TRUE(ppapi_html5_.filesystem_template()->AddEmptyFile(kPath, NULL));
195 ScopedRef<Html5FsForTesting> fs(new Html5FsForTesting(map, &ppapi_));
198 ASSERT_TRUE(fs->Exists(kPath));
199 ASSERT_EQ(0, fs->Remove(path));
200 EXPECT_FALSE(fs->Exists(kPath));
203 TEST_F(Html5FsTest, Unlink) {
204 EXPECT_TRUE(ppapi_html5_.filesystem_template()->AddEmptyFile("/file", NULL));
205 EXPECT_TRUE(ppapi_html5_.filesystem_template()->AddDirectory("/dir", NULL));
208 ScopedRef<Html5FsForTesting> fs(new Html5FsForTesting(map, &ppapi_));
210 ASSERT_TRUE(fs->Exists("/dir"));
211 ASSERT_TRUE(fs->Exists("/file"));
212 ASSERT_EQ(0, fs->Unlink(Path("/file")));
213 ASSERT_EQ(EISDIR, fs->Unlink(Path("/dir")));
214 EXPECT_FALSE(fs->Exists("/file"));
215 EXPECT_TRUE(fs->Exists("/dir"));
218 TEST_F(Html5FsTest, Rmdir) {
219 EXPECT_TRUE(ppapi_html5_.filesystem_template()->AddEmptyFile("/file", NULL));
220 EXPECT_TRUE(ppapi_html5_.filesystem_template()->AddDirectory("/dir", NULL));
223 ScopedRef<Html5FsForTesting> fs(new Html5FsForTesting(map, &ppapi_));
225 ASSERT_EQ(ENOTDIR, fs->Rmdir(Path("/file")));
226 EXPECT_EQ(0, fs->Rmdir(Path("/dir")));
227 EXPECT_FALSE(fs->Exists("/dir"));
228 EXPECT_TRUE(fs->Exists("/file"));
231 TEST_F(Html5FsTest, Rename) {
232 EXPECT_TRUE(ppapi_html5_.filesystem_template()->AddEmptyFile("/foo", NULL));
235 ScopedRef<Html5FsForTesting> fs(new Html5FsForTesting(map, &ppapi_));
237 ASSERT_TRUE(fs->Exists("/foo"));
238 ASSERT_EQ(0, fs->Rename(Path("/foo"), Path("/bar")));
239 EXPECT_FALSE(fs->Exists("/foo"));
240 EXPECT_TRUE(fs->Exists("/bar"));
243 TEST_F(Html5FsTest, OpenForCreate) {
245 ScopedRef<Html5FsForTesting> fs(new Html5FsForTesting(map, &ppapi_));
247 EXPECT_FALSE(fs->Exists("/foo"));
251 ASSERT_EQ(0, fs->Open(path, O_CREAT | O_RDWR, &node));
254 char contents[] = "contents";
255 int bytes_written = 0;
256 EXPECT_EQ(0, node->Write(HandleAttr(), &contents[0], strlen(contents),
258 EXPECT_EQ(strlen(contents), bytes_written);
261 ASSERT_EQ(0, fs->Open(path, O_CREAT, &node));
263 // Check that the file still has data.
265 EXPECT_EQ(0, node->GetSize(&size));
266 EXPECT_EQ(strlen(contents), size);
269 EXPECT_EQ(EEXIST, fs->Open(path, O_CREAT | O_EXCL, &node));
271 // Try to truncate without write access.
272 EXPECT_EQ(EINVAL, fs->Open(path, O_CREAT | O_TRUNC, &node));
274 // Open and truncate.
275 ASSERT_EQ(0, fs->Open(path, O_CREAT | O_TRUNC | O_WRONLY, &node));
277 // File should be empty.
278 EXPECT_EQ(0, node->GetSize(&size));
282 TEST_F(Html5FsTest, Read) {
283 const char contents[] = "contents";
285 ppapi_html5_.filesystem_template()->AddFile("/file", contents, NULL));
286 ASSERT_TRUE(ppapi_html5_.filesystem_template()->AddDirectory("/dir", NULL));
288 ScopedRef<Html5FsForTesting> fs(new Html5FsForTesting(map, &ppapi_));
291 ASSERT_EQ(0, fs->Open(Path("/file"), O_RDONLY, &node));
293 char buffer[10] = {0};
296 ASSERT_EQ(0, node->Read(attr, &buffer[0], sizeof(buffer), &bytes_read));
297 ASSERT_EQ(strlen(contents), bytes_read);
298 ASSERT_STREQ(contents, buffer);
300 // Read nothing past the end of the file.
302 ASSERT_EQ(0, node->Read(attr, &buffer[0], sizeof(buffer), &bytes_read));
303 ASSERT_EQ(0, bytes_read);
305 // Read part of the data.
307 ASSERT_EQ(0, node->Read(attr, &buffer[0], sizeof(buffer), &bytes_read));
308 ASSERT_EQ(strlen(contents) - 4, bytes_read);
309 buffer[bytes_read] = 0;
310 ASSERT_STREQ("ents", buffer);
312 // Writing should fail.
313 int bytes_written = 1; // Set to a non-zero value.
316 node->Write(attr, &buffer[0], sizeof(buffer), &bytes_written));
317 ASSERT_EQ(0, bytes_written);
319 // Reading from a directory should fail.
320 ASSERT_EQ(0, fs->Open(Path("/dir"), O_RDONLY, &node));
321 ASSERT_EQ(EISDIR, node->Read(attr, &buffer[0], sizeof(buffer), &bytes_read));
324 TEST_F(Html5FsTest, Write) {
325 const char contents[] = "contents";
327 ppapi_html5_.filesystem_template()->AddFile("/file", contents, NULL));
328 EXPECT_TRUE(ppapi_html5_.filesystem_template()->AddDirectory("/dir", NULL));
331 ScopedRef<Html5FsForTesting> fs(new Html5FsForTesting(map, &ppapi_));
334 ASSERT_EQ(0, fs->Open(Path("/file"), O_WRONLY, &node));
336 // Reading should fail.
338 int bytes_read = 1; // Set to a non-zero value.
340 EXPECT_EQ(EACCES, node->Read(attr, &buffer[0], sizeof(buffer), &bytes_read));
341 EXPECT_EQ(0, bytes_read);
343 // Reopen as read-write.
344 ASSERT_EQ(0, fs->Open(Path("/file"), O_RDWR, &node));
346 int bytes_written = 1; // Set to a non-zero value.
348 EXPECT_EQ(0, node->Write(attr, "struct", 6, &bytes_written));
349 EXPECT_EQ(6, bytes_written);
352 EXPECT_EQ(0, node->Read(attr, &buffer[0], sizeof(buffer), &bytes_read));
353 EXPECT_EQ(9, bytes_read);
354 buffer[bytes_read] = 0;
355 EXPECT_STREQ("construct", buffer);
357 // Writing to a directory should fail.
358 EXPECT_EQ(0, fs->Open(Path("/dir"), O_RDWR, &node));
359 EXPECT_EQ(EISDIR, node->Write(attr, &buffer[0], sizeof(buffer), &bytes_read));
362 TEST_F(Html5FsTest, GetStat) {
363 const int creation_time = 1000;
364 const int access_time = 2000;
365 const int modified_time = 3000;
366 const char contents[] = "contents";
369 FakeHtml5FsNode* fake_node;
370 EXPECT_TRUE(ppapi_html5_.filesystem_template()->AddFile(
371 "/file", contents, &fake_node));
372 fake_node->set_creation_time(creation_time);
373 fake_node->set_last_access_time(access_time);
374 fake_node->set_last_modified_time(modified_time);
376 // Create fake directory.
378 ppapi_html5_.filesystem_template()->AddDirectory("/dir", &fake_node));
379 fake_node->set_creation_time(creation_time);
380 fake_node->set_last_access_time(access_time);
381 fake_node->set_last_modified_time(modified_time);
384 ScopedRef<Html5FsForTesting> fs(new Html5FsForTesting(map, &ppapi_));
387 ASSERT_EQ(0, fs->Open(Path("/file"), O_RDONLY, &node));
390 EXPECT_EQ(0, node->GetStat(&statbuf));
391 EXPECT_EQ(S_IFREG, statbuf.st_mode & S_IFMT);
392 EXPECT_EQ(S_IRALL | S_IWALL | S_IXALL, statbuf.st_mode & ~S_IFMT);
393 EXPECT_EQ(strlen(contents), statbuf.st_size);
394 EXPECT_EQ(access_time, statbuf.st_atime);
395 EXPECT_EQ(creation_time, statbuf.st_ctime);
396 EXPECT_EQ(modified_time, statbuf.st_mtime);
398 // Test Get* and Isa* methods.
400 EXPECT_EQ(0, node->GetSize(&size));
401 EXPECT_EQ(strlen(contents), size);
402 EXPECT_FALSE(node->IsaDir());
403 EXPECT_TRUE(node->IsaFile());
404 EXPECT_EQ(ENOTTY, node->Isatty());
406 // GetStat on a directory...
407 EXPECT_EQ(0, fs->Open(Path("/dir"), O_RDONLY, &node));
408 EXPECT_EQ(0, node->GetStat(&statbuf));
409 EXPECT_EQ(S_IFDIR, statbuf.st_mode & S_IFMT);
410 EXPECT_EQ(S_IRALL | S_IWALL | S_IXALL, statbuf.st_mode & ~S_IFMT);
411 EXPECT_EQ(0, statbuf.st_size);
412 EXPECT_EQ(access_time, statbuf.st_atime);
413 EXPECT_EQ(creation_time, statbuf.st_ctime);
414 EXPECT_EQ(modified_time, statbuf.st_mtime);
416 // Test Get* and Isa* methods.
417 EXPECT_EQ(0, node->GetSize(&size));
419 EXPECT_TRUE(node->IsaDir());
420 EXPECT_FALSE(node->IsaFile());
421 EXPECT_EQ(ENOTTY, node->Isatty());
424 TEST_F(Html5FsTest, FTruncate) {
425 const char contents[] = "contents";
427 ppapi_html5_.filesystem_template()->AddFile("/file", contents, NULL));
428 EXPECT_TRUE(ppapi_html5_.filesystem_template()->AddDirectory("/dir", NULL));
431 ScopedRef<Html5FsForTesting> fs(new Html5FsForTesting(map, &ppapi_));
434 ASSERT_EQ(0, fs->Open(Path("/file"), O_RDWR, &node));
437 char buffer[10] = {0};
440 // First make the file shorter...
441 EXPECT_EQ(0, node->FTruncate(4));
442 EXPECT_EQ(0, node->Read(attr, &buffer[0], sizeof(buffer), &bytes_read));
443 EXPECT_EQ(4, bytes_read);
444 buffer[bytes_read] = 0;
445 EXPECT_STREQ("cont", buffer);
447 // Now make the file longer...
448 EXPECT_EQ(0, node->FTruncate(8));
449 EXPECT_EQ(0, node->Read(attr, &buffer[0], sizeof(buffer), &bytes_read));
450 EXPECT_EQ(8, bytes_read);
451 buffer[bytes_read] = 0;
452 EXPECT_STREQ("cont\0\0\0\0", buffer);
454 // Ftruncate should fail for a directory.
455 EXPECT_EQ(0, fs->Open(Path("/dir"), O_RDONLY, &node));
456 EXPECT_EQ(EISDIR, node->FTruncate(4));
459 TEST_F(Html5FsTest, GetDents) {
460 const char contents[] = "contents";
462 ppapi_html5_.filesystem_template()->AddFile("/file", contents, NULL));
465 ScopedRef<Html5FsForTesting> fs(new Html5FsForTesting(map, &ppapi_));
468 ASSERT_EQ(0, fs->Open(Path("/"), O_RDONLY, &root));
471 ASSERT_EQ(0, fs->Open(Path("/file"), O_RDWR, &node));
474 ASSERT_EQ(0, node->GetStat(&stat));
475 ino_t file1_ino = stat.st_ino;
477 // Should fail for regular files.
478 const size_t kMaxDirents = 5;
479 dirent dirents[kMaxDirents];
480 int bytes_read = 1; // Set to a non-zero value.
482 memset(&dirents[0], 0, sizeof(dirents));
484 node->GetDents(0, &dirents[0], sizeof(dirents), &bytes_read));
485 EXPECT_EQ(0, bytes_read);
487 // Should work with root directory.
488 // +2 to test a size that is not a multiple of sizeof(dirent).
489 // Expect it to round down.
490 memset(&dirents[0], 0, sizeof(dirents));
492 0, root->GetDents(0, &dirents[0], sizeof(dirent) * 3 + 2, &bytes_read));
495 size_t num_dirents = bytes_read / sizeof(dirent);
496 EXPECT_EQ(3, num_dirents);
497 EXPECT_EQ(sizeof(dirent) * num_dirents, bytes_read);
499 std::multiset<std::string> dirnames;
500 for (size_t i = 0; i < num_dirents; ++i) {
501 EXPECT_EQ(sizeof(dirent), dirents[i].d_off);
502 EXPECT_EQ(sizeof(dirent), dirents[i].d_reclen);
503 dirnames.insert(dirents[i].d_name);
506 EXPECT_EQ(1, dirnames.count("file"));
507 EXPECT_EQ(1, dirnames.count("."));
508 EXPECT_EQ(1, dirnames.count(".."));
511 // Add another file...
512 ASSERT_EQ(0, fs->Open(Path("/file2"), O_CREAT, &node));
513 ASSERT_EQ(0, node->GetStat(&stat));
514 ino_t file2_ino = stat.st_ino;
516 // These files SHOULD not hash to the same value but COULD.
517 EXPECT_NE(file1_ino, file2_ino);
519 // Read the root directory again.
520 memset(&dirents[0], 0, sizeof(dirents));
521 EXPECT_EQ(0, root->GetDents(0, &dirents[0], sizeof(dirents), &bytes_read));
524 size_t num_dirents = bytes_read / sizeof(dirent);
525 EXPECT_EQ(4, num_dirents);
526 EXPECT_EQ(sizeof(dirent) * num_dirents, bytes_read);
528 std::multiset<std::string> dirnames;
529 for (size_t i = 0; i < num_dirents; ++i) {
530 EXPECT_EQ(sizeof(dirent), dirents[i].d_off);
531 EXPECT_EQ(sizeof(dirent), dirents[i].d_reclen);
532 dirnames.insert(dirents[i].d_name);
534 if (!strcmp(dirents[i].d_name, "file")) {
535 EXPECT_EQ(dirents[i].d_ino, file1_ino);
537 if (!strcmp(dirents[i].d_name, "file2")) {
538 EXPECT_EQ(dirents[i].d_ino, file2_ino);
542 EXPECT_EQ(1, dirnames.count("file"));
543 EXPECT_EQ(1, dirnames.count("file2"));
544 EXPECT_EQ(1, dirnames.count("."));
545 EXPECT_EQ(1, dirnames.count(".."));