Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / net / base / mime_util_unittest.cc
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.
4
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"
10
11 namespace net {
12
13 TEST(MimeUtilTest, ExtensionTest) {
14   const struct {
15     const base::FilePath::CharType* extension;
16     const char* mime_type;
17     bool valid;
18   } tests[] = {
19     { FILE_PATH_LITERAL("png"), "image/png", true },
20     { FILE_PATH_LITERAL("css"), "text/css", true },
21     { FILE_PATH_LITERAL("pjp"), "image/jpeg", true },
22     { FILE_PATH_LITERAL("pjpeg"), "image/jpeg", true },
23     { FILE_PATH_LITERAL("not an extension / for sure"), "", false },
24   };
25
26   std::string mime_type;
27   bool rv;
28
29   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
30     rv = GetMimeTypeFromExtension(tests[i].extension, &mime_type);
31     EXPECT_EQ(tests[i].valid, rv);
32     if (rv)
33       EXPECT_EQ(tests[i].mime_type, mime_type);
34   }
35 }
36
37 TEST(MimeUtilTest, FileTest) {
38   const struct {
39     const base::FilePath::CharType* file_path;
40     const char* mime_type;
41     bool valid;
42   } tests[] = {
43     { FILE_PATH_LITERAL("c:\\foo\\bar.css"), "text/css", true },
44     { FILE_PATH_LITERAL("c:\\blah"), "", false },
45     { FILE_PATH_LITERAL("/usr/local/bin/mplayer"), "", false },
46     { FILE_PATH_LITERAL("/home/foo/bar.css"), "text/css", true },
47     { FILE_PATH_LITERAL("/blah."), "", false },
48     { FILE_PATH_LITERAL("c:\\blah."), "", false },
49   };
50
51   std::string mime_type;
52   bool rv;
53
54   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
55     rv = GetMimeTypeFromFile(base::FilePath(tests[i].file_path),
56                                   &mime_type);
57     EXPECT_EQ(tests[i].valid, rv);
58     if (rv)
59       EXPECT_EQ(tests[i].mime_type, mime_type);
60   }
61 }
62
63 TEST(MimeUtilTest, LookupTypes) {
64   EXPECT_FALSE(IsUnsupportedTextMimeType("text/banana"));
65   EXPECT_TRUE(IsUnsupportedTextMimeType("text/vcard"));
66
67   EXPECT_TRUE(IsSupportedImageMimeType("image/jpeg"));
68   EXPECT_FALSE(IsSupportedImageMimeType("image/lolcat"));
69   EXPECT_TRUE(IsSupportedNonImageMimeType("text/html"));
70   EXPECT_TRUE(IsSupportedNonImageMimeType("text/css"));
71   EXPECT_TRUE(IsSupportedNonImageMimeType("text/"));
72   EXPECT_TRUE(IsSupportedNonImageMimeType("text/banana"));
73   EXPECT_FALSE(IsSupportedNonImageMimeType("text/vcard"));
74   EXPECT_FALSE(IsSupportedNonImageMimeType("application/virus"));
75   EXPECT_TRUE(IsSupportedNonImageMimeType("application/x-x509-user-cert"));
76   EXPECT_TRUE(IsSupportedNonImageMimeType("application/json"));
77   EXPECT_TRUE(IsSupportedNonImageMimeType("application/+json"));
78   EXPECT_TRUE(IsSupportedNonImageMimeType("application/x-suggestions+json"));
79   EXPECT_TRUE(IsSupportedNonImageMimeType("application/x-s+json;x=2"));
80 #if defined(OS_ANDROID)
81   EXPECT_TRUE(IsSupportedNonImageMimeType("application/x-x509-ca-cert"));
82   EXPECT_TRUE(IsSupportedNonImageMimeType("application/x-pkcs12"));
83   EXPECT_TRUE(IsSupportedMediaMimeType("application/vnd.apple.mpegurl"));
84   EXPECT_TRUE(IsSupportedMediaMimeType("application/x-mpegurl"));
85 #endif
86
87   EXPECT_TRUE(IsSupportedMimeType("image/jpeg"));
88   EXPECT_FALSE(IsSupportedMimeType("image/lolcat"));
89   EXPECT_TRUE(IsSupportedMimeType("text/html"));
90   EXPECT_TRUE(IsSupportedMimeType("text/banana"));
91   EXPECT_FALSE(IsSupportedMimeType("text/vcard"));
92   EXPECT_FALSE(IsSupportedMimeType("application/virus"));
93   EXPECT_FALSE(IsSupportedMimeType("application/x-json"));
94   EXPECT_FALSE(IsSupportedNonImageMimeType("application/vnd.doc;x=y+json"));
95 }
96
97 TEST(MimeUtilTest, StrictMediaMimeType) {
98   EXPECT_TRUE(IsStrictMediaMimeType("video/webm"));
99   EXPECT_TRUE(IsStrictMediaMimeType("audio/webm"));
100
101   EXPECT_TRUE(IsStrictMediaMimeType("audio/wav"));
102   EXPECT_TRUE(IsStrictMediaMimeType("audio/x-wav"));
103
104   EXPECT_TRUE(IsStrictMediaMimeType("video/ogg"));
105   EXPECT_TRUE(IsStrictMediaMimeType("audio/ogg"));
106   EXPECT_TRUE(IsStrictMediaMimeType("application/ogg"));
107
108   EXPECT_TRUE(IsStrictMediaMimeType("audio/mpeg"));
109   EXPECT_TRUE(IsStrictMediaMimeType("audio/mp3"));
110   EXPECT_TRUE(IsStrictMediaMimeType("audio/x-mp3"));
111
112   // TODO(amogh.bihani): These will be fixed http://crbug.com/53193
113   EXPECT_FALSE(IsStrictMediaMimeType("video/mp4"));
114   EXPECT_FALSE(IsStrictMediaMimeType("video/x-m4v"));
115   EXPECT_FALSE(IsStrictMediaMimeType("audio/mp4"));
116   EXPECT_FALSE(IsStrictMediaMimeType("audio/x-m4a"));
117
118   EXPECT_FALSE(IsStrictMediaMimeType("application/x-mpegurl"));
119   EXPECT_FALSE(IsStrictMediaMimeType("application/vnd.apple.mpegurl"));
120   // ---------------------------------------------------------------------------
121
122   EXPECT_FALSE(IsStrictMediaMimeType("video/unknown"));
123   EXPECT_FALSE(IsStrictMediaMimeType("audio/unknown"));
124   EXPECT_FALSE(IsStrictMediaMimeType("application/unknown"));
125   EXPECT_FALSE(IsStrictMediaMimeType("unknown/unknown"));
126 }
127
128 TEST(MimeUtilTest, MatchesMimeType) {
129   EXPECT_TRUE(MatchesMimeType("*", "video/x-mpeg"));
130   EXPECT_TRUE(MatchesMimeType("video/*", "video/x-mpeg"));
131   EXPECT_TRUE(MatchesMimeType("video/*", "video/*"));
132   EXPECT_TRUE(MatchesMimeType("video/x-mpeg", "video/x-mpeg"));
133   EXPECT_TRUE(MatchesMimeType("application/*+xml",
134                                    "application/html+xml"));
135   EXPECT_TRUE(MatchesMimeType("application/*+xml", "application/+xml"));
136   EXPECT_TRUE(MatchesMimeType("application/*+json",
137                                    "application/x-myformat+json"));
138   EXPECT_TRUE(MatchesMimeType("aaa*aaa", "aaaaaa"));
139   EXPECT_TRUE(MatchesMimeType("*", std::string()));
140   EXPECT_FALSE(MatchesMimeType("video/", "video/x-mpeg"));
141   EXPECT_FALSE(MatchesMimeType(std::string(), "video/x-mpeg"));
142   EXPECT_FALSE(MatchesMimeType(std::string(), std::string()));
143   EXPECT_FALSE(MatchesMimeType("video/x-mpeg", std::string()));
144   EXPECT_FALSE(MatchesMimeType("application/*+xml", "application/xml"));
145   EXPECT_FALSE(MatchesMimeType("application/*+xml",
146                                     "application/html+xmlz"));
147   EXPECT_FALSE(MatchesMimeType("application/*+xml",
148                                     "applcation/html+xml"));
149   EXPECT_FALSE(MatchesMimeType("aaa*aaa", "aaaaa"));
150
151   EXPECT_TRUE(MatchesMimeType("*", "video/x-mpeg;param=val"));
152   EXPECT_TRUE(MatchesMimeType("video/*", "video/x-mpeg;param=val"));
153   EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/mpeg"));
154   EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/mpeg;param=other"));
155   EXPECT_TRUE(MatchesMimeType("video/*;param=val", "video/mpeg;param=val"));
156   EXPECT_TRUE(MatchesMimeType("video/x-mpeg", "video/x-mpeg;param=val"));
157   EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param=val",
158                               "video/x-mpeg;param=val"));
159   EXPECT_FALSE(MatchesMimeType("video/x-mpeg;param2=val2",
160                                "video/x-mpeg;param=val"));
161   EXPECT_FALSE(MatchesMimeType("video/x-mpeg;param2=val2",
162                                "video/x-mpeg;param2=val"));
163   EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param=val",
164                               "video/x-mpeg;param=val;param2=val2"));
165   EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param=val;param2=val2",
166                               "video/x-mpeg;param=val;param2=val2"));
167   EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param2=val2;param=val",
168                               "video/x-mpeg;param=val;param2=val2"));
169   EXPECT_FALSE(MatchesMimeType("video/x-mpeg;param3=val3;param=val",
170                                "video/x-mpeg;param=val;param2=val2"));
171   EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param=val ;param2=val2 ",
172                               "video/x-mpeg;param=val;param2=val2"));
173
174   EXPECT_TRUE(MatchesMimeType("*/*;param=val", "video/x-mpeg;param=val"));
175   EXPECT_FALSE(MatchesMimeType("*/*;param=val", "video/x-mpeg;param=val2"));
176
177   EXPECT_TRUE(MatchesMimeType("*", "*"));
178   EXPECT_TRUE(MatchesMimeType("*", "*/*"));
179   EXPECT_TRUE(MatchesMimeType("*/*", "*/*"));
180   EXPECT_TRUE(MatchesMimeType("*/*", "*"));
181   EXPECT_TRUE(MatchesMimeType("video/*", "video/*"));
182   EXPECT_FALSE(MatchesMimeType("video/*", "*/*"));
183   EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/*"));
184   EXPECT_TRUE(MatchesMimeType("video/*;param=val", "video/*;param=val"));
185   EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/*;param=val2"));
186
187   EXPECT_TRUE(MatchesMimeType("ab*cd", "abxxxcd"));
188   EXPECT_TRUE(MatchesMimeType("ab*cd", "abx/xcd"));
189   EXPECT_TRUE(MatchesMimeType("ab/*cd", "ab/xxxcd"));
190 }
191
192 // Note: codecs should only be a list of 2 or fewer; hence the restriction of
193 // results' length to 2.
194 TEST(MimeUtilTest, ParseCodecString) {
195   const struct {
196     const char* original;
197     size_t expected_size;
198     const char* results[2];
199   } tests[] = {
200     { "\"bogus\"",                  1, { "bogus" }            },
201     { "0",                          1, { "0" }                },
202     { "avc1.42E01E, mp4a.40.2",     2, { "avc1",   "mp4a" }   },
203     { "\"mp4v.20.240, mp4a.40.2\"", 2, { "mp4v",   "mp4a" }   },
204     { "mp4v.20.8, samr",            2, { "mp4v",   "samr" }   },
205     { "\"theora, vorbis\"",         2, { "theora", "vorbis" } },
206     { "",                           0, { }                    },
207     { "\"\"",                       0, { }                    },
208     { "\"   \"",                    0, { }                    },
209     { ",",                          2, { "", "" }             },
210   };
211
212   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
213     std::vector<std::string> codecs_out;
214     ParseCodecString(tests[i].original, &codecs_out, true);
215     ASSERT_EQ(tests[i].expected_size, codecs_out.size());
216     for (size_t j = 0; j < tests[i].expected_size; ++j)
217       EXPECT_EQ(tests[i].results[j], codecs_out[j]);
218   }
219
220   // Test without stripping the codec type.
221   std::vector<std::string> codecs_out;
222   ParseCodecString("avc1.42E01E, mp4a.40.2", &codecs_out, false);
223   ASSERT_EQ(2u, codecs_out.size());
224   EXPECT_EQ("avc1.42E01E", codecs_out[0]);
225   EXPECT_EQ("mp4a.40.2", codecs_out[1]);
226 }
227
228 TEST(MimeUtilTest, TestIsMimeType) {
229   std::string nonAscii("application/nonutf8");
230   EXPECT_TRUE(IsMimeType(nonAscii));
231 #if defined(OS_WIN)
232   nonAscii.append(base::WideToUTF8(std::wstring(L"\u2603")));
233 #else
234   nonAscii.append("\u2603");  // unicode snowman
235 #endif
236   EXPECT_FALSE(IsMimeType(nonAscii));
237
238   EXPECT_TRUE(IsMimeType("application/mime"));
239   EXPECT_TRUE(IsMimeType("application/json"));
240   EXPECT_TRUE(IsMimeType("application/x-suggestions+json"));
241   EXPECT_TRUE(IsMimeType("application/+json"));
242   EXPECT_TRUE(IsMimeType("audio/mime"));
243   EXPECT_TRUE(IsMimeType("example/mime"));
244   EXPECT_TRUE(IsMimeType("image/mime"));
245   EXPECT_TRUE(IsMimeType("message/mime"));
246   EXPECT_TRUE(IsMimeType("model/mime"));
247   EXPECT_TRUE(IsMimeType("multipart/mime"));
248   EXPECT_TRUE(IsMimeType("text/mime"));
249   EXPECT_TRUE(IsMimeType("TEXT/mime"));
250   EXPECT_TRUE(IsMimeType("Text/mime"));
251   EXPECT_TRUE(IsMimeType("TeXt/mime"));
252   EXPECT_TRUE(IsMimeType("video/mime"));
253   EXPECT_TRUE(IsMimeType("video/mime;parameter"));
254   EXPECT_TRUE(IsMimeType("*/*"));
255   EXPECT_TRUE(IsMimeType("*"));
256
257   EXPECT_TRUE(IsMimeType("x-video/mime"));
258   EXPECT_TRUE(IsMimeType("X-Video/mime"));
259   EXPECT_FALSE(IsMimeType("x-video/"));
260   EXPECT_FALSE(IsMimeType("x-/mime"));
261   EXPECT_FALSE(IsMimeType("mime/looking"));
262   EXPECT_FALSE(IsMimeType("text/"));
263 }
264
265 TEST(MimeUtilTest, TestToIANAMediaType) {
266   EXPECT_EQ("", GetIANAMediaType("texting/driving"));
267   EXPECT_EQ("", GetIANAMediaType("ham/sandwich"));
268   EXPECT_EQ("", GetIANAMediaType(std::string()));
269   EXPECT_EQ("", GetIANAMediaType("/application/hamsandwich"));
270
271   EXPECT_EQ("application", GetIANAMediaType("application/poodle-wrestler"));
272   EXPECT_EQ("audio", GetIANAMediaType("audio/mpeg"));
273   EXPECT_EQ("example", GetIANAMediaType("example/yomomma"));
274   EXPECT_EQ("image", GetIANAMediaType("image/png"));
275   EXPECT_EQ("message", GetIANAMediaType("message/sipfrag"));
276   EXPECT_EQ("model", GetIANAMediaType("model/vrml"));
277   EXPECT_EQ("multipart", GetIANAMediaType("multipart/mixed"));
278   EXPECT_EQ("text", GetIANAMediaType("text/plain"));
279   EXPECT_EQ("video", GetIANAMediaType("video/H261"));
280 }
281
282 TEST(MimeUtilTest, TestGetExtensionsForMimeType) {
283   const struct {
284     const char* mime_type;
285     size_t min_expected_size;
286     const char* contained_result;
287   } tests[] = {
288     { "text/plain", 2, "txt" },
289     { "*",          0, NULL  },
290     { "message/*",  1, "eml" },
291     { "MeSsAge/*",  1, "eml" },
292     { "image/bmp",  1, "bmp" },
293     { "video/*",    6, "mp4" },
294 #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_IOS)
295     { "video/*",    6, "mpg" },
296 #else
297     { "video/*",    6, "mpeg" },
298 #endif
299     { "audio/*",    6, "oga" },
300     { "aUDIo/*",    6, "wav" },
301   };
302
303   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
304     std::vector<base::FilePath::StringType> extensions;
305     GetExtensionsForMimeType(tests[i].mime_type, &extensions);
306     ASSERT_TRUE(tests[i].min_expected_size <= extensions.size());
307
308     if (!tests[i].contained_result)
309       continue;
310
311     bool found = false;
312     for (size_t j = 0; !found && j < extensions.size(); ++j) {
313 #if defined(OS_WIN)
314       if (extensions[j] == base::UTF8ToWide(tests[i].contained_result))
315         found = true;
316 #else
317       if (extensions[j] == tests[i].contained_result)
318         found = true;
319 #endif
320     }
321     ASSERT_TRUE(found) << "Must find at least the contained result within "
322                        << tests[i].mime_type;
323   }
324 }
325
326 TEST(MimeUtilTest, TestGetCertificateMimeTypeForMimeType) {
327   EXPECT_EQ(CERTIFICATE_MIME_TYPE_X509_USER_CERT,
328             GetCertificateMimeTypeForMimeType("application/x-x509-user-cert"));
329 #if defined(OS_ANDROID)
330   // Only Android supports CA Certs and PKCS12 archives.
331   EXPECT_EQ(CERTIFICATE_MIME_TYPE_X509_CA_CERT,
332             GetCertificateMimeTypeForMimeType("application/x-x509-ca-cert"));
333   EXPECT_EQ(CERTIFICATE_MIME_TYPE_PKCS12_ARCHIVE,
334             GetCertificateMimeTypeForMimeType("application/x-pkcs12"));
335 #else
336   EXPECT_EQ(CERTIFICATE_MIME_TYPE_UNKNOWN,
337             GetCertificateMimeTypeForMimeType("application/x-x509-ca-cert"));
338   EXPECT_EQ(CERTIFICATE_MIME_TYPE_UNKNOWN,
339             GetCertificateMimeTypeForMimeType("application/x-pkcs12"));
340 #endif
341   EXPECT_EQ(CERTIFICATE_MIME_TYPE_UNKNOWN,
342             GetCertificateMimeTypeForMimeType("text/plain"));
343 }
344
345 TEST(MimeUtilTest, TestAddMultipartValueForUpload) {
346   const char* ref_output = "--boundary\r\nContent-Disposition: form-data;"
347                            " name=\"value name\"\r\nContent-Type: content type"
348                            "\r\n\r\nvalue\r\n"
349                            "--boundary\r\nContent-Disposition: form-data;"
350                            " name=\"value name\"\r\n\r\nvalue\r\n"
351                            "--boundary--\r\n";
352   std::string post_data;
353   AddMultipartValueForUpload("value name", "value", "boundary",
354                              "content type", &post_data);
355   AddMultipartValueForUpload("value name", "value", "boundary",
356                              "", &post_data);
357   AddMultipartFinalDelimiterForUpload("boundary", &post_data);
358   EXPECT_STREQ(ref_output, post_data.c_str());
359 }
360
361 }  // namespace net