| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "googleurl/src/gurl.h" | 6 #include "googleurl/src/gurl.h" |
| 7 #include "net/base/mime_sniffer.h" | 7 #include "net/base/mime_sniffer.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 9 |
| 10 namespace { | 10 namespace { |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 "http://www.example.com/foo", | 83 "http://www.example.com/foo", |
| 84 "application/octet-stream", "image/png" }, | 84 "application/octet-stream", "image/png" }, |
| 85 { "\xFF\xD8\xFF\x23\x49\xAF", sizeof("\xFF\xD8\xFF\x23\x49\xAF")-1, | 85 { "\xFF\xD8\xFF\x23\x49\xAF", sizeof("\xFF\xD8\xFF\x23\x49\xAF")-1, |
| 86 "http://www.example.com/foo", | 86 "http://www.example.com/foo", |
| 87 "", "image/jpeg" }, | 87 "", "image/jpeg" }, |
| 88 }; | 88 }; |
| 89 | 89 |
| 90 TestArray(tests, arraysize(tests)); | 90 TestArray(tests, arraysize(tests)); |
| 91 } | 91 } |
| 92 | 92 |
| 93 TEST(MimeSnifferTest, ChromeExtensionsTest) { |
| 94 SnifferTest tests[] = { |
| 95 // schemes |
| 96 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, |
| 97 "http://www.example.com/foo.crx", |
| 98 "", "application/x-chrome-extension" }, |
| 99 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, |
| 100 "https://www.example.com/foo.crx", |
| 101 "", "application/x-chrome-extension" }, |
| 102 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, |
| 103 "ftp://www.example.com/foo.crx", |
| 104 "", "application/x-chrome-extension" }, |
| 105 |
| 106 // some other mimetypes that should get converted |
| 107 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, |
| 108 "http://www.example.com/foo.crx", |
| 109 "text/plain", "application/x-chrome-extension" }, |
| 110 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, |
| 111 "http://www.example.com/foo.crx", |
| 112 "application/octet-stream", "application/x-chrome-extension" }, |
| 113 |
| 114 // success edge cases |
| 115 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, |
| 116 "http://www.example.com/foo.crx?query=string", |
| 117 "", "application/x-chrome-extension" }, |
| 118 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, |
| 119 "http://www.example.com/foo..crx", |
| 120 "", "application/x-chrome-extension" }, |
| 121 |
| 122 // wrong file extension |
| 123 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, |
| 124 "http://www.example.com/foo.bin", |
| 125 "", "application/octet-stream" }, |
| 126 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, |
| 127 "http://www.example.com/foo.bin?monkey", |
| 128 "", "application/octet-stream" }, |
| 129 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, |
| 130 "invalid-url", |
| 131 "", "application/octet-stream" }, |
| 132 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, |
| 133 "http://www.example.com", |
| 134 "", "application/octet-stream" }, |
| 135 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, |
| 136 "http://www.example.com/", |
| 137 "", "application/octet-stream" }, |
| 138 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, |
| 139 "http://www.example.com/foo", |
| 140 "", "application/octet-stream" }, |
| 141 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, |
| 142 "http://www.example.com/foocrx", |
| 143 "", "application/octet-stream" }, |
| 144 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, |
| 145 "http://www.example.com/foo.crx.blech", |
| 146 "", "application/octet-stream" }, |
| 147 |
| 148 // wrong magic |
| 149 { "Cr24\x02\x00\x00\x01", sizeof("Cr24\x02\x00\x00\x01")-1, |
| 150 "http://www.example.com/foo.crx?monkey", |
| 151 "", "application/octet-stream" }, |
| 152 }; |
| 153 |
| 154 TestArray(tests, arraysize(tests)); |
| 155 } |
| 156 |
| 93 TEST(MimeSnifferTest, MozillaCompatibleTest) { | 157 TEST(MimeSnifferTest, MozillaCompatibleTest) { |
| 94 SnifferTest tests[] = { | 158 SnifferTest tests[] = { |
| 95 { " \n <hTmL>\n <hea", sizeof(" \n <hTmL>\n <hea")-1, | 159 { " \n <hTmL>\n <hea", sizeof(" \n <hTmL>\n <hea")-1, |
| 96 "http://www.example.com/", | 160 "http://www.example.com/", |
| 97 "", "text/html" }, | 161 "", "text/html" }, |
| 98 { " \n <hTmL>\n <hea", sizeof(" \n <hTmL>\n <hea")-1, | 162 { " \n <hTmL>\n <hea", sizeof(" \n <hTmL>\n <hea")-1, |
| 99 "http://www.example.com/", | 163 "http://www.example.com/", |
| 100 "text/plain", "text/plain" }, | 164 "text/plain", "text/plain" }, |
| 101 { "BMjlakdsfk", sizeof("BMjlakdsfk")-1, | 165 { "BMjlakdsfk", sizeof("BMjlakdsfk")-1, |
| 102 "http://www.example.com/foo", | 166 "http://www.example.com/foo", |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 // Make a large input, with 600 bytes of "x". | 365 // Make a large input, with 600 bytes of "x". |
| 302 std::string content; | 366 std::string content; |
| 303 content.resize(600); | 367 content.resize(600); |
| 304 std::fill(content.begin(), content.end(), 'x'); | 368 std::fill(content.begin(), content.end(), 'x'); |
| 305 | 369 |
| 306 // content.size() >= kMaxBytesToSniff (512) so the sniff is unambiguous. | 370 // content.size() >= kMaxBytesToSniff (512) so the sniff is unambiguous. |
| 307 std::string mime_type; | 371 std::string mime_type; |
| 308 EXPECT_TRUE(net::SniffMimeType(content.data(), content.size(), GURL(), | 372 EXPECT_TRUE(net::SniffMimeType(content.data(), content.size(), GURL(), |
| 309 "text/xml", &mime_type)); | 373 "text/xml", &mime_type)); |
| 310 } | 374 } |
| OLD | NEW |