Updating test to include vendor security patch level

This will enforce that vendors at least have a date present for their
last vendor security patch and that it's formatted correctly and
reasonably dated.

Test: This cts test passes
Bug: 76428542
Change-Id: Iaab24155477263e6069e4e66e98ca7168bf34960
Merged-In: Iaab24155477263e6069e4e66e98ca7168bf34960
(cherry picked from commit 7312d60c48cf4f952bd0f7d419aa109da9d78177)
diff --git a/tests/tests/os/src/android/os/cts/SecurityPatchTest.java b/tests/tests/os/src/android/os/cts/SecurityPatchTest.java
index d6fb646..da72966 100644
--- a/tests/tests/os/src/android/os/cts/SecurityPatchTest.java
+++ b/tests/tests/os/src/android/os/cts/SecurityPatchTest.java
@@ -30,17 +30,21 @@
 
     private static final String TAG = SecurityPatchTest.class.getSimpleName();
     private static final String SECURITY_PATCH_ERROR =
-            "ro.build.version.security_patch should be in the format \"YYYY-MM-DD\". Found \"%s\"";
+            "security_patch should be in the format \"YYYY-MM-DD\". Found \"%s\"";
     private static final String SECURITY_PATCH_DATE_ERROR =
-            "ro.build.version.security_patch should be \"%d-%02d\" or later. Found \"%s\"";
+            "security_patch should be \"%d-%02d\" or later. Found \"%s\"";
     private static final int SECURITY_PATCH_YEAR = 2016;
     private static final int SECURITY_PATCH_MONTH = 12;
 
     private boolean mSkipTests = false;
+    private String mVendorSecurityPatch;
+    private String mBuildSecurityPatch;
 
     @Override
     protected void setUp() {
         mSkipTests = (ApiLevelUtil.isBefore(Build.VERSION_CODES.M));
+        mVendorSecurityPatch = SystemProperties.get("ro.vendor.build.security_patch", "");
+        mBuildSecurityPatch = Build.VERSION.SECURITY_PATCH;
     }
 
     /** Security patch string must exist in M or higher **/
@@ -49,54 +53,79 @@
             Log.w(TAG, "Skipping M+ Test.");
             return;
         }
+        String error = String.format(SECURITY_PATCH_ERROR, mBuildSecurityPatch);
+        assertTrue(error, !mBuildSecurityPatch.isEmpty());
+    }
 
-        String buildSecurityPatch = Build.VERSION.SECURITY_PATCH;
-        String error = String.format(SECURITY_PATCH_ERROR, buildSecurityPatch);
-        assertTrue(error, !buildSecurityPatch.isEmpty());
+    public void testVendorSecurityPatchFound() {
+        if (Build.VERSION.FIRST_SDK_INT <= Build.VERSION_CODES.O) {
+            Log.w(TAG, "Skipping P+ Test");
+            return;
+        }
+        assertTrue(!mVendorSecurityPatch.isEmpty());
+    }
+
+    public void testSecurityPatchesFormat() {
+        if (mSkipTests) {
+            Log.w(TAG, "Skipping M+ Test.");
+            return;
+        }
+        String error = String.format(SECURITY_PATCH_ERROR, mBuildSecurityPatch);
+        testSecurityPatchFormat(mBuildSecurityPatch, error);
+
+        if (Build.VERSION.FIRST_SDK_INT <= Build.VERSION_CODES.O) {
+            Log.w(TAG, "Skipping P+ Test");
+            return;
+        }
+        error = String.format(SECURITY_PATCH_ERROR, mVendorSecurityPatch);
+        testSecurityPatchFormat(mVendorSecurityPatch, error);
     }
 
     /** Security patch should be of the form YYYY-MM-DD in M or higher */
-    public void testSecurityPatchFormat() {
-        if (mSkipTests) {
-            Log.w(TAG, "Skipping M+ Test.");
-            return;
-        }
-
-        String buildSecurityPatch = Build.VERSION.SECURITY_PATCH;
-        String error = String.format(SECURITY_PATCH_ERROR, buildSecurityPatch);
-
-        assertEquals(error, 10, buildSecurityPatch.length());
-        assertTrue(error, Character.isDigit(buildSecurityPatch.charAt(0)));
-        assertTrue(error, Character.isDigit(buildSecurityPatch.charAt(1)));
-        assertTrue(error, Character.isDigit(buildSecurityPatch.charAt(2)));
-        assertTrue(error, Character.isDigit(buildSecurityPatch.charAt(3)));
-        assertEquals(error, '-', buildSecurityPatch.charAt(4));
-        assertTrue(error, Character.isDigit(buildSecurityPatch.charAt(5)));
-        assertTrue(error, Character.isDigit(buildSecurityPatch.charAt(6)));
-        assertEquals(error, '-', buildSecurityPatch.charAt(7));
-        assertTrue(error, Character.isDigit(buildSecurityPatch.charAt(8)));
-        assertTrue(error, Character.isDigit(buildSecurityPatch.charAt(9)));
+    private void testSecurityPatchFormat(String patch, String error) {
+        assertEquals(error, 10, patch.length());
+        assertTrue(error, Character.isDigit(patch.charAt(0)));
+        assertTrue(error, Character.isDigit(patch.charAt(1)));
+        assertTrue(error, Character.isDigit(patch.charAt(2)));
+        assertTrue(error, Character.isDigit(patch.charAt(3)));
+        assertEquals(error, '-', patch.charAt(4));
+        assertTrue(error, Character.isDigit(patch.charAt(5)));
+        assertTrue(error, Character.isDigit(patch.charAt(6)));
+        assertEquals(error, '-', patch.charAt(7));
+        assertTrue(error, Character.isDigit(patch.charAt(8)));
+        assertTrue(error, Character.isDigit(patch.charAt(9)));
     }
 
-    /** Security patch should no older than the month this test was updated in M or higher **/
-    public void testSecurityPatchDate() {
+    public void testSecurityPatchDates() {
         if (mSkipTests) {
             Log.w(TAG, "Skipping M+ Test.");
             return;
         }
 
-        String buildSecurityPatch = Build.VERSION.SECURITY_PATCH;
         String error = String.format(SECURITY_PATCH_DATE_ERROR,
                                      SECURITY_PATCH_YEAR,
                                      SECURITY_PATCH_MONTH,
-                                     buildSecurityPatch);
+                                     mBuildSecurityPatch);
+        testSecurityPatchDate(mBuildSecurityPatch, error);
 
+        if (Build.VERSION.FIRST_SDK_INT <= Build.VERSION_CODES.O) {
+            Log.w(TAG, "Skipping P+ Test");
+            return;
+        }
+        error = String.format(SECURITY_PATCH_DATE_ERROR,
+                                     SECURITY_PATCH_YEAR,
+                                     SECURITY_PATCH_MONTH,
+                                     mVendorSecurityPatch);
+        testSecurityPatchDate(mVendorSecurityPatch, error);
+    }
+    /** Security patch should no older than the month this test was updated in M or higher **/
+    private void testSecurityPatchDate(String patch, String error) {
         int declaredYear = 0;
         int declaredMonth = 0;
 
         try {
-            declaredYear = Integer.parseInt(buildSecurityPatch.substring(0,4));
-            declaredMonth = Integer.parseInt(buildSecurityPatch.substring(5,7));
+            declaredYear = Integer.parseInt(patch.substring(0,4));
+            declaredMonth = Integer.parseInt(patch.substring(5,7));
         } catch (Exception e) {
             assertTrue(error, false);
         }