1 // Copyright 2023 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 17 package com.google.crypto.tink.prf; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.junit.Assert.assertThrows; 21 22 import java.security.GeneralSecurityException; 23 import org.junit.Test; 24 import org.junit.experimental.theories.DataPoints; 25 import org.junit.experimental.theories.FromDataPoints; 26 import org.junit.experimental.theories.Theories; 27 import org.junit.experimental.theories.Theory; 28 import org.junit.runner.RunWith; 29 30 @RunWith(Theories.class) 31 public final class AesCmacPrfParametersTest { 32 @DataPoints("keySizes") 33 public static final int[] KEY_SIZES = new int[] {16, 32}; 34 35 @Theory createParametersAndGetProperties(@romDataPoints"keySizes") int keySize)36 public void createParametersAndGetProperties(@FromDataPoints("keySizes") int keySize) 37 throws Exception { 38 AesCmacPrfParameters parameters = AesCmacPrfParameters.create(keySize); 39 assertThat(parameters.getKeySizeBytes()).isEqualTo(keySize); 40 assertThat(parameters.hasIdRequirement()).isFalse(); 41 assertThat(parameters.toString()) 42 .isEqualTo("AesCmac PRF Parameters (" + keySize + "-byte key)"); 43 } 44 45 @Test createWithUnsupportedKeySize_fails()46 public void createWithUnsupportedKeySize_fails() throws Exception { 47 assertThrows(GeneralSecurityException.class, () -> AesCmacPrfParameters.create(19)); 48 } 49 50 @Theory testEqualsAndHashCode(@romDataPoints"keySizes") int keySize)51 public void testEqualsAndHashCode(@FromDataPoints("keySizes") int keySize) throws Exception { 52 AesCmacPrfParameters parameters = AesCmacPrfParameters.create(keySize); 53 AesCmacPrfParameters sameParameters = AesCmacPrfParameters.create(keySize); 54 55 assertThat(sameParameters).isEqualTo(parameters); 56 assertThat(sameParameters.hashCode()).isEqualTo(parameters.hashCode()); 57 } 58 59 @Test testEqualsAndHashCode_different()60 public void testEqualsAndHashCode_different() throws Exception { 61 AesCmacPrfParameters parameters16 = AesCmacPrfParameters.create(16); 62 AesCmacPrfParameters parameters32 = AesCmacPrfParameters.create(32); 63 64 assertThat(parameters16).isNotEqualTo(parameters32); 65 assertThat(parameters16.hashCode()).isNotEqualTo(parameters32.hashCode()); 66 } 67 68 @Test 69 @SuppressWarnings("TruthIncompatibleType") testEqualDifferentClass()70 public void testEqualDifferentClass() throws Exception { 71 AesCmacPrfParameters aesCmacPrfParameters = AesCmacPrfParameters.create(16); 72 HkdfPrfParameters hkdfPrfParameters = 73 HkdfPrfParameters.builder() 74 .setKeySizeBytes(16) 75 .setHashType(HkdfPrfParameters.HashType.SHA256) 76 .build(); 77 assertThat(aesCmacPrfParameters).isNotEqualTo(hkdfPrfParameters); 78 } 79 } 80