• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
18 
19 import static android.os.SecurityStateManager.KEY_KERNEL_VERSION;
20 import static android.os.SecurityStateManager.KEY_SYSTEM_SPL;
21 import static android.os.SecurityStateManager.KEY_VENDOR_SPL;
22 
23 import static com.android.server.SecurityStateManagerService.KERNEL_RELEASE_PATTERN;
24 import static com.android.server.SecurityStateManagerService.VENDOR_SECURITY_PATCH_PROPERTY_KEY;
25 
26 import static junit.framework.Assert.assertEquals;
27 
28 import static org.mockito.ArgumentMatchers.anyInt;
29 import static org.mockito.ArgumentMatchers.anyString;
30 import static org.mockito.Mockito.when;
31 
32 import android.content.Context;
33 import android.content.pm.PackageInfo;
34 import android.content.pm.PackageManager;
35 import android.content.res.Resources;
36 import android.os.Build;
37 import android.os.Bundle;
38 import android.os.SystemProperties;
39 import android.os.VintfRuntimeInfo;
40 
41 import androidx.test.filters.SmallTest;
42 import androidx.test.runner.AndroidJUnit4;
43 
44 import com.android.internal.R;
45 
46 import org.junit.Before;
47 import org.junit.Rule;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
50 import org.mockito.Mock;
51 import org.mockito.junit.MockitoJUnit;
52 import org.mockito.junit.MockitoRule;
53 
54 import java.util.regex.Matcher;
55 
56 @RunWith(AndroidJUnit4.class)
57 @SmallTest
58 public class SecurityStateTest {
59     @Rule
60     public MockitoRule mockitoRule = MockitoJUnit.rule();
61 
62     @Mock
63     private Context mMockContext;
64 
65     @Mock
66     private PackageManager mMockPackageManager;
67 
68     @Mock
69     private Resources mMockResources;
70 
71     private static final String DEFAULT_MODULE_METADATA_PROVIDER = "com.android.modulemetadata";
72     private static final String DEFAULT_MODULE_METADATA_PROVIDER_VERSION = "2023-12-01";
73     private static final String DEFAULT_SECURITY_STATE_PACKAGE = "com.google.android.gms";
74     private static final String DEFAULT_SECURITY_STATE_PACKAGE_VERSION = "2023-12-05";
75     private static final String[] SECURITY_STATE_PACKAGES =
76             new String[]{DEFAULT_SECURITY_STATE_PACKAGE};
77 
78     @Before
setUp()79     public void setUp() throws Exception {
80         when(mMockContext.getPackageManager()).thenReturn(mMockPackageManager);
81         when(mMockContext.getResources()).thenReturn(mMockResources);
82         when(mMockContext.getString(R.string.config_defaultModuleMetadataProvider))
83                 .thenReturn(DEFAULT_MODULE_METADATA_PROVIDER);
84         when(mMockPackageManager.getPackageInfo(anyString(), anyInt()))
85                 .thenReturn(new PackageInfo());
86         PackageInfo moduleMetadataPackageInfo = new PackageInfo();
87         moduleMetadataPackageInfo.versionName = DEFAULT_MODULE_METADATA_PROVIDER_VERSION;
88         when(mMockPackageManager.getPackageInfo(DEFAULT_MODULE_METADATA_PROVIDER, 0))
89                 .thenReturn(moduleMetadataPackageInfo);
90         PackageInfo securityStatePackageInfo = new PackageInfo();
91         securityStatePackageInfo.versionName = DEFAULT_SECURITY_STATE_PACKAGE_VERSION;
92         when(mMockPackageManager.getPackageInfo(DEFAULT_SECURITY_STATE_PACKAGE, 0))
93                 .thenReturn(securityStatePackageInfo);
94         when(mMockResources.getStringArray(R.array.config_securityStatePackages))
95                 .thenReturn(SECURITY_STATE_PACKAGES);
96     }
97 
98     @Test
testGetGlobalSecurityState_returnsBundle()99     public void testGetGlobalSecurityState_returnsBundle() {
100         SecurityStateManagerService securityState = new SecurityStateManagerService(mMockContext);
101 
102         Bundle bundle = securityState.getGlobalSecurityState();
103 
104         assertEquals(bundle.getString(KEY_SYSTEM_SPL), Build.VERSION.SECURITY_PATCH);
105         assertEquals(bundle.getString(KEY_VENDOR_SPL),
106                 SystemProperties.get(VENDOR_SECURITY_PATCH_PROPERTY_KEY, ""));
107         Matcher matcher = KERNEL_RELEASE_PATTERN.matcher(VintfRuntimeInfo.getKernelVersion());
108         String kernelVersion = "";
109         if (matcher.matches()) {
110             kernelVersion = matcher.group(1);
111         }
112         assertEquals(bundle.getString(KEY_KERNEL_VERSION), kernelVersion);
113         assertEquals(bundle.getString(DEFAULT_MODULE_METADATA_PROVIDER),
114                 DEFAULT_MODULE_METADATA_PROVIDER_VERSION);
115         assertEquals(bundle.getString(DEFAULT_SECURITY_STATE_PACKAGE),
116                 DEFAULT_SECURITY_STATE_PACKAGE_VERSION);
117     }
118 }
119