1ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved. 2c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Use of this source code is governed by a BSD-style license that can be 3c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// found in the LICENSE file. 4c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 5c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/basictypes.h" 6c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "googleurl/src/gurl.h" 7c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "net/base/mime_sniffer.h" 8c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "testing/gtest/include/gtest/gtest.h" 9c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 10ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsennamespace net { 11c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 12c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstruct SnifferTest { 13c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const char* content; 14c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott size_t content_len; 15c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott std::string url; 16c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott std::string type_hint; 17c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const char* mime_type; 18c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}; 19c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 20c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstatic void TestArray(SnifferTest* tests, size_t count) { 21c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott std::string mime_type; 22c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 23c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott for (size_t i = 0; i < count; ++i) { 24ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen SniffMimeType(tests[i].content, 25c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott tests[i].content_len, 26c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott GURL(tests[i].url), 27c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott tests[i].type_hint, 28c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott &mime_type); 29c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ(tests[i].mime_type, mime_type); 30c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 31c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 32c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 33c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// TODO(evanm): convert other tests to use SniffMimeType instead of TestArray, 34c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// so the error messages produced by test failures are more useful. 35c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstatic std::string SniffMimeType(const std::string& content, 36c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const std::string& url, 37c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const std::string& mime_type_hint) { 38c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott std::string mime_type; 39ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen SniffMimeType(content.data(), content.size(), GURL(url), 40c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott mime_type_hint, &mime_type); 41c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return mime_type; 42c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 43c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 44c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottTEST(MimeSnifferTest, BoundaryConditionsTest) { 45c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott std::string mime_type; 46c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott std::string type_hint; 47c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 48c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott char buf[] = { 49c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 'd', '\x1f', '\xFF' 50c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott }; 51c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 52c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott GURL url; 53c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 54ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen SniffMimeType(buf, 0, url, type_hint, &mime_type); 55c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ("text/plain", mime_type); 56ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen SniffMimeType(buf, 1, url, type_hint, &mime_type); 57c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ("text/plain", mime_type); 58ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen SniffMimeType(buf, 2, url, type_hint, &mime_type); 59c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ("application/octet-stream", mime_type); 60c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 61c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 62c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottTEST(MimeSnifferTest, BasicSniffingTest) { 63c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott SnifferTest tests[] = { 64c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "<!DOCTYPE html PUBLIC", sizeof("<!DOCTYPE html PUBLIC")-1, 65c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/", 66c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "text/html" }, 67c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "<HtMl><Body></body></htMl>", sizeof("<HtMl><Body></body></htMl>")-1, 68c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo.gif", 69c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "application/octet-stream", "application/octet-stream" }, 70c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "GIF89a\x1F\x83\x94", sizeof("GIF89a\xAF\x83\x94")-1, 71c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo", 72c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "text/plain", "image/gif" }, 73c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "Gif87a\x1F\x83\x94", sizeof("Gif87a\xAF\x83\x94")-1, 74c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo?param=tt.gif", 75c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "application/octet-stream" }, 76c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "%!PS-Adobe-3.0", sizeof("%!PS-Adobe-3.0")-1, 77c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo", 78c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "text/plain", "text/plain" }, 79c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "\x89" "PNG\x0D\x0A\x1A\x0A", sizeof("\x89" "PNG\x0D\x0A\x1A\x0A")-1, 80c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo", 81c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "application/octet-stream", "application/octet-stream" }, 82c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "\xFF\xD8\xFF\x23\x49\xAF", sizeof("\xFF\xD8\xFF\x23\x49\xAF")-1, 83c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo", 84c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "image/jpeg" }, 85c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott }; 86c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 87c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott TestArray(tests, arraysize(tests)); 88c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 89c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 90c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottTEST(MimeSnifferTest, ChromeExtensionsTest) { 91c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott SnifferTest tests[] = { 92c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // schemes 93c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, 94c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo.crx", 95c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "application/x-chrome-extension" }, 96c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, 97c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "https://www.example.com/foo.crx", 98c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "application/x-chrome-extension" }, 99c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, 100c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "ftp://www.example.com/foo.crx", 101c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "application/x-chrome-extension" }, 102c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 103c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // some other mimetypes that should get converted 104c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, 105c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo.crx", 106c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "text/plain", "application/x-chrome-extension" }, 107c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, 108c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo.crx", 109c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "application/octet-stream", "application/x-chrome-extension" }, 110c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 111c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // success edge cases 112c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, 113c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo.crx?query=string", 114c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "application/x-chrome-extension" }, 115c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, 116c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo..crx", 117c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "application/x-chrome-extension" }, 118c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 119c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // wrong file extension 120c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, 121c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo.bin", 122c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "application/octet-stream" }, 123c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, 124c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo.bin?monkey", 125c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "application/octet-stream" }, 126c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, 127c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "invalid-url", 128c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "application/octet-stream" }, 129c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, 130c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com", 131c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "application/octet-stream" }, 132c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, 133c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/", 134c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "application/octet-stream" }, 135c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, 136c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo", 137c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "application/octet-stream" }, 138c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, 139c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foocrx", 140c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "application/octet-stream" }, 141c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, 142c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo.crx.blech", 143c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "application/octet-stream" }, 144c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 145c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // wrong magic 146c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "Cr24\x02\x00\x00\x01", sizeof("Cr24\x02\x00\x00\x01")-1, 147c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo.crx?monkey", 148c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "application/octet-stream" }, 149c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch { "PADDING_Cr24\x02\x00\x00\x00", sizeof("PADDING_Cr24\x02\x00\x00\x00")-1, 150c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "http://www.example.com/foo.crx?monkey", 151c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "", "application/octet-stream" }, 152c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott }; 153c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 154c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott TestArray(tests, arraysize(tests)); 155c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 156c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 157c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottTEST(MimeSnifferTest, MozillaCompatibleTest) { 158c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott SnifferTest tests[] = { 159c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { " \n <hTmL>\n <hea", sizeof(" \n <hTmL>\n <hea")-1, 160c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/", 161c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "text/html" }, 162c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { " \n <hTmL>\n <hea", sizeof(" \n <hTmL>\n <hea")-1, 163c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/", 164c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "text/plain", "text/plain" }, 165c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "BMjlakdsfk", sizeof("BMjlakdsfk")-1, 166c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo", 167c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "image/bmp" }, 168c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "\x00\x00\x30\x00", sizeof("\x00\x00\x30\x00")-1, 169c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/favicon.ico", 170c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "application/octet-stream" }, 171c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "#!/bin/sh\nls /\n", sizeof("#!/bin/sh\nls /\n")-1, 172c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo", 173c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "text/plain" }, 174c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "From: Fred\nTo: Bob\n\nHi\n.\n", 175c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott sizeof("From: Fred\nTo: Bob\n\nHi\n.\n")-1, 176c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo", 177c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "text/plain" }, 178c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", 179c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott sizeof("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")-1, 180c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo", 181c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "text/xml" }, 182c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", 183c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott sizeof("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")-1, 184c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo", 185c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "application/octet-stream", "application/octet-stream" }, 186c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott }; 187c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 188c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott TestArray(tests, arraysize(tests)); 189c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 190c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 191c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottTEST(MimeSnifferTest, DontAllowPrivilegeEscalationTest) { 192c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott SnifferTest tests[] = { 193c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "GIF87a\n<html>\n<body>" 194c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "<script>alert('haxorzed');\n</script>" 195c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "</body></html>\n", 196c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott sizeof("GIF87a\n<html>\n<body>" 197c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "<script>alert('haxorzed');\n</script>" 198c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "</body></html>\n")-1, 199c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo", 200c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "image/gif" }, 201c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "GIF87a\n<html>\n<body>" 202c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "<script>alert('haxorzed');\n</script>" 203c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "</body></html>\n", 204c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott sizeof("GIF87a\n<html>\n<body>" 205c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "<script>alert('haxorzed');\n</script>" 206c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "</body></html>\n")-1, 207c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo?q=ttt.html", 208c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "image/gif" }, 209c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "GIF87a\n<html>\n<body>" 210c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "<script>alert('haxorzed');\n</script>" 211c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "</body></html>\n", 212c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott sizeof("GIF87a\n<html>\n<body>" 213c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "<script>alert('haxorzed');\n</script>" 214c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "</body></html>\n")-1, 215c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo#ttt.html", 216c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "image/gif" }, 217c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "a\n<html>\n<body>" 218c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "<script>alert('haxorzed');\n</script>" 219c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "</body></html>\n", 220c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott sizeof("a\n<html>\n<body>" 221c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "<script>alert('haxorzed');\n</script>" 222c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "</body></html>\n")-1, 223c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo", 224c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "text/plain" }, 225c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "a\n<html>\n<body>" 226c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "<script>alert('haxorzed');\n</script>" 227c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "</body></html>\n", 228c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott sizeof("a\n<html>\n<body>" 229c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "<script>alert('haxorzed');\n</script>" 230c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "</body></html>\n")-1, 231c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo?q=ttt.html", 232c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "text/plain" }, 233c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "a\n<html>\n<body>" 234c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "<script>alert('haxorzed');\n</script>" 235c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "</body></html>\n", 236c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott sizeof("a\n<html>\n<body>" 237c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "<script>alert('haxorzed');\n</script>" 238c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "</body></html>\n")-1, 239c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo#ttt.html", 240c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "text/plain" }, 241c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "a\n<html>\n<body>" 242c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "<script>alert('haxorzed');\n</script>" 243c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "</body></html>\n", 244c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott sizeof("a\n<html>\n<body>" 245c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "<script>alert('haxorzed');\n</script>" 246c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "</body></html>\n")-1, 247c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo.html", 248c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "text/plain" }, 249c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott }; 250c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 251c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott TestArray(tests, arraysize(tests)); 252c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 253c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 254c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottTEST(MimeSnifferTest, UnicodeTest) { 255c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott SnifferTest tests[] = { 256c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "\xEF\xBB\xBF" "Hi there", sizeof("\xEF\xBB\xBF" "Hi there")-1, 257c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo", 258c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "text/plain" }, 259c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "\xEF\xBB\xBF\xED\x7A\xAD\x7A\x0D\x79", 260c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott sizeof("\xEF\xBB\xBF\xED\x7A\xAD\x7A\x0D\x79")-1, 261c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo", 262c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "text/plain" }, 263c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "\xFE\xFF\xD0\xA5\xD0\xBE\xD0\xBB\xD1\x83\xD0\xB9", 264c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott sizeof("\xFE\xFF\xD0\xA5\xD0\xBE\xD0\xBB\xD1\x83\xD0\xB9")-1, 265c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo", 266c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "text/plain" }, 267c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "\xFE\xFF\x00\x41\x00\x20\xD8\x00\xDC\x00\xD8\x00\xDC\x01", 268c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott sizeof("\xFE\xFF\x00\x41\x00\x20\xD8\x00\xDC\x00\xD8\x00\xDC\x01")-1, 269c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo", 270c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "text/plain" }, 271c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott }; 272c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 273c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott TestArray(tests, arraysize(tests)); 274c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 275c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 276c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottTEST(MimeSnifferTest, FlashTest) { 277c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott SnifferTest tests[] = { 278c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "CWSdd\x00\xB3", sizeof("CWSdd\x00\xB3")-1, 279c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo", 280c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "application/octet-stream" }, 281c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "FLVjdkl*(#)0sdj\x00", sizeof("FLVjdkl*(#)0sdj\x00")-1, 282c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo?q=ttt.swf", 283c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "application/octet-stream" }, 284c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "FWS3$9\r\b\x00", sizeof("FWS3$9\r\b\x00")-1, 285c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo#ttt.swf", 286c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "application/octet-stream" }, 287c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "FLVjdkl*(#)0sdj", sizeof("FLVjdkl*(#)0sdj")-1, 288c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo.swf", 289c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "text/plain" }, 290c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "FLVjdkl*(#)0s\x01dj", sizeof("FLVjdkl*(#)0s\x01dj")-1, 291c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo/bar.swf", 292c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "application/octet-stream" }, 293c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "FWS3$9\r\b\x1A", sizeof("FWS3$9\r\b\x1A")-1, 294c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo.swf?clickTAG=http://www.adnetwork.com/bar", 295c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "application/octet-stream" }, 296c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott { "FWS3$9\r\x1C\b", sizeof("FWS3$9\r\x1C\b")-1, 297c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/foo.swf?clickTAG=http://www.adnetwork.com/bar", 298c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "text/plain", "application/octet-stream" }, 299c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott }; 300c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 301c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott TestArray(tests, arraysize(tests)); 302c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 303c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 304c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottTEST(MimeSnifferTest, XMLTest) { 305c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // An easy feed to identify. 306c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ("application/atom+xml", 307c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott SniffMimeType("<?xml?><feed", "", "text/xml")); 308c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Don't sniff out of plain text. 309c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ("text/plain", 310c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott SniffMimeType("<?xml?><feed", "", "text/plain")); 311c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Simple RSS. 312c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ("application/rss+xml", 313c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott SniffMimeType("<?xml version='1.0'?>\r\n<rss", "", "text/xml")); 314c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 315c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // The top of CNN's RSS feed, which we'd like to recognize as RSS. 316c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott static const char kCNNRSS[] = 317c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 318c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "<?xml-stylesheet href=\"http://rss.cnn.com/~d/styles/rss2full.xsl\" " 319c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "type=\"text/xsl\" media=\"screen\"?>" 320c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "<?xml-stylesheet href=\"http://rss.cnn.com/~d/styles/itemcontent.css\" " 321c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "type=\"text/css\" media=\"screen\"?>" 322c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "<rss xmlns:feedburner=\"http://rssnamespace.org/feedburner/ext/1.0\" " 323c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "version=\"2.0\">"; 324c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // CNN's RSS 325c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ("application/rss+xml", 326c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott SniffMimeType(kCNNRSS, "", "text/xml")); 327c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ("text/plain", 328c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott SniffMimeType(kCNNRSS, "", "text/plain")); 329c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 330c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Don't sniff random XML as something different. 331c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ("text/xml", 332c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott SniffMimeType("<?xml?><notafeed", "", "text/xml")); 333c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Don't sniff random plain-text as something different. 334c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ("text/plain", 335c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott SniffMimeType("<?xml?><notafeed", "", "text/plain")); 336c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 337c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Positive test for the two instances we upgrade to XHTML. 338c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ("application/xhtml+xml", 339c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott SniffMimeType("<html xmlns=\"http://www.w3.org/1999/xhtml\">", 340c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "text/xml")); 341c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ("application/xhtml+xml", 342c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott SniffMimeType("<html xmlns=\"http://www.w3.org/1999/xhtml\">", 343c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "application/xml")); 344c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 345c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Following our behavior with HTML, don't call other mime types XHTML. 346c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ("text/plain", 347c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott SniffMimeType("<html xmlns=\"http://www.w3.org/1999/xhtml\">", 348c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "text/plain")); 349c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ("application/rss+xml", 350c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott SniffMimeType("<html xmlns=\"http://www.w3.org/1999/xhtml\">", 351c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "application/rss+xml")); 352c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 353c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Don't sniff other HTML-looking bits as HTML. 354c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ("text/xml", 355c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott SniffMimeType("<html><head>", "", "text/xml")); 356c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ("text/xml", 357c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott SniffMimeType("<foo><html xmlns=\"http://www.w3.org/1999/xhtml\">", 358c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", "text/xml")); 359c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 360c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 361c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 362c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Test content which is >= 1024 bytes, and includes no open angle bracket. 363c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// http://code.google.com/p/chromium/issues/detail?id=3521 364c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottTEST(MimeSnifferTest, XMLTestLargeNoAngledBracket) { 365c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Make a large input, with 1024 bytes of "x". 366c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott std::string content; 367c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch content.resize(1024); 368c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott std::fill(content.begin(), content.end(), 'x'); 369c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 370c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // content.size() >= 1024 so the sniff is unambiguous. 371c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott std::string mime_type; 372ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen EXPECT_TRUE(SniffMimeType(content.data(), content.size(), GURL(), 373ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen "text/xml", &mime_type)); 374c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ("text/xml", mime_type); 375c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 376c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 377c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Test content which is >= 1024 bytes, and includes a binary looking byte. 378c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// http://code.google.com/p/chromium/issues/detail?id=15314 379c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochTEST(MimeSnifferTest, LooksBinary) { 380c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Make a large input, with 1024 bytes of "x" and 1 byte of 0x01. 381c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch std::string content; 382c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch content.resize(1024); 383c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch std::fill(content.begin(), content.end(), 'x'); 384c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch content[1000] = 0x01; 385c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 386c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // content.size() >= 1024 so the sniff is unambiguous. 387c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch std::string mime_type; 388ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen EXPECT_TRUE(SniffMimeType(content.data(), content.size(), GURL(), 389ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen "text/plain", &mime_type)); 390c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ("application/octet-stream", mime_type); 391c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 392ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen 393ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen} // namespace net 394