• 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 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 KeyParser}. */
35 @RunWith(JUnit4.class)
36 public final class KeyParserTest {
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 
parse( ExampleSerialization serialization, @Nullable SecretKeyAccess access)65   private static ExampleKey parse(
66       ExampleSerialization serialization, @Nullable SecretKeyAccess access)
67       throws GeneralSecurityException {
68     SecretKeyAccess.requireAccess(access);
69     return new ExampleKey();
70   }
71 
72   @Test
createParser_works()73   public void createParser_works() throws Exception {
74     Object unused =
75         KeyParser.create(
76             KeyParserTest::parse, Bytes.copyFrom(new byte[0]), ExampleSerialization.class);
77   }
78 
79   @Test
createParser_parseKey_works()80   public void createParser_parseKey_works() throws Exception {
81     KeyParser<ExampleSerialization> parser =
82         KeyParser.create(
83             KeyParserTest::parse, Bytes.copyFrom(new byte[0]), ExampleSerialization.class);
84     assertThat(parser.parseKey(new ExampleSerialization(), InsecureSecretKeyAccess.get()))
85         .isNotNull();
86     assertThrows(
87         GeneralSecurityException.class,
88         () -> parser.parseKey(new ExampleSerialization(), /* access = */ null));
89   }
90 
91   @Test
createParser_classes_work()92   public void createParser_classes_work() throws Exception {
93     KeyParser<ExampleSerialization> parser =
94         KeyParser.create(
95             KeyParserTest::parse, Bytes.copyFrom(new byte[] {1, 2, 3}), ExampleSerialization.class);
96     assertThat(parser.getObjectIdentifier()).isEqualTo(Bytes.copyFrom(new byte[] {1, 2, 3}));
97     assertThat(parser.getSerializationClass()).isEqualTo(ExampleSerialization.class);
98   }
99 }
100