TestProtocolProxyService.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "gtest/gtest.h"
  2. #include "nsCOMPtr.h"
  3. #include "nsNetCID.h"
  4. #include "nsIURL.h"
  5. #include "nsString.h"
  6. #include "nsComponentManagerUtils.h"
  7. #include "../../base/nsProtocolProxyService.h"
  8. #include "nsServiceManagerUtils.h"
  9. #include "mozilla/Preferences.h"
  10. namespace mozilla {
  11. namespace net {
  12. TEST(TestProtocolProxyService, LoadHostFilters) {
  13. nsCOMPtr<nsIProtocolProxyService2> ps = do_GetService(NS_PROTOCOLPROXYSERVICE_CID);
  14. ASSERT_TRUE(ps);
  15. mozilla::net::nsProtocolProxyService* pps = static_cast<mozilla::net::nsProtocolProxyService*>(ps.get());
  16. nsCOMPtr<nsIURL> url( do_CreateInstance(NS_STANDARDURL_CONTRACTID) );
  17. ASSERT_TRUE(url) << "couldn't create URL";
  18. nsAutoCString spec;
  19. auto CheckLoopbackURLs = [&](bool expected)
  20. {
  21. // loopback IPs are always filtered
  22. spec = "http://127.0.0.1";
  23. ASSERT_EQ(url->SetSpec(spec), NS_OK);
  24. ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
  25. spec = "http://[::1]";
  26. ASSERT_EQ(url->SetSpec(spec), NS_OK);
  27. ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
  28. };
  29. auto CheckURLs = [&](bool expected)
  30. {
  31. spec = "http://example.com";
  32. ASSERT_EQ(url->SetSpec(spec), NS_OK);
  33. ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
  34. spec = "https://10.2.3.4";
  35. ASSERT_EQ(url->SetSpec(spec), NS_OK);
  36. ASSERT_EQ(pps->CanUseProxy(url, 443), expected);
  37. spec = "http://1.2.3.4";
  38. ASSERT_EQ(url->SetSpec(spec), NS_OK);
  39. ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
  40. spec = "http://1.2.3.4:8080";
  41. ASSERT_EQ(url->SetSpec(spec), NS_OK);
  42. ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
  43. spec = "http://[2001::1]";
  44. ASSERT_EQ(url->SetSpec(spec), NS_OK);
  45. ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
  46. spec = "http://2.3.4.5:7777";
  47. ASSERT_EQ(url->SetSpec(spec), NS_OK);
  48. ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
  49. spec = "http://[abcd::2]:123";
  50. ASSERT_EQ(url->SetSpec(spec), NS_OK);
  51. ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
  52. spec = "http://bla.test.com";
  53. ASSERT_EQ(url->SetSpec(spec), NS_OK);
  54. ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
  55. };
  56. auto CheckPortDomain = [&](bool expected)
  57. {
  58. spec = "http://blabla.com:10";
  59. ASSERT_EQ(url->SetSpec(spec), NS_OK);
  60. ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
  61. };
  62. auto CheckLocalDomain = [&](bool expected)
  63. {
  64. spec = "http://test";
  65. ASSERT_EQ(url->SetSpec(spec), NS_OK);
  66. ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
  67. };
  68. // --------------------------------------------------------------------------
  69. nsAutoCString filter;
  70. // Anything is allowed when there are no filters set
  71. printf("Testing empty filter: %s\n", filter.get());
  72. pps->LoadHostFilters(filter);
  73. CheckLoopbackURLs(true); // only time when loopbacks can be proxied. bug?
  74. CheckLocalDomain(true);
  75. CheckURLs(true);
  76. CheckPortDomain(true);
  77. // --------------------------------------------------------------------------
  78. filter = "example.com, 1.2.3.4/16, [2001::1], 10.0.0.0/8, 2.3.0.0/16:7777, [abcd::1]/64:123, *.test.com";
  79. printf("Testing filter: %s\n", filter.get());
  80. pps->LoadHostFilters(filter);
  81. // Check URLs can no longer use filtered proxy
  82. CheckURLs(false);
  83. CheckLoopbackURLs(false);
  84. CheckLocalDomain(true);
  85. CheckPortDomain(true);
  86. // --------------------------------------------------------------------------
  87. // This is space separated. See bug 1346711 comment 4. We check this to keep
  88. // backwards compatibility.
  89. filter = "<local> blabla.com:10";
  90. printf("Testing filter: %s\n", filter.get());
  91. pps->LoadHostFilters(filter);
  92. CheckURLs(true);
  93. CheckLoopbackURLs(false);
  94. CheckLocalDomain(false);
  95. CheckPortDomain(false);
  96. // Check that we don't crash on weird input
  97. filter = "a b c abc:1x2, ,, * ** *.* *:10 :20 :40/12 */12:90";
  98. printf("Testing filter: %s\n", filter.get());
  99. pps->LoadHostFilters(filter);
  100. }
  101. } // namespace net
  102. } // namespace mozila