OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/strings/string_split.h" | 6 #include "base/strings/string_split.h" |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "net/base/mime_util.h" | 8 #include "net/base/mime_util.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
11 namespace net { | 11 namespace net { |
12 | 12 |
13 TEST(MimeUtilTest, ExtensionTest) { | 13 TEST(MimeUtilTest, ExtensionTest) { |
14 const struct { | 14 const struct { |
15 const base::FilePath::CharType* extension; | 15 const base::FilePath::CharType* extension; |
16 const char* mime_type; | 16 const char* mime_type; |
17 bool valid; | 17 bool valid; |
18 } tests[] = { | 18 } tests[] = { |
19 { FILE_PATH_LITERAL("png"), "image/png", true }, | 19 {FILE_PATH_LITERAL("png"), "image/png", true}, |
20 { FILE_PATH_LITERAL("css"), "text/css", true }, | 20 {FILE_PATH_LITERAL("css"), "text/css", true}, |
21 { FILE_PATH_LITERAL("pjp"), "image/jpeg", true }, | 21 {FILE_PATH_LITERAL("pjp"), "image/jpeg", true}, |
22 { FILE_PATH_LITERAL("pjpeg"), "image/jpeg", true }, | 22 {FILE_PATH_LITERAL("pjpeg"), "image/jpeg", true}, |
23 { FILE_PATH_LITERAL("not an extension / for sure"), "", false }, | 23 {FILE_PATH_LITERAL("not an extension / for sure"), "", false}, |
24 }; | 24 }; |
mmenke
2014/10/10 18:12:39
This (And all cases like it) are pretty hideous.
| |
25 | 25 |
26 std::string mime_type; | 26 std::string mime_type; |
27 bool rv; | 27 bool rv; |
28 | 28 |
29 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 29 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
30 rv = GetMimeTypeFromExtension(tests[i].extension, &mime_type); | 30 rv = GetMimeTypeFromExtension(tests[i].extension, &mime_type); |
31 EXPECT_EQ(tests[i].valid, rv); | 31 EXPECT_EQ(tests[i].valid, rv); |
32 if (rv) | 32 if (rv) |
33 EXPECT_EQ(tests[i].mime_type, mime_type); | 33 EXPECT_EQ(tests[i].mime_type, mime_type); |
34 } | 34 } |
35 } | 35 } |
36 | 36 |
37 TEST(MimeUtilTest, FileTest) { | 37 TEST(MimeUtilTest, FileTest) { |
38 const struct { | 38 const struct { |
39 const base::FilePath::CharType* file_path; | 39 const base::FilePath::CharType* file_path; |
40 const char* mime_type; | 40 const char* mime_type; |
41 bool valid; | 41 bool valid; |
42 } tests[] = { | 42 } tests[] = { |
43 { FILE_PATH_LITERAL("c:\\foo\\bar.css"), "text/css", true }, | 43 {FILE_PATH_LITERAL("c:\\foo\\bar.css"), "text/css", true}, |
44 { FILE_PATH_LITERAL("c:\\blah"), "", false }, | 44 {FILE_PATH_LITERAL("c:\\blah"), "", false}, |
45 { FILE_PATH_LITERAL("/usr/local/bin/mplayer"), "", false }, | 45 {FILE_PATH_LITERAL("/usr/local/bin/mplayer"), "", false}, |
46 { FILE_PATH_LITERAL("/home/foo/bar.css"), "text/css", true }, | 46 {FILE_PATH_LITERAL("/home/foo/bar.css"), "text/css", true}, |
47 { FILE_PATH_LITERAL("/blah."), "", false }, | 47 {FILE_PATH_LITERAL("/blah."), "", false}, |
48 { FILE_PATH_LITERAL("c:\\blah."), "", false }, | 48 {FILE_PATH_LITERAL("c:\\blah."), "", false}, |
49 }; | 49 }; |
50 | 50 |
51 std::string mime_type; | 51 std::string mime_type; |
52 bool rv; | 52 bool rv; |
53 | 53 |
54 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 54 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
55 rv = GetMimeTypeFromFile(base::FilePath(tests[i].file_path), | 55 rv = GetMimeTypeFromFile(base::FilePath(tests[i].file_path), &mime_type); |
56 &mime_type); | |
57 EXPECT_EQ(tests[i].valid, rv); | 56 EXPECT_EQ(tests[i].valid, rv); |
58 if (rv) | 57 if (rv) |
59 EXPECT_EQ(tests[i].mime_type, mime_type); | 58 EXPECT_EQ(tests[i].mime_type, mime_type); |
60 } | 59 } |
61 } | 60 } |
62 | 61 |
63 TEST(MimeUtilTest, LookupTypes) { | 62 TEST(MimeUtilTest, LookupTypes) { |
64 EXPECT_FALSE(IsUnsupportedTextMimeType("text/banana")); | 63 EXPECT_FALSE(IsUnsupportedTextMimeType("text/banana")); |
65 EXPECT_TRUE(IsUnsupportedTextMimeType("text/vcard")); | 64 EXPECT_TRUE(IsUnsupportedTextMimeType("text/vcard")); |
66 | 65 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
123 EXPECT_FALSE(IsStrictMediaMimeType("audio/unknown")); | 122 EXPECT_FALSE(IsStrictMediaMimeType("audio/unknown")); |
124 EXPECT_FALSE(IsStrictMediaMimeType("application/unknown")); | 123 EXPECT_FALSE(IsStrictMediaMimeType("application/unknown")); |
125 EXPECT_FALSE(IsStrictMediaMimeType("unknown/unknown")); | 124 EXPECT_FALSE(IsStrictMediaMimeType("unknown/unknown")); |
126 } | 125 } |
127 | 126 |
128 TEST(MimeUtilTest, MatchesMimeType) { | 127 TEST(MimeUtilTest, MatchesMimeType) { |
129 EXPECT_TRUE(MatchesMimeType("*", "video/x-mpeg")); | 128 EXPECT_TRUE(MatchesMimeType("*", "video/x-mpeg")); |
130 EXPECT_TRUE(MatchesMimeType("video/*", "video/x-mpeg")); | 129 EXPECT_TRUE(MatchesMimeType("video/*", "video/x-mpeg")); |
131 EXPECT_TRUE(MatchesMimeType("video/*", "video/*")); | 130 EXPECT_TRUE(MatchesMimeType("video/*", "video/*")); |
132 EXPECT_TRUE(MatchesMimeType("video/x-mpeg", "video/x-mpeg")); | 131 EXPECT_TRUE(MatchesMimeType("video/x-mpeg", "video/x-mpeg")); |
133 EXPECT_TRUE(MatchesMimeType("application/*+xml", | 132 EXPECT_TRUE(MatchesMimeType("application/*+xml", "application/html+xml")); |
134 "application/html+xml")); | |
135 EXPECT_TRUE(MatchesMimeType("application/*+xml", "application/+xml")); | 133 EXPECT_TRUE(MatchesMimeType("application/*+xml", "application/+xml")); |
136 EXPECT_TRUE(MatchesMimeType("application/*+json", | 134 EXPECT_TRUE( |
137 "application/x-myformat+json")); | 135 MatchesMimeType("application/*+json", "application/x-myformat+json")); |
138 EXPECT_TRUE(MatchesMimeType("aaa*aaa", "aaaaaa")); | 136 EXPECT_TRUE(MatchesMimeType("aaa*aaa", "aaaaaa")); |
139 EXPECT_TRUE(MatchesMimeType("*", std::string())); | 137 EXPECT_TRUE(MatchesMimeType("*", std::string())); |
140 EXPECT_FALSE(MatchesMimeType("video/", "video/x-mpeg")); | 138 EXPECT_FALSE(MatchesMimeType("video/", "video/x-mpeg")); |
141 EXPECT_FALSE(MatchesMimeType(std::string(), "video/x-mpeg")); | 139 EXPECT_FALSE(MatchesMimeType(std::string(), "video/x-mpeg")); |
142 EXPECT_FALSE(MatchesMimeType(std::string(), std::string())); | 140 EXPECT_FALSE(MatchesMimeType(std::string(), std::string())); |
143 EXPECT_FALSE(MatchesMimeType("video/x-mpeg", std::string())); | 141 EXPECT_FALSE(MatchesMimeType("video/x-mpeg", std::string())); |
144 EXPECT_FALSE(MatchesMimeType("application/*+xml", "application/xml")); | 142 EXPECT_FALSE(MatchesMimeType("application/*+xml", "application/xml")); |
145 EXPECT_FALSE(MatchesMimeType("application/*+xml", | 143 EXPECT_FALSE(MatchesMimeType("application/*+xml", "application/html+xmlz")); |
146 "application/html+xmlz")); | 144 EXPECT_FALSE(MatchesMimeType("application/*+xml", "applcation/html+xml")); |
147 EXPECT_FALSE(MatchesMimeType("application/*+xml", | |
148 "applcation/html+xml")); | |
149 EXPECT_FALSE(MatchesMimeType("aaa*aaa", "aaaaa")); | 145 EXPECT_FALSE(MatchesMimeType("aaa*aaa", "aaaaa")); |
150 | 146 |
151 EXPECT_TRUE(MatchesMimeType("*", "video/x-mpeg;param=val")); | 147 EXPECT_TRUE(MatchesMimeType("*", "video/x-mpeg;param=val")); |
152 EXPECT_TRUE(MatchesMimeType("video/*", "video/x-mpeg;param=val")); | 148 EXPECT_TRUE(MatchesMimeType("video/*", "video/x-mpeg;param=val")); |
153 EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/mpeg")); | 149 EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/mpeg")); |
154 EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/mpeg;param=other")); | 150 EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/mpeg;param=other")); |
155 EXPECT_TRUE(MatchesMimeType("video/*;param=val", "video/mpeg;param=val")); | 151 EXPECT_TRUE(MatchesMimeType("video/*;param=val", "video/mpeg;param=val")); |
156 EXPECT_TRUE(MatchesMimeType("video/x-mpeg", "video/x-mpeg;param=val")); | 152 EXPECT_TRUE(MatchesMimeType("video/x-mpeg", "video/x-mpeg;param=val")); |
157 EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param=val", | 153 EXPECT_TRUE( |
158 "video/x-mpeg;param=val")); | 154 MatchesMimeType("video/x-mpeg;param=val", "video/x-mpeg;param=val")); |
159 EXPECT_FALSE(MatchesMimeType("video/x-mpeg;param2=val2", | 155 EXPECT_FALSE( |
160 "video/x-mpeg;param=val")); | 156 MatchesMimeType("video/x-mpeg;param2=val2", "video/x-mpeg;param=val")); |
161 EXPECT_FALSE(MatchesMimeType("video/x-mpeg;param2=val2", | 157 EXPECT_FALSE( |
162 "video/x-mpeg;param2=val")); | 158 MatchesMimeType("video/x-mpeg;param2=val2", "video/x-mpeg;param2=val")); |
163 EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param=val", | 159 EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param=val", |
164 "video/x-mpeg;param=val;param2=val2")); | 160 "video/x-mpeg;param=val;param2=val2")); |
165 EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param=val;param2=val2", | 161 EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param=val;param2=val2", |
166 "video/x-mpeg;param=val;param2=val2")); | 162 "video/x-mpeg;param=val;param2=val2")); |
167 EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param2=val2;param=val", | 163 EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param2=val2;param=val", |
168 "video/x-mpeg;param=val;param2=val2")); | 164 "video/x-mpeg;param=val;param2=val2")); |
169 EXPECT_FALSE(MatchesMimeType("video/x-mpeg;param3=val3;param=val", | 165 EXPECT_FALSE(MatchesMimeType("video/x-mpeg;param3=val3;param=val", |
170 "video/x-mpeg;param=val;param2=val2")); | 166 "video/x-mpeg;param=val;param2=val2")); |
171 EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param=val ;param2=val2 ", | 167 EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param=val ;param2=val2 ", |
172 "video/x-mpeg;param=val;param2=val2")); | 168 "video/x-mpeg;param=val;param2=val2")); |
(...skipping 17 matching lines...) Expand all Loading... | |
190 } | 186 } |
191 | 187 |
192 // Note: codecs should only be a list of 2 or fewer; hence the restriction of | 188 // Note: codecs should only be a list of 2 or fewer; hence the restriction of |
193 // results' length to 2. | 189 // results' length to 2. |
194 TEST(MimeUtilTest, ParseCodecString) { | 190 TEST(MimeUtilTest, ParseCodecString) { |
195 const struct { | 191 const struct { |
196 const char* original; | 192 const char* original; |
197 size_t expected_size; | 193 size_t expected_size; |
198 const char* results[2]; | 194 const char* results[2]; |
199 } tests[] = { | 195 } tests[] = { |
200 { "\"bogus\"", 1, { "bogus" } }, | 196 {"\"bogus\"", 1, {"bogus"}}, |
201 { "0", 1, { "0" } }, | 197 {"0", 1, {"0"}}, |
202 { "avc1.42E01E, mp4a.40.2", 2, { "avc1", "mp4a" } }, | 198 {"avc1.42E01E, mp4a.40.2", 2, {"avc1", "mp4a"}}, |
203 { "\"mp4v.20.240, mp4a.40.2\"", 2, { "mp4v", "mp4a" } }, | 199 {"\"mp4v.20.240, mp4a.40.2\"", 2, {"mp4v", "mp4a"}}, |
204 { "mp4v.20.8, samr", 2, { "mp4v", "samr" } }, | 200 {"mp4v.20.8, samr", 2, {"mp4v", "samr"}}, |
205 { "\"theora, vorbis\"", 2, { "theora", "vorbis" } }, | 201 {"\"theora, vorbis\"", 2, {"theora", "vorbis"}}, |
206 { "", 0, { } }, | 202 {"", 0, {}}, |
207 { "\"\"", 0, { } }, | 203 {"\"\"", 0, {}}, |
208 { "\" \"", 0, { } }, | 204 {"\" \"", 0, {}}, |
209 { ",", 2, { "", "" } }, | 205 {",", 2, {"", ""}}, |
210 }; | 206 }; |
211 | 207 |
212 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 208 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
213 std::vector<std::string> codecs_out; | 209 std::vector<std::string> codecs_out; |
214 ParseCodecString(tests[i].original, &codecs_out, true); | 210 ParseCodecString(tests[i].original, &codecs_out, true); |
215 ASSERT_EQ(tests[i].expected_size, codecs_out.size()); | 211 ASSERT_EQ(tests[i].expected_size, codecs_out.size()); |
216 for (size_t j = 0; j < tests[i].expected_size; ++j) | 212 for (size_t j = 0; j < tests[i].expected_size; ++j) |
217 EXPECT_EQ(tests[i].results[j], codecs_out[j]); | 213 EXPECT_EQ(tests[i].results[j], codecs_out[j]); |
218 } | 214 } |
219 | 215 |
220 // Test without stripping the codec type. | 216 // Test without stripping the codec type. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
278 EXPECT_EQ("text", GetIANAMediaType("text/plain")); | 274 EXPECT_EQ("text", GetIANAMediaType("text/plain")); |
279 EXPECT_EQ("video", GetIANAMediaType("video/H261")); | 275 EXPECT_EQ("video", GetIANAMediaType("video/H261")); |
280 } | 276 } |
281 | 277 |
282 TEST(MimeUtilTest, TestGetExtensionsForMimeType) { | 278 TEST(MimeUtilTest, TestGetExtensionsForMimeType) { |
283 const struct { | 279 const struct { |
284 const char* mime_type; | 280 const char* mime_type; |
285 size_t min_expected_size; | 281 size_t min_expected_size; |
286 const char* contained_result; | 282 const char* contained_result; |
287 } tests[] = { | 283 } tests[] = { |
288 { "text/plain", 2, "txt" }, | 284 {"text/plain", 2, "txt"}, |
289 { "*", 0, NULL }, | 285 {"*", 0, NULL}, |
290 { "message/*", 1, "eml" }, | 286 {"message/*", 1, "eml"}, |
291 { "MeSsAge/*", 1, "eml" }, | 287 {"MeSsAge/*", 1, "eml"}, |
292 { "image/bmp", 1, "bmp" }, | 288 {"image/bmp", 1, "bmp"}, |
293 { "video/*", 6, "mp4" }, | 289 {"video/*", 6, "mp4"}, |
294 #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_IOS) | 290 #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_IOS) |
295 { "video/*", 6, "mpg" }, | 291 {"video/*", 6, "mpg"}, |
296 #else | 292 #else |
297 { "video/*", 6, "mpeg" }, | 293 {"video/*", 6, "mpeg"}, |
298 #endif | 294 #endif |
299 { "audio/*", 6, "oga" }, | 295 {"audio/*", 6, "oga"}, |
300 { "aUDIo/*", 6, "wav" }, | 296 {"aUDIo/*", 6, "wav"}, |
301 }; | 297 }; |
302 | 298 |
303 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 299 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
304 std::vector<base::FilePath::StringType> extensions; | 300 std::vector<base::FilePath::StringType> extensions; |
305 GetExtensionsForMimeType(tests[i].mime_type, &extensions); | 301 GetExtensionsForMimeType(tests[i].mime_type, &extensions); |
306 ASSERT_TRUE(tests[i].min_expected_size <= extensions.size()); | 302 ASSERT_TRUE(tests[i].min_expected_size <= extensions.size()); |
307 | 303 |
308 if (!tests[i].contained_result) | 304 if (!tests[i].contained_result) |
309 continue; | 305 continue; |
310 | 306 |
(...skipping 25 matching lines...) Expand all Loading... | |
336 EXPECT_EQ(CERTIFICATE_MIME_TYPE_UNKNOWN, | 332 EXPECT_EQ(CERTIFICATE_MIME_TYPE_UNKNOWN, |
337 GetCertificateMimeTypeForMimeType("application/x-x509-ca-cert")); | 333 GetCertificateMimeTypeForMimeType("application/x-x509-ca-cert")); |
338 EXPECT_EQ(CERTIFICATE_MIME_TYPE_UNKNOWN, | 334 EXPECT_EQ(CERTIFICATE_MIME_TYPE_UNKNOWN, |
339 GetCertificateMimeTypeForMimeType("application/x-pkcs12")); | 335 GetCertificateMimeTypeForMimeType("application/x-pkcs12")); |
340 #endif | 336 #endif |
341 EXPECT_EQ(CERTIFICATE_MIME_TYPE_UNKNOWN, | 337 EXPECT_EQ(CERTIFICATE_MIME_TYPE_UNKNOWN, |
342 GetCertificateMimeTypeForMimeType("text/plain")); | 338 GetCertificateMimeTypeForMimeType("text/plain")); |
343 } | 339 } |
344 | 340 |
345 TEST(MimeUtilTest, TestAddMultipartValueForUpload) { | 341 TEST(MimeUtilTest, TestAddMultipartValueForUpload) { |
346 const char* ref_output = "--boundary\r\nContent-Disposition: form-data;" | 342 const char* ref_output = |
347 " name=\"value name\"\r\nContent-Type: content type" | 343 "--boundary\r\nContent-Disposition: form-data;" |
348 "\r\n\r\nvalue\r\n" | 344 " name=\"value name\"\r\nContent-Type: content type" |
349 "--boundary\r\nContent-Disposition: form-data;" | 345 "\r\n\r\nvalue\r\n" |
350 " name=\"value name\"\r\n\r\nvalue\r\n" | 346 "--boundary\r\nContent-Disposition: form-data;" |
351 "--boundary--\r\n"; | 347 " name=\"value name\"\r\n\r\nvalue\r\n" |
348 "--boundary--\r\n"; | |
352 std::string post_data; | 349 std::string post_data; |
353 AddMultipartValueForUpload("value name", "value", "boundary", | 350 AddMultipartValueForUpload( |
354 "content type", &post_data); | 351 "value name", "value", "boundary", "content type", &post_data); |
355 AddMultipartValueForUpload("value name", "value", "boundary", | 352 AddMultipartValueForUpload("value name", "value", "boundary", "", &post_data); |
356 "", &post_data); | |
357 AddMultipartFinalDelimiterForUpload("boundary", &post_data); | 353 AddMultipartFinalDelimiterForUpload("boundary", &post_data); |
358 EXPECT_STREQ(ref_output, post_data.c_str()); | 354 EXPECT_STREQ(ref_output, post_data.c_str()); |
359 } | 355 } |
360 | 356 |
361 } // namespace net | 357 } // namespace net |
OLD | NEW |