blob: 37ff06a18492aa2ceb91edc471bf185c677e80dd [file] [log] [blame]
Song Panf93a39c2019-11-19 00:58:31 +00001/*
2 * Copyright (C) 2019 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
17package com.android.server.integrity;
18
19import static android.content.pm.PackageManager.EXTRA_VERIFICATION_ID;
20
21import static org.mockito.Mockito.verify;
22
23import android.content.Intent;
24import android.content.pm.PackageManager;
25import android.content.pm.PackageManagerInternal;
26
27import androidx.test.InstrumentationRegistry;
28import androidx.test.runner.AndroidJUnit4;
29
30import com.android.server.LocalServices;
31
32import org.junit.Before;
33import org.junit.Rule;
34import org.junit.Test;
35import org.junit.runner.RunWith;
36import org.mockito.Mock;
37import org.mockito.junit.MockitoJUnit;
38import org.mockito.junit.MockitoRule;
39
40/** Unit test for {@link com.android.server.integrity.AppIntegrityManagerServiceImpl} */
41@RunWith(AndroidJUnit4.class)
42public class AppIntegrityManagerServiceImplTest {
43
44 @Rule public MockitoRule mMockitoRule = MockitoJUnit.rule();
45
46 @Mock PackageManagerInternal mPackageManagerInternal;
47
48 // under test
49 private AppIntegrityManagerServiceImpl mService;
50
51 @Before
52 public void setup() {
53 LocalServices.addService(PackageManagerInternal.class, mPackageManagerInternal);
54
55 mService = new AppIntegrityManagerServiceImpl(InstrumentationRegistry.getContext());
56 }
57
58 @Test
59 public void integrityVerification_allow() {
60 int verificationId = 2;
61 Intent integrityVerificationIntent = new Intent();
62 integrityVerificationIntent.setAction(Intent.ACTION_PACKAGE_NEEDS_INTEGRITY_VERIFICATION);
63 integrityVerificationIntent.putExtra(EXTRA_VERIFICATION_ID, verificationId);
64
65 // We cannot send the broadcast using the context since it is a protected broadcast and
66 // we will get a security exception.
67 mService.handleIntegrityVerification(integrityVerificationIntent);
68
69 verify(mPackageManagerInternal)
70 .setIntegrityVerificationResult(verificationId, PackageManager.VERIFICATION_ALLOW);
71 }
72}