1 // Copyright 2022 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.internal; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.junit.Assert.assertThrows; 21 22 import com.google.crypto.tink.InsecureSecretKeyAccess; 23 import com.google.crypto.tink.Key; 24 import com.google.crypto.tink.Parameters; 25 import com.google.crypto.tink.SecretKeyAccess; 26 import com.google.crypto.tink.util.Bytes; 27 import com.google.errorprone.annotations.Immutable; 28 import java.security.GeneralSecurityException; 29 import javax.annotation.Nullable; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.junit.runners.JUnit4; 33 34 /** Tests for {@link KeySerializer}. */ 35 @RunWith(JUnit4.class) 36 public final class KeySerializerTest { 37 38 @Immutable 39 private static class ExampleKey extends Key { 40 @Override equalsKey(Key k)41 public boolean equalsKey(Key k) { 42 return k == this; 43 } 44 45 @Override 46 @Nullable getIdRequirementOrNull()47 public Integer getIdRequirementOrNull() { 48 return null; 49 } 50 51 @Override getParameters()52 public Parameters getParameters() { 53 throw new UnsupportedOperationException("Not needed in test"); 54 } 55 } 56 57 @Immutable 58 private static class ExampleSerialization implements Serialization { 59 @Override getObjectIdentifier()60 public Bytes getObjectIdentifier() { 61 return Bytes.copyFrom(new byte[0]); 62 } 63 } 64 serialize(ExampleKey k, @Nullable SecretKeyAccess access)65 private static ExampleSerialization serialize(ExampleKey k, @Nullable SecretKeyAccess access) 66 throws GeneralSecurityException { 67 SecretKeyAccess.requireAccess(access); 68 return new ExampleSerialization(); 69 } 70 71 @Test createSerializer_works()72 public void createSerializer_works() throws Exception { 73 Object unused = 74 KeySerializer.create( 75 KeySerializerTest::serialize, ExampleKey.class, ExampleSerialization.class); 76 } 77 78 @Test createSerializer_serializeKey_works()79 public void createSerializer_serializeKey_works() throws Exception { 80 KeySerializer<ExampleKey, ExampleSerialization> serializer = 81 KeySerializer.create( 82 KeySerializerTest::serialize, ExampleKey.class, ExampleSerialization.class); 83 assertThat(serializer.serializeKey(new ExampleKey(), InsecureSecretKeyAccess.get())) 84 .isNotNull(); 85 assertThrows( 86 GeneralSecurityException.class, 87 () -> serializer.serializeKey(new ExampleKey(), /* access = */ null)); 88 } 89 90 @Test createSerializer_classes_work()91 public void createSerializer_classes_work() throws Exception { 92 KeySerializer<ExampleKey, ExampleSerialization> serializer = 93 KeySerializer.create( 94 KeySerializerTest::serialize, ExampleKey.class, ExampleSerialization.class); 95 assertThat(serializer.getKeyClass()).isEqualTo(ExampleKey.class); 96 assertThat(serializer.getSerializationClass()).isEqualTo(ExampleSerialization.class); 97 } 98 } 99