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.fromJson; 20 import static android.adservices.debuggablects.CustomAudienceSubject.assertThat; 21 22 import static com.android.adservices.service.CommonDebugFlagsConstants.KEY_ADSERVICES_SHELL_COMMAND_ENABLED; 23 import static com.android.adservices.service.DebugFlagsConstants.KEY_CONSENT_NOTIFICATION_DEBUG_MODE; 24 import static com.android.adservices.service.DebugFlagsConstants.KEY_FLEDGE_IS_CUSTOM_AUDIENCE_CLI_ENABLED; 25 import static com.android.adservices.service.FlagsConstants.KEY_DISABLE_FLEDGE_ENROLLMENT_CHECK; 26 27 import static com.google.common.truth.Truth.assertThat; 28 29 import android.adservices.common.AdTechIdentifier; 30 import android.adservices.customaudience.CustomAudience; 31 import android.adservices.customaudience.CustomAudienceFixture; 32 import android.adservices.utils.CustomAudienceTestFixture; 33 import android.adservices.utils.DevContextUtils; 34 35 import com.android.adservices.common.AdServicesShellCommandHelper; 36 import com.android.adservices.common.AdservicesTestHelper; 37 import com.android.adservices.common.annotations.SetPpapiAppAllowList; 38 import com.android.adservices.shared.testing.SupportedByConditionRule; 39 import com.android.adservices.shared.testing.annotations.EnableDebugFlag; 40 import com.android.adservices.shared.testing.annotations.SetFlagEnabled; 41 import com.android.adservices.shared.testing.shell.CommandResult; 42 43 import com.google.errorprone.annotations.FormatMethod; 44 import com.google.errorprone.annotations.FormatString; 45 46 import org.json.JSONArray; 47 import org.json.JSONException; 48 import org.json.JSONObject; 49 import org.junit.After; 50 import org.junit.Before; 51 import org.junit.Rule; 52 import org.junit.Test; 53 54 import java.util.List; 55 56 @EnableDebugFlag(KEY_ADSERVICES_SHELL_COMMAND_ENABLED) 57 @EnableDebugFlag(KEY_FLEDGE_IS_CUSTOM_AUDIENCE_CLI_ENABLED) 58 @EnableDebugFlag(KEY_CONSENT_NOTIFICATION_DEBUG_MODE) 59 @SetFlagEnabled(KEY_DISABLE_FLEDGE_ENROLLMENT_CHECK) 60 @SetPpapiAppAllowList 61 public final class CustomAudienceShellCommandsE2ETest extends AdServicesDebuggableTestCase { 62 private static final AdTechIdentifier BUYER = AdTechIdentifier.fromString("localhost"); 63 64 @Rule(order = 11) 65 public final SupportedByConditionRule devOptionsEnabled = 66 DevContextUtils.createDevOptionsAvailableRule(mContext, LOGCAT_TAG_FLEDGE); 67 68 private final AdServicesShellCommandHelper mShellCommandHelper = 69 new AdServicesShellCommandHelper(); 70 71 private CustomAudience mShirtsCustomAudience; 72 private CustomAudience mShoesCustomAudience; 73 private CustomAudienceTestFixture mCustomAudienceTestFixture; 74 75 76 @Before setUp()77 public void setUp() throws Exception { 78 AdservicesTestHelper.killAdservicesProcess(mContext); 79 80 mCustomAudienceTestFixture = new CustomAudienceTestFixture(mContext); 81 mShirtsCustomAudience = 82 mCustomAudienceTestFixture.createCustomAudience( 83 "shirts", 84 BUYER, 85 List.of(1D), 86 null, 87 CustomAudienceFixture.VALID_EXPIRATION_TIME); 88 mShoesCustomAudience = 89 mCustomAudienceTestFixture.createCustomAudience( 90 "shoes", 91 BUYER, 92 List.of(1D), 93 null, 94 CustomAudienceFixture.VALID_EXPIRATION_TIME); 95 } 96 97 @After tearDown()98 public void tearDown() throws Exception { 99 mCustomAudienceTestFixture.leaveJoinedCustomAudiences(); 100 } 101 102 @Test testRun_listCustomAudience_happyPath()103 public void testRun_listCustomAudience_happyPath() throws Exception { 104 mCustomAudienceTestFixture.joinCustomAudience(mShirtsCustomAudience); 105 mCustomAudienceTestFixture.joinCustomAudience(mShoesCustomAudience); 106 107 JSONArray customAudiences = 108 runAndParseShellCommandJson( 109 "custom-audience list --owner %s --buyer %s", mPackageName, BUYER) 110 .getJSONArray("custom_audiences"); 111 mCustomAudienceTestFixture.leaveCustomAudience(mShirtsCustomAudience); 112 JSONArray customAudiencesAfterLeaving = 113 runAndParseShellCommandJson( 114 "custom-audience list --owner %s --buyer %s", mPackageName, BUYER) 115 .getJSONArray("custom_audiences"); 116 117 assertThat( 118 List.of( 119 fromJson(customAudiences.getJSONObject(0)), 120 fromJson(customAudiences.getJSONObject(1)))) 121 .containsExactly(mShirtsCustomAudience, mShoesCustomAudience); 122 assertThat(fromJson(customAudiencesAfterLeaving.getJSONObject(0))) 123 .isEqualTo(mShoesCustomAudience); 124 JSONObject customAudience1 = customAudiences.getJSONObject(0); 125 JSONObject customAudience2 = customAudiences.getJSONObject(1); 126 JSONObject customAudience3 = customAudiencesAfterLeaving.getJSONObject(0); 127 assertThat(customAudience1).hasValidActivationTime(); 128 assertThat(customAudience1).hasValidationFailures(0); 129 assertThat(customAudience1).hasTimeoutFailures(0); 130 assertThat(customAudience2).hasValidActivationTime(); 131 assertThat(customAudience2).hasValidationFailures(0); 132 assertThat(customAudience2).hasTimeoutFailures(0); 133 assertThat(customAudience3).hasValidActivationTime(); 134 assertThat(customAudience3).hasValidationFailures(0); 135 assertThat(customAudience3).hasTimeoutFailures(0); 136 } 137 138 @Test testRun_viewCustomAudience_happyPath()139 public void testRun_viewCustomAudience_happyPath() throws Exception { 140 mCustomAudienceTestFixture.joinCustomAudience(mShirtsCustomAudience); 141 142 JSONObject customAudience = 143 runAndParseShellCommandJson( 144 "custom-audience view --owner %s --buyer %s --name %s", 145 mPackageName, BUYER, mShirtsCustomAudience.getName()); 146 147 CustomAudience parsedCustomAudience = fromJson(customAudience); 148 assertThat(mShirtsCustomAudience).isEqualTo(parsedCustomAudience); 149 assertThat(customAudience).hasValidActivationTime(); 150 assertThat(customAudience).hasValidationFailures(0); 151 assertThat(customAudience).hasTimeoutFailures(0); 152 assertThat(customAudience.getBoolean("is_eligible_for_on_device_auction")).isEqualTo(true); 153 assertThat(customAudience.getBoolean("is_eligible_for_server_auction")).isEqualTo(false); 154 } 155 156 @Test testRun_refreshCustomAudiences_verifyNoCustomAudienceChanged()157 public void testRun_refreshCustomAudiences_verifyNoCustomAudienceChanged() { 158 CommandResult commandResult = 159 mShellCommandHelper.runCommandRwe( 160 "custom-audience refresh --owner %s --buyer %s --name %s", 161 mPackageName, BUYER, mShirtsCustomAudience.getName()); 162 163 assertThat(commandResult.getOut()).isEmpty(); 164 assertThat(commandResult.getErr()).contains("No custom audience found"); 165 } 166 167 @FormatMethod runAndParseShellCommandJson( @ormatString String template, Object... commandArgs)168 private JSONObject runAndParseShellCommandJson( 169 @FormatString String template, Object... commandArgs) throws JSONException { 170 return new JSONObject(mShellCommandHelper.runCommand(template, commandArgs)); 171 } 172 } 173