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