OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "net/base/mime_sniffer.h" | 6 #include "net/base/mime_sniffer.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "url/gurl.h" | 8 #include "url/gurl.h" |
9 | 9 |
10 namespace net { | 10 namespace net { |
| 11 namespace { |
| 12 |
| 13 using ::testing::Range; |
| 14 using ::testing::Values; |
| 15 using ::net::SniffMimeType; // It is shadowed by SniffMimeType(), below. |
11 | 16 |
12 struct SnifferTest { | 17 struct SnifferTest { |
13 const char* content; | 18 const char* content; |
14 size_t content_len; | 19 size_t content_len; |
15 std::string url; | 20 std::string url; |
16 std::string type_hint; | 21 std::string type_hint; |
17 const char* mime_type; | 22 const char* mime_type; |
18 }; | 23 }; |
19 | 24 |
20 static void TestArray(SnifferTest* tests, size_t count) { | 25 static void TestArray(SnifferTest* tests, size_t count) { |
(...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
477 | 482 |
478 const char kAACTestData[] = | 483 const char kAACTestData[] = |
479 "\xff\xf1\x50\x80\x02\x20\xb0\x23\x0a\x83\x20\x7d\x61\x90\x3e\xb1"; | 484 "\xff\xf1\x50\x80\x02\x20\xb0\x23\x0a\x83\x20\x7d\x61\x90\x3e\xb1"; |
480 EXPECT_TRUE(SniffMimeTypeFromLocalData(kAACTestData, | 485 EXPECT_TRUE(SniffMimeTypeFromLocalData(kAACTestData, |
481 sizeof(kAACTestData), | 486 sizeof(kAACTestData), |
482 &mime_type)); | 487 &mime_type)); |
483 EXPECT_EQ("audio/mpeg", mime_type); | 488 EXPECT_EQ("audio/mpeg", mime_type); |
484 mime_type.clear(); | 489 mime_type.clear(); |
485 } | 490 } |
486 | 491 |
| 492 // The tests need char parameters, but the ranges to test include 0xFF, and some |
| 493 // platforms have signed chars and are noisy about it. Using an int parameter |
| 494 // and casting it to char inside the test case solves both these problems. |
| 495 class MimeSnifferBinaryTest : public ::testing::TestWithParam<int> {}; |
| 496 |
| 497 // From https://mimesniff.spec.whatwg.org/#binary-data-byte : |
| 498 // A binary data byte is a byte in the range 0x00 to 0x08 (NUL to BS), the byte |
| 499 // 0x0B (VT), a byte in the range 0x0E to 0x1A (SO to SUB), or a byte in the |
| 500 // range 0x1C to 0x1F (FS to US). |
| 501 TEST_P(MimeSnifferBinaryTest, IsBinaryControlCode) { |
| 502 char param = static_cast<char>(GetParam()); |
| 503 EXPECT_TRUE(LooksLikeBinary(¶m, 1)); |
| 504 } |
| 505 |
| 506 // ::testing::Range(a, b) tests an open-ended range, ie. "b" is not included. |
| 507 INSTANTIATE_TEST_CASE_P(MimeSnifferBinaryTestRange1, |
| 508 MimeSnifferBinaryTest, |
| 509 Range(0x00, 0x09)); |
| 510 |
| 511 INSTANTIATE_TEST_CASE_P(MimeSnifferBinaryTestByte0x0B, |
| 512 MimeSnifferBinaryTest, |
| 513 Values(0x0B)); |
| 514 |
| 515 INSTANTIATE_TEST_CASE_P(MimeSnifferBinaryTestRange2, |
| 516 MimeSnifferBinaryTest, |
| 517 Range(0x0E, 0x1B)); |
| 518 |
| 519 INSTANTIATE_TEST_CASE_P(MimeSnifferBinaryTestRange3, |
| 520 MimeSnifferBinaryTest, |
| 521 Range(0x1C, 0x20)); |
| 522 |
| 523 class MimeSnifferPlainTextTest : public ::testing::TestWithParam<int> {}; |
| 524 |
| 525 TEST_P(MimeSnifferPlainTextTest, NotBinaryControlCode) { |
| 526 char param = static_cast<char>(GetParam()); |
| 527 EXPECT_FALSE(LooksLikeBinary(¶m, 1)); |
| 528 } |
| 529 |
| 530 INSTANTIATE_TEST_CASE_P(MimeSnifferPlainTextTestPlainTextControlCodes, |
| 531 MimeSnifferPlainTextTest, |
| 532 Values(0x09, 0x0A, 0x0C, 0x0D, 0x1B)); |
| 533 |
| 534 INSTANTIATE_TEST_CASE_P(MimeSnifferPlainTextTestNotControlCodeRange, |
| 535 MimeSnifferPlainTextTest, |
| 536 Range(0x20, 0x100)); |
| 537 |
| 538 class MimeSnifferControlCodesEdgeCaseTest |
| 539 : public ::testing::TestWithParam<const char*> {}; |
| 540 |
| 541 TEST_P(MimeSnifferControlCodesEdgeCaseTest, EdgeCase) { |
| 542 const char* param = GetParam(); |
| 543 EXPECT_TRUE(LooksLikeBinary(param, strlen(param))); |
| 544 } |
| 545 |
| 546 INSTANTIATE_TEST_CASE_P(MimeSnifferControlCodesEdgeCaseTest, |
| 547 MimeSnifferControlCodesEdgeCaseTest, |
| 548 Values("\x01__", // first byte is binary |
| 549 "__\x03", // last byte is binary |
| 550 "_\x02_" // a byte in the middle is binary |
| 551 )); |
| 552 |
| 553 } // namespace |
487 } // namespace net | 554 } // namespace net |
OLD | NEW |