diff options
author | tholenst <tholenst@google.com> | 2023-07-10 02:22:25 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-07-10 02:24:06 -0700 |
commit | d59bd73d0265b71fef52b4badb3e55b2737f8b56 (patch) | |
tree | 1e838616721f132ae81346358660b7deacb7db18 | |
parent | 57cf066e5345296ecbc2819ff7823b77d5c4ae4a (diff) | |
download | tink-d59bd73d0265b71fef52b4badb3e55b2737f8b56.tar.gz |
Macs: 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: 546806101
3 files changed, 48 insertions, 27 deletions
diff --git a/java_src/src/test/java/com/google/crypto/tink/mac/AesCmacKeyManagerTest.java b/java_src/src/test/java/com/google/crypto/tink/mac/AesCmacKeyManagerTest.java index 8c2ca3c30..0724a3308 100644 --- a/java_src/src/test/java/com/google/crypto/tink/mac/AesCmacKeyManagerTest.java +++ b/java_src/src/test/java/com/google/crypto/tink/mac/AesCmacKeyManagerTest.java @@ -21,8 +21,9 @@ 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.Mac; -import com.google.crypto.tink.internal.KeyTypeManager; import com.google.crypto.tink.proto.AesCmacKey; import com.google.crypto.tink.proto.AesCmacKeyFormat; import com.google.crypto.tink.proto.AesCmacParams; @@ -33,15 +34,16 @@ 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 AesCmacKeyManager. */ -@RunWith(JUnit4.class) +@RunWith(Theories.class) public class AesCmacKeyManagerTest { private final AesCmacKeyManager manager = new AesCmacKeyManager(); - private final KeyTypeManager.KeyFactory<AesCmacKeyFormat, AesCmacKey> factory = - manager.keyFactory(); @Before public void register() throws Exception { @@ -235,9 +237,14 @@ public class AesCmacKeyManagerTest { testKeyTemplateCompatible(manager, AesCmacKeyManager.rawAes256CmacTemplate()); } - @Test - public void testKeyFormats() throws Exception { - factory.validateKeyFormat(factory.keyFormats().get("AES256_CMAC").keyFormat); - factory.validateKeyFormat(factory.keyFormats().get("AES256_CMAC_RAW").keyFormat); + @DataPoints("templateNames") + public static final String[] KEY_TEMPLATES = new String[] {"AES256_CMAC", "AES256_CMAC_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/mac/BUILD.bazel b/java_src/src/test/java/com/google/crypto/tink/mac/BUILD.bazel index 3ee09eaed..a6951af06 100644 --- a/java_src/src/test/java/com/google/crypto/tink/mac/BUILD.bazel +++ b/java_src/src/test/java/com/google/crypto/tink/mac/BUILD.bazel @@ -95,7 +95,9 @@ java_test( "//proto:common_java_proto", "//proto:hmac_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:mac", + "//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/mac:hmac_key_manager", "//src/main/java/com/google/crypto/tink/mac:hmac_parameters", @@ -148,7 +150,9 @@ java_test( deps = [ "//proto:aes_cmac_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:mac", + "//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/mac:aes_cmac_key_manager", "//src/main/java/com/google/crypto/tink/mac:aes_cmac_parameters", diff --git a/java_src/src/test/java/com/google/crypto/tink/mac/HmacKeyManagerTest.java b/java_src/src/test/java/com/google/crypto/tink/mac/HmacKeyManagerTest.java index dd1d82ecd..85db5cf76 100644 --- a/java_src/src/test/java/com/google/crypto/tink/mac/HmacKeyManagerTest.java +++ b/java_src/src/test/java/com/google/crypto/tink/mac/HmacKeyManagerTest.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.Mac; import com.google.crypto.tink.internal.KeyTypeManager; import com.google.crypto.tink.proto.HashType; @@ -40,11 +42,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 HmacKeyManager}. */ -@RunWith(JUnit4.class) +@RunWith(Theories.class) public class HmacKeyManagerTest { private final HmacKeyManager manager = new HmacKeyManager(); private final KeyTypeManager.KeyFactory<HmacKeyFormat, HmacKey> factory = manager.keyFactory(); @@ -400,21 +405,26 @@ public class HmacKeyManagerTest { testKeyTemplateCompatible(manager, HmacKeyManager.hmacSha512HalfDigestTemplate()); } - @Test - public void testKeyFormats() throws Exception { - factory.validateKeyFormat(factory.keyFormats().get("HMAC_SHA256_128BITTAG").keyFormat); - factory.validateKeyFormat(factory.keyFormats().get("HMAC_SHA256_128BITTAG_RAW").keyFormat); - - factory.validateKeyFormat(factory.keyFormats().get("HMAC_SHA256_256BITTAG").keyFormat); - factory.validateKeyFormat(factory.keyFormats().get("HMAC_SHA256_256BITTAG_RAW").keyFormat); - - factory.validateKeyFormat(factory.keyFormats().get("HMAC_SHA512_128BITTAG").keyFormat); - factory.validateKeyFormat(factory.keyFormats().get("HMAC_SHA512_128BITTAG_RAW").keyFormat); - - factory.validateKeyFormat(factory.keyFormats().get("HMAC_SHA512_256BITTAG").keyFormat); - factory.validateKeyFormat(factory.keyFormats().get("HMAC_SHA512_256BITTAG_RAW").keyFormat); - - factory.validateKeyFormat(factory.keyFormats().get("HMAC_SHA512_512BITTAG").keyFormat); - factory.validateKeyFormat(factory.keyFormats().get("HMAC_SHA512_512BITTAG_RAW").keyFormat); + @DataPoints("templateNames") + public static final String[] KEY_TEMPLATES = + new String[] { + "HMAC_SHA256_128BITTAG", + "HMAC_SHA256_128BITTAG_RAW", + "HMAC_SHA256_256BITTAG", + "HMAC_SHA256_256BITTAG_RAW", + "HMAC_SHA512_128BITTAG", + "HMAC_SHA512_128BITTAG_RAW", + "HMAC_SHA512_256BITTAG", + "HMAC_SHA512_256BITTAG_RAW", + "HMAC_SHA512_512BITTAG", + "HMAC_SHA512_512BITTAG_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()); } } |