TestStandardURL.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "gtest/gtest.h"
  2. #include "gtest/MozGTestBench.h" // For MOZ_GTEST_BENCH
  3. #include "nsCOMPtr.h"
  4. #include "nsNetCID.h"
  5. #include "nsIURL.h"
  6. #include "nsIStandardURL.h"
  7. #include "nsString.h"
  8. #include "nsComponentManagerUtils.h"
  9. #include "nsIIPCSerializableURI.h"
  10. #include "mozilla/ipc/URIUtils.h"
  11. #include "mozilla/Unused.h"
  12. #include "nsSerializationHelper.h"
  13. #include "mozilla/Base64.h"
  14. #include "nsEscape.h"
  15. TEST(TestStandardURL, Simple) {
  16. nsCOMPtr<nsIURL> url( do_CreateInstance(NS_STANDARDURL_CONTRACTID) );
  17. ASSERT_TRUE(url);
  18. ASSERT_EQ(url->SetSpec(NS_LITERAL_CSTRING("http://example.com")), NS_OK);
  19. nsAutoCString out;
  20. ASSERT_EQ(url->GetSpec(out), NS_OK);
  21. ASSERT_TRUE(out == NS_LITERAL_CSTRING("http://example.com/"));
  22. ASSERT_EQ(url->Resolve(NS_LITERAL_CSTRING("foo.html?q=45"), out), NS_OK);
  23. ASSERT_TRUE(out == NS_LITERAL_CSTRING("http://example.com/foo.html?q=45"));
  24. ASSERT_EQ(url->SetScheme(NS_LITERAL_CSTRING("foo")), NS_OK);
  25. ASSERT_EQ(url->GetScheme(out), NS_OK);
  26. ASSERT_TRUE(out == NS_LITERAL_CSTRING("foo"));
  27. ASSERT_EQ(url->GetHost(out), NS_OK);
  28. ASSERT_TRUE(out == NS_LITERAL_CSTRING("example.com"));
  29. ASSERT_EQ(url->SetHost(NS_LITERAL_CSTRING("www.yahoo.com")), NS_OK);
  30. ASSERT_EQ(url->GetHost(out), NS_OK);
  31. ASSERT_TRUE(out == NS_LITERAL_CSTRING("www.yahoo.com"));
  32. ASSERT_EQ(url->SetPath(NS_LITERAL_CSTRING("/some-path/one-the-net/about.html?with-a-query#for-you")), NS_OK);
  33. ASSERT_EQ(url->GetPath(out), NS_OK);
  34. ASSERT_TRUE(out == NS_LITERAL_CSTRING("/some-path/one-the-net/about.html?with-a-query#for-you"));
  35. ASSERT_EQ(url->SetQuery(NS_LITERAL_CSTRING("a=b&d=c&what-ever-you-want-to-be-called=45")), NS_OK);
  36. ASSERT_EQ(url->GetQuery(out), NS_OK);
  37. ASSERT_TRUE(out == NS_LITERAL_CSTRING("a=b&d=c&what-ever-you-want-to-be-called=45"));
  38. ASSERT_EQ(url->SetRef(NS_LITERAL_CSTRING("#some-book-mark")), NS_OK);
  39. ASSERT_EQ(url->GetRef(out), NS_OK);
  40. ASSERT_TRUE(out == NS_LITERAL_CSTRING("some-book-mark"));
  41. }
  42. #define COUNT 10000
  43. MOZ_GTEST_BENCH(TestStandardURL, Perf, [] {
  44. nsCOMPtr<nsIURL> url( do_CreateInstance(NS_STANDARDURL_CONTRACTID) );
  45. ASSERT_TRUE(url);
  46. nsAutoCString out;
  47. for (int i = COUNT; i; --i) {
  48. ASSERT_EQ(url->SetSpec(NS_LITERAL_CSTRING("http://example.com")), NS_OK);
  49. ASSERT_EQ(url->GetSpec(out), NS_OK);
  50. url->Resolve(NS_LITERAL_CSTRING("foo.html?q=45"), out);
  51. url->SetScheme(NS_LITERAL_CSTRING("foo"));
  52. url->GetScheme(out);
  53. url->SetHost(NS_LITERAL_CSTRING("www.yahoo.com"));
  54. url->GetHost(out);
  55. url->SetPath(NS_LITERAL_CSTRING("/some-path/one-the-net/about.html?with-a-query#for-you"));
  56. url->GetPath(out);
  57. url->SetQuery(NS_LITERAL_CSTRING("a=b&d=c&what-ever-you-want-to-be-called=45"));
  58. url->GetQuery(out);
  59. url->SetRef(NS_LITERAL_CSTRING("#some-book-mark"));
  60. url->GetRef(out);
  61. }
  62. });
  63. TEST(TestStandardURL, Deserialize_Bug1392739)
  64. {
  65. mozilla::ipc::StandardURLParams standard_params;
  66. standard_params.urlType() = nsIStandardURL::URLTYPE_STANDARD;
  67. standard_params.spec() = NS_LITERAL_CSTRING("");
  68. standard_params.host() = mozilla::ipc::StandardURLSegment(4294967295, 1);
  69. mozilla::ipc::URIParams params(standard_params);
  70. nsCOMPtr<nsIIPCSerializableURI> url = do_CreateInstance(NS_STANDARDURL_CID);
  71. ASSERT_EQ(url->Deserialize(params), false);
  72. }
  73. TEST(TestStandardURL, CorruptSerialization)
  74. {
  75. auto spec = "http://user:pass@example.com/path/to/file.ext?query#hash"_ns;
  76. nsCOMPtr<nsIURI> uri;
  77. nsresult rv = NS_MutateURI(NS_STANDARDURLMUTATOR_CONTRACTID)
  78. .SetSpec(spec)
  79. .Finalize(uri);
  80. ASSERT_EQ(rv, NS_OK);
  81. nsAutoCString serialization;
  82. nsCOMPtr<nsISerializable> serializable = do_QueryInterface(uri);
  83. ASSERT_TRUE(serializable);
  84. // Check that the URL is normally serializable.
  85. ASSERT_EQ(NS_OK, NS_SerializeToString(serializable, serialization));
  86. nsCOMPtr<nsISupports> deserializedObject;
  87. ASSERT_EQ(NS_OK, NS_DeserializeObject(serialization,
  88. getter_AddRefs(deserializedObject)));
  89. nsAutoCString canonicalBin;
  90. Unused << Base64Decode(serialization, canonicalBin);
  91. // The spec serialization begins at byte 49
  92. // If the implementation of nsStandardURL::Write changes, this test will need
  93. // to be adjusted.
  94. #define SPEC_OFFSET 49
  95. ASSERT_EQ(Substring(canonicalBin, SPEC_OFFSET, 7), "http://"_ns);
  96. nsAutoCString corruptedBin = canonicalBin;
  97. // change mScheme.mPos
  98. corruptedBin.BeginWriting()[SPEC_OFFSET + spec.Length()] = 1;
  99. Unused << Base64Encode(corruptedBin, serialization);
  100. ASSERT_EQ(
  101. NS_ERROR_MALFORMED_URI,
  102. NS_DeserializeObject(serialization, getter_AddRefs(deserializedObject)));
  103. corruptedBin = canonicalBin;
  104. // change mScheme.mLen
  105. corruptedBin.BeginWriting()[SPEC_OFFSET + spec.Length() + 4] = 127;
  106. Unused << Base64Encode(corruptedBin, serialization);
  107. ASSERT_EQ(
  108. NS_ERROR_MALFORMED_URI,
  109. NS_DeserializeObject(serialization, getter_AddRefs(deserializedObject)));
  110. }