common_unittests.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2019 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 "components/services/quarantine/common.h"
  5. #include "testing/gtest/include/gtest/gtest.h"
  6. #include "url/gurl.h"
  7. TEST(QuarantineCommonTest, SanitizeUrlForQuarantine) {
  8. struct {
  9. const char* input;
  10. const char* expected_output;
  11. } kTestCases[] = {
  12. // Credentials stripped from http URL.
  13. {"http://foo:bar@s.example/x/y/z", "http://s.example/x/y/z"},
  14. // Preserve query and fragment of http{s} URL.
  15. {"http://a.example/x/y?q=f&r=g#blah",
  16. "http://a.example/x/y?q=f&r=g#blah"},
  17. {"https://a.example/x/y?q=f#h", "https://a.example/x/y?q=f#h"},
  18. // Ditto for ws{s} URL.
  19. {"ws://a.example/x/y?q=f", "ws://a.example/x/y?q=f"},
  20. {"wss://a.example/x/y?q=f", "wss://a.example/x/y?q=f"},
  21. // Credentials stripped from wss URL.
  22. {"wss://foo:bar@a.example/x/y?q=f", "wss://a.example/x/y?q=f"},
  23. // blob URLs get reduced to origin.
  24. {"blob:https://b.example/x/y/z", "https://b.example/"},
  25. {"blob:https://foo:bar@b.example/x/y/z?q", "https://b.example/"},
  26. // filesystem URLs get reduced to origin.
  27. {"filesystem:https://example.com/temporary/m", "https://example.com/"},
  28. {"filesystem:https://foo:bar@example.com/temporary/m",
  29. "https://example.com/"},
  30. // Unknown scheme is passed through as-is.
  31. {"some-random-scheme:randomdata", "some-random-scheme:randomdata"},
  32. // data URL is dropped.
  33. {"data:text/plain,hello%20world!", ""},
  34. // Trailing / added.
  35. {"http://www.source.example.com", "http://www.source.example.com/"},
  36. {"http://www.referrer.example.com", "http://www.referrer.example.com/"},
  37. // Invalid URL is dropped.
  38. {"1|\\|\\/4L||>", ""},
  39. };
  40. for (const auto test_case : kTestCases) {
  41. GURL input_url{test_case.input};
  42. GURL output = quarantine::SanitizeUrlForQuarantine(input_url);
  43. EXPECT_EQ(test_case.expected_output, output)
  44. << "Input : " << test_case.input;
  45. }
  46. }