- add sources.
[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/banana"));
71   EXPECT_FALSE(IsSupportedNonImageMimeType("text/vcard"));
72   EXPECT_FALSE(IsSupportedNonImageMimeType("application/virus"));
73   EXPECT_TRUE(IsSupportedNonImageMimeType("application/x-x509-user-cert"));
74 #if defined(OS_ANDROID)
75   EXPECT_TRUE(IsSupportedNonImageMimeType("application/x-x509-ca-cert"));
76   EXPECT_TRUE(IsSupportedNonImageMimeType("application/x-pkcs12"));
77 #endif
78
79   EXPECT_TRUE(IsSupportedMimeType("image/jpeg"));
80   EXPECT_FALSE(IsSupportedMimeType("image/lolcat"));
81   EXPECT_TRUE(IsSupportedMimeType("text/html"));
82   EXPECT_TRUE(IsSupportedMimeType("text/banana"));
83   EXPECT_FALSE(IsSupportedMimeType("text/vcard"));
84   EXPECT_FALSE(IsSupportedMimeType("application/virus"));
85 }
86
87 TEST(MimeUtilTest, MatchesMimeType) {
88   EXPECT_TRUE(MatchesMimeType("*", "video/x-mpeg"));
89   EXPECT_TRUE(MatchesMimeType("video/*", "video/x-mpeg"));
90   EXPECT_TRUE(MatchesMimeType("video/*", "video/*"));
91   EXPECT_TRUE(MatchesMimeType("video/x-mpeg", "video/x-mpeg"));
92   EXPECT_TRUE(MatchesMimeType("application/*+xml",
93                                    "application/html+xml"));
94   EXPECT_TRUE(MatchesMimeType("application/*+xml", "application/+xml"));
95   EXPECT_TRUE(MatchesMimeType("aaa*aaa", "aaaaaa"));
96   EXPECT_TRUE(MatchesMimeType("*", std::string()));
97   EXPECT_FALSE(MatchesMimeType("video/", "video/x-mpeg"));
98   EXPECT_FALSE(MatchesMimeType(std::string(), "video/x-mpeg"));
99   EXPECT_FALSE(MatchesMimeType(std::string(), std::string()));
100   EXPECT_FALSE(MatchesMimeType("video/x-mpeg", std::string()));
101   EXPECT_FALSE(MatchesMimeType("application/*+xml", "application/xml"));
102   EXPECT_FALSE(MatchesMimeType("application/*+xml",
103                                     "application/html+xmlz"));
104   EXPECT_FALSE(MatchesMimeType("application/*+xml",
105                                     "applcation/html+xml"));
106   EXPECT_FALSE(MatchesMimeType("aaa*aaa", "aaaaa"));
107
108   EXPECT_TRUE(MatchesMimeType("*", "video/x-mpeg;param=val"));
109   EXPECT_TRUE(MatchesMimeType("video/*", "video/x-mpeg;param=val"));
110   EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/mpeg"));
111   EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/mpeg;param=other"));
112   EXPECT_TRUE(MatchesMimeType("video/*;param=val", "video/mpeg;param=val"));
113   EXPECT_TRUE(MatchesMimeType("video/x-mpeg", "video/x-mpeg;param=val"));
114   EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param=val",
115                               "video/x-mpeg;param=val"));
116   EXPECT_FALSE(MatchesMimeType("video/x-mpeg;param2=val2",
117                                "video/x-mpeg;param=val"));
118   EXPECT_FALSE(MatchesMimeType("video/x-mpeg;param2=val2",
119                                "video/x-mpeg;param2=val"));
120   EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param=val",
121                               "video/x-mpeg;param=val;param2=val2"));
122   EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param=val;param2=val2",
123                               "video/x-mpeg;param=val;param2=val2"));
124   EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param2=val2;param=val",
125                               "video/x-mpeg;param=val;param2=val2"));
126   EXPECT_FALSE(MatchesMimeType("video/x-mpeg;param3=val3;param=val",
127                                "video/x-mpeg;param=val;param2=val2"));
128   EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param=val ;param2=val2 ",
129                               "video/x-mpeg;param=val;param2=val2"));
130
131   EXPECT_TRUE(MatchesMimeType("*/*;param=val", "video/x-mpeg;param=val"));
132   EXPECT_FALSE(MatchesMimeType("*/*;param=val", "video/x-mpeg;param=val2"));
133
134   EXPECT_TRUE(MatchesMimeType("*", "*"));
135   EXPECT_TRUE(MatchesMimeType("*", "*/*"));
136   EXPECT_TRUE(MatchesMimeType("*/*", "*/*"));
137   EXPECT_TRUE(MatchesMimeType("*/*", "*"));
138   EXPECT_TRUE(MatchesMimeType("video/*", "video/*"));
139   EXPECT_FALSE(MatchesMimeType("video/*", "*/*"));
140   EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/*"));
141   EXPECT_TRUE(MatchesMimeType("video/*;param=val", "video/*;param=val"));
142   EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/*;param=val2"));
143
144   EXPECT_TRUE(MatchesMimeType("ab*cd", "abxxxcd"));
145   EXPECT_TRUE(MatchesMimeType("ab*cd", "abx/xcd"));
146   EXPECT_TRUE(MatchesMimeType("ab/*cd", "ab/xxxcd"));
147 }
148
149 // Note: codecs should only be a list of 2 or fewer; hence the restriction of
150 // results' length to 2.
151 TEST(MimeUtilTest, ParseCodecString) {
152   const struct {
153     const char* original;
154     size_t expected_size;
155     const char* results[2];
156   } tests[] = {
157     { "\"bogus\"",                  1, { "bogus" }            },
158     { "0",                          1, { "0" }                },
159     { "avc1.42E01E, mp4a.40.2",     2, { "avc1",   "mp4a" }   },
160     { "\"mp4v.20.240, mp4a.40.2\"", 2, { "mp4v",   "mp4a" }   },
161     { "mp4v.20.8, samr",            2, { "mp4v",   "samr" }   },
162     { "\"theora, vorbis\"",         2, { "theora", "vorbis" } },
163     { "",                           0, { }                    },
164     { "\"\"",                       0, { }                    },
165     { "\"   \"",                    0, { }                    },
166     { ",",                          2, { "", "" }             },
167   };
168
169   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
170     std::vector<std::string> codecs_out;
171     ParseCodecString(tests[i].original, &codecs_out, true);
172     ASSERT_EQ(tests[i].expected_size, codecs_out.size());
173     for (size_t j = 0; j < tests[i].expected_size; ++j)
174       EXPECT_EQ(tests[i].results[j], codecs_out[j]);
175   }
176
177   // Test without stripping the codec type.
178   std::vector<std::string> codecs_out;
179   ParseCodecString("avc1.42E01E, mp4a.40.2", &codecs_out, false);
180   ASSERT_EQ(2u, codecs_out.size());
181   EXPECT_EQ("avc1.42E01E", codecs_out[0]);
182   EXPECT_EQ("mp4a.40.2", codecs_out[1]);
183 }
184
185 TEST(MimeUtilTest, TestIsMimeType) {
186   std::string nonAscii("application/nonutf8");
187   EXPECT_TRUE(IsMimeType(nonAscii));
188 #if defined(OS_WIN)
189   nonAscii.append(WideToUTF8(std::wstring(L"\u2603")));
190 #else
191   nonAscii.append("\u2603");  // unicode snowman
192 #endif
193   EXPECT_FALSE(IsMimeType(nonAscii));
194
195   EXPECT_TRUE(IsMimeType("application/mime"));
196   EXPECT_TRUE(IsMimeType("audio/mime"));
197   EXPECT_TRUE(IsMimeType("example/mime"));
198   EXPECT_TRUE(IsMimeType("image/mime"));
199   EXPECT_TRUE(IsMimeType("message/mime"));
200   EXPECT_TRUE(IsMimeType("model/mime"));
201   EXPECT_TRUE(IsMimeType("multipart/mime"));
202   EXPECT_TRUE(IsMimeType("text/mime"));
203   EXPECT_TRUE(IsMimeType("TEXT/mime"));
204   EXPECT_TRUE(IsMimeType("Text/mime"));
205   EXPECT_TRUE(IsMimeType("TeXt/mime"));
206   EXPECT_TRUE(IsMimeType("video/mime"));
207   EXPECT_TRUE(IsMimeType("video/mime;parameter"));
208   EXPECT_TRUE(IsMimeType("*/*"));
209   EXPECT_TRUE(IsMimeType("*"));
210
211   EXPECT_TRUE(IsMimeType("x-video/mime"));
212   EXPECT_TRUE(IsMimeType("X-Video/mime"));
213   EXPECT_FALSE(IsMimeType("x-video/"));
214   EXPECT_FALSE(IsMimeType("x-/mime"));
215   EXPECT_FALSE(IsMimeType("mime/looking"));
216   EXPECT_FALSE(IsMimeType("text/"));
217 }
218
219 TEST(MimeUtilTest, TestToIANAMediaType) {
220   EXPECT_EQ("", GetIANAMediaType("texting/driving"));
221   EXPECT_EQ("", GetIANAMediaType("ham/sandwich"));
222   EXPECT_EQ("", GetIANAMediaType(std::string()));
223   EXPECT_EQ("", GetIANAMediaType("/application/hamsandwich"));
224
225   EXPECT_EQ("application", GetIANAMediaType("application/poodle-wrestler"));
226   EXPECT_EQ("audio", GetIANAMediaType("audio/mpeg"));
227   EXPECT_EQ("example", GetIANAMediaType("example/yomomma"));
228   EXPECT_EQ("image", GetIANAMediaType("image/png"));
229   EXPECT_EQ("message", GetIANAMediaType("message/sipfrag"));
230   EXPECT_EQ("model", GetIANAMediaType("model/vrml"));
231   EXPECT_EQ("multipart", GetIANAMediaType("multipart/mixed"));
232   EXPECT_EQ("text", GetIANAMediaType("text/plain"));
233   EXPECT_EQ("video", GetIANAMediaType("video/H261"));
234 }
235
236 TEST(MimeUtilTest, TestGetExtensionsForMimeType) {
237   const struct {
238     const char* mime_type;
239     size_t min_expected_size;
240     const char* contained_result;
241   } tests[] = {
242     { "text/plain", 2, "txt" },
243     { "*",          0, NULL  },
244     { "message/*",  1, "eml" },
245     { "MeSsAge/*",  1, "eml" },
246     { "image/bmp",  1, "bmp" },
247     { "video/*",    6, "mp4" },
248 #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_IOS)
249     { "video/*",    6, "mpg" },
250 #else
251     { "video/*",    6, "mpeg" },
252 #endif
253     { "audio/*",    6, "oga" },
254     { "aUDIo/*",    6, "wav" },
255   };
256
257   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
258     std::vector<base::FilePath::StringType> extensions;
259     GetExtensionsForMimeType(tests[i].mime_type, &extensions);
260     ASSERT_TRUE(tests[i].min_expected_size <= extensions.size());
261
262     if (!tests[i].contained_result)
263       continue;
264
265     bool found = false;
266     for (size_t j = 0; !found && j < extensions.size(); ++j) {
267 #if defined(OS_WIN)
268       if (extensions[j] == UTF8ToWide(tests[i].contained_result))
269         found = true;
270 #else
271       if (extensions[j] == tests[i].contained_result)
272         found = true;
273 #endif
274     }
275     ASSERT_TRUE(found) << "Must find at least the contained result within "
276                        << tests[i].mime_type;
277   }
278 }
279
280 TEST(MimeUtilTest, TestGetCertificateMimeTypeForMimeType) {
281   EXPECT_EQ(CERTIFICATE_MIME_TYPE_X509_USER_CERT,
282             GetCertificateMimeTypeForMimeType("application/x-x509-user-cert"));
283 #if defined(OS_ANDROID)
284   // Only Android supports CA Certs and PKCS12 archives.
285   EXPECT_EQ(CERTIFICATE_MIME_TYPE_X509_CA_CERT,
286             GetCertificateMimeTypeForMimeType("application/x-x509-ca-cert"));
287   EXPECT_EQ(CERTIFICATE_MIME_TYPE_PKCS12_ARCHIVE,
288             GetCertificateMimeTypeForMimeType("application/x-pkcs12"));
289 #else
290   EXPECT_EQ(CERTIFICATE_MIME_TYPE_UNKNOWN,
291             GetCertificateMimeTypeForMimeType("application/x-x509-ca-cert"));
292   EXPECT_EQ(CERTIFICATE_MIME_TYPE_UNKNOWN,
293             GetCertificateMimeTypeForMimeType("application/x-pkcs12"));
294 #endif
295   EXPECT_EQ(CERTIFICATE_MIME_TYPE_UNKNOWN,
296             GetCertificateMimeTypeForMimeType("text/plain"));
297 }
298
299 TEST(MimeUtilTest, TestAddMultipartValueForUpload) {
300   const char* ref_output = "--boundary\r\nContent-Disposition: form-data;"
301                            " name=\"value name\"\r\nContent-Type: content type"
302                            "\r\n\r\nvalue\r\n"
303                            "--boundary\r\nContent-Disposition: form-data;"
304                            " name=\"value name\"\r\n\r\nvalue\r\n"
305                            "--boundary--\r\n";
306   std::string post_data;
307   AddMultipartValueForUpload("value name", "value", "boundary",
308                              "content type", &post_data);
309   AddMultipartValueForUpload("value name", "value", "boundary",
310                              "", &post_data);
311   AddMultipartFinalDelimiterForUpload("boundary", &post_data);
312   EXPECT_STREQ(ref_output, post_data.c_str());
313 }
314
315 }  // namespace net