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

Side by Side Diff: native_client_sdk/src/libraries/nacl_mounts_test/mount_html5fs_test.cc

Issue 12194030: Rename mount (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix whitespace Created 7 years, 10 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 /* Copyright (c) 2012 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
6 #include <fcntl.h>
7 #include <gmock/gmock.h>
8 #include <ppapi/c/ppb_file_io.h>
9 #include <ppapi/c/pp_errors.h>
10 #include <ppapi/c/pp_instance.h>
11
12 #include "nacl_mounts/mount_html5fs.h"
13 #include "nacl_mounts/osdirent.h"
14 #include "pepper_interface_mock.h"
15
16 using ::testing::_;
17 using ::testing::DoAll;
18 using ::testing::Return;
19 using ::testing::SetArgPointee;
20 using ::testing::StrEq;
21
22 namespace {
23
24 ACTION_TEMPLATE(CallCallback,
25 HAS_1_TEMPLATE_PARAMS(int, k),
26 AND_1_VALUE_PARAMS(result)) {
27 PP_CompletionCallback callback = std::tr1::get<k>(args);
28 if (callback.func) {
29 (*callback.func)(callback.user_data, result);
30 }
31
32 // Dummy return value.
33 return 0;
34 }
35
36 MATCHER_P(IsEqualToVar, var, "") {
37 if (arg.type != var.type)
38 return false;
39
40 switch (arg.type) {
41 case PP_VARTYPE_BOOL:
42 return arg.value.as_bool == var.value.as_bool;
43
44 case PP_VARTYPE_INT32:
45 return arg.value.as_int == var.value.as_int;
46
47 case PP_VARTYPE_DOUBLE:
48 return arg.value.as_double == var.value.as_double;
49
50 case PP_VARTYPE_STRING:
51 return arg.value.as_id == var.value.as_id;
52
53 case PP_VARTYPE_UNDEFINED:
54 case PP_VARTYPE_NULL:
55 return true;
56
57 case PP_VARTYPE_ARRAY:
58 case PP_VARTYPE_ARRAY_BUFFER:
59 case PP_VARTYPE_DICTIONARY:
60 case PP_VARTYPE_OBJECT:
61 default:
62 // Not supported.
63 return false;
64 }
65 }
66
67
68 class MountHtml5FsMock : public MountHtml5Fs {
69 public:
70 explicit MountHtml5FsMock(StringMap_t map, PepperInterfaceMock* ppapi)
71 : MountHtml5Fs() {
72 Init(1, map, ppapi);
73 }
74
75 ~MountHtml5FsMock() {
76 Destroy();
77 }
78 };
79
80 class MountHtml5FsTest : public ::testing::Test {
81 public:
82 MountHtml5FsTest();
83 ~MountHtml5FsTest();
84 void SetUpFilesystem(PP_FileSystemType, int);
85
86 protected:
87 PepperInterfaceMock* ppapi_;
88
89 static const PP_Instance instance_ = 123;
90 static const PP_Resource filesystem_resource_ = 234;
91 };
92
93 MountHtml5FsTest::MountHtml5FsTest()
94 : ppapi_(NULL) {
95 }
96
97 MountHtml5FsTest::~MountHtml5FsTest() {
98 delete ppapi_;
99 }
100
101 void MountHtml5FsTest::SetUpFilesystem(PP_FileSystemType fstype,
102 int expected_size) {
103 ppapi_ = new PepperInterfaceMock(instance_);
104 FileSystemInterfaceMock* filesystem = ppapi_->GetFileSystemInterface();
105 EXPECT_CALL(*filesystem, Create(instance_, fstype))
106 .Times(1)
107 .WillOnce(Return(filesystem_resource_));
108 EXPECT_CALL(*filesystem, Open(filesystem_resource_, expected_size, _))
109 .WillOnce(CallCallback<2>(int32_t(PP_OK)));
110
111 EXPECT_CALL(*ppapi_, ReleaseResource(filesystem_resource_));
112 }
113
114 class MountHtml5FsNodeTest : public MountHtml5FsTest {
115 public:
116 MountHtml5FsNodeTest();
117 virtual void SetUp();
118 virtual void TearDown();
119
120 protected:
121 MountHtml5FsMock* mnt_;
122 MountNode* node_;
123
124 FileRefInterfaceMock* fileref_;
125 FileIoInterfaceMock* fileio_;
126
127 static const char path_[];
128 static const PP_Resource fileref_resource_ = 235;
129 static const PP_Resource fileio_resource_ = 236;
130 };
131
132 // static
133 const char MountHtml5FsNodeTest::path_[] = "/foo";
134
135 MountHtml5FsNodeTest::MountHtml5FsNodeTest()
136 : mnt_(NULL),
137 node_(NULL),
138 fileref_(NULL),
139 fileio_(NULL) {
140 }
141
142 void MountHtml5FsNodeTest::SetUp() {
143 SetUpFilesystem(PP_FILESYSTEMTYPE_LOCALPERSISTENT, 0);
144 StringMap_t map;
145 mnt_ = new MountHtml5FsMock(map, ppapi_);
146
147 fileref_ = ppapi_->GetFileRefInterface();
148 fileio_ = ppapi_->GetFileIoInterface();
149
150 // Open.
151 EXPECT_CALL(*fileref_, Create(filesystem_resource_, StrEq(&path_[0])))
152 .WillOnce(Return(fileref_resource_));
153 EXPECT_CALL(*fileio_, Create(instance_)).WillOnce(Return(fileio_resource_));
154 int32_t open_flags = PP_FILEOPENFLAG_READ | PP_FILEOPENFLAG_WRITE |
155 PP_FILEOPENFLAG_CREATE;
156 EXPECT_CALL(*fileio_,
157 Open(fileio_resource_, fileref_resource_, open_flags, _))
158 .WillOnce(Return(int32_t(PP_OK)));
159
160 // Close.
161 EXPECT_CALL(*fileio_, Close(fileio_resource_));
162 EXPECT_CALL(*ppapi_, ReleaseResource(fileref_resource_));
163 EXPECT_CALL(*ppapi_, ReleaseResource(fileio_resource_));
164 EXPECT_CALL(*fileio_, Flush(fileio_resource_, _));
165
166 node_ = mnt_->Open(Path(path_), O_CREAT | O_RDWR);
167 ASSERT_NE((MountNode*)NULL, node_);
168 }
169
170 void MountHtml5FsNodeTest::TearDown() {
171 mnt_->ReleaseNode(node_);
172 delete mnt_;
173 }
174
175 } // namespace
176
177
178 TEST_F(MountHtml5FsTest, FilesystemType) {
179 SetUpFilesystem(PP_FILESYSTEMTYPE_LOCALPERSISTENT, 100);
180
181 StringMap_t map;
182 map["type"] = "PERSISTENT";
183 map["expected_size"] = "100";
184 MountHtml5FsMock mnt(map, ppapi_);
185 }
186
187 TEST_F(MountHtml5FsTest, Mkdir) {
188 const char path[] = "/foo";
189 const PP_Resource fileref_resource = 235;
190
191 // These are the default values.
192 SetUpFilesystem(PP_FILESYSTEMTYPE_LOCALPERSISTENT, 0);
193
194 FileRefInterfaceMock* fileref = ppapi_->GetFileRefInterface();
195
196 EXPECT_CALL(*fileref, Create(filesystem_resource_, StrEq(&path[0])))
197 .WillOnce(Return(fileref_resource));
198 EXPECT_CALL(*fileref, MakeDirectory(fileref_resource, _, _))
199 .WillOnce(Return(int32_t(PP_OK)));
200 EXPECT_CALL(*ppapi_, ReleaseResource(fileref_resource));
201
202 StringMap_t map;
203 MountHtml5FsMock mnt(map, ppapi_);
204
205 const int permissions = 0; // unused.
206 int32_t result = mnt.Mkdir(Path(path), permissions);
207 ASSERT_EQ(0, result);
208 }
209
210 TEST_F(MountHtml5FsTest, Remove) {
211 const char path[] = "/foo";
212 const PP_Resource fileref_resource = 235;
213
214 // These are the default values.
215 SetUpFilesystem(PP_FILESYSTEMTYPE_LOCALPERSISTENT, 0);
216
217 FileRefInterfaceMock* fileref = ppapi_->GetFileRefInterface();
218
219 EXPECT_CALL(*fileref, Create(filesystem_resource_, StrEq(&path[0])))
220 .WillOnce(Return(fileref_resource));
221 EXPECT_CALL(*fileref, Delete(fileref_resource, _))
222 .WillOnce(Return(int32_t(PP_OK)));
223 EXPECT_CALL(*ppapi_, ReleaseResource(fileref_resource));
224
225 StringMap_t map;
226 MountHtml5FsMock mnt(map, ppapi_);
227
228 int32_t result = mnt.Remove(Path(path));
229 ASSERT_EQ(0, result);
230 }
231
232 TEST_F(MountHtml5FsNodeTest, OpenAndClose) {
233 }
234
235 TEST_F(MountHtml5FsNodeTest, Write) {
236 const int offset = 10;
237 const int count = 20;
238 const char buffer[30] = {0};
239
240 EXPECT_CALL(*fileio_, Write(fileio_resource_, offset, &buffer[0], count, _))
241 .WillOnce(Return(count));
242
243 int result = node_->Write(offset, &buffer, count);
244 EXPECT_EQ(count, result);
245 }
246
247 TEST_F(MountHtml5FsNodeTest, Read) {
248 const int offset = 10;
249 const int count = 20;
250 char buffer[30] = {0};
251
252 EXPECT_CALL(*fileio_, Read(fileio_resource_, offset, &buffer[0], count, _))
253 .WillOnce(Return(count));
254
255 int result = node_->Read(offset, &buffer, count);
256 EXPECT_EQ(count, result);
257 }
258
259 TEST_F(MountHtml5FsNodeTest, GetStat) {
260 const int size = 123;
261 const int creation_time = 1000;
262 const int access_time = 2000;
263 const int modified_time = 3000;
264
265 PP_FileInfo info;
266 info.size = size;
267 info.type = PP_FILETYPE_REGULAR;
268 info.system_type = PP_FILESYSTEMTYPE_LOCALPERSISTENT;
269 info.creation_time = creation_time;
270 info.last_access_time = access_time;
271 info.last_modified_time = modified_time;
272
273 EXPECT_CALL(*fileio_, Query(fileio_resource_, _, _))
274 .WillOnce(DoAll(SetArgPointee<1>(info),
275 Return(int32_t(PP_OK))));
276
277 struct stat statbuf;
278 int result = node_->GetStat(&statbuf);
279
280 EXPECT_EQ(0, result);
281 EXPECT_EQ(S_IFREG | S_IWRITE | S_IREAD, statbuf.st_mode);
282 EXPECT_EQ(size, statbuf.st_size);
283 EXPECT_EQ(access_time, statbuf.st_atime);
284 EXPECT_EQ(modified_time, statbuf.st_mtime);
285 EXPECT_EQ(creation_time, statbuf.st_ctime);
286 }
287
288 TEST_F(MountHtml5FsNodeTest, Truncate) {
289 const int size = 123;
290 EXPECT_CALL(*fileio_, SetLength(fileio_resource_, size, _))
291 .WillOnce(Return(int32_t(PP_OK)));
292
293 int result = node_->Truncate(size);
294 EXPECT_EQ(0, result);
295 }
296
297 TEST_F(MountHtml5FsNodeTest, GetDents) {
298 const int dir_reader_resource = 237;
299 const int fileref_resource_1 = 238;
300 const int fileref_resource_2 = 239;
301
302 PP_DirectoryEntry_Dev directory_entry_1 = {
303 fileref_resource_1, PP_FILETYPE_REGULAR
304 };
305 PP_DirectoryEntry_Dev directory_entry_2 = {
306 fileref_resource_2, PP_FILETYPE_REGULAR
307 };
308 PP_DirectoryEntry_Dev directory_entry_end = {
309 0, PP_FILETYPE_REGULAR,
310 };
311
312 const int fileref_name_id_1 = 240;
313 const char fileref_name_cstr_1[] = "bar";
314 PP_Var fileref_name_1;
315 fileref_name_1.type = PP_VARTYPE_STRING;
316 fileref_name_1.value.as_id = fileref_name_id_1;
317
318 const int fileref_name_id_2 = 241;
319 const char fileref_name_cstr_2[] = "quux";
320 PP_Var fileref_name_2;
321 fileref_name_2.type = PP_VARTYPE_STRING;
322 fileref_name_2.value.as_id = fileref_name_id_2;
323
324 VarInterfaceMock* var = ppapi_->GetVarInterface();
325 DirectoryReaderInterfaceMock* dir_reader =
326 ppapi_->GetDirectoryReaderInterface();
327
328 EXPECT_CALL(*dir_reader, Create(fileref_resource_))
329 .WillOnce(Return(dir_reader_resource));
330 EXPECT_CALL(*dir_reader, GetNextEntry(dir_reader_resource, _, _))
331 .WillOnce(DoAll(SetArgPointee<1>(directory_entry_1),
332 Return(int32_t(PP_OK))))
333 .WillOnce(DoAll(SetArgPointee<1>(directory_entry_2),
334 Return(int32_t(PP_OK))))
335 .WillOnce(DoAll(SetArgPointee<1>(directory_entry_end),
336 Return(int32_t(PP_OK))));
337
338 EXPECT_CALL(*fileref_, GetName(fileref_resource_1))
339 .WillOnce(Return(fileref_name_1));
340 EXPECT_CALL(*fileref_, GetName(fileref_resource_2))
341 .WillOnce(Return(fileref_name_2));
342
343 EXPECT_CALL(*var, VarToUtf8(IsEqualToVar(fileref_name_1), _))
344 .WillOnce(Return(fileref_name_cstr_1));
345 EXPECT_CALL(*var, VarToUtf8(IsEqualToVar(fileref_name_2), _))
346 .WillOnce(Return(fileref_name_cstr_2));
347
348 EXPECT_CALL(*ppapi_, ReleaseResource(dir_reader_resource));
349
350 struct dirent dirents[2];
351 memset(&dirents[0], 0, sizeof(dirents));
352 int result = node_->GetDents(0, &dirents[0], sizeof(dirent) * 2);
353
354 EXPECT_EQ(0, result);
355 EXPECT_STREQ(&fileref_name_cstr_1[0], &dirents[0].d_name[0]);
356 EXPECT_STREQ(&fileref_name_cstr_2[0], &dirents[1].d_name[0]);
357 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698