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.aggregation;
18 
19 import android.net.Uri;
20 
21 import com.android.adservices.LogUtil;
22 import com.android.adservices.common.WebUtil;
23 import com.android.adservices.service.measurement.EventReport;
24 import com.android.adservices.service.measurement.util.UnsignedLong;
25 
26 import org.json.JSONException;
27 
28 import java.math.BigInteger;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.UUID;
32 import java.util.concurrent.TimeUnit;
33 
34 public final class AggregateReportFixture {
AggregateReportFixture()35     private AggregateReportFixture() {}
36 
37     private static final long MIN_TIME_MS = TimeUnit.MINUTES.toMillis(10L);
38     private static final long MAX_TIME_MS = TimeUnit.MINUTES.toMillis(60L);
39 
40     // Assume the field values in this AggregateReport.Builder have no relation to the field
41     // values in {@link ValidAggregateReportParams}
getValidAggregateReportBuilder()42     public static AggregateReport.Builder getValidAggregateReportBuilder() {
43         return new AggregateReport.Builder()
44                 .setId(UUID.randomUUID().toString())
45                 .setPublisher(ValidAggregateReportParams.PUBLISHER)
46                 .setAttributionDestination(ValidAggregateReportParams.ATTRIBUTION_DESTINATION)
47                 .setSourceRegistrationTime(ValidAggregateReportParams.SOURCE_REGISTRATION_TIME)
48                 .setScheduledReportTime(ValidAggregateReportParams.TRIGGER_TIME + getRandomTime())
49                 .setEnrollmentId(ValidAggregateReportParams.ENROLLMENT_ID)
50                 .setSourceDebugKey(ValidAggregateReportParams.SOURCE_DEBUG_KEY)
51                 .setTriggerDebugKey(ValidAggregateReportParams.TRIGGER_DEBUG_KEY)
52                 .setDebugCleartextPayload(ValidAggregateReportParams.getDebugPayload())
53                 .setStatus(EventReport.Status.PENDING)
54                 .setDebugReportStatus(EventReport.DebugReportStatus.PENDING)
55                 .setDedupKey(ValidAggregateReportParams.DEDUP_KEY)
56                 .setRegistrationOrigin(ValidAggregateReportParams.REGISTRATION_ORIGIN)
57                 .setAggregationCoordinatorOrigin(
58                         ValidAggregateReportParams.AGGREGATION_COORDINATOR_ORIGIN)
59                 .setIsFakeReport(false)
60                 .setTriggerContextId(ValidAggregateReportParams.TRIGGER_CONTEXT_ID)
61                 .setApi(ValidAggregateReportParams.API)
62                 .setAggregatableFilteringIdMaxBytes(
63                         ValidAggregateReportParams.AGGREGATABLE_FILTERING_ID_MAX_BYTES);
64     }
65 
getValidAggregateReport()66     public static AggregateReport getValidAggregateReport() {
67         return getValidAggregateReportBuilder().build();
68     }
69 
70     public static class ValidAggregateReportParams {
71         public static final Uri PUBLISHER = Uri.parse("android-app://com.registrant");
72         public static final Uri ATTRIBUTION_DESTINATION =
73                 Uri.parse("android-app://com.destination");
74         public static final long SOURCE_REGISTRATION_TIME = 8640000000L;
75         public static final long TRIGGER_TIME = 8640000000L;
76         public static final UnsignedLong SOURCE_DEBUG_KEY = new UnsignedLong(43254545L);
77         public static final UnsignedLong TRIGGER_DEBUG_KEY = new UnsignedLong(67878545L);
78         public static final String ENROLLMENT_ID = "enrollment-id";
79         public static final UnsignedLong DEDUP_KEY = new UnsignedLong(67878545L);
80         public static final Uri REGISTRATION_ORIGIN =
81                 WebUtil.validUri("https://subdomain.example.test");
82         public static final Uri AGGREGATION_COORDINATOR_ORIGIN =
83                 WebUtil.validUri("https://coordinator.example.test");
84         public static final String TRIGGER_CONTEXT_ID = "trigger_context_id";
85         public static final String API = "attribution-reporting";
86         public static final int AGGREGATABLE_FILTERING_ID_MAX_BYTES = 1;
87 
getDebugPayload()88         public static final String getDebugPayload() {
89             List<AggregateHistogramContribution> contributions = new ArrayList<>();
90             AggregateHistogramContribution contribution1 =
91                     new AggregateHistogramContribution.Builder()
92                             .setKey(BigInteger.valueOf(1369L)).setValue(32768).build();
93             AggregateHistogramContribution contribution2 =
94                     new AggregateHistogramContribution.Builder()
95                             .setKey(BigInteger.valueOf(3461L)).setValue(1664).build();
96             contributions.add(contribution1);
97             contributions.add(contribution2);
98             String debugPayload = null;
99             try {
100                 debugPayload = AggregateReport.generateDebugPayload(contributions);
101             } catch (JSONException e) {
102                 LogUtil.e("JSONException when generating debug payload.");
103             }
104             return debugPayload;
105         }
106     }
107 
getRandomTime()108     private static long getRandomTime() {
109         return (long) ((Math.random() * (MAX_TIME_MS - MIN_TIME_MS)) + MIN_TIME_MS);
110     }
111 }
112