Fix failing test because assert was stronger than necessary

The test that verifies that the boot-complete receiver deletes adservices files within the ExtServices APK data directory was asserting that once the receiver finished, there would be no files present whose names contained the word "adservices". However, the code is only supposed to delete files whose names *begin with* the word "adservices". So, the assert was failing though the code was behaving correctly. Fixing the assert to match expected behavior.

Bug: 332178737
Test: atest CtsExtServicesHostTests-tplus

Change-Id: I266868c866349d3d1f41739ea93626c8de04f87b
diff --git a/java/tests/hosttests/src/android/ext/services/hosttests/AdServicesFilesCleanupBootCompleteReceiverHostTest.java b/java/tests/hosttests/src/android/ext/services/hosttests/AdServicesFilesCleanupBootCompleteReceiverHostTest.java
index 76b0892..2ffaed9 100644
--- a/java/tests/hosttests/src/android/ext/services/hosttests/AdServicesFilesCleanupBootCompleteReceiverHostTest.java
+++ b/java/tests/hosttests/src/android/ext/services/hosttests/AdServicesFilesCleanupBootCompleteReceiverHostTest.java
@@ -58,8 +58,11 @@
     private static final String RECEIVER_DISABLED_LOG_TEXT =
             "Disabled AdServices files cleanup receiver";
     private static final String RECEIVER_EXECUTED_LOG_TEXT = "AdServices files cleanup receiver";
+    private static final String FILE_WITH_ADSERVICES_AS_PREFIX = "adservices_data.txt";
+    private static final String FILE_WITH_ADSERVICES_NOT_PREFIX = "test_adservices.txt";
 
     private String mAdServicesFilePath;
+    private String mAdServicesNotPrefixFilePath;
     private String mExtServicesPackageName;
 
     @Rule
@@ -87,26 +90,20 @@
                         .findFirst()
                         .orElse(null);
         assertWithMessage("ExtServices package").that(extServicesPackage).isNotNull();
+
         mExtServicesPackageName = extServicesPackage.getPackageName();
+        assertWithMessage("ExtServices package name").that(mExtServicesPackageName).isNotNull();
 
         // Put some data in the ExtServices apk
-        mAdServicesFilePath =
-                String.format(
-                        "/data/user/%d/%s/adservices_data.txt",
-                        device.getCurrentUser(), extServicesPackage.getPackageName());
-        runShellCommand("echo \"Hello\" > %s", mAdServicesFilePath);
-        assertWithMessage("%s exists", mAdServicesFilePath)
-                .that(device.doesFileExist(mAdServicesFilePath))
-                .isTrue();
+        mAdServicesFilePath = createFile(FILE_WITH_ADSERVICES_AS_PREFIX);
+        mAdServicesNotPrefixFilePath = createFile(FILE_WITH_ADSERVICES_NOT_PREFIX);
     }
 
     @After
     public void tearDown() throws Exception {
         if (mDevice != null) {
-            if (mAdServicesFilePath != null && mDevice.doesFileExist(mAdServicesFilePath)) {
-                mDevice.deleteFile(mAdServicesFilePath);
-            }
-
+            cleanupFile(mAdServicesFilePath);
+            cleanupFile(mAdServicesNotPrefixFilePath);
             mDevice.disableAdbRoot();
         }
     }
@@ -147,16 +144,18 @@
         // Reboot, wait, and verify logs.
         verifyReceiverExecuted(device);
 
-        // Verify that all adservices files were deleted.
+        // Verify that the specific file we created with the adservices prefix was deleted.
         assertWithMessage("%s exists", mAdServicesFilePath)
                 .that(device.doesFileExist(mAdServicesFilePath))
                 .isFalse();
 
-        String lsCommand =
-                String.format(
-                        "ls /data/user/%d/%s -R", device.getCurrentUser(), mExtServicesPackageName);
-        String lsOutput = device.executeShellCommand(lsCommand).toLowerCase(Locale.ROOT);
-        assertWithMessage("Output of %s", lsCommand).that(lsOutput).doesNotContain("adservices");
+        // Verify that there are no files beginning with the "adservices" prefix.
+        assertNoFilesBeginningWithAdServices();
+
+        // Verify that the file with adservices in the name, but not at the beginning, is present
+        assertWithMessage("%s exists", mAdServicesNotPrefixFilePath)
+                .that(mDevice.doesFileExist(mAdServicesNotPrefixFilePath))
+                .isTrue();
 
         // Verify that after a reboot the receiver does not execute
         verifyReceiverDidNotExecute(device);
@@ -265,4 +264,39 @@
         CLog.d("ExtServices apex version = <%s>, apk version = <%s>, priv-app = <%s>", apexVersion,
                 apkVersion, privAppName);
     }
+
+    private String createFile(String fileName) throws DeviceNotAvailableException {
+        String fullPath = String.format(
+                "/data/user/%d/%s/%s",
+                mDevice.getCurrentUser(),
+                mExtServicesPackageName,
+                fileName);
+
+        runShellCommand("echo \"Hello\" > %s", fullPath);
+        assertWithMessage("%s exists", fullPath)
+                .that(mDevice.doesFileExist(fullPath))
+                .isTrue();
+        return fullPath;
+    }
+
+    private void cleanupFile(String fullPath) throws DeviceNotAvailableException {
+        if (fullPath != null && mDevice.doesFileExist(fullPath)) {
+            mDevice.deleteFile(fullPath);
+        }
+    }
+
+    private void assertNoFilesBeginningWithAdServices() throws DeviceNotAvailableException {
+        String lsCommand =
+                String.format(
+                        "ls /data/user/%d/%s -R -1",
+                        mDevice.getCurrentUser(), mExtServicesPackageName);
+        String lsOutput = mDevice.executeShellCommand(lsCommand).toLowerCase(Locale.ROOT);
+        boolean noFilesBeginningWithAdServices =
+                Arrays.stream(lsOutput.split("\n")).noneMatch(s -> s.startsWith("adservices"));
+
+        assertWithMessage("No files beginning with 'adservices' in output of \"%s\": [\n%s\n]",
+                lsCommand, lsOutput)
+                .that(noFilesBeginningWithAdServices)
+                .isTrue();
+    }
 }