Add negative tests for KeyPairGeneratorSpecTest

This adds the negative tests to make sure exceptions are thrown when a
spec is built incorrectly.

Bug: 8608597
Change-Id: Ia2fb6847dd94ee33c87a8924cecb934ffe090016
diff --git a/tests/tests/keystore/src/android/keystore/cts/KeyPairGeneratorSpecTest.java b/tests/tests/keystore/src/android/keystore/cts/KeyPairGeneratorSpecTest.java
index 06ac987..33c8955 100644
--- a/tests/tests/keystore/src/android/keystore/cts/KeyPairGeneratorSpecTest.java
+++ b/tests/tests/keystore/src/android/keystore/cts/KeyPairGeneratorSpecTest.java
@@ -83,4 +83,91 @@
 
         assertTrue("encryption flag should be on", spec.isEncryptionRequired());
     }
+
+    public void testBuilder_NullContext_Failure() throws Exception {
+        try {
+            new KeyPairGeneratorSpec.Builder(null);
+            fail("Should throw NullPointerException when context is null");
+        } catch (NullPointerException expected) {
+        }
+    }
+
+    public void testBuilder_MissingAlias_Failure() throws Exception {
+        try {
+            new KeyPairGeneratorSpec.Builder(getContext())
+                    .setSubject(TEST_DN_1)
+                    .setSerialNumber(SERIAL_1)
+                    .setStartDate(NOW)
+                    .setEndDate(NOW_PLUS_10_YEARS)
+                    .build();
+            fail("Should throw IllegalArgumentException when alias is missing");
+        } catch (IllegalArgumentException expected) {
+        }
+    }
+
+    public void testBuilder_MissingSubjectDN_Failure() throws Exception {
+        try {
+            new KeyPairGeneratorSpec.Builder(getContext())
+                    .setAlias(TEST_ALIAS_1)
+                    .setSerialNumber(SERIAL_1)
+                    .setStartDate(NOW)
+                    .setEndDate(NOW_PLUS_10_YEARS)
+                    .build();
+            fail("Should throw IllegalArgumentException when subject is missing");
+        } catch (IllegalArgumentException expected) {
+        }
+    }
+
+    public void testBuilder_MissingSerialNumber_Failure() throws Exception {
+        try {
+            new KeyPairGeneratorSpec.Builder(getContext())
+                    .setAlias(TEST_ALIAS_1)
+                    .setSubject(TEST_DN_1)
+                    .setStartDate(NOW)
+                    .setEndDate(NOW_PLUS_10_YEARS)
+                    .build();
+            fail("Should throw IllegalArgumentException when serialNumber is missing");
+        } catch (IllegalArgumentException expected) {
+        }
+    }
+
+    public void testBuilder_MissingStartDate_Failure() throws Exception {
+        try {
+            new KeyPairGeneratorSpec.Builder(getContext())
+                    .setAlias(TEST_ALIAS_1)
+                    .setSubject(TEST_DN_1)
+                    .setSerialNumber(SERIAL_1)
+                    .setEndDate(NOW_PLUS_10_YEARS)
+                    .build();
+            fail("Should throw IllegalArgumentException when startDate is missing");
+        } catch (IllegalArgumentException expected) {
+        }
+    }
+
+    public void testBuilder_MissingEndDate_Failure() throws Exception {
+        try {
+            new KeyPairGeneratorSpec.Builder(getContext())
+                    .setAlias(TEST_ALIAS_1)
+                    .setSubject(TEST_DN_1)
+                    .setSerialNumber(SERIAL_1)
+                    .setStartDate(NOW)
+                    .build();
+            fail("Should throw IllegalArgumentException when endDate is missing");
+        } catch (IllegalArgumentException expected) {
+        }
+    }
+
+    public void testBuilder_EndBeforeStart_Failure() throws Exception {
+        try {
+            new KeyPairGeneratorSpec.Builder(getContext())
+                    .setAlias(TEST_ALIAS_1)
+                    .setSubject(TEST_DN_1)
+                    .setSerialNumber(SERIAL_1)
+                    .setStartDate(NOW_PLUS_10_YEARS)
+                    .setEndDate(NOW)
+                    .build();
+            fail("Should throw IllegalArgumentException when end is before start");
+        } catch (IllegalArgumentException expected) {
+        }
+    }
 }