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 "build/build_config.h" |
8 #include "net/base/mime_util.h" | 9 #include "net/base/mime_util.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
10 | 11 |
11 #if defined(OS_ANDROID) | |
12 #include "base/android/build_info.h" | |
13 #endif | |
14 | |
15 namespace net { | 12 namespace net { |
16 | 13 |
17 TEST(MimeUtilTest, ExtensionTest) { | 14 TEST(MimeUtilTest, ExtensionTest) { |
18 const struct { | 15 const struct { |
19 const base::FilePath::CharType* extension; | 16 const base::FilePath::CharType* extension; |
20 const char* const mime_type; | 17 const char* const mime_type; |
21 bool valid; | 18 bool valid; |
22 } tests[] = { | 19 } tests[] = { |
23 {FILE_PATH_LITERAL("png"), "image/png", true}, | 20 {FILE_PATH_LITERAL("png"), "image/png", true}, |
24 {FILE_PATH_LITERAL("PNG"), "image/png", true}, | 21 {FILE_PATH_LITERAL("PNG"), "image/png", true}, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 | 59 |
63 for (size_t i = 0; i < arraysize(tests); ++i) { | 60 for (size_t i = 0; i < arraysize(tests); ++i) { |
64 rv = GetMimeTypeFromFile(base::FilePath(tests[i].file_path), | 61 rv = GetMimeTypeFromFile(base::FilePath(tests[i].file_path), |
65 &mime_type); | 62 &mime_type); |
66 EXPECT_EQ(tests[i].valid, rv); | 63 EXPECT_EQ(tests[i].valid, rv); |
67 if (rv) | 64 if (rv) |
68 EXPECT_EQ(tests[i].mime_type, mime_type); | 65 EXPECT_EQ(tests[i].mime_type, mime_type); |
69 } | 66 } |
70 } | 67 } |
71 | 68 |
72 TEST(MimeUtilTest, LookupTypes) { | |
73 #if defined(OS_ANDROID) | |
74 EXPECT_TRUE(IsSupportedMediaMimeType("application/vnd.apple.mpegurl")); | |
75 EXPECT_TRUE(IsSupportedMediaMimeType("application/x-mpegurl")); | |
76 EXPECT_TRUE(IsSupportedMediaMimeType("Application/X-MPEGURL")); | |
77 #endif | |
78 } | |
79 | |
80 TEST(MimeUtilTest, StrictMediaMimeType) { | |
81 EXPECT_TRUE(IsStrictMediaMimeType("video/webm")); | |
82 EXPECT_TRUE(IsStrictMediaMimeType("Video/WEBM")); | |
83 EXPECT_TRUE(IsStrictMediaMimeType("audio/webm")); | |
84 | |
85 EXPECT_TRUE(IsStrictMediaMimeType("audio/wav")); | |
86 EXPECT_TRUE(IsStrictMediaMimeType("audio/x-wav")); | |
87 | |
88 EXPECT_TRUE(IsStrictMediaMimeType("video/ogg")); | |
89 EXPECT_TRUE(IsStrictMediaMimeType("audio/ogg")); | |
90 EXPECT_TRUE(IsStrictMediaMimeType("application/ogg")); | |
91 | |
92 EXPECT_TRUE(IsStrictMediaMimeType("audio/mpeg")); | |
93 EXPECT_TRUE(IsStrictMediaMimeType("audio/mp3")); | |
94 EXPECT_TRUE(IsStrictMediaMimeType("audio/x-mp3")); | |
95 | |
96 EXPECT_TRUE(IsStrictMediaMimeType("video/mp4")); | |
97 EXPECT_TRUE(IsStrictMediaMimeType("video/x-m4v")); | |
98 EXPECT_TRUE(IsStrictMediaMimeType("audio/mp4")); | |
99 EXPECT_TRUE(IsStrictMediaMimeType("audio/x-m4a")); | |
100 | |
101 EXPECT_TRUE(IsStrictMediaMimeType("application/x-mpegurl")); | |
102 EXPECT_TRUE(IsStrictMediaMimeType("application/vnd.apple.mpegurl")); | |
103 | |
104 EXPECT_FALSE(IsStrictMediaMimeType("video/unknown")); | |
105 EXPECT_FALSE(IsStrictMediaMimeType("Video/UNKNOWN")); | |
106 EXPECT_FALSE(IsStrictMediaMimeType("audio/unknown")); | |
107 EXPECT_FALSE(IsStrictMediaMimeType("application/unknown")); | |
108 EXPECT_FALSE(IsStrictMediaMimeType("unknown/unknown")); | |
109 } | |
110 | |
111 TEST(MimeUtilTest, MatchesMimeType) { | 69 TEST(MimeUtilTest, MatchesMimeType) { |
112 // MIME types are case insensitive. | 70 // MIME types are case insensitive. |
113 EXPECT_TRUE(MatchesMimeType("VIDEO/*", "video/x-mpeg")); | 71 EXPECT_TRUE(MatchesMimeType("VIDEO/*", "video/x-mpeg")); |
114 EXPECT_TRUE(MatchesMimeType("video/*", "VIDEO/X-MPEG")); | 72 EXPECT_TRUE(MatchesMimeType("video/*", "VIDEO/X-MPEG")); |
115 | 73 |
116 EXPECT_TRUE(MatchesMimeType("*", "video/x-mpeg")); | 74 EXPECT_TRUE(MatchesMimeType("*", "video/x-mpeg")); |
117 EXPECT_TRUE(MatchesMimeType("video/*", "video/x-mpeg")); | 75 EXPECT_TRUE(MatchesMimeType("video/*", "video/x-mpeg")); |
118 EXPECT_TRUE(MatchesMimeType("video/*", "video/*")); | 76 EXPECT_TRUE(MatchesMimeType("video/*", "video/*")); |
119 EXPECT_TRUE(MatchesMimeType("video/x-mpeg", "video/x-mpeg")); | 77 EXPECT_TRUE(MatchesMimeType("video/x-mpeg", "video/x-mpeg")); |
120 EXPECT_TRUE(MatchesMimeType("application/*+xml", | 78 EXPECT_TRUE(MatchesMimeType("application/*+xml", |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 EXPECT_FALSE(MatchesMimeType("video/*", "*/*")); | 138 EXPECT_FALSE(MatchesMimeType("video/*", "*/*")); |
181 EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/*")); | 139 EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/*")); |
182 EXPECT_TRUE(MatchesMimeType("video/*;param=val", "video/*;param=val")); | 140 EXPECT_TRUE(MatchesMimeType("video/*;param=val", "video/*;param=val")); |
183 EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/*;param=val2")); | 141 EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/*;param=val2")); |
184 | 142 |
185 EXPECT_TRUE(MatchesMimeType("ab*cd", "abxxxcd")); | 143 EXPECT_TRUE(MatchesMimeType("ab*cd", "abxxxcd")); |
186 EXPECT_TRUE(MatchesMimeType("ab*cd", "abx/xcd")); | 144 EXPECT_TRUE(MatchesMimeType("ab*cd", "abx/xcd")); |
187 EXPECT_TRUE(MatchesMimeType("ab/*cd", "ab/xxxcd")); | 145 EXPECT_TRUE(MatchesMimeType("ab/*cd", "ab/xxxcd")); |
188 } | 146 } |
189 | 147 |
190 TEST(MimeUtilTest, CommonMediaMimeType) { | |
191 #if defined(OS_ANDROID) | |
192 bool HLSSupported; | |
193 if (base::android::BuildInfo::GetInstance()->sdk_int() < 14) | |
194 HLSSupported = false; | |
195 else | |
196 HLSSupported = true; | |
197 #endif | |
198 | |
199 EXPECT_TRUE(IsSupportedMediaMimeType("audio/webm")); | |
200 EXPECT_TRUE(IsSupportedMediaMimeType("video/webm")); | |
201 | |
202 EXPECT_TRUE(IsSupportedMediaMimeType("audio/wav")); | |
203 EXPECT_TRUE(IsSupportedMediaMimeType("audio/x-wav")); | |
204 | |
205 EXPECT_TRUE(IsSupportedMediaMimeType("audio/ogg")); | |
206 EXPECT_TRUE(IsSupportedMediaMimeType("application/ogg")); | |
207 #if defined(OS_ANDROID) | |
208 EXPECT_FALSE(IsSupportedMediaMimeType("video/ogg")); | |
209 EXPECT_EQ(HLSSupported, IsSupportedMediaMimeType("application/x-mpegurl")); | |
210 EXPECT_EQ(HLSSupported, | |
211 IsSupportedMediaMimeType("application/vnd.apple.mpegurl")); | |
212 #else | |
213 EXPECT_TRUE(IsSupportedMediaMimeType("video/ogg")); | |
214 EXPECT_FALSE(IsSupportedMediaMimeType("application/x-mpegurl")); | |
215 EXPECT_FALSE(IsSupportedMediaMimeType("application/vnd.apple.mpegurl")); | |
216 #endif // OS_ANDROID | |
217 | |
218 #if defined(USE_PROPRIETARY_CODECS) | |
219 EXPECT_TRUE(IsSupportedMediaMimeType("audio/mp4")); | |
220 EXPECT_TRUE(IsSupportedMediaMimeType("audio/x-m4a")); | |
221 EXPECT_TRUE(IsSupportedMediaMimeType("video/mp4")); | |
222 EXPECT_TRUE(IsSupportedMediaMimeType("video/x-m4v")); | |
223 | |
224 EXPECT_TRUE(IsSupportedMediaMimeType("audio/mp3")); | |
225 EXPECT_TRUE(IsSupportedMediaMimeType("audio/x-mp3")); | |
226 EXPECT_TRUE(IsSupportedMediaMimeType("audio/mpeg")); | |
227 EXPECT_TRUE(IsSupportedMediaMimeType("audio/aac")); | |
228 | |
229 #if defined(ENABLE_MPEG2TS_STREAM_PARSER) | |
230 EXPECT_TRUE(IsSupportedMediaMimeType("video/mp2t")); | |
231 #else | |
232 EXPECT_FALSE(IsSupportedMediaMimeType("video/mp2t")); | |
233 #endif | |
234 #else | |
235 EXPECT_FALSE(IsSupportedMediaMimeType("audio/mp4")); | |
236 EXPECT_FALSE(IsSupportedMediaMimeType("audio/x-m4a")); | |
237 EXPECT_FALSE(IsSupportedMediaMimeType("video/mp4")); | |
238 EXPECT_FALSE(IsSupportedMediaMimeType("video/x-m4v")); | |
239 | |
240 EXPECT_FALSE(IsSupportedMediaMimeType("audio/mp3")); | |
241 EXPECT_FALSE(IsSupportedMediaMimeType("audio/x-mp3")); | |
242 EXPECT_FALSE(IsSupportedMediaMimeType("audio/mpeg")); | |
243 EXPECT_FALSE(IsSupportedMediaMimeType("audio/aac")); | |
244 #endif // USE_PROPRIETARY_CODECS | |
245 EXPECT_FALSE(IsSupportedMediaMimeType("video/mp3")); | |
246 | |
247 EXPECT_FALSE(IsSupportedMediaMimeType("video/unknown")); | |
248 EXPECT_FALSE(IsSupportedMediaMimeType("audio/unknown")); | |
249 EXPECT_FALSE(IsSupportedMediaMimeType("unknown/unknown")); | |
250 } | |
251 | |
252 // Note: codecs should only be a list of 2 or fewer; hence the restriction of | |
253 // results' length to 2. | |
254 TEST(MimeUtilTest, ParseCodecString) { | |
255 const struct { | |
256 const char* const original; | |
257 size_t expected_size; | |
258 const char* const results[2]; | |
259 } tests[] = { | |
260 { "\"bogus\"", 1, { "bogus" } }, | |
261 { "0", 1, { "0" } }, | |
262 { "avc1.42E01E, mp4a.40.2", 2, { "avc1", "mp4a" } }, | |
263 { "\"mp4v.20.240, mp4a.40.2\"", 2, { "mp4v", "mp4a" } }, | |
264 { "mp4v.20.8, samr", 2, { "mp4v", "samr" } }, | |
265 { "\"theora, vorbis\"", 2, { "theora", "vorbis" } }, | |
266 { "", 0, { } }, | |
267 { "\"\"", 0, { } }, | |
268 { "\" \"", 0, { } }, | |
269 { ",", 2, { "", "" } }, | |
270 }; | |
271 | |
272 for (size_t i = 0; i < arraysize(tests); ++i) { | |
273 std::vector<std::string> codecs_out; | |
274 ParseCodecString(tests[i].original, &codecs_out, true); | |
275 ASSERT_EQ(tests[i].expected_size, codecs_out.size()); | |
276 for (size_t j = 0; j < tests[i].expected_size; ++j) | |
277 EXPECT_EQ(tests[i].results[j], codecs_out[j]); | |
278 } | |
279 | |
280 // Test without stripping the codec type. | |
281 std::vector<std::string> codecs_out; | |
282 ParseCodecString("avc1.42E01E, mp4a.40.2", &codecs_out, false); | |
283 ASSERT_EQ(2u, codecs_out.size()); | |
284 EXPECT_EQ("avc1.42E01E", codecs_out[0]); | |
285 EXPECT_EQ("mp4a.40.2", codecs_out[1]); | |
286 } | |
287 | |
288 TEST(MimeUtilTest, TestParseMimeTypeWithoutParameter) { | 148 TEST(MimeUtilTest, TestParseMimeTypeWithoutParameter) { |
289 std::string nonAscii("application/nonutf8"); | 149 std::string nonAscii("application/nonutf8"); |
290 EXPECT_TRUE(ParseMimeTypeWithoutParameter(nonAscii, NULL, NULL)); | 150 EXPECT_TRUE(ParseMimeTypeWithoutParameter(nonAscii, NULL, NULL)); |
291 #if defined(OS_WIN) | 151 #if defined(OS_WIN) |
292 nonAscii.append(base::WideToUTF8(L"\u2603")); | 152 nonAscii.append(base::WideToUTF8(L"\u2603")); |
293 #else | 153 #else |
294 nonAscii.append("\u2603"); // unicode snowman | 154 nonAscii.append("\u2603"); // unicode snowman |
295 #endif | 155 #endif |
296 EXPECT_FALSE(ParseMimeTypeWithoutParameter(nonAscii, NULL, NULL)); | 156 EXPECT_FALSE(ParseMimeTypeWithoutParameter(nonAscii, NULL, NULL)); |
297 | 157 |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 std::string post_data; | 274 std::string post_data; |
415 AddMultipartValueForUpload("value name", "value", "boundary", | 275 AddMultipartValueForUpload("value name", "value", "boundary", |
416 "content type", &post_data); | 276 "content type", &post_data); |
417 AddMultipartValueForUpload("value name", "value", "boundary", | 277 AddMultipartValueForUpload("value name", "value", "boundary", |
418 "", &post_data); | 278 "", &post_data); |
419 AddMultipartFinalDelimiterForUpload("boundary", &post_data); | 279 AddMultipartFinalDelimiterForUpload("boundary", &post_data); |
420 EXPECT_STREQ(ref_output, post_data.c_str()); | 280 EXPECT_STREQ(ref_output, post_data.c_str()); |
421 } | 281 } |
422 | 282 |
423 } // namespace net | 283 } // namespace net |
OLD | NEW |