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. 4 5 #include "ppapi/tests/test_url_util.h" 6 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" 10 11 REGISTER_TEST_CASE(URLUtil); 12 13 static bool ComponentEquals(const PP_URLComponent_Dev& component, 14 int begin, int len) { 15 return component.begin == begin && component.len == len; 16 } 17 18 bool TestURLUtil::Init() { 19 util_ = pp::URLUtil_Dev::Get(); 20 return !!util_; 21 } 22 23 void TestURLUtil::RunTests(const std::string& filter) { 24 RUN_TEST(Canonicalize, filter); 25 RUN_TEST(ResolveRelative, filter); 26 RUN_TEST(IsSameSecurityOrigin, filter); 27 RUN_TEST(DocumentCanRequest, filter); 28 RUN_TEST(DocumentCanAccessDocument, filter); 29 RUN_TEST(GetDocumentURL, filter); 30 RUN_TEST(GetPluginInstanceURL, filter); 31 } 32 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/"); 37 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)); 55 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)); 69 70 PASS(); 71 } 72 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/"}, 86 }; 87 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); 95 } 96 } 97 PASS(); 98 } 99 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(); 106 } 107 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(); 113 } 114 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(); 119 } 120 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(); 130 } 131 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(); 138 } 139