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

Side by Side Diff: native_client_sdk/src/libraries/nacl_io_test/mount_http_test.cc

Issue 16232016: [NaCl SDK] nacl_io: big refactor to return error value (errno). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: merge master, fix windows Created 7 years, 6 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
1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 5
6 #include <fcntl.h> 6 #include <fcntl.h>
7 #include <gmock/gmock.h> 7 #include <gmock/gmock.h>
8 #include <ppapi/c/ppb_file_io.h> 8 #include <ppapi/c/ppb_file_io.h>
9 #include <ppapi/c/pp_errors.h> 9 #include <ppapi/c/pp_errors.h>
10 #include <ppapi/c/pp_instance.h> 10 #include <ppapi/c/pp_instance.h>
(...skipping 11 matching lines...) Expand all
22 using ::testing::DoAll; 22 using ::testing::DoAll;
23 using ::testing::Mock; 23 using ::testing::Mock;
24 using ::testing::Return; 24 using ::testing::Return;
25 using ::testing::SetArgPointee; 25 using ::testing::SetArgPointee;
26 using ::testing::StrEq; 26 using ::testing::StrEq;
27 27
28 28
29 class MountHttpMock : public MountHttp { 29 class MountHttpMock : public MountHttp {
30 public: 30 public:
31 MountHttpMock(StringMap_t map, PepperInterfaceMock* ppapi) { 31 MountHttpMock(StringMap_t map, PepperInterfaceMock* ppapi) {
32 EXPECT_TRUE(Init(1, map, ppapi)); 32 EXPECT_EQ(0, Init(1, map, ppapi));
33 } 33 }
34 34
35 ~MountHttpMock() { 35 ~MountHttpMock() {
36 Destroy(); 36 Destroy();
37 } 37 }
38 38
39 NodeMap_t& GetMap() { return node_cache_; } 39 NodeMap_t& GetMap() { return node_cache_; }
40 40
41 using MountHttp::ParseManifest; 41 using MountHttp::ParseManifest;
42 using MountHttp::FindOrCreateDir; 42 using MountHttp::FindOrCreateDir;
(...skipping 21 matching lines...) Expand all
64 } 64 }
65 65
66 66
67 TEST_F(MountHttpTest, MountEmpty) { 67 TEST_F(MountHttpTest, MountEmpty) {
68 StringMap_t args; 68 StringMap_t args;
69 mnt_ = new MountHttpMock(args, &ppapi_); 69 mnt_ = new MountHttpMock(args, &ppapi_);
70 } 70 }
71 71
72 TEST_F(MountHttpTest, ParseManifest) { 72 TEST_F(MountHttpTest, ParseManifest) {
73 StringMap_t args; 73 StringMap_t args;
74 size_t result_size = 0;
75
74 mnt_ = new MountHttpMock(args, &ppapi_); 76 mnt_ = new MountHttpMock(args, &ppapi_);
75 77
76 char manifest[] = "-r-- 123 /mydir/foo\n-rw- 234 /thatdir/bar\n"; 78 char manifest[] = "-r-- 123 /mydir/foo\n-rw- 234 /thatdir/bar\n";
77 EXPECT_TRUE(mnt_->ParseManifest(manifest)); 79 EXPECT_EQ(0, mnt_->ParseManifest(manifest));
78 80
79 MountNodeDir* root = mnt_->FindOrCreateDir(Path("/")); 81 MountNodeDir* root = NULL;
82 EXPECT_EQ(0, mnt_->FindOrCreateDir(Path("/"), &root));
83 ASSERT_NE((MountNode*)NULL, root);
80 EXPECT_EQ(2, root->ChildCount()); 84 EXPECT_EQ(2, root->ChildCount());
81 85
82 MountNodeDir* dir = mnt_->FindOrCreateDir(Path("/mydir")); 86 MountNodeDir* dir = NULL;
87 EXPECT_EQ(0, mnt_->FindOrCreateDir(Path("/mydir"), &dir));
88 ASSERT_NE((MountNode*)NULL, dir);
83 EXPECT_EQ(1, dir->ChildCount()); 89 EXPECT_EQ(1, dir->ChildCount());
84 90
85 MountNode* node = mnt_->GetMap()["/mydir/foo"]; 91 MountNode* node = mnt_->GetMap()["/mydir/foo"];
86 EXPECT_TRUE(node); 92 EXPECT_NE((MountNode*)NULL, node);
87 EXPECT_EQ(123, node->GetSize()); 93 EXPECT_EQ(0, node->GetSize(&result_size));
94 EXPECT_EQ(123, result_size);
88 95
89 // Since these files are cached thanks to the manifest, we can open them 96 // Since these files are cached thanks to the manifest, we can open them
90 // without accessing the PPAPI URL API. 97 // without accessing the PPAPI URL API.
91 MountNode* foo = mnt_->Open(Path("/mydir/foo"), O_RDONLY); 98 MountNode* foo = NULL;
92 MountNode* bar = mnt_->Open(Path("/thatdir/bar"), O_RDWR); 99 EXPECT_EQ(0, mnt_->Open(Path("/mydir/foo"), O_RDONLY, &foo));
100 MountNode* bar = NULL;
101 EXPECT_EQ(0, mnt_->Open(Path("/thatdir/bar"), O_RDWR, &bar));
93 102
94 struct stat sfoo; 103 struct stat sfoo;
95 struct stat sbar; 104 struct stat sbar;
96 105
97 EXPECT_FALSE(foo->GetStat(&sfoo)); 106 EXPECT_FALSE(foo->GetStat(&sfoo));
98 EXPECT_FALSE(bar->GetStat(&sbar)); 107 EXPECT_FALSE(bar->GetStat(&sbar));
99 108
100 EXPECT_EQ(123, sfoo.st_size); 109 EXPECT_EQ(123, sfoo.st_size);
101 EXPECT_EQ(S_IFREG | S_IREAD, sfoo.st_mode); 110 EXPECT_EQ(S_IFREG | S_IREAD, sfoo.st_mode);
102 111
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 } 250 }
242 251
243 void MountHttpNodeTest::SetResponseBody(const char* body) { 252 void MountHttpNodeTest::SetResponseBody(const char* body) {
244 response_body_offset_ = 0; 253 response_body_offset_ = 0;
245 EXPECT_CALL(*loader_, ReadResponseBody(loader_resource_, _, _, _)) 254 EXPECT_CALL(*loader_, ReadResponseBody(loader_resource_, _, _, _))
246 .WillRepeatedly(ReadResponseBodyAction( 255 .WillRepeatedly(ReadResponseBodyAction(
247 &response_body_offset_, body, strlen(body))); 256 &response_body_offset_, body, strlen(body)));
248 } 257 }
249 258
250 void MountHttpNodeTest::OpenNode() { 259 void MountHttpNodeTest::OpenNode() {
251 node_ = mnt_->Open(Path(path_), O_RDONLY); 260 ASSERT_EQ(0, mnt_->Open(Path(path_), O_RDONLY, &node_));
252 ASSERT_NE((MountNode*)NULL, node_); 261 ASSERT_NE((MountNode*)NULL, node_);
253 } 262 }
254 263
255 void MountHttpNodeTest::ResetMocks() { 264 void MountHttpNodeTest::ResetMocks() {
256 Mock::VerifyAndClearExpectations(&ppapi_); 265 Mock::VerifyAndClearExpectations(&ppapi_);
257 Mock::VerifyAndClearExpectations(loader_); 266 Mock::VerifyAndClearExpectations(loader_);
258 Mock::VerifyAndClearExpectations(request_); 267 Mock::VerifyAndClearExpectations(request_);
259 Mock::VerifyAndClearExpectations(response_); 268 Mock::VerifyAndClearExpectations(response_);
260 Mock::VerifyAndClearExpectations(var_); 269 Mock::VerifyAndClearExpectations(var_);
261 } 270 }
262 271
263 void MountHttpNodeTest::TearDown() { 272 void MountHttpNodeTest::TearDown() {
264 if (node_) 273 if (node_)
265 mnt_->ReleaseNode(node_); 274 mnt_->ReleaseNode(node_);
266 delete mnt_; 275 delete mnt_;
267 } 276 }
268 277
269 TEST_F(MountHttpNodeTest, OpenAndClose) { 278 TEST_F(MountHttpNodeTest, OpenAndCloseNoCache) {
279 StringMap_t smap;
280 smap["cache_content"] = "false";
270 SetMountArgs(StringMap_t()); 281 SetMountArgs(StringMap_t());
271 ExpectOpen("HEAD"); 282 ExpectOpen("HEAD");
272 ExpectHeaders(""); 283 ExpectHeaders("");
273 SetResponse(200, ""); 284 SetResponse(200, "");
274 OpenNode(); 285 OpenNode();
275 } 286 }
276 287
277 TEST_F(MountHttpNodeTest, ReadCached) { 288 TEST_F(MountHttpNodeTest, ReadCached) {
289 size_t result_size = 0;
290 int result_bytes = 0;
291
278 SetMountArgs(StringMap_t()); 292 SetMountArgs(StringMap_t());
279 ExpectOpen("HEAD"); 293 ExpectOpen("HEAD");
280 ExpectHeaders(""); 294 ExpectHeaders("");
281 SetResponse(200, "Content-Length: 42\n"); 295 SetResponse(200, "Content-Length: 42\n");
282 OpenNode(); 296 OpenNode();
283 ResetMocks(); 297 ResetMocks();
284 298
285 EXPECT_EQ(42, node_->GetSize()); 299 EXPECT_EQ(0, node_->GetSize(&result_size));
300 EXPECT_EQ(42, result_size);
286 301
287 char buf[10]; 302 char buf[10];
288 memset(&buf[0], 0, sizeof(buf)); 303 memset(&buf[0], 0, sizeof(buf));
289 304
290 ExpectOpen("GET"); 305 ExpectOpen("GET");
291 ExpectHeaders(""); 306 ExpectHeaders("");
292 SetResponse(200, "Content-Length: 42\n"); 307 SetResponse(200, "Content-Length: 42\n");
293 SetResponseBody("Here is some response text. And some more."); 308 SetResponseBody("Here is some response text. And some more.");
294 node_->Read(0, buf, sizeof(buf) - 1); 309 EXPECT_EQ(0, node_->Read(0, buf, sizeof(buf) - 1, &result_bytes));
295 EXPECT_STREQ("Here is s", &buf[0]); 310 EXPECT_STREQ("Here is s", &buf[0]);
296 ResetMocks(); 311 ResetMocks();
297 312
298 // Further reads should be cached. 313 // Further reads should be cached.
299 node_->Read(0, buf, sizeof(buf) - 1); 314 EXPECT_EQ(0, node_->Read(0, buf, sizeof(buf) - 1, &result_bytes));
300 EXPECT_STREQ("Here is s", &buf[0]); 315 EXPECT_STREQ("Here is s", &buf[0]);
301 node_->Read(10, buf, sizeof(buf) - 1); 316 EXPECT_EQ(0, node_->Read(10, buf, sizeof(buf) - 1, &result_bytes));
302 EXPECT_STREQ("me respon", &buf[0]); 317 EXPECT_STREQ("me respon", &buf[0]);
303 318
304 EXPECT_EQ(42, node_->GetSize()); 319 EXPECT_EQ(0, node_->GetSize(&result_size));
320 EXPECT_EQ(42, result_size);
305 } 321 }
306 322
307 TEST_F(MountHttpNodeTest, ReadCachedNoContentLength) { 323 TEST_F(MountHttpNodeTest, ReadCachedNoContentLength) {
324 size_t result_size = 0;
325 int result_bytes = 0;
326
308 SetMountArgs(StringMap_t()); 327 SetMountArgs(StringMap_t());
309 ExpectOpen("HEAD"); 328 ExpectOpen("HEAD");
310 ExpectHeaders(""); 329 ExpectHeaders("");
311 SetResponse(200, ""); 330 SetResponse(200, "");
312 OpenNode(); 331 OpenNode();
313 ResetMocks(); 332 ResetMocks();
314 333
315 ExpectOpen("GET"); 334 ExpectOpen("GET");
316 ExpectHeaders(""); 335 ExpectHeaders("");
317 SetResponse(200, ""); // No Content-Length response here. 336 SetResponse(200, ""); // No Content-Length response here.
318 SetResponseBody("Here is some response text. And some more."); 337 SetResponseBody("Here is some response text. And some more.");
319 338
320 // GetSize will Read() because it didn't get the content length from the HEAD 339 // GetSize will Read() because it didn't get the content length from the HEAD
321 // request. 340 // request.
322 EXPECT_EQ(42, node_->GetSize()); 341 EXPECT_EQ(0, node_->GetSize(&result_size));
342 EXPECT_EQ(42, result_size);
323 343
324 char buf[10]; 344 char buf[10];
325 memset(&buf[0], 0, sizeof(buf)); 345 memset(&buf[0], 0, sizeof(buf));
326 346
327 node_->Read(0, buf, sizeof(buf) - 1); 347 EXPECT_EQ(0, node_->Read(0, buf, sizeof(buf) - 1, &result_bytes));
328 EXPECT_STREQ("Here is s", &buf[0]); 348 EXPECT_STREQ("Here is s", &buf[0]);
329 ResetMocks(); 349 ResetMocks();
330 350
331 // Further reads should be cached. 351 // Further reads should be cached.
332 node_->Read(0, buf, sizeof(buf) - 1); 352 EXPECT_EQ(0, node_->Read(0, buf, sizeof(buf) - 1, &result_bytes));
333 EXPECT_STREQ("Here is s", &buf[0]); 353 EXPECT_STREQ("Here is s", &buf[0]);
334 node_->Read(10, buf, sizeof(buf) - 1); 354 EXPECT_EQ(0, node_->Read(10, buf, sizeof(buf) - 1, &result_bytes));
335 EXPECT_STREQ("me respon", &buf[0]); 355 EXPECT_STREQ("me respon", &buf[0]);
336 356
337 EXPECT_EQ(42, node_->GetSize()); 357 EXPECT_EQ(0, node_->GetSize(&result_size));
358 EXPECT_EQ(42, result_size);
338 } 359 }
339 360
340 TEST_F(MountHttpNodeTest, ReadCachedUnderrun) { 361 TEST_F(MountHttpNodeTest, ReadCachedUnderrun) {
362 size_t result_size = 0;
363 int result_bytes = 0;
364
341 SetMountArgs(StringMap_t()); 365 SetMountArgs(StringMap_t());
342 ExpectOpen("HEAD"); 366 ExpectOpen("HEAD");
343 ExpectHeaders(""); 367 ExpectHeaders("");
344 SetResponse(200, "Content-Length: 100\n"); 368 SetResponse(200, "Content-Length: 100\n");
345 OpenNode(); 369 OpenNode();
346 ResetMocks(); 370 ResetMocks();
347 371
348 EXPECT_EQ(100, node_->GetSize()); 372 EXPECT_EQ(0, node_->GetSize(&result_size));
373 EXPECT_EQ(100, result_size);
349 374
350 char buf[10]; 375 char buf[10];
351 memset(&buf[0], 0, sizeof(buf)); 376 memset(&buf[0], 0, sizeof(buf));
352 377
353 ExpectOpen("GET"); 378 ExpectOpen("GET");
354 ExpectHeaders(""); 379 ExpectHeaders("");
355 SetResponse(200, "Content-Length: 100\n"); 380 SetResponse(200, "Content-Length: 100\n");
356 SetResponseBody("abcdefghijklmnopqrstuvwxyz"); 381 SetResponseBody("abcdefghijklmnopqrstuvwxyz");
357 node_->Read(0, buf, sizeof(buf) - 1); 382 EXPECT_EQ(0, node_->Read(0, buf, sizeof(buf) - 1, &result_bytes));
383 EXPECT_EQ(sizeof(buf) - 1, result_bytes);
358 EXPECT_STREQ("abcdefghi", &buf[0]); 384 EXPECT_STREQ("abcdefghi", &buf[0]);
359 ResetMocks(); 385 ResetMocks();
360 386
361 EXPECT_EQ(26, node_->GetSize()); 387 EXPECT_EQ(0, node_->GetSize(&result_size));
388 EXPECT_EQ(26, result_size);
362 } 389 }
363 390
364 TEST_F(MountHttpNodeTest, ReadCachedOverrun) { 391 TEST_F(MountHttpNodeTest, ReadCachedOverrun) {
392 size_t result_size = 0;
393 int result_bytes = 0;
394
365 SetMountArgs(StringMap_t()); 395 SetMountArgs(StringMap_t());
366 ExpectOpen("HEAD"); 396 ExpectOpen("HEAD");
367 ExpectHeaders(""); 397 ExpectHeaders("");
368 SetResponse(200, "Content-Length: 15\n"); 398 SetResponse(200, "Content-Length: 15\n");
369 OpenNode(); 399 OpenNode();
370 ResetMocks(); 400 ResetMocks();
371 401
372 EXPECT_EQ(15, node_->GetSize()); 402 EXPECT_EQ(0, node_->GetSize(&result_size));
403 EXPECT_EQ(15, result_size);
373 404
374 char buf[10]; 405 char buf[10];
375 memset(&buf[0], 0, sizeof(buf)); 406 memset(&buf[0], 0, sizeof(buf));
376 407
377 ExpectOpen("GET"); 408 ExpectOpen("GET");
378 ExpectHeaders(""); 409 ExpectHeaders("");
379 SetResponse(200, "Content-Length: 15\n"); 410 SetResponse(200, "Content-Length: 15\n");
380 SetResponseBody("01234567890123456789"); 411 SetResponseBody("01234567890123456789");
381 node_->Read(10, buf, sizeof(buf) - 1); 412 EXPECT_EQ(0, node_->Read(10, buf, sizeof(buf) - 1, &result_bytes));
413 EXPECT_EQ(5, result_bytes);
382 EXPECT_STREQ("01234", &buf[0]); 414 EXPECT_STREQ("01234", &buf[0]);
383 ResetMocks(); 415 ResetMocks();
384 416
385 EXPECT_EQ(15, node_->GetSize()); 417 EXPECT_EQ(0, node_->GetSize(&result_size));
418 EXPECT_EQ(15, result_size);
386 } 419 }
387 420
388 TEST_F(MountHttpNodeTest, ReadPartial) { 421 TEST_F(MountHttpNodeTest, ReadPartial) {
422 int result_bytes = 0;
423
389 StringMap_t args; 424 StringMap_t args;
390 args["cache_content"] = "false"; 425 args["cache_content"] = "false";
391 SetMountArgs(args); 426 SetMountArgs(args);
392 ExpectOpen("HEAD"); 427 ExpectOpen("HEAD");
393 ExpectHeaders(""); 428 ExpectHeaders("");
394 SetResponse(200, ""); 429 SetResponse(200, "");
395 OpenNode(); 430 OpenNode();
396 ResetMocks(); 431 ResetMocks();
397 432
398 char buf[10]; 433 char buf[10];
399 memset(&buf[0], 0, sizeof(buf)); 434 memset(&buf[0], 0, sizeof(buf));
400 435
401 ExpectOpen("GET"); 436 ExpectOpen("GET");
402 ExpectHeaders("Range: bytes=0-8\n"); 437 ExpectHeaders("Range: bytes=0-8\n");
403 SetResponse(206, "Content-Length: 9\nContent-Range: bytes=0-8\n"); 438 SetResponse(206, "Content-Length: 9\nContent-Range: bytes=0-8\n");
404 SetResponseBody("012345678"); 439 SetResponseBody("012345678");
405 node_->Read(0, buf, sizeof(buf) - 1); 440 EXPECT_EQ(0, node_->Read(0, buf, sizeof(buf) - 1, &result_bytes));
441 EXPECT_EQ(sizeof(buf) - 1, result_bytes);
406 EXPECT_STREQ("012345678", &buf[0]); 442 EXPECT_STREQ("012345678", &buf[0]);
407 ResetMocks(); 443 ResetMocks();
408 444
409 // Another read is another request. 445 // Another read is another request.
410 ExpectOpen("GET"); 446 ExpectOpen("GET");
411 ExpectHeaders("Range: bytes=10-18\n"); 447 ExpectHeaders("Range: bytes=10-18\n");
412 SetResponse(206, "Content-Length: 9\nContent-Range: bytes=10-18\n"); 448 SetResponse(206, "Content-Length: 9\nContent-Range: bytes=10-18\n");
413 SetResponseBody("abcdefghi"); 449 SetResponseBody("abcdefghi");
414 node_->Read(10, buf, sizeof(buf) - 1); 450 EXPECT_EQ(0, node_->Read(10, buf, sizeof(buf) - 1, &result_bytes));
451 EXPECT_EQ(sizeof(buf) - 1, result_bytes);
415 EXPECT_STREQ("abcdefghi", &buf[0]); 452 EXPECT_STREQ("abcdefghi", &buf[0]);
416 } 453 }
417 454
418 TEST_F(MountHttpNodeTest, ReadPartialNoServerSupport) { 455 TEST_F(MountHttpNodeTest, ReadPartialNoServerSupport) {
456 int result_bytes = 0;
457
419 StringMap_t args; 458 StringMap_t args;
420 args["cache_content"] = "false"; 459 args["cache_content"] = "false";
421 SetMountArgs(args); 460 SetMountArgs(args);
422 ExpectOpen("HEAD"); 461 ExpectOpen("HEAD");
423 ExpectHeaders(""); 462 ExpectHeaders("");
424 SetResponse(200, ""); 463 SetResponse(200, "");
425 OpenNode(); 464 OpenNode();
426 ResetMocks(); 465 ResetMocks();
427 466
428 char buf[10]; 467 char buf[10];
429 memset(&buf[0], 0, sizeof(buf)); 468 memset(&buf[0], 0, sizeof(buf));
430 469
431 ExpectOpen("GET"); 470 ExpectOpen("GET");
432 ExpectHeaders("Range: bytes=10-18\n"); 471 ExpectHeaders("Range: bytes=10-18\n");
433 SetResponse(200, "Content-Length: 20\n"); 472 SetResponse(200, "Content-Length: 20\n");
434 SetResponseBody("0123456789abcdefghij"); 473 SetResponseBody("0123456789abcdefghij");
435 node_->Read(10, buf, sizeof(buf) - 1); 474 EXPECT_EQ(0, node_->Read(10, buf, sizeof(buf) - 1, &result_bytes));
475 EXPECT_EQ(sizeof(buf) - 1, result_bytes);
436 EXPECT_STREQ("abcdefghi", &buf[0]); 476 EXPECT_STREQ("abcdefghi", &buf[0]);
437 } 477 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698