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