1 /* 2 * Copyright (C) 2023 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.server.contentprotection; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.verify; 22 import static org.mockito.Mockito.verifyZeroInteractions; 23 import static org.mockito.Mockito.when; 24 25 import android.app.admin.DevicePolicyManagerInternal; 26 import android.content.ContentResolver; 27 import android.net.Uri; 28 import android.os.Handler; 29 import android.os.Looper; 30 import android.os.UserHandle; 31 import android.provider.Settings; 32 import android.testing.TestableContentResolver; 33 import android.testing.TestableContext; 34 35 import androidx.test.core.app.ApplicationProvider; 36 import androidx.test.ext.junit.runners.AndroidJUnit4; 37 import androidx.test.filters.SmallTest; 38 39 import org.junit.Rule; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 import org.mockito.Mock; 43 import org.mockito.junit.MockitoJUnit; 44 import org.mockito.junit.MockitoRule; 45 46 /** 47 * Test for {@link ContentProtectionConsentManager}. 48 * 49 * <p>Run with: {@code atest 50 * FrameworksServicesTests:com.android.server.contentprotection.ContentProtectionConsentManagerTest} 51 */ 52 @RunWith(AndroidJUnit4.class) 53 @SmallTest 54 public class ContentProtectionConsentManagerTest { 55 56 private static final String KEY_PACKAGE_VERIFIER_USER_CONSENT = "package_verifier_user_consent"; 57 58 private static final Uri URI_PACKAGE_VERIFIER_USER_CONSENT = 59 Settings.Global.getUriFor(KEY_PACKAGE_VERIFIER_USER_CONSENT); 60 61 private static final int VALUE_TRUE = 1; 62 63 private static final int VALUE_FALSE = -1; 64 65 private static final int VALUE_DEFAULT = 0; 66 67 private static final int TEST_USER_ID = 1234; 68 69 @Rule public final MockitoRule mMockitoRule = MockitoJUnit.rule(); 70 71 @Rule 72 public final TestableContext mTestableContext = 73 new TestableContext(ApplicationProvider.getApplicationContext()); 74 75 private final TestableContentResolver mTestableContentResolver = 76 mTestableContext.getContentResolver(); 77 78 @Mock private ContentResolver mMockContentResolver; 79 80 @Mock private DevicePolicyManagerInternal mMockDevicePolicyManagerInternal; 81 82 @Test constructor_registersContentObserver()83 public void constructor_registersContentObserver() { 84 ContentProtectionConsentManager manager = 85 createContentProtectionConsentManager(mMockContentResolver); 86 87 assertThat(manager.mContentObserver).isNotNull(); 88 verify(mMockContentResolver) 89 .registerContentObserver( 90 URI_PACKAGE_VERIFIER_USER_CONSENT, 91 /* notifyForDescendants= */ false, 92 manager.mContentObserver, 93 UserHandle.USER_ALL); 94 } 95 96 @Test isConsentGranted_packageVerifierNotGranted()97 public void isConsentGranted_packageVerifierNotGranted() { 98 ContentProtectionConsentManager manager = 99 createContentProtectionConsentManager(VALUE_FALSE); 100 101 boolean actual = manager.isConsentGranted(TEST_USER_ID); 102 103 assertThat(actual).isFalse(); 104 verifyZeroInteractions(mMockDevicePolicyManagerInternal); 105 } 106 107 @Test isConsentGranted_packageVerifierGranted_userNotManaged()108 public void isConsentGranted_packageVerifierGranted_userNotManaged() { 109 ContentProtectionConsentManager manager = createContentProtectionConsentManager(VALUE_TRUE); 110 111 boolean actual = manager.isConsentGranted(TEST_USER_ID); 112 113 assertThat(actual).isTrue(); 114 verify(mMockDevicePolicyManagerInternal).isUserOrganizationManaged(TEST_USER_ID); 115 } 116 117 @Test isConsentGranted_packageVerifierGranted_userManaged()118 public void isConsentGranted_packageVerifierGranted_userManaged() { 119 when(mMockDevicePolicyManagerInternal.isUserOrganizationManaged(TEST_USER_ID)) 120 .thenReturn(true); 121 ContentProtectionConsentManager manager = createContentProtectionConsentManager(VALUE_TRUE); 122 123 boolean actual = manager.isConsentGranted(TEST_USER_ID); 124 125 assertThat(actual).isFalse(); 126 } 127 128 @Test isConsentGranted_packageVerifierDefault()129 public void isConsentGranted_packageVerifierDefault() { 130 ContentProtectionConsentManager manager = 131 createContentProtectionConsentManager(VALUE_DEFAULT); 132 133 boolean actual = manager.isConsentGranted(TEST_USER_ID); 134 135 assertThat(actual).isFalse(); 136 verifyZeroInteractions(mMockDevicePolicyManagerInternal); 137 } 138 139 @Test contentObserver()140 public void contentObserver() throws Exception { 141 ContentProtectionConsentManager manager = createContentProtectionConsentManager(VALUE_TRUE); 142 boolean firstActual = manager.isConsentGranted(TEST_USER_ID); 143 144 Settings.Global.putInt( 145 mTestableContentResolver, KEY_PACKAGE_VERIFIER_USER_CONSENT, VALUE_FALSE); 146 // Observer has to be called manually, mTestableContentResolver is not propagating 147 manager.mContentObserver.onChange( 148 /* selfChange= */ false, URI_PACKAGE_VERIFIER_USER_CONSENT, TEST_USER_ID); 149 boolean secondActual = manager.isConsentGranted(TEST_USER_ID); 150 151 assertThat(firstActual).isTrue(); 152 assertThat(secondActual).isFalse(); 153 verify(mMockDevicePolicyManagerInternal).isUserOrganizationManaged(TEST_USER_ID); 154 } 155 createContentProtectionConsentManager( ContentResolver contentResolver)156 private ContentProtectionConsentManager createContentProtectionConsentManager( 157 ContentResolver contentResolver) { 158 return new ContentProtectionConsentManager( 159 new Handler(Looper.getMainLooper()), 160 contentResolver, 161 mMockDevicePolicyManagerInternal); 162 } 163 createContentProtectionConsentManager( int valuePackageVerifierUserConsent)164 private ContentProtectionConsentManager createContentProtectionConsentManager( 165 int valuePackageVerifierUserConsent) { 166 Settings.Global.putInt( 167 mTestableContentResolver, 168 KEY_PACKAGE_VERIFIER_USER_CONSENT, 169 valuePackageVerifierUserConsent); 170 return createContentProtectionConsentManager(mTestableContentResolver); 171 } 172 } 173