| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <map> | 5 #include <map> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "services/files/files_test_base.h" | 8 #include "services/files/files_test_base.h" |
| 9 | 9 |
| 10 namespace mojo { | 10 namespace mojo { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 // Make some files. | 21 // Make some files. |
| 22 const struct { | 22 const struct { |
| 23 const char* name; | 23 const char* name; |
| 24 uint32_t open_flags; | 24 uint32_t open_flags; |
| 25 } files_to_create[] = { | 25 } files_to_create[] = { |
| 26 {"my_file1", kOpenFlagRead | kOpenFlagWrite | kOpenFlagCreate}, | 26 {"my_file1", kOpenFlagRead | kOpenFlagWrite | kOpenFlagCreate}, |
| 27 {"my_file2", kOpenFlagWrite | kOpenFlagCreate | kOpenFlagExclusive}, | 27 {"my_file2", kOpenFlagWrite | kOpenFlagCreate | kOpenFlagExclusive}, |
| 28 {"my_file3", kOpenFlagWrite | kOpenFlagCreate | kOpenFlagAppend}, | 28 {"my_file3", kOpenFlagWrite | kOpenFlagCreate | kOpenFlagAppend}, |
| 29 {"my_file4", kOpenFlagWrite | kOpenFlagCreate | kOpenFlagTruncate}}; | 29 {"my_file4", kOpenFlagWrite | kOpenFlagCreate | kOpenFlagTruncate}}; |
| 30 for (size_t i = 0; i < arraysize(files_to_create); i++) { | 30 for (size_t i = 0; i < arraysize(files_to_create); i++) { |
| 31 error = ERROR_INTERNAL; | 31 error = Error::INTERNAL; |
| 32 directory->OpenFile(files_to_create[i].name, nullptr, | 32 directory->OpenFile(files_to_create[i].name, nullptr, |
| 33 files_to_create[i].open_flags, Capture(&error)); | 33 files_to_create[i].open_flags, Capture(&error)); |
| 34 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 34 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 35 EXPECT_EQ(ERROR_OK, error); | 35 EXPECT_EQ(Error::OK, error); |
| 36 } | 36 } |
| 37 // Make a directory. | 37 // Make a directory. |
| 38 error = ERROR_INTERNAL; | 38 error = Error::INTERNAL; |
| 39 directory->OpenDirectory("my_dir", nullptr, | 39 directory->OpenDirectory("my_dir", nullptr, |
| 40 kOpenFlagRead | kOpenFlagWrite | kOpenFlagCreate, | 40 kOpenFlagRead | kOpenFlagWrite | kOpenFlagCreate, |
| 41 Capture(&error)); | 41 Capture(&error)); |
| 42 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 42 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 43 EXPECT_EQ(ERROR_OK, error); | 43 EXPECT_EQ(Error::OK, error); |
| 44 | 44 |
| 45 error = ERROR_INTERNAL; | 45 error = Error::INTERNAL; |
| 46 Array<DirectoryEntryPtr> directory_contents; | 46 Array<DirectoryEntryPtr> directory_contents; |
| 47 directory->Read(Capture(&error, &directory_contents)); | 47 directory->Read(Capture(&error, &directory_contents)); |
| 48 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 48 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 49 EXPECT_EQ(ERROR_OK, error); | 49 EXPECT_EQ(Error::OK, error); |
| 50 | 50 |
| 51 // Expected contents of the directory. | 51 // Expected contents of the directory. |
| 52 std::map<std::string, FileType> expected_contents; | 52 std::map<std::string, FileType> expected_contents; |
| 53 expected_contents["my_file1"] = FILE_TYPE_REGULAR_FILE; | 53 expected_contents["my_file1"] = FileType::REGULAR_FILE; |
| 54 expected_contents["my_file2"] = FILE_TYPE_REGULAR_FILE; | 54 expected_contents["my_file2"] = FileType::REGULAR_FILE; |
| 55 expected_contents["my_file3"] = FILE_TYPE_REGULAR_FILE; | 55 expected_contents["my_file3"] = FileType::REGULAR_FILE; |
| 56 expected_contents["my_file4"] = FILE_TYPE_REGULAR_FILE; | 56 expected_contents["my_file4"] = FileType::REGULAR_FILE; |
| 57 expected_contents["my_dir"] = FILE_TYPE_DIRECTORY; | 57 expected_contents["my_dir"] = FileType::DIRECTORY; |
| 58 expected_contents["."] = FILE_TYPE_DIRECTORY; | 58 expected_contents["."] = FileType::DIRECTORY; |
| 59 expected_contents[".."] = FILE_TYPE_DIRECTORY; | 59 expected_contents[".."] = FileType::DIRECTORY; |
| 60 | 60 |
| 61 EXPECT_EQ(expected_contents.size(), directory_contents.size()); | 61 EXPECT_EQ(expected_contents.size(), directory_contents.size()); |
| 62 for (size_t i = 0; i < directory_contents.size(); i++) { | 62 for (size_t i = 0; i < directory_contents.size(); i++) { |
| 63 ASSERT_TRUE(directory_contents[i]); | 63 ASSERT_TRUE(directory_contents[i]); |
| 64 ASSERT_TRUE(directory_contents[i]->name); | 64 ASSERT_TRUE(directory_contents[i]->name); |
| 65 auto it = expected_contents.find(directory_contents[i]->name.get()); | 65 auto it = expected_contents.find(directory_contents[i]->name.get()); |
| 66 ASSERT_TRUE(it != expected_contents.end()); | 66 ASSERT_TRUE(it != expected_contents.end()); |
| 67 EXPECT_EQ(it->second, directory_contents[i]->type); | 67 EXPECT_EQ(it->second, directory_contents[i]->type); |
| 68 expected_contents.erase(it); | 68 expected_contents.erase(it); |
| 69 } | 69 } |
| 70 } | 70 } |
| 71 | 71 |
| 72 // Note: Ignore nanoseconds, since it may not always be supported. We expect at | 72 // Note: Ignore nanoseconds, since it may not always be supported. We expect at |
| 73 // least second-resolution support though. | 73 // least second-resolution support though. |
| 74 // TODO(vtl): Maybe share this with |FileImplTest.StatTouch| ... but then it'd | 74 // TODO(vtl): Maybe share this with |FileImplTest.StatTouch| ... but then it'd |
| 75 // be harder to split this file. | 75 // be harder to split this file. |
| 76 TEST_F(DirectoryImplTest, StatTouch) { | 76 TEST_F(DirectoryImplTest, StatTouch) { |
| 77 DirectoryPtr directory; | 77 DirectoryPtr directory; |
| 78 GetTemporaryRoot(&directory); | 78 GetTemporaryRoot(&directory); |
| 79 Error error; | 79 Error error; |
| 80 | 80 |
| 81 // Stat it. | 81 // Stat it. |
| 82 error = ERROR_INTERNAL; | 82 error = Error::INTERNAL; |
| 83 FileInformationPtr file_info; | 83 FileInformationPtr file_info; |
| 84 directory->Stat(Capture(&error, &file_info)); | 84 directory->Stat(Capture(&error, &file_info)); |
| 85 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 85 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 86 EXPECT_EQ(ERROR_OK, error); | 86 EXPECT_EQ(Error::OK, error); |
| 87 ASSERT_FALSE(file_info.is_null()); | 87 ASSERT_FALSE(file_info.is_null()); |
| 88 EXPECT_EQ(FILE_TYPE_DIRECTORY, file_info->type); | 88 EXPECT_EQ(FileType::DIRECTORY, file_info->type); |
| 89 EXPECT_EQ(0, file_info->size); | 89 EXPECT_EQ(0, file_info->size); |
| 90 ASSERT_FALSE(file_info->atime.is_null()); | 90 ASSERT_FALSE(file_info->atime.is_null()); |
| 91 EXPECT_GT(file_info->atime->seconds, 0); // Expect that it's not 1970-01-01. | 91 EXPECT_GT(file_info->atime->seconds, 0); // Expect that it's not 1970-01-01. |
| 92 ASSERT_FALSE(file_info->mtime.is_null()); | 92 ASSERT_FALSE(file_info->mtime.is_null()); |
| 93 EXPECT_GT(file_info->mtime->seconds, 0); | 93 EXPECT_GT(file_info->mtime->seconds, 0); |
| 94 int64_t first_mtime = file_info->mtime->seconds; | 94 int64_t first_mtime = file_info->mtime->seconds; |
| 95 | 95 |
| 96 // Touch only the atime. | 96 // Touch only the atime. |
| 97 error = ERROR_INTERNAL; | 97 error = Error::INTERNAL; |
| 98 TimespecOrNowPtr t(TimespecOrNow::New()); | 98 TimespecOrNowPtr t(TimespecOrNow::New()); |
| 99 t->now = false; | 99 t->now = false; |
| 100 t->timespec = Timespec::New(); | 100 t->timespec = Timespec::New(); |
| 101 const int64_t kPartyTime1 = 1234567890; // Party like it's 2009-02-13. | 101 const int64_t kPartyTime1 = 1234567890; // Party like it's 2009-02-13. |
| 102 t->timespec->seconds = kPartyTime1; | 102 t->timespec->seconds = kPartyTime1; |
| 103 directory->Touch(t.Pass(), nullptr, Capture(&error)); | 103 directory->Touch(t.Pass(), nullptr, Capture(&error)); |
| 104 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 104 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 105 EXPECT_EQ(ERROR_OK, error); | 105 EXPECT_EQ(Error::OK, error); |
| 106 | 106 |
| 107 // Stat again. | 107 // Stat again. |
| 108 error = ERROR_INTERNAL; | 108 error = Error::INTERNAL; |
| 109 file_info.reset(); | 109 file_info.reset(); |
| 110 directory->Stat(Capture(&error, &file_info)); | 110 directory->Stat(Capture(&error, &file_info)); |
| 111 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 111 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 112 EXPECT_EQ(ERROR_OK, error); | 112 EXPECT_EQ(Error::OK, error); |
| 113 ASSERT_FALSE(file_info.is_null()); | 113 ASSERT_FALSE(file_info.is_null()); |
| 114 ASSERT_FALSE(file_info->atime.is_null()); | 114 ASSERT_FALSE(file_info->atime.is_null()); |
| 115 EXPECT_EQ(kPartyTime1, file_info->atime->seconds); | 115 EXPECT_EQ(kPartyTime1, file_info->atime->seconds); |
| 116 ASSERT_FALSE(file_info->mtime.is_null()); | 116 ASSERT_FALSE(file_info->mtime.is_null()); |
| 117 EXPECT_EQ(first_mtime, file_info->mtime->seconds); | 117 EXPECT_EQ(first_mtime, file_info->mtime->seconds); |
| 118 | 118 |
| 119 // Touch only the mtime. | 119 // Touch only the mtime. |
| 120 t = TimespecOrNow::New(); | 120 t = TimespecOrNow::New(); |
| 121 t->now = false; | 121 t->now = false; |
| 122 t->timespec = Timespec::New(); | 122 t->timespec = Timespec::New(); |
| 123 const int64_t kPartyTime2 = 1425059525; // No time like the present. | 123 const int64_t kPartyTime2 = 1425059525; // No time like the present. |
| 124 t->timespec->seconds = kPartyTime2; | 124 t->timespec->seconds = kPartyTime2; |
| 125 directory->Touch(nullptr, t.Pass(), Capture(&error)); | 125 directory->Touch(nullptr, t.Pass(), Capture(&error)); |
| 126 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 126 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 127 EXPECT_EQ(ERROR_OK, error); | 127 EXPECT_EQ(Error::OK, error); |
| 128 | 128 |
| 129 // Stat again. | 129 // Stat again. |
| 130 error = ERROR_INTERNAL; | 130 error = Error::INTERNAL; |
| 131 file_info.reset(); | 131 file_info.reset(); |
| 132 directory->Stat(Capture(&error, &file_info)); | 132 directory->Stat(Capture(&error, &file_info)); |
| 133 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 133 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 134 EXPECT_EQ(ERROR_OK, error); | 134 EXPECT_EQ(Error::OK, error); |
| 135 ASSERT_FALSE(file_info.is_null()); | 135 ASSERT_FALSE(file_info.is_null()); |
| 136 ASSERT_FALSE(file_info->atime.is_null()); | 136 ASSERT_FALSE(file_info->atime.is_null()); |
| 137 EXPECT_EQ(kPartyTime1, file_info->atime->seconds); | 137 EXPECT_EQ(kPartyTime1, file_info->atime->seconds); |
| 138 ASSERT_FALSE(file_info->mtime.is_null()); | 138 ASSERT_FALSE(file_info->mtime.is_null()); |
| 139 EXPECT_EQ(kPartyTime2, file_info->mtime->seconds); | 139 EXPECT_EQ(kPartyTime2, file_info->mtime->seconds); |
| 140 | 140 |
| 141 // TODO(vtl): Also test Touch() "now" options. | 141 // TODO(vtl): Also test Touch() "now" options. |
| 142 // TODO(vtl): Also test touching both atime and mtime. | 142 // TODO(vtl): Also test touching both atime and mtime. |
| 143 } | 143 } |
| 144 | 144 |
| 145 // TODO(vtl): Properly test OpenFile() and OpenDirectory() (including flags). | 145 // TODO(vtl): Properly test OpenFile() and OpenDirectory() (including flags). |
| 146 | 146 |
| 147 TEST_F(DirectoryImplTest, BasicRenameDelete) { | 147 TEST_F(DirectoryImplTest, BasicRenameDelete) { |
| 148 DirectoryPtr directory; | 148 DirectoryPtr directory; |
| 149 GetTemporaryRoot(&directory); | 149 GetTemporaryRoot(&directory); |
| 150 Error error; | 150 Error error; |
| 151 | 151 |
| 152 // Create my_file. | 152 // Create my_file. |
| 153 error = ERROR_INTERNAL; | 153 error = Error::INTERNAL; |
| 154 directory->OpenFile("my_file", nullptr, kOpenFlagWrite | kOpenFlagCreate, | 154 directory->OpenFile("my_file", nullptr, kOpenFlagWrite | kOpenFlagCreate, |
| 155 Capture(&error)); | 155 Capture(&error)); |
| 156 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 156 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 157 EXPECT_EQ(ERROR_OK, error); | 157 EXPECT_EQ(Error::OK, error); |
| 158 | 158 |
| 159 // Opening my_file should succeed. | 159 // Opening my_file should succeed. |
| 160 error = ERROR_INTERNAL; | 160 error = Error::INTERNAL; |
| 161 directory->OpenFile("my_file", nullptr, kOpenFlagRead, Capture(&error)); | 161 directory->OpenFile("my_file", nullptr, kOpenFlagRead, Capture(&error)); |
| 162 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 162 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 163 EXPECT_EQ(ERROR_OK, error); | 163 EXPECT_EQ(Error::OK, error); |
| 164 | 164 |
| 165 // Rename my_file to my_new_file. | 165 // Rename my_file to my_new_file. |
| 166 directory->Rename("my_file", "my_new_file", Capture(&error)); | 166 directory->Rename("my_file", "my_new_file", Capture(&error)); |
| 167 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 167 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 168 EXPECT_EQ(ERROR_OK, error); | 168 EXPECT_EQ(Error::OK, error); |
| 169 | 169 |
| 170 // Opening my_file should fail. | 170 // Opening my_file should fail. |
| 171 error = ERROR_INTERNAL; | 171 error = Error::INTERNAL; |
| 172 directory->OpenFile("my_file", nullptr, kOpenFlagRead, Capture(&error)); | 172 directory->OpenFile("my_file", nullptr, kOpenFlagRead, Capture(&error)); |
| 173 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 173 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 174 EXPECT_EQ(ERROR_UNKNOWN, error); | 174 EXPECT_EQ(Error::UNKNOWN, error); |
| 175 | 175 |
| 176 // Opening my_new_file should succeed. | 176 // Opening my_new_file should succeed. |
| 177 error = ERROR_INTERNAL; | 177 error = Error::INTERNAL; |
| 178 directory->OpenFile("my_new_file", nullptr, kOpenFlagRead, Capture(&error)); | 178 directory->OpenFile("my_new_file", nullptr, kOpenFlagRead, Capture(&error)); |
| 179 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 179 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 180 EXPECT_EQ(ERROR_OK, error); | 180 EXPECT_EQ(Error::OK, error); |
| 181 | 181 |
| 182 // Delete my_new_file (no flags). | 182 // Delete my_new_file (no flags). |
| 183 directory->Delete("my_new_file", 0, Capture(&error)); | 183 directory->Delete("my_new_file", 0, Capture(&error)); |
| 184 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 184 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 185 EXPECT_EQ(ERROR_OK, error); | 185 EXPECT_EQ(Error::OK, error); |
| 186 | 186 |
| 187 // Opening my_new_file should fail. | 187 // Opening my_new_file should fail. |
| 188 error = ERROR_INTERNAL; | 188 error = Error::INTERNAL; |
| 189 directory->OpenFile("my_new_file", nullptr, kOpenFlagRead, Capture(&error)); | 189 directory->OpenFile("my_new_file", nullptr, kOpenFlagRead, Capture(&error)); |
| 190 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 190 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 191 EXPECT_EQ(ERROR_UNKNOWN, error); | 191 EXPECT_EQ(Error::UNKNOWN, error); |
| 192 } | 192 } |
| 193 | 193 |
| 194 // TODO(vtl): Test that an open file can be moved (by someone else) without | 194 // TODO(vtl): Test that an open file can be moved (by someone else) without |
| 195 // operations on it being affected. | 195 // operations on it being affected. |
| 196 // TODO(vtl): Test delete flags. | 196 // TODO(vtl): Test delete flags. |
| 197 | 197 |
| 198 } // namespace | 198 } // namespace |
| 199 } // namespace files | 199 } // namespace files |
| 200 } // namespace mojo | 200 } // namespace mojo |
| OLD | NEW |