WebKit roll 98705:98715
[chromium-blink-merge.git] / ppapi / tests / test_url_util.cc
blobdc6cb4737736853f2bbca1a7b671a6bdc99b265e
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.
5 #include "ppapi/tests/test_url_util.h"
7 #include "ppapi/c/dev/ppb_url_util_dev.h"
8 #include "ppapi/cpp/dev/url_util_dev.h"
9 #include "ppapi/tests/testing_instance.h"
11 REGISTER_TEST_CASE(URLUtil);
13 static bool ComponentEquals(const PP_URLComponent_Dev& component,
14 int begin, int len) {
15 return component.begin == begin && component.len == len;
18 bool TestURLUtil::Init() {
19 util_ = pp::URLUtil_Dev::Get();
20 return !!util_;
23 void TestURLUtil::RunTest() {
24 RUN_TEST(Canonicalize);
25 RUN_TEST(ResolveRelative);
26 RUN_TEST(IsSameSecurityOrigin);
27 RUN_TEST(DocumentCanRequest);
28 RUN_TEST(DocumentCanAccessDocument);
29 RUN_TEST(GetDocumentURL);
30 RUN_TEST(GetPluginInstanceURL);
33 std::string TestURLUtil::TestCanonicalize() {
34 // Test no canonicalize output.
35 pp::Var result = util_->Canonicalize("http://Google.com");
36 ASSERT_TRUE(result.AsString() == "http://google.com/");
38 // Test all the components
39 PP_URLComponents_Dev c;
40 result = util_->Canonicalize(
41 "http://me:pw@Google.com:1234/path?query#ref ",
42 &c);
43 ASSERT_TRUE(result.AsString() ==
44 // 0 1 2 3 4
45 // 0123456789012345678901234567890123456789012
46 "http://me:pw@google.com:1234/path?query#ref");
47 ASSERT_TRUE(ComponentEquals(c.scheme, 0, 4));
48 ASSERT_TRUE(ComponentEquals(c.username, 7, 2));
49 ASSERT_TRUE(ComponentEquals(c.password, 10, 2));
50 ASSERT_TRUE(ComponentEquals(c.host, 13, 10));
51 ASSERT_TRUE(ComponentEquals(c.port, 24, 4));
52 ASSERT_TRUE(ComponentEquals(c.path, 28, 5));
53 ASSERT_TRUE(ComponentEquals(c.query, 34, 5));
54 ASSERT_TRUE(ComponentEquals(c.ref, 40, 3));
56 // Test minimal components.
57 result = util_->Canonicalize("http://google.com/", &c);
58 // 0 1
59 // 0123456789012345678
60 ASSERT_TRUE(result.AsString() == "http://google.com/");
61 ASSERT_TRUE(ComponentEquals(c.scheme, 0, 4));
62 ASSERT_TRUE(ComponentEquals(c.username, 0, -1));
63 ASSERT_TRUE(ComponentEquals(c.password, 0, -1));
64 ASSERT_TRUE(ComponentEquals(c.host, 7, 10));
65 ASSERT_TRUE(ComponentEquals(c.port, 0, -1));
66 ASSERT_TRUE(ComponentEquals(c.path, 17, 1));
67 ASSERT_TRUE(ComponentEquals(c.query, 0, -1));
68 ASSERT_TRUE(ComponentEquals(c.ref, 0, -1));
70 PASS();
73 std::string TestURLUtil::TestResolveRelative() {
74 const int kTestCount = 6;
75 struct TestCase {
76 const char* base;
77 const char* relative;
78 const char* expected; // NULL if
79 } test_cases[kTestCount] = {
80 {"http://google.com/", "foo", "http://google.com/foo"},
81 {"http://google.com/foo", "/bar", "http://google.com/bar"},
82 {"http://foo/", "http://bar", "http://bar/"},
83 {"data:foo", "/bar", NULL},
84 {"data:foo", "http://foo/", "http://foo/"},
85 {"http://foo/", "", "http://foo/"},
88 for (int i = 0; i < kTestCount; i++) {
89 pp::Var result = util_->ResolveRelativeToURL(test_cases[i].base,
90 test_cases[i].relative);
91 if (test_cases[i].expected == NULL) {
92 ASSERT_TRUE(result.is_null());
93 } else {
94 ASSERT_TRUE(result.AsString() == test_cases[i].expected);
97 PASS();
100 std::string TestURLUtil::TestIsSameSecurityOrigin() {
101 ASSERT_FALSE(util_->IsSameSecurityOrigin("http://google.com/",
102 "http://example.com/"));
103 ASSERT_TRUE(util_->IsSameSecurityOrigin("http://google.com/foo",
104 "http://google.com/bar"));
105 PASS();
108 std::string TestURLUtil::TestDocumentCanRequest() {
109 // This is hard to test, but we can at least verify we can't request
110 // some random domain.
111 ASSERT_FALSE(util_->DocumentCanRequest(*instance_, "http://evil.com/"));
112 PASS();
115 std::string TestURLUtil::TestDocumentCanAccessDocument() {
116 // This is hard to test, but we can at least verify we can access ourselves.
117 ASSERT_TRUE(util_->DocumentCanAccessDocument(*instance_, *instance_));
118 PASS();
121 std::string TestURLUtil::TestGetDocumentURL() {
122 pp::Var url = util_->GetDocumentURL(*instance_);
123 ASSERT_TRUE(url.is_string());
124 pp::VarPrivate window = instance_->GetWindowObject();
125 pp::Var href = window.GetProperty("location").GetProperty("href");
126 ASSERT_TRUE(href.is_string());
127 // In the test framework, they should be the same.
128 ASSERT_EQ(url.AsString(), href.AsString());
129 PASS();
132 std::string TestURLUtil::TestGetPluginInstanceURL() {
133 pp::Var url = util_->GetPluginInstanceURL(*instance_);
134 ASSERT_TRUE(url.is_string());
135 // see test_case.html
136 ASSERT_EQ(url.AsString(), "http://a.b.c/test");
137 PASS();