| 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 "files/public/c/tests/test_utils.h" | 5 #include "files/public/c/tests/test_utils.h" |
| 6 | 6 |
| 7 #include "files/public/c/lib/template_util.h" | 7 #include "files/public/c/lib/template_util.h" |
| 8 #include "files/public/interfaces/file.mojom.h" | 8 #include "files/public/interfaces/file.mojom.h" |
| 9 #include "files/public/interfaces/types.mojom.h" | 9 #include "files/public/interfaces/types.mojom.h" |
| 10 #include "mojo/public/cpp/environment/logging.h" | 10 #include "mojo/public/cpp/environment/logging.h" |
| 11 | 11 |
| 12 namespace mojio { | 12 namespace mojio { |
| 13 namespace test { | 13 namespace test { |
| 14 | 14 |
| 15 void MakeDirAt(mojo::files::DirectoryPtr* root, const char* path) { | 15 void MakeDirAt(mojo::files::DirectoryPtr* root, const char* path) { |
| 16 MOJO_CHECK(root); | 16 MOJO_CHECK(root); |
| 17 MOJO_CHECK(path); | 17 MOJO_CHECK(path); |
| 18 mojo::files::DirectoryPtr& dir = *root; | 18 mojo::files::DirectoryPtr& dir = *root; |
| 19 | 19 |
| 20 mojo::files::Error error = mojo::files::ERROR_INTERNAL; | 20 mojo::files::Error error = mojo::files::Error::INTERNAL; |
| 21 dir->OpenDirectory(path, nullptr, | 21 dir->OpenDirectory(path, nullptr, |
| 22 mojo::files::kOpenFlagRead | mojo::files::kOpenFlagWrite | | 22 mojo::files::kOpenFlagRead | mojo::files::kOpenFlagWrite | |
| 23 mojo::files::kOpenFlagCreate, | 23 mojo::files::kOpenFlagCreate, |
| 24 Capture(&error)); | 24 Capture(&error)); |
| 25 MOJO_CHECK(dir.WaitForIncomingResponse()); | 25 MOJO_CHECK(dir.WaitForIncomingResponse()); |
| 26 MOJO_CHECK(error == mojo::files::ERROR_OK); | 26 MOJO_CHECK(error == mojo::files::Error::OK); |
| 27 } | 27 } |
| 28 | 28 |
| 29 mojo::files::FilePtr OpenFileAt(mojo::files::DirectoryPtr* root, | 29 mojo::files::FilePtr OpenFileAt(mojo::files::DirectoryPtr* root, |
| 30 const char* path, | 30 const char* path, |
| 31 uint32_t open_flags) { | 31 uint32_t open_flags) { |
| 32 MOJO_CHECK(root); | 32 MOJO_CHECK(root); |
| 33 MOJO_CHECK(path); | 33 MOJO_CHECK(path); |
| 34 mojo::files::DirectoryPtr& dir = *root; | 34 mojo::files::DirectoryPtr& dir = *root; |
| 35 | 35 |
| 36 mojo::files::FilePtr file; | 36 mojo::files::FilePtr file; |
| 37 mojo::files::Error error = mojo::files::ERROR_INTERNAL; | 37 mojo::files::Error error = mojo::files::Error::INTERNAL; |
| 38 dir->OpenFile(path, mojo::GetProxy(&file), open_flags, Capture(&error)); | 38 dir->OpenFile(path, mojo::GetProxy(&file), open_flags, Capture(&error)); |
| 39 MOJO_CHECK(dir.WaitForIncomingResponse()); | 39 MOJO_CHECK(dir.WaitForIncomingResponse()); |
| 40 if (error != mojo::files::ERROR_OK) | 40 if (error != mojo::files::Error::OK) |
| 41 return nullptr; | 41 return nullptr; |
| 42 | 42 |
| 43 return file.Pass(); | 43 return file.Pass(); |
| 44 } | 44 } |
| 45 | 45 |
| 46 void CreateTestFileAt(mojo::files::DirectoryPtr* root, | 46 void CreateTestFileAt(mojo::files::DirectoryPtr* root, |
| 47 const char* path, | 47 const char* path, |
| 48 size_t size) { | 48 size_t size) { |
| 49 mojo::files::FilePtr file = OpenFileAt( | 49 mojo::files::FilePtr file = OpenFileAt( |
| 50 root, path, mojo::files::kOpenFlagWrite | mojo::files::kOpenFlagCreate | | 50 root, path, mojo::files::kOpenFlagWrite | mojo::files::kOpenFlagCreate | |
| 51 mojo::files::kOpenFlagExclusive); | 51 mojo::files::kOpenFlagExclusive); |
| 52 MOJO_CHECK(file); | 52 MOJO_CHECK(file); |
| 53 | 53 |
| 54 if (!size) | 54 if (!size) |
| 55 return; | 55 return; |
| 56 | 56 |
| 57 std::vector<uint8_t> bytes_to_write(size); | 57 std::vector<uint8_t> bytes_to_write(size); |
| 58 for (size_t i = 0; i < size; i++) | 58 for (size_t i = 0; i < size; i++) |
| 59 bytes_to_write[i] = static_cast<uint8_t>(i); | 59 bytes_to_write[i] = static_cast<uint8_t>(i); |
| 60 mojo::files::Error error = mojo::files::ERROR_INTERNAL; | 60 mojo::files::Error error = mojo::files::Error::INTERNAL; |
| 61 uint32_t num_bytes_written = 0; | 61 uint32_t num_bytes_written = 0; |
| 62 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, | 62 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, |
| 63 mojo::files::WHENCE_FROM_CURRENT, | 63 mojo::files::Whence::FROM_CURRENT, |
| 64 Capture(&error, &num_bytes_written)); | 64 Capture(&error, &num_bytes_written)); |
| 65 MOJO_CHECK(file.WaitForIncomingResponse()); | 65 MOJO_CHECK(file.WaitForIncomingResponse()); |
| 66 MOJO_CHECK(error == mojo::files::ERROR_OK); | 66 MOJO_CHECK(error == mojo::files::Error::OK); |
| 67 MOJO_CHECK(num_bytes_written == bytes_to_write.size()); | 67 MOJO_CHECK(num_bytes_written == bytes_to_write.size()); |
| 68 } | 68 } |
| 69 | 69 |
| 70 int64_t GetFileSize(mojo::files::DirectoryPtr* root, const char* path) { | 70 int64_t GetFileSize(mojo::files::DirectoryPtr* root, const char* path) { |
| 71 mojo::files::FilePtr file = | 71 mojo::files::FilePtr file = |
| 72 OpenFileAt(root, path, mojo::files::kOpenFlagRead); | 72 OpenFileAt(root, path, mojo::files::kOpenFlagRead); |
| 73 if (!file) | 73 if (!file) |
| 74 return -1; | 74 return -1; |
| 75 | 75 |
| 76 // Seek to the end to get the file size (we could also stat). | 76 // Seek to the end to get the file size (we could also stat). |
| 77 mojo::files::Error error = mojo::files::ERROR_INTERNAL; | 77 mojo::files::Error error = mojo::files::Error::INTERNAL; |
| 78 int64_t position = -1; | 78 int64_t position = -1; |
| 79 file->Seek(0, mojo::files::WHENCE_FROM_END, Capture(&error, &position)); | 79 file->Seek(0, mojo::files::Whence::FROM_END, Capture(&error, &position)); |
| 80 MOJO_CHECK(file.WaitForIncomingResponse()); | 80 MOJO_CHECK(file.WaitForIncomingResponse()); |
| 81 MOJO_CHECK(error == mojo::files::ERROR_OK); | 81 MOJO_CHECK(error == mojo::files::Error::OK); |
| 82 MOJO_CHECK(position >= 0); | 82 MOJO_CHECK(position >= 0); |
| 83 return position; | 83 return position; |
| 84 } | 84 } |
| 85 | 85 |
| 86 std::string GetFileContents(mojo::files::DirectoryPtr* root, const char* path) { | 86 std::string GetFileContents(mojo::files::DirectoryPtr* root, const char* path) { |
| 87 mojo::files::FilePtr file = | 87 mojo::files::FilePtr file = |
| 88 OpenFileAt(root, path, mojo::files::kOpenFlagRead); | 88 OpenFileAt(root, path, mojo::files::kOpenFlagRead); |
| 89 MOJO_CHECK(file); | 89 MOJO_CHECK(file); |
| 90 | 90 |
| 91 mojo::Array<uint8_t> bytes_read; | 91 mojo::Array<uint8_t> bytes_read; |
| 92 mojo::files::Error error = mojo::files::ERROR_INTERNAL; | 92 mojo::files::Error error = mojo::files::Error::INTERNAL; |
| 93 file->Read(1024 * 1024, 0, mojo::files::WHENCE_FROM_START, | 93 file->Read(1024 * 1024, 0, mojo::files::Whence::FROM_START, |
| 94 Capture(&error, &bytes_read)); | 94 Capture(&error, &bytes_read)); |
| 95 MOJO_CHECK(file.WaitForIncomingResponse()); | 95 MOJO_CHECK(file.WaitForIncomingResponse()); |
| 96 if (!bytes_read.size()) | 96 if (!bytes_read.size()) |
| 97 return std::string(); | 97 return std::string(); |
| 98 return std::string(reinterpret_cast<const char*>(&bytes_read[0]), | 98 return std::string(reinterpret_cast<const char*>(&bytes_read[0]), |
| 99 bytes_read.size()); | 99 bytes_read.size()); |
| 100 } | 100 } |
| 101 | 101 |
| 102 } // namespace test | 102 } // namespace test |
| 103 } // namespace mojio | 103 } // namespace mojio |
| OLD | NEW |