diff options
author | tholenst <tholenst@google.com> | 2023-07-10 06:35:31 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-07-10 06:36:24 -0700 |
commit | 0bc4e25cd6e21862629247f3c632d4874d536573 (patch) | |
tree | 969c94ce3756d04e649518bf8f87f99cabfcf555 /java_src/src/test/java/com/google/crypto/tink | |
parent | b53d160793b07c603f0abffee363f25184a9627f (diff) | |
download | tink-0bc4e25cd6e21862629247f3c632d4874d536573.tar.gz |
Prfs & StreamingAeads: 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: 546854974
Diffstat (limited to 'java_src/src/test/java/com/google/crypto/tink')
7 files changed, 106 insertions, 40 deletions
diff --git a/java_src/src/test/java/com/google/crypto/tink/prf/AesCmacPrfKeyManagerTest.java b/java_src/src/test/java/com/google/crypto/tink/prf/AesCmacPrfKeyManagerTest.java index 313708426..f7331e235 100644 --- a/java_src/src/test/java/com/google/crypto/tink/prf/AesCmacPrfKeyManagerTest.java +++ b/java_src/src/test/java/com/google/crypto/tink/prf/AesCmacPrfKeyManagerTest.java @@ -21,7 +21,8 @@ import static com.google.crypto.tink.testing.KeyTypeManagerTestUtil.testKeyTempl import static org.junit.Assert.assertThrows; import com.google.crypto.tink.KeyTemplate; -import com.google.crypto.tink.internal.KeyTypeManager; +import com.google.crypto.tink.KeyTemplates; +import com.google.crypto.tink.KeysetHandle; import com.google.crypto.tink.proto.AesCmacPrfKey; import com.google.crypto.tink.proto.AesCmacPrfKeyFormat; import com.google.crypto.tink.subtle.PrfAesCmac; @@ -30,15 +31,15 @@ import com.google.protobuf.ByteString; import java.security.GeneralSecurityException; 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 AesCmacPrfKeyManager. */ -@RunWith(JUnit4.class) +@RunWith(Theories.class) public class AesCmacPrfKeyManagerTest { - private final AesCmacPrfKeyManager manager = new AesCmacPrfKeyManager(); - private final KeyTypeManager.KeyFactory<AesCmacPrfKeyFormat, AesCmacPrfKey> factory = - manager.keyFactory(); @Before public void register() throws Exception { @@ -155,9 +156,17 @@ public class AesCmacPrfKeyManagerTest { testKeyTemplateCompatible(manager, AesCmacPrfKeyManager.aes256CmacTemplate()); } - @Test - public void testKeyFormats() throws Exception { - factory.validateKeyFormat(factory.keyFormats().get("AES256_CMAC_PRF").keyFormat); - factory.validateKeyFormat(factory.keyFormats().get("AES_CMAC_PRF").keyFormat); + @DataPoints("templateNames") + public static final String[] KEY_TEMPLATES = + new String[] { + "AES256_CMAC_PRF", "AES_CMAC_PRF", + }; + + @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/prf/BUILD.bazel b/java_src/src/test/java/com/google/crypto/tink/prf/BUILD.bazel index 00fd27b11..9fd3afc37 100644 --- a/java_src/src/test/java/com/google/crypto/tink/prf/BUILD.bazel +++ b/java_src/src/test/java/com/google/crypto/tink/prf/BUILD.bazel @@ -54,7 +54,8 @@ java_test( deps = [ "//proto:aes_cmac_prf_java_proto", "//src/main/java/com/google/crypto/tink:key_template", - "//src/main/java/com/google/crypto/tink/internal:key_type_manager", + "//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/prf:aes_cmac_prf_key_manager", "//src/main/java/com/google/crypto/tink/prf:aes_cmac_prf_parameters", "//src/main/java/com/google/crypto/tink/prf:prf_config", @@ -76,6 +77,8 @@ java_test( "//proto:common_java_proto", "//proto:hmac_prf_java_proto", "//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/internal:key_type_manager", "//src/main/java/com/google/crypto/tink/prf:hmac_prf_key_manager", "//src/main/java/com/google/crypto/tink/prf:hmac_prf_parameters", @@ -100,6 +103,8 @@ java_test( "//proto:hkdf_prf_java_proto", "//proto:tink_java_proto", "//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/internal:key_type_manager", "//src/main/java/com/google/crypto/tink/prf:hkdf_prf_key_manager", "//src/main/java/com/google/crypto/tink/prf:hkdf_prf_parameters", diff --git a/java_src/src/test/java/com/google/crypto/tink/prf/HkdfPrfKeyManagerTest.java b/java_src/src/test/java/com/google/crypto/tink/prf/HkdfPrfKeyManagerTest.java index 60f1f7cd8..0033f37de 100644 --- a/java_src/src/test/java/com/google/crypto/tink/prf/HkdfPrfKeyManagerTest.java +++ b/java_src/src/test/java/com/google/crypto/tink/prf/HkdfPrfKeyManagerTest.java @@ -22,6 +22,8 @@ import static java.nio.charset.StandardCharsets.UTF_8; import static org.junit.Assert.assertThrows; 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.HashType; import com.google.crypto.tink.proto.HkdfPrfKey; @@ -38,11 +40,14 @@ import java.io.InputStream; import java.security.GeneralSecurityException; 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 HkdfPrfKeyManager. */ -@RunWith(JUnit4.class) +@RunWith(Theories.class) public class HkdfPrfKeyManagerTest { private final HkdfPrfKeyManager manager = new HkdfPrfKeyManager(); private final KeyTypeManager.KeyFactory<HkdfPrfKeyFormat, HkdfPrfKey> factory = @@ -284,8 +289,17 @@ public class HkdfPrfKeyManagerTest { testKeyTemplateCompatible(manager, HkdfPrfKeyManager.hkdfSha256Template()); } - @Test - public void testKeyFormats() throws Exception { - factory.validateKeyFormat(factory.keyFormats().get("HKDF_SHA256").keyFormat); + @DataPoints("templateNames") + public static final String[] KEY_TEMPLATES = + new String[] { + "HKDF_SHA256", + }; + + @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/prf/HmacPrfKeyManagerTest.java b/java_src/src/test/java/com/google/crypto/tink/prf/HmacPrfKeyManagerTest.java index 88ca4370f..3a27fd629 100644 --- a/java_src/src/test/java/com/google/crypto/tink/prf/HmacPrfKeyManagerTest.java +++ b/java_src/src/test/java/com/google/crypto/tink/prf/HmacPrfKeyManagerTest.java @@ -21,6 +21,8 @@ import static com.google.crypto.tink.testing.KeyTypeManagerTestUtil.testKeyTempl import static org.junit.Assert.assertThrows; 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.HashType; import com.google.crypto.tink.proto.HmacPrfKey; @@ -38,11 +40,14 @@ import java.util.TreeSet; import javax.crypto.spec.SecretKeySpec; 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; /** Unit tests for {@link HmacPrfKeyManager}. */ -@RunWith(JUnit4.class) +@RunWith(Theories.class) public class HmacPrfKeyManagerTest { private final HmacPrfKeyManager manager = new HmacPrfKeyManager(); private final KeyTypeManager.KeyFactory<HmacPrfKeyFormat, HmacPrfKey> factory = @@ -295,9 +300,14 @@ public class HmacPrfKeyManagerTest { testKeyTemplateCompatible(manager, HmacPrfKeyManager.hmacSha512Template()); } - @Test - public void testKeyFormats() throws Exception { - factory.validateKeyFormat(factory.keyFormats().get("HMAC_SHA256_PRF").keyFormat); - factory.validateKeyFormat(factory.keyFormats().get("HMAC_SHA512_PRF").keyFormat); + @DataPoints("templateNames") + public static final String[] KEY_TEMPLATES = new String[] {"HMAC_SHA256_PRF", "HMAC_SHA512_PRF"}; + + @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/streamingaead/AesCtrHmacStreamingKeyManagerTest.java b/java_src/src/test/java/com/google/crypto/tink/streamingaead/AesCtrHmacStreamingKeyManagerTest.java index c095416fb..0d6fabc40 100644 --- a/java_src/src/test/java/com/google/crypto/tink/streamingaead/AesCtrHmacStreamingKeyManagerTest.java +++ b/java_src/src/test/java/com/google/crypto/tink/streamingaead/AesCtrHmacStreamingKeyManagerTest.java @@ -20,6 +20,8 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertThrows; import com.google.crypto.tink.KeyTemplate; +import com.google.crypto.tink.KeyTemplates; +import com.google.crypto.tink.KeysetHandle; import com.google.crypto.tink.StreamingAead; import com.google.crypto.tink.internal.KeyTypeManager; import com.google.crypto.tink.internal.Util; @@ -37,11 +39,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 AesCtrHmacStreamingKeyManager. */ -@RunWith(JUnit4.class) +@RunWith(Theories.class) public class AesCtrHmacStreamingKeyManagerTest { private final AesCtrHmacStreamingKeyManager manager = new AesCtrHmacStreamingKeyManager(); private final KeyTypeManager.KeyFactory<AesCtrHmacStreamingKeyFormat, AesCtrHmacStreamingKey> @@ -303,12 +308,20 @@ public class AesCtrHmacStreamingKeyManagerTest { .build()); } - @Test - public void testKeyFormats() throws Exception { - factory.validateKeyFormat(factory.keyFormats().get("AES128_CTR_HMAC_SHA256_4KB").keyFormat); - factory.validateKeyFormat(factory.keyFormats().get("AES128_CTR_HMAC_SHA256_1MB").keyFormat); - - factory.validateKeyFormat(factory.keyFormats().get("AES256_CTR_HMAC_SHA256_4KB").keyFormat); - factory.validateKeyFormat(factory.keyFormats().get("AES256_CTR_HMAC_SHA256_1MB").keyFormat); + @DataPoints("templateNames") + public static final String[] KEY_TEMPLATES = + new String[] { + "AES128_CTR_HMAC_SHA256_4KB", + "AES128_CTR_HMAC_SHA256_1MB", + "AES256_CTR_HMAC_SHA256_4KB", + "AES256_CTR_HMAC_SHA256_1MB" + }; + + @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/streamingaead/AesGcmHkdfStreamingKeyManagerTest.java b/java_src/src/test/java/com/google/crypto/tink/streamingaead/AesGcmHkdfStreamingKeyManagerTest.java index a758d3c99..25378e0ea 100644 --- a/java_src/src/test/java/com/google/crypto/tink/streamingaead/AesGcmHkdfStreamingKeyManagerTest.java +++ b/java_src/src/test/java/com/google/crypto/tink/streamingaead/AesGcmHkdfStreamingKeyManagerTest.java @@ -20,6 +20,8 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertThrows; import com.google.crypto.tink.KeyTemplate; +import com.google.crypto.tink.KeyTemplates; +import com.google.crypto.tink.KeysetHandle; import com.google.crypto.tink.StreamingAead; import com.google.crypto.tink.internal.KeyTypeManager; import com.google.crypto.tink.proto.AesGcmHkdfStreamingKey; @@ -37,11 +39,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 AesGcmHkdfStreamingKeyManager. */ -@RunWith(JUnit4.class) +@RunWith(Theories.class) public class AesGcmHkdfStreamingKeyManagerTest { private final AesGcmHkdfStreamingKeyManager manager = new AesGcmHkdfStreamingKeyManager(); private final KeyTypeManager.KeyFactory<AesGcmHkdfStreamingKeyFormat, AesGcmHkdfStreamingKey> @@ -296,12 +301,18 @@ public class AesGcmHkdfStreamingKeyManagerTest { .build()); } - @Test - public void testKeyFormats() throws Exception { - factory.validateKeyFormat(factory.keyFormats().get("AES128_GCM_HKDF_4KB").keyFormat); - factory.validateKeyFormat(factory.keyFormats().get("AES128_GCM_HKDF_1MB").keyFormat); - - factory.validateKeyFormat(factory.keyFormats().get("AES256_GCM_HKDF_4KB").keyFormat); - factory.validateKeyFormat(factory.keyFormats().get("AES256_GCM_HKDF_1MB").keyFormat); + @DataPoints("templateNames") + public static final String[] KEY_TEMPLATES = + new String[] { + "AES128_GCM_HKDF_4KB", "AES128_GCM_HKDF_1MB", "AES256_GCM_HKDF_4KB", "AES256_GCM_HKDF_1MB", + }; + + @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/streamingaead/BUILD.bazel b/java_src/src/test/java/com/google/crypto/tink/streamingaead/BUILD.bazel index fffa5e2fa..4af6735b7 100644 --- a/java_src/src/test/java/com/google/crypto/tink/streamingaead/BUILD.bazel +++ b/java_src/src/test/java/com/google/crypto/tink/streamingaead/BUILD.bazel @@ -97,6 +97,8 @@ java_test( "//proto:common_java_proto", "//proto:tink_java_proto", "//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:streaming_aead", "//src/main/java/com/google/crypto/tink/internal:key_type_manager", "//src/main/java/com/google/crypto/tink/streamingaead:aes_gcm_hkdf_streaming_key_manager", @@ -120,6 +122,8 @@ java_test( "//proto:hmac_java_proto", "//proto:tink_java_proto", "//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:streaming_aead", "//src/main/java/com/google/crypto/tink/internal:key_type_manager", "//src/main/java/com/google/crypto/tink/internal:util", |