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