Skip to content

Commit

Permalink
Add tests for JWT registration.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 563002709
Change-Id: I44fd47f77df5aad44cfdeea4093f33d19abf023a
  • Loading branch information
juergw authored and copybara-github committed Sep 6, 2023
1 parent d8203de commit df1ac75
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/test/java/com/google/crypto/tink/jwt/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -651,3 +651,33 @@ java_test(
"@maven//:junit_junit",
],
)

java_test(
name = "JwtSignatureConfigTest",
size = "small",
srcs = ["JwtSignatureConfigTest.java"],
tags = ["fips"],
deps = [
"//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/config:tink_fips",
"//src/main/java/com/google/crypto/tink/config/internal:tink_fips_util",
"//src/main/java/com/google/crypto/tink/jwt:jwt_signature_config",
"@maven//:junit_junit",
],
)

java_test(
name = "JwtMacConfigTest",
size = "small",
srcs = ["JwtMacConfigTest.java"],
tags = ["fips"],
deps = [
"//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/config:tink_fips",
"//src/main/java/com/google/crypto/tink/config/internal:tink_fips_util",
"//src/main/java/com/google/crypto/tink/jwt:jwt_mac_config",
"@maven//:junit_junit",
],
)
55 changes: 55 additions & 0 deletions src/test/java/com/google/crypto/tink/jwt/JwtMacConfigTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2023 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////

package com.google.crypto.tink.jwt;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;

import com.google.crypto.tink.KeyTemplates;
import com.google.crypto.tink.KeysetHandle;
import com.google.crypto.tink.config.TinkFips;
import com.google.crypto.tink.config.internal.TinkFipsUtil;
import java.security.GeneralSecurityException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for JwtMacConfigTest. */
@RunWith(JUnit4.class)
public class JwtMacConfigTest {

@Test
public void failIfAndOnlyIfInInvalidFipsState() throws Exception {
boolean invalidFipsState = TinkFips.useOnlyFips() && !TinkFipsUtil.fipsModuleAvailable();

if (invalidFipsState) {
assertThrows(GeneralSecurityException.class, JwtMacConfig::register);
assertThrows(
GeneralSecurityException.class,
() -> KeysetHandle.generateNew(KeyTemplates.get("JWT_HS256")));

} else {
if (TinkFips.useOnlyFips()) {
// TODO(b/298896710): This currently fails, but this is a bug.
assertThrows(GeneralSecurityException.class, JwtMacConfig::register);
return;
}
JwtMacConfig.register();
assertNotNull(KeysetHandle.generateNew(KeyTemplates.get("JWT_HS256")));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2023 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////

package com.google.crypto.tink.jwt;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;

import com.google.crypto.tink.KeyTemplates;
import com.google.crypto.tink.KeysetHandle;
import com.google.crypto.tink.config.TinkFips;
import com.google.crypto.tink.config.internal.TinkFipsUtil;
import java.security.GeneralSecurityException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for JwtSignatureConfigTest. */
@RunWith(JUnit4.class)
public class JwtSignatureConfigTest {

@Test
public void failIfAndOnlyIfInInvalidFipsState() throws Exception {
boolean invalidFipsState = TinkFips.useOnlyFips() && !TinkFipsUtil.fipsModuleAvailable();

if (invalidFipsState) {
assertThrows(GeneralSecurityException.class, JwtSignatureConfig::register);
assertThrows(
GeneralSecurityException.class,
() -> KeysetHandle.generateNew(KeyTemplates.get("JWT_ES256")));
assertThrows(
GeneralSecurityException.class,
() -> KeysetHandle.generateNew(KeyTemplates.get("JWT_RS256_2048_F4")));
assertThrows(
GeneralSecurityException.class,
() -> KeysetHandle.generateNew(KeyTemplates.get("JWT_PS256_2048_F4")));

} else {
if (TinkFips.useOnlyFips()) {
// TODO(b/298896710): This currently fails, but this is a bug.
assertThrows(GeneralSecurityException.class, JwtSignatureConfig::register);
return;
}
JwtSignatureConfig.register();
assertNotNull(KeysetHandle.generateNew(KeyTemplates.get("JWT_ES256")));
assertNotNull(KeysetHandle.generateNew(KeyTemplates.get("JWT_RS256_2048_F4")));
assertNotNull(KeysetHandle.generateNew(KeyTemplates.get("JWT_PS256_2048_F4")));
}
}
}

0 comments on commit df1ac75

Please sign in to comment.