Chromium Code Reviews| 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 } |
|
mmenke
2014/12/02 19:22:29
No linebreak at the end of a namespace is weird.
| |
| 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, | |
| 26 "", | |
| 27 "", | |
| 28 "" }, | |
| 29 | 24 |
| 30 { "data:,", | 25 {"data:,", true, "text/plain", "US-ASCII", ""}, |
| 31 true, | |
| 32 "text/plain", | |
| 33 "US-ASCII", | |
| 34 "" }, | |
| 35 | 26 |
| 36 { "data:;base64,", | 27 {"data:;base64,", true, "text/plain", "US-ASCII", ""}, |
| 37 true, | |
| 38 "text/plain", | |
| 39 "US-ASCII", | |
| 40 "" }, | |
| 41 | 28 |
| 42 { "data:;charset=,test", | 29 {"data:;charset=,test", false, "", "", ""}, |
| 43 false, | |
| 44 "", | |
| 45 "", | |
| 46 "" }, | |
| 47 | 30 |
| 48 { "data:TeXt/HtMl,<b>x</b>", | 31 {"data:TeXt/HtMl,<b>x</b>", true, "text/html", "US-ASCII", "<b>x</b>"}, |
| 49 true, | |
| 50 "text/html", | |
| 51 "US-ASCII", | |
| 52 "<b>x</b>" }, | |
| 53 | 32 |
| 54 { "data:,foo", | 33 {"data:,foo", true, "text/plain", "US-ASCII", "foo"}, |
| 55 true, | |
| 56 "text/plain", | |
| 57 "US-ASCII", | |
| 58 "foo" }, | |
| 59 | 34 |
| 60 { "data:;base64,aGVsbG8gd29ybGQ=", | 35 {"data:;base64,aGVsbG8gd29ybGQ=", |
| 61 true, | 36 true, |
| 62 "text/plain", | 37 "text/plain", |
| 63 "US-ASCII", | 38 "US-ASCII", |
| 64 "hello world" }, | 39 "hello world"}, |
| 65 | 40 |
| 66 // Allow invalid mediatype for backward compatibility but set mime_type to | 41 // Allow invalid mediatype for backward compatibility but set mime_type to |
| 67 // "text/plain" instead of the invalid mediatype. | 42 // "text/plain" instead of the invalid mediatype. |
| 68 { "data:foo,boo", | 43 {"data:foo,boo", true, "text/plain", "US-ASCII", "boo"}, |
| 69 true, | |
| 70 "text/plain", | |
| 71 "US-ASCII", | |
| 72 "boo" }, | |
| 73 | 44 |
| 74 // When accepting an invalid mediatype, override charset with "US-ASCII" | 45 // When accepting an invalid mediatype, override charset with "US-ASCII" |
| 75 { "data:foo;charset=UTF-8,boo", | 46 {"data:foo;charset=UTF-8,boo", true, "text/plain", "US-ASCII", "boo"}, |
| 76 true, | |
| 77 "text/plain", | |
| 78 "US-ASCII", | |
| 79 "boo" }, | |
| 80 | 47 |
| 81 // Invalid mediatype. Includes a slash but the type part is not a token. | 48 // Invalid mediatype. Includes a slash but the type part is not a token. |
| 82 { "data:f(oo/bar;baz=1;charset=kk,boo", | 49 {"data:f(oo/bar;baz=1;charset=kk,boo", |
| 83 true, | 50 true, |
| 84 "text/plain", | 51 "text/plain", |
| 85 "US-ASCII", | 52 "US-ASCII", |
| 86 "boo" }, | 53 "boo"}, |
| 87 | 54 |
| 88 { "data:foo/bar;baz=1;charset=kk,boo", | 55 {"data:foo/bar;baz=1;charset=kk,boo", true, "foo/bar", "kk", "boo"}, |
| 89 true, | |
| 90 "foo/bar", | |
| 91 "kk", | |
| 92 "boo" }, | |
| 93 | 56 |
| 94 { "data:foo/bar;charset=kk;baz=1,boo", | 57 {"data:foo/bar;charset=kk;baz=1,boo", true, "foo/bar", "kk", "boo"}, |
| 95 true, | |
| 96 "foo/bar", | |
| 97 "kk", | |
| 98 "boo" }, | |
| 99 | 58 |
| 100 { "data:text/html,%3Chtml%3E%3Cbody%3E%3Cb%3Ehello%20world" | 59 {"data:text/html,%3Chtml%3E%3Cbody%3E%3Cb%3Ehello%20world" |
| 101 "%3C%2Fb%3E%3C%2Fbody%3E%3C%2Fhtml%3E", | 60 "%3C%2Fb%3E%3C%2Fbody%3E%3C%2Fhtml%3E", |
| 102 true, | 61 true, |
| 103 "text/html", | 62 "text/html", |
| 104 "US-ASCII", | 63 "US-ASCII", |
| 105 "<html><body><b>hello world</b></body></html>" }, | 64 "<html><body><b>hello world</b></body></html>"}, |
| 106 | 65 |
| 107 { "data:text/html,<html><body><b>hello world</b></body></html>", | 66 {"data:text/html,<html><body><b>hello world</b></body></html>", |
| 108 true, | 67 true, |
| 109 "text/html", | 68 "text/html", |
| 110 "US-ASCII", | 69 "US-ASCII", |
| 111 "<html><body><b>hello world</b></body></html>" }, | 70 "<html><body><b>hello world</b></body></html>"}, |
| 112 | 71 |
| 113 // the comma cannot be url-escaped! | 72 // the comma cannot be url-escaped! |
| 114 { "data:%2Cblah", | 73 {"data:%2Cblah", false, "", "", ""}, |
| 115 false, | |
| 116 "", | |
| 117 "", | |
| 118 "" }, | |
| 119 | 74 |
| 120 // invalid base64 content | 75 // invalid base64 content |
| 121 { "data:;base64,aGVs_-_-", | 76 {"data:;base64,aGVs_-_-", false, "", "", ""}, |
| 122 false, | |
| 123 "", | |
| 124 "", | |
| 125 "" }, | |
| 126 | 77 |
| 127 // Spaces should be removed from non-text data URLs (we already tested | 78 // Spaces should be removed from non-text data URLs (we already tested |
| 128 // spaces above). | 79 // spaces above). |
| 129 { "data:image/fractal,a b c d e f g", | 80 {"data:image/fractal,a b c d e f g", |
| 130 true, | 81 true, |
| 131 "image/fractal", | 82 "image/fractal", |
| 132 "US-ASCII", | 83 "US-ASCII", |
| 133 "abcdefg" }, | 84 "abcdefg"}, |
| 134 | 85 |
| 135 // Spaces should also be removed from anything base-64 encoded | 86 // Spaces should also be removed from anything base-64 encoded |
| 136 { "data:;base64,aGVs bG8gd2 9ybGQ=", | 87 {"data:;base64,aGVs bG8gd2 9ybGQ=", |
| 137 true, | 88 true, |
| 138 "text/plain", | 89 "text/plain", |
| 139 "US-ASCII", | 90 "US-ASCII", |
| 140 "hello world" }, | 91 "hello world"}, |
| 141 | 92 |
| 142 // Other whitespace should also be removed from anything base-64 encoded. | 93 // Other whitespace should also be removed from anything base-64 encoded. |
| 143 { "data:;base64,aGVs bG8gd2 \n9ybGQ=", | 94 {"data:;base64,aGVs bG8gd2 \n9ybGQ=", |
| 144 true, | 95 true, |
| 145 "text/plain", | 96 "text/plain", |
| 146 "US-ASCII", | 97 "US-ASCII", |
| 147 "hello world" }, | 98 "hello world"}, |
| 148 | 99 |
| 149 // In base64 encoding, escaped whitespace should be stripped. | 100 // In base64 encoding, escaped whitespace should be stripped. |
| 150 // (This test was taken from acid3) | 101 // (This test was taken from acid3) |
| 151 // http://b/1054495 | 102 // http://b/1054495 |
| 152 { "data:text/javascript;base64,%20ZD%20Qg%0D%0APS%20An%20Zm91cic%0D%0A%207" | 103 {"data:text/javascript;base64,%20ZD%20Qg%0D%0APS%20An%20Zm91cic%0D%0A%207" |
| 153 "%20", | 104 "%20", |
| 154 true, | 105 true, |
| 155 "text/javascript", | 106 "text/javascript", |
| 156 "US-ASCII", | 107 "US-ASCII", |
| 157 "d4 = 'four';" }, | 108 "d4 = 'four';"}, |
| 158 | 109 |
| 159 // Only unescaped whitespace should be stripped in non-base64. | 110 // Only unescaped whitespace should be stripped in non-base64. |
| 160 // http://b/1157796 | 111 // http://b/1157796 |
| 161 { "data:img/png,A B %20 %0A C", | 112 {"data:img/png,A B %20 %0A C", true, "img/png", "US-ASCII", "AB \nC"}, |
| 162 true, | |
| 163 "img/png", | |
| 164 "US-ASCII", | |
| 165 "AB \nC" }, | |
| 166 | 113 |
| 167 { "data:text/plain;charset=utf-8;base64,SGVsbMO2", | 114 {"data:text/plain;charset=utf-8;base64,SGVsbMO2", |
| 168 true, | 115 true, |
| 169 "text/plain", | 116 "text/plain", |
| 170 "utf-8", | 117 "utf-8", |
| 171 "Hell\xC3\xB6" }, | 118 "Hell\xC3\xB6"}, |
| 172 | 119 |
| 173 // Not sufficiently padded. | 120 // Not sufficiently padded. |
| 174 { "data:;base64,aGVsbG8gd29ybGQ", | 121 {"data:;base64,aGVsbG8gd29ybGQ", |
| 175 true, | 122 true, |
| 176 "text/plain", | 123 "text/plain", |
| 177 "US-ASCII", | 124 "US-ASCII", |
| 178 "hello world" }, | 125 "hello world"}, |
| 179 | 126 |
| 180 // Bad encoding (truncated). | 127 // Bad encoding (truncated). |
| 181 { "data:;base64,aGVsbG8gd29yb", | 128 {"data:;base64,aGVsbG8gd29yb", false, "", "", ""}, |
| 182 false, | |
| 183 "", | |
| 184 "", | |
| 185 "" }, | |
| 186 | 129 |
| 187 // BiDi control characters should be unescaped and preserved as is, and | 130 // BiDi control characters should be unescaped and preserved as is, and |
| 188 // should not be replaced with % versions. In the below case, \xE2\x80\x8F | 131 // should not be replaced with % versions. In the below case, \xE2\x80\x8F |
| 189 // is the RTL mark and the parsed text should preserve it as is. | 132 // is the RTL mark and the parsed text should preserve it as is. |
| 190 { | 133 {"data:text/plain;charset=utf-8,\xE2\x80\x8Ftest", |
| 191 "data:text/plain;charset=utf-8,\xE2\x80\x8Ftest", | 134 true, |
| 192 true, | 135 "text/plain", |
| 193 "text/plain", | 136 "utf-8", |
| 194 "utf-8", | 137 "\xE2\x80\x8Ftest"}, |
| 195 "\xE2\x80\x8Ftest"}, | |
| 196 | 138 |
| 197 // Same as above but with Arabic text after RTL mark. | 139 // Same as above but with Arabic text after RTL mark. |
| 198 { | 140 {"data:text/plain;charset=utf-8," |
| 199 "data:text/plain;charset=utf-8," | 141 "\xE2\x80\x8F\xD8\xA7\xD8\xAE\xD8\xAA\xD8\xA8\xD8\xA7\xD8\xB1", |
| 200 "\xE2\x80\x8F\xD8\xA7\xD8\xAE\xD8\xAA\xD8\xA8\xD8\xA7\xD8\xB1", | 142 true, |
| 201 true, | 143 "text/plain", |
| 202 "text/plain", | 144 "utf-8", |
| 203 "utf-8", | 145 "\xE2\x80\x8F\xD8\xA7\xD8\xAE\xD8\xAA\xD8\xA8\xD8\xA7\xD8\xB1"}, |
| 204 "\xE2\x80\x8F\xD8\xA7\xD8\xAE\xD8\xAA\xD8\xA8\xD8\xA7\xD8\xB1"}, | |
| 205 | 146 |
| 206 // RTL mark encoded as %E2%80%8F should be unescaped too. Note that when | 147 // RTL mark encoded as %E2%80%8F should be unescaped too. Note that when |
| 207 // wrapped in a GURL, this URL and the next effectively become the same as | 148 // wrapped in a GURL, this URL and the next effectively become the same as |
| 208 // the previous two URLs. | 149 // the previous two URLs. |
| 209 { | 150 {"data:text/plain;charset=utf-8,%E2%80%8Ftest", |
| 210 "data:text/plain;charset=utf-8,%E2%80%8Ftest", | 151 true, |
| 211 true, | 152 "text/plain", |
| 212 "text/plain", | 153 "utf-8", |
| 213 "utf-8", | 154 "\xE2\x80\x8Ftest"}, |
| 214 "\xE2\x80\x8Ftest"}, | |
| 215 | 155 |
| 216 // Same as above but with Arabic text after RTL mark. | 156 // Same as above but with Arabic text after RTL mark. |
| 217 { | 157 {"data:text/plain;charset=utf-8," |
| 218 "data:text/plain;charset=utf-8," | 158 "%E2%80%8F\xD8\xA7\xD8\xAE\xD8\xAA\xD8\xA8\xD8\xA7\xD8\xB1", |
| 219 "%E2%80%8F\xD8\xA7\xD8\xAE\xD8\xAA\xD8\xA8\xD8\xA7\xD8\xB1", | 159 true, |
| 220 true, | 160 "text/plain", |
| 221 "text/plain", | 161 "utf-8", |
| 222 "utf-8", | 162 "\xE2\x80\x8F\xD8\xA7\xD8\xAE\xD8\xAA\xD8\xA8\xD8\xA7\xD8\xB1"} |
| 223 "\xE2\x80\x8F\xD8\xA7\xD8\xAE\xD8\xAA\xD8\xA8\xD8\xA7\xD8\xB1"} | |
| 224 | 163 |
| 225 // TODO(darin): add more interesting tests | 164 // TODO(darin): add more interesting tests |
| 226 }; | 165 }; |
| 227 | 166 |
| 228 for (size_t i = 0; i < arraysize(tests); ++i) { | 167 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 229 std::string mime_type; | 168 std::string mime_type; |
| 230 std::string charset; | 169 std::string charset; |
| 231 std::string data; | 170 std::string data; |
| 232 bool ok = | 171 bool ok = |
| 233 net::DataURL::Parse(GURL(tests[i].url), &mime_type, &charset, &data); | 172 net::DataURL::Parse(GURL(tests[i].url), &mime_type, &charset, &data); |
| 234 EXPECT_EQ(ok, tests[i].is_valid); | 173 EXPECT_EQ(ok, tests[i].is_valid); |
| 235 if (tests[i].is_valid) { | 174 if (tests[i].is_valid) { |
| 236 EXPECT_EQ(tests[i].mime_type, mime_type); | 175 EXPECT_EQ(tests[i].mime_type, mime_type); |
| 237 EXPECT_EQ(tests[i].charset, charset); | 176 EXPECT_EQ(tests[i].charset, charset); |
| 238 EXPECT_EQ(tests[i].data, data); | 177 EXPECT_EQ(tests[i].data, data); |
| 239 } | 178 } |
| 240 } | 179 } |
| 241 } | 180 } |
| OLD | NEW |