| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 "http://www.example.com/foo.crx?monkey", | 150 "http://www.example.com/foo.crx?monkey", |
| 151 "", "application/octet-stream" }, | 151 "", "application/octet-stream" }, |
| 152 { "PADDING_Cr24\x02\x00\x00\x00", sizeof("PADDING_Cr24\x02\x00\x00\x00")-1, | 152 { "PADDING_Cr24\x02\x00\x00\x00", sizeof("PADDING_Cr24\x02\x00\x00\x00")-1, |
| 153 "http://www.example.com/foo.crx?monkey", | 153 "http://www.example.com/foo.crx?monkey", |
| 154 "", "application/octet-stream" }, | 154 "", "application/octet-stream" }, |
| 155 }; | 155 }; |
| 156 | 156 |
| 157 TestArray(tests, arraysize(tests)); | 157 TestArray(tests, arraysize(tests)); |
| 158 } | 158 } |
| 159 | 159 |
| 160 TEST(MimeSnifferTest, MHTMLTest) { |
| 161 // Simple MHT file. |
| 162 EXPECT_EQ("multipart/related", |
| 163 SniffMimeType("From: Chrome\r\n", |
| 164 "file://c/Users/Q/foo.mht", |
| 165 "")); |
| 166 // Simple MHTML file. |
| 167 EXPECT_EQ("multipart/related", |
| 168 SniffMimeType("From: Chrome\r\n", |
| 169 "file://c/Users/Q/foo.mhtml", |
| 170 "")); |
| 171 // File with bad extension. |
| 172 EXPECT_EQ("text/plain", |
| 173 SniffMimeType("From: Chrome\r\n", |
| 174 "file://c/Users/Q/foo.mhtm", |
| 175 "")); |
| 176 // MHTML should not be sniffed, regardless of the extension. |
| 177 EXPECT_FALSE(net::ShouldSniffMimeType( |
| 178 GURL("http://www.example.com/foo.mht"), "multipart/related")); |
| 179 EXPECT_FALSE(net::ShouldSniffMimeType( |
| 180 GURL("http://www.example.com/foo.mhtml"), "multipart/related")); |
| 181 EXPECT_FALSE(net::ShouldSniffMimeType( |
| 182 GURL("http://www.example.com/foo"), "multipart/related")); |
| 183 // MHTML served as plain-text is not recognized. |
| 184 EXPECT_EQ("text/plain", |
| 185 SniffMimeType("From: Chrome\r\n", |
| 186 "http://www.example.com/foo", |
| 187 "text/plain")); |
| 188 } |
| 189 |
| 160 TEST(MimeSnifferTest, MozillaCompatibleTest) { | 190 TEST(MimeSnifferTest, MozillaCompatibleTest) { |
| 161 SnifferTest tests[] = { | 191 SnifferTest tests[] = { |
| 162 { " \n <hTmL>\n <hea", sizeof(" \n <hTmL>\n <hea")-1, | 192 { " \n <hTmL>\n <hea", sizeof(" \n <hTmL>\n <hea")-1, |
| 163 "http://www.example.com/", | 193 "http://www.example.com/", |
| 164 "", "text/html" }, | 194 "", "text/html" }, |
| 165 { " \n <hTmL>\n <hea", sizeof(" \n <hTmL>\n <hea")-1, | 195 { " \n <hTmL>\n <hea", sizeof(" \n <hTmL>\n <hea")-1, |
| 166 "http://www.example.com/", | 196 "http://www.example.com/", |
| 167 "text/plain", "text/plain" }, | 197 "text/plain", "text/plain" }, |
| 168 { "BMjlakdsfk", sizeof("BMjlakdsfk")-1, | 198 { "BMjlakdsfk", sizeof("BMjlakdsfk")-1, |
| 169 "http://www.example.com/foo", | 199 "http://www.example.com/foo", |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 content.resize(1024); | 415 content.resize(1024); |
| 386 std::fill(content.begin(), content.end(), 'x'); | 416 std::fill(content.begin(), content.end(), 'x'); |
| 387 content[1000] = 0x01; | 417 content[1000] = 0x01; |
| 388 | 418 |
| 389 // content.size() >= 1024 so the sniff is unambiguous. | 419 // content.size() >= 1024 so the sniff is unambiguous. |
| 390 std::string mime_type; | 420 std::string mime_type; |
| 391 EXPECT_TRUE(net::SniffMimeType(content.data(), content.size(), GURL(), | 421 EXPECT_TRUE(net::SniffMimeType(content.data(), content.size(), GURL(), |
| 392 "text/plain", &mime_type)); | 422 "text/plain", &mime_type)); |
| 393 EXPECT_EQ("application/octet-stream", mime_type); | 423 EXPECT_EQ("application/octet-stream", mime_type); |
| 394 } | 424 } |
| OLD | NEW |