• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.adservices.common;
18 
19 import android.net.Uri;
20 import android.os.Process;
21 import android.util.Log;
22 
23 import androidx.test.core.app.ApplicationProvider;
24 
25 import com.android.adservices.LogUtil;
26 import com.android.adservices.service.FakeFlagsFactory;
27 import com.android.adservices.service.Flags;
28 import com.android.adservices.service.common.ValidatorUtil;
29 import com.android.modules.utils.build.SdkLevel;
30 
31 import com.google.common.truth.Truth;
32 
33 import java.time.Clock;
34 import java.time.Instant;
35 import java.time.ZoneOffset;
36 import java.time.temporal.ChronoUnit;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.HashSet;
40 import java.util.List;
41 import java.util.Locale;
42 import java.util.Set;
43 
44 public class CommonFixture {
45     private static final String LOG_TAG = "adservices";
46 
47     public static final String TEST_PACKAGE_NAME = processName();
48     public static final String TEST_PACKAGE_NAME_1 = "android.adservices.tests1";
49     public static final String TEST_PACKAGE_NAME_2 = "android.adservices.tests2";
50     public static final Set<String> PACKAGE_SET =
51             new HashSet<>(Arrays.asList(TEST_PACKAGE_NAME_1, TEST_PACKAGE_NAME_2));
52 
53     public static final Flags FLAGS_FOR_TEST = FakeFlagsFactory.getFlagsForTest();
54 
55     public static final Instant FIXED_NOW = Instant.now();
56     public static final Instant FIXED_NOW_TRUNCATED_TO_MILLI =
57             FIXED_NOW.truncatedTo(ChronoUnit.MILLIS);
58     public static final Instant FIXED_EARLIER_ONE_DAY = FIXED_NOW.minus(1, ChronoUnit.DAYS);
59     public static final Instant FIXED_NEXT_ONE_DAY = FIXED_NOW.plus(1, ChronoUnit.DAYS);
60     public static final Clock FIXED_CLOCK_TRUNCATED_TO_MILLI =
61             Clock.fixed(FIXED_NOW.truncatedTo(ChronoUnit.MILLIS), ZoneOffset.UTC);
62     public static final AdTechIdentifier NOT_ENROLLED_BUYER =
63             AdTechIdentifier.fromString("notenrolled.com");
64     public static final AdTechIdentifier VALID_BUYER_1 = AdTechIdentifier.fromString("test.com");
65     public static final AdTechIdentifier VALID_BUYER_2 = AdTechIdentifier.fromString("test2.com");
66     public static final AdTechIdentifier VALID_WINNING_SELLER_2 =
67             AdTechIdentifier.fromString("test2.com");
68     public static final AdTechIdentifier INVALID_EMPTY_BUYER = AdTechIdentifier.fromString("");
69     public static final Set<AdTechIdentifier> BUYER_SET =
70             new HashSet<>(Arrays.asList(VALID_BUYER_1, VALID_BUYER_2));
71 
getUri(String authority, String path)72     public static Uri getUri(String authority, String path) {
73         return Uri.parse(ValidatorUtil.HTTPS_SCHEME + "://" + authority + path);
74     }
75 
getUriWithGivenSubdomain(String subdomain, String authority, String path)76     public static Uri getUriWithGivenSubdomain(String subdomain, String authority, String path) {
77         return Uri.parse(ValidatorUtil.HTTPS_SCHEME + "://" + subdomain + "." + authority + path);
78     }
79 
getUriWithValidSubdomain(String authority, String path)80     public static Uri getUriWithValidSubdomain(String authority, String path) {
81         return getUriWithGivenSubdomain("valid.subdomain", authority, path);
82     }
83 
getUri(AdTechIdentifier authority, String path)84     public static Uri getUri(AdTechIdentifier authority, String path) {
85         return getUri(authority.toString(), path);
86     }
87 
88     @SafeVarargs
assertHaveSameHashCode(T... objs)89     public static <T> void assertHaveSameHashCode(T... objs) {
90         Set<T> helperSet = new HashSet<>(Arrays.asList(objs));
91         Truth.assertThat(helperSet).hasSize(1);
92     }
93 
94     @SafeVarargs
assertDifferentHashCode(T... objs)95     public static <T> void assertDifferentHashCode(T... objs) {
96         Set<T> helperSet = new HashSet<>(Arrays.asList(objs));
97         Truth.assertThat(helperSet).hasSize(objs.length);
98     }
99 
doSleep(long timeout)100     public static void doSleep(long timeout) {
101         Log.i(LOG_TAG, "Starting to sleep for " + timeout + " ms");
102         long currentTime = System.currentTimeMillis();
103         long wakeupTime = currentTime + timeout;
104         while (wakeupTime - currentTime > 0) {
105             Log.i(LOG_TAG, "Time left to sleep: " + (wakeupTime - currentTime) + " ms");
106             try {
107                 Thread.sleep(wakeupTime - currentTime);
108             } catch (InterruptedException ignored) {
109                 Log.w(LOG_TAG, "Interrupted while sleeping");
110                 Thread.currentThread().interrupt();
111             }
112             currentTime = System.currentTimeMillis();
113         }
114         Log.i(LOG_TAG, "Done sleeping");
115     }
116 
processName()117     private static String processName() {
118         if (SdkLevel.isAtLeastT()) {
119             return Process.myProcessName();
120         } else {
121             try {
122                 return ApplicationProvider.getApplicationContext().getPackageName();
123             } catch (IllegalStateException e) {
124                 // TODO(b/275062019): Remove this try/catch once instrumentation context can be
125                 // passed in AppConsentSettingsUiAutomatorTest
126                 LogUtil.e(e, "Failed to get package name from Instrumentation context");
127                 return "android.adservices.tests";
128             }
129         }
130     }
131 
132     /** Returns a random alphanumeric string of length n. */
getAlphaNumericString(int n)133     public static String getAlphaNumericString(int n) {
134         String AlphaNumericString =
135                 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789" + "abcdefghijklmnopqrstuvxyz";
136 
137         StringBuilder sb = new StringBuilder(n);
138 
139         for (int i = 0; i < n; i++) {
140             int index = (int) (AlphaNumericString.length() * Math.random());
141 
142             // add Character one by one in end of sb
143             sb.append(AlphaNumericString.charAt(index));
144         }
145         return sb.toString();
146     }
147 
148     /** Creates a list of {@code AdTechIdentifier} with N amountOfBuyers */
createNAmountOfBuyers(int amountOfBuyers)149     public static List<AdTechIdentifier> createNAmountOfBuyers(int amountOfBuyers) {
150         List<AdTechIdentifier> buyers = new ArrayList<>();
151         for (int b = 1; b <= amountOfBuyers; b++) {
152             String buyerName = String.format(Locale.ENGLISH, "test%d.com", b);
153             buyers.add(AdTechIdentifier.fromString(buyerName));
154         }
155         return buyers;
156     }
157 }
158