summaryrefslogtreecommitdiffstats
path: root/net/base/data_url_unittest.cc
blob: a4ea6aafaf8db7782cfb880a6affd87505d6409e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/basictypes.h"
#include "googleurl/src/gurl.h"
#include "net/base/data_url.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

struct ParseTestData {
  const char* url;
  bool is_valid;
  const char* mime_type;
  const char* charset;
  const char* data;
};

}

TEST(DataURLTest, Parse) {
  const ParseTestData tests[] = {
    { "data:",
       false,
       "",
       "",
       "" },

    { "data:,",
      true,
      "text/plain",
      "US-ASCII",
      "" },

    { "data:;base64,",
      true,
      "text/plain",
      "US-ASCII",
      "" },

    { "data:;charset=,test",
      true,
      "text/plain",
      "US-ASCII",
      "test" },

    { "data:TeXt/HtMl,<b>x</b>",
      true,
      "text/html",
      "US-ASCII",
      "<b>x</b>" },

    { "data:,foo",
      true,
      "text/plain",
      "US-ASCII",
      "foo" },

    { "data:;base64,aGVsbG8gd29ybGQ=",
      true,
      "text/plain",
      "US-ASCII",
      "hello world" },

    { "data:foo/bar;baz=1;charset=kk,boo",
      true,
      "foo/bar",
      "kk",
      "boo" },

    { "data:foo/bar;charset=kk;baz=1,boo",
      true,
      "foo/bar",
      "kk",
      "boo" },

    { "data:text/html,%3Chtml%3E%3Cbody%3E%3Cb%3Ehello%20world"
          "%3C%2Fb%3E%3C%2Fbody%3E%3C%2Fhtml%3E",
      true,
      "text/html",
      "US-ASCII",
      "<html><body><b>hello world</b></body></html>" },

    { "data:text/html,<html><body><b>hello world</b></body></html>",
      true,
      "text/html",
      "US-ASCII",
      "<html><body><b>hello world</b></body></html>" },

    // the comma cannot be url-escaped!
    { "data:%2Cblah",
      false,
      "",
      "",
      "" },

    // invalid base64 content
    { "data:;base64,aGVs_-_-",
      false,
      "",
      "",
      "" },

    // Spaces should be removed from non-text data URLs (we already tested
    // spaces above).
    { "data:image/fractal,a b c d e f g",
      true,
      "image/fractal",
      "US-ASCII",
      "abcdefg" },

    // Spaces should also be removed from anything base-64 encoded
    { "data:;base64,aGVs bG8gd2  9ybGQ=",
      true,
      "text/plain",
      "US-ASCII",
      "hello world" },

    // Other whitespace should also be removed from anything base-64 encoded.
    { "data:;base64,aGVs bG8gd2  \n9ybGQ=",
      true,
      "text/plain",
      "US-ASCII",
      "hello world" },

    // In base64 encoding, escaped whitespace should be stripped.
    // (This test was taken from acid3)
    // http://b/1054495
    { "data:text/javascript;base64,%20ZD%20Qg%0D%0APS%20An%20Zm91cic%0D%0A%207"
          "%20",
      true,
      "text/javascript",
      "US-ASCII",
      "d4 = 'four';" },

    // Only unescaped whitespace should be stripped in non-base64.
    // http://b/1157796
    { "data:img/png,A  B  %20  %0A  C",
      true,
      "img/png",
      "US-ASCII",
      "AB \nC" },

    { "data:text/plain;charset=utf-8;base64,SGVsbMO2",
      true,
      "text/plain",
      "utf-8",
      "Hell\xC3\xB6" },

    // Not sufficiently padded.
    { "data:;base64,aGVsbG8gd29ybGQ",
      true,
      "text/plain",
      "US-ASCII",
      "hello world" },

    // Bad encoding (truncated).
    { "data:;base64,aGVsbG8gd29yb",
      false,
      "",
      "",
      "" },

    // TODO(darin): add more interesting tests
  };

  for (size_t i = 0; i < arraysize(tests); ++i) {
    std::string mime_type;
    std::string charset;
    std::string data;
    bool ok =
        net::DataURL::Parse(GURL(tests[i].url), &mime_type, &charset, &data);
    EXPECT_EQ(ok, tests[i].is_valid);
    if (tests[i].is_valid) {
      EXPECT_EQ(tests[i].mime_type, mime_type);
      EXPECT_EQ(tests[i].charset, charset);
      EXPECT_EQ(tests[i].data, data);
    }
  }
}