test_url_util.cc 5.1 KB

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