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 <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "components/filesystem/files_test_base.h" | 12 #include "components/filesystem/files_test_base.h" |
13 #include "mojo/common/common_type_converters.h" | |
14 #include "mojo/util/capture_util.h" | 13 #include "mojo/util/capture_util.h" |
15 | 14 |
16 using mojo::Capture; | 15 using mojo::Capture; |
17 | 16 |
18 namespace filesystem { | 17 namespace filesystem { |
19 namespace { | 18 namespace { |
20 | 19 |
21 using DirectoryImplTest = FilesTestBase; | 20 using DirectoryImplTest = FilesTestBase; |
22 | 21 |
23 TEST_F(DirectoryImplTest, Read) { | 22 TEST_F(DirectoryImplTest, Read) { |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 // Attempt to open that directory as a file. This must fail! | 146 // Attempt to open that directory as a file. This must fail! |
148 FilePtr file; | 147 FilePtr file; |
149 error = FileError::FAILED; | 148 error = FileError::FAILED; |
150 directory->OpenFile("my_file", GetProxy(&file), kFlagRead | kFlagOpen, | 149 directory->OpenFile("my_file", GetProxy(&file), kFlagRead | kFlagOpen, |
151 Capture(&error)); | 150 Capture(&error)); |
152 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 151 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
153 EXPECT_EQ(FileError::NOT_A_FILE, error); | 152 EXPECT_EQ(FileError::NOT_A_FILE, error); |
154 } | 153 } |
155 } | 154 } |
156 | 155 |
157 TEST_F(DirectoryImplTest, WriteFileReadFile) { | |
158 DirectoryPtr directory; | |
159 GetTemporaryRoot(&directory); | |
160 FileError error; | |
161 | |
162 std::string data("one two three"); | |
163 { | |
164 directory->WriteFile("data", mojo::Array<uint8_t>::From(data), | |
165 Capture(&error)); | |
166 ASSERT_TRUE(directory.WaitForIncomingResponse()); | |
167 EXPECT_EQ(FileError::OK, error); | |
168 } | |
169 | |
170 { | |
171 mojo::Array<uint8_t> file_contents; | |
172 directory->ReadEntireFile("data", Capture(&error, &file_contents)); | |
173 ASSERT_TRUE(directory.WaitForIncomingResponse()); | |
174 EXPECT_EQ(FileError::OK, error); | |
175 | |
176 EXPECT_EQ(data, file_contents.To<std::string>()); | |
177 } | |
178 } | |
179 | |
180 TEST_F(DirectoryImplTest, ReadEmptyFileIsNotFoundError) { | |
181 DirectoryPtr directory; | |
182 GetTemporaryRoot(&directory); | |
183 FileError error; | |
184 | |
185 { | |
186 mojo::Array<uint8_t> file_contents; | |
187 directory->ReadEntireFile("doesnt_exist", Capture(&error, &file_contents)); | |
188 ASSERT_TRUE(directory.WaitForIncomingResponse()); | |
189 EXPECT_EQ(FileError::NOT_FOUND, error); | |
190 } | |
191 } | |
192 | |
193 TEST_F(DirectoryImplTest, CantReadEntireFileOnADirectory) { | |
194 DirectoryPtr directory; | |
195 GetTemporaryRoot(&directory); | |
196 FileError error; | |
197 | |
198 // Create a directory | |
199 { | |
200 DirectoryPtr my_file_directory; | |
201 error = FileError::FAILED; | |
202 directory->OpenDirectory( | |
203 "my_dir", GetProxy(&my_file_directory), | |
204 kFlagRead | kFlagWrite | kFlagCreate, | |
205 Capture(&error)); | |
206 ASSERT_TRUE(directory.WaitForIncomingResponse()); | |
207 EXPECT_EQ(FileError::OK, error); | |
208 } | |
209 | |
210 // Try to read it as a file | |
211 { | |
212 mojo::Array<uint8_t> file_contents; | |
213 directory->ReadEntireFile("my_dir", Capture(&error, &file_contents)); | |
214 ASSERT_TRUE(directory.WaitForIncomingResponse()); | |
215 EXPECT_EQ(FileError::NOT_A_FILE, error); | |
216 } | |
217 } | |
218 | |
219 TEST_F(DirectoryImplTest, CantWriteFileOnADirectory) { | |
220 DirectoryPtr directory; | |
221 GetTemporaryRoot(&directory); | |
222 FileError error; | |
223 | |
224 // Create a directory | |
225 { | |
226 DirectoryPtr my_file_directory; | |
227 error = FileError::FAILED; | |
228 directory->OpenDirectory( | |
229 "my_dir", GetProxy(&my_file_directory), | |
230 kFlagRead | kFlagWrite | kFlagCreate, | |
231 Capture(&error)); | |
232 ASSERT_TRUE(directory.WaitForIncomingResponse()); | |
233 EXPECT_EQ(FileError::OK, error); | |
234 } | |
235 | |
236 { | |
237 std::string data("one two three"); | |
238 directory->WriteFile("my_dir", mojo::Array<uint8_t>::From(data), | |
239 Capture(&error)); | |
240 ASSERT_TRUE(directory.WaitForIncomingResponse()); | |
241 EXPECT_EQ(FileError::NOT_A_FILE, error); | |
242 } | |
243 } | |
244 | 156 |
245 // TODO(vtl): Test delete flags. | 157 // TODO(vtl): Test delete flags. |
246 | 158 |
247 } // namespace | 159 } // namespace |
248 } // namespace filesystem | 160 } // namespace filesystem |
OLD | NEW |