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