| 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" |
| 11 #include "components/filesystem/files_test_base.h" | 11 #include "components/filesystem/files_test_base.h" |
| 12 #include "mojo/public/cpp/bindings/interface_request.h" | 12 #include "mojo/public/cpp/bindings/interface_request.h" |
| 13 #include "mojo/public/cpp/bindings/type_converter.h" | 13 #include "mojo/public/cpp/bindings/type_converter.h" |
| 14 #include "mojo/public/cpp/system/platform_handle.h" | 14 #include "mojo/public/cpp/system/platform_handle.h" |
| 15 | 15 |
| 16 namespace filesystem { | 16 namespace filesystem { |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 using FileImplTest = FilesTestBase; | 19 using FileImplTest = FilesTestBase; |
| 20 | 20 |
| 21 TEST_F(FileImplTest, CreateWriteCloseRenameOpenRead) { | 21 TEST_F(FileImplTest, CreateWriteCloseRenameOpenRead) { |
| 22 mojom::DirectoryPtr directory; | 22 mojom::DirectoryPtr directory; |
| 23 GetTemporaryRoot(&directory); | 23 GetTemporaryRoot(&directory); |
| 24 mojom::FileError error; | 24 mojom::FileError error; |
| 25 bool handled = false; |
| 25 | 26 |
| 26 { | 27 { |
| 27 // Create my_file. | 28 // Create my_file. |
| 28 mojom::FilePtr file; | 29 mojom::FilePtr file; |
| 29 error = mojom::FileError::FAILED; | 30 error = mojom::FileError::FAILED; |
| 30 directory->OpenFile("my_file", GetProxy(&file), | 31 handled = |
| 31 mojom::kFlagWrite | mojom::kFlagCreate, | 32 directory->OpenFile("my_file", GetProxy(&file), |
| 32 Capture(&error)); | 33 mojom::kFlagWrite | mojom::kFlagCreate, &error); |
| 33 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 34 ASSERT_TRUE(handled); |
| 34 EXPECT_EQ(mojom::FileError::OK, error); | 35 EXPECT_EQ(mojom::FileError::OK, error); |
| 35 | 36 |
| 36 // Write to it. | 37 // Write to it. |
| 37 std::vector<uint8_t> bytes_to_write; | 38 std::vector<uint8_t> bytes_to_write; |
| 38 bytes_to_write.push_back(static_cast<uint8_t>('h')); | 39 bytes_to_write.push_back(static_cast<uint8_t>('h')); |
| 39 bytes_to_write.push_back(static_cast<uint8_t>('e')); | 40 bytes_to_write.push_back(static_cast<uint8_t>('e')); |
| 40 bytes_to_write.push_back(static_cast<uint8_t>('l')); | 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>('o')); | 43 bytes_to_write.push_back(static_cast<uint8_t>('o')); |
| 43 error = mojom::FileError::FAILED; | 44 error = mojom::FileError::FAILED; |
| 44 uint32_t num_bytes_written = 0; | 45 uint32_t num_bytes_written = 0; |
| 45 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, | 46 handled = |
| 46 mojom::Whence::FROM_CURRENT, | 47 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, |
| 47 Capture(&error, &num_bytes_written)); | 48 mojom::Whence::FROM_CURRENT, &error, &num_bytes_written); |
| 48 ASSERT_TRUE(file.WaitForIncomingResponse()); | 49 ASSERT_TRUE(handled); |
| 49 EXPECT_EQ(mojom::FileError::OK, error); | 50 EXPECT_EQ(mojom::FileError::OK, error); |
| 50 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); | 51 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); |
| 51 | 52 |
| 52 // Close it. | 53 // Close it. |
| 53 error = mojom::FileError::FAILED; | 54 error = mojom::FileError::FAILED; |
| 54 file->Close(Capture(&error)); | 55 handled = file->Close((&error)); |
| 55 ASSERT_TRUE(file.WaitForIncomingResponse()); | 56 ASSERT_TRUE(handled); |
| 56 EXPECT_EQ(mojom::FileError::OK, error); | 57 EXPECT_EQ(mojom::FileError::OK, error); |
| 57 } | 58 } |
| 58 | 59 |
| 59 // Rename it. | 60 // Rename it. |
| 60 error = mojom::FileError::FAILED; | 61 error = mojom::FileError::FAILED; |
| 61 directory->Rename("my_file", "your_file", Capture(&error)); | 62 handled = directory->Rename("my_file", "your_file", &error); |
| 62 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 63 ASSERT_TRUE(handled); |
| 63 EXPECT_EQ(mojom::FileError::OK, error); | 64 EXPECT_EQ(mojom::FileError::OK, error); |
| 64 | 65 |
| 65 { | 66 { |
| 66 // Open my_file again. | 67 // Open my_file again. |
| 67 mojom::FilePtr file; | 68 mojom::FilePtr file; |
| 68 error = mojom::FileError::FAILED; | 69 error = mojom::FileError::FAILED; |
| 69 directory->OpenFile("your_file", GetProxy(&file), | 70 bool handled = |
| 70 mojom::kFlagRead | mojom::kFlagOpen, Capture(&error)); | 71 directory->OpenFile("your_file", GetProxy(&file), |
| 71 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 72 mojom::kFlagRead | mojom::kFlagOpen, &error); |
| 73 ASSERT_TRUE(handled); |
| 72 EXPECT_EQ(mojom::FileError::OK, error); | 74 EXPECT_EQ(mojom::FileError::OK, error); |
| 73 | 75 |
| 74 // Read from it. | 76 // Read from it. |
| 75 mojo::Array<uint8_t> bytes_read; | 77 mojo::Array<uint8_t> bytes_read; |
| 76 error = mojom::FileError::FAILED; | 78 error = mojom::FileError::FAILED; |
| 77 file->Read(3, 1, mojom::Whence::FROM_BEGIN, Capture(&error, &bytes_read)); | 79 handled = file->Read(3, 1, mojom::Whence::FROM_BEGIN, &error, &bytes_read); |
| 78 ASSERT_TRUE(file.WaitForIncomingResponse()); | 80 ASSERT_TRUE(handled); |
| 79 EXPECT_EQ(mojom::FileError::OK, error); | 81 EXPECT_EQ(mojom::FileError::OK, error); |
| 80 ASSERT_EQ(3u, bytes_read.size()); | 82 ASSERT_EQ(3u, bytes_read.size()); |
| 81 EXPECT_EQ(static_cast<uint8_t>('e'), bytes_read[0]); | 83 EXPECT_EQ(static_cast<uint8_t>('e'), bytes_read[0]); |
| 82 EXPECT_EQ(static_cast<uint8_t>('l'), bytes_read[1]); | 84 EXPECT_EQ(static_cast<uint8_t>('l'), bytes_read[1]); |
| 83 EXPECT_EQ(static_cast<uint8_t>('l'), bytes_read[2]); | 85 EXPECT_EQ(static_cast<uint8_t>('l'), bytes_read[2]); |
| 84 } | 86 } |
| 85 | 87 |
| 86 // TODO(vtl): Test read/write offset options. | 88 // TODO(vtl): Test read/write offset options. |
| 87 } | 89 } |
| 88 | 90 |
| 89 TEST_F(FileImplTest, CantWriteInReadMode) { | 91 TEST_F(FileImplTest, CantWriteInReadMode) { |
| 90 mojom::DirectoryPtr directory; | 92 mojom::DirectoryPtr directory; |
| 91 GetTemporaryRoot(&directory); | 93 GetTemporaryRoot(&directory); |
| 92 mojom::FileError error; | 94 mojom::FileError error; |
| 93 | 95 |
| 94 std::vector<uint8_t> bytes_to_write; | 96 std::vector<uint8_t> bytes_to_write; |
| 95 bytes_to_write.push_back(static_cast<uint8_t>('h')); | 97 bytes_to_write.push_back(static_cast<uint8_t>('h')); |
| 96 bytes_to_write.push_back(static_cast<uint8_t>('e')); | 98 bytes_to_write.push_back(static_cast<uint8_t>('e')); |
| 97 bytes_to_write.push_back(static_cast<uint8_t>('l')); | 99 bytes_to_write.push_back(static_cast<uint8_t>('l')); |
| 98 bytes_to_write.push_back(static_cast<uint8_t>('l')); | 100 bytes_to_write.push_back(static_cast<uint8_t>('l')); |
| 99 bytes_to_write.push_back(static_cast<uint8_t>('o')); | 101 bytes_to_write.push_back(static_cast<uint8_t>('o')); |
| 100 | 102 |
| 101 { | 103 { |
| 102 // Create my_file. | 104 // Create my_file. |
| 103 mojom::FilePtr file; | 105 mojom::FilePtr file; |
| 104 error = mojom::FileError::FAILED; | 106 error = mojom::FileError::FAILED; |
| 105 directory->OpenFile("my_file", GetProxy(&file), | 107 bool handled = |
| 106 mojom::kFlagWrite | mojom::kFlagCreate, | 108 directory->OpenFile("my_file", GetProxy(&file), |
| 107 Capture(&error)); | 109 mojom::kFlagWrite | mojom::kFlagCreate, &error); |
| 108 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 110 ASSERT_TRUE(handled); |
| 109 EXPECT_EQ(mojom::FileError::OK, error); | 111 EXPECT_EQ(mojom::FileError::OK, error); |
| 110 | 112 |
| 111 // Write to it. | 113 // Write to it. |
| 112 error = mojom::FileError::FAILED; | 114 error = mojom::FileError::FAILED; |
| 113 uint32_t num_bytes_written = 0; | 115 uint32_t num_bytes_written = 0; |
| 114 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, | 116 handled = |
| 115 mojom::Whence::FROM_CURRENT, | 117 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, |
| 116 Capture(&error, &num_bytes_written)); | 118 mojom::Whence::FROM_CURRENT, &error, &num_bytes_written); |
| 117 ASSERT_TRUE(file.WaitForIncomingResponse()); | 119 ASSERT_TRUE(handled); |
| 118 EXPECT_EQ(mojom::FileError::OK, error); | 120 EXPECT_EQ(mojom::FileError::OK, error); |
| 119 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); | 121 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); |
| 120 | 122 |
| 121 // Close it. | 123 // Close it. |
| 122 error = mojom::FileError::FAILED; | 124 error = mojom::FileError::FAILED; |
| 123 file->Close(Capture(&error)); | 125 handled = file->Close((&error)); |
| 124 ASSERT_TRUE(file.WaitForIncomingResponse()); | 126 ASSERT_TRUE(handled); |
| 125 EXPECT_EQ(mojom::FileError::OK, error); | 127 EXPECT_EQ(mojom::FileError::OK, error); |
| 126 } | 128 } |
| 127 | 129 |
| 128 { | 130 { |
| 129 // Open my_file again, this time with read only mode. | 131 // Open my_file again, this time with read only mode. |
| 130 mojom::FilePtr file; | 132 mojom::FilePtr file; |
| 131 error = mojom::FileError::FAILED; | 133 error = mojom::FileError::FAILED; |
| 132 directory->OpenFile("my_file", GetProxy(&file), | 134 bool handled = |
| 133 mojom::kFlagRead | mojom::kFlagOpen, Capture(&error)); | 135 directory->OpenFile("my_file", GetProxy(&file), |
| 134 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 136 mojom::kFlagRead | mojom::kFlagOpen, &error); |
| 137 ASSERT_TRUE(handled); |
| 135 EXPECT_EQ(mojom::FileError::OK, error); | 138 EXPECT_EQ(mojom::FileError::OK, error); |
| 136 | 139 |
| 137 // Try to write in read mode; it should fail. | 140 // Try to write in read mode; it should fail. |
| 138 error = mojom::FileError::OK; | 141 error = mojom::FileError::OK; |
| 139 uint32_t num_bytes_written = 0; | 142 uint32_t num_bytes_written = 0; |
| 140 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, | 143 handled = |
| 141 mojom::Whence::FROM_CURRENT, | 144 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, |
| 142 Capture(&error, &num_bytes_written)); | 145 mojom::Whence::FROM_CURRENT, &error, &num_bytes_written); |
| 143 | 146 |
| 144 ASSERT_TRUE(file.WaitForIncomingResponse()); | 147 ASSERT_TRUE(handled); |
| 145 EXPECT_EQ(mojom::FileError::FAILED, error); | 148 EXPECT_EQ(mojom::FileError::FAILED, error); |
| 146 EXPECT_EQ(0u, num_bytes_written); | 149 EXPECT_EQ(0u, num_bytes_written); |
| 147 | 150 |
| 148 // Close it. | 151 // Close it. |
| 149 error = mojom::FileError::FAILED; | 152 error = mojom::FileError::FAILED; |
| 150 file->Close(Capture(&error)); | 153 handled = file->Close((&error)); |
| 151 ASSERT_TRUE(file.WaitForIncomingResponse()); | 154 ASSERT_TRUE(handled); |
| 152 EXPECT_EQ(mojom::FileError::OK, error); | 155 EXPECT_EQ(mojom::FileError::OK, error); |
| 153 } | 156 } |
| 154 } | 157 } |
| 155 | 158 |
| 156 TEST_F(FileImplTest, OpenInAppendMode) { | 159 TEST_F(FileImplTest, OpenInAppendMode) { |
| 157 mojom::DirectoryPtr directory; | 160 mojom::DirectoryPtr directory; |
| 158 GetTemporaryRoot(&directory); | 161 GetTemporaryRoot(&directory); |
| 159 mojom::FileError error; | 162 mojom::FileError error; |
| 160 | 163 |
| 161 { | 164 { |
| 162 // Create my_file. | 165 // Create my_file. |
| 163 mojom::FilePtr file; | 166 mojom::FilePtr file; |
| 164 error = mojom::FileError::FAILED; | 167 error = mojom::FileError::FAILED; |
| 165 directory->OpenFile("my_file", GetProxy(&file), | 168 bool handled = |
| 166 mojom::kFlagWrite | mojom::kFlagCreate, | 169 directory->OpenFile("my_file", GetProxy(&file), |
| 167 Capture(&error)); | 170 mojom::kFlagWrite | mojom::kFlagCreate, &error); |
| 168 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 171 ASSERT_TRUE(handled); |
| 169 EXPECT_EQ(mojom::FileError::OK, error); | 172 EXPECT_EQ(mojom::FileError::OK, error); |
| 170 | 173 |
| 171 // Write to it. | 174 // Write to it. |
| 172 std::vector<uint8_t> bytes_to_write; | 175 std::vector<uint8_t> bytes_to_write; |
| 173 bytes_to_write.push_back(static_cast<uint8_t>('h')); | 176 bytes_to_write.push_back(static_cast<uint8_t>('h')); |
| 174 bytes_to_write.push_back(static_cast<uint8_t>('e')); | 177 bytes_to_write.push_back(static_cast<uint8_t>('e')); |
| 175 bytes_to_write.push_back(static_cast<uint8_t>('l')); | 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>('o')); | 180 bytes_to_write.push_back(static_cast<uint8_t>('o')); |
| 178 error = mojom::FileError::FAILED; | 181 error = mojom::FileError::FAILED; |
| 179 uint32_t num_bytes_written = 0; | 182 uint32_t num_bytes_written = 0; |
| 180 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, | 183 handled = |
| 181 mojom::Whence::FROM_CURRENT, | 184 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, |
| 182 Capture(&error, &num_bytes_written)); | 185 mojom::Whence::FROM_CURRENT, &error, &num_bytes_written); |
| 183 ASSERT_TRUE(file.WaitForIncomingResponse()); | 186 ASSERT_TRUE(handled); |
| 184 EXPECT_EQ(mojom::FileError::OK, error); | 187 EXPECT_EQ(mojom::FileError::OK, error); |
| 185 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); | 188 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); |
| 186 | 189 |
| 187 // Close it. | 190 // Close it. |
| 188 error = mojom::FileError::FAILED; | 191 error = mojom::FileError::FAILED; |
| 189 file->Close(Capture(&error)); | 192 handled = file->Close(&error); |
| 190 ASSERT_TRUE(file.WaitForIncomingResponse()); | 193 ASSERT_TRUE(handled); |
| 191 EXPECT_EQ(mojom::FileError::OK, error); | 194 EXPECT_EQ(mojom::FileError::OK, error); |
| 192 } | 195 } |
| 193 | 196 |
| 194 { | 197 { |
| 195 // Append to my_file. | 198 // Append to my_file. |
| 196 mojom::FilePtr file; | 199 mojom::FilePtr file; |
| 197 error = mojom::FileError::FAILED; | 200 error = mojom::FileError::FAILED; |
| 198 directory->OpenFile("my_file", GetProxy(&file), | 201 bool handled = |
| 199 mojom::kFlagAppend | mojom::kFlagOpen, Capture(&error)); | 202 directory->OpenFile("my_file", GetProxy(&file), |
| 200 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 203 mojom::kFlagAppend | mojom::kFlagOpen, &error); |
| 204 ASSERT_TRUE(handled); |
| 201 EXPECT_EQ(mojom::FileError::OK, error); | 205 EXPECT_EQ(mojom::FileError::OK, error); |
| 202 | 206 |
| 203 // Write to it. | 207 // Write to it. |
| 204 std::vector<uint8_t> bytes_to_write; | 208 std::vector<uint8_t> bytes_to_write; |
| 205 bytes_to_write.push_back(static_cast<uint8_t>('g')); | 209 bytes_to_write.push_back(static_cast<uint8_t>('g')); |
| 206 bytes_to_write.push_back(static_cast<uint8_t>('o')); | 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>('d')); | 212 bytes_to_write.push_back(static_cast<uint8_t>('d')); |
| 209 bytes_to_write.push_back(static_cast<uint8_t>('b')); | 213 bytes_to_write.push_back(static_cast<uint8_t>('b')); |
| 210 bytes_to_write.push_back(static_cast<uint8_t>('y')); | 214 bytes_to_write.push_back(static_cast<uint8_t>('y')); |
| 211 bytes_to_write.push_back(static_cast<uint8_t>('e')); | 215 bytes_to_write.push_back(static_cast<uint8_t>('e')); |
| 212 error = mojom::FileError::FAILED; | 216 error = mojom::FileError::FAILED; |
| 213 uint32_t num_bytes_written = 0; | 217 uint32_t num_bytes_written = 0; |
| 214 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, | 218 handled = |
| 215 mojom::Whence::FROM_CURRENT, | 219 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, |
| 216 Capture(&error, &num_bytes_written)); | 220 mojom::Whence::FROM_CURRENT, &error, &num_bytes_written); |
| 217 ASSERT_TRUE(file.WaitForIncomingResponse()); | 221 ASSERT_TRUE(handled); |
| 218 EXPECT_EQ(mojom::FileError::OK, error); | 222 EXPECT_EQ(mojom::FileError::OK, error); |
| 219 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); | 223 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); |
| 220 | 224 |
| 221 // Close it. | 225 // Close it. |
| 222 error = mojom::FileError::FAILED; | 226 error = mojom::FileError::FAILED; |
| 223 file->Close(Capture(&error)); | 227 handled = file->Close((&error)); |
| 224 ASSERT_TRUE(file.WaitForIncomingResponse()); | 228 ASSERT_TRUE(handled); |
| 225 EXPECT_EQ(mojom::FileError::OK, error); | 229 EXPECT_EQ(mojom::FileError::OK, error); |
| 226 } | 230 } |
| 227 | 231 |
| 228 { | 232 { |
| 229 // Open my_file again. | 233 // Open my_file again. |
| 230 mojom::FilePtr file; | 234 mojom::FilePtr file; |
| 231 error = mojom::FileError::FAILED; | 235 error = mojom::FileError::FAILED; |
| 232 directory->OpenFile("my_file", GetProxy(&file), | 236 bool handled = |
| 233 mojom::kFlagRead | mojom::kFlagOpen, Capture(&error)); | 237 directory->OpenFile("my_file", GetProxy(&file), |
| 234 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 238 mojom::kFlagRead | mojom::kFlagOpen, &error); |
| 239 ASSERT_TRUE(handled); |
| 235 EXPECT_EQ(mojom::FileError::OK, error); | 240 EXPECT_EQ(mojom::FileError::OK, error); |
| 236 | 241 |
| 237 // Read from it. | 242 // Read from it. |
| 238 mojo::Array<uint8_t> bytes_read; | 243 mojo::Array<uint8_t> bytes_read; |
| 239 error = mojom::FileError::FAILED; | 244 error = mojom::FileError::FAILED; |
| 240 file->Read(12, 0, mojom::Whence::FROM_BEGIN, Capture(&error, &bytes_read)); | 245 handled = file->Read(12, 0, mojom::Whence::FROM_BEGIN, &error, &bytes_read); |
| 241 ASSERT_TRUE(file.WaitForIncomingResponse()); | 246 ASSERT_TRUE(handled); |
| 242 EXPECT_EQ(mojom::FileError::OK, error); | 247 EXPECT_EQ(mojom::FileError::OK, error); |
| 243 ASSERT_EQ(12u, bytes_read.size()); | 248 ASSERT_EQ(12u, bytes_read.size()); |
| 244 EXPECT_EQ(static_cast<uint8_t>('l'), bytes_read[3]); | 249 EXPECT_EQ(static_cast<uint8_t>('l'), bytes_read[3]); |
| 245 EXPECT_EQ(static_cast<uint8_t>('o'), bytes_read[4]); | 250 EXPECT_EQ(static_cast<uint8_t>('o'), bytes_read[4]); |
| 246 EXPECT_EQ(static_cast<uint8_t>('g'), bytes_read[5]); | 251 EXPECT_EQ(static_cast<uint8_t>('g'), bytes_read[5]); |
| 247 EXPECT_EQ(static_cast<uint8_t>('o'), bytes_read[6]); | 252 EXPECT_EQ(static_cast<uint8_t>('o'), bytes_read[6]); |
| 248 } | 253 } |
| 249 } | 254 } |
| 250 | 255 |
| 251 TEST_F(FileImplTest, OpenInTruncateMode) { | 256 TEST_F(FileImplTest, OpenInTruncateMode) { |
| 252 mojom::DirectoryPtr directory; | 257 mojom::DirectoryPtr directory; |
| 253 GetTemporaryRoot(&directory); | 258 GetTemporaryRoot(&directory); |
| 254 mojom::FileError error; | 259 mojom::FileError error; |
| 255 | 260 |
| 256 { | 261 { |
| 257 // Create my_file. | 262 // Create my_file. |
| 258 mojom::FilePtr file; | 263 mojom::FilePtr file; |
| 259 error = mojom::FileError::FAILED; | 264 error = mojom::FileError::FAILED; |
| 260 directory->OpenFile("my_file", GetProxy(&file), | 265 bool handled = |
| 261 mojom::kFlagWrite | mojom::kFlagCreate, | 266 directory->OpenFile("my_file", GetProxy(&file), |
| 262 Capture(&error)); | 267 mojom::kFlagWrite | mojom::kFlagCreate, &error); |
| 263 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 268 ASSERT_TRUE(handled); |
| 264 EXPECT_EQ(mojom::FileError::OK, error); | 269 EXPECT_EQ(mojom::FileError::OK, error); |
| 265 | 270 |
| 266 // Write to it. | 271 // Write to it. |
| 267 std::vector<uint8_t> bytes_to_write; | 272 std::vector<uint8_t> bytes_to_write; |
| 268 bytes_to_write.push_back(static_cast<uint8_t>('h')); | 273 bytes_to_write.push_back(static_cast<uint8_t>('h')); |
| 269 bytes_to_write.push_back(static_cast<uint8_t>('e')); | 274 bytes_to_write.push_back(static_cast<uint8_t>('e')); |
| 270 bytes_to_write.push_back(static_cast<uint8_t>('l')); | 275 bytes_to_write.push_back(static_cast<uint8_t>('l')); |
| 271 bytes_to_write.push_back(static_cast<uint8_t>('l')); | 276 bytes_to_write.push_back(static_cast<uint8_t>('l')); |
| 272 bytes_to_write.push_back(static_cast<uint8_t>('o')); | 277 bytes_to_write.push_back(static_cast<uint8_t>('o')); |
| 273 error = mojom::FileError::FAILED; | 278 error = mojom::FileError::FAILED; |
| 274 uint32_t num_bytes_written = 0; | 279 uint32_t num_bytes_written = 0; |
| 275 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, | 280 handled = |
| 276 mojom::Whence::FROM_CURRENT, | 281 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, |
| 277 Capture(&error, &num_bytes_written)); | 282 mojom::Whence::FROM_CURRENT, &error, &num_bytes_written); |
| 278 ASSERT_TRUE(file.WaitForIncomingResponse()); | 283 ASSERT_TRUE(handled); |
| 279 EXPECT_EQ(mojom::FileError::OK, error); | 284 EXPECT_EQ(mojom::FileError::OK, error); |
| 280 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); | 285 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); |
| 281 | 286 |
| 282 // Close it. | 287 // Close it. |
| 283 error = mojom::FileError::FAILED; | 288 error = mojom::FileError::FAILED; |
| 284 file->Close(Capture(&error)); | 289 handled = file->Close(&error); |
| 285 ASSERT_TRUE(file.WaitForIncomingResponse()); | 290 ASSERT_TRUE(handled); |
| 286 EXPECT_EQ(mojom::FileError::OK, error); | 291 EXPECT_EQ(mojom::FileError::OK, error); |
| 287 } | 292 } |
| 288 | 293 |
| 289 { | 294 { |
| 290 // Append to my_file. | 295 // Append to my_file. |
| 291 mojom::FilePtr file; | 296 mojom::FilePtr file; |
| 292 error = mojom::FileError::FAILED; | 297 error = mojom::FileError::FAILED; |
| 293 directory->OpenFile("my_file", GetProxy(&file), | 298 bool handled = directory->OpenFile( |
| 294 mojom::kFlagWrite | mojom::kFlagOpenTruncated, | 299 "my_file", GetProxy(&file), |
| 295 Capture(&error)); | 300 mojom::kFlagWrite | mojom::kFlagOpenTruncated, &error); |
| 296 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 301 ASSERT_TRUE(handled); |
| 297 EXPECT_EQ(mojom::FileError::OK, error); | 302 EXPECT_EQ(mojom::FileError::OK, error); |
| 298 | 303 |
| 299 // Write to it. | 304 // Write to it. |
| 300 std::vector<uint8_t> bytes_to_write; | 305 std::vector<uint8_t> bytes_to_write; |
| 301 bytes_to_write.push_back(static_cast<uint8_t>('g')); | 306 bytes_to_write.push_back(static_cast<uint8_t>('g')); |
| 302 bytes_to_write.push_back(static_cast<uint8_t>('o')); | 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>('d')); | 309 bytes_to_write.push_back(static_cast<uint8_t>('d')); |
| 305 bytes_to_write.push_back(static_cast<uint8_t>('b')); | 310 bytes_to_write.push_back(static_cast<uint8_t>('b')); |
| 306 bytes_to_write.push_back(static_cast<uint8_t>('y')); | 311 bytes_to_write.push_back(static_cast<uint8_t>('y')); |
| 307 bytes_to_write.push_back(static_cast<uint8_t>('e')); | 312 bytes_to_write.push_back(static_cast<uint8_t>('e')); |
| 308 error = mojom::FileError::FAILED; | 313 error = mojom::FileError::FAILED; |
| 309 uint32_t num_bytes_written = 0; | 314 uint32_t num_bytes_written = 0; |
| 310 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, | 315 handled = |
| 311 mojom::Whence::FROM_CURRENT, | 316 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, |
| 312 Capture(&error, &num_bytes_written)); | 317 mojom::Whence::FROM_CURRENT, &error, &num_bytes_written); |
| 313 ASSERT_TRUE(file.WaitForIncomingResponse()); | 318 ASSERT_TRUE(handled); |
| 314 EXPECT_EQ(mojom::FileError::OK, error); | 319 EXPECT_EQ(mojom::FileError::OK, error); |
| 315 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); | 320 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); |
| 316 | 321 |
| 317 // Close it. | 322 // Close it. |
| 318 error = mojom::FileError::FAILED; | 323 error = mojom::FileError::FAILED; |
| 319 file->Close(Capture(&error)); | 324 handled = file->Close(&error); |
| 320 ASSERT_TRUE(file.WaitForIncomingResponse()); | 325 ASSERT_TRUE(handled); |
| 321 EXPECT_EQ(mojom::FileError::OK, error); | 326 EXPECT_EQ(mojom::FileError::OK, error); |
| 322 } | 327 } |
| 323 | 328 |
| 324 { | 329 { |
| 325 // Open my_file again. | 330 // Open my_file again. |
| 326 mojom::FilePtr file; | 331 mojom::FilePtr file; |
| 327 error = mojom::FileError::FAILED; | 332 error = mojom::FileError::FAILED; |
| 328 directory->OpenFile("my_file", GetProxy(&file), | 333 bool handled = |
| 329 mojom::kFlagRead | mojom::kFlagOpen, Capture(&error)); | 334 directory->OpenFile("my_file", GetProxy(&file), |
| 330 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 335 mojom::kFlagRead | mojom::kFlagOpen, &error); |
| 336 ASSERT_TRUE(handled); |
| 331 EXPECT_EQ(mojom::FileError::OK, error); | 337 EXPECT_EQ(mojom::FileError::OK, error); |
| 332 | 338 |
| 333 // Read from it. | 339 // Read from it. |
| 334 mojo::Array<uint8_t> bytes_read; | 340 mojo::Array<uint8_t> bytes_read; |
| 335 error = mojom::FileError::FAILED; | 341 error = mojom::FileError::FAILED; |
| 336 file->Read(7, 0, mojom::Whence::FROM_BEGIN, Capture(&error, &bytes_read)); | 342 handled = file->Read(7, 0, mojom::Whence::FROM_BEGIN, &error, &bytes_read); |
| 337 ASSERT_TRUE(file.WaitForIncomingResponse()); | 343 ASSERT_TRUE(handled); |
| 338 EXPECT_EQ(mojom::FileError::OK, error); | 344 EXPECT_EQ(mojom::FileError::OK, error); |
| 339 ASSERT_EQ(7u, bytes_read.size()); | 345 ASSERT_EQ(7u, bytes_read.size()); |
| 340 EXPECT_EQ(static_cast<uint8_t>('g'), bytes_read[0]); | 346 EXPECT_EQ(static_cast<uint8_t>('g'), bytes_read[0]); |
| 341 EXPECT_EQ(static_cast<uint8_t>('o'), bytes_read[1]); | 347 EXPECT_EQ(static_cast<uint8_t>('o'), bytes_read[1]); |
| 342 EXPECT_EQ(static_cast<uint8_t>('o'), bytes_read[2]); | 348 EXPECT_EQ(static_cast<uint8_t>('o'), bytes_read[2]); |
| 343 EXPECT_EQ(static_cast<uint8_t>('d'), bytes_read[3]); | 349 EXPECT_EQ(static_cast<uint8_t>('d'), bytes_read[3]); |
| 344 } | 350 } |
| 345 } | 351 } |
| 346 | 352 |
| 347 // Note: Ignore nanoseconds, since it may not always be supported. We expect at | 353 // Note: Ignore nanoseconds, since it may not always be supported. We expect at |
| 348 // least second-resolution support though. | 354 // least second-resolution support though. |
| 349 TEST_F(FileImplTest, StatTouch) { | 355 TEST_F(FileImplTest, StatTouch) { |
| 350 mojom::DirectoryPtr directory; | 356 mojom::DirectoryPtr directory; |
| 351 GetTemporaryRoot(&directory); | 357 GetTemporaryRoot(&directory); |
| 352 mojom::FileError error; | 358 mojom::FileError error; |
| 353 | 359 |
| 354 // Create my_file. | 360 // Create my_file. |
| 355 mojom::FilePtr file; | 361 mojom::FilePtr file; |
| 356 error = mojom::FileError::FAILED; | 362 error = mojom::FileError::FAILED; |
| 357 directory->OpenFile("my_file", GetProxy(&file), | 363 bool handled = |
| 358 mojom::kFlagWrite | mojom::kFlagCreate, Capture(&error)); | 364 directory->OpenFile("my_file", GetProxy(&file), |
| 359 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 365 mojom::kFlagWrite | mojom::kFlagCreate, &error); |
| 366 ASSERT_TRUE(handled); |
| 360 EXPECT_EQ(mojom::FileError::OK, error); | 367 EXPECT_EQ(mojom::FileError::OK, error); |
| 361 | 368 |
| 362 // Stat it. | 369 // Stat it. |
| 363 error = mojom::FileError::FAILED; | 370 error = mojom::FileError::FAILED; |
| 364 mojom::FileInformationPtr file_info; | 371 mojom::FileInformationPtr file_info; |
| 365 file->Stat(Capture(&error, &file_info)); | 372 handled = file->Stat(&error, &file_info); |
| 366 ASSERT_TRUE(file.WaitForIncomingResponse()); | 373 ASSERT_TRUE(handled); |
| 367 EXPECT_EQ(mojom::FileError::OK, error); | 374 EXPECT_EQ(mojom::FileError::OK, error); |
| 368 ASSERT_FALSE(file_info.is_null()); | 375 ASSERT_FALSE(file_info.is_null()); |
| 369 EXPECT_EQ(mojom::FsFileType::REGULAR_FILE, file_info->type); | 376 EXPECT_EQ(mojom::FsFileType::REGULAR_FILE, file_info->type); |
| 370 EXPECT_EQ(0, file_info->size); | 377 EXPECT_EQ(0, file_info->size); |
| 371 EXPECT_GT(file_info->atime, 0); // Expect that it's not 1970-01-01. | 378 EXPECT_GT(file_info->atime, 0); // Expect that it's not 1970-01-01. |
| 372 EXPECT_GT(file_info->mtime, 0); | 379 EXPECT_GT(file_info->mtime, 0); |
| 373 double first_mtime = file_info->mtime; | 380 double first_mtime = file_info->mtime; |
| 374 | 381 |
| 375 // Touch only the atime. | 382 // Touch only the atime. |
| 376 error = mojom::FileError::FAILED; | 383 error = mojom::FileError::FAILED; |
| 377 mojom::TimespecOrNowPtr t(mojom::TimespecOrNow::New()); | 384 mojom::TimespecOrNowPtr t(mojom::TimespecOrNow::New()); |
| 378 t->now = false; | 385 t->now = false; |
| 379 const int64_t kPartyTime1 = 1234567890; // Party like it's 2009-02-13. | 386 const int64_t kPartyTime1 = 1234567890; // Party like it's 2009-02-13. |
| 380 t->seconds = kPartyTime1; | 387 t->seconds = kPartyTime1; |
| 381 file->Touch(std::move(t), nullptr, Capture(&error)); | 388 handled = file->Touch(std::move(t), nullptr, &error); |
| 382 ASSERT_TRUE(file.WaitForIncomingResponse()); | 389 ASSERT_TRUE(handled); |
| 383 EXPECT_EQ(mojom::FileError::OK, error); | 390 EXPECT_EQ(mojom::FileError::OK, error); |
| 384 | 391 |
| 385 // Stat again. | 392 // Stat again. |
| 386 error = mojom::FileError::FAILED; | 393 error = mojom::FileError::FAILED; |
| 387 file_info.reset(); | 394 file_info.reset(); |
| 388 file->Stat(Capture(&error, &file_info)); | 395 handled = file->Stat(&error, &file_info); |
| 389 ASSERT_TRUE(file.WaitForIncomingResponse()); | 396 ASSERT_TRUE(handled); |
| 390 EXPECT_EQ(mojom::FileError::OK, error); | 397 EXPECT_EQ(mojom::FileError::OK, error); |
| 391 ASSERT_FALSE(file_info.is_null()); | 398 ASSERT_FALSE(file_info.is_null()); |
| 392 EXPECT_EQ(kPartyTime1, file_info->atime); | 399 EXPECT_EQ(kPartyTime1, file_info->atime); |
| 393 EXPECT_EQ(first_mtime, file_info->mtime); | 400 EXPECT_EQ(first_mtime, file_info->mtime); |
| 394 | 401 |
| 395 // Touch only the mtime. | 402 // Touch only the mtime. |
| 396 t = mojom::TimespecOrNow::New(); | 403 t = mojom::TimespecOrNow::New(); |
| 397 t->now = false; | 404 t->now = false; |
| 398 const int64_t kPartyTime2 = 1425059525; // No time like the present. | 405 const int64_t kPartyTime2 = 1425059525; // No time like the present. |
| 399 t->seconds = kPartyTime2; | 406 t->seconds = kPartyTime2; |
| 400 file->Touch(nullptr, std::move(t), Capture(&error)); | 407 handled = file->Touch(nullptr, std::move(t), &error); |
| 401 ASSERT_TRUE(file.WaitForIncomingResponse()); | 408 ASSERT_TRUE(handled); |
| 402 EXPECT_EQ(mojom::FileError::OK, error); | 409 EXPECT_EQ(mojom::FileError::OK, error); |
| 403 | 410 |
| 404 // Stat again. | 411 // Stat again. |
| 405 error = mojom::FileError::FAILED; | 412 error = mojom::FileError::FAILED; |
| 406 file_info.reset(); | 413 file_info.reset(); |
| 407 file->Stat(Capture(&error, &file_info)); | 414 handled = file->Stat(&error, &file_info); |
| 408 ASSERT_TRUE(file.WaitForIncomingResponse()); | 415 ASSERT_TRUE(handled); |
| 409 EXPECT_EQ(mojom::FileError::OK, error); | 416 EXPECT_EQ(mojom::FileError::OK, error); |
| 410 ASSERT_FALSE(file_info.is_null()); | 417 ASSERT_FALSE(file_info.is_null()); |
| 411 EXPECT_EQ(kPartyTime1, file_info->atime); | 418 EXPECT_EQ(kPartyTime1, file_info->atime); |
| 412 EXPECT_EQ(kPartyTime2, file_info->mtime); | 419 EXPECT_EQ(kPartyTime2, file_info->mtime); |
| 413 | 420 |
| 414 // TODO(vtl): Also test non-zero file size. | 421 // TODO(vtl): Also test non-zero file size. |
| 415 // TODO(vtl): Also test Touch() "now" options. | 422 // TODO(vtl): Also test Touch() "now" options. |
| 416 // TODO(vtl): Also test touching both atime and mtime. | 423 // TODO(vtl): Also test touching both atime and mtime. |
| 417 } | 424 } |
| 418 | 425 |
| 419 TEST_F(FileImplTest, TellSeek) { | 426 TEST_F(FileImplTest, TellSeek) { |
| 420 mojom::DirectoryPtr directory; | 427 mojom::DirectoryPtr directory; |
| 421 GetTemporaryRoot(&directory); | 428 GetTemporaryRoot(&directory); |
| 422 mojom::FileError error; | 429 mojom::FileError error; |
| 423 | 430 |
| 424 // Create my_file. | 431 // Create my_file. |
| 425 mojom::FilePtr file; | 432 mojom::FilePtr file; |
| 426 error = mojom::FileError::FAILED; | 433 error = mojom::FileError::FAILED; |
| 427 directory->OpenFile("my_file", GetProxy(&file), | 434 bool handled = |
| 428 mojom::kFlagWrite | mojom::kFlagCreate, Capture(&error)); | 435 directory->OpenFile("my_file", GetProxy(&file), |
| 429 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 436 mojom::kFlagWrite | mojom::kFlagCreate, &error); |
| 437 ASSERT_TRUE(handled); |
| 430 EXPECT_EQ(mojom::FileError::OK, error); | 438 EXPECT_EQ(mojom::FileError::OK, error); |
| 431 | 439 |
| 432 // Write to it. | 440 // Write to it. |
| 433 std::vector<uint8_t> bytes_to_write(1000, '!'); | 441 std::vector<uint8_t> bytes_to_write(1000, '!'); |
| 434 error = mojom::FileError::FAILED; | 442 error = mojom::FileError::FAILED; |
| 435 uint32_t num_bytes_written = 0; | 443 uint32_t num_bytes_written = 0; |
| 436 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, | 444 handled = |
| 437 mojom::Whence::FROM_CURRENT, Capture(&error, &num_bytes_written)); | 445 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, |
| 438 ASSERT_TRUE(file.WaitForIncomingResponse()); | 446 mojom::Whence::FROM_CURRENT, &error, &num_bytes_written); |
| 447 ASSERT_TRUE(handled); |
| 439 EXPECT_EQ(mojom::FileError::OK, error); | 448 EXPECT_EQ(mojom::FileError::OK, error); |
| 440 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); | 449 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); |
| 441 const int size = static_cast<int>(num_bytes_written); | 450 const int size = static_cast<int>(num_bytes_written); |
| 442 | 451 |
| 443 // Tell. | 452 // Tell. |
| 444 error = mojom::FileError::FAILED; | 453 error = mojom::FileError::FAILED; |
| 445 int64_t position = -1; | 454 int64_t position = -1; |
| 446 file->Tell(Capture(&error, &position)); | 455 handled = file->Tell(&error, &position); |
| 447 ASSERT_TRUE(file.WaitForIncomingResponse()); | 456 ASSERT_TRUE(handled); |
| 448 // Should be at the end. | 457 // Should be at the end. |
| 449 EXPECT_EQ(mojom::FileError::OK, error); | 458 EXPECT_EQ(mojom::FileError::OK, error); |
| 450 EXPECT_EQ(size, position); | 459 EXPECT_EQ(size, position); |
| 451 | 460 |
| 452 // Seek back 100. | 461 // Seek back 100. |
| 453 error = mojom::FileError::FAILED; | 462 error = mojom::FileError::FAILED; |
| 454 position = -1; | 463 position = -1; |
| 455 file->Seek(-100, mojom::Whence::FROM_CURRENT, Capture(&error, &position)); | 464 handled = file->Seek(-100, mojom::Whence::FROM_CURRENT, &error, &position); |
| 456 ASSERT_TRUE(file.WaitForIncomingResponse()); | 465 ASSERT_TRUE(handled); |
| 457 EXPECT_EQ(mojom::FileError::OK, error); | 466 EXPECT_EQ(mojom::FileError::OK, error); |
| 458 EXPECT_EQ(size - 100, position); | 467 EXPECT_EQ(size - 100, position); |
| 459 | 468 |
| 460 // Tell. | 469 // Tell. |
| 461 error = mojom::FileError::FAILED; | 470 error = mojom::FileError::FAILED; |
| 462 position = -1; | 471 position = -1; |
| 463 file->Tell(Capture(&error, &position)); | 472 handled = file->Tell(&error, &position); |
| 464 ASSERT_TRUE(file.WaitForIncomingResponse()); | 473 ASSERT_TRUE(handled); |
| 465 EXPECT_EQ(mojom::FileError::OK, error); | 474 EXPECT_EQ(mojom::FileError::OK, error); |
| 466 EXPECT_EQ(size - 100, position); | 475 EXPECT_EQ(size - 100, position); |
| 467 | 476 |
| 468 // Seek to 123 from start. | 477 // Seek to 123 from start. |
| 469 error = mojom::FileError::FAILED; | 478 error = mojom::FileError::FAILED; |
| 470 position = -1; | 479 position = -1; |
| 471 file->Seek(123, mojom::Whence::FROM_BEGIN, Capture(&error, &position)); | 480 handled = file->Seek(123, mojom::Whence::FROM_BEGIN, &error, &position); |
| 472 ASSERT_TRUE(file.WaitForIncomingResponse()); | 481 ASSERT_TRUE(handled); |
| 473 EXPECT_EQ(mojom::FileError::OK, error); | 482 EXPECT_EQ(mojom::FileError::OK, error); |
| 474 EXPECT_EQ(123, position); | 483 EXPECT_EQ(123, position); |
| 475 | 484 |
| 476 // Tell. | 485 // Tell. |
| 477 error = mojom::FileError::FAILED; | 486 error = mojom::FileError::FAILED; |
| 478 position = -1; | 487 position = -1; |
| 479 file->Tell(Capture(&error, &position)); | 488 handled = file->Tell(&error, &position); |
| 480 ASSERT_TRUE(file.WaitForIncomingResponse()); | 489 ASSERT_TRUE(handled); |
| 481 EXPECT_EQ(mojom::FileError::OK, error); | 490 EXPECT_EQ(mojom::FileError::OK, error); |
| 482 EXPECT_EQ(123, position); | 491 EXPECT_EQ(123, position); |
| 483 | 492 |
| 484 // Seek to 123 back from end. | 493 // Seek to 123 back from end. |
| 485 error = mojom::FileError::FAILED; | 494 error = mojom::FileError::FAILED; |
| 486 position = -1; | 495 position = -1; |
| 487 file->Seek(-123, mojom::Whence::FROM_END, Capture(&error, &position)); | 496 handled = file->Seek(-123, mojom::Whence::FROM_END, &error, &position); |
| 488 ASSERT_TRUE(file.WaitForIncomingResponse()); | 497 ASSERT_TRUE(handled); |
| 489 EXPECT_EQ(mojom::FileError::OK, error); | 498 EXPECT_EQ(mojom::FileError::OK, error); |
| 490 EXPECT_EQ(size - 123, position); | 499 EXPECT_EQ(size - 123, position); |
| 491 | 500 |
| 492 // Tell. | 501 // Tell. |
| 493 error = mojom::FileError::FAILED; | 502 error = mojom::FileError::FAILED; |
| 494 position = -1; | 503 position = -1; |
| 495 file->Tell(Capture(&error, &position)); | 504 handled = file->Tell(&error, &position); |
| 496 ASSERT_TRUE(file.WaitForIncomingResponse()); | 505 ASSERT_TRUE(handled); |
| 497 EXPECT_EQ(mojom::FileError::OK, error); | 506 EXPECT_EQ(mojom::FileError::OK, error); |
| 498 EXPECT_EQ(size - 123, position); | 507 EXPECT_EQ(size - 123, position); |
| 499 | 508 |
| 500 // TODO(vtl): Check that seeking actually affects reading/writing. | 509 // TODO(vtl): Check that seeking actually affects reading/writing. |
| 501 // TODO(vtl): Check that seeking can extend the file? | 510 // TODO(vtl): Check that seeking can extend the file? |
| 502 } | 511 } |
| 503 | 512 |
| 504 TEST_F(FileImplTest, Dup) { | 513 TEST_F(FileImplTest, Dup) { |
| 505 mojom::DirectoryPtr directory; | 514 mojom::DirectoryPtr directory; |
| 506 GetTemporaryRoot(&directory); | 515 GetTemporaryRoot(&directory); |
| 507 mojom::FileError error; | 516 mojom::FileError error; |
| 508 | 517 |
| 509 // Create my_file. | 518 // Create my_file. |
| 510 mojom::FilePtr file1; | 519 mojom::FilePtr file1; |
| 511 error = mojom::FileError::FAILED; | 520 error = mojom::FileError::FAILED; |
| 512 directory->OpenFile("my_file", GetProxy(&file1), | 521 bool handled = directory->OpenFile( |
| 513 mojom::kFlagRead | mojom::kFlagWrite | mojom::kFlagCreate, | 522 "my_file", GetProxy(&file1), |
| 514 Capture(&error)); | 523 mojom::kFlagRead | mojom::kFlagWrite | mojom::kFlagCreate, &error); |
| 515 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 524 ASSERT_TRUE(handled); |
| 516 EXPECT_EQ(mojom::FileError::OK, error); | 525 EXPECT_EQ(mojom::FileError::OK, error); |
| 517 | 526 |
| 518 // Write to it. | 527 // Write to it. |
| 519 std::vector<uint8_t> bytes_to_write; | 528 std::vector<uint8_t> bytes_to_write; |
| 520 bytes_to_write.push_back(static_cast<uint8_t>('h')); | 529 bytes_to_write.push_back(static_cast<uint8_t>('h')); |
| 521 bytes_to_write.push_back(static_cast<uint8_t>('e')); | 530 bytes_to_write.push_back(static_cast<uint8_t>('e')); |
| 522 bytes_to_write.push_back(static_cast<uint8_t>('l')); | 531 bytes_to_write.push_back(static_cast<uint8_t>('l')); |
| 523 bytes_to_write.push_back(static_cast<uint8_t>('l')); | 532 bytes_to_write.push_back(static_cast<uint8_t>('l')); |
| 524 bytes_to_write.push_back(static_cast<uint8_t>('o')); | 533 bytes_to_write.push_back(static_cast<uint8_t>('o')); |
| 525 error = mojom::FileError::FAILED; | 534 error = mojom::FileError::FAILED; |
| 526 uint32_t num_bytes_written = 0; | 535 uint32_t num_bytes_written = 0; |
| 527 file1->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, | 536 handled = |
| 528 mojom::Whence::FROM_CURRENT, | 537 file1->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, |
| 529 Capture(&error, &num_bytes_written)); | 538 mojom::Whence::FROM_CURRENT, &error, &num_bytes_written); |
| 530 ASSERT_TRUE(file1.WaitForIncomingResponse()); | 539 ASSERT_TRUE(handled); |
| 531 EXPECT_EQ(mojom::FileError::OK, error); | 540 EXPECT_EQ(mojom::FileError::OK, error); |
| 532 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); | 541 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); |
| 533 const int end_hello_pos = static_cast<int>(num_bytes_written); | 542 const int end_hello_pos = static_cast<int>(num_bytes_written); |
| 534 | 543 |
| 535 // Dup it. | 544 // Dup it. |
| 536 mojom::FilePtr file2; | 545 mojom::FilePtr file2; |
| 537 error = mojom::FileError::FAILED; | 546 error = mojom::FileError::FAILED; |
| 538 file1->Dup(GetProxy(&file2), Capture(&error)); | 547 handled = file1->Dup(GetProxy(&file2), &error); |
| 539 ASSERT_TRUE(file1.WaitForIncomingResponse()); | 548 ASSERT_TRUE(handled); |
| 540 EXPECT_EQ(mojom::FileError::OK, error); | 549 EXPECT_EQ(mojom::FileError::OK, error); |
| 541 | 550 |
| 542 // |file2| should have the same position. | 551 // |file2| should have the same position. |
| 543 error = mojom::FileError::FAILED; | 552 error = mojom::FileError::FAILED; |
| 544 int64_t position = -1; | 553 int64_t position = -1; |
| 545 file2->Tell(Capture(&error, &position)); | 554 handled = file2->Tell(&error, &position); |
| 546 ASSERT_TRUE(file2.WaitForIncomingResponse()); | 555 ASSERT_TRUE(handled); |
| 547 EXPECT_EQ(mojom::FileError::OK, error); | 556 EXPECT_EQ(mojom::FileError::OK, error); |
| 548 EXPECT_EQ(end_hello_pos, position); | 557 EXPECT_EQ(end_hello_pos, position); |
| 549 | 558 |
| 550 // Write using |file2|. | 559 // Write using |file2|. |
| 551 std::vector<uint8_t> more_bytes_to_write; | 560 std::vector<uint8_t> more_bytes_to_write; |
| 552 more_bytes_to_write.push_back(static_cast<uint8_t>('w')); | 561 more_bytes_to_write.push_back(static_cast<uint8_t>('w')); |
| 553 more_bytes_to_write.push_back(static_cast<uint8_t>('o')); | 562 more_bytes_to_write.push_back(static_cast<uint8_t>('o')); |
| 554 more_bytes_to_write.push_back(static_cast<uint8_t>('r')); | 563 more_bytes_to_write.push_back(static_cast<uint8_t>('r')); |
| 555 more_bytes_to_write.push_back(static_cast<uint8_t>('l')); | 564 more_bytes_to_write.push_back(static_cast<uint8_t>('l')); |
| 556 more_bytes_to_write.push_back(static_cast<uint8_t>('d')); | 565 more_bytes_to_write.push_back(static_cast<uint8_t>('d')); |
| 557 error = mojom::FileError::FAILED; | 566 error = mojom::FileError::FAILED; |
| 558 num_bytes_written = 0; | 567 num_bytes_written = 0; |
| 559 file2->Write(mojo::Array<uint8_t>::From(more_bytes_to_write), 0, | 568 handled = |
| 560 mojom::Whence::FROM_CURRENT, | 569 file2->Write(mojo::Array<uint8_t>::From(more_bytes_to_write), 0, |
| 561 Capture(&error, &num_bytes_written)); | 570 mojom::Whence::FROM_CURRENT, &error, &num_bytes_written); |
| 562 ASSERT_TRUE(file2.WaitForIncomingResponse()); | 571 ASSERT_TRUE(handled); |
| 563 EXPECT_EQ(mojom::FileError::OK, error); | 572 EXPECT_EQ(mojom::FileError::OK, error); |
| 564 EXPECT_EQ(more_bytes_to_write.size(), num_bytes_written); | 573 EXPECT_EQ(more_bytes_to_write.size(), num_bytes_written); |
| 565 const int end_world_pos = end_hello_pos + static_cast<int>(num_bytes_written); | 574 const int end_world_pos = end_hello_pos + static_cast<int>(num_bytes_written); |
| 566 | 575 |
| 567 // |file1| should have the same position. | 576 // |file1| should have the same position. |
| 568 error = mojom::FileError::FAILED; | 577 error = mojom::FileError::FAILED; |
| 569 position = -1; | 578 position = -1; |
| 570 file1->Tell(Capture(&error, &position)); | 579 handled = file1->Tell(&error, &position); |
| 571 ASSERT_TRUE(file1.WaitForIncomingResponse()); | 580 ASSERT_TRUE(handled); |
| 572 EXPECT_EQ(mojom::FileError::OK, error); | 581 EXPECT_EQ(mojom::FileError::OK, error); |
| 573 EXPECT_EQ(end_world_pos, position); | 582 EXPECT_EQ(end_world_pos, position); |
| 574 | 583 |
| 575 // Close |file1|. | 584 // Close |file1|. |
| 576 error = mojom::FileError::FAILED; | 585 error = mojom::FileError::FAILED; |
| 577 file1->Close(Capture(&error)); | 586 handled = file1->Close(&error); |
| 578 ASSERT_TRUE(file1.WaitForIncomingResponse()); | 587 ASSERT_TRUE(handled); |
| 579 EXPECT_EQ(mojom::FileError::OK, error); | 588 EXPECT_EQ(mojom::FileError::OK, error); |
| 580 | 589 |
| 581 // Read everything using |file2|. | 590 // Read everything using |file2|. |
| 582 mojo::Array<uint8_t> bytes_read; | 591 mojo::Array<uint8_t> bytes_read; |
| 583 error = mojom::FileError::FAILED; | 592 error = mojom::FileError::FAILED; |
| 584 file2->Read(1000, 0, mojom::Whence::FROM_BEGIN, Capture(&error, &bytes_read)); | 593 handled = |
| 585 ASSERT_TRUE(file2.WaitForIncomingResponse()); | 594 file2->Read(1000, 0, mojom::Whence::FROM_BEGIN, &error, &bytes_read); |
| 595 ASSERT_TRUE(handled); |
| 586 EXPECT_EQ(mojom::FileError::OK, error); | 596 EXPECT_EQ(mojom::FileError::OK, error); |
| 587 ASSERT_EQ(static_cast<size_t>(end_world_pos), bytes_read.size()); | 597 ASSERT_EQ(static_cast<size_t>(end_world_pos), bytes_read.size()); |
| 588 // Just check the first and last bytes. | 598 // Just check the first and last bytes. |
| 589 EXPECT_EQ(static_cast<uint8_t>('h'), bytes_read[0]); | 599 EXPECT_EQ(static_cast<uint8_t>('h'), bytes_read[0]); |
| 590 EXPECT_EQ(static_cast<uint8_t>('d'), bytes_read[end_world_pos - 1]); | 600 EXPECT_EQ(static_cast<uint8_t>('d'), bytes_read[end_world_pos - 1]); |
| 591 | 601 |
| 592 // TODO(vtl): Test that |file2| has the same open options as |file1|. | 602 // TODO(vtl): Test that |file2| has the same open options as |file1|. |
| 593 } | 603 } |
| 594 | 604 |
| 595 TEST_F(FileImplTest, Truncate) { | 605 TEST_F(FileImplTest, Truncate) { |
| 596 const uint32_t kInitialSize = 1000; | 606 const uint32_t kInitialSize = 1000; |
| 597 const uint32_t kTruncatedSize = 654; | 607 const uint32_t kTruncatedSize = 654; |
| 598 | 608 |
| 599 mojom::DirectoryPtr directory; | 609 mojom::DirectoryPtr directory; |
| 600 GetTemporaryRoot(&directory); | 610 GetTemporaryRoot(&directory); |
| 601 mojom::FileError error; | 611 mojom::FileError error; |
| 602 | 612 |
| 603 // Create my_file. | 613 // Create my_file. |
| 604 mojom::FilePtr file; | 614 mojom::FilePtr file; |
| 605 error = mojom::FileError::FAILED; | 615 error = mojom::FileError::FAILED; |
| 606 directory->OpenFile("my_file", GetProxy(&file), | 616 bool handled = |
| 607 mojom::kFlagWrite | mojom::kFlagCreate, Capture(&error)); | 617 directory->OpenFile("my_file", GetProxy(&file), |
| 608 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 618 mojom::kFlagWrite | mojom::kFlagCreate, &error); |
| 619 ASSERT_TRUE(handled); |
| 609 EXPECT_EQ(mojom::FileError::OK, error); | 620 EXPECT_EQ(mojom::FileError::OK, error); |
| 610 | 621 |
| 611 // Write to it. | 622 // Write to it. |
| 612 std::vector<uint8_t> bytes_to_write(kInitialSize, '!'); | 623 std::vector<uint8_t> bytes_to_write(kInitialSize, '!'); |
| 613 error = mojom::FileError::FAILED; | 624 error = mojom::FileError::FAILED; |
| 614 uint32_t num_bytes_written = 0; | 625 uint32_t num_bytes_written = 0; |
| 615 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, | 626 handled = |
| 616 mojom::Whence::FROM_CURRENT, Capture(&error, &num_bytes_written)); | 627 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, |
| 617 ASSERT_TRUE(file.WaitForIncomingResponse()); | 628 mojom::Whence::FROM_CURRENT, &error, &num_bytes_written); |
| 629 ASSERT_TRUE(handled); |
| 618 EXPECT_EQ(mojom::FileError::OK, error); | 630 EXPECT_EQ(mojom::FileError::OK, error); |
| 619 EXPECT_EQ(kInitialSize, num_bytes_written); | 631 EXPECT_EQ(kInitialSize, num_bytes_written); |
| 620 | 632 |
| 621 // Stat it. | 633 // Stat it. |
| 622 error = mojom::FileError::FAILED; | 634 error = mojom::FileError::FAILED; |
| 623 mojom::FileInformationPtr file_info; | 635 mojom::FileInformationPtr file_info; |
| 624 file->Stat(Capture(&error, &file_info)); | 636 handled = file->Stat(&error, &file_info); |
| 625 ASSERT_TRUE(file.WaitForIncomingResponse()); | 637 ASSERT_TRUE(handled); |
| 626 EXPECT_EQ(mojom::FileError::OK, error); | 638 EXPECT_EQ(mojom::FileError::OK, error); |
| 627 ASSERT_FALSE(file_info.is_null()); | 639 ASSERT_FALSE(file_info.is_null()); |
| 628 EXPECT_EQ(kInitialSize, file_info->size); | 640 EXPECT_EQ(kInitialSize, file_info->size); |
| 629 | 641 |
| 630 // Truncate it. | 642 // Truncate it. |
| 631 error = mojom::FileError::FAILED; | 643 error = mojom::FileError::FAILED; |
| 632 file->Truncate(kTruncatedSize, Capture(&error)); | 644 handled = file->Truncate(kTruncatedSize, &error); |
| 633 ASSERT_TRUE(file.WaitForIncomingResponse()); | 645 ASSERT_TRUE(handled); |
| 634 EXPECT_EQ(mojom::FileError::OK, error); | 646 EXPECT_EQ(mojom::FileError::OK, error); |
| 635 | 647 |
| 636 // Stat again. | 648 // Stat again. |
| 637 error = mojom::FileError::FAILED; | 649 error = mojom::FileError::FAILED; |
| 638 file_info.reset(); | 650 file_info.reset(); |
| 639 file->Stat(Capture(&error, &file_info)); | 651 handled = file->Stat(&error, &file_info); |
| 640 ASSERT_TRUE(file.WaitForIncomingResponse()); | 652 ASSERT_TRUE(handled); |
| 641 EXPECT_EQ(mojom::FileError::OK, error); | 653 EXPECT_EQ(mojom::FileError::OK, error); |
| 642 ASSERT_FALSE(file_info.is_null()); | 654 ASSERT_FALSE(file_info.is_null()); |
| 643 EXPECT_EQ(kTruncatedSize, file_info->size); | 655 EXPECT_EQ(kTruncatedSize, file_info->size); |
| 644 } | 656 } |
| 645 | 657 |
| 646 TEST_F(FileImplTest, AsHandle) { | 658 TEST_F(FileImplTest, AsHandle) { |
| 647 mojom::DirectoryPtr directory; | 659 mojom::DirectoryPtr directory; |
| 648 GetTemporaryRoot(&directory); | 660 GetTemporaryRoot(&directory); |
| 649 mojom::FileError error; | 661 mojom::FileError error; |
| 650 | 662 |
| 651 { | 663 { |
| 652 // Create my_file. | 664 // Create my_file. |
| 653 mojom::FilePtr file1; | 665 mojom::FilePtr file1; |
| 654 error = mojom::FileError::FAILED; | 666 error = mojom::FileError::FAILED; |
| 655 directory->OpenFile( | 667 bool handled = directory->OpenFile( |
| 656 "my_file", GetProxy(&file1), | 668 "my_file", GetProxy(&file1), |
| 657 mojom::kFlagRead | mojom::kFlagWrite | mojom::kFlagCreate, | 669 mojom::kFlagRead | mojom::kFlagWrite | mojom::kFlagCreate, &error); |
| 658 Capture(&error)); | 670 ASSERT_TRUE(handled); |
| 659 ASSERT_TRUE(directory.WaitForIncomingResponse()); | |
| 660 EXPECT_EQ(mojom::FileError::OK, error); | 671 EXPECT_EQ(mojom::FileError::OK, error); |
| 661 | 672 |
| 662 // Fetch the handle | 673 // Fetch the handle |
| 663 error = mojom::FileError::FAILED; | 674 error = mojom::FileError::FAILED; |
| 664 mojo::ScopedHandle handle; | 675 mojo::ScopedHandle handle; |
| 665 file1->AsHandle(Capture(&error, &handle)); | 676 handled = file1->AsHandle(&error, &handle); |
| 666 ASSERT_TRUE(file1.WaitForIncomingResponse()); | 677 ASSERT_TRUE(handled); |
| 667 EXPECT_EQ(mojom::FileError::OK, error); | 678 EXPECT_EQ(mojom::FileError::OK, error); |
| 668 | 679 |
| 669 // Pull a file descriptor out of the scoped handle. | 680 // Pull a file descriptor out of the scoped handle. |
| 670 base::PlatformFile platform_file; | 681 base::PlatformFile platform_file; |
| 671 MojoResult unwrap_result = mojo::UnwrapPlatformFile(std::move(handle), | 682 MojoResult unwrap_result = mojo::UnwrapPlatformFile(std::move(handle), |
| 672 &platform_file); | 683 &platform_file); |
| 673 EXPECT_EQ(MOJO_RESULT_OK, unwrap_result); | 684 EXPECT_EQ(MOJO_RESULT_OK, unwrap_result); |
| 674 | 685 |
| 675 // Pass this raw file descriptor to a base::File. | 686 // Pass this raw file descriptor to a base::File. |
| 676 base::File raw_file(platform_file); | 687 base::File raw_file(platform_file); |
| 677 ASSERT_TRUE(raw_file.IsValid()); | 688 ASSERT_TRUE(raw_file.IsValid()); |
| 678 EXPECT_EQ(5, raw_file.WriteAtCurrentPos("hello", 5)); | 689 EXPECT_EQ(5, raw_file.WriteAtCurrentPos("hello", 5)); |
| 679 } | 690 } |
| 680 | 691 |
| 681 { | 692 { |
| 682 // Reopen my_file. | 693 // Reopen my_file. |
| 683 mojom::FilePtr file2; | 694 mojom::FilePtr file2; |
| 684 error = mojom::FileError::FAILED; | 695 error = mojom::FileError::FAILED; |
| 685 directory->OpenFile("my_file", GetProxy(&file2), | 696 bool handled = |
| 686 mojom::kFlagRead | mojom::kFlagOpen, Capture(&error)); | 697 directory->OpenFile("my_file", GetProxy(&file2), |
| 687 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 698 mojom::kFlagRead | mojom::kFlagOpen, &error); |
| 699 ASSERT_TRUE(handled); |
| 688 EXPECT_EQ(mojom::FileError::OK, error); | 700 EXPECT_EQ(mojom::FileError::OK, error); |
| 689 | 701 |
| 690 // Verify that we wrote data raw on the file descriptor. | 702 // Verify that we wrote data raw on the file descriptor. |
| 691 mojo::Array<uint8_t> bytes_read; | 703 mojo::Array<uint8_t> bytes_read; |
| 692 error = mojom::FileError::FAILED; | 704 error = mojom::FileError::FAILED; |
| 693 file2->Read(5, 0, mojom::Whence::FROM_BEGIN, Capture(&error, &bytes_read)); | 705 handled = file2->Read(5, 0, mojom::Whence::FROM_BEGIN, &error, &bytes_read); |
| 694 ASSERT_TRUE(file2.WaitForIncomingResponse()); | 706 ASSERT_TRUE(handled); |
| 695 EXPECT_EQ(mojom::FileError::OK, error); | 707 EXPECT_EQ(mojom::FileError::OK, error); |
| 696 ASSERT_EQ(5u, bytes_read.size()); | 708 ASSERT_EQ(5u, bytes_read.size()); |
| 697 EXPECT_EQ(static_cast<uint8_t>('h'), bytes_read[0]); | 709 EXPECT_EQ(static_cast<uint8_t>('h'), bytes_read[0]); |
| 698 EXPECT_EQ(static_cast<uint8_t>('e'), bytes_read[1]); | 710 EXPECT_EQ(static_cast<uint8_t>('e'), bytes_read[1]); |
| 699 EXPECT_EQ(static_cast<uint8_t>('l'), bytes_read[2]); | 711 EXPECT_EQ(static_cast<uint8_t>('l'), bytes_read[2]); |
| 700 EXPECT_EQ(static_cast<uint8_t>('l'), bytes_read[3]); | 712 EXPECT_EQ(static_cast<uint8_t>('l'), bytes_read[3]); |
| 701 EXPECT_EQ(static_cast<uint8_t>('o'), bytes_read[4]); | 713 EXPECT_EQ(static_cast<uint8_t>('o'), bytes_read[4]); |
| 702 } | 714 } |
| 703 } | 715 } |
| 704 | 716 |
| 705 TEST_F(FileImplTest, SimpleLockUnlock) { | 717 TEST_F(FileImplTest, SimpleLockUnlock) { |
| 706 mojom::DirectoryPtr directory; | 718 mojom::DirectoryPtr directory; |
| 707 GetTemporaryRoot(&directory); | 719 GetTemporaryRoot(&directory); |
| 708 mojom::FileError error; | 720 mojom::FileError error; |
| 709 | 721 |
| 710 // Create my_file. | 722 // Create my_file. |
| 711 mojom::FilePtr file; | 723 mojom::FilePtr file; |
| 712 error = mojom::FileError::FAILED; | 724 error = mojom::FileError::FAILED; |
| 713 directory->OpenFile("my_file", GetProxy(&file), | 725 bool handled = directory->OpenFile( |
| 714 mojom::kFlagRead | mojom::kFlagWrite | mojom::kFlagCreate, | 726 "my_file", GetProxy(&file), |
| 715 Capture(&error)); | 727 mojom::kFlagRead | mojom::kFlagWrite | mojom::kFlagCreate, &error); |
| 716 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 728 ASSERT_TRUE(handled); |
| 717 EXPECT_EQ(mojom::FileError::OK, error); | 729 EXPECT_EQ(mojom::FileError::OK, error); |
| 718 | 730 |
| 719 // Lock the file. | 731 // Lock the file. |
| 720 error = mojom::FileError::FAILED; | 732 error = mojom::FileError::FAILED; |
| 721 file->Lock(Capture(&error)); | 733 handled = file->Lock(&error); |
| 722 ASSERT_TRUE(file.WaitForIncomingResponse()); | 734 ASSERT_TRUE(handled); |
| 723 EXPECT_EQ(mojom::FileError::OK, error); | 735 EXPECT_EQ(mojom::FileError::OK, error); |
| 724 | 736 |
| 725 // Unlock the file. | 737 // Unlock the file. |
| 726 error = mojom::FileError::FAILED; | 738 error = mojom::FileError::FAILED; |
| 727 file->Unlock(Capture(&error)); | 739 handled = file->Unlock(&error); |
| 728 ASSERT_TRUE(file.WaitForIncomingResponse()); | 740 ASSERT_TRUE(handled); |
| 729 EXPECT_EQ(mojom::FileError::OK, error); | 741 EXPECT_EQ(mojom::FileError::OK, error); |
| 730 } | 742 } |
| 731 | 743 |
| 732 TEST_F(FileImplTest, CantDoubleLock) { | 744 TEST_F(FileImplTest, CantDoubleLock) { |
| 733 mojom::DirectoryPtr directory; | 745 mojom::DirectoryPtr directory; |
| 734 GetTemporaryRoot(&directory); | 746 GetTemporaryRoot(&directory); |
| 735 mojom::FileError error; | 747 mojom::FileError error; |
| 736 | 748 |
| 737 // Create my_file. | 749 // Create my_file. |
| 738 mojom::FilePtr file; | 750 mojom::FilePtr file; |
| 739 error = mojom::FileError::FAILED; | 751 error = mojom::FileError::FAILED; |
| 740 directory->OpenFile("my_file", GetProxy(&file), | 752 bool handled = directory->OpenFile( |
| 741 mojom::kFlagRead | mojom::kFlagWrite | mojom::kFlagCreate, | 753 "my_file", GetProxy(&file), |
| 742 Capture(&error)); | 754 mojom::kFlagRead | mojom::kFlagWrite | mojom::kFlagCreate, &error); |
| 743 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 755 ASSERT_TRUE(handled); |
| 744 EXPECT_EQ(mojom::FileError::OK, error); | 756 EXPECT_EQ(mojom::FileError::OK, error); |
| 745 | 757 |
| 746 // Lock the file. | 758 // Lock the file. |
| 747 error = mojom::FileError::FAILED; | 759 error = mojom::FileError::FAILED; |
| 748 file->Lock(Capture(&error)); | 760 handled = file->Lock(&error); |
| 749 ASSERT_TRUE(file.WaitForIncomingResponse()); | 761 ASSERT_TRUE(handled); |
| 750 EXPECT_EQ(mojom::FileError::OK, error); | 762 EXPECT_EQ(mojom::FileError::OK, error); |
| 751 | 763 |
| 752 // Lock the file again. | 764 // Lock the file again. |
| 753 error = mojom::FileError::OK; | 765 error = mojom::FileError::OK; |
| 754 file->Lock(Capture(&error)); | 766 handled = file->Lock(&error); |
| 755 ASSERT_TRUE(file.WaitForIncomingResponse()); | 767 ASSERT_TRUE(handled); |
| 756 EXPECT_EQ(mojom::FileError::FAILED, error); | 768 EXPECT_EQ(mojom::FileError::FAILED, error); |
| 757 } | 769 } |
| 758 | 770 |
| 759 TEST_F(FileImplTest, ClosingFileClearsLock) { | 771 TEST_F(FileImplTest, ClosingFileClearsLock) { |
| 760 mojom::DirectoryPtr directory; | 772 mojom::DirectoryPtr directory; |
| 761 GetTemporaryRoot(&directory); | 773 GetTemporaryRoot(&directory); |
| 762 mojom::FileError error; | 774 mojom::FileError error; |
| 763 | 775 |
| 764 { | 776 { |
| 765 // Create my_file. | 777 // Create my_file. |
| 766 mojom::FilePtr file; | 778 mojom::FilePtr file; |
| 767 error = mojom::FileError::FAILED; | 779 error = mojom::FileError::FAILED; |
| 768 directory->OpenFile( | 780 bool handled = directory->OpenFile( |
| 769 "my_file", GetProxy(&file), | 781 "my_file", GetProxy(&file), |
| 770 mojom::kFlagRead | mojom::kFlagWrite | mojom::kFlagOpenAlways, | 782 mojom::kFlagRead | mojom::kFlagWrite | mojom::kFlagOpenAlways, &error); |
| 771 Capture(&error)); | 783 ASSERT_TRUE(handled); |
| 772 ASSERT_TRUE(directory.WaitForIncomingResponse()); | |
| 773 EXPECT_EQ(mojom::FileError::OK, error); | 784 EXPECT_EQ(mojom::FileError::OK, error); |
| 774 | 785 |
| 775 // Lock the file. | 786 // Lock the file. |
| 776 error = mojom::FileError::FAILED; | 787 error = mojom::FileError::FAILED; |
| 777 file->Lock(Capture(&error)); | 788 handled = file->Lock(&error); |
| 778 ASSERT_TRUE(file.WaitForIncomingResponse()); | 789 ASSERT_TRUE(handled); |
| 779 EXPECT_EQ(mojom::FileError::OK, error); | 790 EXPECT_EQ(mojom::FileError::OK, error); |
| 780 } | 791 } |
| 781 | 792 |
| 782 { | 793 { |
| 783 // Open the file again. | 794 // Open the file again. |
| 784 mojom::FilePtr file; | 795 mojom::FilePtr file; |
| 785 error = mojom::FileError::FAILED; | 796 error = mojom::FileError::FAILED; |
| 786 directory->OpenFile( | 797 bool handled = directory->OpenFile( |
| 787 "my_file", GetProxy(&file), | 798 "my_file", GetProxy(&file), |
| 788 mojom::kFlagRead | mojom::kFlagWrite | mojom::kFlagOpenAlways, | 799 mojom::kFlagRead | mojom::kFlagWrite | mojom::kFlagOpenAlways, &error); |
| 789 Capture(&error)); | 800 ASSERT_TRUE(handled); |
| 790 ASSERT_TRUE(directory.WaitForIncomingResponse()); | |
| 791 EXPECT_EQ(mojom::FileError::OK, error); | 801 EXPECT_EQ(mojom::FileError::OK, error); |
| 792 | 802 |
| 793 // The file shouldn't be locked (and we check by trying to lock it). | 803 // The file shouldn't be locked (and we check by trying to lock it). |
| 794 error = mojom::FileError::FAILED; | 804 error = mojom::FileError::FAILED; |
| 795 file->Lock(Capture(&error)); | 805 handled = file->Lock(&error); |
| 796 ASSERT_TRUE(file.WaitForIncomingResponse()); | 806 ASSERT_TRUE(handled); |
| 797 EXPECT_EQ(mojom::FileError::OK, error); | 807 EXPECT_EQ(mojom::FileError::OK, error); |
| 798 } | 808 } |
| 799 } | 809 } |
| 800 | 810 |
| 801 } // namespace | 811 } // namespace |
| 802 } // namespace filesystem | 812 } // namespace filesystem |
| OLD | NEW |