• 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 java.nio.charset.StandardCharsets.UTF_8;
21 import static org.junit.Assert.assertThrows;
22 
23 import com.google.crypto.tink.proto.KeyData.KeyMaterialType;
24 import com.google.crypto.tink.proto.OutputPrefixType;
25 import com.google.crypto.tink.util.Bytes;
26 import com.google.protobuf.ByteString;
27 import java.security.GeneralSecurityException;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.junit.runners.JUnit4;
31 
32 /** Tests for {@code ProtoKeySerialization} */
33 @RunWith(JUnit4.class)
34 public final class ProtoKeySerializationTest {
35   @Test
testCreationAndValues_basic()36   public void testCreationAndValues_basic() throws Exception {
37     ProtoKeySerialization serialization =
38         ProtoKeySerialization.create(
39             "myTypeUrl",
40             ByteString.copyFrom(new byte[] {10, 11, 12}),
41             KeyMaterialType.SYMMETRIC,
42             OutputPrefixType.RAW,
43             /* idRequirement = */ null);
44 
45     assertThat(serialization.getValue()).isEqualTo(ByteString.copyFrom(new byte[] {10, 11, 12}));
46     assertThat(serialization.getKeyMaterialType()).isEqualTo(KeyMaterialType.SYMMETRIC);
47     assertThat(serialization.getOutputPrefixType()).isEqualTo(OutputPrefixType.RAW);
48     assertThat(serialization.getTypeUrl()).isEqualTo("myTypeUrl");
49     assertThat(serialization.getIdRequirementOrNull()).isNull();
50     assertThat(serialization.getObjectIdentifier())
51         .isEqualTo(Bytes.copyFrom("myTypeUrl".getBytes(UTF_8)));
52   }
53 
54   @Test
testIdRequirement_present()55   public void testIdRequirement_present() throws Exception {
56     final String typeUrl = "myTypeUrl";
57     final ByteString value = ByteString.copyFrom(new byte[] {10, 11, 12});
58     final KeyMaterialType keyMaterialType = KeyMaterialType.SYMMETRIC;
59 
60     ProtoKeySerialization serialization =
61         ProtoKeySerialization.create(typeUrl, value, keyMaterialType, OutputPrefixType.TINK, 123);
62     assertThat(serialization.getOutputPrefixType()).isEqualTo(OutputPrefixType.TINK);
63     assertThat(serialization.getIdRequirementOrNull()).isEqualTo(123);
64   }
65 
66   @Test
testCreationWithTypeUrlWithNonAsciiCharacter_throws()67   public void testCreationWithTypeUrlWithNonAsciiCharacter_throws() throws Exception {
68     assertThrows(
69         GeneralSecurityException.class,
70         () ->
71             ProtoKeySerialization.create(
72                 /* typeUrl= */ "\t",
73                 ByteString.copyFrom(new byte[] {10, 11, 12}),
74                 KeyMaterialType.SYMMETRIC,
75                 OutputPrefixType.RAW,
76                 /* idRequirement= */ null));
77   }
78 
79   @Test
testIdRequirement_presentMustMatchoutputPrefixType()80   public void testIdRequirement_presentMustMatchoutputPrefixType() throws Exception {
81     final String typeUrl = "myTypeUrl";
82     final ByteString value = ByteString.copyFrom(new byte[] {10, 11, 12});
83     final KeyMaterialType keyMaterialType = KeyMaterialType.SYMMETRIC;
84 
85     Object unused =
86         ProtoKeySerialization.create(
87             typeUrl, value, keyMaterialType, OutputPrefixType.RAW, /* idRequirement= */ null);
88     unused =
89         ProtoKeySerialization.create(typeUrl, value, keyMaterialType, OutputPrefixType.TINK, 123);
90     unused =
91         ProtoKeySerialization.create(
92             typeUrl, value, keyMaterialType, OutputPrefixType.CRUNCHY, 123);
93     unused =
94         ProtoKeySerialization.create(typeUrl, value, keyMaterialType, OutputPrefixType.LEGACY, 123);
95 
96     assertThrows(
97         GeneralSecurityException.class,
98         () ->
99             ProtoKeySerialization.create(
100                 typeUrl, value, keyMaterialType, OutputPrefixType.RAW, 123));
101     assertThrows(
102         GeneralSecurityException.class,
103         () ->
104             ProtoKeySerialization.create(
105                 typeUrl,
106                 value,
107                 keyMaterialType,
108                 OutputPrefixType.TINK,
109                 /* idRequirement = */ null));
110     assertThrows(
111         GeneralSecurityException.class,
112         () ->
113             ProtoKeySerialization.create(
114                 typeUrl,
115                 value,
116                 keyMaterialType,
117                 OutputPrefixType.CRUNCHY,
118                 /* idRequirement = */ null));
119     assertThrows(
120         GeneralSecurityException.class,
121         () ->
122             ProtoKeySerialization.create(
123                 typeUrl,
124                 value,
125                 keyMaterialType,
126                 OutputPrefixType.LEGACY,
127                 /* idRequirement = */ null));
128   }
129 }
130