diff options
author | tholenst <tholenst@google.com> | 2023-07-10 04:33:38 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-07-10 04:34:29 -0700 |
commit | 2995b1ecd7725bd4534e1acf21af0b038265b87d (patch) | |
tree | ca3f095564e7d9798bedc355637a42dda0ea76f4 /java_src | |
parent | 13736ff92436c2e22ae846e6654d387b9a5897ec (diff) | |
download | tink-2995b1ecd7725bd4534e1acf21af0b038265b87d.tar.gz |
Aeads: Replace the validation tests with tests checking if the key generation succeeds.
This is a bit more accurate, but has the disadvantage of being potentially slower. Anyhow, i want to change the template functions to not return these pairs (output_prefix, format) anymore, so we need to change this.
PiperOrigin-RevId: 546832073
Diffstat (limited to 'java_src')
7 files changed, 119 insertions, 48 deletions
diff --git a/java_src/src/test/java/com/google/crypto/tink/aead/AesCtrHmacAeadKeyManagerTest.java b/java_src/src/test/java/com/google/crypto/tink/aead/AesCtrHmacAeadKeyManagerTest.java index 3d57024d4..0504d6c6b 100644 --- a/java_src/src/test/java/com/google/crypto/tink/aead/AesCtrHmacAeadKeyManagerTest.java +++ b/java_src/src/test/java/com/google/crypto/tink/aead/AesCtrHmacAeadKeyManagerTest.java @@ -22,6 +22,8 @@ import static org.junit.Assert.assertThrows; import com.google.crypto.tink.Aead; import com.google.crypto.tink.KeyTemplate; +import com.google.crypto.tink.KeyTemplates; +import com.google.crypto.tink.KeysetHandle; import com.google.crypto.tink.internal.KeyTypeManager; import com.google.crypto.tink.proto.AesCtrHmacAeadKey; import com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat; @@ -42,11 +44,14 @@ import java.util.Set; import java.util.TreeSet; import org.junit.Before; import org.junit.Test; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.FromDataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; /** Tests for AesCtrHmacAeadKeyManager. */ -@RunWith(JUnit4.class) +@RunWith(Theories.class) public class AesCtrHmacAeadKeyManagerTest { private final AesCtrHmacAeadKeyManager manager = new AesCtrHmacAeadKeyManager(); private final KeyTypeManager.KeyFactory<AesCtrHmacAeadKeyFormat, AesCtrHmacAeadKey> factory = @@ -366,12 +371,20 @@ public class AesCtrHmacAeadKeyManagerTest { testKeyTemplateCompatible(manager, AesCtrHmacAeadKeyManager.aes128CtrHmacSha256Template()); } - @Test - public void testKeyFormats() throws Exception { - factory.validateKeyFormat(factory.keyFormats().get("AES128_CTR_HMAC_SHA256").keyFormat); - factory.validateKeyFormat(factory.keyFormats().get("AES128_CTR_HMAC_SHA256_RAW").keyFormat); - - factory.validateKeyFormat(factory.keyFormats().get("AES256_CTR_HMAC_SHA256").keyFormat); - factory.validateKeyFormat(factory.keyFormats().get("AES256_CTR_HMAC_SHA256_RAW").keyFormat); + @DataPoints("templateNames") + public static final String[] KEY_TEMPLATES = + new String[] { + "AES128_CTR_HMAC_SHA256", + "AES128_CTR_HMAC_SHA256_RAW", + "AES256_CTR_HMAC_SHA256", + "AES256_CTR_HMAC_SHA256_RAW", + }; + + @Theory + public void testTemplates(@FromDataPoints("templateNames") String templateName) throws Exception { + KeysetHandle h = KeysetHandle.generateNew(KeyTemplates.get(templateName)); + assertThat(h.size()).isEqualTo(1); + assertThat(h.getAt(0).getKey().getParameters()) + .isEqualTo(KeyTemplates.get(templateName).toParameters()); } } diff --git a/java_src/src/test/java/com/google/crypto/tink/aead/AesEaxKeyManagerTest.java b/java_src/src/test/java/com/google/crypto/tink/aead/AesEaxKeyManagerTest.java index b32b2d142..126630606 100644 --- a/java_src/src/test/java/com/google/crypto/tink/aead/AesEaxKeyManagerTest.java +++ b/java_src/src/test/java/com/google/crypto/tink/aead/AesEaxKeyManagerTest.java @@ -26,6 +26,8 @@ import static org.junit.Assert.fail; import com.google.crypto.tink.Aead; import com.google.crypto.tink.KeyTemplate; +import com.google.crypto.tink.KeyTemplates; +import com.google.crypto.tink.KeysetHandle; import com.google.crypto.tink.internal.KeyTypeManager; import com.google.crypto.tink.proto.AesEaxKey; import com.google.crypto.tink.proto.AesEaxKeyFormat; @@ -41,11 +43,14 @@ import java.util.Set; import java.util.TreeSet; import org.junit.Before; import org.junit.Test; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.FromDataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; /** Test for AesEaxJce and its key manager. */ -@RunWith(JUnit4.class) +@RunWith(Theories.class) public class AesEaxKeyManagerTest { private final AesEaxKeyManager manager = new AesEaxKeyManager(); private final KeyTypeManager.KeyFactory<AesEaxKeyFormat, AesEaxKey> factory = @@ -353,12 +358,17 @@ public class AesEaxKeyManagerTest { testKeyTemplateCompatible(manager, AesEaxKeyManager.rawAes256EaxTemplate()); } - @Test - public void testKeyFormats() throws Exception { - factory.validateKeyFormat(factory.keyFormats().get("AES128_EAX").keyFormat); - factory.validateKeyFormat(factory.keyFormats().get("AES128_EAX_RAW").keyFormat); + @DataPoints("templateNames") + public static final String[] KEY_TEMPLATES = + new String[] { + "AES128_EAX", "AES128_EAX_RAW", "AES256_EAX", "AES256_EAX_RAW", + }; - factory.validateKeyFormat(factory.keyFormats().get("AES256_EAX").keyFormat); - factory.validateKeyFormat(factory.keyFormats().get("AES256_EAX_RAW").keyFormat); + @Theory + public void testTemplates(@FromDataPoints("templateNames") String templateName) throws Exception { + KeysetHandle h = KeysetHandle.generateNew(KeyTemplates.get(templateName)); + assertThat(h.size()).isEqualTo(1); + assertThat(h.getAt(0).getKey().getParameters()) + .isEqualTo(KeyTemplates.get(templateName).toParameters()); } } diff --git a/java_src/src/test/java/com/google/crypto/tink/aead/AesGcmKeyManagerTest.java b/java_src/src/test/java/com/google/crypto/tink/aead/AesGcmKeyManagerTest.java index f6c745eee..c527c245d 100644 --- a/java_src/src/test/java/com/google/crypto/tink/aead/AesGcmKeyManagerTest.java +++ b/java_src/src/test/java/com/google/crypto/tink/aead/AesGcmKeyManagerTest.java @@ -25,6 +25,8 @@ import static org.junit.Assert.fail; import com.google.crypto.tink.Aead; import com.google.crypto.tink.KeyTemplate; +import com.google.crypto.tink.KeyTemplates; +import com.google.crypto.tink.KeysetHandle; import com.google.crypto.tink.internal.KeyTypeManager; import com.google.crypto.tink.proto.AesGcmKey; import com.google.crypto.tink.proto.AesGcmKeyFormat; @@ -41,11 +43,14 @@ import java.util.Set; import java.util.TreeSet; import org.junit.Before; import org.junit.Test; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.FromDataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; /** Test for AesGcmJce and its key manager. */ -@RunWith(JUnit4.class) +@RunWith(Theories.class) public class AesGcmKeyManagerTest { private final AesGcmKeyManager manager = new AesGcmKeyManager(); private final KeyTypeManager.KeyFactory<AesGcmKeyFormat, AesGcmKey> factory = @@ -506,12 +511,17 @@ public class AesGcmKeyManagerTest { testKeyTemplateCompatible(manager, AesGcmKeyManager.rawAes256GcmTemplate()); } - @Test - public void testKeyFormats() throws Exception { - factory.validateKeyFormat(factory.keyFormats().get("AES128_GCM").keyFormat); - factory.validateKeyFormat(factory.keyFormats().get("AES128_GCM_RAW").keyFormat); - - factory.validateKeyFormat(factory.keyFormats().get("AES256_GCM").keyFormat); - factory.validateKeyFormat(factory.keyFormats().get("AES256_GCM_RAW").keyFormat); + @DataPoints("templateNames") + public static final String[] KEY_TEMPLATES = + new String[] { + "AES128_GCM", "AES128_GCM_RAW", "AES256_GCM", "AES256_GCM_RAW", + }; + + @Theory + public void testTemplates(@FromDataPoints("templateNames") String templateName) throws Exception { + KeysetHandle h = KeysetHandle.generateNew(KeyTemplates.get(templateName)); + assertThat(h.size()).isEqualTo(1); + assertThat(h.getAt(0).getKey().getParameters()) + .isEqualTo(KeyTemplates.get(templateName).toParameters()); } } diff --git a/java_src/src/test/java/com/google/crypto/tink/aead/AesGcmSivKeyManagerTest.java b/java_src/src/test/java/com/google/crypto/tink/aead/AesGcmSivKeyManagerTest.java index 95ecdc109..4f9c1cc65 100644 --- a/java_src/src/test/java/com/google/crypto/tink/aead/AesGcmSivKeyManagerTest.java +++ b/java_src/src/test/java/com/google/crypto/tink/aead/AesGcmSivKeyManagerTest.java @@ -23,6 +23,8 @@ import static org.junit.Assert.assertThrows; import com.google.crypto.tink.Aead; import com.google.crypto.tink.KeyTemplate; +import com.google.crypto.tink.KeyTemplates; +import com.google.crypto.tink.KeysetHandle; import com.google.crypto.tink.aead.subtle.AesGcmSiv; import com.google.crypto.tink.internal.KeyTypeManager; import com.google.crypto.tink.proto.AesGcmSivKey; @@ -39,11 +41,14 @@ import java.util.TreeSet; import org.conscrypt.Conscrypt; import org.junit.Before; import org.junit.Test; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.FromDataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; /** Test for AesGcmJce and its key manager. */ -@RunWith(JUnit4.class) +@RunWith(Theories.class) public class AesGcmSivKeyManagerTest { private final AesGcmSivKeyManager manager = new AesGcmSivKeyManager(); private final KeyTypeManager.KeyFactory<AesGcmSivKeyFormat, AesGcmSivKey> factory = @@ -301,12 +306,15 @@ public class AesGcmSivKeyManagerTest { testKeyTemplateCompatible(manager, AesGcmSivKeyManager.rawAes256GcmSivTemplate()); } - @Test - public void testKeyFormats() throws Exception { - factory.validateKeyFormat(factory.keyFormats().get("AES128_GCM_SIV").keyFormat); - factory.validateKeyFormat(factory.keyFormats().get("AES128_GCM_SIV_RAW").keyFormat); + @DataPoints("templateNames") + public static final String[] KEY_TEMPLATES = + new String[] {"AES128_GCM_SIV", "AES256_GCM_SIV", "AES256_GCM_SIV_RAW", "AES128_GCM_SIV_RAW"}; - factory.validateKeyFormat(factory.keyFormats().get("AES256_GCM_SIV").keyFormat); - factory.validateKeyFormat(factory.keyFormats().get("AES256_GCM_SIV_RAW").keyFormat); + @Theory + public void testTemplates(@FromDataPoints("templateNames") String templateName) throws Exception { + KeysetHandle h = KeysetHandle.generateNew(KeyTemplates.get(templateName)); + assertThat(h.size()).isEqualTo(1); + assertThat(h.getAt(0).getKey().getParameters()) + .isEqualTo(KeyTemplates.get(templateName).toParameters()); } } diff --git a/java_src/src/test/java/com/google/crypto/tink/aead/BUILD.bazel b/java_src/src/test/java/com/google/crypto/tink/aead/BUILD.bazel index e11391150..2ac2a9af0 100644 --- a/java_src/src/test/java/com/google/crypto/tink/aead/BUILD.bazel +++ b/java_src/src/test/java/com/google/crypto/tink/aead/BUILD.bazel @@ -73,6 +73,8 @@ java_test( "//proto:tink_java_proto", "//src/main/java/com/google/crypto/tink:aead", "//src/main/java/com/google/crypto/tink:key_template", + "//src/main/java/com/google/crypto/tink:key_templates", + "//src/main/java/com/google/crypto/tink:registry_cluster", "//src/main/java/com/google/crypto/tink/aead:aead_config", "//src/main/java/com/google/crypto/tink/aead:cha_cha20_poly1305_key_manager", "//src/main/java/com/google/crypto/tink/aead:cha_cha20_poly1305_parameters", @@ -123,6 +125,8 @@ java_test( "//proto:tink_java_proto", "//src/main/java/com/google/crypto/tink:aead", "//src/main/java/com/google/crypto/tink:key_template", + "//src/main/java/com/google/crypto/tink:key_templates", + "//src/main/java/com/google/crypto/tink:registry_cluster", "//src/main/java/com/google/crypto/tink/aead:aead_config", "//src/main/java/com/google/crypto/tink/aead:aes_gcm_key_manager", "//src/main/java/com/google/crypto/tink/aead:aes_gcm_parameters", @@ -150,6 +154,8 @@ java_test( "//proto:tink_java_proto", "//src/main/java/com/google/crypto/tink:aead", "//src/main/java/com/google/crypto/tink:key_template", + "//src/main/java/com/google/crypto/tink:key_templates", + "//src/main/java/com/google/crypto/tink:registry_cluster", "//src/main/java/com/google/crypto/tink/aead:aead_config", "//src/main/java/com/google/crypto/tink/aead:aes_ctr_hmac_aead_key_manager", "//src/main/java/com/google/crypto/tink/aead:aes_ctr_hmac_aead_parameters", @@ -254,6 +260,8 @@ java_test( "//proto:tink_java_proto", "//src/main/java/com/google/crypto/tink:aead", "//src/main/java/com/google/crypto/tink:key_template", + "//src/main/java/com/google/crypto/tink:key_templates", + "//src/main/java/com/google/crypto/tink:registry_cluster", "//src/main/java/com/google/crypto/tink/aead:aead_config", "//src/main/java/com/google/crypto/tink/aead:aes_eax_key_manager", "//src/main/java/com/google/crypto/tink/aead:aes_eax_parameters", @@ -296,6 +304,8 @@ java_test( "//proto:xchacha20_poly1305_java_proto", "//src/main/java/com/google/crypto/tink:aead", "//src/main/java/com/google/crypto/tink:key_template", + "//src/main/java/com/google/crypto/tink:key_templates", + "//src/main/java/com/google/crypto/tink:registry_cluster", "//src/main/java/com/google/crypto/tink/aead:aead_config", "//src/main/java/com/google/crypto/tink/aead:x_cha_cha20_poly1305_key_manager", "//src/main/java/com/google/crypto/tink/aead:x_cha_cha20_poly1305_parameters", diff --git a/java_src/src/test/java/com/google/crypto/tink/aead/ChaCha20Poly1305KeyManagerTest.java b/java_src/src/test/java/com/google/crypto/tink/aead/ChaCha20Poly1305KeyManagerTest.java index 240a75915..adc7bf258 100644 --- a/java_src/src/test/java/com/google/crypto/tink/aead/ChaCha20Poly1305KeyManagerTest.java +++ b/java_src/src/test/java/com/google/crypto/tink/aead/ChaCha20Poly1305KeyManagerTest.java @@ -24,6 +24,8 @@ import static org.junit.Assert.assertThrows; import com.google.crypto.tink.Aead; import com.google.crypto.tink.KeyTemplate; +import com.google.crypto.tink.KeyTemplates; +import com.google.crypto.tink.KeysetHandle; import com.google.crypto.tink.internal.KeyTypeManager; import com.google.crypto.tink.proto.ChaCha20Poly1305Key; import com.google.crypto.tink.proto.ChaCha20Poly1305KeyFormat; @@ -35,15 +37,16 @@ import java.security.GeneralSecurityException; import java.util.TreeSet; import org.junit.Before; import org.junit.Test; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.FromDataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; /** Test for ChaCha20Poly1305KeyManager. */ -@RunWith(JUnit4.class) +@RunWith(Theories.class) public class ChaCha20Poly1305KeyManagerTest { private final ChaCha20Poly1305KeyManager manager = new ChaCha20Poly1305KeyManager(); - private final KeyTypeManager.KeyFactory<ChaCha20Poly1305KeyFormat, ChaCha20Poly1305Key> factory = - manager.keyFactory(); @Before public void register() throws Exception { @@ -164,9 +167,15 @@ public class ChaCha20Poly1305KeyManagerTest { testKeyTemplateCompatible(manager, ChaCha20Poly1305KeyManager.rawChaCha20Poly1305Template()); } - @Test - public void testKeyFormats() throws Exception { - factory.validateKeyFormat(factory.keyFormats().get("CHACHA20_POLY1305").keyFormat); - factory.validateKeyFormat(factory.keyFormats().get("CHACHA20_POLY1305_RAW").keyFormat); + @DataPoints("templateNames") + public static final String[] KEY_TEMPLATES = + new String[] {"CHACHA20_POLY1305", "CHACHA20_POLY1305_RAW"}; + + @Theory + public void testTemplates(@FromDataPoints("templateNames") String templateName) throws Exception { + KeysetHandle h = KeysetHandle.generateNew(KeyTemplates.get(templateName)); + assertThat(h.size()).isEqualTo(1); + assertThat(h.getAt(0).getKey().getParameters()) + .isEqualTo(KeyTemplates.get(templateName).toParameters()); } } diff --git a/java_src/src/test/java/com/google/crypto/tink/aead/XChaCha20Poly1305KeyManagerTest.java b/java_src/src/test/java/com/google/crypto/tink/aead/XChaCha20Poly1305KeyManagerTest.java index 5f9c5c8be..5d65c0faf 100644 --- a/java_src/src/test/java/com/google/crypto/tink/aead/XChaCha20Poly1305KeyManagerTest.java +++ b/java_src/src/test/java/com/google/crypto/tink/aead/XChaCha20Poly1305KeyManagerTest.java @@ -22,6 +22,8 @@ import static org.junit.Assert.assertThrows; import com.google.crypto.tink.Aead; import com.google.crypto.tink.KeyTemplate; +import com.google.crypto.tink.KeyTemplates; +import com.google.crypto.tink.KeysetHandle; import com.google.crypto.tink.internal.KeyTypeManager; import com.google.crypto.tink.proto.KeyData.KeyMaterialType; import com.google.crypto.tink.proto.XChaCha20Poly1305Key; @@ -36,11 +38,14 @@ import java.util.Set; import java.util.TreeSet; import org.junit.Before; import org.junit.Test; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.FromDataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; /** Test for XChaCha20Poly1305KeyManager. */ -@RunWith(JUnit4.class) +@RunWith(Theories.class) public class XChaCha20Poly1305KeyManagerTest { private final XChaCha20Poly1305KeyManager manager = new XChaCha20Poly1305KeyManager(); private final KeyTypeManager.KeyFactory<XChaCha20Poly1305KeyFormat, XChaCha20Poly1305Key> @@ -193,9 +198,15 @@ public class XChaCha20Poly1305KeyManagerTest { testKeyTemplateCompatible(manager, XChaCha20Poly1305KeyManager.rawXChaCha20Poly1305Template()); } - @Test - public void testKeyFormats() throws Exception { - factory.validateKeyFormat(factory.keyFormats().get("XCHACHA20_POLY1305").keyFormat); - factory.validateKeyFormat(factory.keyFormats().get("XCHACHA20_POLY1305_RAW").keyFormat); + @DataPoints("templateNames") + public static final String[] KEY_TEMPLATES = + new String[] {"XCHACHA20_POLY1305", "XCHACHA20_POLY1305_RAW"}; + + @Theory + public void testTemplates(@FromDataPoints("templateNames") String templateName) throws Exception { + KeysetHandle h = KeysetHandle.generateNew(KeyTemplates.get(templateName)); + assertThat(h.size()).isEqualTo(1); + assertThat(h.getAt(0).getKey().getParameters()) + .isEqualTo(KeyTemplates.get(templateName).toParameters()); } } |