| 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 #include <utility> | 7 #include <utility> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/files/file.h" | 10 #include "base/files/file.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 | 36 |
| 37 // Write to it. | 37 // Write to it. |
| 38 std::vector<uint8_t> bytes_to_write; | 38 std::vector<uint8_t> bytes_to_write; |
| 39 bytes_to_write.push_back(static_cast<uint8_t>('h')); | 39 bytes_to_write.push_back(static_cast<uint8_t>('h')); |
| 40 bytes_to_write.push_back(static_cast<uint8_t>('e')); | 40 bytes_to_write.push_back(static_cast<uint8_t>('e')); |
| 41 bytes_to_write.push_back(static_cast<uint8_t>('l')); | 41 bytes_to_write.push_back(static_cast<uint8_t>('l')); |
| 42 bytes_to_write.push_back(static_cast<uint8_t>('l')); | 42 bytes_to_write.push_back(static_cast<uint8_t>('l')); |
| 43 bytes_to_write.push_back(static_cast<uint8_t>('o')); | 43 bytes_to_write.push_back(static_cast<uint8_t>('o')); |
| 44 error = mojom::FileError::FAILED; | 44 error = mojom::FileError::FAILED; |
| 45 uint32_t num_bytes_written = 0; | 45 uint32_t num_bytes_written = 0; |
| 46 handled = | 46 handled = file->Write(bytes_to_write, 0, mojom::Whence::FROM_CURRENT, |
| 47 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, | 47 &error, &num_bytes_written); |
| 48 mojom::Whence::FROM_CURRENT, &error, &num_bytes_written); | |
| 49 ASSERT_TRUE(handled); | 48 ASSERT_TRUE(handled); |
| 50 EXPECT_EQ(mojom::FileError::OK, error); | 49 EXPECT_EQ(mojom::FileError::OK, error); |
| 51 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); | 50 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); |
| 52 | 51 |
| 53 // Close it. | 52 // Close it. |
| 54 error = mojom::FileError::FAILED; | 53 error = mojom::FileError::FAILED; |
| 55 handled = file->Close((&error)); | 54 handled = file->Close((&error)); |
| 56 ASSERT_TRUE(handled); | 55 ASSERT_TRUE(handled); |
| 57 EXPECT_EQ(mojom::FileError::OK, error); | 56 EXPECT_EQ(mojom::FileError::OK, error); |
| 58 } | 57 } |
| 59 | 58 |
| 60 // Rename it. | 59 // Rename it. |
| 61 error = mojom::FileError::FAILED; | 60 error = mojom::FileError::FAILED; |
| 62 handled = directory->Rename("my_file", "your_file", &error); | 61 handled = directory->Rename("my_file", "your_file", &error); |
| 63 ASSERT_TRUE(handled); | 62 ASSERT_TRUE(handled); |
| 64 EXPECT_EQ(mojom::FileError::OK, error); | 63 EXPECT_EQ(mojom::FileError::OK, error); |
| 65 | 64 |
| 66 { | 65 { |
| 67 // Open my_file again. | 66 // Open my_file again. |
| 68 mojom::FilePtr file; | 67 mojom::FilePtr file; |
| 69 error = mojom::FileError::FAILED; | 68 error = mojom::FileError::FAILED; |
| 70 bool handled = | 69 bool handled = |
| 71 directory->OpenFile("your_file", GetProxy(&file), | 70 directory->OpenFile("your_file", GetProxy(&file), |
| 72 mojom::kFlagRead | mojom::kFlagOpen, &error); | 71 mojom::kFlagRead | mojom::kFlagOpen, &error); |
| 73 ASSERT_TRUE(handled); | 72 ASSERT_TRUE(handled); |
| 74 EXPECT_EQ(mojom::FileError::OK, error); | 73 EXPECT_EQ(mojom::FileError::OK, error); |
| 75 | 74 |
| 76 // Read from it. | 75 // Read from it. |
| 77 mojo::Array<uint8_t> bytes_read; | 76 base::Optional<std::vector<uint8_t>> bytes_read; |
| 78 error = mojom::FileError::FAILED; | 77 error = mojom::FileError::FAILED; |
| 79 handled = file->Read(3, 1, mojom::Whence::FROM_BEGIN, &error, &bytes_read); | 78 handled = file->Read(3, 1, mojom::Whence::FROM_BEGIN, &error, &bytes_read); |
| 80 ASSERT_TRUE(handled); | 79 ASSERT_TRUE(handled); |
| 81 EXPECT_EQ(mojom::FileError::OK, error); | 80 EXPECT_EQ(mojom::FileError::OK, error); |
| 82 ASSERT_EQ(3u, bytes_read.size()); | 81 ASSERT_TRUE(bytes_read.has_value()); |
| 83 EXPECT_EQ(static_cast<uint8_t>('e'), bytes_read[0]); | 82 ASSERT_EQ(3u, bytes_read.value().size()); |
| 84 EXPECT_EQ(static_cast<uint8_t>('l'), bytes_read[1]); | 83 EXPECT_EQ(static_cast<uint8_t>('e'), bytes_read.value()[0]); |
| 85 EXPECT_EQ(static_cast<uint8_t>('l'), bytes_read[2]); | 84 EXPECT_EQ(static_cast<uint8_t>('l'), bytes_read.value()[1]); |
| 85 EXPECT_EQ(static_cast<uint8_t>('l'), bytes_read.value()[2]); |
| 86 } | 86 } |
| 87 | 87 |
| 88 // TODO(vtl): Test read/write offset options. | 88 // TODO(vtl): Test read/write offset options. |
| 89 } | 89 } |
| 90 | 90 |
| 91 TEST_F(FileImplTest, CantWriteInReadMode) { | 91 TEST_F(FileImplTest, CantWriteInReadMode) { |
| 92 mojom::DirectoryPtr directory; | 92 mojom::DirectoryPtr directory; |
| 93 GetTemporaryRoot(&directory); | 93 GetTemporaryRoot(&directory); |
| 94 mojom::FileError error; | 94 mojom::FileError error; |
| 95 | 95 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 106 error = mojom::FileError::FAILED; | 106 error = mojom::FileError::FAILED; |
| 107 bool handled = | 107 bool handled = |
| 108 directory->OpenFile("my_file", GetProxy(&file), | 108 directory->OpenFile("my_file", GetProxy(&file), |
| 109 mojom::kFlagWrite | mojom::kFlagCreate, &error); | 109 mojom::kFlagWrite | mojom::kFlagCreate, &error); |
| 110 ASSERT_TRUE(handled); | 110 ASSERT_TRUE(handled); |
| 111 EXPECT_EQ(mojom::FileError::OK, error); | 111 EXPECT_EQ(mojom::FileError::OK, error); |
| 112 | 112 |
| 113 // Write to it. | 113 // Write to it. |
| 114 error = mojom::FileError::FAILED; | 114 error = mojom::FileError::FAILED; |
| 115 uint32_t num_bytes_written = 0; | 115 uint32_t num_bytes_written = 0; |
| 116 handled = | 116 handled = file->Write(bytes_to_write, 0, mojom::Whence::FROM_CURRENT, |
| 117 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, | 117 &error, &num_bytes_written); |
| 118 mojom::Whence::FROM_CURRENT, &error, &num_bytes_written); | |
| 119 ASSERT_TRUE(handled); | 118 ASSERT_TRUE(handled); |
| 120 EXPECT_EQ(mojom::FileError::OK, error); | 119 EXPECT_EQ(mojom::FileError::OK, error); |
| 121 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); | 120 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); |
| 122 | 121 |
| 123 // Close it. | 122 // Close it. |
| 124 error = mojom::FileError::FAILED; | 123 error = mojom::FileError::FAILED; |
| 125 handled = file->Close((&error)); | 124 handled = file->Close((&error)); |
| 126 ASSERT_TRUE(handled); | 125 ASSERT_TRUE(handled); |
| 127 EXPECT_EQ(mojom::FileError::OK, error); | 126 EXPECT_EQ(mojom::FileError::OK, error); |
| 128 } | 127 } |
| 129 | 128 |
| 130 { | 129 { |
| 131 // Open my_file again, this time with read only mode. | 130 // Open my_file again, this time with read only mode. |
| 132 mojom::FilePtr file; | 131 mojom::FilePtr file; |
| 133 error = mojom::FileError::FAILED; | 132 error = mojom::FileError::FAILED; |
| 134 bool handled = | 133 bool handled = |
| 135 directory->OpenFile("my_file", GetProxy(&file), | 134 directory->OpenFile("my_file", GetProxy(&file), |
| 136 mojom::kFlagRead | mojom::kFlagOpen, &error); | 135 mojom::kFlagRead | mojom::kFlagOpen, &error); |
| 137 ASSERT_TRUE(handled); | 136 ASSERT_TRUE(handled); |
| 138 EXPECT_EQ(mojom::FileError::OK, error); | 137 EXPECT_EQ(mojom::FileError::OK, error); |
| 139 | 138 |
| 140 // Try to write in read mode; it should fail. | 139 // Try to write in read mode; it should fail. |
| 141 error = mojom::FileError::OK; | 140 error = mojom::FileError::OK; |
| 142 uint32_t num_bytes_written = 0; | 141 uint32_t num_bytes_written = 0; |
| 143 handled = | 142 handled = file->Write(bytes_to_write, 0, mojom::Whence::FROM_CURRENT, |
| 144 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, | 143 &error, &num_bytes_written); |
| 145 mojom::Whence::FROM_CURRENT, &error, &num_bytes_written); | |
| 146 | 144 |
| 147 ASSERT_TRUE(handled); | 145 ASSERT_TRUE(handled); |
| 148 EXPECT_EQ(mojom::FileError::FAILED, error); | 146 EXPECT_EQ(mojom::FileError::FAILED, error); |
| 149 EXPECT_EQ(0u, num_bytes_written); | 147 EXPECT_EQ(0u, num_bytes_written); |
| 150 | 148 |
| 151 // Close it. | 149 // Close it. |
| 152 error = mojom::FileError::FAILED; | 150 error = mojom::FileError::FAILED; |
| 153 handled = file->Close((&error)); | 151 handled = file->Close((&error)); |
| 154 ASSERT_TRUE(handled); | 152 ASSERT_TRUE(handled); |
| 155 EXPECT_EQ(mojom::FileError::OK, error); | 153 EXPECT_EQ(mojom::FileError::OK, error); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 173 | 171 |
| 174 // Write to it. | 172 // Write to it. |
| 175 std::vector<uint8_t> bytes_to_write; | 173 std::vector<uint8_t> bytes_to_write; |
| 176 bytes_to_write.push_back(static_cast<uint8_t>('h')); | 174 bytes_to_write.push_back(static_cast<uint8_t>('h')); |
| 177 bytes_to_write.push_back(static_cast<uint8_t>('e')); | 175 bytes_to_write.push_back(static_cast<uint8_t>('e')); |
| 178 bytes_to_write.push_back(static_cast<uint8_t>('l')); | 176 bytes_to_write.push_back(static_cast<uint8_t>('l')); |
| 179 bytes_to_write.push_back(static_cast<uint8_t>('l')); | 177 bytes_to_write.push_back(static_cast<uint8_t>('l')); |
| 180 bytes_to_write.push_back(static_cast<uint8_t>('o')); | 178 bytes_to_write.push_back(static_cast<uint8_t>('o')); |
| 181 error = mojom::FileError::FAILED; | 179 error = mojom::FileError::FAILED; |
| 182 uint32_t num_bytes_written = 0; | 180 uint32_t num_bytes_written = 0; |
| 183 handled = | 181 handled = file->Write(bytes_to_write, 0, mojom::Whence::FROM_CURRENT, |
| 184 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, | 182 &error, &num_bytes_written); |
| 185 mojom::Whence::FROM_CURRENT, &error, &num_bytes_written); | |
| 186 ASSERT_TRUE(handled); | 183 ASSERT_TRUE(handled); |
| 187 EXPECT_EQ(mojom::FileError::OK, error); | 184 EXPECT_EQ(mojom::FileError::OK, error); |
| 188 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); | 185 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); |
| 189 | 186 |
| 190 // Close it. | 187 // Close it. |
| 191 error = mojom::FileError::FAILED; | 188 error = mojom::FileError::FAILED; |
| 192 handled = file->Close(&error); | 189 handled = file->Close(&error); |
| 193 ASSERT_TRUE(handled); | 190 ASSERT_TRUE(handled); |
| 194 EXPECT_EQ(mojom::FileError::OK, error); | 191 EXPECT_EQ(mojom::FileError::OK, error); |
| 195 } | 192 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 208 std::vector<uint8_t> bytes_to_write; | 205 std::vector<uint8_t> bytes_to_write; |
| 209 bytes_to_write.push_back(static_cast<uint8_t>('g')); | 206 bytes_to_write.push_back(static_cast<uint8_t>('g')); |
| 210 bytes_to_write.push_back(static_cast<uint8_t>('o')); | 207 bytes_to_write.push_back(static_cast<uint8_t>('o')); |
| 211 bytes_to_write.push_back(static_cast<uint8_t>('o')); | 208 bytes_to_write.push_back(static_cast<uint8_t>('o')); |
| 212 bytes_to_write.push_back(static_cast<uint8_t>('d')); | 209 bytes_to_write.push_back(static_cast<uint8_t>('d')); |
| 213 bytes_to_write.push_back(static_cast<uint8_t>('b')); | 210 bytes_to_write.push_back(static_cast<uint8_t>('b')); |
| 214 bytes_to_write.push_back(static_cast<uint8_t>('y')); | 211 bytes_to_write.push_back(static_cast<uint8_t>('y')); |
| 215 bytes_to_write.push_back(static_cast<uint8_t>('e')); | 212 bytes_to_write.push_back(static_cast<uint8_t>('e')); |
| 216 error = mojom::FileError::FAILED; | 213 error = mojom::FileError::FAILED; |
| 217 uint32_t num_bytes_written = 0; | 214 uint32_t num_bytes_written = 0; |
| 218 handled = | 215 handled = file->Write(bytes_to_write, 0, mojom::Whence::FROM_CURRENT, |
| 219 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, | 216 &error, &num_bytes_written); |
| 220 mojom::Whence::FROM_CURRENT, &error, &num_bytes_written); | |
| 221 ASSERT_TRUE(handled); | 217 ASSERT_TRUE(handled); |
| 222 EXPECT_EQ(mojom::FileError::OK, error); | 218 EXPECT_EQ(mojom::FileError::OK, error); |
| 223 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); | 219 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); |
| 224 | 220 |
| 225 // Close it. | 221 // Close it. |
| 226 error = mojom::FileError::FAILED; | 222 error = mojom::FileError::FAILED; |
| 227 handled = file->Close((&error)); | 223 handled = file->Close((&error)); |
| 228 ASSERT_TRUE(handled); | 224 ASSERT_TRUE(handled); |
| 229 EXPECT_EQ(mojom::FileError::OK, error); | 225 EXPECT_EQ(mojom::FileError::OK, error); |
| 230 } | 226 } |
| 231 | 227 |
| 232 { | 228 { |
| 233 // Open my_file again. | 229 // Open my_file again. |
| 234 mojom::FilePtr file; | 230 mojom::FilePtr file; |
| 235 error = mojom::FileError::FAILED; | 231 error = mojom::FileError::FAILED; |
| 236 bool handled = | 232 bool handled = |
| 237 directory->OpenFile("my_file", GetProxy(&file), | 233 directory->OpenFile("my_file", GetProxy(&file), |
| 238 mojom::kFlagRead | mojom::kFlagOpen, &error); | 234 mojom::kFlagRead | mojom::kFlagOpen, &error); |
| 239 ASSERT_TRUE(handled); | 235 ASSERT_TRUE(handled); |
| 240 EXPECT_EQ(mojom::FileError::OK, error); | 236 EXPECT_EQ(mojom::FileError::OK, error); |
| 241 | 237 |
| 242 // Read from it. | 238 // Read from it. |
| 243 mojo::Array<uint8_t> bytes_read; | 239 base::Optional<std::vector<uint8_t>> bytes_read; |
| 244 error = mojom::FileError::FAILED; | 240 error = mojom::FileError::FAILED; |
| 245 handled = file->Read(12, 0, mojom::Whence::FROM_BEGIN, &error, &bytes_read); | 241 handled = file->Read(12, 0, mojom::Whence::FROM_BEGIN, &error, &bytes_read); |
| 246 ASSERT_TRUE(handled); | 242 ASSERT_TRUE(handled); |
| 247 EXPECT_EQ(mojom::FileError::OK, error); | 243 EXPECT_EQ(mojom::FileError::OK, error); |
| 248 ASSERT_EQ(12u, bytes_read.size()); | 244 ASSERT_TRUE(bytes_read.has_value()); |
| 249 EXPECT_EQ(static_cast<uint8_t>('l'), bytes_read[3]); | 245 ASSERT_EQ(12u, bytes_read.value().size()); |
| 250 EXPECT_EQ(static_cast<uint8_t>('o'), bytes_read[4]); | 246 EXPECT_EQ(static_cast<uint8_t>('l'), bytes_read.value()[3]); |
| 251 EXPECT_EQ(static_cast<uint8_t>('g'), bytes_read[5]); | 247 EXPECT_EQ(static_cast<uint8_t>('o'), bytes_read.value()[4]); |
| 252 EXPECT_EQ(static_cast<uint8_t>('o'), bytes_read[6]); | 248 EXPECT_EQ(static_cast<uint8_t>('g'), bytes_read.value()[5]); |
| 249 EXPECT_EQ(static_cast<uint8_t>('o'), bytes_read.value()[6]); |
| 253 } | 250 } |
| 254 } | 251 } |
| 255 | 252 |
| 256 TEST_F(FileImplTest, OpenInTruncateMode) { | 253 TEST_F(FileImplTest, OpenInTruncateMode) { |
| 257 mojom::DirectoryPtr directory; | 254 mojom::DirectoryPtr directory; |
| 258 GetTemporaryRoot(&directory); | 255 GetTemporaryRoot(&directory); |
| 259 mojom::FileError error; | 256 mojom::FileError error; |
| 260 | 257 |
| 261 { | 258 { |
| 262 // Create my_file. | 259 // Create my_file. |
| 263 mojom::FilePtr file; | 260 mojom::FilePtr file; |
| 264 error = mojom::FileError::FAILED; | 261 error = mojom::FileError::FAILED; |
| 265 bool handled = | 262 bool handled = |
| 266 directory->OpenFile("my_file", GetProxy(&file), | 263 directory->OpenFile("my_file", GetProxy(&file), |
| 267 mojom::kFlagWrite | mojom::kFlagCreate, &error); | 264 mojom::kFlagWrite | mojom::kFlagCreate, &error); |
| 268 ASSERT_TRUE(handled); | 265 ASSERT_TRUE(handled); |
| 269 EXPECT_EQ(mojom::FileError::OK, error); | 266 EXPECT_EQ(mojom::FileError::OK, error); |
| 270 | 267 |
| 271 // Write to it. | 268 // Write to it. |
| 272 std::vector<uint8_t> bytes_to_write; | 269 std::vector<uint8_t> bytes_to_write; |
| 273 bytes_to_write.push_back(static_cast<uint8_t>('h')); | 270 bytes_to_write.push_back(static_cast<uint8_t>('h')); |
| 274 bytes_to_write.push_back(static_cast<uint8_t>('e')); | 271 bytes_to_write.push_back(static_cast<uint8_t>('e')); |
| 275 bytes_to_write.push_back(static_cast<uint8_t>('l')); | 272 bytes_to_write.push_back(static_cast<uint8_t>('l')); |
| 276 bytes_to_write.push_back(static_cast<uint8_t>('l')); | 273 bytes_to_write.push_back(static_cast<uint8_t>('l')); |
| 277 bytes_to_write.push_back(static_cast<uint8_t>('o')); | 274 bytes_to_write.push_back(static_cast<uint8_t>('o')); |
| 278 error = mojom::FileError::FAILED; | 275 error = mojom::FileError::FAILED; |
| 279 uint32_t num_bytes_written = 0; | 276 uint32_t num_bytes_written = 0; |
| 280 handled = | 277 handled = file->Write(bytes_to_write, 0, mojom::Whence::FROM_CURRENT, |
| 281 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, | 278 &error, &num_bytes_written); |
| 282 mojom::Whence::FROM_CURRENT, &error, &num_bytes_written); | |
| 283 ASSERT_TRUE(handled); | 279 ASSERT_TRUE(handled); |
| 284 EXPECT_EQ(mojom::FileError::OK, error); | 280 EXPECT_EQ(mojom::FileError::OK, error); |
| 285 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); | 281 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); |
| 286 | 282 |
| 287 // Close it. | 283 // Close it. |
| 288 error = mojom::FileError::FAILED; | 284 error = mojom::FileError::FAILED; |
| 289 handled = file->Close(&error); | 285 handled = file->Close(&error); |
| 290 ASSERT_TRUE(handled); | 286 ASSERT_TRUE(handled); |
| 291 EXPECT_EQ(mojom::FileError::OK, error); | 287 EXPECT_EQ(mojom::FileError::OK, error); |
| 292 } | 288 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 305 std::vector<uint8_t> bytes_to_write; | 301 std::vector<uint8_t> bytes_to_write; |
| 306 bytes_to_write.push_back(static_cast<uint8_t>('g')); | 302 bytes_to_write.push_back(static_cast<uint8_t>('g')); |
| 307 bytes_to_write.push_back(static_cast<uint8_t>('o')); | 303 bytes_to_write.push_back(static_cast<uint8_t>('o')); |
| 308 bytes_to_write.push_back(static_cast<uint8_t>('o')); | 304 bytes_to_write.push_back(static_cast<uint8_t>('o')); |
| 309 bytes_to_write.push_back(static_cast<uint8_t>('d')); | 305 bytes_to_write.push_back(static_cast<uint8_t>('d')); |
| 310 bytes_to_write.push_back(static_cast<uint8_t>('b')); | 306 bytes_to_write.push_back(static_cast<uint8_t>('b')); |
| 311 bytes_to_write.push_back(static_cast<uint8_t>('y')); | 307 bytes_to_write.push_back(static_cast<uint8_t>('y')); |
| 312 bytes_to_write.push_back(static_cast<uint8_t>('e')); | 308 bytes_to_write.push_back(static_cast<uint8_t>('e')); |
| 313 error = mojom::FileError::FAILED; | 309 error = mojom::FileError::FAILED; |
| 314 uint32_t num_bytes_written = 0; | 310 uint32_t num_bytes_written = 0; |
| 315 handled = | 311 handled = file->Write(bytes_to_write, 0, mojom::Whence::FROM_CURRENT, |
| 316 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, | 312 &error, &num_bytes_written); |
| 317 mojom::Whence::FROM_CURRENT, &error, &num_bytes_written); | |
| 318 ASSERT_TRUE(handled); | 313 ASSERT_TRUE(handled); |
| 319 EXPECT_EQ(mojom::FileError::OK, error); | 314 EXPECT_EQ(mojom::FileError::OK, error); |
| 320 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); | 315 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); |
| 321 | 316 |
| 322 // Close it. | 317 // Close it. |
| 323 error = mojom::FileError::FAILED; | 318 error = mojom::FileError::FAILED; |
| 324 handled = file->Close(&error); | 319 handled = file->Close(&error); |
| 325 ASSERT_TRUE(handled); | 320 ASSERT_TRUE(handled); |
| 326 EXPECT_EQ(mojom::FileError::OK, error); | 321 EXPECT_EQ(mojom::FileError::OK, error); |
| 327 } | 322 } |
| 328 | 323 |
| 329 { | 324 { |
| 330 // Open my_file again. | 325 // Open my_file again. |
| 331 mojom::FilePtr file; | 326 mojom::FilePtr file; |
| 332 error = mojom::FileError::FAILED; | 327 error = mojom::FileError::FAILED; |
| 333 bool handled = | 328 bool handled = |
| 334 directory->OpenFile("my_file", GetProxy(&file), | 329 directory->OpenFile("my_file", GetProxy(&file), |
| 335 mojom::kFlagRead | mojom::kFlagOpen, &error); | 330 mojom::kFlagRead | mojom::kFlagOpen, &error); |
| 336 ASSERT_TRUE(handled); | 331 ASSERT_TRUE(handled); |
| 337 EXPECT_EQ(mojom::FileError::OK, error); | 332 EXPECT_EQ(mojom::FileError::OK, error); |
| 338 | 333 |
| 339 // Read from it. | 334 // Read from it. |
| 340 mojo::Array<uint8_t> bytes_read; | 335 base::Optional<std::vector<uint8_t>> bytes_read; |
| 341 error = mojom::FileError::FAILED; | 336 error = mojom::FileError::FAILED; |
| 342 handled = file->Read(7, 0, mojom::Whence::FROM_BEGIN, &error, &bytes_read); | 337 handled = file->Read(7, 0, mojom::Whence::FROM_BEGIN, &error, &bytes_read); |
| 343 ASSERT_TRUE(handled); | 338 ASSERT_TRUE(handled); |
| 344 EXPECT_EQ(mojom::FileError::OK, error); | 339 EXPECT_EQ(mojom::FileError::OK, error); |
| 345 ASSERT_EQ(7u, bytes_read.size()); | 340 ASSERT_TRUE(bytes_read.has_value()); |
| 346 EXPECT_EQ(static_cast<uint8_t>('g'), bytes_read[0]); | 341 ASSERT_EQ(7u, bytes_read.value().size()); |
| 347 EXPECT_EQ(static_cast<uint8_t>('o'), bytes_read[1]); | 342 EXPECT_EQ(static_cast<uint8_t>('g'), bytes_read.value()[0]); |
| 348 EXPECT_EQ(static_cast<uint8_t>('o'), bytes_read[2]); | 343 EXPECT_EQ(static_cast<uint8_t>('o'), bytes_read.value()[1]); |
| 349 EXPECT_EQ(static_cast<uint8_t>('d'), bytes_read[3]); | 344 EXPECT_EQ(static_cast<uint8_t>('o'), bytes_read.value()[2]); |
| 345 EXPECT_EQ(static_cast<uint8_t>('d'), bytes_read.value()[3]); |
| 350 } | 346 } |
| 351 } | 347 } |
| 352 | 348 |
| 353 // Note: Ignore nanoseconds, since it may not always be supported. We expect at | 349 // Note: Ignore nanoseconds, since it may not always be supported. We expect at |
| 354 // least second-resolution support though. | 350 // least second-resolution support though. |
| 355 TEST_F(FileImplTest, StatTouch) { | 351 TEST_F(FileImplTest, StatTouch) { |
| 356 mojom::DirectoryPtr directory; | 352 mojom::DirectoryPtr directory; |
| 357 GetTemporaryRoot(&directory); | 353 GetTemporaryRoot(&directory); |
| 358 mojom::FileError error; | 354 mojom::FileError error; |
| 359 | 355 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 434 bool handled = | 430 bool handled = |
| 435 directory->OpenFile("my_file", GetProxy(&file), | 431 directory->OpenFile("my_file", GetProxy(&file), |
| 436 mojom::kFlagWrite | mojom::kFlagCreate, &error); | 432 mojom::kFlagWrite | mojom::kFlagCreate, &error); |
| 437 ASSERT_TRUE(handled); | 433 ASSERT_TRUE(handled); |
| 438 EXPECT_EQ(mojom::FileError::OK, error); | 434 EXPECT_EQ(mojom::FileError::OK, error); |
| 439 | 435 |
| 440 // Write to it. | 436 // Write to it. |
| 441 std::vector<uint8_t> bytes_to_write(1000, '!'); | 437 std::vector<uint8_t> bytes_to_write(1000, '!'); |
| 442 error = mojom::FileError::FAILED; | 438 error = mojom::FileError::FAILED; |
| 443 uint32_t num_bytes_written = 0; | 439 uint32_t num_bytes_written = 0; |
| 444 handled = | 440 handled = file->Write(bytes_to_write, 0, mojom::Whence::FROM_CURRENT, &error, |
| 445 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, | 441 &num_bytes_written); |
| 446 mojom::Whence::FROM_CURRENT, &error, &num_bytes_written); | |
| 447 ASSERT_TRUE(handled); | 442 ASSERT_TRUE(handled); |
| 448 EXPECT_EQ(mojom::FileError::OK, error); | 443 EXPECT_EQ(mojom::FileError::OK, error); |
| 449 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); | 444 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); |
| 450 const int size = static_cast<int>(num_bytes_written); | 445 const int size = static_cast<int>(num_bytes_written); |
| 451 | 446 |
| 452 // Tell. | 447 // Tell. |
| 453 error = mojom::FileError::FAILED; | 448 error = mojom::FileError::FAILED; |
| 454 int64_t position = -1; | 449 int64_t position = -1; |
| 455 handled = file->Tell(&error, &position); | 450 handled = file->Tell(&error, &position); |
| 456 ASSERT_TRUE(handled); | 451 ASSERT_TRUE(handled); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 526 | 521 |
| 527 // Write to it. | 522 // Write to it. |
| 528 std::vector<uint8_t> bytes_to_write; | 523 std::vector<uint8_t> bytes_to_write; |
| 529 bytes_to_write.push_back(static_cast<uint8_t>('h')); | 524 bytes_to_write.push_back(static_cast<uint8_t>('h')); |
| 530 bytes_to_write.push_back(static_cast<uint8_t>('e')); | 525 bytes_to_write.push_back(static_cast<uint8_t>('e')); |
| 531 bytes_to_write.push_back(static_cast<uint8_t>('l')); | 526 bytes_to_write.push_back(static_cast<uint8_t>('l')); |
| 532 bytes_to_write.push_back(static_cast<uint8_t>('l')); | 527 bytes_to_write.push_back(static_cast<uint8_t>('l')); |
| 533 bytes_to_write.push_back(static_cast<uint8_t>('o')); | 528 bytes_to_write.push_back(static_cast<uint8_t>('o')); |
| 534 error = mojom::FileError::FAILED; | 529 error = mojom::FileError::FAILED; |
| 535 uint32_t num_bytes_written = 0; | 530 uint32_t num_bytes_written = 0; |
| 536 handled = | 531 handled = file1->Write(bytes_to_write, 0, mojom::Whence::FROM_CURRENT, &error, |
| 537 file1->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, | 532 &num_bytes_written); |
| 538 mojom::Whence::FROM_CURRENT, &error, &num_bytes_written); | |
| 539 ASSERT_TRUE(handled); | 533 ASSERT_TRUE(handled); |
| 540 EXPECT_EQ(mojom::FileError::OK, error); | 534 EXPECT_EQ(mojom::FileError::OK, error); |
| 541 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); | 535 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); |
| 542 const int end_hello_pos = static_cast<int>(num_bytes_written); | 536 const int end_hello_pos = static_cast<int>(num_bytes_written); |
| 543 | 537 |
| 544 // Dup it. | 538 // Dup it. |
| 545 mojom::FilePtr file2; | 539 mojom::FilePtr file2; |
| 546 error = mojom::FileError::FAILED; | 540 error = mojom::FileError::FAILED; |
| 547 handled = file1->Dup(GetProxy(&file2), &error); | 541 handled = file1->Dup(GetProxy(&file2), &error); |
| 548 ASSERT_TRUE(handled); | 542 ASSERT_TRUE(handled); |
| 549 EXPECT_EQ(mojom::FileError::OK, error); | 543 EXPECT_EQ(mojom::FileError::OK, error); |
| 550 | 544 |
| 551 // |file2| should have the same position. | 545 // |file2| should have the same position. |
| 552 error = mojom::FileError::FAILED; | 546 error = mojom::FileError::FAILED; |
| 553 int64_t position = -1; | 547 int64_t position = -1; |
| 554 handled = file2->Tell(&error, &position); | 548 handled = file2->Tell(&error, &position); |
| 555 ASSERT_TRUE(handled); | 549 ASSERT_TRUE(handled); |
| 556 EXPECT_EQ(mojom::FileError::OK, error); | 550 EXPECT_EQ(mojom::FileError::OK, error); |
| 557 EXPECT_EQ(end_hello_pos, position); | 551 EXPECT_EQ(end_hello_pos, position); |
| 558 | 552 |
| 559 // Write using |file2|. | 553 // Write using |file2|. |
| 560 std::vector<uint8_t> more_bytes_to_write; | 554 std::vector<uint8_t> more_bytes_to_write; |
| 561 more_bytes_to_write.push_back(static_cast<uint8_t>('w')); | 555 more_bytes_to_write.push_back(static_cast<uint8_t>('w')); |
| 562 more_bytes_to_write.push_back(static_cast<uint8_t>('o')); | 556 more_bytes_to_write.push_back(static_cast<uint8_t>('o')); |
| 563 more_bytes_to_write.push_back(static_cast<uint8_t>('r')); | 557 more_bytes_to_write.push_back(static_cast<uint8_t>('r')); |
| 564 more_bytes_to_write.push_back(static_cast<uint8_t>('l')); | 558 more_bytes_to_write.push_back(static_cast<uint8_t>('l')); |
| 565 more_bytes_to_write.push_back(static_cast<uint8_t>('d')); | 559 more_bytes_to_write.push_back(static_cast<uint8_t>('d')); |
| 566 error = mojom::FileError::FAILED; | 560 error = mojom::FileError::FAILED; |
| 567 num_bytes_written = 0; | 561 num_bytes_written = 0; |
| 568 handled = | 562 handled = file2->Write(more_bytes_to_write, 0, mojom::Whence::FROM_CURRENT, |
| 569 file2->Write(mojo::Array<uint8_t>::From(more_bytes_to_write), 0, | 563 &error, &num_bytes_written); |
| 570 mojom::Whence::FROM_CURRENT, &error, &num_bytes_written); | |
| 571 ASSERT_TRUE(handled); | 564 ASSERT_TRUE(handled); |
| 572 EXPECT_EQ(mojom::FileError::OK, error); | 565 EXPECT_EQ(mojom::FileError::OK, error); |
| 573 EXPECT_EQ(more_bytes_to_write.size(), num_bytes_written); | 566 EXPECT_EQ(more_bytes_to_write.size(), num_bytes_written); |
| 574 const int end_world_pos = end_hello_pos + static_cast<int>(num_bytes_written); | 567 const int end_world_pos = end_hello_pos + static_cast<int>(num_bytes_written); |
| 575 | 568 |
| 576 // |file1| should have the same position. | 569 // |file1| should have the same position. |
| 577 error = mojom::FileError::FAILED; | 570 error = mojom::FileError::FAILED; |
| 578 position = -1; | 571 position = -1; |
| 579 handled = file1->Tell(&error, &position); | 572 handled = file1->Tell(&error, &position); |
| 580 ASSERT_TRUE(handled); | 573 ASSERT_TRUE(handled); |
| 581 EXPECT_EQ(mojom::FileError::OK, error); | 574 EXPECT_EQ(mojom::FileError::OK, error); |
| 582 EXPECT_EQ(end_world_pos, position); | 575 EXPECT_EQ(end_world_pos, position); |
| 583 | 576 |
| 584 // Close |file1|. | 577 // Close |file1|. |
| 585 error = mojom::FileError::FAILED; | 578 error = mojom::FileError::FAILED; |
| 586 handled = file1->Close(&error); | 579 handled = file1->Close(&error); |
| 587 ASSERT_TRUE(handled); | 580 ASSERT_TRUE(handled); |
| 588 EXPECT_EQ(mojom::FileError::OK, error); | 581 EXPECT_EQ(mojom::FileError::OK, error); |
| 589 | 582 |
| 590 // Read everything using |file2|. | 583 // Read everything using |file2|. |
| 591 mojo::Array<uint8_t> bytes_read; | 584 base::Optional<std::vector<uint8_t>> bytes_read; |
| 592 error = mojom::FileError::FAILED; | 585 error = mojom::FileError::FAILED; |
| 593 handled = | 586 handled = |
| 594 file2->Read(1000, 0, mojom::Whence::FROM_BEGIN, &error, &bytes_read); | 587 file2->Read(1000, 0, mojom::Whence::FROM_BEGIN, &error, &bytes_read); |
| 595 ASSERT_TRUE(handled); | 588 ASSERT_TRUE(handled); |
| 596 EXPECT_EQ(mojom::FileError::OK, error); | 589 EXPECT_EQ(mojom::FileError::OK, error); |
| 597 ASSERT_EQ(static_cast<size_t>(end_world_pos), bytes_read.size()); | 590 ASSERT_TRUE(bytes_read.has_value()); |
| 591 ASSERT_EQ(static_cast<size_t>(end_world_pos), bytes_read.value().size()); |
| 598 // Just check the first and last bytes. | 592 // Just check the first and last bytes. |
| 599 EXPECT_EQ(static_cast<uint8_t>('h'), bytes_read[0]); | 593 EXPECT_EQ(static_cast<uint8_t>('h'), bytes_read.value()[0]); |
| 600 EXPECT_EQ(static_cast<uint8_t>('d'), bytes_read[end_world_pos - 1]); | 594 EXPECT_EQ(static_cast<uint8_t>('d'), bytes_read.value()[end_world_pos - 1]); |
| 601 | 595 |
| 602 // TODO(vtl): Test that |file2| has the same open options as |file1|. | 596 // TODO(vtl): Test that |file2| has the same open options as |file1|. |
| 603 } | 597 } |
| 604 | 598 |
| 605 TEST_F(FileImplTest, Truncate) { | 599 TEST_F(FileImplTest, Truncate) { |
| 606 const uint32_t kInitialSize = 1000; | 600 const uint32_t kInitialSize = 1000; |
| 607 const uint32_t kTruncatedSize = 654; | 601 const uint32_t kTruncatedSize = 654; |
| 608 | 602 |
| 609 mojom::DirectoryPtr directory; | 603 mojom::DirectoryPtr directory; |
| 610 GetTemporaryRoot(&directory); | 604 GetTemporaryRoot(&directory); |
| 611 mojom::FileError error; | 605 mojom::FileError error; |
| 612 | 606 |
| 613 // Create my_file. | 607 // Create my_file. |
| 614 mojom::FilePtr file; | 608 mojom::FilePtr file; |
| 615 error = mojom::FileError::FAILED; | 609 error = mojom::FileError::FAILED; |
| 616 bool handled = | 610 bool handled = |
| 617 directory->OpenFile("my_file", GetProxy(&file), | 611 directory->OpenFile("my_file", GetProxy(&file), |
| 618 mojom::kFlagWrite | mojom::kFlagCreate, &error); | 612 mojom::kFlagWrite | mojom::kFlagCreate, &error); |
| 619 ASSERT_TRUE(handled); | 613 ASSERT_TRUE(handled); |
| 620 EXPECT_EQ(mojom::FileError::OK, error); | 614 EXPECT_EQ(mojom::FileError::OK, error); |
| 621 | 615 |
| 622 // Write to it. | 616 // Write to it. |
| 623 std::vector<uint8_t> bytes_to_write(kInitialSize, '!'); | 617 std::vector<uint8_t> bytes_to_write(kInitialSize, '!'); |
| 624 error = mojom::FileError::FAILED; | 618 error = mojom::FileError::FAILED; |
| 625 uint32_t num_bytes_written = 0; | 619 uint32_t num_bytes_written = 0; |
| 626 handled = | 620 handled = file->Write(bytes_to_write, 0, mojom::Whence::FROM_CURRENT, &error, |
| 627 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, | 621 &num_bytes_written); |
| 628 mojom::Whence::FROM_CURRENT, &error, &num_bytes_written); | |
| 629 ASSERT_TRUE(handled); | 622 ASSERT_TRUE(handled); |
| 630 EXPECT_EQ(mojom::FileError::OK, error); | 623 EXPECT_EQ(mojom::FileError::OK, error); |
| 631 EXPECT_EQ(kInitialSize, num_bytes_written); | 624 EXPECT_EQ(kInitialSize, num_bytes_written); |
| 632 | 625 |
| 633 // Stat it. | 626 // Stat it. |
| 634 error = mojom::FileError::FAILED; | 627 error = mojom::FileError::FAILED; |
| 635 mojom::FileInformationPtr file_info; | 628 mojom::FileInformationPtr file_info; |
| 636 handled = file->Stat(&error, &file_info); | 629 handled = file->Stat(&error, &file_info); |
| 637 ASSERT_TRUE(handled); | 630 ASSERT_TRUE(handled); |
| 638 EXPECT_EQ(mojom::FileError::OK, error); | 631 EXPECT_EQ(mojom::FileError::OK, error); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 693 // Reopen my_file. | 686 // Reopen my_file. |
| 694 mojom::FilePtr file2; | 687 mojom::FilePtr file2; |
| 695 error = mojom::FileError::FAILED; | 688 error = mojom::FileError::FAILED; |
| 696 bool handled = | 689 bool handled = |
| 697 directory->OpenFile("my_file", GetProxy(&file2), | 690 directory->OpenFile("my_file", GetProxy(&file2), |
| 698 mojom::kFlagRead | mojom::kFlagOpen, &error); | 691 mojom::kFlagRead | mojom::kFlagOpen, &error); |
| 699 ASSERT_TRUE(handled); | 692 ASSERT_TRUE(handled); |
| 700 EXPECT_EQ(mojom::FileError::OK, error); | 693 EXPECT_EQ(mojom::FileError::OK, error); |
| 701 | 694 |
| 702 // Verify that we wrote data raw on the file descriptor. | 695 // Verify that we wrote data raw on the file descriptor. |
| 703 mojo::Array<uint8_t> bytes_read; | 696 base::Optional<std::vector<uint8_t>> bytes_read; |
| 704 error = mojom::FileError::FAILED; | 697 error = mojom::FileError::FAILED; |
| 705 handled = file2->Read(5, 0, mojom::Whence::FROM_BEGIN, &error, &bytes_read); | 698 handled = file2->Read(5, 0, mojom::Whence::FROM_BEGIN, &error, &bytes_read); |
| 706 ASSERT_TRUE(handled); | 699 ASSERT_TRUE(handled); |
| 707 EXPECT_EQ(mojom::FileError::OK, error); | 700 EXPECT_EQ(mojom::FileError::OK, error); |
| 708 ASSERT_EQ(5u, bytes_read.size()); | 701 ASSERT_TRUE(bytes_read.has_value()); |
| 709 EXPECT_EQ(static_cast<uint8_t>('h'), bytes_read[0]); | 702 ASSERT_EQ(5u, bytes_read.value().size()); |
| 710 EXPECT_EQ(static_cast<uint8_t>('e'), bytes_read[1]); | 703 EXPECT_EQ(static_cast<uint8_t>('h'), bytes_read.value()[0]); |
| 711 EXPECT_EQ(static_cast<uint8_t>('l'), bytes_read[2]); | 704 EXPECT_EQ(static_cast<uint8_t>('e'), bytes_read.value()[1]); |
| 712 EXPECT_EQ(static_cast<uint8_t>('l'), bytes_read[3]); | 705 EXPECT_EQ(static_cast<uint8_t>('l'), bytes_read.value()[2]); |
| 713 EXPECT_EQ(static_cast<uint8_t>('o'), bytes_read[4]); | 706 EXPECT_EQ(static_cast<uint8_t>('l'), bytes_read.value()[3]); |
| 707 EXPECT_EQ(static_cast<uint8_t>('o'), bytes_read.value()[4]); |
| 714 } | 708 } |
| 715 } | 709 } |
| 716 | 710 |
| 717 TEST_F(FileImplTest, SimpleLockUnlock) { | 711 TEST_F(FileImplTest, SimpleLockUnlock) { |
| 718 mojom::DirectoryPtr directory; | 712 mojom::DirectoryPtr directory; |
| 719 GetTemporaryRoot(&directory); | 713 GetTemporaryRoot(&directory); |
| 720 mojom::FileError error; | 714 mojom::FileError error; |
| 721 | 715 |
| 722 // Create my_file. | 716 // Create my_file. |
| 723 mojom::FilePtr file; | 717 mojom::FilePtr file; |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 803 // The file shouldn't be locked (and we check by trying to lock it). | 797 // The file shouldn't be locked (and we check by trying to lock it). |
| 804 error = mojom::FileError::FAILED; | 798 error = mojom::FileError::FAILED; |
| 805 handled = file->Lock(&error); | 799 handled = file->Lock(&error); |
| 806 ASSERT_TRUE(handled); | 800 ASSERT_TRUE(handled); |
| 807 EXPECT_EQ(mojom::FileError::OK, error); | 801 EXPECT_EQ(mojom::FileError::OK, error); |
| 808 } | 802 } |
| 809 } | 803 } |
| 810 | 804 |
| 811 } // namespace | 805 } // namespace |
| 812 } // namespace filesystem | 806 } // namespace filesystem |
| OLD | NEW |