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