]> git.sesse.net Git - casparcg/blob - dependencies64/cef/linux/tests/ceftests/parser_unittest.cc
aa39ef3ab3fe1dc3a9a478a3ed2ca911f2f0491b
[casparcg] / dependencies64 / cef / linux / tests / ceftests / parser_unittest.cc
1 // Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4
5 #include "include/cef_parser.h"
6 #include "tests/gtest/include/gtest/gtest.h"
7
8 // Create the URL using the spec.
9 TEST(ParserTest, CreateURLSpec) {
10   CefURLParts parts;
11   CefString url;
12   CefString(&parts.spec).FromASCII(
13       "http://user:pass@www.example.com:88/path/to.html?foo=test&bar=test2");
14   EXPECT_TRUE(CefCreateURL(parts, url));
15   EXPECT_STREQ(
16       "http://user:pass@www.example.com:88/path/to.html?foo=test&bar=test2",
17       url.ToString().c_str());
18 }
19
20 // Test that host is required.
21 TEST(ParserTest, CreateURLHostRequired) {
22   CefURLParts parts;
23   CefString url;
24   CefString(&parts.scheme).FromASCII("http");
25   EXPECT_FALSE(CefCreateURL(parts, url));
26 }
27
28 // Test that scheme is required.
29 TEST(ParserTest, CreateURLSchemeRequired) {
30   CefURLParts parts;
31   CefString url;
32   CefString(&parts.host).FromASCII("www.example.com");
33   EXPECT_FALSE(CefCreateURL(parts, url));
34 }
35
36 // Create the URL using scheme and host.
37 TEST(ParserTest, CreateURLSchemeHost) {
38   CefURLParts parts;
39   CefString url;
40   CefString(&parts.scheme).FromASCII("http");
41   CefString(&parts.host).FromASCII("www.example.com");
42   EXPECT_TRUE(CefCreateURL(parts, url));
43   EXPECT_STREQ("http://www.example.com/", url.ToString().c_str());
44 }
45
46 // Create the URL using scheme, host and path.
47 TEST(ParserTest, CreateURLSchemeHostPath) {
48   CefURLParts parts;
49   CefString url;
50   CefString(&parts.scheme).FromASCII("http");
51   CefString(&parts.host).FromASCII("www.example.com");
52   CefString(&parts.path).FromASCII("/path/to.html");
53   EXPECT_TRUE(CefCreateURL(parts, url));
54   EXPECT_STREQ("http://www.example.com/path/to.html", url.ToString().c_str());
55 }
56
57 // Create the URL using scheme, host, path and query.
58 TEST(ParserTest, CreateURLSchemeHostPathQuery) {
59   CefURLParts parts;
60   CefString url;
61   CefString(&parts.scheme).FromASCII("http");
62   CefString(&parts.host).FromASCII("www.example.com");
63   CefString(&parts.path).FromASCII("/path/to.html");
64   CefString(&parts.query).FromASCII("foo=test&bar=test2");
65   EXPECT_TRUE(CefCreateURL(parts, url));
66   EXPECT_STREQ("http://www.example.com/path/to.html?foo=test&bar=test2",
67                 url.ToString().c_str());
68 }
69
70 // Create the URL using all the various components.
71 TEST(ParserTest, CreateURLAll) {
72   CefURLParts parts;
73   CefString url;
74   CefString(&parts.scheme).FromASCII("http");
75   CefString(&parts.username).FromASCII("user");
76   CefString(&parts.password).FromASCII("pass");
77   CefString(&parts.host).FromASCII("www.example.com");
78   CefString(&parts.port).FromASCII("88");
79   CefString(&parts.path).FromASCII("/path/to.html");
80   CefString(&parts.query).FromASCII("foo=test&bar=test2");
81   EXPECT_TRUE(CefCreateURL(parts, url));
82   EXPECT_STREQ(
83       "http://user:pass@www.example.com:88/path/to.html?foo=test&bar=test2",
84       url.ToString().c_str());
85 }
86
87 // Parse the URL using scheme and host.
88 TEST(ParserTest, ParseURLSchemeHost) {
89   CefURLParts parts;
90   CefString url;
91   url.FromASCII("http://www.example.com");
92   EXPECT_TRUE(CefParseURL(url, parts));
93
94   CefString spec(&parts.spec);
95   EXPECT_STREQ("http://www.example.com/", spec.ToString().c_str());
96   EXPECT_EQ(0U, parts.username.length);
97   EXPECT_EQ(0U, parts.password.length);
98   CefString scheme(&parts.scheme);
99   EXPECT_STREQ("http", scheme.ToString().c_str());
100   CefString host(&parts.host);
101   EXPECT_STREQ("www.example.com", host.ToString().c_str());
102   EXPECT_EQ(0U, parts.port.length);
103   CefString origin(&parts.origin);
104   EXPECT_STREQ(origin.ToString().c_str(), "http://www.example.com/");
105   CefString path(&parts.path);
106   EXPECT_STREQ("/", path.ToString().c_str());
107   EXPECT_EQ(0U, parts.query.length);
108 }
109
110 // Parse the URL using scheme, host and path.
111 TEST(ParserTest, ParseURLSchemeHostPath) {
112   CefURLParts parts;
113   CefString url;
114   url.FromASCII("http://www.example.com/path/to.html");
115   EXPECT_TRUE(CefParseURL(url, parts));
116
117   CefString spec(&parts.spec);
118   EXPECT_STREQ("http://www.example.com/path/to.html",
119                 spec.ToString().c_str());
120   EXPECT_EQ(0U, parts.username.length);
121   EXPECT_EQ(0U, parts.password.length);
122   CefString scheme(&parts.scheme);
123   EXPECT_STREQ("http", scheme.ToString().c_str());
124   CefString host(&parts.host);
125   EXPECT_STREQ("www.example.com", host.ToString().c_str());
126   EXPECT_EQ(0U, parts.port.length);
127   CefString origin(&parts.origin);
128   EXPECT_STREQ(origin.ToString().c_str(), "http://www.example.com/");
129   CefString path(&parts.path);
130   EXPECT_STREQ("/path/to.html", path.ToString().c_str());
131   EXPECT_EQ(0U, parts.query.length);
132 }
133
134 // Parse the URL using scheme, host, path and query.
135 TEST(ParserTest, ParseURLSchemeHostPathQuery) {
136   CefURLParts parts;
137   CefString url;
138   url.FromASCII("http://www.example.com/path/to.html?foo=test&bar=test2");
139   EXPECT_TRUE(CefParseURL(url, parts));
140
141   CefString spec(&parts.spec);
142   EXPECT_STREQ("http://www.example.com/path/to.html?foo=test&bar=test2",
143                 spec.ToString().c_str());
144   EXPECT_EQ(0U, parts.username.length);
145   EXPECT_EQ(0U, parts.password.length);
146   CefString scheme(&parts.scheme);
147   EXPECT_STREQ("http", scheme.ToString().c_str());
148   CefString host(&parts.host);
149   EXPECT_STREQ("www.example.com", host.ToString().c_str());
150   EXPECT_EQ(0U, parts.port.length);
151   CefString origin(&parts.origin);
152   EXPECT_STREQ(origin.ToString().c_str(), "http://www.example.com/");
153   CefString path(&parts.path);
154   EXPECT_STREQ("/path/to.html", path.ToString().c_str());
155   CefString query(&parts.query);
156   EXPECT_STREQ("foo=test&bar=test2", query.ToString().c_str());
157 }
158
159 // Parse the URL using all the various components.
160 TEST(ParserTest, ParseURLAll) {
161   CefURLParts parts;
162   CefString url;
163   url.FromASCII(
164       "http://user:pass@www.example.com:88/path/to.html?foo=test&bar=test2");
165   EXPECT_TRUE(CefParseURL(url, parts));
166
167   CefString spec(&parts.spec);
168   EXPECT_STREQ(
169       "http://user:pass@www.example.com:88/path/to.html?foo=test&bar=test2",
170       spec.ToString().c_str());
171   CefString scheme(&parts.scheme);
172   EXPECT_STREQ("http", scheme.ToString().c_str());
173   CefString username(&parts.username);
174   EXPECT_STREQ("user", username.ToString().c_str());
175   CefString password(&parts.password);
176   EXPECT_STREQ("pass", password.ToString().c_str());
177   CefString host(&parts.host);
178   EXPECT_STREQ("www.example.com", host.ToString().c_str());
179   CefString port(&parts.port);
180   EXPECT_STREQ("88", port.ToString().c_str());
181   CefString origin(&parts.origin);
182   EXPECT_STREQ(origin.ToString().c_str(), "http://www.example.com:88/");
183   CefString path(&parts.path);
184   EXPECT_STREQ("/path/to.html", path.ToString().c_str());
185   CefString query(&parts.query);
186   EXPECT_STREQ("foo=test&bar=test2", query.ToString().c_str());
187 }
188
189 // Parse an invalid URL.
190 TEST(ParserTest, ParseURLInvalid) {
191   CefURLParts parts;
192   CefString url;
193   url.FromASCII("www.example.com");
194   EXPECT_FALSE(CefParseURL(url, parts));
195 }
196
197 // Parse a non-standard scheme.
198 TEST(ParserTest, ParseURLNonStandard) {
199   CefURLParts parts;
200   CefString url;
201   url.FromASCII("custom:something%20else?foo");
202   EXPECT_TRUE(CefParseURL(url, parts));
203
204   CefString spec(&parts.spec);
205   EXPECT_STREQ("custom:something%20else?foo", spec.ToString().c_str());
206   EXPECT_EQ(0U, parts.username.length);
207   EXPECT_EQ(0U, parts.password.length);
208   CefString scheme(&parts.scheme);
209   EXPECT_STREQ("custom", scheme.ToString().c_str());
210   EXPECT_EQ(0U, parts.host.length);
211   EXPECT_EQ(0U, parts.port.length);
212   EXPECT_EQ(0U, parts.origin.length);
213   CefString path(&parts.path);
214   EXPECT_STREQ("something%20else", path.ToString().c_str());
215   CefString query(&parts.query);
216   EXPECT_STREQ("foo", query.ToString().c_str());
217 }
218
219 TEST(ParserTest, FormatUrlForSecurityDisplay) {
220   CefString result;
221
222   // Omits the protocol if it's standard.
223   result = CefFormatUrlForSecurityDisplay("http://tests.com/foo.html");
224   EXPECT_STREQ("http://tests.com", result.ToString().c_str());
225
226   // Omits the port if it's the expected value for the protocol.
227   result = CefFormatUrlForSecurityDisplay("http://tests.com:80/foo.html");
228   EXPECT_STREQ("http://tests.com", result.ToString().c_str());
229
230   // Don't omit non-standard ports.
231   result = CefFormatUrlForSecurityDisplay("http://tests.com:8088/foo.html");
232   EXPECT_STREQ("http://tests.com:8088", result.ToString().c_str());
233
234   // Don't omit the protocol for file URLs.
235   result = CefFormatUrlForSecurityDisplay("file:///c/tests/foo.html");
236   EXPECT_STREQ("file:///c/tests/foo.html", result.ToString().c_str());
237 }
238
239 TEST(ParserTest, GetMimeType) {
240   CefString mime_type;
241
242   mime_type = CefGetMimeType("html");
243   EXPECT_STREQ("text/html", mime_type.ToString().c_str());
244
245   mime_type = CefGetMimeType("txt");
246   EXPECT_STREQ("text/plain", mime_type.ToString().c_str());
247
248   mime_type = CefGetMimeType("gif");
249   EXPECT_STREQ("image/gif", mime_type.ToString().c_str());
250 }
251
252 TEST(ParserTest, Base64Encode) {
253   const std::string& test_str_decoded = "A test string";
254   const std::string& test_str_encoded = "QSB0ZXN0IHN0cmluZw==";
255   const CefString& encoded_value =
256       CefBase64Encode(test_str_decoded.data(), test_str_decoded.size());
257   EXPECT_STREQ(test_str_encoded.c_str(), encoded_value.ToString().c_str());
258 }
259
260 TEST(ParserTest, Base64Decode) {
261   const std::string& test_str_decoded = "A test string";
262   const std::string& test_str_encoded = "QSB0ZXN0IHN0cmluZw==";
263   CefRefPtr<CefBinaryValue> decoded_value = CefBase64Decode(test_str_encoded);
264   EXPECT_TRUE(decoded_value.get());
265
266   const size_t decoded_size = decoded_value->GetSize();
267   EXPECT_EQ(test_str_decoded.size(), decoded_size);
268
269   std::string decoded_str;
270   decoded_str.resize(decoded_size + 1);  // Include space for NUL-terminator.
271   const size_t get_data_result =
272       decoded_value->GetData(const_cast<char*>(decoded_str.data()),
273                              decoded_size, 0);
274   EXPECT_EQ(decoded_size, get_data_result);
275   EXPECT_STREQ(test_str_decoded.c_str(), decoded_str.c_str());
276 }
277
278 TEST(ParserTest, URIEncode) {
279   const std::string& test_str_decoded = "A test string=";
280   const std::string& test_str_encoded = "A%20test%20string%3D";
281   const CefString& encoded_value = CefURIEncode(test_str_decoded, false);
282   EXPECT_STREQ(test_str_encoded.c_str(), encoded_value.ToString().c_str());
283 }
284
285 TEST(ParserTest, URIDecode) {
286   const std::string& test_str_decoded = "A test string=";
287   const std::string& test_str_encoded = "A%20test%20string%3D";
288   const CefString& decoded_value =
289       CefURIDecode(test_str_encoded, false,
290                    static_cast<cef_uri_unescape_rule_t>(
291                        UU_SPACES |
292                        UU_URL_SPECIAL_CHARS_EXCEPT_PATH_SEPARATORS));
293   EXPECT_STREQ(test_str_decoded.c_str(), decoded_value.ToString().c_str());
294 }
295
296 TEST(ParserTest, ParseJSONInvalid) {
297   const char data[] = "This is my test data";
298   CefRefPtr<CefValue> value = CefParseJSON(data, JSON_PARSER_RFC);
299   EXPECT_FALSE(value.get());
300 }
301
302 TEST(ParserTest, ParseJSONNull) {
303   const char data[] = "{\"key1\":null}";
304   CefRefPtr<CefValue> value = CefParseJSON(data, JSON_PARSER_RFC);
305   EXPECT_TRUE(value.get());
306   EXPECT_TRUE(value->IsValid());
307   EXPECT_TRUE(value->GetType() == VTYPE_DICTIONARY);
308   EXPECT_FALSE(value->IsOwned());
309   CefRefPtr<CefDictionaryValue> dict = value->GetDictionary();
310   CefDictionaryValue::KeyList key_list;
311   EXPECT_TRUE(dict->GetKeys(key_list));
312   EXPECT_EQ(1U, key_list.size());
313   EXPECT_EQ("key1", key_list[0].ToString());
314   EXPECT_EQ(VTYPE_NULL, dict->GetType("key1"));
315
316   // generate string from parsed result
317   CefString result = CefWriteJSON(value, JSON_WRITER_DEFAULT);
318   CefString expected_result = data;
319   EXPECT_EQ(expected_result, result);
320 }
321
322 TEST(ParserTest, WriteJSONBinary) {
323   const char data[] = "\00\01\02";
324   CefRefPtr<CefDictionaryValue> dict = CefDictionaryValue::Create();
325   CefRefPtr<CefBinaryValue> binary = CefBinaryValue::Create(data, sizeof(data));
326   dict->SetBinary("key1", binary);
327   CefRefPtr<CefValue> node = CefValue::Create();
328   node->SetDictionary(dict);
329   CefString result = CefWriteJSON(node, JSON_WRITER_DEFAULT);
330   CefString expect_result = "";
331   // binary data will be omitted.
332   EXPECT_EQ(expect_result, result);
333 }
334
335 TEST(ParserTest, ParseJSONDictionary) {
336   const char data[] = "{\"key1\":\"value1\",\"key2\":123,\"key3\":[1,2,3]}";
337   CefRefPtr<CefValue> value = CefParseJSON(data, JSON_PARSER_RFC);
338   EXPECT_TRUE(value.get());
339   EXPECT_TRUE(value->IsValid());
340   EXPECT_FALSE(value->IsOwned());
341   EXPECT_TRUE(value->GetType() == VTYPE_DICTIONARY);
342   CefRefPtr<CefDictionaryValue> dict = value->GetDictionary();
343   CefDictionaryValue::KeyList key_list;
344   EXPECT_TRUE(dict->GetKeys(key_list));
345   EXPECT_EQ(3U, key_list.size());
346   EXPECT_EQ("key1", key_list[0].ToString());
347   EXPECT_EQ("key2", key_list[1].ToString());
348   EXPECT_EQ("key3", key_list[2].ToString());
349   EXPECT_EQ(VTYPE_STRING, dict->GetType("key1"));
350   EXPECT_EQ(dict->GetString("key1"), "value1");
351   EXPECT_EQ(VTYPE_INT, dict->GetType("key2"));
352   EXPECT_EQ(123, dict->GetInt("key2"));
353   EXPECT_EQ(VTYPE_LIST, dict->GetType("key3"));
354   CefRefPtr<CefListValue> key3 = dict->GetList("key3");
355   EXPECT_TRUE(NULL != key3);
356   EXPECT_TRUE(key3->IsValid());
357   EXPECT_EQ(3U, key3->GetSize());
358   EXPECT_EQ(1, key3->GetInt(0));
359   EXPECT_EQ(2, key3->GetInt(1));
360   EXPECT_EQ(3, key3->GetInt(2));
361
362   // generate string from parsed result
363   CefString result = CefWriteJSON(value, JSON_WRITER_DEFAULT);
364   CefString expected_result = data;
365   EXPECT_EQ(expected_result, result);
366 }
367
368 TEST(ParserTest, ParseJSONList) {
369   const char data[] = "[\"value1\", 123, {\"key3\": [1, 2, 3]}]";
370   CefRefPtr<CefValue> value = CefParseJSON(data, JSON_PARSER_RFC);
371   EXPECT_TRUE(value.get());
372   EXPECT_TRUE(value->IsValid());
373   EXPECT_TRUE(value->GetType() == VTYPE_LIST);
374   EXPECT_FALSE(value->IsOwned());
375   CefRefPtr<CefListValue> list = value->GetList();
376   EXPECT_TRUE(NULL != list);
377   EXPECT_TRUE(list->IsValid());
378   EXPECT_EQ(3U, list->GetSize());
379
380   EXPECT_EQ(VTYPE_STRING, list->GetType(0));
381   EXPECT_EQ(list->GetString(0), "value1");
382   EXPECT_EQ(VTYPE_INT, list->GetType(1));
383   EXPECT_EQ(123, list->GetInt(1));
384   EXPECT_EQ(VTYPE_DICTIONARY, list->GetType(2));
385   CefRefPtr<CefDictionaryValue> dict = list->GetDictionary(2);
386   CefDictionaryValue::KeyList key_list2;
387   EXPECT_TRUE(dict->GetKeys(key_list2));
388   EXPECT_EQ(1U, key_list2.size());
389   CefRefPtr<CefListValue> list2 = dict->GetList("key3");
390   EXPECT_EQ(3U, list2->GetSize());
391   EXPECT_EQ(1, list2->GetInt(0));
392   EXPECT_EQ(2, list2->GetInt(1));
393   EXPECT_EQ(3, list2->GetInt(2));
394
395   // generate string from parsed result
396   CefString result = CefWriteJSON(value, JSON_WRITER_DEFAULT);
397   CefString expected_result = "[\"value1\",123,{\"key3\":[1,2,3]}]";
398   EXPECT_EQ(expected_result.ToString(), result.ToString());
399 }
400
401 TEST(ParserTest, ParseJSONAndReturnErrorInvalid) {
402   const char data[] = "This is my test data";
403   cef_json_parser_error_t error_code;
404   CefString error_msg;
405   CefRefPtr<CefValue> value = CefParseJSONAndReturnError(data,
406       JSON_PARSER_RFC, error_code, error_msg);
407   CefString expect_error_msg = "Line: 1, column: 1, Unexpected token.";
408   EXPECT_FALSE(value.get());
409   EXPECT_EQ(JSON_UNEXPECTED_TOKEN, error_code);
410   EXPECT_EQ(expect_error_msg, error_msg);
411 }
412
413 TEST(ParserTest, ParseJSONAndReturnErrorTrailingComma) {
414   const char data[] = "{\"key1\":123,}";
415   cef_json_parser_error_t error_code;
416   CefString error_msg;
417   CefRefPtr<CefValue> value = CefParseJSONAndReturnError(data,
418       JSON_PARSER_RFC, error_code, error_msg);
419   CefString expect_error_msg =
420       "Line: 1, column: 13, Trailing comma not allowed.";
421   EXPECT_FALSE(value.get());
422   EXPECT_EQ(JSON_TRAILING_COMMA, error_code);
423   EXPECT_EQ(expect_error_msg, error_msg);
424 }