Skip to content

Commit 7cc6582

Browse files
tholenstcopybara-github
authored andcommitted
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 Change-Id: I3192b6570f7dc56033e4f0076934199f53364ccc
1 parent 6bc7521 commit 7cc6582

File tree

3 files changed

+48
-27
lines changed

3 files changed

+48
-27
lines changed

src/test/java/com/google/crypto/tink/mac/AesCmacKeyManagerTest.java

+16-9
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
import static org.junit.Assert.assertThrows;
2222

2323
import com.google.crypto.tink.KeyTemplate;
24+
import com.google.crypto.tink.KeyTemplates;
25+
import com.google.crypto.tink.KeysetHandle;
2426
import com.google.crypto.tink.Mac;
25-
import com.google.crypto.tink.internal.KeyTypeManager;
2627
import com.google.crypto.tink.proto.AesCmacKey;
2728
import com.google.crypto.tink.proto.AesCmacKeyFormat;
2829
import com.google.crypto.tink.proto.AesCmacParams;
@@ -33,15 +34,16 @@
3334
import java.security.GeneralSecurityException;
3435
import org.junit.Before;
3536
import org.junit.Test;
37+
import org.junit.experimental.theories.DataPoints;
38+
import org.junit.experimental.theories.FromDataPoints;
39+
import org.junit.experimental.theories.Theories;
40+
import org.junit.experimental.theories.Theory;
3641
import org.junit.runner.RunWith;
37-
import org.junit.runners.JUnit4;
3842

3943
/** Test for AesCmacKeyManager. */
40-
@RunWith(JUnit4.class)
44+
@RunWith(Theories.class)
4145
public class AesCmacKeyManagerTest {
4246
private final AesCmacKeyManager manager = new AesCmacKeyManager();
43-
private final KeyTypeManager.KeyFactory<AesCmacKeyFormat, AesCmacKey> factory =
44-
manager.keyFactory();
4547

4648
@Before
4749
public void register() throws Exception {
@@ -235,9 +237,14 @@ public void testKeyTemplateAndManagerCompatibility() throws Exception {
235237
testKeyTemplateCompatible(manager, AesCmacKeyManager.rawAes256CmacTemplate());
236238
}
237239

238-
@Test
239-
public void testKeyFormats() throws Exception {
240-
factory.validateKeyFormat(factory.keyFormats().get("AES256_CMAC").keyFormat);
241-
factory.validateKeyFormat(factory.keyFormats().get("AES256_CMAC_RAW").keyFormat);
240+
@DataPoints("templateNames")
241+
public static final String[] KEY_TEMPLATES = new String[] {"AES256_CMAC", "AES256_CMAC_RAW"};
242+
243+
@Theory
244+
public void testTemplates(@FromDataPoints("templateNames") String templateName) throws Exception {
245+
KeysetHandle h = KeysetHandle.generateNew(KeyTemplates.get(templateName));
246+
assertThat(h.size()).isEqualTo(1);
247+
assertThat(h.getAt(0).getKey().getParameters())
248+
.isEqualTo(KeyTemplates.get(templateName).toParameters());
242249
}
243250
}

src/test/java/com/google/crypto/tink/mac/BUILD.bazel

+4
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ java_test(
9595
"//proto:common_java_proto",
9696
"//proto:hmac_java_proto",
9797
"//src/main/java/com/google/crypto/tink:key_template",
98+
"//src/main/java/com/google/crypto/tink:key_templates",
9899
"//src/main/java/com/google/crypto/tink:mac",
100+
"//src/main/java/com/google/crypto/tink:registry_cluster",
99101
"//src/main/java/com/google/crypto/tink/internal:key_type_manager",
100102
"//src/main/java/com/google/crypto/tink/mac:hmac_key_manager",
101103
"//src/main/java/com/google/crypto/tink/mac:hmac_parameters",
@@ -148,7 +150,9 @@ java_test(
148150
deps = [
149151
"//proto:aes_cmac_java_proto",
150152
"//src/main/java/com/google/crypto/tink:key_template",
153+
"//src/main/java/com/google/crypto/tink:key_templates",
151154
"//src/main/java/com/google/crypto/tink:mac",
155+
"//src/main/java/com/google/crypto/tink:registry_cluster",
152156
"//src/main/java/com/google/crypto/tink/internal:key_type_manager",
153157
"//src/main/java/com/google/crypto/tink/mac:aes_cmac_key_manager",
154158
"//src/main/java/com/google/crypto/tink/mac:aes_cmac_parameters",

src/test/java/com/google/crypto/tink/mac/HmacKeyManagerTest.java

+28-18
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import static org.junit.Assert.assertThrows;
2222

2323
import com.google.crypto.tink.KeyTemplate;
24+
import com.google.crypto.tink.KeyTemplates;
25+
import com.google.crypto.tink.KeysetHandle;
2426
import com.google.crypto.tink.Mac;
2527
import com.google.crypto.tink.internal.KeyTypeManager;
2628
import com.google.crypto.tink.proto.HashType;
@@ -40,11 +42,14 @@
4042
import javax.crypto.spec.SecretKeySpec;
4143
import org.junit.Before;
4244
import org.junit.Test;
45+
import org.junit.experimental.theories.DataPoints;
46+
import org.junit.experimental.theories.FromDataPoints;
47+
import org.junit.experimental.theories.Theories;
48+
import org.junit.experimental.theories.Theory;
4349
import org.junit.runner.RunWith;
44-
import org.junit.runners.JUnit4;
4550

4651
/** Unit tests for {@link HmacKeyManager}. */
47-
@RunWith(JUnit4.class)
52+
@RunWith(Theories.class)
4853
public class HmacKeyManagerTest {
4954
private final HmacKeyManager manager = new HmacKeyManager();
5055
private final KeyTypeManager.KeyFactory<HmacKeyFormat, HmacKey> factory = manager.keyFactory();
@@ -400,21 +405,26 @@ public void testKeyTemplateAndManagerCompatibility() throws Exception {
400405
testKeyTemplateCompatible(manager, HmacKeyManager.hmacSha512HalfDigestTemplate());
401406
}
402407

403-
@Test
404-
public void testKeyFormats() throws Exception {
405-
factory.validateKeyFormat(factory.keyFormats().get("HMAC_SHA256_128BITTAG").keyFormat);
406-
factory.validateKeyFormat(factory.keyFormats().get("HMAC_SHA256_128BITTAG_RAW").keyFormat);
407-
408-
factory.validateKeyFormat(factory.keyFormats().get("HMAC_SHA256_256BITTAG").keyFormat);
409-
factory.validateKeyFormat(factory.keyFormats().get("HMAC_SHA256_256BITTAG_RAW").keyFormat);
410-
411-
factory.validateKeyFormat(factory.keyFormats().get("HMAC_SHA512_128BITTAG").keyFormat);
412-
factory.validateKeyFormat(factory.keyFormats().get("HMAC_SHA512_128BITTAG_RAW").keyFormat);
413-
414-
factory.validateKeyFormat(factory.keyFormats().get("HMAC_SHA512_256BITTAG").keyFormat);
415-
factory.validateKeyFormat(factory.keyFormats().get("HMAC_SHA512_256BITTAG_RAW").keyFormat);
416-
417-
factory.validateKeyFormat(factory.keyFormats().get("HMAC_SHA512_512BITTAG").keyFormat);
418-
factory.validateKeyFormat(factory.keyFormats().get("HMAC_SHA512_512BITTAG_RAW").keyFormat);
408+
@DataPoints("templateNames")
409+
public static final String[] KEY_TEMPLATES =
410+
new String[] {
411+
"HMAC_SHA256_128BITTAG",
412+
"HMAC_SHA256_128BITTAG_RAW",
413+
"HMAC_SHA256_256BITTAG",
414+
"HMAC_SHA256_256BITTAG_RAW",
415+
"HMAC_SHA512_128BITTAG",
416+
"HMAC_SHA512_128BITTAG_RAW",
417+
"HMAC_SHA512_256BITTAG",
418+
"HMAC_SHA512_256BITTAG_RAW",
419+
"HMAC_SHA512_512BITTAG",
420+
"HMAC_SHA512_512BITTAG_RAW"
421+
};
422+
423+
@Theory
424+
public void testTemplates(@FromDataPoints("templateNames") String templateName) throws Exception {
425+
KeysetHandle h = KeysetHandle.generateNew(KeyTemplates.get(templateName));
426+
assertThat(h.size()).isEqualTo(1);
427+
assertThat(h.getAt(0).getKey().getParameters())
428+
.isEqualTo(KeyTemplates.get(templateName).toParameters());
419429
}
420430
}

0 commit comments

Comments
 (0)