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 "googleurl/src/gurl.h"
      7 #include "net/base/data_url.h"
      8 #include "testing/gtest/include/gtest/gtest.h"
      9 
     10 namespace {
     11 
     12 struct ParseTestData {
     13   const char* url;
     14   bool is_valid;
     15   const char* mime_type;
     16   const char* charset;
     17   const char* data;
     18 };
     19 
     20 }
     21 
     22 TEST(DataURLTest, Parse) {
     23   const ParseTestData tests[] = {
     24     { "data:",
     25        false,
     26        "",
     27        "",
     28        "" },
     29 
     30     { "data:,",
     31       true,
     32       "text/plain",
     33       "US-ASCII",
     34       "" },
     35 
     36     { "data:;base64,",
     37       true,
     38       "text/plain",
     39       "US-ASCII",
     40       "" },
     41 
     42     { "data:;charset=,test",
     43       true,
     44       "text/plain",
     45       "US-ASCII",
     46       "test" },
     47 
     48     { "data:TeXt/HtMl,<b>x</b>",
     49       true,
     50       "text/html",
     51       "US-ASCII",
     52       "<b>x</b>" },
     53 
     54     { "data:,foo",
     55       true,
     56       "text/plain",
     57       "US-ASCII",
     58       "foo" },
     59 
     60     { "data:;base64,aGVsbG8gd29ybGQ=",
     61       true,
     62       "text/plain",
     63       "US-ASCII",
     64       "hello world" },
     65 
     66     { "data:foo/bar;baz=1;charset=kk,boo",
     67       true,
     68       "foo/bar",
     69       "kk",
     70       "boo" },
     71 
     72     { "data:text/html,%3Chtml%3E%3Cbody%3E%3Cb%3Ehello%20world"
     73           "%3C%2Fb%3E%3C%2Fbody%3E%3C%2Fhtml%3E",
     74       true,
     75       "text/html",
     76       "US-ASCII",
     77       "<html><body><b>hello world</b></body></html>" },
     78 
     79     { "data:text/html,<html><body><b>hello world</b></body></html>",
     80       true,
     81       "text/html",
     82       "US-ASCII",
     83       "<html><body><b>hello world</b></body></html>" },
     84 
     85     // the comma cannot be url-escaped!
     86     { "data:%2Cblah",
     87       false,
     88       "",
     89       "",
     90       "" },
     91 
     92     // invalid base64 content
     93     { "data:;base64,aGVs_-_-",
     94       false,
     95       "",
     96       "",
     97       "" },
     98 
     99     // Spaces should be removed from non-text data URLs (we already tested
    100     // spaces above).
    101     { "data:image/fractal,a b c d e f g",
    102       true,
    103       "image/fractal",
    104       "US-ASCII",
    105       "abcdefg" },
    106 
    107     // Spaces should also be removed from anything base-64 encoded
    108     { "data:;base64,aGVs bG8gd2  9ybGQ=",
    109       true,
    110       "text/plain",
    111       "US-ASCII",
    112       "hello world" },
    113 
    114     // Other whitespace should also be removed from anything base-64 encoded.
    115     { "data:;base64,aGVs bG8gd2  \n9ybGQ=",
    116       true,
    117       "text/plain",
    118       "US-ASCII",
    119       "hello world" },
    120 
    121     // In base64 encoding, escaped whitespace should be stripped.
    122     // (This test was taken from acid3)
    123     // http://b/1054495
    124     { "data:text/javascript;base64,%20ZD%20Qg%0D%0APS%20An%20Zm91cic%0D%0A%207"
    125           "%20",
    126       true,
    127       "text/javascript",
    128       "US-ASCII",
    129       "d4 = 'four';" },
    130 
    131     // Only unescaped whitespace should be stripped in non-base64.
    132     // http://b/1157796
    133     { "data:img/png,A  B  %20  %0A  C",
    134       true,
    135       "img/png",
    136       "US-ASCII",
    137       "AB \nC" },
    138 
    139     // TODO(darin): add more interesting tests
    140   };
    141 
    142   for (size_t i = 0; i < arraysize(tests); ++i) {
    143     std::string mime_type;
    144     std::string charset;
    145     std::string data;
    146     bool ok =
    147         net::DataURL::Parse(GURL(tests[i].url), &mime_type, &charset, &data);
    148     EXPECT_EQ(ok, tests[i].is_valid);
    149     if (tests[i].is_valid) {
    150       EXPECT_EQ(tests[i].mime_type, mime_type);
    151       EXPECT_EQ(tests[i].charset, charset);
    152       EXPECT_EQ(tests[i].data, data);
    153     }
    154   }
    155 }
    156