Merge "Add tests for BinaryTransparencyService."

This commit is contained in:
TreeHugger Robot
2022-02-07 19:01:24 +00:00
committed by Android (Google) Code Review
3 changed files with 125 additions and 4 deletions

View File

@@ -32,6 +32,7 @@ import android.os.SystemProperties;
import android.util.PackageUtils;
import android.util.Slog;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.os.IBinaryTransparencyService;
import com.android.internal.util.FrameworkStatsLog;
@@ -52,11 +53,15 @@ import java.util.stream.Collectors;
public class BinaryTransparencyService extends SystemService {
private static final String TAG = "TransparencyService";
private static final String VBMETA_DIGEST_UNINITIALIZED = "vbmeta-digest-uninitialized";
private static final String VBMETA_DIGEST_UNAVAILABLE = "vbmeta-digest-unavailable";
private static final String SYSPROP_NAME_VBETA_DIGEST = "ro.boot.vbmeta.digest";
@VisibleForTesting
static final String VBMETA_DIGEST_UNINITIALIZED = "vbmeta-digest-uninitialized";
@VisibleForTesting
static final String VBMETA_DIGEST_UNAVAILABLE = "vbmeta-digest-unavailable";
@VisibleForTesting
static final String SYSPROP_NAME_VBETA_DIGEST = "ro.boot.vbmeta.digest";
private static final String BINARY_HASH_ERROR = "SHA256HashError";
@VisibleForTesting
static final String BINARY_HASH_ERROR = "SHA256HashError";
private final Context mContext;
private String mVbmetaDigest;

View File

@@ -30,6 +30,10 @@
}
],
"file_patterns": ["SensorPrivacyService\\.java"]
},
{
"name": "BinaryTransparencyServiceTest",
"file_patterns": ["BinaryTransparencyService\\.java"]
}
],
"presubmit-large": [

View File

@@ -0,0 +1,112 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.RemoteException;
import android.os.ResultReceiver;
import android.os.SystemProperties;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.FileDescriptor;
import java.util.HashMap;
import java.util.Map;
@RunWith(AndroidJUnit4.class)
public class BinaryTransparencyServiceTest {
private Context mContext;
private BinaryTransparencyService mBinaryTransparencyService;
private BinaryTransparencyService.BinaryTransparencyServiceImpl mTestInterface;
@Before
public void setUp() {
mContext = ApplicationProvider.getApplicationContext();
mBinaryTransparencyService = new BinaryTransparencyService(mContext);
mTestInterface = mBinaryTransparencyService.new BinaryTransparencyServiceImpl();
}
private void prepSignedInfo() {
// simulate what happens on boot completed phase
mBinaryTransparencyService.onBootPhase(SystemService.PHASE_BOOT_COMPLETED);
}
private void prepApexInfo() throws RemoteException {
// simulates what happens to apex info after computations are done.
String[] args = {"get", "apex_info"};
mTestInterface.onShellCommand(FileDescriptor.in, FileDescriptor.out, FileDescriptor.err,
args, null, new ResultReceiver(null));
}
@Test
public void getSignedImageInfo_preInitialize_returnsUninitializedString() {
String result = mTestInterface.getSignedImageInfo();
Assert.assertNotNull("VBMeta digest value should not be null", result);
Assert.assertEquals(BinaryTransparencyService.VBMETA_DIGEST_UNINITIALIZED, result);
}
@Test
public void getSignedImageInfo_postInitialize_returnsNonErrorStrings() {
prepSignedInfo();
String result = mTestInterface.getSignedImageInfo();
Assert.assertNotNull("Initialized VBMeta digest string should not be null", result);
Assert.assertNotEquals("VBMeta digest value is uninitialized",
BinaryTransparencyService.VBMETA_DIGEST_UNINITIALIZED, result);
Assert.assertNotEquals("VBMeta value should not be unavailable",
BinaryTransparencyService.VBMETA_DIGEST_UNAVAILABLE, result);
}
@Test
public void getSignedImageInfo_postInitialize_returnsCorrectValue() {
prepSignedInfo();
String result = mTestInterface.getSignedImageInfo();
Assert.assertEquals(
SystemProperties.get(BinaryTransparencyService.SYSPROP_NAME_VBETA_DIGEST,
BinaryTransparencyService.VBMETA_DIGEST_UNAVAILABLE), result);
}
@Test
public void getApexInfo_postInitialize_returnsValidEntries() throws RemoteException {
prepApexInfo();
Map result = mTestInterface.getApexInfo();
Assert.assertNotNull("Apex info map should not be null", result);
Assert.assertFalse("Apex info map should not be empty", result.isEmpty());
}
@Test
public void getApexInfo_postInitialize_returnsActualApexs()
throws RemoteException, PackageManager.NameNotFoundException {
prepApexInfo();
Map result = mTestInterface.getApexInfo();
PackageManager pm = mContext.getPackageManager();
Assert.assertNotNull(pm);
HashMap<PackageInfo, String> castedResult = (HashMap<PackageInfo, String>) result;
for (PackageInfo packageInfo : castedResult.keySet()) {
Assert.assertTrue(packageInfo.packageName + "is not an APEX!", packageInfo.isApex);
}
}
}