• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ParametersParser}. */
30 @RunWith(JUnit4.class)
31 public final class ParametersParserTest {
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 
parse(ExampleSerialization serialization)49   private static ExampleParameters parse(ExampleSerialization serialization)
50       throws GeneralSecurityException {
51     return new ExampleParameters();
52   }
53 
54   @Test
createParser_works()55   public void createParser_works() throws Exception {
56     Object unused =
57         ParametersParser.create(
58             ParametersParserTest::parse, Bytes.copyFrom(new byte[0]), ExampleSerialization.class);
59   }
60 
61   @Test
createParser_parseKey_works()62   public void createParser_parseKey_works() throws Exception {
63     ParametersParser<ExampleSerialization> parser =
64         ParametersParser.create(
65             ParametersParserTest::parse, Bytes.copyFrom(new byte[0]), ExampleSerialization.class);
66     assertThat(parser.parseParameters(new ExampleSerialization())).isNotNull();
67   }
68 
69   @Test
createParser_classes_work()70   public void createParser_classes_work() throws Exception {
71     ParametersParser<ExampleSerialization> parser =
72         ParametersParser.create(
73             ParametersParserTest::parse,
74             Bytes.copyFrom(new byte[] {1, 2, 3}),
75             ExampleSerialization.class);
76     assertThat(parser.getObjectIdentifier()).isEqualTo(Bytes.copyFrom(new byte[] {1, 2, 3}));
77     assertThat(parser.getSerializationClass()).isEqualTo(ExampleSerialization.class);
78   }
79 }
80