Skip to content
This repository was archived by the owner on Apr 17, 2024. It is now read-only.

Commit 40510c4

Browse files
juergwcopybara-github
authored andcommitted
Use parametrized tests in JwkSetConverterTest.java.
Also, remove debugging output. PiperOrigin-RevId: 620046474
1 parent 0fdbac7 commit 40510c4

File tree

2 files changed

+47
-41
lines changed

2 files changed

+47
-41
lines changed

java_src/src/test/java/com/google/crypto/tink/jwt/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ java_test(
301301
"//src/main/java/com/google/crypto/tink:insecure_secret_key_access",
302302
"//src/main/java/com/google/crypto/tink:key_templates",
303303
"//src/main/java/com/google/crypto/tink:registry_cluster",
304+
"//src/main/java/com/google/crypto/tink:registry_configuration",
304305
"//src/main/java/com/google/crypto/tink:tink_json_proto_keyset_format",
305306
"//src/main/java/com/google/crypto/tink:tink_proto_keyset_format",
306307
"//src/main/java/com/google/crypto/tink/jwt:jwk_set_converter",

java_src/src/test/java/com/google/crypto/tink/jwt/JwkSetConverterTest.java

+46-41
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.crypto.tink.InsecureSecretKeyAccess;
2424
import com.google.crypto.tink.KeyTemplates;
2525
import com.google.crypto.tink.KeysetHandle;
26+
import com.google.crypto.tink.RegistryConfiguration;
2627
import com.google.crypto.tink.TinkJsonProtoKeysetFormat;
2728
import com.google.crypto.tink.TinkProtoKeysetFormat;
2829
import com.google.crypto.tink.proto.KeyData;
@@ -41,11 +42,14 @@
4142
import java.util.HashSet;
4243
import org.junit.Before;
4344
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;
4449
import org.junit.runner.RunWith;
45-
import org.junit.runners.JUnit4;
4650

4751
/** Unit tests for JwkSetConverter */
48-
@RunWith(JUnit4.class)
52+
@RunWith(Theories.class)
4953
public final class JwkSetConverterTest {
5054

5155
@Before
@@ -551,12 +555,10 @@ public void jwkPs256WithKid_isImportedAsRaw() throws Exception {
551555
KeysetHandle expected =
552556
TinkJsonProtoKeysetFormat.parseKeyset(
553557
PS256_JWK_SET_KID_TINK, InsecureSecretKeyAccess.get());
554-
System.out.println(
555-
TinkJsonProtoKeysetFormat.serializeKeyset(converted, InsecureSecretKeyAccess.get()));
556558
// The KeyID is picked at random, hence we just compare the keys.
557559
assertTrue(converted.getAt(0).getKey().equalsKey(expected.getAt(0).getKey()));
558560
}
559-
561+
560562
@Test
561563
public void jwkWithEmptyKid_kidIsPreserved() throws Exception {
562564
String esWithEmptyKid = ES256_JWK_SET_KID.replace("\"ENgjPA\"", "\"\"");
@@ -589,46 +591,49 @@ public void toPublicKeysetHandleSetsKeyIdsAndPrimaryKeyId() throws Exception {
589591
assertThat(ketsetInfo.getPrimaryKeyId()).isIn(keyIdSet);
590592
}
591593

592-
@Test
593-
public void convertTinkToJwksTokenVerification_success() throws Exception {
594+
@DataPoints("templatesNames")
595+
public static final String[] TEMPLATE_NAMES =
596+
new String[] {
597+
"JWT_ES256",
598+
"JWT_ES384",
599+
"JWT_ES512",
600+
"JWT_ES256_RAW",
601+
"JWT_RS256_2048_F4",
602+
"JWT_RS256_3072_F4",
603+
"JWT_RS384_3072_F4",
604+
"JWT_RS512_4096_F4",
605+
"JWT_RS256_2048_F4_RAW",
606+
"JWT_PS256_2048_F4",
607+
"JWT_PS256_3072_F4",
608+
"JWT_PS384_3072_F4",
609+
"JWT_PS512_4096_F4",
610+
"JWT_PS256_2048_F4_RAW",
611+
};
612+
613+
@Theory
614+
public void convertTinkToJwksTokenVerification_success(
615+
@FromDataPoints("templatesNames") String templateName) throws Exception {
594616
if (TestUtil.isTsan()) {
595617
// KeysetHandle.generateNew is too slow in Tsan.
596618
return;
597619
}
598-
// TODO(juerg): Use parametrized tests once b/26110951 is resolved.
599-
String[] templateNames = new String[] {
600-
"JWT_ES256",
601-
"JWT_ES384",
602-
"JWT_ES512",
603-
"JWT_ES256_RAW",
604-
"JWT_RS256_2048_F4",
605-
"JWT_RS256_3072_F4",
606-
"JWT_RS384_3072_F4",
607-
"JWT_RS512_4096_F4",
608-
"JWT_RS256_2048_F4_RAW",
609-
"JWT_PS256_2048_F4",
610-
"JWT_PS256_3072_F4",
611-
"JWT_PS384_3072_F4",
612-
"JWT_PS512_4096_F4",
613-
"JWT_PS256_2048_F4_RAW",
614-
};
615-
for (String templateName : templateNames) {
616-
KeysetHandle keysetHandle = KeysetHandle.generateNew(KeyTemplates.get(templateName));
617-
618-
String jwksString =
619-
JwkSetConverter.fromPublicKeysetHandle(keysetHandle.getPublicKeysetHandle());
620-
621-
KeysetHandle publicKeysetHandle = JwkSetConverter.toPublicKeysetHandle(jwksString);
622-
623-
JwtPublicKeySign signer = keysetHandle.getPrimitive(JwtPublicKeySign.class);
624-
JwtPublicKeyVerify verifier = publicKeysetHandle.getPrimitive(JwtPublicKeyVerify.class);
625-
626-
RawJwt rawToken = RawJwt.newBuilder().setJwtId("jwtId").withoutExpiration().build();
627-
String signedCompact = signer.signAndEncode(rawToken);
628-
JwtValidator validator = JwtValidator.newBuilder().allowMissingExpiration().build();
629-
VerifiedJwt verifiedToken = verifier.verifyAndDecode(signedCompact, validator);
630-
assertThat(verifiedToken.getJwtId()).isEqualTo("jwtId");
631-
}
620+
KeysetHandle keysetHandle = KeysetHandle.generateNew(KeyTemplates.get(templateName));
621+
622+
String jwksString =
623+
JwkSetConverter.fromPublicKeysetHandle(keysetHandle.getPublicKeysetHandle());
624+
625+
KeysetHandle publicKeysetHandle = JwkSetConverter.toPublicKeysetHandle(jwksString);
626+
627+
JwtPublicKeySign signer =
628+
keysetHandle.getPrimitive(RegistryConfiguration.get(), JwtPublicKeySign.class);
629+
JwtPublicKeyVerify verifier =
630+
publicKeysetHandle.getPrimitive(RegistryConfiguration.get(), JwtPublicKeyVerify.class);
631+
632+
RawJwt rawToken = RawJwt.newBuilder().setJwtId("jwtId").withoutExpiration().build();
633+
String signedCompact = signer.signAndEncode(rawToken);
634+
JwtValidator validator = JwtValidator.newBuilder().allowMissingExpiration().build();
635+
VerifiedJwt verifiedToken = verifier.verifyAndDecode(signedCompact, validator);
636+
assertThat(verifiedToken.getJwtId()).isEqualTo("jwtId");
632637
}
633638

634639
@Test

0 commit comments

Comments
 (0)