• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 android.adservices.debuggablects;
18 
19 import static android.adservices.debuggablects.CustomAudienceShellCommandHelper.ACTIVATION_TIME;
20 import static android.adservices.debuggablects.CustomAudienceShellCommandHelper.DAILY_UPDATE;
21 import static android.adservices.debuggablects.CustomAudienceShellCommandHelper.ELIGIBLE_UPDATE_TIME;
22 import static android.adservices.debuggablects.CustomAudienceShellCommandHelper.NUM_TIMEOUT_FAILURES;
23 import static android.adservices.debuggablects.CustomAudienceShellCommandHelper.NUM_VALIDATION_FAILURES;
24 
25 import static com.google.common.truth.Truth.assertAbout;
26 
27 import androidx.annotation.Nullable;
28 
29 import com.google.common.truth.Fact;
30 import com.google.common.truth.FailureMetadata;
31 import com.google.common.truth.Subject;
32 
33 import org.json.JSONException;
34 import org.json.JSONObject;
35 
36 import java.time.Instant;
37 import java.time.format.DateTimeParseException;
38 
39 class CustomAudienceSubject extends Subject {
40 
builder()41     static Factory<CustomAudienceSubject, JSONObject> builder() {
42         return CustomAudienceSubject::new;
43     }
44 
assertThat(@ullable JSONObject customAudienceJson)45     static CustomAudienceSubject assertThat(@Nullable JSONObject customAudienceJson) {
46         return assertAbout(builder()).that(customAudienceJson);
47     }
48 
49     @Nullable private final JSONObject mActual;
50 
CustomAudienceSubject(FailureMetadata metadata, @Nullable JSONObject actual)51     CustomAudienceSubject(FailureMetadata metadata, @Nullable JSONObject actual) {
52         super(metadata, actual);
53         this.mActual = actual;
54     }
55 
hasValidActivationTime()56     void hasValidActivationTime() {
57         // Activation time is inconsistent so only parse for format correctness.
58         try {
59             Instant.parse(mActual.getString(ACTIVATION_TIME));
60         } catch (JSONException | DateTimeParseException e) {
61             failWithoutActual(Fact.simpleFact("Could not parse activation time."));
62         }
63     }
64 
hasTimeoutFailures(int expected)65     void hasTimeoutFailures(int expected) {
66         try {
67             int actual = getBackgroundFetchData().getInt(NUM_TIMEOUT_FAILURES);
68             if (actual != expected) {
69                 failWithoutActual(
70                         Fact.simpleFact(
71                                 String.format(
72                                         "Expected %d timeout failures but was %d",
73                                         expected, actual)));
74             }
75         } catch (JSONException e) {
76             failWithoutActual(
77                     Fact.simpleFact("Failed to parse background fetch timeout failures."));
78         }
79     }
80 
hasValidationFailures(int expected)81     void hasValidationFailures(int expected) {
82         try {
83             int actual = getBackgroundFetchData().getInt(NUM_VALIDATION_FAILURES);
84             if (actual != expected) {
85                 failWithoutActual(
86                         Fact.simpleFact(
87                                 String.format(
88                                         "Expected %d validation failures but was %d",
89                                         expected, actual)));
90             }
91         } catch (JSONException e) {
92             failWithoutActual(
93                     Fact.simpleFact("Failed to parse background fetch validation failures."));
94         }
95     }
96 
hasValidEligibleUpdateTime()97     void hasValidEligibleUpdateTime() {
98         try {
99             Instant.parse(getBackgroundFetchData().getString(ELIGIBLE_UPDATE_TIME));
100         } catch (JSONException e) {
101             failWithoutActual(
102                     Fact.simpleFact(
103                             "Background fetch data does hot have valid eligible update time."));
104         }
105     }
106 
getBackgroundFetchData()107     private JSONObject getBackgroundFetchData() {
108         try {
109             return mActual.getJSONObject(DAILY_UPDATE);
110         } catch (JSONException e) {
111             failWithoutActual(Fact.simpleFact("Failed to parse background fetch data."));
112             return null;
113         }
114     }
115 }
116