url_param_filter_test_helper.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2022 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. #ifndef COMPONENTS_URL_PARAM_FILTER_CORE_URL_PARAM_FILTER_TEST_HELPER_H_
  5. #define COMPONENTS_URL_PARAM_FILTER_CORE_URL_PARAM_FILTER_TEST_HELPER_H_
  6. #include "components/url_param_filter/core/url_param_classifications_loader.h"
  7. #include "components/url_param_filter/core/url_param_filterer.h"
  8. #include "testing/gmock/include/gmock/gmock.h"
  9. namespace url_param_filter {
  10. MATCHER_P(EqualsProto,
  11. want,
  12. "Matches an argument against an expected a proto Message.") {
  13. return arg.SerializeAsString() == want.SerializeAsString();
  14. }
  15. // A helper to easily create URL param filter classification maps based
  16. // on the passed-in source. `source` should map an eTLD+1 or wildcard to a
  17. // vector of params for the given role. For example, for eTLD+1 source.xyz, when
  18. // observed as the source (aka referer) of a navigation, block params
  19. // "plzblock" and "plzblock1".
  20. url_param_filter::ClassificationMap CreateClassificationMapForTesting(
  21. const std::map<std::string,
  22. std::map<FilterClassification::UseCase,
  23. std::vector<std::string>>>& source,
  24. url_param_filter::FilterClassification_SiteRole role);
  25. // Equivalent to the other overload, but uses the default (unknown) use case for
  26. // all parameters.
  27. url_param_filter::ClassificationMap CreateClassificationMapForTesting(
  28. const std::map<std::string, std::vector<std::string>>& source,
  29. url_param_filter::FilterClassification_SiteRole role);
  30. // A helper to easily create URL param filter classification maps based
  31. // on the passed-in source. `source` should map a ClassificationMapKey to a
  32. // vector of params. Gives all parameters the default (unknown) use case and the
  33. // NON_EXPERIMENTAL tag.
  34. url_param_filter::ClassificationMap CreateClassificationMapForTesting(
  35. const std::map<ClassificationMapKey, std::vector<std::string>>& source);
  36. // Creates and serializes the URL param filter classifications proto.
  37. // Used for simulating reading the classifications file from Component Updater.
  38. std::string CreateSerializedUrlParamFilterClassificationForTesting(
  39. const std::map<std::string,
  40. std::map<FilterClassification::UseCase,
  41. std::vector<std::string>>>& source_params,
  42. const std::map<std::string,
  43. std::map<FilterClassification::UseCase,
  44. std::vector<std::string>>>& destination_params,
  45. const std::vector<std::string>& experiment_tags);
  46. // Equivalent to the other overload, but uses empty use case lists for all
  47. // parameters.
  48. std::string CreateSerializedUrlParamFilterClassificationForTesting(
  49. const std::map<std::string, std::vector<std::string>>& source_params,
  50. const std::map<std::string, std::vector<std::string>>& destination_params,
  51. const std::vector<std::string>& experiment_tags);
  52. // Create a base64 representation of the URL param filter classifications
  53. // proto. Used for initialization of the feature params in tests.
  54. std::string CreateBase64EncodedFilterParamClassificationForTesting(
  55. const std::map<std::string,
  56. std::map<FilterClassification::UseCase,
  57. std::vector<std::string>>>& source_params,
  58. const std::map<std::string,
  59. std::map<FilterClassification::UseCase,
  60. std::vector<std::string>>>& destination_params);
  61. // Equivalent to the other overload, but uses empty use case lists for all
  62. // parameters.
  63. std::string CreateBase64EncodedFilterParamClassificationForTesting(
  64. const std::map<std::string, std::vector<std::string>>& source_params,
  65. const std::map<std::string, std::vector<std::string>>& destination_params);
  66. // Make a FilterClassifications proto using two maps, for source and destination
  67. // classifications. Each map takes the form "site"->["p1", "p2", ...] where
  68. // each "pi" in the list is a param that should be filtered from that site.
  69. // Defaults match type to EXACT_ETLD_PLUS_ONE for each created classification.
  70. FilterClassifications MakeClassificationsProtoFromMapWithUseCases(
  71. const std::map<std::string,
  72. std::map<FilterClassification::UseCase,
  73. std::vector<std::string>>>& source_map,
  74. const std::map<std::string,
  75. std::map<FilterClassification::UseCase,
  76. std::vector<std::string>>>& dest_map);
  77. // Make a FilterClassifications proto using a map of all classifications.
  78. // Each map takes the form ClassificationMapKey->["p1", "p2", ...] where
  79. // each "pi" in the list is a param that should be filtered from that site.
  80. FilterClassifications MakeClassificationsProtoFromMap(
  81. const std::map<ClassificationMapKey, std::vector<std::string>>& map);
  82. // Equivalent to the other overload, but uses empty use case lists for all
  83. // parameters. Defaults site_match_type to EXACT_ETLD_PLUS_ONE.
  84. FilterClassifications MakeClassificationsProtoFromMap(
  85. const std::map<std::string, std::vector<std::string>>& source_map,
  86. const std::map<std::string, std::vector<std::string>>& dest_map);
  87. // Make a FilterClassification proto provided a site, role, match type, and list
  88. // of params.
  89. FilterClassification MakeFilterClassification(
  90. const std::string& site,
  91. FilterClassification_SiteRole role,
  92. FilterClassification_SiteMatchType site_match_type,
  93. const std::vector<std::string>& params,
  94. const std::vector<FilterClassification::UseCase>& use_cases);
  95. // Equivalent to the other overload, but uses an empty list of use cases.
  96. FilterClassification MakeFilterClassification(
  97. const std::string& site,
  98. FilterClassification_SiteRole role,
  99. FilterClassification_SiteMatchType site_match_type,
  100. const std::vector<std::string>& params);
  101. // Make a FilterClassification proto provided a site, role, match type,
  102. // experiment override, and list of params.
  103. FilterClassification MakeFilterClassification(
  104. const std::string& site,
  105. FilterClassification_SiteRole role,
  106. FilterClassification_SiteMatchType site_match_type,
  107. const std::vector<std::string>& params,
  108. const std::vector<FilterClassification::UseCase>& use_cases,
  109. const std::string& experiment_identifier);
  110. // Helper method for adding repeated classifications on a FilterClassification.
  111. void AddClassification(
  112. FilterClassification* classification,
  113. const std::string& site,
  114. FilterClassification_SiteRole role,
  115. FilterClassification_SiteMatchType site_match_type,
  116. const std::vector<std::string>& params,
  117. const std::vector<FilterClassification::UseCase>& use_cases,
  118. const std::vector<std::string>& experiment_tags);
  119. } // namespace url_param_filter
  120. #endif // COMPONENTS_URL_PARAM_FILTER_CORE_URL_PARAM_FILTER_TEST_HELPER_H_