| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 <fcntl.h> | 5 #include <fcntl.h> |
| 6 | 6 |
| 7 #include <gtest/gtest.h> | 7 #include <gtest/gtest.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 }; | 29 }; |
| 30 | 30 |
| 31 // Implementation of a simple flat memory filesystem. | 31 // Implementation of a simple flat memory filesystem. |
| 32 struct File { | 32 struct File { |
| 33 std::string name; | 33 std::string name; |
| 34 std::vector<uint8_t> data; | 34 std::vector<uint8_t> data; |
| 35 }; | 35 }; |
| 36 | 36 |
| 37 typedef std::vector<File> Files; | 37 typedef std::vector<File> Files; |
| 38 Files g_files; | 38 Files g_files; |
| 39 mode_t last_create_mode = 0666; |
| 39 | 40 |
| 40 bool IsValidPath(const char* path) { | 41 bool IsValidPath(const char* path) { |
| 41 if (path == NULL) | 42 if (path == NULL) |
| 42 return false; | 43 return false; |
| 43 | 44 |
| 44 if (strlen(path) <= 1) | 45 if (strlen(path) <= 1) |
| 45 return false; | 46 return false; |
| 46 | 47 |
| 47 if (path[0] != '/') | 48 if (path[0] != '/') |
| 48 return false; | 49 return false; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 67 | 68 |
| 68 if (strcmp(path, "/") == 0) { | 69 if (strcmp(path, "/") == 0) { |
| 69 stbuf->st_mode = S_IFDIR | 0755; | 70 stbuf->st_mode = S_IFDIR | 0755; |
| 70 return 0; | 71 return 0; |
| 71 } | 72 } |
| 72 | 73 |
| 73 File* file = FindFile(path); | 74 File* file = FindFile(path); |
| 74 if (file == NULL) | 75 if (file == NULL) |
| 75 return -ENOENT; | 76 return -ENOENT; |
| 76 | 77 |
| 77 stbuf->st_mode = S_IFREG | 0666; | 78 stbuf->st_mode = S_IFREG | last_create_mode; |
| 78 stbuf->st_size = file->data.size(); | 79 stbuf->st_size = file->data.size(); |
| 79 return 0; | 80 return 0; |
| 80 } | 81 } |
| 81 | 82 |
| 82 int testfs_readdir(const char* path, | 83 int testfs_readdir(const char* path, |
| 83 void* buf, | 84 void* buf, |
| 84 fuse_fill_dir_t filler, | 85 fuse_fill_dir_t filler, |
| 85 off_t offset, | 86 off_t offset, |
| 86 struct fuse_file_info*) { | 87 struct fuse_file_info*) { |
| 87 if (strcmp(path, "/") != 0) | 88 if (strcmp(path, "/") != 0) |
| 88 return -ENOENT; | 89 return -ENOENT; |
| 89 | 90 |
| 90 filler(buf, ".", NULL, 0); | 91 filler(buf, ".", NULL, 0); |
| 91 filler(buf, "..", NULL, 0); | 92 filler(buf, "..", NULL, 0); |
| 92 for (Files::iterator iter = g_files.begin(); iter != g_files.end(); ++iter) { | 93 for (Files::iterator iter = g_files.begin(); iter != g_files.end(); ++iter) { |
| 93 filler(buf, iter->name.c_str(), NULL, 0); | 94 filler(buf, iter->name.c_str(), NULL, 0); |
| 94 } | 95 } |
| 95 return 0; | 96 return 0; |
| 96 } | 97 } |
| 97 | 98 |
| 98 int testfs_create(const char* path, mode_t, struct fuse_file_info* fi) { | 99 int testfs_create(const char* path, mode_t mode, struct fuse_file_info* fi) { |
| 99 if (!IsValidPath(path)) | 100 if (!IsValidPath(path)) |
| 100 return -ENOENT; | 101 return -ENOENT; |
| 101 | 102 |
| 102 File* file = FindFile(path); | 103 File* file = FindFile(path); |
| 103 if (file != NULL) { | 104 if (file != NULL) { |
| 104 if (fi->flags & O_EXCL) | 105 if (fi->flags & O_EXCL) |
| 105 return -EEXIST; | 106 return -EEXIST; |
| 106 } else { | 107 } else { |
| 107 g_files.push_back(File()); | 108 g_files.push_back(File()); |
| 108 file = &g_files.back(); | 109 file = &g_files.back(); |
| 109 file->name = &path[1]; // Skip initial / | 110 file->name = &path[1]; // Skip initial / |
| 110 } | 111 } |
| 112 last_create_mode = mode; |
| 111 | 113 |
| 112 return 0; | 114 return 0; |
| 113 } | 115 } |
| 114 | 116 |
| 115 int testfs_open(const char* path, struct fuse_file_info*) { | 117 int testfs_open(const char* path, struct fuse_file_info*) { |
| 116 // open is only called to open an existing file, otherwise create is | 118 // open is only called to open an existing file, otherwise create is |
| 117 // called. We don't need to do any additional work here, the path will be | 119 // called. We don't need to do any additional work here, the path will be |
| 118 // passed to any other operations. | 120 // passed to any other operations. |
| 119 return FindFile(path) != NULL; | 121 return FindFile(path) != NULL; |
| 120 } | 122 } |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 ASSERT_EQ(strlen(hello_world), bytes_read); | 227 ASSERT_EQ(strlen(hello_world), bytes_read); |
| 226 ASSERT_STREQ(hello_world, buffer); | 228 ASSERT_STREQ(hello_world, buffer); |
| 227 | 229 |
| 228 // Try to read past the end of the file. | 230 // Try to read past the end of the file. |
| 229 attr.offs = strlen(hello_world) - 7; | 231 attr.offs = strlen(hello_world) - 7; |
| 230 ASSERT_EQ(0, node->Read(attr, &buffer[0], sizeof(buffer), &bytes_read)); | 232 ASSERT_EQ(0, node->Read(attr, &buffer[0], sizeof(buffer), &bytes_read)); |
| 231 ASSERT_EQ(7, bytes_read); | 233 ASSERT_EQ(7, bytes_read); |
| 232 ASSERT_STREQ("World!\n", buffer); | 234 ASSERT_STREQ("World!\n", buffer); |
| 233 } | 235 } |
| 234 | 236 |
| 237 TEST_F(FuseFsTest, CreateWithMode) { |
| 238 ScopedNode node; |
| 239 struct stat statbuf; |
| 240 |
| 241 ASSERT_EQ(0, fs_.OpenWithMode(Path("/hello"), |
| 242 O_RDWR | O_CREAT, 0723, &node)); |
| 243 EXPECT_EQ(0, node->GetStat(&statbuf)); |
| 244 EXPECT_EQ(S_IFREG, statbuf.st_mode & S_IFMT); |
| 245 EXPECT_EQ(0723, statbuf.st_mode & ~S_IFMT); |
| 246 } |
| 247 |
| 235 TEST_F(FuseFsTest, CreateAndWrite) { | 248 TEST_F(FuseFsTest, CreateAndWrite) { |
| 236 ScopedNode node; | 249 ScopedNode node; |
| 237 ASSERT_EQ(0, fs_.Open(Path("/foobar"), O_RDWR | O_CREAT, &node)); | 250 ASSERT_EQ(0, fs_.Open(Path("/foobar"), O_RDWR | O_CREAT, &node)); |
| 238 | 251 |
| 239 HandleAttr attr; | 252 HandleAttr attr; |
| 240 const char message[] = "Something interesting"; | 253 const char message[] = "Something interesting"; |
| 241 int bytes_written; | 254 int bytes_written; |
| 242 ASSERT_EQ(0, node->Write(attr, &message[0], strlen(message), &bytes_written)); | 255 ASSERT_EQ(0, node->Write(attr, &message[0], strlen(message), &bytes_written)); |
| 243 ASSERT_EQ(bytes_written, strlen(message)); | 256 ASSERT_EQ(bytes_written, strlen(message)); |
| 244 | 257 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 | 347 |
| 335 void KernelProxyFuseTest::TearDown() { | 348 void KernelProxyFuseTest::TearDown() { |
| 336 nacl_io_unregister_fs_type("flatfs"); | 349 nacl_io_unregister_fs_type("flatfs"); |
| 337 ki_uninit(); | 350 ki_uninit(); |
| 338 } | 351 } |
| 339 | 352 |
| 340 } // namespace | 353 } // namespace |
| 341 | 354 |
| 342 TEST_F(KernelProxyFuseTest, Basic) { | 355 TEST_F(KernelProxyFuseTest, Basic) { |
| 343 // Write a file. | 356 // Write a file. |
| 344 int fd = ki_open("/hello", O_WRONLY | O_CREAT); | 357 int fd = ki_open("/hello", O_WRONLY | O_CREAT, 0777); |
| 345 ASSERT_GT(fd, -1); | 358 ASSERT_GT(fd, -1); |
| 346 ASSERT_EQ(sizeof(hello_world), | 359 ASSERT_EQ(sizeof(hello_world), |
| 347 ki_write(fd, hello_world, sizeof(hello_world))); | 360 ki_write(fd, hello_world, sizeof(hello_world))); |
| 348 EXPECT_EQ(0, ki_close(fd)); | 361 EXPECT_EQ(0, ki_close(fd)); |
| 349 | 362 |
| 350 // Then read it back in. | 363 // Then read it back in. |
| 351 fd = ki_open("/hello", O_RDONLY); | 364 fd = ki_open("/hello", O_RDONLY, 0); |
| 352 ASSERT_GT(fd, -1); | 365 ASSERT_GT(fd, -1); |
| 353 | 366 |
| 354 char buffer[30]; | 367 char buffer[30]; |
| 355 memset(buffer, 0, sizeof(buffer)); | 368 memset(buffer, 0, sizeof(buffer)); |
| 356 ASSERT_EQ(sizeof(hello_world), ki_read(fd, buffer, sizeof(buffer))); | 369 ASSERT_EQ(sizeof(hello_world), ki_read(fd, buffer, sizeof(buffer))); |
| 357 EXPECT_STREQ(hello_world, buffer); | 370 EXPECT_STREQ(hello_world, buffer); |
| 358 EXPECT_EQ(0, ki_close(fd)); | 371 EXPECT_EQ(0, ki_close(fd)); |
| 359 } | 372 } |
| OLD | NEW |