2626#include " cc/core/async_executor/mock/mock_async_executor.h"
2727#include " cc/core/config_provider/mock/mock_config_provider.h"
2828#include " cc/core/interface/async_context.h"
29- #include " cc/core/interface/async_executor_interface.h"
3029#include " cc/core/interface/config_provider_interface.h"
30+ #include " cc/core/interface/errors.h"
3131#include " cc/core/interface/http_server_interface.h"
3232#include " cc/core/interface/http_types.h"
3333#include " cc/core/telemetry/mock/in_memory_metric_router.h"
3939#include " cc/pbs/interface/type_def.h"
4040#include " cc/public/core/interface/errors.h"
4141#include " cc/public/core/interface/execution_result.h"
42+ #include " google/protobuf/text_format.h"
4243
4344namespace privacy_sandbox ::pbs {
4445
4546namespace {
47+ using ::privacy_sandbox::pbs::v1::ConsumePrivacyBudgetRequest;
4648using ::privacy_sandbox::pbs_common::AsyncContext;
4749using ::privacy_sandbox::pbs_common::ExecutionResult;
50+ using ::privacy_sandbox::pbs_common::ExecutionResultOr;
4851using ::privacy_sandbox::pbs_common::HttpRequest;
4952using ::privacy_sandbox::pbs_common::HttpResponse;
5053} // namespace
@@ -92,17 +95,20 @@ class FrontEndServiceV2Peer {
9295
9396 ExecutionResult Init () { return front_end_service_v2_->Init (); }
9497
98+ ExecutionResultOr<std::unique_ptr<BudgetConsumer>> GetBudgetConsumer (
99+ const ConsumePrivacyBudgetRequest& req) {
100+ return front_end_service_v2_->GetBudgetConsumer (req);
101+ }
102+
95103 private:
96104 std::unique_ptr<FrontEndServiceV2> front_end_service_v2_;
97105};
98106
99107namespace {
100108
109+ using ::google::protobuf::TextFormat;
101110using ::privacy_sandbox::pbs_common::AsyncContext;
102- using ::privacy_sandbox::pbs_common::AsyncExecutorInterface;
103111using ::privacy_sandbox::pbs_common::Byte;
104- using ::privacy_sandbox::pbs_common::ConfigProviderInterface;
105- using ::privacy_sandbox::pbs_common::ExecutionResultOr;
106112using ::privacy_sandbox::pbs_common::FailureExecutionResult;
107113using ::privacy_sandbox::pbs_common::GetErrorMessage;
108114using ::privacy_sandbox::pbs_common::GetMetricPointData;
@@ -115,7 +121,6 @@ using ::privacy_sandbox::pbs_common::MockAsyncExecutor;
115121using ::privacy_sandbox::pbs_common::MockConfigProvider;
116122using ::privacy_sandbox::pbs_common::SuccessExecutionResult;
117123using ::testing::_;
118- using ::testing::Invoke;
119124using ::testing::NiceMock;
120125using ::testing::Return;
121126using ::testing::UnorderedElementsAreArray;
@@ -126,7 +131,6 @@ constexpr absl::string_view kTransactionId =
126131constexpr absl::string_view kTransactionSecret = " secret" ;
127132constexpr absl::string_view kReportingOrigin = " https://fake.com" ;
128133constexpr absl::string_view kClaimedIdentity = " https://origin.site.com" ;
129- constexpr absl::string_view kClaimedIdentityInvalid = " 123" ;
130134constexpr absl::string_view kUserAgent = " aggregation-service/2.8.7" ;
131135constexpr size_t k20191212DaysFromEpoch = 18242 ;
132136constexpr size_t k20191012DaysFromEpoch = 18181 ;
@@ -1029,5 +1033,56 @@ TEST_P(FrontEndServiceV2LifecycleTest, TestGetTransactionStatusReturns404) {
10291033 SC_PBS_FRONT_END_SERVICE_GET_TRANSACTION_STATUS_RETURNS_404_BY_DEFAULT );
10301034}
10311035
1036+ TEST (TestOneToOneMappingBetBudgetTypeAndBudgetConsumer,
1037+ TestOneToOneMappingBetBudgetTypeAndBudgetConsumer) {
1038+ auto budget_consumption_helper =
1039+ std::make_unique<MockBudgetConsumptionHelper>();
1040+ FrontEndServiceV2PeerOptions options;
1041+ options.budget_consumption_helper = budget_consumption_helper.get ();
1042+ auto front_end_service_v2_peer = MakeFrontEndServiceV2Peer (options);
1043+ auto execution_result = front_end_service_v2_peer->Init ();
1044+ ASSERT_TRUE (execution_result)
1045+ << GetErrorMessage (execution_result.status_code );
1046+
1047+ using PrivacyBudgetKey = ConsumePrivacyBudgetRequest::PrivacyBudgetKey;
1048+ std::string proto_string = R"pb(
1049+ version: "2.0"
1050+ data {
1051+ reporting_origin: "http://a.fake.com"
1052+ keys { budget_type: BUDGET_TYPE_BINARY_BUDGET }
1053+ }
1054+ )pb" ;
1055+
1056+ ConsumePrivacyBudgetRequest req;
1057+ ASSERT_TRUE (TextFormat::ParseFromString (proto_string, &req));
1058+
1059+ absl::flat_hash_set<std::string> returned_consumer_types;
1060+ const google::protobuf::EnumDescriptor* enum_descriptor =
1061+ PrivacyBudgetKey::BudgetType_descriptor ();
1062+
1063+ // Do not test BUDGET_TYPE_UNSPECIFIED
1064+ for (int i = 1 ; i < enum_descriptor->value_count (); ++i) {
1065+ const google::protobuf::EnumValueDescriptor* value_descriptor =
1066+ enum_descriptor->value (i);
1067+ PrivacyBudgetKey::BudgetType budget_type =
1068+ static_cast <PrivacyBudgetKey::BudgetType>(value_descriptor->number ());
1069+
1070+ req.mutable_data ()->at (0 ).mutable_keys ()->at (0 ).set_budget_type (
1071+ budget_type);
1072+ auto budget_consumer = front_end_service_v2_peer->GetBudgetConsumer (req);
1073+ ASSERT_TRUE (budget_consumer.has_value ())
1074+ << GetErrorMessage (budget_consumer.result ().status_code );
1075+ ASSERT_NE (budget_consumer->get (), nullptr );
1076+
1077+ // Ignore warning about deferencing the pointer
1078+ #pragma GCC diagnostic ignored "-Wpotentially-evaluated-expression"
1079+ std::string type_name = typeid (*budget_consumer->get ()).name ();
1080+
1081+ ASSERT_TRUE (returned_consumer_types.insert (type_name).second )
1082+ << " Duplicate consumer type returned for enum value " << budget_type
1083+ << " . Type: " << type_name << " was already returned by another enum." ;
1084+ }
1085+ }
1086+
10321087} // namespace
10331088} // namespace privacy_sandbox::pbs
0 commit comments