• 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 javax.annotation.Nullable;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.junit.runners.JUnit4;
28 
29 /** Tests for {@link KeyTester}. */
30 @RunWith(JUnit4.class)
31 public final class KeyTesterTest {
32   private static class TestParameters extends Parameters {
33     private final int hashCode;
34 
TestParameters(int hashCode)35     public TestParameters(int hashCode) {
36       this.hashCode = hashCode;
37     }
38 
39     @Override
hasIdRequirement()40     public boolean hasIdRequirement() {
41       return false;
42     }
43 
44     @Override
hashCode()45     public int hashCode() {
46       return hashCode;
47     }
48 
49     @Override
equals(Object o)50     public boolean equals(Object o) {
51       if (!(o instanceof TestParameters)) {
52         return false;
53       }
54       return ((TestParameters) o).hashCode == hashCode;
55     }
56   }
57 
58   private static class TestKey extends Key {
59     private final int id;
60     private final TestParameters parameters;
61 
TestKey(int id, TestParameters parameters)62     public TestKey(int id, TestParameters parameters) {
63       this.id = id;
64       this.parameters = parameters;
65     }
66 
TestKey(int id)67     public TestKey(int id) {
68       this.id = id;
69       this.parameters = new TestParameters(0);
70     }
71 
72     @Override
equalsKey(Key other)73     public boolean equalsKey(Key other) {
74       return ((TestKey) other).id == id;
75     }
76 
77     @Override
78     @Nullable
getIdRequirementOrNull()79     public Integer getIdRequirementOrNull() {
80       return parameters.hasIdRequirement() ? id : null;
81     }
82 
83     @Override
getParameters()84     public Parameters getParameters() {
85       return parameters;
86     }
87   }
88 
89   @Test
keyTester_works()90   public void keyTester_works() throws Exception {
91     TestParameters parameters0 = new TestParameters(0);
92     TestParameters parameters1 = new TestParameters(1);
93     new KeyTester()
94         .addEqualityGroup("Group 0a", new TestKey(0, parameters0), new TestKey(0, parameters0))
95         .addEqualityGroup("Group 0b", new TestKey(1, parameters0), new TestKey(1, parameters0))
96         .addEqualityGroup("Group 1", new TestKey(2, parameters1), new TestKey(2, parameters1))
97         .doTests();
98   }
99 
100   @Test
differentKeysSameGroup_throws()101   public void differentKeysSameGroup_throws() throws Exception {
102     KeyTester tester =
103         new KeyTester().addEqualityGroup("MyWrongGroup", new TestKey(0), new TestKey(1));
104     AssertionError thrown = assertThrows(AssertionError.class, tester::doTests);
105     assertThat(thrown).hasMessageThat().contains("from group 'MyWrongGroup' are not equal");
106   }
107 
108   @Test
sameKeyDifferentGroup_throws()109   public void sameKeyDifferentGroup_throws() throws Exception {
110     KeyTester tester =
111         new KeyTester()
112             .addEqualityGroup("MyGroup0", new TestKey(0))
113             .addEqualityGroup("MyGroup1", new TestKey(0));
114     AssertionError thrown = assertThrows(AssertionError.class, tester::doTests);
115     assertThat(thrown).hasMessageThat().contains("equalsKey returns true");
116     assertThat(thrown).hasMessageThat().contains("MyGroup0");
117     assertThat(thrown).hasMessageThat().contains("MyGroup1");
118   }
119 
120   @Test
sameKeyGroupDifferentParameters_throws()121   public void sameKeyGroupDifferentParameters_throws() throws Exception {
122     TestParameters parameters0 = new TestParameters(0);
123     TestParameters parameters1 = new TestParameters(1);
124     KeyTester tester =
125         new KeyTester()
126             .addEqualityGroup("MyGroup0", new TestKey(0, parameters0), new TestKey(0, parameters1));
127     assertThrows(AssertionError.class, tester::doTests);
128   }
129 
130   @Test
sameKeyGroup_parametersDifferentHashCode_throws()131   public void sameKeyGroup_parametersDifferentHashCode_throws() throws Exception {
132     TestParameters parameters0 = new TestParameters(0);
133     TestParameters parameters1 =
134         new TestParameters(0) {
135           @Override
136           public int hashCode() {
137             return 12345;
138           }
139         };
140     KeyTester tester =
141         new KeyTester()
142             .addEqualityGroup("MyGroup0", new TestKey(0, parameters0), new TestKey(0, parameters1));
143     assertThrows(AssertionError.class, tester::doTests);
144   }
145 
146   @Test
sameKeyGroup_parametersDifferentIdRequirements_throws()147   public void sameKeyGroup_parametersDifferentIdRequirements_throws() throws Exception {
148     TestParameters parameters0 = new TestParameters(0);
149     TestParameters parameters1 =
150         new TestParameters(0) {
151           @Override
152           public boolean hasIdRequirement() {
153             return true;
154           }
155         };
156     KeyTester tester =
157         new KeyTester()
158             .addEqualityGroup("MyGroup0", new TestKey(0, parameters0), new TestKey(0, parameters1));
159     assertThrows(AssertionError.class, tester::doTests);
160   }
161 
162   @Test
twoGroupsWithSameName_throws()163   public void twoGroupsWithSameName_throws() throws Exception {
164     KeyTester tester = new KeyTester().addEqualityGroup("MyGroup0", new TestKey(0));
165     assertThrows(AssertionError.class, () -> tester.addEqualityGroup("MyGroup0", new TestKey(0)));
166   }
167 
168   @Test
testIdRequirementTrue_isOk()169   public void testIdRequirementTrue_isOk() throws Exception {
170     TestParameters parameters =
171         new TestParameters(1) {
172           @Override
173           public boolean hasIdRequirement() {
174             return true;
175           }
176         };
177     new KeyTester().addEqualityGroup("", new TestKey(0, parameters)).doTests();
178   }
179 
180   @Test
testIdRequirementInconsistent_throws()181   public void testIdRequirementInconsistent_throws() throws Exception {
182     TestParameters parameters =
183         new TestParameters(1) {
184           @Override
185           public boolean hasIdRequirement() {
186             return true;
187           }
188         };
189     TestKey key =
190         new TestKey(10, parameters) {
191           @Override
192           @Nullable
193           public Integer getIdRequirementOrNull() {
194             return null;
195           }
196         };
197     KeyTester tester = new KeyTester().addEqualityGroup("", key);
198     assertThrows(AssertionError.class, tester::doTests);
199   }
200 
201   @Test
testIdRequirementInconsistent2_throws()202   public void testIdRequirementInconsistent2_throws() throws Exception {
203     TestParameters parameters =
204         new TestParameters(1) {
205           @Override
206           public boolean hasIdRequirement() {
207             return false;
208           }
209         };
210     TestKey key =
211         new TestKey(10, parameters) {
212           @Override
213           @Nullable
214           public Integer getIdRequirementOrNull() {
215             return 15;
216           }
217         };
218     KeyTester tester = new KeyTester().addEqualityGroup("", key);
219     assertThrows(AssertionError.class, tester::doTests);
220   }
221 }
222