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