• 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.Key;
23 import com.google.crypto.tink.Parameters;
24 import com.google.errorprone.annotations.Immutable;
25 import java.security.GeneralSecurityException;
26 import javax.annotation.Nullable;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.junit.runners.JUnit4;
30 
31 /**
32  * Tests for {@link MutablePrimitiveRegistryTest}.
33  *
34  * <p>We repeat the main tests in PrimitiveRegistryTest. There really shouldn't be both classes, but
35  * currently this is what we need, and the other is what we should have.
36  */
37 @RunWith(JUnit4.class)
38 public final class MutablePrimitiveRegistryTest {
39   @Immutable
40   private static final class TestKey1 extends Key {
41     @Override
getParameters()42     public Parameters getParameters() {
43       throw new UnsupportedOperationException("Not needed in test");
44     }
45 
46     @Override
47     @Nullable
getIdRequirementOrNull()48     public Integer getIdRequirementOrNull() {
49       throw new UnsupportedOperationException("Not needed in test");
50     }
51 
52     @Override
equalsKey(Key other)53     public boolean equalsKey(Key other) {
54       throw new UnsupportedOperationException("Not needed in test");
55     }
56   }
57 
58   @Immutable
59   private static final class TestKey2 extends Key {
60     @Override
getParameters()61     public Parameters getParameters() {
62       throw new UnsupportedOperationException("Not needed in test");
63     }
64 
65     @Override
66     @Nullable
getIdRequirementOrNull()67     public Integer getIdRequirementOrNull() {
68       throw new UnsupportedOperationException("Not needed in test");
69     }
70 
71     @Override
equalsKey(Key other)72     public boolean equalsKey(Key other) {
73       throw new UnsupportedOperationException("Not needed in test");
74     }
75   }
76 
77   @Immutable
78   private static final class TestPrimitiveA {
TestPrimitiveA()79     public TestPrimitiveA() {}
80   }
81 
82   @Immutable
83   private static final class TestPrimitiveB {
TestPrimitiveB()84     public TestPrimitiveB() {}
85   }
86 
getPrimitiveAKey1( MutablePrimitiveRegistryTest.TestKey1 key)87   private static MutablePrimitiveRegistryTest.TestPrimitiveA getPrimitiveAKey1(
88       MutablePrimitiveRegistryTest.TestKey1 key) {
89     return new MutablePrimitiveRegistryTest.TestPrimitiveA();
90   }
91 
getPrimitiveAKey2( MutablePrimitiveRegistryTest.TestKey2 key)92   private static MutablePrimitiveRegistryTest.TestPrimitiveA getPrimitiveAKey2(
93       MutablePrimitiveRegistryTest.TestKey2 key) {
94     return new MutablePrimitiveRegistryTest.TestPrimitiveA();
95   }
96 
getPrimitiveBKey1( MutablePrimitiveRegistryTest.TestKey1 key)97   private static MutablePrimitiveRegistryTest.TestPrimitiveB getPrimitiveBKey1(
98       MutablePrimitiveRegistryTest.TestKey1 key) {
99     return new MutablePrimitiveRegistryTest.TestPrimitiveB();
100   }
101 
getPrimitiveBKey2( MutablePrimitiveRegistryTest.TestKey2 key)102   private static MutablePrimitiveRegistryTest.TestPrimitiveB getPrimitiveBKey2(
103       MutablePrimitiveRegistryTest.TestKey2 key) {
104     return new MutablePrimitiveRegistryTest.TestPrimitiveB();
105   }
106 
107   @Immutable
108   private static final class TestWrapperA
109       implements PrimitiveWrapper<TestPrimitiveA, TestPrimitiveA> {
110 
111     @Override
wrap(final PrimitiveSet<TestPrimitiveA> primitives)112     public TestPrimitiveA wrap(final PrimitiveSet<TestPrimitiveA> primitives)
113         throws GeneralSecurityException {
114       return new TestPrimitiveA();
115     }
116 
117     @Override
getPrimitiveClass()118     public Class<TestPrimitiveA> getPrimitiveClass() {
119       return TestPrimitiveA.class;
120     }
121 
122     @Override
getInputPrimitiveClass()123     public Class<TestPrimitiveA> getInputPrimitiveClass() {
124       return TestPrimitiveA.class;
125     }
126   }
127 
128   @Immutable
129   private static final class TestWrapperB
130       implements PrimitiveWrapper<TestPrimitiveA, TestPrimitiveB> {
131 
132     @Override
wrap(final PrimitiveSet<TestPrimitiveA> primitives)133     public TestPrimitiveB wrap(final PrimitiveSet<TestPrimitiveA> primitives)
134         throws GeneralSecurityException {
135       return new TestPrimitiveB();
136     }
137 
138     @Override
getPrimitiveClass()139     public Class<TestPrimitiveB> getPrimitiveClass() {
140       return TestPrimitiveB.class;
141     }
142 
143     @Override
getInputPrimitiveClass()144     public Class<TestPrimitiveA> getInputPrimitiveClass() {
145       return TestPrimitiveA.class;
146     }
147   }
148 
149   @Test
test_registerAll_checkDispatch()150   public void test_registerAll_checkDispatch() throws Exception {
151     MutablePrimitiveRegistry registry = new MutablePrimitiveRegistry();
152 
153     registry.registerPrimitiveConstructor(
154         PrimitiveConstructor.create(
155             MutablePrimitiveRegistryTest::getPrimitiveAKey1, TestKey1.class, TestPrimitiveA.class));
156     registry.registerPrimitiveConstructor(
157         PrimitiveConstructor.create(
158             MutablePrimitiveRegistryTest::getPrimitiveAKey2, TestKey2.class, TestPrimitiveA.class));
159     registry.registerPrimitiveConstructor(
160         PrimitiveConstructor.create(
161             MutablePrimitiveRegistryTest::getPrimitiveBKey1, TestKey1.class, TestPrimitiveB.class));
162     registry.registerPrimitiveConstructor(
163         PrimitiveConstructor.create(
164             MutablePrimitiveRegistryTest::getPrimitiveBKey2, TestKey2.class, TestPrimitiveB.class));
165 
166     assertThat(
167             registry.getPrimitive(
168                 new MutablePrimitiveRegistryTest.TestKey1(),
169                 MutablePrimitiveRegistryTest.TestPrimitiveA.class))
170         .isNotNull();
171     assertThat(
172             registry.getPrimitive(
173                 new MutablePrimitiveRegistryTest.TestKey2(),
174                 MutablePrimitiveRegistryTest.TestPrimitiveA.class))
175         .isNotNull();
176     assertThat(
177             registry.getPrimitive(
178                 new MutablePrimitiveRegistryTest.TestKey1(),
179                 MutablePrimitiveRegistryTest.TestPrimitiveB.class))
180         .isNotNull();
181     assertThat(
182             registry.getPrimitive(
183                 new MutablePrimitiveRegistryTest.TestKey2(),
184                 MutablePrimitiveRegistryTest.TestPrimitiveB.class))
185         .isNotNull();
186   }
187 
188   @Test
test_emptyRegistry_throws()189   public void test_emptyRegistry_throws() throws Exception {
190     MutablePrimitiveRegistry registry = new MutablePrimitiveRegistry();
191     assertThrows(
192         GeneralSecurityException.class,
193         () ->
194             registry.getPrimitive(
195                 new MutablePrimitiveRegistryTest.TestKey1(),
196                 MutablePrimitiveRegistryTest.TestPrimitiveA.class));
197     assertThrows(
198         GeneralSecurityException.class,
199         () -> registry.getInputPrimitiveClass(MutablePrimitiveRegistryTest.TestPrimitiveA.class));
200     assertThrows(
201         GeneralSecurityException.class,
202         () ->
203             registry.wrap(
204                 PrimitiveSet.newBuilder(MutablePrimitiveRegistryTest.TestPrimitiveA.class).build(),
205                 MutablePrimitiveRegistryTest.TestPrimitiveA.class));
206   }
207 
208   @Test
test_registerAllWrappers_checkDispatch()209   public void test_registerAllWrappers_checkDispatch() throws Exception {
210     MutablePrimitiveRegistry registry = new MutablePrimitiveRegistry();
211 
212     registry.registerPrimitiveWrapper(new TestWrapperA());
213     registry.registerPrimitiveWrapper(new TestWrapperB());
214 
215     assertThat(registry.getInputPrimitiveClass(TestPrimitiveA.class))
216         .isEqualTo(TestPrimitiveA.class);
217     assertThat(registry.getInputPrimitiveClass(TestPrimitiveB.class))
218         .isEqualTo(TestPrimitiveA.class);
219     assertThat(
220             registry.wrap(
221                 PrimitiveSet.newBuilder(TestPrimitiveA.class).build(), TestPrimitiveA.class))
222         .isInstanceOf(TestPrimitiveA.class);
223     assertThat(
224             registry.wrap(
225                 PrimitiveSet.newBuilder(TestPrimitiveA.class).build(), TestPrimitiveB.class))
226         .isInstanceOf(TestPrimitiveB.class);
227   }
228 }
229