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

Commit 826f8380 authored by Mark Teffeteller's avatar Mark Teffeteller Committed by Android (Google) Code Review
Browse files

Merge "Add the `TestLoggingService` service for mocking communication with GMS core." into main

parents c0bf94ea ced265e1
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ android_test {
        "androidx.test.rules",
        "androidx.test.runner",
        "compatibility-device-util-axt",
        "coretests-aidl",
        "frameworks-base-testutils",
        "junit",
        "platform-test-annotations",
+2 −0
Original line number Diff line number Diff line
@@ -31,6 +31,8 @@
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
            </intent-filter>
        </receiver>
      <service android:name="com.android.server.security.intrusiondetection.TestLoggingService"
                 android:exported="true"/>
    </application>

    <instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
+66 −2
Original line number Diff line number Diff line
@@ -42,6 +42,10 @@ import android.app.admin.SecurityLog;
import android.app.admin.SecurityLog.SecurityEvent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.Bundle;
import android.os.Looper;
import android.os.PermissionEnforcer;
import android.os.RemoteException;
@@ -52,6 +56,7 @@ import android.security.intrusiondetection.IIntrusionDetectionServiceStateCallba
import android.security.intrusiondetection.IntrusionDetectionEvent;
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties;
import android.util.Log;

import androidx.test.core.app.ApplicationProvider;

@@ -66,6 +71,8 @@ import com.android.bedstead.permissions.CommonPermissions;
import com.android.bedstead.permissions.PermissionContext;
import com.android.bedstead.permissions.annotations.EnsureHasPermission;
import com.android.server.ServiceThread;
import com.android.server.security.intrusiondetection.TestLoggingService;
import com.android.server.security.intrusiondetection.TestLoggingService.LocalBinder;

import org.junit.Before;
import org.junit.Ignore;
@@ -116,6 +123,8 @@ public class IntrusionDetectionServiceTest {
    private TestLooper mTestLooperOfDataAggregator;
    private Looper mLooperOfDataAggregator;
    private FakePermissionEnforcer mPermissionEnforcer;
    private TestLoggingService mService;
    private boolean mBoundToLoggingService = false;

    @BeforeClass
    public static void setDeviceOwner() {
@@ -141,8 +150,8 @@ public class IntrusionDetectionServiceTest {

    @SuppressLint("VisibleForTests")
    @Before
    public void setUp() {
        mContext = spy(ApplicationProvider.getApplicationContext());
    public void setUp() throws Exception {
        mContext = ApplicationProvider.getApplicationContext();

        mPermissionEnforcer = new FakePermissionEnforcer();
        mPermissionEnforcer.grant(READ_INTRUSION_DETECTION_STATE);
@@ -565,6 +574,61 @@ public class IntrusionDetectionServiceTest {
        }
    }

    @Test
    public void test_StartBackupTransportService() {
        final String TAG = "test_StartBackupTransportService";
        ServiceConnection serviceConnection = null;

        assertEquals(false, mBoundToLoggingService);
        try {
            serviceConnection = startTestService();
            assertEquals(true, mBoundToLoggingService);
            assertNotNull(serviceConnection);
        } catch (SecurityException e) {
            Log.e(TAG, "SecurityException while starting: ", e);
            fail("Exception thrown while connecting to service");
        } catch (InterruptedException e) {
            Log.e(TAG, "InterruptedException while starting: ", e);
            fail("Interrupted while connecting to service");
        } finally {
            mContext.unbindService(serviceConnection);
        }
    }

    private ServiceConnection startTestService() throws SecurityException, InterruptedException {
        final String TAG = "startTestService";
        final CountDownLatch latch = new CountDownLatch(1);

        ServiceConnection serviceConnection = new ServiceConnection() {
            // Called when the connection with the service is established.
            @Override
            public void onServiceConnected(ComponentName className, IBinder service) {
                // Because we have bound to an explicit
                // service that is running in our own process, we can
                // cast its IBinder to a concrete class and directly access it.
                Log.d(TAG, "onServiceConnected");
                LocalBinder binder = (LocalBinder) service;
                mService = binder.getService();
                mBoundToLoggingService = true;
                latch.countDown();
            }

            // Called when the connection with the service disconnects unexpectedly.
            @Override
            public void onServiceDisconnected(ComponentName className) {
                Log.d(TAG, "onServiceDisconnected");
                mBoundToLoggingService = false;
                latch.countDown();
            }
        };

        Intent intent = new Intent(mContext, TestLoggingService.class);
        mContext.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
        latch.await(5, TimeUnit.SECONDS);

        return serviceConnection;
    }

    private class MockInjector implements IntrusionDetectionService.Injector {
        private final Context mContext;

+50 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.security.intrusiondetection;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Process;
import android.util.Log;


public class TestLoggingService extends Service {
    private static final String TAG = "TestLoggingService";

    // Binder given to clients.
    private final IBinder binder = new LocalBinder();

    /**
     * Class used for the client Binder.  Because we know this service always
     * runs in the same process as its clients, we don't need to deal with IPC.
     */
    public class LocalBinder extends Binder {
        TestLoggingService getService() {
            // Return this instance of TestLoggingService so clients
            // can call public methods.
            return TestLoggingService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        // Return the binder for the service
        return binder;
    }
}