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