OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ppapi/tests/test_flash_file.h" | 5 #include "ppapi/tests/test_flash_file.h" |
6 | 6 |
7 #include <algorithm> | |
8 #include <vector> | |
9 | |
10 #include "ppapi/c/pp_file_info.h" | |
11 #include "ppapi/c/ppb_file_io.h" | |
12 #include "ppapi/cpp/module.h" | 7 #include "ppapi/cpp/module.h" |
13 #include "ppapi/cpp/private/flash_file.h" | 8 #include "ppapi/cpp/private/flash_file.h" |
14 #include "ppapi/tests/testing_instance.h" | 9 #include "ppapi/tests/testing_instance.h" |
15 #include "ppapi/tests/test_utils.h" | 10 #include "ppapi/tests/test_utils.h" |
16 | 11 |
17 #if defined(PPAPI_OS_WIN) | 12 #if defined(PPAPI_OS_WIN) |
18 #include <windows.h> | 13 #include <windows.h> |
19 #else | 14 #else |
20 #include <errno.h> | 15 #include <errno.h> |
21 #include <unistd.h> | 16 #include <unistd.h> |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 result = bytes_read != -1; | 67 result = bytes_read != -1; |
73 if (bytes_read > 0) | 68 if (bytes_read > 0) |
74 contents->append(buffer, bytes_read); | 69 contents->append(buffer, bytes_read); |
75 } while (bytes_read > 0); | 70 } while (bytes_read > 0); |
76 #endif | 71 #endif |
77 | 72 |
78 delete[] buffer; | 73 delete[] buffer; |
79 return result; | 74 return result; |
80 } | 75 } |
81 | 76 |
82 bool DirEntryEqual(FileModuleLocal::DirEntry i, | |
83 FileModuleLocal::DirEntry j) { | |
84 return i.name == j.name && i.is_dir == j.is_dir; | |
85 } | |
86 | |
87 bool DirEntryLessThan(FileModuleLocal::DirEntry i, | |
88 FileModuleLocal::DirEntry j) { | |
89 if (i.name == j.name) | |
90 return i.is_dir < j.is_dir; | |
91 return i.name < j.name; | |
92 } | |
93 | |
94 } // namespace | 77 } // namespace |
95 | 78 |
96 REGISTER_TEST_CASE(FlashFile); | 79 REGISTER_TEST_CASE(FlashFile); |
97 | 80 |
98 TestFlashFile::TestFlashFile(TestingInstance* instance) | 81 TestFlashFile::TestFlashFile(TestingInstance* instance) |
99 : TestCase(instance) { | 82 : TestCase(instance) { |
100 } | 83 } |
101 | 84 |
102 TestFlashFile::~TestFlashFile() { | 85 TestFlashFile::~TestFlashFile() { |
103 } | 86 } |
104 | 87 |
105 bool TestFlashFile::Init() { | 88 bool TestFlashFile::Init() { |
106 return FileModuleLocal::IsAvailable(); | 89 return FileModuleLocal::IsAvailable(); |
107 } | 90 } |
108 | 91 |
109 void TestFlashFile::RunTests(const std::string& filter) { | 92 void TestFlashFile::RunTests(const std::string& filter) { |
110 RUN_TEST(OpenFile, filter); | |
111 RUN_TEST(RenameFile, filter); | |
112 RUN_TEST(DeleteFileOrDir, filter); | |
113 RUN_TEST(CreateDir, filter); | |
114 RUN_TEST(QueryFile, filter); | |
115 RUN_TEST(GetDirContents, filter); | |
116 RUN_TEST(CreateTemporaryFile, filter); | 93 RUN_TEST(CreateTemporaryFile, filter); |
117 } | 94 } |
118 | 95 |
119 void TestFlashFile::SetUp() { | 96 std::string TestFlashFile::TestCreateTemporaryFile() { |
120 // Clear out existing test data. | 97 ASSERT_TRUE(FileModuleLocal::IsCreateTemporaryFileAvailable()); |
121 FileModuleLocal::DeleteFileOrDir(instance_, std::string(), true); | 98 |
122 // Make sure that the root directory exists. | 99 // Make sure that the root directory exists. |
123 FileModuleLocal::CreateDir(instance_, std::string()); | 100 FileModuleLocal::CreateDir(instance_, std::string()); |
124 } | |
125 | 101 |
126 std::string TestFlashFile::TestOpenFile() { | |
127 SetUp(); | |
128 std::string filename = "abc.txt"; | |
129 PP_FileHandle file_handle = FileModuleLocal::OpenFile(instance_, | |
130 filename, | |
131 PP_FILEOPENFLAG_WRITE | | |
132 PP_FILEOPENFLAG_CREATE); | |
133 ASSERT_NE(PP_kInvalidFileHandle, file_handle); | |
134 | |
135 std::string contents = "This is file."; | |
136 std::string read_contents; | |
137 ASSERT_TRUE(WriteFile(file_handle, contents)); | |
138 ASSERT_FALSE(ReadFile(file_handle, &read_contents)); | |
139 CloseFileHandle(file_handle); | |
140 | |
141 file_handle = FileModuleLocal::OpenFile(instance_, | |
142 filename, | |
143 PP_FILEOPENFLAG_READ); | |
144 ASSERT_NE(PP_kInvalidFileHandle, file_handle); | |
145 | |
146 ASSERT_FALSE(WriteFile(file_handle, contents)); | |
147 ASSERT_TRUE(ReadFile(file_handle, &read_contents)); | |
148 ASSERT_EQ(contents, read_contents); | |
149 CloseFileHandle(file_handle); | |
150 | |
151 PASS(); | |
152 } | |
153 | |
154 std::string TestFlashFile::TestRenameFile() { | |
155 SetUp(); | |
156 std::string filename = "abc.txt"; | |
157 std::string new_filename = "abc_new.txt"; | |
158 std::string contents = "This is file."; | |
159 std::string read_contents; | |
160 | |
161 PP_FileHandle file_handle = FileModuleLocal::OpenFile(instance_, | |
162 filename, | |
163 PP_FILEOPENFLAG_WRITE | | |
164 PP_FILEOPENFLAG_CREATE); | |
165 ASSERT_NE(PP_kInvalidFileHandle, file_handle); | |
166 ASSERT_TRUE(WriteFile(file_handle, contents)); | |
167 CloseFileHandle(file_handle); | |
168 | |
169 ASSERT_TRUE(FileModuleLocal::RenameFile(instance_, filename, new_filename)); | |
170 | |
171 file_handle = FileModuleLocal::OpenFile(instance_, | |
172 new_filename, | |
173 PP_FILEOPENFLAG_READ); | |
174 ASSERT_NE(PP_kInvalidFileHandle, file_handle); | |
175 ASSERT_TRUE(ReadFile(file_handle, &read_contents)); | |
176 ASSERT_EQ(contents, read_contents); | |
177 CloseFileHandle(file_handle); | |
178 | |
179 // Check that the old file no longer exists. | |
180 PP_FileInfo unused; | |
181 ASSERT_FALSE(FileModuleLocal::QueryFile(instance_, filename, &unused)); | |
182 | |
183 PASS(); | |
184 } | |
185 | |
186 std::string TestFlashFile::TestDeleteFileOrDir() { | |
187 SetUp(); | |
188 std::string filename = "abc.txt"; | |
189 std::string dirname = "def"; | |
190 std::string contents = "This is file."; | |
191 | |
192 // Test file deletion. | |
193 PP_FileHandle file_handle = FileModuleLocal::OpenFile(instance_, | |
194 filename, | |
195 PP_FILEOPENFLAG_WRITE | | |
196 PP_FILEOPENFLAG_CREATE); | |
197 ASSERT_NE(PP_kInvalidFileHandle, file_handle); | |
198 ASSERT_TRUE(WriteFile(file_handle, contents)); | |
199 CloseFileHandle(file_handle); | |
200 ASSERT_TRUE(FileModuleLocal::DeleteFileOrDir(instance_, filename, false)); | |
201 PP_FileInfo unused; | |
202 ASSERT_FALSE(FileModuleLocal::QueryFile(instance_, filename, &unused)); | |
203 | |
204 // Test directory deletion. | |
205 ASSERT_TRUE(FileModuleLocal::CreateDir(instance_, dirname)); | |
206 ASSERT_TRUE(FileModuleLocal::DeleteFileOrDir(instance_, dirname, false)); | |
207 ASSERT_FALSE(FileModuleLocal::QueryFile(instance_, dirname, &unused)); | |
208 | |
209 // Test recursive directory deletion. | |
210 ASSERT_TRUE(FileModuleLocal::CreateDir(instance_, dirname)); | |
211 file_handle = FileModuleLocal::OpenFile( | |
212 instance_, dirname + "/" + filename, | |
213 PP_FILEOPENFLAG_WRITE | PP_FILEOPENFLAG_CREATE); | |
214 ASSERT_NE(PP_kInvalidFileHandle, file_handle); | |
215 ASSERT_TRUE(WriteFile(file_handle, contents)); | |
216 CloseFileHandle(file_handle); | |
217 ASSERT_FALSE(FileModuleLocal::DeleteFileOrDir(instance_, dirname, false)); | |
218 ASSERT_TRUE(FileModuleLocal::DeleteFileOrDir(instance_, dirname, true)); | |
219 ASSERT_FALSE(FileModuleLocal::QueryFile(instance_, filename, &unused)); | |
220 | |
221 PASS(); | |
222 } | |
223 | |
224 std::string TestFlashFile::TestCreateDir() { | |
225 SetUp(); | |
226 std::string dirname = "abc"; | |
227 PP_FileInfo info; | |
228 ASSERT_FALSE(FileModuleLocal::QueryFile(instance_, dirname, &info)); | |
229 ASSERT_TRUE(FileModuleLocal::CreateDir(instance_, dirname)); | |
230 ASSERT_TRUE(FileModuleLocal::QueryFile(instance_, dirname, &info)); | |
231 ASSERT_EQ(info.type, PP_FILETYPE_DIRECTORY); | |
232 | |
233 PASS(); | |
234 } | |
235 | |
236 std::string TestFlashFile::TestQueryFile() { | |
237 std::string filename = "abc.txt"; | |
238 std::string dirname = "def"; | |
239 std::string contents = "This is file."; | |
240 PP_FileInfo info; | |
241 | |
242 // Test querying a file. | |
243 PP_FileHandle file_handle = FileModuleLocal::OpenFile(instance_, | |
244 filename, | |
245 PP_FILEOPENFLAG_WRITE | | |
246 PP_FILEOPENFLAG_CREATE); | |
247 ASSERT_NE(PP_kInvalidFileHandle, file_handle); | |
248 ASSERT_TRUE(WriteFile(file_handle, contents)); | |
249 CloseFileHandle(file_handle); | |
250 ASSERT_TRUE(FileModuleLocal::QueryFile(instance_, filename, &info)); | |
251 ASSERT_EQ(static_cast<size_t>(info.size), contents.size()); | |
252 ASSERT_EQ(info.type, PP_FILETYPE_REGULAR); | |
253 // TODO(raymes): Test the other fields. | |
254 | |
255 // Test querying a directory. | |
256 ASSERT_TRUE(FileModuleLocal::CreateDir(instance_, dirname)); | |
257 ASSERT_TRUE(FileModuleLocal::QueryFile(instance_, dirname, &info)); | |
258 ASSERT_EQ(info.type, PP_FILETYPE_DIRECTORY); | |
259 // TODO(raymes): Test the other fields. | |
260 | |
261 // Test querying a non-existent file. | |
262 ASSERT_FALSE(FileModuleLocal::QueryFile(instance_, "xx", &info)); | |
263 | |
264 PASS(); | |
265 } | |
266 | |
267 std::string TestFlashFile::TestGetDirContents() { | |
268 SetUp(); | |
269 std::vector<FileModuleLocal::DirEntry> result; | |
270 ASSERT_TRUE(FileModuleLocal::GetDirContents(instance_, std::string(), | |
271 &result)); | |
272 ASSERT_EQ(result.size(), 1); | |
273 ASSERT_EQ(result[0].name, ".."); | |
274 ASSERT_EQ(result[0].is_dir, true); | |
275 | |
276 std::string filename = "abc.txt"; | |
277 std::string dirname = "def"; | |
278 std::string contents = "This is file."; | |
279 PP_FileHandle file_handle = FileModuleLocal::OpenFile(instance_, | |
280 filename, | |
281 PP_FILEOPENFLAG_WRITE | | |
282 PP_FILEOPENFLAG_CREATE); | |
283 ASSERT_NE(PP_kInvalidFileHandle, file_handle); | |
284 ASSERT_TRUE(WriteFile(file_handle, contents)); | |
285 CloseFileHandle(file_handle); | |
286 ASSERT_TRUE(FileModuleLocal::CreateDir(instance_, dirname)); | |
287 | |
288 ASSERT_TRUE(FileModuleLocal::GetDirContents(instance_, "", &result)); | |
289 FileModuleLocal::DirEntry expected[] = | |
290 { {"..", true}, | |
291 {filename, false}, | |
292 {dirname, true} | |
293 }; | |
294 size_t expected_size = sizeof(expected) / sizeof(expected[0]); | |
295 | |
296 std::sort(expected, expected + expected_size, DirEntryLessThan); | |
297 std::sort(result.begin(), result.end(), DirEntryLessThan); | |
298 | |
299 ASSERT_EQ(expected_size, result.size()); | |
300 ASSERT_TRUE(std::equal(expected, expected + expected_size, result.begin(), | |
301 DirEntryEqual)); | |
302 | |
303 PASS(); | |
304 } | |
305 | |
306 std::string TestFlashFile::TestCreateTemporaryFile() { | |
307 SetUp(); | |
308 size_t before_create = 0; | 102 size_t before_create = 0; |
309 ASSERT_SUBTEST_SUCCESS(GetItemCountUnderModuleLocalRoot(&before_create)); | 103 ASSERT_SUBTEST_SUCCESS(GetItemCountUnderModuleLocalRoot(&before_create)); |
310 | 104 |
311 PP_FileHandle file_handle = FileModuleLocal::CreateTemporaryFile(instance_); | 105 PP_FileHandle file_handle = FileModuleLocal::CreateTemporaryFile(instance_); |
312 ASSERT_NE(PP_kInvalidFileHandle, file_handle); | 106 ASSERT_NE(PP_kInvalidFileHandle, file_handle); |
313 | 107 |
314 std::string contents = "This is a temp file."; | 108 std::string contents = "This is a temp file."; |
315 ASSERT_TRUE(WriteFile(file_handle, contents)); | 109 ASSERT_TRUE(WriteFile(file_handle, contents)); |
316 std::string read_contents; | 110 std::string read_contents; |
317 ASSERT_TRUE(ReadFile(file_handle, &read_contents)); | 111 ASSERT_TRUE(ReadFile(file_handle, &read_contents)); |
318 ASSERT_EQ(contents, read_contents); | 112 ASSERT_EQ(contents, read_contents); |
319 | 113 |
320 CloseFileHandle(file_handle); | 114 CloseFileHandle(file_handle); |
321 | 115 |
322 size_t after_close = 0; | 116 size_t after_close = 0; |
323 ASSERT_SUBTEST_SUCCESS(GetItemCountUnderModuleLocalRoot(&after_close)); | 117 ASSERT_SUBTEST_SUCCESS(GetItemCountUnderModuleLocalRoot(&after_close)); |
324 ASSERT_EQ(before_create, after_close); | 118 ASSERT_EQ(before_create, after_close); |
325 | 119 |
326 PASS(); | 120 PASS(); |
327 } | 121 } |
328 | 122 |
329 std::string TestFlashFile::GetItemCountUnderModuleLocalRoot( | 123 std::string TestFlashFile::GetItemCountUnderModuleLocalRoot( |
330 size_t* item_count) { | 124 size_t* item_count) { |
331 std::vector<FileModuleLocal::DirEntry> contents; | 125 std::vector<FileModuleLocal::DirEntry> contents; |
332 ASSERT_TRUE(FileModuleLocal::GetDirContents(instance_, "", &contents)); | 126 ASSERT_TRUE(FileModuleLocal::GetDirContents(instance_, "", &contents)); |
333 *item_count = contents.size(); | 127 *item_count = contents.size(); |
334 PASS(); | 128 PASS(); |
335 } | 129 } |
OLD | NEW |