Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: mojo/file_utils/tests/file_util_unittest.cc

Issue 1915193002: Simplify the organization of //mojo_file_utils. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/file_utils/tests/file_util_test_base.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 <stdint.h>
6 #include <vector>
7
8 #include "mojo/file_utils/file_util.h"
9 #include "mojo/file_utils/tests/file_util_test_base.h"
10 #include "mojo/public/cpp/application/application_impl.h"
11 #include "mojo/public/cpp/application/application_test_base.h"
12 #include "mojo/public/cpp/utility/run_loop.h"
13 #include "mojo/services/files/interfaces/directory.mojom.h"
14 #include "mojo/services/files/interfaces/file.mojom.h"
15 #include "mojo/services/files/interfaces/files.mojom.h"
16
17 namespace file_utils {
18 namespace test {
19 namespace {
20
21 using FileUtilTest = file_utils::test::FileUtilTestBase;
22
23 // TODO(smklein): Stuff copied from mojo/services/files/c/lib/template_util.h
24 typedef char YesType;
25
26 struct NoType {
27 YesType dummy[2];
28 };
29
30 template <typename T>
31 struct IsMoveOnlyType {
32 template <typename U>
33 static YesType Test(const typename U::MoveOnlyTypeForCPP03*);
34
35 template <typename U>
36 static NoType Test(...);
37
38 static const bool value =
39 sizeof(Test<T>(0)) == sizeof(YesType) && !std::is_const<T>::value;
40 };
41
42 template <typename T>
43 typename std::enable_if<!IsMoveOnlyType<T>::value, T>::type& Forward(T& t) {
44 return t;
45 }
46
47 template <typename T>
48 typename std::enable_if<IsMoveOnlyType<T>::value, T>::type Forward(T& t) {
49 return t.Pass();
50 }
51
52 template <typename T1>
53 mojo::Callback<void(T1)> Capture(T1* t1) {
54 return [t1](T1 got_t1) { *t1 = Forward(got_t1); };
55 }
56
57 template <typename T1, typename T2>
58 mojo::Callback<void(T1, T2)> Capture(T1* t1, T2* t2) {
59 return [t1, t2](T1 got_t1, T2 got_t2) {
60 *t1 = Forward(got_t1);
61 *t2 = Forward(got_t2);
62 };
63 }
64 // TODO(smklein): (End of stuff copied from template_util.h)
65
66 // Given a FilePtr |file|, write the string |input| and verify
67 // the output matches the |input|'s size.
68 void WriteFileHelper(mojo::files::FilePtr* file, const std::string& input) {
69 std::vector<uint8_t> bytes_to_write;
70 for (size_t i = 0; i < input.size(); i++)
71 bytes_to_write.push_back(static_cast<uint8_t>(input[i]));
72 mojo::files::Error error = mojo::files::Error::INTERNAL;
73 uint32_t num_bytes_written = 0;
74 (*file)->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0,
75 mojo::files::Whence::FROM_CURRENT,
76 Capture(&error, &num_bytes_written));
77 ASSERT_TRUE(file->WaitForIncomingResponse());
78 EXPECT_EQ(mojo::files::Error::OK, error);
79 EXPECT_EQ(bytes_to_write.size(), num_bytes_written);
80 }
81
82 // Given a FilePtr |file|, seek to offset |offset| using whence
83 // FROM_CURRENT. Verifies the post-seek position is |expected_position|.
84 void SeekFileHelper(mojo::files::FilePtr* file,
85 int64_t offset,
86 int64_t expected_position) {
87 int64_t position = -1;
88 mojo::files::Error error = mojo::files::Error::INTERNAL;
89 (*file)->Seek(offset, mojo::files::Whence::FROM_CURRENT,
90 Capture(&error, &position));
91 ASSERT_TRUE(file->WaitForIncomingResponse());
92 EXPECT_EQ(mojo::files::Error::OK, error);
93 EXPECT_EQ(expected_position, position);
94 }
95
96 // Given a FilePtr |file|, read from the file at offset |offset| (using
97 // Whence::FROM_CURRENT), and verify the result matches the string
98 // |expected_output|.
99 void ReadFileHelper(mojo::files::FilePtr* file,
100 int64_t offset,
101 const std::string& expected_output) {
102 mojo::Array<uint8_t> bytes_read;
103 mojo::files::Error error = mojo::files::Error::INTERNAL;
104 uint32_t num_bytes_to_read = expected_output.size();
105 (*file)->Read(num_bytes_to_read, offset, mojo::files::Whence::FROM_CURRENT,
106 Capture(&error, &bytes_read));
107 ASSERT_TRUE(file->WaitForIncomingResponse());
108 EXPECT_EQ(mojo::files::Error::OK, error);
109 EXPECT_EQ(num_bytes_to_read, bytes_read.size());
110 for (size_t i = 0; i < expected_output.size(); i++)
111 EXPECT_EQ(static_cast<uint8_t>(expected_output[i]), bytes_read[i]);
112 }
113
114 // Open the file named |path_name| in |directory| and verify that no error
115 // occurs. Returns the newly opened file.
116 mojo::files::FilePtr OpenFileHelper(mojo::files::DirectoryPtr* directory,
117 const std::string& path_name) {
118 mojo::files::FilePtr temp_file;
119 mojo::files::Error error = mojo::files::Error::INTERNAL;
120 (*directory)
121 ->OpenFile(path_name, GetProxy(&temp_file),
122 mojo::files::kOpenFlagWrite | mojo::files::kOpenFlagRead,
123 Capture(&error));
124 EXPECT_EQ(true, directory->WaitForIncomingResponse());
125 EXPECT_EQ(mojo::files::Error::OK, error);
126 return temp_file;
127 }
128
129 // Close the FilePtr |file| and verify that no error occurs.
130 void CloseFileHelper(mojo::files::FilePtr* file) {
131 mojo::files::Error error = mojo::files::Error::INTERNAL;
132 (*file)->Close(Capture(&error));
133 ASSERT_TRUE(file->WaitForIncomingResponse());
134 EXPECT_EQ(mojo::files::Error::OK, error);
135 }
136
137 TEST_F(FileUtilTest, BasicCreateTemporaryFile) {
138 mojo::files::Error error = mojo::files::Error::INTERNAL;
139 mojo::files::DirectoryPtr directory;
140 files()->OpenFileSystem(nullptr, mojo::GetProxy(&directory), Capture(&error));
141
142 ASSERT_TRUE(files().WaitForIncomingResponse());
143 EXPECT_EQ(mojo::files::Error::OK, error);
144
145 std::string filename1, filename2, filename3;
146 mojo::files::FilePtr file1, file2, file3;
147 file1 = CreateTemporaryFileInDir(&directory, &filename1);
148 ASSERT_TRUE(file1);
149 file2 = CreateTemporaryFileInDir(&directory, &filename2);
150 ASSERT_TRUE(file2);
151 file3 = CreateTemporaryFileInDir(&directory, &filename3);
152 ASSERT_TRUE(file3);
153
154 // The temp filenames should not be equal.
155 EXPECT_NE(filename1, filename2);
156 EXPECT_NE(filename1, filename3);
157 EXPECT_NE(filename2, filename3);
158
159 // Test that 'Write' can be called on the temp files.
160 WriteFileHelper(&file1, "abcde");
161 WriteFileHelper(&file2, "fghij");
162 WriteFileHelper(&file3, "lmnop");
163
164 // Test that 'Seek' can be called on the temp files.
165 SeekFileHelper(&file1, -5, 0);
166 SeekFileHelper(&file2, -4, 1);
167 SeekFileHelper(&file3, -3, 2);
168
169 // Test that 'Read' can be called on the temp files.
170 ReadFileHelper(&file1, 0, "abcde");
171 ReadFileHelper(&file2, 0, "ghij");
172 ReadFileHelper(&file3, 0, "nop");
173
174 // Test that the files can be closed.
175 CloseFileHelper(&file1);
176 CloseFileHelper(&file2);
177 CloseFileHelper(&file3);
178
179 // Test that the files can be reopened after closing.
180 file1 = OpenFileHelper(&directory, filename1);
181 file2 = OpenFileHelper(&directory, filename2);
182 file3 = OpenFileHelper(&directory, filename3);
183
184 // Verify the contents of the reopened files.
185 ReadFileHelper(&file1, 0, "abcde");
186 ReadFileHelper(&file2, 0, "fghij");
187 ReadFileHelper(&file3, 0, "lmnop");
188
189 // Test that the files can be closed once more.
190 CloseFileHelper(&file1);
191 CloseFileHelper(&file2);
192 CloseFileHelper(&file3);
193 }
194
195 } // namespace
196 } // namespace test
197 } // namespace file_utils
OLDNEW
« no previous file with comments | « mojo/file_utils/tests/file_util_test_base.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698