1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.adservices.service.measurement;
18 
19 import android.net.Uri;
20 import android.util.Pair;
21 
22 import com.android.adservices.common.WebUtil;
23 import com.android.adservices.service.FakeFlagsFactory;
24 import com.android.adservices.service.measurement.util.UnsignedLong;
25 
26 import org.json.JSONArray;
27 
28 import java.util.Collections;
29 import java.util.Map;
30 import java.util.UUID;
31 
32 public final class TriggerFixture {
TriggerFixture()33     private TriggerFixture() { }
34 
35     // Assume the field values in this Trigger.Builder have no relation to the field values in
36     // {@link ValidTriggerParams}
getValidTriggerBuilder()37     public static Trigger.Builder getValidTriggerBuilder() {
38         return new Trigger.Builder()
39                 .setAttributionDestination(ValidTriggerParams.ATTRIBUTION_DESTINATION)
40                 .setEnrollmentId(ValidTriggerParams.ENROLLMENT_ID)
41                 .setRegistrant(ValidTriggerParams.REGISTRANT)
42                 .setAggregatableSourceRegistrationTimeConfig(
43                         ValidTriggerParams.AGGREGATABLE_SOURCE_REGISTRATION_TIME_CONFIG)
44                 .setRegistrationOrigin(ValidTriggerParams.REGISTRATION_ORIGIN);
45     }
46 
47     // Assume the field values in this Trigger have no relation to the field values in
48     // {@link ValidTriggerParams}
getValidTrigger()49     public static Trigger getValidTrigger() {
50         return new Trigger.Builder()
51                 .setId(UUID.randomUUID().toString())
52                 .setAttributionDestination(ValidTriggerParams.ATTRIBUTION_DESTINATION)
53                 .setEnrollmentId(ValidTriggerParams.ENROLLMENT_ID)
54                 .setRegistrant(ValidTriggerParams.REGISTRANT)
55                 .setTriggerTime(ValidTriggerParams.TRIGGER_TIME)
56                 .setEventTriggers(ValidTriggerParams.EVENT_TRIGGERS)
57                 .setAggregateTriggerData(ValidTriggerParams.AGGREGATE_TRIGGER_DATA)
58                 .setAggregateValuesString(ValidTriggerParams.AGGREGATE_VALUES_STRING)
59                 .setFilters(ValidTriggerParams.TOP_LEVEL_FILTERS_JSON_STRING)
60                 .setNotFilters(ValidTriggerParams.TOP_LEVEL_NOT_FILTERS_JSON_STRING)
61                 .setAttributionConfig(ValidTriggerParams.ATTRIBUTION_CONFIGS_STRING)
62                 .setAdtechBitMapping(ValidTriggerParams.X_NETWORK_KEY_MAPPING)
63                 .setRegistrationOrigin(ValidTriggerParams.REGISTRATION_ORIGIN)
64                 .setAggregationCoordinatorOrigin(ValidTriggerParams.AGGREGATION_COORDINATOR_ORIGIN)
65                 .setAggregatableSourceRegistrationTimeConfig(
66                         ValidTriggerParams.AGGREGATABLE_SOURCE_REGISTRATION_TIME_CONFIG)
67                 .setTriggerContextId(ValidTriggerParams.TRIGGER_CONTEXT_ID)
68                 .setAttributionScopesString(ValidTriggerParams.ATTRIBUTION_SCOPES)
69                 .setAggregatableFilteringIdMaxBytes(
70                         ValidTriggerParams.AGGREGATABLE_FILTERING_ID_MAX_BYTES)
71                 .setAggregateDebugReportingString(ValidTriggerParams.AGGREGATE_DEBUG_REPORT)
72                 .build();
73     }
74 
75     public static class ValidTriggerParams {
76         public static final Long TRIGGER_TIME = 8640000000L;
77         public static final Uri ATTRIBUTION_DESTINATION =
78                 Uri.parse("android-app://com.destination");
79         public static final Uri REGISTRANT = Uri.parse("android-app://com.registrant");
80         public static final String ENROLLMENT_ID = "enrollment-id";
81         public static final String TOP_LEVEL_FILTERS_JSON_STRING =
82                 "[{\n"
83                         + "  \"key_1\": [\"value_1\", \"value_2\"],\n"
84                         + "  \"key_2\": [\"value_1\", \"value_2\"]\n"
85                         + "}]\n";
86 
87         public static final String TOP_LEVEL_NOT_FILTERS_JSON_STRING =
88                 "[{\"geo\": [], \"source_type\": [\"event\"]}]";
89 
90         public static final String EVENT_TRIGGERS =
91                 "[\n"
92                         + "{\n"
93                         + "  \"trigger_data\": \"5\",\n"
94                         + "  \"priority\": \"123\",\n"
95                         + "  \"filters\": [{\n"
96                         + "    \"source_type\": [\"navigation\"],\n"
97                         + "    \"key_1\": [\"value_1\"] \n"
98                         + "   }]\n"
99                         + "},\n"
100                         + "{\n"
101                         + "  \"trigger_data\": \"0\",\n"
102                         + "  \"priority\": \"124\",\n"
103                         + "  \"deduplication_key\": \"101\",\n"
104                         + "  \"filters\": [{\n"
105                         + "     \"source_type\": [\"event\"]\n"
106                         + "   }]\n"
107                         + "}\n"
108                         + "]\n";
109 
110         public static final String AGGREGATE_TRIGGER_DATA =
111                 "["
112                     + "{"
113                         + "\"key_piece\":\"0xA80\","
114                         + "\"source_keys\":[\"geoValue\",\"noMatch\"]"
115                     + "}"
116                 + "]";
117 
118         public static final String AGGREGATE_VALUES_STRING =
119                 "{" + "\"campaignCounts\":32768," + "\"geoValue\":1664" + "}";
120 
121         public static final UnsignedLong DEBUG_KEY = new UnsignedLong(27836L);
122 
123         public static final AttributionConfig ATTRIBUTION_CONFIG =
124                 new AttributionConfig.Builder()
125                         .setExpiry(604800L)
126                         .setSourceAdtech("AdTech1-Ads")
127                         .setSourcePriorityRange(new Pair<>(100L, 1000L))
128                         .setSourceFilters(
129                                 Collections.singletonList(
130                                         new FilterMap.Builder()
131                                                 .setAttributionFilterMap(
132                                                         Map.of(
133                                                                 "campaign_type",
134                                                                 Collections.singletonList(
135                                                                         "install"),
136                                                                 "source_type",
137                                                                 Collections.singletonList(
138                                                                         "navigation")))
139                                                 .build()))
140                         .setPriority(99L)
141                         .setExpiry(604800L)
142                         .setFilterData(
143                                 new FilterMap.Builder()
144                                         .setAttributionFilterMap(
145                                                 Map.of(
146                                                         "campaign_type",
147                                                         Collections.singletonList("install")))
148                                         .build())
149                         .build();
150 
151         public static final String ATTRIBUTION_CONFIGS_STRING =
152                 new JSONArray(
153                                 Collections.singletonList(
154                                         ATTRIBUTION_CONFIG.serializeAsJson(
155                                                 FakeFlagsFactory.getFlagsForTest())))
156                         .toString();
157 
158         public static final String X_NETWORK_KEY_MAPPING =
159                 "{"
160                         + "\"AdTechA-enrollment_id\": \"0x1\","
161                         + "\"AdTechB-enrollment_id\": \"0x2\""
162                         + "}";
163         public static final Uri REGISTRATION_ORIGIN =
164                 WebUtil.validUri("https://subdomain.example.test");
165 
166         public static final Uri AGGREGATION_COORDINATOR_ORIGIN =
167                 WebUtil.validUri("https://coordinator.example.test");
168 
169         public static final Trigger.SourceRegistrationTimeConfig
170                 AGGREGATABLE_SOURCE_REGISTRATION_TIME_CONFIG =
171                         Trigger.SourceRegistrationTimeConfig.INCLUDE;
172 
173         public static final String PLATFORM_AD_ID = "test-platform-ad-id";
174         public static final String TRIGGER_CONTEXT_ID = "test-trigger-context-id";
175         public static final String ATTRIBUTION_SCOPES = "[\"1\"]";
176         public static final int AGGREGATABLE_FILTERING_ID_MAX_BYTES = 1;
177         public static final String AGGREGATE_DEBUG_REPORT =
178                 "{\"key_piece\":\"0x222\","
179                         + "\"debug_data\":["
180                         + "{"
181                         + "\"types\": [\"trigger-aggregate-insufficient-budget\", "
182                         + "\"trigger-aggregate-deduplicated\"],"
183                         + "\"key_piece\": \"0x333\","
184                         + "\"value\": 333"
185                         + "},"
186                         + "{"
187                         + "\"types\": [\"trigger-aggregate-report-window-passed\", "
188                         + "\"trigger-event-low-priority\"],"
189                         + "\"key_piece\": \"0x444\","
190                         + "\"value\": 444"
191                         + "},"
192                         + "{"
193                         + "\"types\": [\"unspecified\"],"
194                         + "\"key_piece\": \"0x555\","
195                         + "\"value\": 555"
196                         + "}"
197                         + "],"
198                         + "\"aggregation_coordinator_origin\":\"https://aws.example\"}";
199     }
200 }
201