| 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 "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 net { | 10 namespace net { |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 "http://www.example.com/foo.swf?clickTAG=http://www.adnetwork.com/bar", | 297 "http://www.example.com/foo.swf?clickTAG=http://www.adnetwork.com/bar", |
| 298 "text/plain", "application/octet-stream" }, | 298 "text/plain", "application/octet-stream" }, |
| 299 }; | 299 }; |
| 300 | 300 |
| 301 TestArray(tests, arraysize(tests)); | 301 TestArray(tests, arraysize(tests)); |
| 302 } | 302 } |
| 303 | 303 |
| 304 TEST(MimeSnifferTest, XMLTest) { | 304 TEST(MimeSnifferTest, XMLTest) { |
| 305 // An easy feed to identify. | 305 // An easy feed to identify. |
| 306 EXPECT_EQ("application/atom+xml", | 306 EXPECT_EQ("application/atom+xml", |
| 307 SniffMimeType("<?xml?><feed", "", "text/xml")); | 307 SniffMimeType("<?xml?><feed", std::string(), "text/xml")); |
| 308 // Don't sniff out of plain text. | 308 // Don't sniff out of plain text. |
| 309 EXPECT_EQ("text/plain", | 309 EXPECT_EQ("text/plain", |
| 310 SniffMimeType("<?xml?><feed", "", "text/plain")); | 310 SniffMimeType("<?xml?><feed", std::string(), "text/plain")); |
| 311 // Simple RSS. | 311 // Simple RSS. |
| 312 EXPECT_EQ("application/rss+xml", | 312 EXPECT_EQ("application/rss+xml", |
| 313 SniffMimeType("<?xml version='1.0'?>\r\n<rss", "", "text/xml")); | 313 SniffMimeType( |
| 314 "<?xml version='1.0'?>\r\n<rss", std::string(), "text/xml")); |
| 314 | 315 |
| 315 // The top of CNN's RSS feed, which we'd like to recognize as RSS. | 316 // The top of CNN's RSS feed, which we'd like to recognize as RSS. |
| 316 static const char kCNNRSS[] = | 317 static const char kCNNRSS[] = |
| 317 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" | 318 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" |
| 318 "<?xml-stylesheet href=\"http://rss.cnn.com/~d/styles/rss2full.xsl\" " | 319 "<?xml-stylesheet href=\"http://rss.cnn.com/~d/styles/rss2full.xsl\" " |
| 319 "type=\"text/xsl\" media=\"screen\"?>" | 320 "type=\"text/xsl\" media=\"screen\"?>" |
| 320 "<?xml-stylesheet href=\"http://rss.cnn.com/~d/styles/itemcontent.css\" " | 321 "<?xml-stylesheet href=\"http://rss.cnn.com/~d/styles/itemcontent.css\" " |
| 321 "type=\"text/css\" media=\"screen\"?>" | 322 "type=\"text/css\" media=\"screen\"?>" |
| 322 "<rss xmlns:feedburner=\"http://rssnamespace.org/feedburner/ext/1.0\" " | 323 "<rss xmlns:feedburner=\"http://rssnamespace.org/feedburner/ext/1.0\" " |
| 323 "version=\"2.0\">"; | 324 "version=\"2.0\">"; |
| 324 // CNN's RSS | 325 // CNN's RSS |
| 325 EXPECT_EQ("application/rss+xml", | 326 EXPECT_EQ("application/rss+xml", |
| 326 SniffMimeType(kCNNRSS, "", "text/xml")); | 327 SniffMimeType(kCNNRSS, std::string(), "text/xml")); |
| 327 EXPECT_EQ("text/plain", | 328 EXPECT_EQ("text/plain", SniffMimeType(kCNNRSS, std::string(), "text/plain")); |
| 328 SniffMimeType(kCNNRSS, "", "text/plain")); | |
| 329 | 329 |
| 330 // Don't sniff random XML as something different. | 330 // Don't sniff random XML as something different. |
| 331 EXPECT_EQ("text/xml", | 331 EXPECT_EQ("text/xml", |
| 332 SniffMimeType("<?xml?><notafeed", "", "text/xml")); | 332 SniffMimeType("<?xml?><notafeed", std::string(), "text/xml")); |
| 333 // Don't sniff random plain-text as something different. | 333 // Don't sniff random plain-text as something different. |
| 334 EXPECT_EQ("text/plain", | 334 EXPECT_EQ("text/plain", |
| 335 SniffMimeType("<?xml?><notafeed", "", "text/plain")); | 335 SniffMimeType("<?xml?><notafeed", std::string(), "text/plain")); |
| 336 | 336 |
| 337 // Positive test for the two instances we upgrade to XHTML. | 337 // Positive test for the two instances we upgrade to XHTML. |
| 338 EXPECT_EQ("application/xhtml+xml", | 338 EXPECT_EQ("application/xhtml+xml", |
| 339 SniffMimeType("<html xmlns=\"http://www.w3.org/1999/xhtml\">", | 339 SniffMimeType("<html xmlns=\"http://www.w3.org/1999/xhtml\">", |
| 340 "", "text/xml")); | 340 std::string(), |
| 341 "text/xml")); |
| 341 EXPECT_EQ("application/xhtml+xml", | 342 EXPECT_EQ("application/xhtml+xml", |
| 342 SniffMimeType("<html xmlns=\"http://www.w3.org/1999/xhtml\">", | 343 SniffMimeType("<html xmlns=\"http://www.w3.org/1999/xhtml\">", |
| 343 "", "application/xml")); | 344 std::string(), |
| 345 "application/xml")); |
| 344 | 346 |
| 345 // Following our behavior with HTML, don't call other mime types XHTML. | 347 // Following our behavior with HTML, don't call other mime types XHTML. |
| 346 EXPECT_EQ("text/plain", | 348 EXPECT_EQ("text/plain", |
| 347 SniffMimeType("<html xmlns=\"http://www.w3.org/1999/xhtml\">", | 349 SniffMimeType("<html xmlns=\"http://www.w3.org/1999/xhtml\">", |
| 348 "", "text/plain")); | 350 std::string(), |
| 351 "text/plain")); |
| 349 EXPECT_EQ("application/rss+xml", | 352 EXPECT_EQ("application/rss+xml", |
| 350 SniffMimeType("<html xmlns=\"http://www.w3.org/1999/xhtml\">", | 353 SniffMimeType("<html xmlns=\"http://www.w3.org/1999/xhtml\">", |
| 351 "", "application/rss+xml")); | 354 std::string(), |
| 355 "application/rss+xml")); |
| 352 | 356 |
| 353 // Don't sniff other HTML-looking bits as HTML. | 357 // Don't sniff other HTML-looking bits as HTML. |
| 354 EXPECT_EQ("text/xml", | 358 EXPECT_EQ("text/xml", |
| 355 SniffMimeType("<html><head>", "", "text/xml")); | 359 SniffMimeType("<html><head>", std::string(), "text/xml")); |
| 356 EXPECT_EQ("text/xml", | 360 EXPECT_EQ("text/xml", |
| 357 SniffMimeType("<foo><html xmlns=\"http://www.w3.org/1999/xhtml\">", | 361 SniffMimeType("<foo><html xmlns=\"http://www.w3.org/1999/xhtml\">", |
| 358 "", "text/xml")); | 362 std::string(), |
| 363 "text/xml")); |
| 359 | 364 |
| 360 } | 365 } |
| 361 | 366 |
| 362 // Test content which is >= 1024 bytes, and includes no open angle bracket. | 367 // Test content which is >= 1024 bytes, and includes no open angle bracket. |
| 363 // http://code.google.com/p/chromium/issues/detail?id=3521 | 368 // http://code.google.com/p/chromium/issues/detail?id=3521 |
| 364 TEST(MimeSnifferTest, XMLTestLargeNoAngledBracket) { | 369 TEST(MimeSnifferTest, XMLTestLargeNoAngledBracket) { |
| 365 // Make a large input, with 1024 bytes of "x". | 370 // Make a large input, with 1024 bytes of "x". |
| 366 std::string content; | 371 std::string content; |
| 367 content.resize(1024); | 372 content.resize(1024); |
| 368 std::fill(content.begin(), content.end(), 'x'); | 373 std::fill(content.begin(), content.end(), 'x'); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 384 content[1000] = 0x01; | 389 content[1000] = 0x01; |
| 385 | 390 |
| 386 // content.size() >= 1024 so the sniff is unambiguous. | 391 // content.size() >= 1024 so the sniff is unambiguous. |
| 387 std::string mime_type; | 392 std::string mime_type; |
| 388 EXPECT_TRUE(SniffMimeType(content.data(), content.size(), GURL(), | 393 EXPECT_TRUE(SniffMimeType(content.data(), content.size(), GURL(), |
| 389 "text/plain", &mime_type)); | 394 "text/plain", &mime_type)); |
| 390 EXPECT_EQ("application/octet-stream", mime_type); | 395 EXPECT_EQ("application/octet-stream", mime_type); |
| 391 } | 396 } |
| 392 | 397 |
| 393 } // namespace net | 398 } // namespace net |
| OLD | NEW |