Skip to content

Commit 837c1a2

Browse files
tholenstcopybara-github
authored andcommitted
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 Change-Id: I064887dc5c8d667c9d3d0674fe5393c265d665ed
1 parent a6efd5e commit 837c1a2

7 files changed

+106
-40
lines changed

src/test/java/com/google/crypto/tink/prf/AesCmacPrfKeyManagerTest.java

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

2323
import com.google.crypto.tink.KeyTemplate;
24-
import com.google.crypto.tink.internal.KeyTypeManager;
24+
import com.google.crypto.tink.KeyTemplates;
25+
import com.google.crypto.tink.KeysetHandle;
2526
import com.google.crypto.tink.proto.AesCmacPrfKey;
2627
import com.google.crypto.tink.proto.AesCmacPrfKeyFormat;
2728
import com.google.crypto.tink.subtle.PrfAesCmac;
@@ -30,15 +31,15 @@
3031
import java.security.GeneralSecurityException;
3132
import org.junit.Before;
3233
import org.junit.Test;
34+
import org.junit.experimental.theories.DataPoints;
35+
import org.junit.experimental.theories.FromDataPoints;
36+
import org.junit.experimental.theories.Theories;
37+
import org.junit.experimental.theories.Theory;
3338
import org.junit.runner.RunWith;
34-
import org.junit.runners.JUnit4;
3539

3640
/** Test for AesCmacPrfKeyManager. */
37-
@RunWith(JUnit4.class)
41+
@RunWith(Theories.class)
3842
public class AesCmacPrfKeyManagerTest {
39-
private final AesCmacPrfKeyManager manager = new AesCmacPrfKeyManager();
40-
private final KeyTypeManager.KeyFactory<AesCmacPrfKeyFormat, AesCmacPrfKey> factory =
41-
manager.keyFactory();
4243

4344
@Before
4445
public void register() throws Exception {
@@ -155,9 +156,17 @@ public void testKeyTemplateAndManagerCompatibility() throws Exception {
155156
testKeyTemplateCompatible(manager, AesCmacPrfKeyManager.aes256CmacTemplate());
156157
}
157158

158-
@Test
159-
public void testKeyFormats() throws Exception {
160-
factory.validateKeyFormat(factory.keyFormats().get("AES256_CMAC_PRF").keyFormat);
161-
factory.validateKeyFormat(factory.keyFormats().get("AES_CMAC_PRF").keyFormat);
159+
@DataPoints("templateNames")
160+
public static final String[] KEY_TEMPLATES =
161+
new String[] {
162+
"AES256_CMAC_PRF", "AES_CMAC_PRF",
163+
};
164+
165+
@Theory
166+
public void testTemplates(@FromDataPoints("templateNames") String templateName) throws Exception {
167+
KeysetHandle h = KeysetHandle.generateNew(KeyTemplates.get(templateName));
168+
assertThat(h.size()).isEqualTo(1);
169+
assertThat(h.getAt(0).getKey().getParameters())
170+
.isEqualTo(KeyTemplates.get(templateName).toParameters());
162171
}
163172
}

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ java_test(
5454
deps = [
5555
"//proto:aes_cmac_prf_java_proto",
5656
"//src/main/java/com/google/crypto/tink:key_template",
57-
"//src/main/java/com/google/crypto/tink/internal:key_type_manager",
57+
"//src/main/java/com/google/crypto/tink:key_templates",
58+
"//src/main/java/com/google/crypto/tink:registry_cluster",
5859
"//src/main/java/com/google/crypto/tink/prf:aes_cmac_prf_key_manager",
5960
"//src/main/java/com/google/crypto/tink/prf:aes_cmac_prf_parameters",
6061
"//src/main/java/com/google/crypto/tink/prf:prf_config",
@@ -76,6 +77,8 @@ java_test(
7677
"//proto:common_java_proto",
7778
"//proto:hmac_prf_java_proto",
7879
"//src/main/java/com/google/crypto/tink:key_template",
80+
"//src/main/java/com/google/crypto/tink:key_templates",
81+
"//src/main/java/com/google/crypto/tink:registry_cluster",
7982
"//src/main/java/com/google/crypto/tink/internal:key_type_manager",
8083
"//src/main/java/com/google/crypto/tink/prf:hmac_prf_key_manager",
8184
"//src/main/java/com/google/crypto/tink/prf:hmac_prf_parameters",
@@ -100,6 +103,8 @@ java_test(
100103
"//proto:hkdf_prf_java_proto",
101104
"//proto:tink_java_proto",
102105
"//src/main/java/com/google/crypto/tink:key_template",
106+
"//src/main/java/com/google/crypto/tink:key_templates",
107+
"//src/main/java/com/google/crypto/tink:registry_cluster",
103108
"//src/main/java/com/google/crypto/tink/internal:key_type_manager",
104109
"//src/main/java/com/google/crypto/tink/prf:hkdf_prf_key_manager",
105110
"//src/main/java/com/google/crypto/tink/prf:hkdf_prf_parameters",

src/test/java/com/google/crypto/tink/prf/HkdfPrfKeyManagerTest.java

+19-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import static org.junit.Assert.assertThrows;
2323

2424
import com.google.crypto.tink.KeyTemplate;
25+
import com.google.crypto.tink.KeyTemplates;
26+
import com.google.crypto.tink.KeysetHandle;
2527
import com.google.crypto.tink.internal.KeyTypeManager;
2628
import com.google.crypto.tink.proto.HashType;
2729
import com.google.crypto.tink.proto.HkdfPrfKey;
@@ -38,11 +40,14 @@
3840
import java.security.GeneralSecurityException;
3941
import org.junit.Before;
4042
import org.junit.Test;
43+
import org.junit.experimental.theories.DataPoints;
44+
import org.junit.experimental.theories.FromDataPoints;
45+
import org.junit.experimental.theories.Theories;
46+
import org.junit.experimental.theories.Theory;
4147
import org.junit.runner.RunWith;
42-
import org.junit.runners.JUnit4;
4348

4449
/** Tests for HkdfPrfKeyManager. */
45-
@RunWith(JUnit4.class)
50+
@RunWith(Theories.class)
4651
public class HkdfPrfKeyManagerTest {
4752
private final HkdfPrfKeyManager manager = new HkdfPrfKeyManager();
4853
private final KeyTypeManager.KeyFactory<HkdfPrfKeyFormat, HkdfPrfKey> factory =
@@ -284,8 +289,17 @@ public void testKeyTemplateAndManagerCompatibility() throws Exception {
284289
testKeyTemplateCompatible(manager, HkdfPrfKeyManager.hkdfSha256Template());
285290
}
286291

287-
@Test
288-
public void testKeyFormats() throws Exception {
289-
factory.validateKeyFormat(factory.keyFormats().get("HKDF_SHA256").keyFormat);
292+
@DataPoints("templateNames")
293+
public static final String[] KEY_TEMPLATES =
294+
new String[] {
295+
"HKDF_SHA256",
296+
};
297+
298+
@Theory
299+
public void testTemplates(@FromDataPoints("templateNames") String templateName) throws Exception {
300+
KeysetHandle h = KeysetHandle.generateNew(KeyTemplates.get(templateName));
301+
assertThat(h.size()).isEqualTo(1);
302+
assertThat(h.getAt(0).getKey().getParameters())
303+
.isEqualTo(KeyTemplates.get(templateName).toParameters());
290304
}
291305
}

src/test/java/com/google/crypto/tink/prf/HmacPrfKeyManagerTest.java

+16-6
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.internal.KeyTypeManager;
2527
import com.google.crypto.tink.proto.HashType;
2628
import com.google.crypto.tink.proto.HmacPrfKey;
@@ -38,11 +40,14 @@
3840
import javax.crypto.spec.SecretKeySpec;
3941
import org.junit.Before;
4042
import org.junit.Test;
43+
import org.junit.experimental.theories.DataPoints;
44+
import org.junit.experimental.theories.FromDataPoints;
45+
import org.junit.experimental.theories.Theories;
46+
import org.junit.experimental.theories.Theory;
4147
import org.junit.runner.RunWith;
42-
import org.junit.runners.JUnit4;
4348

4449
/** Unit tests for {@link HmacPrfKeyManager}. */
45-
@RunWith(JUnit4.class)
50+
@RunWith(Theories.class)
4651
public class HmacPrfKeyManagerTest {
4752
private final HmacPrfKeyManager manager = new HmacPrfKeyManager();
4853
private final KeyTypeManager.KeyFactory<HmacPrfKeyFormat, HmacPrfKey> factory =
@@ -295,9 +300,14 @@ public void testKeyTemplateAndManagerCompatibility() throws Exception {
295300
testKeyTemplateCompatible(manager, HmacPrfKeyManager.hmacSha512Template());
296301
}
297302

298-
@Test
299-
public void testKeyFormats() throws Exception {
300-
factory.validateKeyFormat(factory.keyFormats().get("HMAC_SHA256_PRF").keyFormat);
301-
factory.validateKeyFormat(factory.keyFormats().get("HMAC_SHA512_PRF").keyFormat);
303+
@DataPoints("templateNames")
304+
public static final String[] KEY_TEMPLATES = new String[] {"HMAC_SHA256_PRF", "HMAC_SHA512_PRF"};
305+
306+
@Theory
307+
public void testTemplates(@FromDataPoints("templateNames") String templateName) throws Exception {
308+
KeysetHandle h = KeysetHandle.generateNew(KeyTemplates.get(templateName));
309+
assertThat(h.size()).isEqualTo(1);
310+
assertThat(h.getAt(0).getKey().getParameters())
311+
.isEqualTo(KeyTemplates.get(templateName).toParameters());
302312
}
303313
}

src/test/java/com/google/crypto/tink/streamingaead/AesCtrHmacStreamingKeyManagerTest.java

+22-9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import static org.junit.Assert.assertThrows;
2121

2222
import com.google.crypto.tink.KeyTemplate;
23+
import com.google.crypto.tink.KeyTemplates;
24+
import com.google.crypto.tink.KeysetHandle;
2325
import com.google.crypto.tink.StreamingAead;
2426
import com.google.crypto.tink.internal.KeyTypeManager;
2527
import com.google.crypto.tink.internal.Util;
@@ -37,11 +39,14 @@
3739
import java.util.TreeSet;
3840
import org.junit.Before;
3941
import org.junit.Test;
42+
import org.junit.experimental.theories.DataPoints;
43+
import org.junit.experimental.theories.FromDataPoints;
44+
import org.junit.experimental.theories.Theories;
45+
import org.junit.experimental.theories.Theory;
4046
import org.junit.runner.RunWith;
41-
import org.junit.runners.JUnit4;
4247

4348
/** Test for AesCtrHmacStreamingKeyManager. */
44-
@RunWith(JUnit4.class)
49+
@RunWith(Theories.class)
4550
public class AesCtrHmacStreamingKeyManagerTest {
4651
private final AesCtrHmacStreamingKeyManager manager = new AesCtrHmacStreamingKeyManager();
4752
private final KeyTypeManager.KeyFactory<AesCtrHmacStreamingKeyFormat, AesCtrHmacStreamingKey>
@@ -303,12 +308,20 @@ public void testAes256CtrHmacSha2561MBTemplate() throws Exception {
303308
.build());
304309
}
305310

306-
@Test
307-
public void testKeyFormats() throws Exception {
308-
factory.validateKeyFormat(factory.keyFormats().get("AES128_CTR_HMAC_SHA256_4KB").keyFormat);
309-
factory.validateKeyFormat(factory.keyFormats().get("AES128_CTR_HMAC_SHA256_1MB").keyFormat);
310-
311-
factory.validateKeyFormat(factory.keyFormats().get("AES256_CTR_HMAC_SHA256_4KB").keyFormat);
312-
factory.validateKeyFormat(factory.keyFormats().get("AES256_CTR_HMAC_SHA256_1MB").keyFormat);
311+
@DataPoints("templateNames")
312+
public static final String[] KEY_TEMPLATES =
313+
new String[] {
314+
"AES128_CTR_HMAC_SHA256_4KB",
315+
"AES128_CTR_HMAC_SHA256_1MB",
316+
"AES256_CTR_HMAC_SHA256_4KB",
317+
"AES256_CTR_HMAC_SHA256_1MB"
318+
};
319+
320+
@Theory
321+
public void testTemplates(@FromDataPoints("templateNames") String templateName) throws Exception {
322+
KeysetHandle h = KeysetHandle.generateNew(KeyTemplates.get(templateName));
323+
assertThat(h.size()).isEqualTo(1);
324+
assertThat(h.getAt(0).getKey().getParameters())
325+
.isEqualTo(KeyTemplates.get(templateName).toParameters());
313326
}
314327
}

src/test/java/com/google/crypto/tink/streamingaead/AesGcmHkdfStreamingKeyManagerTest.java

+20-9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import static org.junit.Assert.assertThrows;
2121

2222
import com.google.crypto.tink.KeyTemplate;
23+
import com.google.crypto.tink.KeyTemplates;
24+
import com.google.crypto.tink.KeysetHandle;
2325
import com.google.crypto.tink.StreamingAead;
2426
import com.google.crypto.tink.internal.KeyTypeManager;
2527
import com.google.crypto.tink.proto.AesGcmHkdfStreamingKey;
@@ -37,11 +39,14 @@
3739
import java.util.TreeSet;
3840
import org.junit.Before;
3941
import org.junit.Test;
42+
import org.junit.experimental.theories.DataPoints;
43+
import org.junit.experimental.theories.FromDataPoints;
44+
import org.junit.experimental.theories.Theories;
45+
import org.junit.experimental.theories.Theory;
4046
import org.junit.runner.RunWith;
41-
import org.junit.runners.JUnit4;
4247

4348
/** Test for AesGcmHkdfStreamingKeyManager. */
44-
@RunWith(JUnit4.class)
49+
@RunWith(Theories.class)
4550
public class AesGcmHkdfStreamingKeyManagerTest {
4651
private final AesGcmHkdfStreamingKeyManager manager = new AesGcmHkdfStreamingKeyManager();
4752
private final KeyTypeManager.KeyFactory<AesGcmHkdfStreamingKeyFormat, AesGcmHkdfStreamingKey>
@@ -296,12 +301,18 @@ public void testAes256GcmHkdf1MBTemplate() throws Exception {
296301
.build());
297302
}
298303

299-
@Test
300-
public void testKeyFormats() throws Exception {
301-
factory.validateKeyFormat(factory.keyFormats().get("AES128_GCM_HKDF_4KB").keyFormat);
302-
factory.validateKeyFormat(factory.keyFormats().get("AES128_GCM_HKDF_1MB").keyFormat);
303-
304-
factory.validateKeyFormat(factory.keyFormats().get("AES256_GCM_HKDF_4KB").keyFormat);
305-
factory.validateKeyFormat(factory.keyFormats().get("AES256_GCM_HKDF_1MB").keyFormat);
304+
@DataPoints("templateNames")
305+
public static final String[] KEY_TEMPLATES =
306+
new String[] {
307+
"AES128_GCM_HKDF_4KB", "AES128_GCM_HKDF_1MB", "AES256_GCM_HKDF_4KB", "AES256_GCM_HKDF_1MB",
308+
};
309+
310+
@Theory
311+
public void testTemplates(@FromDataPoints("templateNames") String templateName) throws Exception {
312+
KeysetHandle h = KeysetHandle.generateNew(KeyTemplates.get(templateName));
313+
assertThat(h.size()).isEqualTo(1);
314+
assertThat(h.getAt(0).getKey().getParameters())
315+
.isEqualTo(KeyTemplates.get(templateName).toParameters());
306316
}
317+
307318
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ java_test(
9797
"//proto:common_java_proto",
9898
"//proto:tink_java_proto",
9999
"//src/main/java/com/google/crypto/tink:key_template",
100+
"//src/main/java/com/google/crypto/tink:key_templates",
101+
"//src/main/java/com/google/crypto/tink:registry_cluster",
100102
"//src/main/java/com/google/crypto/tink:streaming_aead",
101103
"//src/main/java/com/google/crypto/tink/internal:key_type_manager",
102104
"//src/main/java/com/google/crypto/tink/streamingaead:aes_gcm_hkdf_streaming_key_manager",
@@ -120,6 +122,8 @@ java_test(
120122
"//proto:hmac_java_proto",
121123
"//proto:tink_java_proto",
122124
"//src/main/java/com/google/crypto/tink:key_template",
125+
"//src/main/java/com/google/crypto/tink:key_templates",
126+
"//src/main/java/com/google/crypto/tink:registry_cluster",
123127
"//src/main/java/com/google/crypto/tink:streaming_aead",
124128
"//src/main/java/com/google/crypto/tink/internal:key_type_manager",
125129
"//src/main/java/com/google/crypto/tink/internal:util",

0 commit comments

Comments
 (0)