Home | History | Annotate | Download | only in tests
      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   RUN_TEST(GetPluginReferrerURL, filter);
     32 }
     33 
     34 std::string TestURLUtil::TestCanonicalize() {
     35   // Test no canonicalize output.
     36   pp::Var result = util_->Canonicalize("http://Google.com");
     37   ASSERT_TRUE(result.AsString() == "http://google.com/");
     38 
     39   // Test all the components
     40   PP_URLComponents_Dev c;
     41   result = util_->Canonicalize(
     42       "http://me:pw@Google.com:1234/path?query#ref ",
     43       &c);
     44   ASSERT_TRUE(result.AsString() ==
     45   //          0         1         2         3         4
     46   //          0123456789012345678901234567890123456789012
     47               "http://me:pw@google.com:1234/path?query#ref");
     48   ASSERT_TRUE(ComponentEquals(c.scheme, 0, 4));
     49   ASSERT_TRUE(ComponentEquals(c.username, 7, 2));
     50   ASSERT_TRUE(ComponentEquals(c.password, 10, 2));
     51   ASSERT_TRUE(ComponentEquals(c.host, 13, 10));
     52   ASSERT_TRUE(ComponentEquals(c.port, 24, 4));
     53   ASSERT_TRUE(ComponentEquals(c.path, 28, 5));
     54   ASSERT_TRUE(ComponentEquals(c.query, 34, 5));
     55   ASSERT_TRUE(ComponentEquals(c.ref, 40, 3));
     56 
     57   // Test minimal components.
     58   result = util_->Canonicalize("http://google.com/", &c);
     59   //                                0         1
     60   //                                0123456789012345678
     61   ASSERT_TRUE(result.AsString() == "http://google.com/");
     62   ASSERT_TRUE(ComponentEquals(c.scheme, 0, 4));
     63   ASSERT_TRUE(ComponentEquals(c.username, 0, -1));
     64   ASSERT_TRUE(ComponentEquals(c.password, 0, -1));
     65   ASSERT_TRUE(ComponentEquals(c.host, 7, 10));
     66   ASSERT_TRUE(ComponentEquals(c.port, 0, -1));
     67   ASSERT_TRUE(ComponentEquals(c.path, 17, 1));
     68   ASSERT_TRUE(ComponentEquals(c.query, 0, -1));
     69   ASSERT_TRUE(ComponentEquals(c.ref, 0, -1));
     70 
     71   PASS();
     72 }
     73 
     74 std::string TestURLUtil::TestResolveRelative() {
     75   const int kTestCount = 6;
     76   struct TestCase {
     77     const char* base;
     78     const char* relative;
     79     const char* expected;  // NULL if
     80   } test_cases[kTestCount] = {
     81     {"http://google.com/", "foo", "http://google.com/foo"},
     82     {"http://google.com/foo", "/bar", "http://google.com/bar"},
     83     {"http://foo/", "http://bar", "http://bar/"},
     84     {"data:foo", "/bar", NULL},
     85     {"data:foo", "http://foo/", "http://foo/"},
     86     {"http://foo/", "", "http://foo/"},
     87   };
     88 
     89   for (int i = 0; i < kTestCount; i++) {
     90     pp::Var result = util_->ResolveRelativeToURL(test_cases[i].base,
     91                                                  test_cases[i].relative);
     92     if (test_cases[i].expected == NULL) {
     93       ASSERT_TRUE(result.is_null());
     94     } else {
     95       ASSERT_TRUE(result.AsString() == test_cases[i].expected);
     96     }
     97   }
     98   PASS();
     99 }
    100 
    101 std::string TestURLUtil::TestIsSameSecurityOrigin() {
    102   ASSERT_FALSE(util_->IsSameSecurityOrigin("http://google.com/",
    103                                            "http://example.com/"));
    104   ASSERT_TRUE(util_->IsSameSecurityOrigin("http://google.com/foo",
    105                                           "http://google.com/bar"));
    106   PASS();
    107 }
    108 
    109 std::string TestURLUtil::TestDocumentCanRequest() {
    110   // This is hard to test, but we can at least verify we can't request
    111   // some random domain.
    112   ASSERT_FALSE(util_->DocumentCanRequest(instance_, "http://evil.com/"));
    113   PASS();
    114 }
    115 
    116 std::string TestURLUtil::TestDocumentCanAccessDocument() {
    117   // This is hard to test, but we can at least verify we can access ourselves.
    118   ASSERT_TRUE(util_->DocumentCanAccessDocument(instance_, instance_));
    119   PASS();
    120 }
    121 
    122 std::string TestURLUtil::TestGetDocumentURL() {
    123   pp::Var url = util_->GetDocumentURL(instance_);
    124   ASSERT_TRUE(url.is_string());
    125   pp::VarPrivate window = instance_->GetWindowObject();
    126   pp::Var href = window.GetProperty("location").GetProperty("href");
    127   ASSERT_TRUE(href.is_string());
    128   // In the test framework, they should be the same.
    129   ASSERT_EQ(url.AsString(), href.AsString());
    130   PASS();
    131 }
    132 
    133 std::string TestURLUtil::TestGetPluginInstanceURL() {
    134   pp::Var url = util_->GetPluginInstanceURL(instance_);
    135   ASSERT_TRUE(url.is_string());
    136   // see test_case.html
    137   ASSERT_EQ(url.AsString(), "http://a.b.c/test");
    138   PASS();
    139 }
    140 
    141 std::string TestURLUtil::TestGetPluginReferrerURL() {
    142   pp::Var url = util_->GetPluginReferrerURL(instance_);
    143   ASSERT_TRUE(url.is_string());
    144   pp::VarPrivate window = instance_->GetWindowObject();
    145   pp::Var href = window.GetProperty("location").GetProperty("href");
    146   ASSERT_TRUE(href.is_string());
    147   ASSERT_EQ(url.AsString(), href.AsString());
    148   PASS();
    149 }
    150 
    151