[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / net / base / mime_util_unittest.cc
blob440582be25c4d64d4ca090e38018308b38403d50
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 "build/build_config.h"
9 #include "net/base/mime_util.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace net {
14 TEST(MimeUtilTest, ExtensionTest) {
15 // String: png\0css
16 base::FilePath::StringType containsNullByte;
17 containsNullByte.append(FILE_PATH_LITERAL("png"));
18 containsNullByte.append(1, FILE_PATH_LITERAL('\0'));
19 containsNullByte.append(FILE_PATH_LITERAL("css"));
21 const struct {
22 const base::FilePath::StringType extension;
23 const char* const mime_type;
24 bool valid;
25 } tests[] = {
26 {FILE_PATH_LITERAL("png"), "image/png", true},
27 {FILE_PATH_LITERAL("PNG"), "image/png", true},
28 {FILE_PATH_LITERAL("css"), "text/css", true},
29 {FILE_PATH_LITERAL("pjp"), "image/jpeg", true},
30 {FILE_PATH_LITERAL("pjpeg"), "image/jpeg", true},
31 #if defined(OS_CHROMEOS)
32 // These two are test cases for testing platform mime types on Chrome OS.
33 {FILE_PATH_LITERAL("epub"), "application/epub+zip", true},
34 {FILE_PATH_LITERAL("ics"), "text/calendar", true},
35 #endif
36 #if defined(OS_ANDROID)
37 {FILE_PATH_LITERAL("m3u8"), "application/x-mpegurl", true},
38 #endif
39 {FILE_PATH_LITERAL("not an extension / for sure"), "", false},
40 {containsNullByte, "", false}
43 std::string mime_type;
44 bool rv;
46 for (size_t i = 0; i < arraysize(tests); ++i) {
47 rv = GetMimeTypeFromExtension(tests[i].extension, &mime_type);
48 EXPECT_EQ(tests[i].valid, rv);
49 if (rv)
50 EXPECT_EQ(tests[i].mime_type, mime_type);
54 TEST(MimeUtilTest, FileTest) {
55 const struct {
56 const base::FilePath::CharType* file_path;
57 const char* const mime_type;
58 bool valid;
59 } tests[] = {
60 {FILE_PATH_LITERAL("c:\\foo\\bar.css"), "text/css", true},
61 {FILE_PATH_LITERAL("c:\\foo\\bar.CSS"), "text/css", true},
62 {FILE_PATH_LITERAL("c:\\blah"), "", false},
63 {FILE_PATH_LITERAL("/usr/local/bin/mplayer"), "", false},
64 {FILE_PATH_LITERAL("/home/foo/bar.css"), "text/css", true},
65 {FILE_PATH_LITERAL("/blah."), "", false},
66 {FILE_PATH_LITERAL("c:\\blah."), "", false},
69 std::string mime_type;
70 bool rv;
72 for (size_t i = 0; i < arraysize(tests); ++i) {
73 rv = GetMimeTypeFromFile(base::FilePath(tests[i].file_path),
74 &mime_type);
75 EXPECT_EQ(tests[i].valid, rv);
76 if (rv)
77 EXPECT_EQ(tests[i].mime_type, mime_type);
81 TEST(MimeUtilTest, MatchesMimeType) {
82 // MIME types are case insensitive.
83 EXPECT_TRUE(MatchesMimeType("VIDEO/*", "video/x-mpeg"));
84 EXPECT_TRUE(MatchesMimeType("video/*", "VIDEO/X-MPEG"));
86 EXPECT_TRUE(MatchesMimeType("*", "video/x-mpeg"));
87 EXPECT_TRUE(MatchesMimeType("video/*", "video/x-mpeg"));
88 EXPECT_TRUE(MatchesMimeType("video/*", "video/*"));
89 EXPECT_TRUE(MatchesMimeType("video/x-mpeg", "video/x-mpeg"));
90 EXPECT_TRUE(MatchesMimeType("application/*+xml",
91 "application/html+xml"));
92 EXPECT_TRUE(MatchesMimeType("application/*+xml", "application/+xml"));
93 EXPECT_TRUE(MatchesMimeType("application/*+json",
94 "application/x-myformat+json"));
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("VIDEO/", "Video/X-MPEG"));
99 EXPECT_FALSE(MatchesMimeType(std::string(), "video/x-mpeg"));
100 EXPECT_FALSE(MatchesMimeType(std::string(), std::string()));
101 EXPECT_FALSE(MatchesMimeType("video/x-mpeg", std::string()));
102 EXPECT_FALSE(MatchesMimeType("application/*+xml", "application/xml"));
103 EXPECT_FALSE(MatchesMimeType("application/*+xml",
104 "application/html+xmlz"));
105 EXPECT_FALSE(MatchesMimeType("application/*+xml",
106 "applcation/html+xml"));
107 EXPECT_FALSE(MatchesMimeType("aaa*aaa", "aaaaa"));
109 EXPECT_TRUE(MatchesMimeType("*", "video/x-mpeg;param=val"));
110 EXPECT_TRUE(MatchesMimeType("*", "Video/X-MPEG;PARAM=VAL"));
111 EXPECT_TRUE(MatchesMimeType("video/*", "video/x-mpeg;param=val"));
112 EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/mpeg"));
113 EXPECT_FALSE(MatchesMimeType("Video/*;PARAM=VAL", "VIDEO/Mpeg"));
114 EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/mpeg;param=other"));
115 EXPECT_TRUE(MatchesMimeType("video/*;param=val", "video/mpeg;param=val"));
116 EXPECT_TRUE(MatchesMimeType("Video/*;PARAM=Val", "VIDEO/Mpeg;Param=Val"));
117 EXPECT_FALSE(MatchesMimeType("Video/*;PARAM=VAL", "VIDEO/Mpeg;Param=Val"));
118 EXPECT_TRUE(MatchesMimeType("video/x-mpeg", "video/x-mpeg;param=val"));
119 EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param=val",
120 "video/x-mpeg;param=val"));
121 EXPECT_FALSE(MatchesMimeType("video/x-mpeg;param2=val2",
122 "video/x-mpeg;param=val"));
123 EXPECT_FALSE(MatchesMimeType("video/x-mpeg;param2=val2",
124 "video/x-mpeg;param2=val"));
125 EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param=val",
126 "video/x-mpeg;param=val;param2=val2"));
127 EXPECT_TRUE(MatchesMimeType("Video/X-Mpeg;Param=Val",
128 "VIDEO/X-MPEG;PARAM=Val;PARAM2=val2"));
129 EXPECT_TRUE(MatchesMimeType("Video/X-Mpeg;Param=VAL",
130 "VIDEO/X-MPEG;PARAM=VAL;PARAM2=val2"));
131 EXPECT_FALSE(MatchesMimeType("Video/X-Mpeg;Param=val",
132 "VIDEO/X-MPEG;PARAM=VAL;PARAM2=val2"));
133 EXPECT_FALSE(MatchesMimeType("video/x-mpeg;param=VAL;param2=val2",
134 "video/x-mpeg;param=val;param2=val2"));
135 EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param2=val2;param=val",
136 "video/x-mpeg;param=val;param2=val2"));
137 EXPECT_FALSE(MatchesMimeType("video/x-mpeg;param3=val3;param=val",
138 "video/x-mpeg;param=val;param2=val2"));
139 EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param=val ;param2=val2 ",
140 "video/x-mpeg;param=val;param2=val2"));
142 EXPECT_TRUE(MatchesMimeType("*/*;param=val", "video/x-mpeg;param=val"));
143 EXPECT_FALSE(MatchesMimeType("*/*;param=val", "video/x-mpeg;param=val2"));
145 EXPECT_TRUE(MatchesMimeType("*", "*"));
146 EXPECT_TRUE(MatchesMimeType("*", "*/*"));
147 EXPECT_TRUE(MatchesMimeType("*/*", "*/*"));
148 EXPECT_TRUE(MatchesMimeType("*/*", "*"));
149 EXPECT_TRUE(MatchesMimeType("video/*", "video/*"));
150 EXPECT_FALSE(MatchesMimeType("video/*", "*/*"));
151 EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/*"));
152 EXPECT_TRUE(MatchesMimeType("video/*;param=val", "video/*;param=val"));
153 EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/*;param=val2"));
155 EXPECT_TRUE(MatchesMimeType("ab*cd", "abxxxcd"));
156 EXPECT_TRUE(MatchesMimeType("ab*cd", "abx/xcd"));
157 EXPECT_TRUE(MatchesMimeType("ab/*cd", "ab/xxxcd"));
160 TEST(MimeUtilTest, TestParseMimeTypeWithoutParameter) {
161 std::string nonAscii("application/nonutf8");
162 EXPECT_TRUE(ParseMimeTypeWithoutParameter(nonAscii, NULL, NULL));
163 #if defined(OS_WIN)
164 nonAscii.append(base::WideToUTF8(L"\u2603"));
165 #else
166 nonAscii.append("\u2603"); // unicode snowman
167 #endif
168 EXPECT_FALSE(ParseMimeTypeWithoutParameter(nonAscii, NULL, NULL));
170 std::string top_level_type;
171 std::string subtype;
172 EXPECT_TRUE(ParseMimeTypeWithoutParameter(
173 "application/mime", &top_level_type, &subtype));
174 EXPECT_EQ("application", top_level_type);
175 EXPECT_EQ("mime", subtype);
177 // Various allowed subtype forms.
178 EXPECT_TRUE(ParseMimeTypeWithoutParameter("application/json", NULL, NULL));
179 EXPECT_TRUE(ParseMimeTypeWithoutParameter(
180 "application/x-suggestions+json", NULL, NULL));
181 EXPECT_TRUE(ParseMimeTypeWithoutParameter("application/+json", NULL, NULL));
183 // Upper case letters are allowed.
184 EXPECT_TRUE(ParseMimeTypeWithoutParameter("text/mime", NULL, NULL));
185 EXPECT_TRUE(ParseMimeTypeWithoutParameter("TEXT/mime", NULL, NULL));
186 EXPECT_TRUE(ParseMimeTypeWithoutParameter("Text/mime", NULL, NULL));
187 EXPECT_TRUE(ParseMimeTypeWithoutParameter("TeXt/mime", NULL, NULL));
189 // Experimental types are also considered to be valid.
190 EXPECT_TRUE(ParseMimeTypeWithoutParameter("x-video/mime", NULL, NULL));
191 EXPECT_TRUE(ParseMimeTypeWithoutParameter("X-Video/mime", NULL, NULL));
193 EXPECT_FALSE(ParseMimeTypeWithoutParameter("text", NULL, NULL));
194 EXPECT_FALSE(ParseMimeTypeWithoutParameter("text/", NULL, NULL));
195 EXPECT_FALSE(ParseMimeTypeWithoutParameter("text/ ", NULL, NULL));
196 EXPECT_FALSE(ParseMimeTypeWithoutParameter("te(xt/ ", NULL, NULL));
197 EXPECT_FALSE(ParseMimeTypeWithoutParameter("text/()plain", NULL, NULL));
199 EXPECT_FALSE(ParseMimeTypeWithoutParameter("x-video", NULL, NULL));
200 EXPECT_FALSE(ParseMimeTypeWithoutParameter("x-video/", NULL, NULL));
202 EXPECT_FALSE(ParseMimeTypeWithoutParameter("application/a/b/c", NULL, NULL));
204 //EXPECT_TRUE(ParseMimeTypeWithoutParameter("video/mime;parameter"));
207 TEST(MimeUtilTest, TestIsValidTopLevelMimeType) {
208 EXPECT_TRUE(IsValidTopLevelMimeType("application"));
209 EXPECT_TRUE(IsValidTopLevelMimeType("audio"));
210 EXPECT_TRUE(IsValidTopLevelMimeType("example"));
211 EXPECT_TRUE(IsValidTopLevelMimeType("image"));
212 EXPECT_TRUE(IsValidTopLevelMimeType("message"));
213 EXPECT_TRUE(IsValidTopLevelMimeType("model"));
214 EXPECT_TRUE(IsValidTopLevelMimeType("multipart"));
215 EXPECT_TRUE(IsValidTopLevelMimeType("text"));
216 EXPECT_TRUE(IsValidTopLevelMimeType("video"));
218 EXPECT_TRUE(IsValidTopLevelMimeType("TEXT"));
219 EXPECT_TRUE(IsValidTopLevelMimeType("Text"));
220 EXPECT_TRUE(IsValidTopLevelMimeType("TeXt"));
222 EXPECT_FALSE(IsValidTopLevelMimeType("mime"));
223 EXPECT_FALSE(IsValidTopLevelMimeType(""));
224 EXPECT_FALSE(IsValidTopLevelMimeType("/"));
225 EXPECT_FALSE(IsValidTopLevelMimeType(" "));
227 EXPECT_TRUE(IsValidTopLevelMimeType("x-video"));
228 EXPECT_TRUE(IsValidTopLevelMimeType("X-video"));
230 EXPECT_FALSE(IsValidTopLevelMimeType("x-"));
233 TEST(MimeUtilTest, TestGetExtensionsForMimeType) {
234 const struct {
235 const char* const mime_type;
236 size_t min_expected_size;
237 const char* const contained_result;
238 } tests[] = {
239 { "text/plain", 2, "txt" },
240 { "*", 0, NULL },
241 { "message/*", 1, "eml" },
242 { "MeSsAge/*", 1, "eml" },
243 { "image/bmp", 1, "bmp" },
244 { "video/*", 6, "mp4" },
245 #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_IOS)
246 { "video/*", 6, "mpg" },
247 #else
248 { "video/*", 6, "mpeg" },
249 #endif
250 { "audio/*", 6, "oga" },
251 { "aUDIo/*", 6, "wav" },
254 for (size_t i = 0; i < arraysize(tests); ++i) {
255 std::vector<base::FilePath::StringType> extensions;
256 GetExtensionsForMimeType(tests[i].mime_type, &extensions);
257 ASSERT_TRUE(tests[i].min_expected_size <= extensions.size());
259 if (!tests[i].contained_result)
260 continue;
262 bool found = false;
263 for (size_t j = 0; !found && j < extensions.size(); ++j) {
264 #if defined(OS_WIN)
265 if (extensions[j] == base::UTF8ToWide(tests[i].contained_result))
266 found = true;
267 #else
268 if (extensions[j] == tests[i].contained_result)
269 found = true;
270 #endif
272 ASSERT_TRUE(found) << "Must find at least the contained result within "
273 << tests[i].mime_type;
278 TEST(MimeUtilTest, TestAddMultipartValueForUpload) {
279 const char ref_output[] =
280 "--boundary\r\nContent-Disposition: form-data;"
281 " name=\"value name\"\r\nContent-Type: content type"
282 "\r\n\r\nvalue\r\n"
283 "--boundary\r\nContent-Disposition: form-data;"
284 " name=\"value name\"\r\n\r\nvalue\r\n"
285 "--boundary--\r\n";
286 std::string post_data;
287 AddMultipartValueForUpload("value name", "value", "boundary",
288 "content type", &post_data);
289 AddMultipartValueForUpload("value name", "value", "boundary",
290 "", &post_data);
291 AddMultipartFinalDelimiterForUpload("boundary", &post_data);
292 EXPECT_STREQ(ref_output, post_data.c_str());
295 } // namespace net