From 056ed98e7fe1921951a4527f677b97fd05cfbb9f Mon Sep 17 00:00:00 2001 From: Mark Teffeteller Date: Thu, 6 Mar 2025 15:11:06 -0800 Subject: Verify CTS test data is received in `addData` method. Confirm the events received by the test app are the expected CTS security and network events, and sets a property value if they are received so that the CTS tests can verify the data was received. Bug: 400790619 Test: atest frameworks/base/services/tests/security/intrusiondetection/src/com/android /server/security/intrusiondetection/IntrusionDetectionServiceTest.java Ignore-AOSP-First: security feature Flag: android.security.internal_log_event_listener Change-Id: Id25633e4ef425fdd65eed74e644f9c9c5e84240b --- .../LocalIntrusionDetectionEventTransport.java | 50 +++++++++++++++++++++- .../coretests/apps/testapp/TestLoggingService.java | 13 +++--- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/services/tests/security/intrusiondetection/src/com/android/server/security/intrusiondetection/TestApp/src/com/android/coretests/apps/testapp/LocalIntrusionDetectionEventTransport.java b/services/tests/security/intrusiondetection/src/com/android/server/security/intrusiondetection/TestApp/src/com/android/coretests/apps/testapp/LocalIntrusionDetectionEventTransport.java index f0012da44fa4..b0b781575cb3 100644 --- a/services/tests/security/intrusiondetection/src/com/android/server/security/intrusiondetection/TestApp/src/com/android/coretests/apps/testapp/LocalIntrusionDetectionEventTransport.java +++ b/services/tests/security/intrusiondetection/src/com/android/server/security/intrusiondetection/TestApp/src/com/android/coretests/apps/testapp/LocalIntrusionDetectionEventTransport.java @@ -18,8 +18,13 @@ package com.android.coretests.apps.testapp; +import android.app.admin.SecurityLog; +import android.app.admin.SecurityLog.SecurityEvent; +import android.content.Context; +import android.content.Intent; import android.security.intrusiondetection.IntrusionDetectionEvent; import android.security.intrusiondetection.IntrusionDetectionEventTransport; +import android.util.Log; import java.util.ArrayList; import java.util.List; @@ -36,6 +41,44 @@ import java.util.List; public class LocalIntrusionDetectionEventTransport extends IntrusionDetectionEventTransport { private List mEvents = new ArrayList<>(); + private static final String ACTION_SECURITY_EVENT_RECEIVED = + "com.android.coretests.apps.testapp.ACTION_SECURITY_EVENT_RECEIVED"; + private static final String TAG = "LocalIntrusionDetectionEventTransport"; + private static final String TEST_SECURITY_EVENT_TAG = "test_security_event_tag"; + private static Context sContext; + + public LocalIntrusionDetectionEventTransport(Context context) { + sContext = context; + } + + // Broadcast an intent to the CTS test service to indicate that the security + // event was received. + private static void broadcastSecurityEventReceived() { + try { + Intent intent = new Intent(ACTION_SECURITY_EVENT_RECEIVED); + sContext.sendBroadcast(intent); + Log.i(TAG, "LIZ_TESTING: sent broadcast"); + } catch (Exception e) { + Log.e(TAG, "Exception sending broadcast", e); + } + } + + private static void checkIfSecurityEventReceivedFromCts(List events) { + // Loop through the events and check if any of them are the security event + // that uses the TEST_SECURITY_EVENT_TAG tag, which is set by the CTS test. + for (IntrusionDetectionEvent event : events) { + if (event.getType() == IntrusionDetectionEvent.SECURITY_EVENT) { + SecurityEvent securityEvent = event.getSecurityEvent(); + Object[] eventData = (Object[]) securityEvent.getData(); + if (securityEvent.getTag() == SecurityLog.TAG_KEY_GENERATED + && eventData[1].equals(TEST_SECURITY_EVENT_TAG)) { + broadcastSecurityEventReceived(); + return; + } + } + } + } + @Override public boolean initialize() { return true; @@ -43,6 +86,11 @@ public class LocalIntrusionDetectionEventTransport extends IntrusionDetectionEve @Override public boolean addData(List events) { + // Our CTS tests will generate a security event. In order to + // verify the event is received with the appropriate data, we will + // check the events locally and set a property value that can be + // read by the test. + checkIfSecurityEventReceivedFromCts(events); mEvents.addAll(events); return true; } @@ -55,4 +103,4 @@ public class LocalIntrusionDetectionEventTransport extends IntrusionDetectionEve public List getEvents() { return mEvents; } -} \ No newline at end of file +} diff --git a/services/tests/security/intrusiondetection/src/com/android/server/security/intrusiondetection/TestApp/src/com/android/coretests/apps/testapp/TestLoggingService.java b/services/tests/security/intrusiondetection/src/com/android/server/security/intrusiondetection/TestApp/src/com/android/coretests/apps/testapp/TestLoggingService.java index e4bf987402fd..9183a75580ff 100644 --- a/services/tests/security/intrusiondetection/src/com/android/server/security/intrusiondetection/TestApp/src/com/android/coretests/apps/testapp/TestLoggingService.java +++ b/services/tests/security/intrusiondetection/src/com/android/server/security/intrusiondetection/TestApp/src/com/android/coretests/apps/testapp/TestLoggingService.java @@ -17,19 +17,20 @@ package com.android.coretests.apps.testapp; import android.app.Service; +import android.content.Context; import android.content.Intent; import android.os.IBinder; -import android.os.Process; - -import com.android.internal.infra.AndroidFuture; - public class TestLoggingService extends Service { private static final String TAG = "TestLoggingService"; private LocalIntrusionDetectionEventTransport mLocalIntrusionDetectionEventTransport; - public TestLoggingService() { - mLocalIntrusionDetectionEventTransport = new LocalIntrusionDetectionEventTransport(); + @Override + public void onCreate() { + super.onCreate(); + + Context context = getApplicationContext(); + mLocalIntrusionDetectionEventTransport = new LocalIntrusionDetectionEventTransport(context); } // Binder given to clients. -- cgit v1.2.3-59-g8ed1b