Chromium Code Reviews| 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 | 11 |
| 12 struct SnifferTest { | 12 struct SnifferTest { |
| 13 const char* content; | 13 const char* content; |
| 14 size_t content_len; | 14 size_t content_len; |
| 15 std::string url; | 15 std::string url; |
| 16 std::string type_hint; | 16 std::string type_hint; |
| 17 const char* mime_type; | 17 const char* mime_type; |
| 18 }; | 18 }; |
| 19 | 19 |
| 20 static void TestArray(SnifferTest* tests, size_t count) { | 20 static void TestArray(SnifferTest* tests, size_t count) { |
| 21 std::string mime_type; | 21 std::string mime_type; |
| 22 | 22 |
| 23 for (size_t i = 0; i < count; ++i) { | 23 for (size_t i = 0; i < count; ++i) { |
| 24 SniffMimeType(tests[i].content, | 24 SniffMimeType(tests[i].content, |
| 25 tests[i].content_len, | 25 tests[i].content_len, |
| 26 GURL(tests[i].url), | 26 GURL(tests[i].url), |
| 27 tests[i].type_hint, | 27 tests[i].type_hint, |
| 28 &mime_type); | 28 &mime_type); |
| 29 EXPECT_EQ(tests[i].mime_type, mime_type); | 29 EXPECT_EQ(tests[i].mime_type, mime_type); |
| 30 } | 30 } |
| 31 } | 31 } |
| 32 | 32 |
| 33 // TODO(evanm): convert other tests to use SniffMimeType instead of TestArray, | 33 // TODO(evanm): convert other tests to use SniffMimeType instead of TestArray, |
| 34 // so the error messages produced by test failures are more useful. | 34 // so the error messages produced by test failures are more useful. |
| 35 static std::string SniffMimeType(const std::string& content, | 35 static std::string SniffMimeType(const std::string& content, |
| 36 const std::string& url, | 36 const std::string& url, |
| 37 const std::string& mime_type_hint) { | 37 const std::string& mime_type_hint) { |
| 38 std::string mime_type; | 38 std::string mime_type; |
| 39 SniffMimeType(content.data(), content.size(), GURL(url), | 39 SniffMimeType( |
| 40 mime_type_hint, &mime_type); | 40 content.data(), content.size(), GURL(url), mime_type_hint, &mime_type); |
| 41 return mime_type; | 41 return mime_type; |
| 42 } | 42 } |
| 43 | 43 |
| 44 TEST(MimeSnifferTest, BoundaryConditionsTest) { | 44 TEST(MimeSnifferTest, BoundaryConditionsTest) { |
| 45 std::string mime_type; | 45 std::string mime_type; |
| 46 std::string type_hint; | 46 std::string type_hint; |
| 47 | 47 |
| 48 char buf[] = { | 48 char buf[] = {'d', '\x1f', '\xFF'}; |
| 49 'd', '\x1f', '\xFF' | |
| 50 }; | |
| 51 | 49 |
| 52 GURL url; | 50 GURL url; |
| 53 | 51 |
| 54 SniffMimeType(buf, 0, url, type_hint, &mime_type); | 52 SniffMimeType(buf, 0, url, type_hint, &mime_type); |
| 55 EXPECT_EQ("text/plain", mime_type); | 53 EXPECT_EQ("text/plain", mime_type); |
| 56 SniffMimeType(buf, 1, url, type_hint, &mime_type); | 54 SniffMimeType(buf, 1, url, type_hint, &mime_type); |
| 57 EXPECT_EQ("text/plain", mime_type); | 55 EXPECT_EQ("text/plain", mime_type); |
| 58 SniffMimeType(buf, 2, url, type_hint, &mime_type); | 56 SniffMimeType(buf, 2, url, type_hint, &mime_type); |
| 59 EXPECT_EQ("application/octet-stream", mime_type); | 57 EXPECT_EQ("application/octet-stream", mime_type); |
| 60 } | 58 } |
| 61 | 59 |
| 62 TEST(MimeSnifferTest, BasicSniffingTest) { | 60 TEST(MimeSnifferTest, BasicSniffingTest) { |
| 63 SnifferTest tests[] = { | 61 SnifferTest tests[] = { |
| 64 { "<!DOCTYPE html PUBLIC", sizeof("<!DOCTYPE html PUBLIC")-1, | 62 {"<!DOCTYPE html PUBLIC", |
| 65 "http://www.example.com/", | 63 sizeof("<!DOCTYPE html PUBLIC") - 1, |
| 66 "", "text/html" }, | 64 "http://www.example.com/", |
| 67 { "<HtMl><Body></body></htMl>", sizeof("<HtMl><Body></body></htMl>")-1, | 65 "", |
| 68 "http://www.example.com/foo.gif", | 66 "text/html"}, |
| 69 "application/octet-stream", "application/octet-stream" }, | 67 {"<HtMl><Body></body></htMl>", |
| 70 { "GIF89a\x1F\x83\x94", sizeof("GIF89a\xAF\x83\x94")-1, | 68 sizeof("<HtMl><Body></body></htMl>") - 1, |
| 71 "http://www.example.com/foo", | 69 "http://www.example.com/foo.gif", |
| 72 "text/plain", "image/gif" }, | 70 "application/octet-stream", |
| 73 { "Gif87a\x1F\x83\x94", sizeof("Gif87a\xAF\x83\x94")-1, | 71 "application/octet-stream"}, |
| 74 "http://www.example.com/foo?param=tt.gif", | 72 {"GIF89a\x1F\x83\x94", |
| 75 "", "application/octet-stream" }, | 73 sizeof("GIF89a\xAF\x83\x94") - 1, |
| 76 { "%!PS-Adobe-3.0", sizeof("%!PS-Adobe-3.0")-1, | 74 "http://www.example.com/foo", |
| 77 "http://www.example.com/foo", | 75 "text/plain", |
| 78 "text/plain", "text/plain" }, | 76 "image/gif"}, |
| 79 { "\x89" "PNG\x0D\x0A\x1A\x0A", sizeof("\x89" "PNG\x0D\x0A\x1A\x0A")-1, | 77 {"Gif87a\x1F\x83\x94", |
| 80 "http://www.example.com/foo", | 78 sizeof("Gif87a\xAF\x83\x94") - 1, |
| 81 "application/octet-stream", "application/octet-stream" }, | 79 "http://www.example.com/foo?param=tt.gif", |
| 82 { "\xFF\xD8\xFF\x23\x49\xAF", sizeof("\xFF\xD8\xFF\x23\x49\xAF")-1, | 80 "", |
| 83 "http://www.example.com/foo", | 81 "application/octet-stream"}, |
| 84 "", "image/jpeg" }, | 82 {"%!PS-Adobe-3.0", |
| 83 sizeof("%!PS-Adobe-3.0") - 1, | |
| 84 "http://www.example.com/foo", | |
| 85 "text/plain", | |
| 86 "text/plain"}, | |
| 87 {"\x89" | |
| 88 "PNG\x0D\x0A\x1A\x0A", | |
| 89 sizeof( | |
| 90 "\x89" | |
| 91 "PNG\x0D\x0A\x1A\x0A") - | |
| 92 1, | |
|
davidben
2014/10/10 20:24:16
89 through 92 being on separate lines looks kinda
jkarlin
2014/10/31 00:46:30
Filed https://code.google.com/p/chromium/issues/de
jkarlin
2014/10/31 15:55:14
Bug resolution is to just merge the two string lit
| |
| 93 "http://www.example.com/foo", | |
| 94 "application/octet-stream", | |
| 95 "application/octet-stream"}, | |
| 96 {"\xFF\xD8\xFF\x23\x49\xAF", | |
| 97 sizeof("\xFF\xD8\xFF\x23\x49\xAF") - 1, | |
| 98 "http://www.example.com/foo", | |
| 99 "", | |
| 100 "image/jpeg"}, | |
| 85 }; | 101 }; |
| 86 | 102 |
| 87 TestArray(tests, arraysize(tests)); | 103 TestArray(tests, arraysize(tests)); |
| 88 } | 104 } |
| 89 | 105 |
| 90 TEST(MimeSnifferTest, ChromeExtensionsTest) { | 106 TEST(MimeSnifferTest, ChromeExtensionsTest) { |
| 91 SnifferTest tests[] = { | 107 SnifferTest tests[] = {// schemes |
|
davidben
2014/10/10 20:24:16
Probably worth a bug here too? Indenting all of th
| |
| 92 // schemes | 108 {"Cr24\x02\x00\x00\x00", |
| 93 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, | 109 sizeof("Cr24\x02\x00\x00\x00") - 1, |
| 94 "http://www.example.com/foo.crx", | 110 "http://www.example.com/foo.crx", |
| 95 "", "application/x-chrome-extension" }, | 111 "", |
| 96 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, | 112 "application/x-chrome-extension"}, |
| 97 "https://www.example.com/foo.crx", | 113 {"Cr24\x02\x00\x00\x00", |
| 98 "", "application/x-chrome-extension" }, | 114 sizeof("Cr24\x02\x00\x00\x00") - 1, |
| 99 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, | 115 "https://www.example.com/foo.crx", |
| 100 "ftp://www.example.com/foo.crx", | 116 "", |
| 101 "", "application/x-chrome-extension" }, | 117 "application/x-chrome-extension"}, |
| 118 {"Cr24\x02\x00\x00\x00", | |
| 119 sizeof("Cr24\x02\x00\x00\x00") - 1, | |
| 120 "ftp://www.example.com/foo.crx", | |
| 121 "", | |
| 122 "application/x-chrome-extension"}, | |
| 102 | 123 |
| 103 // some other mimetypes that should get converted | 124 // some other mimetypes that should get converted |
| 104 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, | 125 {"Cr24\x02\x00\x00\x00", |
| 105 "http://www.example.com/foo.crx", | 126 sizeof("Cr24\x02\x00\x00\x00") - 1, |
| 106 "text/plain", "application/x-chrome-extension" }, | 127 "http://www.example.com/foo.crx", |
| 107 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, | 128 "text/plain", |
| 108 "http://www.example.com/foo.crx", | 129 "application/x-chrome-extension"}, |
| 109 "application/octet-stream", "application/x-chrome-extension" }, | 130 {"Cr24\x02\x00\x00\x00", |
| 131 sizeof("Cr24\x02\x00\x00\x00") - 1, | |
| 132 "http://www.example.com/foo.crx", | |
| 133 "application/octet-stream", | |
| 134 "application/x-chrome-extension"}, | |
| 110 | 135 |
| 111 // success edge cases | 136 // success edge cases |
| 112 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, | 137 {"Cr24\x02\x00\x00\x00", |
| 113 "http://www.example.com/foo.crx?query=string", | 138 sizeof("Cr24\x02\x00\x00\x00") - 1, |
| 114 "", "application/x-chrome-extension" }, | 139 "http://www.example.com/foo.crx?query=string", |
| 115 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, | 140 "", |
| 116 "http://www.example.com/foo..crx", | 141 "application/x-chrome-extension"}, |
| 117 "", "application/x-chrome-extension" }, | 142 {"Cr24\x02\x00\x00\x00", |
| 143 sizeof("Cr24\x02\x00\x00\x00") - 1, | |
| 144 "http://www.example.com/foo..crx", | |
| 145 "", | |
| 146 "application/x-chrome-extension"}, | |
| 118 | 147 |
| 119 // wrong file extension | 148 // wrong file extension |
| 120 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, | 149 {"Cr24\x02\x00\x00\x00", |
| 121 "http://www.example.com/foo.bin", | 150 sizeof("Cr24\x02\x00\x00\x00") - 1, |
| 122 "", "application/octet-stream" }, | 151 "http://www.example.com/foo.bin", |
| 123 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, | 152 "", |
| 124 "http://www.example.com/foo.bin?monkey", | 153 "application/octet-stream"}, |
| 125 "", "application/octet-stream" }, | 154 {"Cr24\x02\x00\x00\x00", |
| 126 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, | 155 sizeof("Cr24\x02\x00\x00\x00") - 1, |
| 127 "invalid-url", | 156 "http://www.example.com/foo.bin?monkey", |
| 128 "", "application/octet-stream" }, | 157 "", |
| 129 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, | 158 "application/octet-stream"}, |
| 130 "http://www.example.com", | 159 {"Cr24\x02\x00\x00\x00", |
| 131 "", "application/octet-stream" }, | 160 sizeof("Cr24\x02\x00\x00\x00") - 1, |
| 132 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, | 161 "invalid-url", |
| 133 "http://www.example.com/", | 162 "", |
| 134 "", "application/octet-stream" }, | 163 "application/octet-stream"}, |
| 135 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, | 164 {"Cr24\x02\x00\x00\x00", |
| 136 "http://www.example.com/foo", | 165 sizeof("Cr24\x02\x00\x00\x00") - 1, |
| 137 "", "application/octet-stream" }, | 166 "http://www.example.com", |
| 138 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, | 167 "", |
| 139 "http://www.example.com/foocrx", | 168 "application/octet-stream"}, |
| 140 "", "application/octet-stream" }, | 169 {"Cr24\x02\x00\x00\x00", |
| 141 { "Cr24\x02\x00\x00\x00", sizeof("Cr24\x02\x00\x00\x00")-1, | 170 sizeof("Cr24\x02\x00\x00\x00") - 1, |
| 142 "http://www.example.com/foo.crx.blech", | 171 "http://www.example.com/", |
| 143 "", "application/octet-stream" }, | 172 "", |
| 173 "application/octet-stream"}, | |
| 174 {"Cr24\x02\x00\x00\x00", | |
| 175 sizeof("Cr24\x02\x00\x00\x00") - 1, | |
| 176 "http://www.example.com/foo", | |
| 177 "", | |
| 178 "application/octet-stream"}, | |
| 179 {"Cr24\x02\x00\x00\x00", | |
| 180 sizeof("Cr24\x02\x00\x00\x00") - 1, | |
| 181 "http://www.example.com/foocrx", | |
| 182 "", | |
| 183 "application/octet-stream"}, | |
| 184 {"Cr24\x02\x00\x00\x00", | |
| 185 sizeof("Cr24\x02\x00\x00\x00") - 1, | |
| 186 "http://www.example.com/foo.crx.blech", | |
| 187 "", | |
| 188 "application/octet-stream"}, | |
| 144 | 189 |
| 145 // wrong magic | 190 // wrong magic |
| 146 { "Cr24\x02\x00\x00\x01", sizeof("Cr24\x02\x00\x00\x01")-1, | 191 {"Cr24\x02\x00\x00\x01", |
| 147 "http://www.example.com/foo.crx?monkey", | 192 sizeof("Cr24\x02\x00\x00\x01") - 1, |
| 148 "", "application/octet-stream" }, | 193 "http://www.example.com/foo.crx?monkey", |
| 149 { "PADDING_Cr24\x02\x00\x00\x00", sizeof("PADDING_Cr24\x02\x00\x00\x00")-1, | 194 "", |
| 150 "http://www.example.com/foo.crx?monkey", | 195 "application/octet-stream"}, |
| 151 "", "application/octet-stream" }, | 196 {"PADDING_Cr24\x02\x00\x00\x00", |
| 197 sizeof("PADDING_Cr24\x02\x00\x00\x00") - 1, | |
| 198 "http://www.example.com/foo.crx?monkey", | |
| 199 "", | |
| 200 "application/octet-stream"}, | |
| 152 }; | 201 }; |
| 153 | 202 |
| 154 TestArray(tests, arraysize(tests)); | 203 TestArray(tests, arraysize(tests)); |
| 155 } | 204 } |
| 156 | 205 |
| 157 TEST(MimeSnifferTest, MozillaCompatibleTest) { | 206 TEST(MimeSnifferTest, MozillaCompatibleTest) { |
| 158 SnifferTest tests[] = { | 207 SnifferTest tests[] = { |
| 159 { " \n <hTmL>\n <hea", sizeof(" \n <hTmL>\n <hea")-1, | 208 {" \n <hTmL>\n <hea", |
| 160 "http://www.example.com/", | 209 sizeof(" \n <hTmL>\n <hea") - 1, |
| 161 "", "text/html" }, | 210 "http://www.example.com/", |
| 162 { " \n <hTmL>\n <hea", sizeof(" \n <hTmL>\n <hea")-1, | 211 "", |
| 163 "http://www.example.com/", | 212 "text/html"}, |
| 164 "text/plain", "text/plain" }, | 213 {" \n <hTmL>\n <hea", |
| 165 { "BMjlakdsfk", sizeof("BMjlakdsfk")-1, | 214 sizeof(" \n <hTmL>\n <hea") - 1, |
| 166 "http://www.example.com/foo", | 215 "http://www.example.com/", |
| 167 "", "image/bmp" }, | 216 "text/plain", |
| 168 { "\x00\x00\x30\x00", sizeof("\x00\x00\x30\x00")-1, | 217 "text/plain"}, |
| 169 "http://www.example.com/favicon.ico", | 218 {"BMjlakdsfk", |
| 170 "", "application/octet-stream" }, | 219 sizeof("BMjlakdsfk") - 1, |
| 171 { "#!/bin/sh\nls /\n", sizeof("#!/bin/sh\nls /\n")-1, | 220 "http://www.example.com/foo", |
| 172 "http://www.example.com/foo", | 221 "", |
| 173 "", "text/plain" }, | 222 "image/bmp"}, |
| 174 { "From: Fred\nTo: Bob\n\nHi\n.\n", | 223 {"\x00\x00\x30\x00", |
| 175 sizeof("From: Fred\nTo: Bob\n\nHi\n.\n")-1, | 224 sizeof("\x00\x00\x30\x00") - 1, |
| 176 "http://www.example.com/foo", | 225 "http://www.example.com/favicon.ico", |
| 177 "", "text/plain" }, | 226 "", |
| 178 { "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", | 227 "application/octet-stream"}, |
| 179 sizeof("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")-1, | 228 {"#!/bin/sh\nls /\n", |
| 180 "http://www.example.com/foo", | 229 sizeof("#!/bin/sh\nls /\n") - 1, |
| 181 "", "text/xml" }, | 230 "http://www.example.com/foo", |
| 182 { "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", | 231 "", |
| 183 sizeof("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")-1, | 232 "text/plain"}, |
| 184 "http://www.example.com/foo", | 233 {"From: Fred\nTo: Bob\n\nHi\n.\n", |
| 185 "application/octet-stream", "application/octet-stream" }, | 234 sizeof("From: Fred\nTo: Bob\n\nHi\n.\n") - 1, |
| 235 "http://www.example.com/foo", | |
| 236 "", | |
| 237 "text/plain"}, | |
| 238 {"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", | |
| 239 sizeof("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") - 1, | |
| 240 "http://www.example.com/foo", | |
| 241 "", | |
| 242 "text/xml"}, | |
| 243 {"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", | |
| 244 sizeof("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") - 1, | |
| 245 "http://www.example.com/foo", | |
| 246 "application/octet-stream", | |
| 247 "application/octet-stream"}, | |
| 186 }; | 248 }; |
| 187 | 249 |
| 188 TestArray(tests, arraysize(tests)); | 250 TestArray(tests, arraysize(tests)); |
| 189 } | 251 } |
| 190 | 252 |
| 191 TEST(MimeSnifferTest, DontAllowPrivilegeEscalationTest) { | 253 TEST(MimeSnifferTest, DontAllowPrivilegeEscalationTest) { |
| 192 SnifferTest tests[] = { | 254 SnifferTest tests[] = { |
| 193 { "GIF87a\n<html>\n<body>" | 255 {"GIF87a\n<html>\n<body>" |
| 194 "<script>alert('haxorzed');\n</script>" | 256 "<script>alert('haxorzed');\n</script>" |
| 195 "</body></html>\n", | 257 "</body></html>\n", |
| 196 sizeof("GIF87a\n<html>\n<body>" | 258 sizeof( |
| 197 "<script>alert('haxorzed');\n</script>" | 259 "GIF87a\n<html>\n<body>" |
| 198 "</body></html>\n")-1, | 260 "<script>alert('haxorzed');\n</script>" |
| 199 "http://www.example.com/foo", | 261 "</body></html>\n") - |
| 200 "", "image/gif" }, | 262 1, |
| 201 { "GIF87a\n<html>\n<body>" | 263 "http://www.example.com/foo", |
| 202 "<script>alert('haxorzed');\n</script>" | 264 "", |
| 203 "</body></html>\n", | 265 "image/gif"}, |
| 204 sizeof("GIF87a\n<html>\n<body>" | 266 {"GIF87a\n<html>\n<body>" |
| 205 "<script>alert('haxorzed');\n</script>" | 267 "<script>alert('haxorzed');\n</script>" |
| 206 "</body></html>\n")-1, | 268 "</body></html>\n", |
| 207 "http://www.example.com/foo?q=ttt.html", | 269 sizeof( |
| 208 "", "image/gif" }, | 270 "GIF87a\n<html>\n<body>" |
| 209 { "GIF87a\n<html>\n<body>" | 271 "<script>alert('haxorzed');\n</script>" |
| 210 "<script>alert('haxorzed');\n</script>" | 272 "</body></html>\n") - |
| 211 "</body></html>\n", | 273 1, |
| 212 sizeof("GIF87a\n<html>\n<body>" | 274 "http://www.example.com/foo?q=ttt.html", |
| 213 "<script>alert('haxorzed');\n</script>" | 275 "", |
| 214 "</body></html>\n")-1, | 276 "image/gif"}, |
| 215 "http://www.example.com/foo#ttt.html", | 277 {"GIF87a\n<html>\n<body>" |
| 216 "", "image/gif" }, | 278 "<script>alert('haxorzed');\n</script>" |
| 217 { "a\n<html>\n<body>" | 279 "</body></html>\n", |
| 218 "<script>alert('haxorzed');\n</script>" | 280 sizeof( |
| 219 "</body></html>\n", | 281 "GIF87a\n<html>\n<body>" |
| 220 sizeof("a\n<html>\n<body>" | 282 "<script>alert('haxorzed');\n</script>" |
| 221 "<script>alert('haxorzed');\n</script>" | 283 "</body></html>\n") - |
| 222 "</body></html>\n")-1, | 284 1, |
| 223 "http://www.example.com/foo", | 285 "http://www.example.com/foo#ttt.html", |
| 224 "", "text/plain" }, | 286 "", |
| 225 { "a\n<html>\n<body>" | 287 "image/gif"}, |
| 226 "<script>alert('haxorzed');\n</script>" | 288 {"a\n<html>\n<body>" |
| 227 "</body></html>\n", | 289 "<script>alert('haxorzed');\n</script>" |
| 228 sizeof("a\n<html>\n<body>" | 290 "</body></html>\n", |
| 229 "<script>alert('haxorzed');\n</script>" | 291 sizeof( |
| 230 "</body></html>\n")-1, | 292 "a\n<html>\n<body>" |
| 231 "http://www.example.com/foo?q=ttt.html", | 293 "<script>alert('haxorzed');\n</script>" |
| 232 "", "text/plain" }, | 294 "</body></html>\n") - |
| 233 { "a\n<html>\n<body>" | 295 1, |
| 234 "<script>alert('haxorzed');\n</script>" | 296 "http://www.example.com/foo", |
| 235 "</body></html>\n", | 297 "", |
| 236 sizeof("a\n<html>\n<body>" | 298 "text/plain"}, |
| 237 "<script>alert('haxorzed');\n</script>" | 299 {"a\n<html>\n<body>" |
| 238 "</body></html>\n")-1, | 300 "<script>alert('haxorzed');\n</script>" |
| 239 "http://www.example.com/foo#ttt.html", | 301 "</body></html>\n", |
| 240 "", "text/plain" }, | 302 sizeof( |
| 241 { "a\n<html>\n<body>" | 303 "a\n<html>\n<body>" |
| 242 "<script>alert('haxorzed');\n</script>" | 304 "<script>alert('haxorzed');\n</script>" |
| 243 "</body></html>\n", | 305 "</body></html>\n") - |
| 244 sizeof("a\n<html>\n<body>" | 306 1, |
| 245 "<script>alert('haxorzed');\n</script>" | 307 "http://www.example.com/foo?q=ttt.html", |
| 246 "</body></html>\n")-1, | 308 "", |
| 247 "http://www.example.com/foo.html", | 309 "text/plain"}, |
| 248 "", "text/plain" }, | 310 {"a\n<html>\n<body>" |
| 311 "<script>alert('haxorzed');\n</script>" | |
| 312 "</body></html>\n", | |
| 313 sizeof( | |
| 314 "a\n<html>\n<body>" | |
| 315 "<script>alert('haxorzed');\n</script>" | |
| 316 "</body></html>\n") - | |
| 317 1, | |
| 318 "http://www.example.com/foo#ttt.html", | |
| 319 "", | |
| 320 "text/plain"}, | |
| 321 {"a\n<html>\n<body>" | |
| 322 "<script>alert('haxorzed');\n</script>" | |
| 323 "</body></html>\n", | |
| 324 sizeof( | |
| 325 "a\n<html>\n<body>" | |
| 326 "<script>alert('haxorzed');\n</script>" | |
| 327 "</body></html>\n") - | |
| 328 1, | |
| 329 "http://www.example.com/foo.html", | |
| 330 "", | |
| 331 "text/plain"}, | |
| 249 }; | 332 }; |
| 250 | 333 |
| 251 TestArray(tests, arraysize(tests)); | 334 TestArray(tests, arraysize(tests)); |
| 252 } | 335 } |
| 253 | 336 |
| 254 TEST(MimeSnifferTest, UnicodeTest) { | 337 TEST(MimeSnifferTest, UnicodeTest) { |
| 255 SnifferTest tests[] = { | 338 SnifferTest tests[] = { |
| 256 { "\xEF\xBB\xBF" "Hi there", sizeof("\xEF\xBB\xBF" "Hi there")-1, | 339 {"\xEF\xBB\xBF" |
| 257 "http://www.example.com/foo", | 340 "Hi there", |
| 258 "", "text/plain" }, | 341 sizeof( |
| 259 { "\xEF\xBB\xBF\xED\x7A\xAD\x7A\x0D\x79", | 342 "\xEF\xBB\xBF" |
| 260 sizeof("\xEF\xBB\xBF\xED\x7A\xAD\x7A\x0D\x79")-1, | 343 "Hi there") - |
| 261 "http://www.example.com/foo", | 344 1, |
| 262 "", "text/plain" }, | 345 "http://www.example.com/foo", |
| 263 { "\xFE\xFF\xD0\xA5\xD0\xBE\xD0\xBB\xD1\x83\xD0\xB9", | 346 "", |
| 264 sizeof("\xFE\xFF\xD0\xA5\xD0\xBE\xD0\xBB\xD1\x83\xD0\xB9")-1, | 347 "text/plain"}, |
| 265 "http://www.example.com/foo", | 348 {"\xEF\xBB\xBF\xED\x7A\xAD\x7A\x0D\x79", |
| 266 "", "text/plain" }, | 349 sizeof("\xEF\xBB\xBF\xED\x7A\xAD\x7A\x0D\x79") - 1, |
| 267 { "\xFE\xFF\x00\x41\x00\x20\xD8\x00\xDC\x00\xD8\x00\xDC\x01", | 350 "http://www.example.com/foo", |
| 268 sizeof("\xFE\xFF\x00\x41\x00\x20\xD8\x00\xDC\x00\xD8\x00\xDC\x01")-1, | 351 "", |
| 269 "http://www.example.com/foo", | 352 "text/plain"}, |
| 270 "", "text/plain" }, | 353 {"\xFE\xFF\xD0\xA5\xD0\xBE\xD0\xBB\xD1\x83\xD0\xB9", |
| 354 sizeof("\xFE\xFF\xD0\xA5\xD0\xBE\xD0\xBB\xD1\x83\xD0\xB9") - 1, | |
| 355 "http://www.example.com/foo", | |
| 356 "", | |
| 357 "text/plain"}, | |
| 358 {"\xFE\xFF\x00\x41\x00\x20\xD8\x00\xDC\x00\xD8\x00\xDC\x01", | |
| 359 sizeof("\xFE\xFF\x00\x41\x00\x20\xD8\x00\xDC\x00\xD8\x00\xDC\x01") - 1, | |
| 360 "http://www.example.com/foo", | |
| 361 "", | |
| 362 "text/plain"}, | |
| 271 }; | 363 }; |
| 272 | 364 |
| 273 TestArray(tests, arraysize(tests)); | 365 TestArray(tests, arraysize(tests)); |
| 274 } | 366 } |
| 275 | 367 |
| 276 TEST(MimeSnifferTest, FlashTest) { | 368 TEST(MimeSnifferTest, FlashTest) { |
| 277 SnifferTest tests[] = { | 369 SnifferTest tests[] = { |
| 278 { "CWSdd\x00\xB3", sizeof("CWSdd\x00\xB3")-1, | 370 {"CWSdd\x00\xB3", |
| 279 "http://www.example.com/foo", | 371 sizeof("CWSdd\x00\xB3") - 1, |
| 280 "", "application/octet-stream" }, | 372 "http://www.example.com/foo", |
| 281 { "FLVjdkl*(#)0sdj\x00", sizeof("FLVjdkl*(#)0sdj\x00")-1, | 373 "", |
| 282 "http://www.example.com/foo?q=ttt.swf", | 374 "application/octet-stream"}, |
| 283 "", "application/octet-stream" }, | 375 {"FLVjdkl*(#)0sdj\x00", |
| 284 { "FWS3$9\r\b\x00", sizeof("FWS3$9\r\b\x00")-1, | 376 sizeof("FLVjdkl*(#)0sdj\x00") - 1, |
| 285 "http://www.example.com/foo#ttt.swf", | 377 "http://www.example.com/foo?q=ttt.swf", |
| 286 "", "application/octet-stream" }, | 378 "", |
| 287 { "FLVjdkl*(#)0sdj", sizeof("FLVjdkl*(#)0sdj")-1, | 379 "application/octet-stream"}, |
| 288 "http://www.example.com/foo.swf", | 380 {"FWS3$9\r\b\x00", |
| 289 "", "text/plain" }, | 381 sizeof("FWS3$9\r\b\x00") - 1, |
| 290 { "FLVjdkl*(#)0s\x01dj", sizeof("FLVjdkl*(#)0s\x01dj")-1, | 382 "http://www.example.com/foo#ttt.swf", |
| 291 "http://www.example.com/foo/bar.swf", | 383 "", |
| 292 "", "application/octet-stream" }, | 384 "application/octet-stream"}, |
| 293 { "FWS3$9\r\b\x1A", sizeof("FWS3$9\r\b\x1A")-1, | 385 {"FLVjdkl*(#)0sdj", |
| 294 "http://www.example.com/foo.swf?clickTAG=http://www.adnetwork.com/bar", | 386 sizeof("FLVjdkl*(#)0sdj") - 1, |
| 295 "", "application/octet-stream" }, | 387 "http://www.example.com/foo.swf", |
| 296 { "FWS3$9\r\x1C\b", sizeof("FWS3$9\r\x1C\b")-1, | 388 "", |
| 297 "http://www.example.com/foo.swf?clickTAG=http://www.adnetwork.com/bar", | 389 "text/plain"}, |
| 298 "text/plain", "application/octet-stream" }, | 390 {"FLVjdkl*(#)0s\x01dj", |
| 391 sizeof("FLVjdkl*(#)0s\x01dj") - 1, | |
| 392 "http://www.example.com/foo/bar.swf", | |
| 393 "", | |
| 394 "application/octet-stream"}, | |
| 395 {"FWS3$9\r\b\x1A", | |
| 396 sizeof("FWS3$9\r\b\x1A") - 1, | |
| 397 "http://www.example.com/foo.swf?clickTAG=http://www.adnetwork.com/bar", | |
| 398 "", | |
| 399 "application/octet-stream"}, | |
| 400 {"FWS3$9\r\x1C\b", | |
| 401 sizeof("FWS3$9\r\x1C\b") - 1, | |
| 402 "http://www.example.com/foo.swf?clickTAG=http://www.adnetwork.com/bar", | |
| 403 "text/plain", | |
| 404 "application/octet-stream"}, | |
| 299 }; | 405 }; |
| 300 | 406 |
| 301 TestArray(tests, arraysize(tests)); | 407 TestArray(tests, arraysize(tests)); |
| 302 } | 408 } |
| 303 | 409 |
| 304 TEST(MimeSnifferTest, XMLTest) { | 410 TEST(MimeSnifferTest, XMLTest) { |
| 305 // An easy feed to identify. | 411 // An easy feed to identify. |
| 306 EXPECT_EQ("application/atom+xml", | 412 EXPECT_EQ("application/atom+xml", |
| 307 SniffMimeType("<?xml?><feed", std::string(), "text/xml")); | 413 SniffMimeType("<?xml?><feed", std::string(), "text/xml")); |
| 308 // Don't sniff out of plain text. | 414 // Don't sniff out of plain text. |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 366 // Test content which is >= 1024 bytes, and includes no open angle bracket. | 472 // Test content which is >= 1024 bytes, and includes no open angle bracket. |
| 367 // http://code.google.com/p/chromium/issues/detail?id=3521 | 473 // http://code.google.com/p/chromium/issues/detail?id=3521 |
| 368 TEST(MimeSnifferTest, XMLTestLargeNoAngledBracket) { | 474 TEST(MimeSnifferTest, XMLTestLargeNoAngledBracket) { |
| 369 // Make a large input, with 1024 bytes of "x". | 475 // Make a large input, with 1024 bytes of "x". |
| 370 std::string content; | 476 std::string content; |
| 371 content.resize(1024); | 477 content.resize(1024); |
| 372 std::fill(content.begin(), content.end(), 'x'); | 478 std::fill(content.begin(), content.end(), 'x'); |
| 373 | 479 |
| 374 // content.size() >= 1024 so the sniff is unambiguous. | 480 // content.size() >= 1024 so the sniff is unambiguous. |
| 375 std::string mime_type; | 481 std::string mime_type; |
| 376 EXPECT_TRUE(SniffMimeType(content.data(), content.size(), GURL(), | 482 EXPECT_TRUE(SniffMimeType( |
| 377 "text/xml", &mime_type)); | 483 content.data(), content.size(), GURL(), "text/xml", &mime_type)); |
| 378 EXPECT_EQ("text/xml", mime_type); | 484 EXPECT_EQ("text/xml", mime_type); |
| 379 } | 485 } |
| 380 | 486 |
| 381 // Test content which is >= 1024 bytes, and includes a binary looking byte. | 487 // Test content which is >= 1024 bytes, and includes a binary looking byte. |
| 382 // http://code.google.com/p/chromium/issues/detail?id=15314 | 488 // http://code.google.com/p/chromium/issues/detail?id=15314 |
| 383 TEST(MimeSnifferTest, LooksBinary) { | 489 TEST(MimeSnifferTest, LooksBinary) { |
| 384 // Make a large input, with 1024 bytes of "x" and 1 byte of 0x01. | 490 // Make a large input, with 1024 bytes of "x" and 1 byte of 0x01. |
| 385 std::string content; | 491 std::string content; |
| 386 content.resize(1024); | 492 content.resize(1024); |
| 387 std::fill(content.begin(), content.end(), 'x'); | 493 std::fill(content.begin(), content.end(), 'x'); |
| 388 content[1000] = 0x01; | 494 content[1000] = 0x01; |
| 389 | 495 |
| 390 // content.size() >= 1024 so the sniff is unambiguous. | 496 // content.size() >= 1024 so the sniff is unambiguous. |
| 391 std::string mime_type; | 497 std::string mime_type; |
| 392 EXPECT_TRUE(SniffMimeType(content.data(), content.size(), GURL(), | 498 EXPECT_TRUE(SniffMimeType( |
| 393 "text/plain", &mime_type)); | 499 content.data(), content.size(), GURL(), "text/plain", &mime_type)); |
| 394 EXPECT_EQ("application/octet-stream", mime_type); | 500 EXPECT_EQ("application/octet-stream", mime_type); |
| 395 } | 501 } |
| 396 | 502 |
| 397 TEST(MimeSnifferTest, OfficeTest) { | 503 TEST(MimeSnifferTest, OfficeTest) { |
| 398 SnifferTest tests[] = { | 504 SnifferTest tests[] = { |
| 399 // Check for URLs incorrectly reported as Microsoft Office files. | 505 // Check for URLs incorrectly reported as Microsoft Office files. |
| 400 { "Hi there", | 506 {"Hi there", |
| 401 sizeof("Hi there")-1, | 507 sizeof("Hi there") - 1, |
| 402 "http://www.example.com/foo.doc", | 508 "http://www.example.com/foo.doc", |
| 403 "application/msword", "application/octet-stream" }, | 509 "application/msword", |
| 404 { "Hi there", | 510 "application/octet-stream"}, |
| 405 sizeof("Hi there")-1, | 511 {"Hi there", |
| 406 "http://www.example.com/foo.xls", | 512 sizeof("Hi there") - 1, |
| 407 "application/vnd.ms-excel", "application/octet-stream" }, | 513 "http://www.example.com/foo.xls", |
| 408 { "Hi there", | 514 "application/vnd.ms-excel", |
| 409 sizeof("Hi there")-1, | 515 "application/octet-stream"}, |
| 410 "http://www.example.com/foo.ppt", | 516 {"Hi there", |
| 411 "application/vnd.ms-powerpoint", "application/octet-stream" }, | 517 sizeof("Hi there") - 1, |
| 412 // Check for Microsoft Office files incorrectly reported as text. | 518 "http://www.example.com/foo.ppt", |
| 413 { "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1" "Hi there", | 519 "application/vnd.ms-powerpoint", |
| 414 sizeof("\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1" "Hi there")-1, | 520 "application/octet-stream"}, |
| 415 "http://www.example.com/foo.doc", | 521 // Check for Microsoft Office files incorrectly reported as text. |
| 416 "text/plain", "application/msword" }, | 522 {"\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1" |
| 417 { "PK\x03\x04" "Hi there", | 523 "Hi there", |
| 418 sizeof("PK\x03\x04" "Hi there")-1, | 524 sizeof( |
| 419 "http://www.example.com/foo.doc", | 525 "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1" |
| 420 "text/plain", | 526 "Hi there") - |
| 421 "application/vnd.openxmlformats-officedocument." | 527 1, |
| 422 "wordprocessingml.document" }, | 528 "http://www.example.com/foo.doc", |
| 423 { "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1" "Hi there", | 529 "text/plain", |
| 424 sizeof("\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1" "Hi there")-1, | 530 "application/msword"}, |
| 425 "http://www.example.com/foo.xls", | 531 {"PK\x03\x04" |
| 426 "text/plain", "application/vnd.ms-excel" }, | 532 "Hi there", |
| 427 { "PK\x03\x04" "Hi there", | 533 sizeof( |
| 428 sizeof("PK\x03\x04" "Hi there")-1, | 534 "PK\x03\x04" |
| 429 "http://www.example.com/foo.xls", | 535 "Hi there") - |
| 430 "text/plain", | 536 1, |
| 431 "application/vnd.openxmlformats-officedocument." | 537 "http://www.example.com/foo.doc", |
| 432 "spreadsheetml.sheet" }, | 538 "text/plain", |
| 433 { "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1" "Hi there", | 539 "application/vnd.openxmlformats-officedocument." |
| 434 sizeof("\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1" "Hi there")-1, | 540 "wordprocessingml.document"}, |
| 435 "http://www.example.com/foo.ppt", | 541 {"\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1" |
| 436 "text/plain", "application/vnd.ms-powerpoint" }, | 542 "Hi there", |
| 437 { "PK\x03\x04" "Hi there", | 543 sizeof( |
| 438 sizeof("PK\x03\x04" "Hi there")-1, | 544 "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1" |
| 439 "http://www.example.com/foo.ppt", | 545 "Hi there") - |
| 440 "text/plain", | 546 1, |
| 441 "application/vnd.openxmlformats-officedocument." | 547 "http://www.example.com/foo.xls", |
| 442 "presentationml.presentation" }, | 548 "text/plain", |
| 549 "application/vnd.ms-excel"}, | |
| 550 {"PK\x03\x04" | |
| 551 "Hi there", | |
| 552 sizeof( | |
| 553 "PK\x03\x04" | |
| 554 "Hi there") - | |
| 555 1, | |
| 556 "http://www.example.com/foo.xls", | |
| 557 "text/plain", | |
| 558 "application/vnd.openxmlformats-officedocument." | |
| 559 "spreadsheetml.sheet"}, | |
| 560 {"\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1" | |
| 561 "Hi there", | |
| 562 sizeof( | |
| 563 "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1" | |
| 564 "Hi there") - | |
| 565 1, | |
| 566 "http://www.example.com/foo.ppt", | |
| 567 "text/plain", | |
| 568 "application/vnd.ms-powerpoint"}, | |
| 569 {"PK\x03\x04" | |
| 570 "Hi there", | |
| 571 sizeof( | |
| 572 "PK\x03\x04" | |
| 573 "Hi there") - | |
| 574 1, | |
| 575 "http://www.example.com/foo.ppt", | |
| 576 "text/plain", | |
| 577 "application/vnd.openxmlformats-officedocument." | |
| 578 "presentationml.presentation"}, | |
| 443 }; | 579 }; |
| 444 | 580 |
| 445 TestArray(tests, arraysize(tests)); | 581 TestArray(tests, arraysize(tests)); |
| 446 } | 582 } |
| 447 | 583 |
| 448 // TODO(thestig) Add more tests for other AV formats. Add another test case for | 584 // TODO(thestig) Add more tests for other AV formats. Add another test case for |
| 449 // RAW images. | 585 // RAW images. |
| 450 TEST(MimeSnifferTest, AudioVideoTest) { | 586 TEST(MimeSnifferTest, AudioVideoTest) { |
| 451 std::string mime_type; | 587 std::string mime_type; |
| 452 const char kFlacTestData[] = | 588 const char kFlacTestData[] = |
| 453 "fLaC\x00\x00\x00\x22\x12\x00\x12\x00\x00\x00\x00\x00"; | 589 "fLaC\x00\x00\x00\x22\x12\x00\x12\x00\x00\x00\x00\x00"; |
| 454 EXPECT_TRUE(SniffMimeTypeFromLocalData(kFlacTestData, | 590 EXPECT_TRUE(SniffMimeTypeFromLocalData( |
| 455 sizeof(kFlacTestData), | 591 kFlacTestData, sizeof(kFlacTestData), &mime_type)); |
| 456 &mime_type)); | |
| 457 EXPECT_EQ("audio/x-flac", mime_type); | 592 EXPECT_EQ("audio/x-flac", mime_type); |
| 458 mime_type.clear(); | 593 mime_type.clear(); |
| 459 | 594 |
| 460 const char kWMATestData[] = | 595 const char kWMATestData[] = |
| 461 "\x30\x26\xb2\x75\x8e\x66\xcf\x11\xa6\xd9\x00\xaa\x00\x62\xce\x6c"; | 596 "\x30\x26\xb2\x75\x8e\x66\xcf\x11\xa6\xd9\x00\xaa\x00\x62\xce\x6c"; |
| 462 EXPECT_TRUE(SniffMimeTypeFromLocalData(kWMATestData, | 597 EXPECT_TRUE(SniffMimeTypeFromLocalData( |
| 463 sizeof(kWMATestData), | 598 kWMATestData, sizeof(kWMATestData), &mime_type)); |
| 464 &mime_type)); | |
| 465 EXPECT_EQ("video/x-ms-asf", mime_type); | 599 EXPECT_EQ("video/x-ms-asf", mime_type); |
| 466 mime_type.clear(); | 600 mime_type.clear(); |
| 467 | 601 |
| 468 // mp4a, m4b, m4p, and alac extension files which share the same container | 602 // mp4a, m4b, m4p, and alac extension files which share the same container |
| 469 // format. | 603 // format. |
| 470 const char kMP4TestData[] = | 604 const char kMP4TestData[] = |
| 471 "\x00\x00\x00\x20\x66\x74\x79\x70\x4d\x34\x41\x20\x00\x00\x00\x00"; | 605 "\x00\x00\x00\x20\x66\x74\x79\x70\x4d\x34\x41\x20\x00\x00\x00\x00"; |
| 472 EXPECT_TRUE(SniffMimeTypeFromLocalData(kMP4TestData, | 606 EXPECT_TRUE(SniffMimeTypeFromLocalData( |
| 473 sizeof(kMP4TestData), | 607 kMP4TestData, sizeof(kMP4TestData), &mime_type)); |
| 474 &mime_type)); | |
| 475 EXPECT_EQ("video/mp4", mime_type); | 608 EXPECT_EQ("video/mp4", mime_type); |
| 476 mime_type.clear(); | 609 mime_type.clear(); |
| 477 | 610 |
| 478 const char kAACTestData[] = | 611 const char kAACTestData[] = |
| 479 "\xff\xf1\x50\x80\x02\x20\xb0\x23\x0a\x83\x20\x7d\x61\x90\x3e\xb1"; | 612 "\xff\xf1\x50\x80\x02\x20\xb0\x23\x0a\x83\x20\x7d\x61\x90\x3e\xb1"; |
| 480 EXPECT_TRUE(SniffMimeTypeFromLocalData(kAACTestData, | 613 EXPECT_TRUE(SniffMimeTypeFromLocalData( |
| 481 sizeof(kAACTestData), | 614 kAACTestData, sizeof(kAACTestData), &mime_type)); |
| 482 &mime_type)); | |
| 483 EXPECT_EQ("audio/mpeg", mime_type); | 615 EXPECT_EQ("audio/mpeg", mime_type); |
| 484 mime_type.clear(); | 616 mime_type.clear(); |
| 485 } | 617 } |
| 486 | 618 |
| 487 } // namespace net | 619 } // namespace net |
| OLD | NEW |