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