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
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
6 #include "base/strings/string_split.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "net/base/mime_util.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 #if defined(OS_ANDROID)
12 #include "base/android/build_info.h"
17 TEST(MimeUtilTest
, ExtensionTest
) {
19 const base::FilePath::CharType
* extension
;
20 const char* const mime_type
;
23 { FILE_PATH_LITERAL("png"), "image/png", true },
24 { FILE_PATH_LITERAL("css"), "text/css", true },
25 { FILE_PATH_LITERAL("pjp"), "image/jpeg", true },
26 { FILE_PATH_LITERAL("pjpeg"), "image/jpeg", true },
27 #if defined(OS_ANDROID)
28 { FILE_PATH_LITERAL("m3u8"), "application/x-mpegurl", true },
30 { FILE_PATH_LITERAL("not an extension / for sure"), "", false },
33 std::string mime_type
;
36 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
37 rv
= GetMimeTypeFromExtension(tests
[i
].extension
, &mime_type
);
38 EXPECT_EQ(tests
[i
].valid
, rv
);
40 EXPECT_EQ(tests
[i
].mime_type
, mime_type
);
44 TEST(MimeUtilTest
, FileTest
) {
46 const base::FilePath::CharType
* file_path
;
47 const char* const mime_type
;
50 { FILE_PATH_LITERAL("c:\\foo\\bar.css"), "text/css", true },
51 { FILE_PATH_LITERAL("c:\\blah"), "", false },
52 { FILE_PATH_LITERAL("/usr/local/bin/mplayer"), "", false },
53 { FILE_PATH_LITERAL("/home/foo/bar.css"), "text/css", true },
54 { FILE_PATH_LITERAL("/blah."), "", false },
55 { FILE_PATH_LITERAL("c:\\blah."), "", false },
58 std::string mime_type
;
61 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
62 rv
= GetMimeTypeFromFile(base::FilePath(tests
[i
].file_path
),
64 EXPECT_EQ(tests
[i
].valid
, rv
);
66 EXPECT_EQ(tests
[i
].mime_type
, mime_type
);
70 TEST(MimeUtilTest
, LookupTypes
) {
71 EXPECT_FALSE(IsUnsupportedTextMimeType("text/banana"));
72 EXPECT_TRUE(IsUnsupportedTextMimeType("text/vcard"));
74 EXPECT_TRUE(IsSupportedImageMimeType("image/jpeg"));
75 EXPECT_FALSE(IsSupportedImageMimeType("image/lolcat"));
76 EXPECT_TRUE(IsSupportedNonImageMimeType("text/html"));
77 EXPECT_TRUE(IsSupportedNonImageMimeType("text/css"));
78 EXPECT_TRUE(IsSupportedNonImageMimeType("text/"));
79 EXPECT_TRUE(IsSupportedNonImageMimeType("text/banana"));
80 EXPECT_FALSE(IsSupportedNonImageMimeType("text/vcard"));
81 EXPECT_FALSE(IsSupportedNonImageMimeType("application/virus"));
82 EXPECT_TRUE(IsSupportedNonImageMimeType("application/x-x509-user-cert"));
83 EXPECT_TRUE(IsSupportedNonImageMimeType("application/json"));
84 EXPECT_TRUE(IsSupportedNonImageMimeType("application/+json"));
85 EXPECT_TRUE(IsSupportedNonImageMimeType("application/x-suggestions+json"));
86 EXPECT_TRUE(IsSupportedNonImageMimeType("application/x-s+json;x=2"));
87 #if defined(OS_ANDROID)
88 EXPECT_TRUE(IsSupportedNonImageMimeType("application/x-x509-ca-cert"));
89 EXPECT_TRUE(IsSupportedNonImageMimeType("application/x-pkcs12"));
90 EXPECT_TRUE(IsSupportedMediaMimeType("application/vnd.apple.mpegurl"));
91 EXPECT_TRUE(IsSupportedMediaMimeType("application/x-mpegurl"));
94 EXPECT_TRUE(IsSupportedMimeType("image/jpeg"));
95 EXPECT_FALSE(IsSupportedMimeType("image/lolcat"));
96 EXPECT_TRUE(IsSupportedMimeType("text/html"));
97 EXPECT_TRUE(IsSupportedMimeType("text/banana"));
98 EXPECT_FALSE(IsSupportedMimeType("text/vcard"));
99 EXPECT_FALSE(IsSupportedMimeType("application/virus"));
100 EXPECT_FALSE(IsSupportedMimeType("application/x-json"));
101 EXPECT_FALSE(IsSupportedNonImageMimeType("application/vnd.doc;x=y+json"));
104 TEST(MimeUtilTest
, StrictMediaMimeType
) {
105 EXPECT_TRUE(IsStrictMediaMimeType("video/webm"));
106 EXPECT_TRUE(IsStrictMediaMimeType("audio/webm"));
108 EXPECT_TRUE(IsStrictMediaMimeType("audio/wav"));
109 EXPECT_TRUE(IsStrictMediaMimeType("audio/x-wav"));
111 EXPECT_TRUE(IsStrictMediaMimeType("video/ogg"));
112 EXPECT_TRUE(IsStrictMediaMimeType("audio/ogg"));
113 EXPECT_TRUE(IsStrictMediaMimeType("application/ogg"));
115 EXPECT_TRUE(IsStrictMediaMimeType("audio/mpeg"));
116 EXPECT_TRUE(IsStrictMediaMimeType("audio/mp3"));
117 EXPECT_TRUE(IsStrictMediaMimeType("audio/x-mp3"));
119 EXPECT_TRUE(IsStrictMediaMimeType("video/mp4"));
120 EXPECT_TRUE(IsStrictMediaMimeType("video/x-m4v"));
121 EXPECT_TRUE(IsStrictMediaMimeType("audio/mp4"));
122 EXPECT_TRUE(IsStrictMediaMimeType("audio/x-m4a"));
124 EXPECT_TRUE(IsStrictMediaMimeType("application/x-mpegurl"));
125 EXPECT_TRUE(IsStrictMediaMimeType("application/vnd.apple.mpegurl"));
127 EXPECT_FALSE(IsStrictMediaMimeType("video/unknown"));
128 EXPECT_FALSE(IsStrictMediaMimeType("audio/unknown"));
129 EXPECT_FALSE(IsStrictMediaMimeType("application/unknown"));
130 EXPECT_FALSE(IsStrictMediaMimeType("unknown/unknown"));
133 TEST(MimeUtilTest
, MatchesMimeType
) {
134 EXPECT_TRUE(MatchesMimeType("*", "video/x-mpeg"));
135 EXPECT_TRUE(MatchesMimeType("video/*", "video/x-mpeg"));
136 EXPECT_TRUE(MatchesMimeType("video/*", "video/*"));
137 EXPECT_TRUE(MatchesMimeType("video/x-mpeg", "video/x-mpeg"));
138 EXPECT_TRUE(MatchesMimeType("application/*+xml",
139 "application/html+xml"));
140 EXPECT_TRUE(MatchesMimeType("application/*+xml", "application/+xml"));
141 EXPECT_TRUE(MatchesMimeType("application/*+json",
142 "application/x-myformat+json"));
143 EXPECT_TRUE(MatchesMimeType("aaa*aaa", "aaaaaa"));
144 EXPECT_TRUE(MatchesMimeType("*", std::string()));
145 EXPECT_FALSE(MatchesMimeType("video/", "video/x-mpeg"));
146 EXPECT_FALSE(MatchesMimeType(std::string(), "video/x-mpeg"));
147 EXPECT_FALSE(MatchesMimeType(std::string(), std::string()));
148 EXPECT_FALSE(MatchesMimeType("video/x-mpeg", std::string()));
149 EXPECT_FALSE(MatchesMimeType("application/*+xml", "application/xml"));
150 EXPECT_FALSE(MatchesMimeType("application/*+xml",
151 "application/html+xmlz"));
152 EXPECT_FALSE(MatchesMimeType("application/*+xml",
153 "applcation/html+xml"));
154 EXPECT_FALSE(MatchesMimeType("aaa*aaa", "aaaaa"));
156 EXPECT_TRUE(MatchesMimeType("*", "video/x-mpeg;param=val"));
157 EXPECT_TRUE(MatchesMimeType("video/*", "video/x-mpeg;param=val"));
158 EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/mpeg"));
159 EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/mpeg;param=other"));
160 EXPECT_TRUE(MatchesMimeType("video/*;param=val", "video/mpeg;param=val"));
161 EXPECT_TRUE(MatchesMimeType("video/x-mpeg", "video/x-mpeg;param=val"));
162 EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param=val",
163 "video/x-mpeg;param=val"));
164 EXPECT_FALSE(MatchesMimeType("video/x-mpeg;param2=val2",
165 "video/x-mpeg;param=val"));
166 EXPECT_FALSE(MatchesMimeType("video/x-mpeg;param2=val2",
167 "video/x-mpeg;param2=val"));
168 EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param=val",
169 "video/x-mpeg;param=val;param2=val2"));
170 EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param=val;param2=val2",
171 "video/x-mpeg;param=val;param2=val2"));
172 EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param2=val2;param=val",
173 "video/x-mpeg;param=val;param2=val2"));
174 EXPECT_FALSE(MatchesMimeType("video/x-mpeg;param3=val3;param=val",
175 "video/x-mpeg;param=val;param2=val2"));
176 EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param=val ;param2=val2 ",
177 "video/x-mpeg;param=val;param2=val2"));
179 EXPECT_TRUE(MatchesMimeType("*/*;param=val", "video/x-mpeg;param=val"));
180 EXPECT_FALSE(MatchesMimeType("*/*;param=val", "video/x-mpeg;param=val2"));
182 EXPECT_TRUE(MatchesMimeType("*", "*"));
183 EXPECT_TRUE(MatchesMimeType("*", "*/*"));
184 EXPECT_TRUE(MatchesMimeType("*/*", "*/*"));
185 EXPECT_TRUE(MatchesMimeType("*/*", "*"));
186 EXPECT_TRUE(MatchesMimeType("video/*", "video/*"));
187 EXPECT_FALSE(MatchesMimeType("video/*", "*/*"));
188 EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/*"));
189 EXPECT_TRUE(MatchesMimeType("video/*;param=val", "video/*;param=val"));
190 EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/*;param=val2"));
192 EXPECT_TRUE(MatchesMimeType("ab*cd", "abxxxcd"));
193 EXPECT_TRUE(MatchesMimeType("ab*cd", "abx/xcd"));
194 EXPECT_TRUE(MatchesMimeType("ab/*cd", "ab/xxxcd"));
197 TEST(MimeUtilTest
, CommonMediaMimeType
) {
198 #if defined(OS_ANDROID)
200 if (base::android::BuildInfo::GetInstance()->sdk_int() < 14)
201 HLSSupported
= false;
206 EXPECT_TRUE(IsSupportedMediaMimeType("audio/webm"));
207 EXPECT_TRUE(IsSupportedMediaMimeType("video/webm"));
209 EXPECT_TRUE(IsSupportedMediaMimeType("audio/wav"));
210 EXPECT_TRUE(IsSupportedMediaMimeType("audio/x-wav"));
212 EXPECT_TRUE(IsSupportedMediaMimeType("audio/ogg"));
213 EXPECT_TRUE(IsSupportedMediaMimeType("application/ogg"));
214 #if defined(OS_ANDROID)
215 EXPECT_FALSE(IsSupportedMediaMimeType("video/ogg"));
216 EXPECT_EQ(HLSSupported
, IsSupportedMediaMimeType("application/x-mpegurl"));
217 EXPECT_EQ(HLSSupported
,
218 IsSupportedMediaMimeType("application/vnd.apple.mpegurl"));
220 EXPECT_TRUE(IsSupportedMediaMimeType("video/ogg"));
221 EXPECT_FALSE(IsSupportedMediaMimeType("application/x-mpegurl"));
222 EXPECT_FALSE(IsSupportedMediaMimeType("application/vnd.apple.mpegurl"));
225 #if defined(USE_PROPRIETARY_CODECS)
226 EXPECT_TRUE(IsSupportedMediaMimeType("audio/mp4"));
227 EXPECT_TRUE(IsSupportedMediaMimeType("audio/x-m4a"));
228 EXPECT_TRUE(IsSupportedMediaMimeType("video/mp4"));
229 EXPECT_TRUE(IsSupportedMediaMimeType("video/x-m4v"));
231 EXPECT_TRUE(IsSupportedMediaMimeType("audio/mp3"));
232 EXPECT_TRUE(IsSupportedMediaMimeType("audio/x-mp3"));
233 EXPECT_TRUE(IsSupportedMediaMimeType("audio/mpeg"));
234 EXPECT_TRUE(IsSupportedMediaMimeType("audio/aac"));
236 #if defined(ENABLE_MPEG2TS_STREAM_PARSER)
237 EXPECT_TRUE(IsSupportedMediaMimeType("video/mp2t"));
239 EXPECT_FALSE(IsSupportedMediaMimeType("video/mp2t"));
242 EXPECT_FALSE(IsSupportedMediaMimeType("audio/mp4"));
243 EXPECT_FALSE(IsSupportedMediaMimeType("audio/x-m4a"));
244 EXPECT_FALSE(IsSupportedMediaMimeType("video/mp4"));
245 EXPECT_FALSE(IsSupportedMediaMimeType("video/x-m4v"));
247 EXPECT_FALSE(IsSupportedMediaMimeType("audio/mp3"));
248 EXPECT_FALSE(IsSupportedMediaMimeType("audio/x-mp3"));
249 EXPECT_FALSE(IsSupportedMediaMimeType("audio/mpeg"));
250 EXPECT_FALSE(IsSupportedMediaMimeType("audio/aac"));
251 #endif // USE_PROPRIETARY_CODECS
252 EXPECT_FALSE(IsSupportedMediaMimeType("video/mp3"));
254 EXPECT_FALSE(IsSupportedMediaMimeType("video/unknown"));
255 EXPECT_FALSE(IsSupportedMediaMimeType("audio/unknown"));
256 EXPECT_FALSE(IsSupportedMediaMimeType("unknown/unknown"));
259 // Note: codecs should only be a list of 2 or fewer; hence the restriction of
260 // results' length to 2.
261 TEST(MimeUtilTest
, ParseCodecString
) {
263 const char* const original
;
264 size_t expected_size
;
265 const char* const results
[2];
267 { "\"bogus\"", 1, { "bogus" } },
269 { "avc1.42E01E, mp4a.40.2", 2, { "avc1", "mp4a" } },
270 { "\"mp4v.20.240, mp4a.40.2\"", 2, { "mp4v", "mp4a" } },
271 { "mp4v.20.8, samr", 2, { "mp4v", "samr" } },
272 { "\"theora, vorbis\"", 2, { "theora", "vorbis" } },
276 { ",", 2, { "", "" } },
279 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
280 std::vector
<std::string
> codecs_out
;
281 ParseCodecString(tests
[i
].original
, &codecs_out
, true);
282 ASSERT_EQ(tests
[i
].expected_size
, codecs_out
.size());
283 for (size_t j
= 0; j
< tests
[i
].expected_size
; ++j
)
284 EXPECT_EQ(tests
[i
].results
[j
], codecs_out
[j
]);
287 // Test without stripping the codec type.
288 std::vector
<std::string
> codecs_out
;
289 ParseCodecString("avc1.42E01E, mp4a.40.2", &codecs_out
, false);
290 ASSERT_EQ(2u, codecs_out
.size());
291 EXPECT_EQ("avc1.42E01E", codecs_out
[0]);
292 EXPECT_EQ("mp4a.40.2", codecs_out
[1]);
295 TEST(MimeUtilTest
, TestParseMimeTypeWithoutParameter
) {
296 std::string
nonAscii("application/nonutf8");
297 EXPECT_TRUE(ParseMimeTypeWithoutParameter(nonAscii
, NULL
, NULL
));
299 nonAscii
.append(base::WideToUTF8(L
"\u2603"));
301 nonAscii
.append("\u2603"); // unicode snowman
303 EXPECT_FALSE(ParseMimeTypeWithoutParameter(nonAscii
, NULL
, NULL
));
305 std::string top_level_type
;
307 EXPECT_TRUE(ParseMimeTypeWithoutParameter(
308 "application/mime", &top_level_type
, &subtype
));
309 EXPECT_EQ("application", top_level_type
);
310 EXPECT_EQ("mime", subtype
);
312 // Various allowed subtype forms.
313 EXPECT_TRUE(ParseMimeTypeWithoutParameter("application/json", NULL
, NULL
));
314 EXPECT_TRUE(ParseMimeTypeWithoutParameter(
315 "application/x-suggestions+json", NULL
, NULL
));
316 EXPECT_TRUE(ParseMimeTypeWithoutParameter("application/+json", NULL
, NULL
));
318 // Upper case letters are allowed.
319 EXPECT_TRUE(ParseMimeTypeWithoutParameter("text/mime", NULL
, NULL
));
320 EXPECT_TRUE(ParseMimeTypeWithoutParameter("TEXT/mime", NULL
, NULL
));
321 EXPECT_TRUE(ParseMimeTypeWithoutParameter("Text/mime", NULL
, NULL
));
322 EXPECT_TRUE(ParseMimeTypeWithoutParameter("TeXt/mime", NULL
, NULL
));
324 // Experimental types are also considered to be valid.
325 EXPECT_TRUE(ParseMimeTypeWithoutParameter("x-video/mime", NULL
, NULL
));
326 EXPECT_TRUE(ParseMimeTypeWithoutParameter("X-Video/mime", NULL
, NULL
));
328 EXPECT_FALSE(ParseMimeTypeWithoutParameter("text", NULL
, NULL
));
329 EXPECT_FALSE(ParseMimeTypeWithoutParameter("text/", NULL
, NULL
));
330 EXPECT_FALSE(ParseMimeTypeWithoutParameter("text/ ", NULL
, NULL
));
331 EXPECT_FALSE(ParseMimeTypeWithoutParameter("te(xt/ ", NULL
, NULL
));
332 EXPECT_FALSE(ParseMimeTypeWithoutParameter("text/()plain", NULL
, NULL
));
334 EXPECT_FALSE(ParseMimeTypeWithoutParameter("x-video", NULL
, NULL
));
335 EXPECT_FALSE(ParseMimeTypeWithoutParameter("x-video/", NULL
, NULL
));
337 EXPECT_FALSE(ParseMimeTypeWithoutParameter("application/a/b/c", NULL
, NULL
));
339 //EXPECT_TRUE(ParseMimeTypeWithoutParameter("video/mime;parameter"));
342 TEST(MimeUtilTest
, TestIsValidTopLevelMimeType
) {
343 EXPECT_TRUE(IsValidTopLevelMimeType("application"));
344 EXPECT_TRUE(IsValidTopLevelMimeType("audio"));
345 EXPECT_TRUE(IsValidTopLevelMimeType("example"));
346 EXPECT_TRUE(IsValidTopLevelMimeType("image"));
347 EXPECT_TRUE(IsValidTopLevelMimeType("message"));
348 EXPECT_TRUE(IsValidTopLevelMimeType("model"));
349 EXPECT_TRUE(IsValidTopLevelMimeType("multipart"));
350 EXPECT_TRUE(IsValidTopLevelMimeType("text"));
351 EXPECT_TRUE(IsValidTopLevelMimeType("video"));
353 EXPECT_TRUE(IsValidTopLevelMimeType("TEXT"));
354 EXPECT_TRUE(IsValidTopLevelMimeType("Text"));
355 EXPECT_TRUE(IsValidTopLevelMimeType("TeXt"));
357 EXPECT_FALSE(IsValidTopLevelMimeType("mime"));
358 EXPECT_FALSE(IsValidTopLevelMimeType(""));
359 EXPECT_FALSE(IsValidTopLevelMimeType("/"));
360 EXPECT_FALSE(IsValidTopLevelMimeType(" "));
362 EXPECT_TRUE(IsValidTopLevelMimeType("x-video"));
363 EXPECT_TRUE(IsValidTopLevelMimeType("X-video"));
365 EXPECT_FALSE(IsValidTopLevelMimeType("x-"));
368 TEST(MimeUtilTest
, TestGetExtensionsForMimeType
) {
370 const char* const mime_type
;
371 size_t min_expected_size
;
372 const char* const contained_result
;
374 { "text/plain", 2, "txt" },
376 { "message/*", 1, "eml" },
377 { "MeSsAge/*", 1, "eml" },
378 { "image/bmp", 1, "bmp" },
379 { "video/*", 6, "mp4" },
380 #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_IOS)
381 { "video/*", 6, "mpg" },
383 { "video/*", 6, "mpeg" },
385 { "audio/*", 6, "oga" },
386 { "aUDIo/*", 6, "wav" },
389 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
390 std::vector
<base::FilePath::StringType
> extensions
;
391 GetExtensionsForMimeType(tests
[i
].mime_type
, &extensions
);
392 ASSERT_TRUE(tests
[i
].min_expected_size
<= extensions
.size());
394 if (!tests
[i
].contained_result
)
398 for (size_t j
= 0; !found
&& j
< extensions
.size(); ++j
) {
400 if (extensions
[j
] == base::UTF8ToWide(tests
[i
].contained_result
))
403 if (extensions
[j
] == tests
[i
].contained_result
)
407 ASSERT_TRUE(found
) << "Must find at least the contained result within "
408 << tests
[i
].mime_type
;
412 TEST(MimeUtilTest
, TestGetCertificateMimeTypeForMimeType
) {
413 EXPECT_EQ(CERTIFICATE_MIME_TYPE_X509_USER_CERT
,
414 GetCertificateMimeTypeForMimeType("application/x-x509-user-cert"));
415 #if defined(OS_ANDROID)
416 // Only Android supports CA Certs and PKCS12 archives.
417 EXPECT_EQ(CERTIFICATE_MIME_TYPE_X509_CA_CERT
,
418 GetCertificateMimeTypeForMimeType("application/x-x509-ca-cert"));
419 EXPECT_EQ(CERTIFICATE_MIME_TYPE_PKCS12_ARCHIVE
,
420 GetCertificateMimeTypeForMimeType("application/x-pkcs12"));
422 EXPECT_EQ(CERTIFICATE_MIME_TYPE_UNKNOWN
,
423 GetCertificateMimeTypeForMimeType("application/x-x509-ca-cert"));
424 EXPECT_EQ(CERTIFICATE_MIME_TYPE_UNKNOWN
,
425 GetCertificateMimeTypeForMimeType("application/x-pkcs12"));
427 EXPECT_EQ(CERTIFICATE_MIME_TYPE_UNKNOWN
,
428 GetCertificateMimeTypeForMimeType("text/plain"));
431 TEST(MimeUtilTest
, TestAddMultipartValueForUpload
) {
432 const char ref_output
[] =
433 "--boundary\r\nContent-Disposition: form-data;"
434 " name=\"value name\"\r\nContent-Type: content type"
436 "--boundary\r\nContent-Disposition: form-data;"
437 " name=\"value name\"\r\n\r\nvalue\r\n"
439 std::string post_data
;
440 AddMultipartValueForUpload("value name", "value", "boundary",
441 "content type", &post_data
);
442 AddMultipartValueForUpload("value name", "value", "boundary",
444 AddMultipartFinalDelimiterForUpload("boundary", &post_data
);
445 EXPECT_STREQ(ref_output
, post_data
.c_str());