1 // Copyright 2021 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 //////////////////////////////////////////////////////////////////////////////// 16 package com.google.crypto.tink.config; 17 18 import static com.google.common.truth.Truth.assertThat; 19 import static org.junit.Assert.assertThrows; 20 21 import com.google.crypto.tink.config.internal.TinkFipsUtil; 22 import java.security.GeneralSecurityException; 23 import org.junit.Assume; 24 import org.junit.Test; 25 import org.junit.runner.RunWith; 26 import org.junit.runners.JUnit4; 27 28 /** 29 * Tests for TinkFips. The tests here check the behavior of Tink when built with or without the 30 * --use_only_fips flag. In order to get complete coverage a build should be tested with: 1) Build 31 * without the --use_only_fips flag. 2) Build with the --use_only_fips flag, and BoringCrypto not 32 * being available. 3) Build with the --use_only_fips flag, and BoringCrypto being available. 33 */ 34 @RunWith(JUnit4.class) 35 public final class TinkFipsTest { 36 37 @Test testFipsOnlyModeConsistentDisabled()38 public void testFipsOnlyModeConsistentDisabled() { 39 // If the TinkFipsUtil reports that FIPS-mode is disabled, then TinkFips must report that 40 // FIPS-mode is disabled. 41 Assume.assumeFalse(TinkFipsUtil.useOnlyFips()); 42 assertThat(TinkFips.useOnlyFips()).isFalse(); 43 } 44 45 @Test testFipsOnlyModeConsistentEnabled()46 public void testFipsOnlyModeConsistentEnabled() { 47 // If the TinkFipsUtil reports that FIPS-mode is enabled, then TinkFips must report that 48 // FIPS-mode is enabled. 49 Assume.assumeTrue(TinkFipsUtil.useOnlyFips()); 50 assertThat(TinkFips.useOnlyFips()).isTrue(); 51 } 52 53 @Test testFipsEnablingAtRuntime_worksWhenFipsModuleIsAvailable()54 public void testFipsEnablingAtRuntime_worksWhenFipsModuleIsAvailable() 55 throws GeneralSecurityException { 56 // If Tink has not been built in FIPS-mode, then the useOnlyFips() call should only return 57 // true after the restrictions have been enabled. 58 Assume.assumeFalse(TinkFipsUtil.useOnlyFips()); 59 Assume.assumeTrue(TinkFipsUtil.fipsModuleAvailable()); 60 61 assertThat(TinkFips.useOnlyFips()).isFalse(); 62 TinkFips.restrictToFips(); 63 assertThat(TinkFips.useOnlyFips()).isTrue(); 64 } 65 66 @Test testFipsEnablingAtRuntime_failsWhenFipsModuleIsNotAvailable()67 public void testFipsEnablingAtRuntime_failsWhenFipsModuleIsNotAvailable() 68 throws GeneralSecurityException { 69 Assume.assumeFalse(TinkFipsUtil.useOnlyFips()); 70 Assume.assumeFalse(TinkFipsUtil.fipsModuleAvailable()); 71 72 assertThrows(GeneralSecurityException.class, TinkFips::restrictToFips); 73 } 74 } 75