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