Add test to check ctime of CompOS output after reboot

Fixes: 215589015
Test: atest com.android.tests.odsign.CompOsSigningHostTest
Change-Id: I8aef90a19dbc859264202d208add4f15bc136317
diff --git a/test/odsign/test-src/com/android/tests/odsign/CompOsSigningHostTest.java b/test/odsign/test-src/com/android/tests/odsign/CompOsSigningHostTest.java
index 38eab97..699a83d 100644
--- a/test/odsign/test-src/com/android/tests/odsign/CompOsSigningHostTest.java
+++ b/test/odsign/test-src/com/android/tests/odsign/CompOsSigningHostTest.java
@@ -62,6 +62,8 @@
 
     private static final String ORIGINAL_CHECKSUMS_KEY = "compos_test_orig_checksums";
     private static final String PENDING_CHECKSUMS_KEY = "compos_test_pending_checksums";
+    private static final String TIMESTAMP_VM_START_KEY = "compos_test_timestamp_vm_start";
+    private static final String TIMESTAMP_REBOOT_KEY = "compos_test_timestamp_reboot";
 
     @BeforeClassWithInfo
     public static void beforeClassWithDevice(TestInformation testInfo) throws Exception {
@@ -71,11 +73,13 @@
 
         testInfo.properties().put(ORIGINAL_CHECKSUMS_KEY,
                 checksumDirectoryContentPartial(device,
-                    "/data/misc/apexdata/com.android.art/dalvik-cache/"));
+                    OdsignTestUtils.ART_APEX_DALVIK_CACHE_DIRNAME));
 
         OdsignTestUtils testUtils = new OdsignTestUtils(testInfo);
         testUtils.installTestApex();
 
+        testInfo.properties().put(TIMESTAMP_VM_START_KEY, getDeviceCurrentTimestamp(device));
+
         // Once the test APK is installed, a CompilationJob is (asynchronously) scheduled to run
         // when certain criteria are met, e.g. the device is charging and idle. Since we don't
         // want to wait in the test, here we start the job by ID as soon as it is scheduled.
@@ -90,9 +94,9 @@
         assertThat(device.getChildren(PENDING_ARTIFACTS_DIR)).asList().containsAtLeast(
                 "cache-info.xml", "compos.info", "compos.info.signature");
         testInfo.properties().put(PENDING_CHECKSUMS_KEY,
-                checksumDirectoryContentPartial(device,
-                    "/data/misc/apexdata/com.android.art/compos-pending/"));
+                checksumDirectoryContentPartial(device, PENDING_ARTIFACTS_DIR));
 
+        testInfo.properties().put(TIMESTAMP_REBOOT_KEY, getDeviceCurrentTimestamp(device));
         testUtils.reboot();
     }
 
@@ -107,7 +111,7 @@
     @Test
     public void checkFileChecksums() throws Exception {
         String actualChecksums = checksumDirectoryContentPartial(getDevice(),
-                "/data/misc/apexdata/com.android.art/dalvik-cache/");
+                OdsignTestUtils.ART_APEX_DALVIK_CACHE_DIRNAME);
 
         String pendingChecksums = getTestInformation().properties().get(PENDING_CHECKSUMS_KEY);
         assertThat(actualChecksums).isEqualTo(pendingChecksums);
@@ -117,6 +121,30 @@
         assertThat(actualChecksums).isNotEqualTo(originalChecksums);
     }
 
+    @Test
+    public void checkFileCreationTimeAfterVmStartAndBeforeReboot() throws Exception {
+        // No files are created before our VM starts.
+        int numFiles = countFilesCreatedBeforeTime(
+                getDevice(),
+                OdsignTestUtils.ART_APEX_DALVIK_CACHE_DIRNAME,
+                getTestInformation().properties().get(TIMESTAMP_VM_START_KEY));
+        assertThat(numFiles).isEqualTo(0);
+
+        // (All) Files are created after our VM starts.
+        numFiles = countFilesCreatedAfterTime(
+                getDevice(),
+                OdsignTestUtils.ART_APEX_DALVIK_CACHE_DIRNAME,
+                getTestInformation().properties().get(TIMESTAMP_VM_START_KEY));
+        assertThat(numFiles).isGreaterThan(0);
+
+        // No files are created after reboot.
+        numFiles = countFilesCreatedAfterTime(
+                getDevice(),
+                OdsignTestUtils.ART_APEX_DALVIK_CACHE_DIRNAME,
+                getTestInformation().properties().get(TIMESTAMP_REBOOT_KEY));
+        assertThat(numFiles).isEqualTo(0);
+    }
+
     @Ignore("Compilation log in CompOS isn't useful, and doesn't need to be generated")
     public void verifyCompilationLogGenerated() {}
 
@@ -132,11 +160,32 @@
                 + "| sort -k2");
     }
 
+    private static String getDeviceCurrentTimestamp(ITestDevice device)
+            throws DeviceNotAvailableException {
+        return assertCommandSucceeds(device, "date +'%s'");
+    }
+
+    private static int countFilesCreatedBeforeTime(ITestDevice device, String directory,
+            String timestamp) throws DeviceNotAvailableException {
+        // For simplicity, directory must be a simple path that doesn't require escaping.
+        String output = assertCommandSucceeds(device,
+                "find " + directory + " -type f ! -newerct '@" + timestamp + "' | wc -l");
+        return Integer.parseInt(output);
+    }
+
+    private static int countFilesCreatedAfterTime(ITestDevice device, String directory,
+            String timestamp) throws DeviceNotAvailableException {
+        // For simplicity, directory must be a simple path that doesn't require escaping.
+        String output = assertCommandSucceeds(device,
+                "find " + directory + " -type f -newerct '@" + timestamp + "' | wc -l");
+        return Integer.parseInt(output);
+    }
+
     private static String assertCommandSucceeds(ITestDevice device, String command)
             throws DeviceNotAvailableException {
         CommandResult result = device.executeShellV2Command(command);
         assertWithMessage(result.toString()).that(result.getExitCode()).isEqualTo(0);
-        return result.getStdout();
+        return result.getStdout().trim();
     }
 
     private static void waitForJobToBeScheduled(ITestDevice device, int timeout)