Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7f02745d authored by Billy Lau's avatar Billy Lau
Browse files

Add tests for BinaryTransparencyService.

Introduced unit tests to test entry points exposed by
BinaryTransparencyService.

Also added TEST_MAPPING to add the tests to presubmit.

Bug: 206436820
Test: BinaryTransparencyServiceTest

Change-Id: I24c9452feb54eaee14312e1c8fbcf43f323ba65e
parent 0c20077d
Loading
Loading
Loading
Loading
+9 −4
Original line number Diff line number Diff line
@@ -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 java.io.File;
@@ -51,11 +52,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;
+4 −0
Original line number Diff line number Diff line
@@ -30,6 +30,10 @@
                }
            ],
            "file_patterns": ["SensorPrivacyService\\.java"]
        },
        {
            "name": "BinaryTransparencyServiceTest",
            "file_patterns": ["BinaryTransparencyService\\.java"]
        }
    ],
    "presubmit-large": [
+112 −0
Original line number Diff line number Diff line
/*
 * 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);
        }
    }

}