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 21 import com.google.crypto.tink.Parameters; 22 import com.google.crypto.tink.util.Bytes; 23 import com.google.errorprone.annotations.Immutable; 24 import java.security.GeneralSecurityException; 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 import org.junit.runners.JUnit4; 28 29 /** Tests for {@link ParametersSerializer}. */ 30 @RunWith(JUnit4.class) 31 public final class ParametersSerializerTest { 32 33 @Immutable 34 private static class ExampleParameters extends Parameters { 35 @Override hasIdRequirement()36 public boolean hasIdRequirement() { 37 return false; 38 } 39 } 40 41 @Immutable 42 private static class ExampleSerialization implements Serialization { 43 @Override getObjectIdentifier()44 public Bytes getObjectIdentifier() { 45 return Bytes.copyFrom(new byte[0]); 46 } 47 } 48 serialize(ExampleParameters k)49 private static ExampleSerialization serialize(ExampleParameters k) 50 throws GeneralSecurityException { 51 return new ExampleSerialization(); 52 } 53 54 @Test createSerializer_works()55 public void createSerializer_works() throws Exception { 56 Object unused = 57 ParametersSerializer.create( 58 ParametersSerializerTest::serialize, 59 ExampleParameters.class, 60 ExampleSerialization.class); 61 } 62 63 @Test createSerializer_serializeKey_works()64 public void createSerializer_serializeKey_works() throws Exception { 65 ParametersSerializer<ExampleParameters, ExampleSerialization> serializer = 66 ParametersSerializer.create( 67 ParametersSerializerTest::serialize, 68 ExampleParameters.class, 69 ExampleSerialization.class); 70 assertThat(serializer.serializeParameters(new ExampleParameters())).isNotNull(); 71 } 72 73 @Test createSerializer_classes_work()74 public void createSerializer_classes_work() throws Exception { 75 ParametersSerializer<ExampleParameters, ExampleSerialization> serializer = 76 ParametersSerializer.create( 77 ParametersSerializerTest::serialize, 78 ExampleParameters.class, 79 ExampleSerialization.class); 80 assertThat(serializer.getParametersClass()).isEqualTo(ExampleParameters.class); 81 assertThat(serializer.getSerializationClass()).isEqualTo(ExampleSerialization.class); 82 } 83 } 84