1 // Copyright (c) 2006-2008 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 "net/base/mime_util.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8
9 namespace {
10 class MimeUtilTest : public testing::Test {
11 };
12 }
13
TEST(MimeUtilTest,ExtensionTest)14 TEST(MimeUtilTest, ExtensionTest) {
15 const struct {
16 const FilePath::CharType* extension;
17 const char* mime_type;
18 bool valid;
19 } tests[] = {
20 { FILE_PATH_LITERAL("png"), "image/png", true },
21 { FILE_PATH_LITERAL("css"), "text/css", true },
22 { FILE_PATH_LITERAL("pjp"), "image/jpeg", true },
23 { FILE_PATH_LITERAL("pjpeg"), "image/jpeg", true },
24 { FILE_PATH_LITERAL("not an extension / for sure"), "", false },
25 };
26
27 std::string mime_type;
28 bool rv;
29
30 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
31 rv = net::GetMimeTypeFromExtension(tests[i].extension, &mime_type);
32 EXPECT_EQ(tests[i].valid, rv);
33 if (rv)
34 EXPECT_EQ(tests[i].mime_type, mime_type);
35 }
36 }
37
TEST(MimeUtilTest,FileTest)38 TEST(MimeUtilTest, FileTest) {
39 const struct {
40 const FilePath::CharType* file_path;
41 const char* mime_type;
42 bool valid;
43 } tests[] = {
44 { FILE_PATH_LITERAL("c:\\foo\\bar.css"), "text/css", true },
45 { FILE_PATH_LITERAL("c:\\blah"), "", false },
46 { FILE_PATH_LITERAL("/usr/local/bin/mplayer"), "", false },
47 { FILE_PATH_LITERAL("/home/foo/bar.css"), "text/css", true },
48 { FILE_PATH_LITERAL("/blah."), "", false },
49 { FILE_PATH_LITERAL("c:\\blah."), "", false },
50 };
51
52 std::string mime_type;
53 bool rv;
54
55 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
56 rv = net::GetMimeTypeFromFile(FilePath(tests[i].file_path),
57 &mime_type);
58 EXPECT_EQ(tests[i].valid, rv);
59 if (rv)
60 EXPECT_EQ(tests[i].mime_type, mime_type);
61 }
62 }
63
TEST(MimeUtilTest,LookupTypes)64 TEST(MimeUtilTest, LookupTypes) {
65 EXPECT_EQ(true, net::IsSupportedImageMimeType("image/jpeg"));
66 EXPECT_EQ(false, net::IsSupportedImageMimeType("image/lolcat"));
67 EXPECT_EQ(true, net::IsSupportedNonImageMimeType("text/html"));
68 EXPECT_EQ(false, net::IsSupportedNonImageMimeType("text/virus"));
69
70 EXPECT_EQ(true, net::IsSupportedMimeType("image/jpeg"));
71 EXPECT_EQ(false, net::IsSupportedMimeType("image/lolcat"));
72 EXPECT_EQ(true, net::IsSupportedMimeType("text/html"));
73 EXPECT_EQ(false, net::IsSupportedMimeType("text/virus"));
74 }
75
TEST(MimeUtilTest,MatchesMimeType)76 TEST(MimeUtilTest, MatchesMimeType) {
77 EXPECT_EQ(true, net::MatchesMimeType("*", "video/x-mpeg"));
78 EXPECT_EQ(true, net::MatchesMimeType("video/*", "video/x-mpeg"));
79 EXPECT_EQ(true, net::MatchesMimeType("video/x-mpeg", "video/x-mpeg"));
80 EXPECT_EQ(true, net::MatchesMimeType("application/*+xml",
81 "application/html+xml"));
82 EXPECT_EQ(true, net::MatchesMimeType("application/*+xml",
83 "application/+xml"));
84 EXPECT_EQ(true, net::MatchesMimeType("aaa*aaa",
85 "aaaaaa"));
86 EXPECT_EQ(false, net::MatchesMimeType("video/", "video/x-mpeg"));
87 EXPECT_EQ(false, net::MatchesMimeType("", "video/x-mpeg"));
88 EXPECT_EQ(false, net::MatchesMimeType("", ""));
89 EXPECT_EQ(false, net::MatchesMimeType("video/x-mpeg", ""));
90 EXPECT_EQ(false, net::MatchesMimeType("application/*+xml",
91 "application/xml"));
92 EXPECT_EQ(false, net::MatchesMimeType("application/*+xml",
93 "application/html+xmlz"));
94 EXPECT_EQ(false, net::MatchesMimeType("application/*+xml",
95 "applcation/html+xml"));
96 EXPECT_EQ(false, net::MatchesMimeType("aaa*aaa",
97 "aaaaa"));
98 }
99
100 // Note: codecs should only be a list of 2 or fewer; hence the restriction of
101 // results' length to 2.
TEST(MimeUtilTest,ParseCodecString)102 TEST(MimeUtilTest, ParseCodecString) {
103 const struct {
104 const char* original;
105 size_t expected_size;
106 const char* results[2];
107 } tests[] = {
108 { "\"bogus\"", 1, { "bogus" } },
109 { "0", 1, { "0" } },
110 { "avc1.42E01E, mp4a.40.2", 2, { "avc1", "mp4a" } },
111 { "\"mp4v.20.240, mp4a.40.2\"", 2, { "mp4v", "mp4a" } },
112 { "mp4v.20.8, samr", 2, { "mp4v", "samr" } },
113 { "\"theora, vorbis\"", 2, { "theora", "vorbis" } },
114 { "", 1, { "" } },
115 { "\"\"", 1, { "" } },
116 { ",", 2, { "", "" } },
117 };
118
119 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
120 std::vector<std::string> codecs_out;
121 net::ParseCodecString(tests[i].original, &codecs_out);
122 EXPECT_EQ(tests[i].expected_size, codecs_out.size());
123 for (size_t j = 0; j < tests[i].expected_size; ++j) {
124 EXPECT_EQ(tests[i].results[j], codecs_out[j]);
125 }
126 }
127 }
128