Skip to content
This repository was archived by the owner on Apr 17, 2024. It is now read-only.

Commit da751cb

Browse files
juergwcopybara-github
authored andcommitted
Add more unit tests for Util.java.
Also, fix some lint warnings. PiperOrigin-RevId: 621605961
1 parent 29fc855 commit da751cb

File tree

2 files changed

+178
-27
lines changed

2 files changed

+178
-27
lines changed

java_src/src/test/java/com/google/crypto/tink/BUILD.bazel

+3
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,10 @@ java_test(
227227
deps = [
228228
"//proto:tink_java_proto",
229229
"//src/main/java/com/google/crypto/tink:util",
230+
"//src/main/java/com/google/crypto/tink/internal:slow_input_stream",
231+
"//src/main/java/com/google/crypto/tink/subtle:random",
230232
"//src/main/java/com/google/crypto/tink/testing:test_util",
233+
"@maven//:com_google_truth_truth",
231234
"@maven//:junit_junit",
232235
],
233236
)

java_src/src/test/java/com/google/crypto/tink/UtilTest.java

+175-27
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,93 @@
1616

1717
package com.google.crypto.tink;
1818

19+
import static com.google.common.truth.Truth.assertThat;
1920
import static com.google.crypto.tink.testing.TestUtil.assertExceptionContains;
20-
import static org.junit.Assert.assertFalse;
21+
import static java.nio.charset.StandardCharsets.UTF_8;
2122
import static org.junit.Assert.assertThrows;
22-
import static org.junit.Assert.assertTrue;
2323
import static org.junit.Assert.fail;
2424

25+
import com.google.crypto.tink.internal.SlowInputStream;
2526
import com.google.crypto.tink.proto.KeyData;
2627
import com.google.crypto.tink.proto.KeyStatusType;
2728
import com.google.crypto.tink.proto.Keyset;
2829
import com.google.crypto.tink.proto.KeysetInfo;
2930
import com.google.crypto.tink.proto.OutputPrefixType;
31+
import com.google.crypto.tink.subtle.Random;
3032
import com.google.crypto.tink.testing.TestUtil;
33+
import java.io.ByteArrayInputStream;
34+
import java.io.InputStream;
3135
import java.security.GeneralSecurityException;
3236
import org.junit.Test;
3337
import org.junit.runner.RunWith;
3438
import org.junit.runners.JUnit4;
3539

36-
// TODO(b/74251398): add tests for other functions.
3740
/** Tests for Util. */
3841
@RunWith(JUnit4.class)
3942
public class UtilTest {
43+
44+
@Test
45+
public void testValidateKey_success() throws Exception {
46+
String keyValue = "0123456789012345";
47+
Keyset.Key key =
48+
TestUtil.createKey(
49+
TestUtil.createHmacKeyData(keyValue.getBytes(UTF_8), 16),
50+
42,
51+
KeyStatusType.ENABLED,
52+
OutputPrefixType.TINK);
53+
Util.validateKey(key);
54+
}
55+
56+
@Test
57+
public void testValidateKey_emptyKeyData_success() throws Exception {
58+
Keyset.Key key =
59+
TestUtil.createKey(
60+
KeyData.getDefaultInstance(), 42, KeyStatusType.ENABLED, OutputPrefixType.TINK);
61+
Util.validateKey(key);
62+
}
63+
64+
@Test
65+
public void testValidateKey_noKeyData_fails() throws Exception {
66+
Keyset.Key key =
67+
Keyset.Key.newBuilder()
68+
.setStatus(KeyStatusType.ENABLED)
69+
.setKeyId(42)
70+
.setOutputPrefixType(OutputPrefixType.TINK)
71+
.build();
72+
assertThrows(GeneralSecurityException.class, () -> Util.validateKey(key));
73+
}
74+
75+
@Test
76+
public void testValidateKey_unknownPrefix_fails() throws Exception {
77+
String keyValue = "0123456789012345";
78+
Keyset.Key key =
79+
TestUtil.createKey(
80+
TestUtil.createHmacKeyData(keyValue.getBytes(UTF_8), 16),
81+
42,
82+
KeyStatusType.ENABLED,
83+
OutputPrefixType.UNKNOWN_PREFIX);
84+
assertThrows(GeneralSecurityException.class, () -> Util.validateKey(key));
85+
}
86+
87+
@Test
88+
public void testValidateKey_unknownStatus_fails() throws Exception {
89+
String keyValue = "0123456789012345";
90+
Keyset.Key key =
91+
TestUtil.createKey(
92+
TestUtil.createHmacKeyData(keyValue.getBytes(UTF_8), 16),
93+
42,
94+
KeyStatusType.UNKNOWN_STATUS,
95+
OutputPrefixType.TINK);
96+
assertThrows(GeneralSecurityException.class, () -> Util.validateKey(key));
97+
}
98+
4099
@Test
41100
public void testValidateKeyset_shouldWork() throws Exception {
42-
String keyValue = "01234567890123456";
101+
String keyValue = "0123456789012345";
43102
Keyset keyset =
44103
TestUtil.createKeyset(
45104
TestUtil.createKey(
46-
TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16),
105+
TestUtil.createHmacKeyData(keyValue.getBytes(UTF_8), 16),
47106
-42,
48107
KeyStatusType.ENABLED,
49108
OutputPrefixType.TINK));
@@ -58,23 +117,23 @@ public void testValidateKeyset_shouldWork() throws Exception {
58117
public void testValidateKeyset_emptyKeyset_shouldFail() throws Exception {
59118
GeneralSecurityException e =
60119
assertThrows(
61-
GeneralSecurityException.class, () -> Util.validateKeyset(Keyset.newBuilder().build()));
120+
GeneralSecurityException.class, () -> Util.validateKeyset(Keyset.getDefaultInstance()));
62121
assertExceptionContains(e, "keyset must contain at least one ENABLED key");
63122
}
64123

65124
@Test
66125
public void testValidateKeyset_multiplePrimaryKeys_shouldFail() throws Exception {
67-
String keyValue = "01234567890123456";
126+
String keyValue = "0123456789012345";
68127
// Multiple primary keys.
69128
Keyset invalidKeyset =
70129
TestUtil.createKeyset(
71130
TestUtil.createKey(
72-
TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16),
131+
TestUtil.createHmacKeyData(keyValue.getBytes(UTF_8), 16),
73132
42,
74133
KeyStatusType.ENABLED,
75134
OutputPrefixType.TINK),
76135
TestUtil.createKey(
77-
TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16),
136+
TestUtil.createHmacKeyData(keyValue.getBytes(UTF_8), 16),
78137
42,
79138
KeyStatusType.ENABLED,
80139
OutputPrefixType.TINK));
@@ -85,17 +144,17 @@ public void testValidateKeyset_multiplePrimaryKeys_shouldFail() throws Exception
85144

86145
@Test
87146
public void testValidateKeyset_primaryKeyIsDisabled_shouldFail() throws Exception {
88-
String keyValue = "01234567890123456";
147+
String keyValue = "0123456789012345";
89148
// Primary key is disabled.
90149
Keyset invalidKeyset =
91150
TestUtil.createKeyset(
92151
TestUtil.createKey(
93-
TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16),
152+
TestUtil.createHmacKeyData(keyValue.getBytes(UTF_8), 16),
94153
42,
95154
KeyStatusType.DISABLED,
96155
OutputPrefixType.TINK),
97156
TestUtil.createKey(
98-
TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16),
157+
TestUtil.createHmacKeyData(keyValue.getBytes(UTF_8), 16),
99158
43,
100159
KeyStatusType.ENABLED,
101160
OutputPrefixType.TINK));
@@ -106,17 +165,17 @@ public void testValidateKeyset_primaryKeyIsDisabled_shouldFail() throws Exceptio
106165

107166
@Test
108167
public void testValidateKeyset_noEnabledKey_shouldFail() throws Exception {
109-
String keyValue = "01234567890123456";
168+
String keyValue = "0123456789012345";
110169
// No ENABLED key.
111170
Keyset invalidKeyset =
112171
TestUtil.createKeyset(
113172
TestUtil.createKey(
114-
TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16),
173+
TestUtil.createHmacKeyData(keyValue.getBytes(UTF_8), 16),
115174
42,
116175
KeyStatusType.DISABLED,
117176
OutputPrefixType.TINK),
118177
TestUtil.createKey(
119-
TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16),
178+
TestUtil.createHmacKeyData(keyValue.getBytes(UTF_8), 16),
120179
42,
121180
KeyStatusType.DESTROYED,
122181
OutputPrefixType.TINK));
@@ -127,13 +186,13 @@ public void testValidateKeyset_noEnabledKey_shouldFail() throws Exception {
127186

128187
@Test
129188
public void testValidateKeyset_noPrimaryKey_shouldFail() throws Exception {
130-
String keyValue = "01234567890123456";
189+
String keyValue = "0123456789012345";
131190
// No primary key.
132191
Keyset invalidKeyset =
133192
Keyset.newBuilder()
134193
.addKey(
135194
Keyset.Key.newBuilder()
136-
.setKeyData(TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16))
195+
.setKeyData(TestUtil.createHmacKeyData(keyValue.getBytes(UTF_8), 16))
137196
.setKeyId(1)
138197
.setStatus(KeyStatusType.ENABLED)
139198
.setOutputPrefixType(OutputPrefixType.TINK)
@@ -154,7 +213,7 @@ public void testValidateKeyset_noPrimaryKey_keysetContainsOnlyPublicKeys_shouldW
154213
Keyset.Key.newBuilder()
155214
.setKeyData(
156215
TestUtil.createKeyData(
157-
KeyData.newBuilder().build(),
216+
KeyData.getDefaultInstance(),
158217
"typeUrl",
159218
KeyData.KeyMaterialType.ASYMMETRIC_PUBLIC))
160219
.setKeyId(1)
@@ -171,16 +230,16 @@ public void testValidateKeyset_noPrimaryKey_keysetContainsOnlyPublicKeys_shouldW
171230

172231
@Test
173232
public void testValidateKeyset_withDestroyedKey_shouldWork() throws Exception {
174-
String keyValue = "01234567890123456";
233+
String keyValue = "0123456789012345";
175234
Keyset validKeyset =
176235
TestUtil.createKeyset(
177236
TestUtil.createKey(
178-
TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16),
237+
TestUtil.createHmacKeyData(keyValue.getBytes(UTF_8), 16),
179238
42,
180239
KeyStatusType.ENABLED,
181240
OutputPrefixType.TINK),
182241
TestUtil.createKey(
183-
TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16),
242+
TestUtil.createHmacKeyData(keyValue.getBytes(UTF_8), 16),
184243
42,
185244
KeyStatusType.DESTROYED,
186245
OutputPrefixType.TINK));
@@ -191,21 +250,94 @@ public void testValidateKeyset_withDestroyedKey_shouldWork() throws Exception {
191250
}
192251
}
193252

194-
/** Tests that getKeysetInfo doesn't contain key material. */
195253
@Test
196-
public void testGetKeysetInfo() throws Exception {
197-
String keyValue = "01234567890123456";
254+
public void testValidateKeyset_withUnknownStatusKey_works() throws Exception {
255+
String keyValue = "0123456789012345";
198256
Keyset keyset =
199257
TestUtil.createKeyset(
258+
/* primary= */ TestUtil.createKey(
259+
TestUtil.createHmacKeyData(keyValue.getBytes(UTF_8), 16),
260+
42,
261+
KeyStatusType.ENABLED,
262+
OutputPrefixType.TINK),
200263
TestUtil.createKey(
201-
TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16),
264+
TestUtil.createHmacKeyData(keyValue.getBytes(UTF_8), 16),
265+
123,
266+
KeyStatusType.UNKNOWN_STATUS,
267+
OutputPrefixType.TINK));
268+
Util.validateKeyset(keyset);
269+
}
270+
271+
@Test
272+
public void testGetKeyInfo_works() throws Exception {
273+
String keyValue = "0123456789012345";
274+
Keyset.Key key =
275+
TestUtil.createKey(
276+
TestUtil.createHmacKeyData(keyValue.getBytes(UTF_8), 16),
277+
42,
278+
KeyStatusType.ENABLED,
279+
OutputPrefixType.TINK);
280+
KeysetInfo.KeyInfo keyInfo = Util.getKeyInfo(key);
281+
assertThat(keyInfo)
282+
.isEqualTo(
283+
KeysetInfo.KeyInfo.newBuilder()
284+
.setTypeUrl("type.googleapis.com/google.crypto.tink.HmacKey")
285+
.setStatus(KeyStatusType.ENABLED)
286+
.setOutputPrefixType(OutputPrefixType.TINK)
287+
.setKeyId(42)
288+
.build());
289+
}
290+
291+
@Test
292+
public void testGetKeysetInfo_works() throws Exception {
293+
Keyset keyset =
294+
TestUtil.createKeyset(
295+
/* primary= */ TestUtil.createKey(
296+
TestUtil.createHmacKeyData("0123456789012345".getBytes(UTF_8), 16),
297+
42,
298+
KeyStatusType.ENABLED,
299+
OutputPrefixType.TINK),
300+
TestUtil.createKey(
301+
TestUtil.createHmacKeyData("1234567890123456".getBytes(UTF_8), 16),
302+
123,
303+
KeyStatusType.DISABLED,
304+
OutputPrefixType.RAW));
305+
KeysetInfo keysetInfo = Util.getKeysetInfo(keyset);
306+
assertThat(keysetInfo)
307+
.isEqualTo(
308+
KeysetInfo.newBuilder()
309+
.setPrimaryKeyId(42)
310+
.addKeyInfo(
311+
KeysetInfo.KeyInfo.newBuilder()
312+
.setTypeUrl("type.googleapis.com/google.crypto.tink.HmacKey")
313+
.setStatus(KeyStatusType.ENABLED)
314+
.setOutputPrefixType(OutputPrefixType.TINK)
315+
.setKeyId(42)
316+
.build())
317+
.addKeyInfo(
318+
KeysetInfo.KeyInfo.newBuilder()
319+
.setTypeUrl("type.googleapis.com/google.crypto.tink.HmacKey")
320+
.setStatus(KeyStatusType.DISABLED)
321+
.setOutputPrefixType(OutputPrefixType.RAW)
322+
.setKeyId(123)
323+
.build())
324+
.build());
325+
}
326+
327+
@Test
328+
public void testGetKeysetInfo_doesNotContainKeyMaterial() throws Exception {
329+
String keyValue = "0123456789012345";
330+
Keyset keyset =
331+
TestUtil.createKeyset(
332+
TestUtil.createKey(
333+
TestUtil.createHmacKeyData(keyValue.getBytes(UTF_8), 16),
202334
42,
203335
KeyStatusType.ENABLED,
204336
OutputPrefixType.TINK));
205-
assertTrue(keyset.toString().contains(keyValue));
337+
assertThat(keyset.toString()).contains(keyValue);
206338

207339
KeysetInfo keysetInfo = Util.getKeysetInfo(keyset);
208-
assertFalse(keysetInfo.toString().contains(keyValue));
340+
assertThat(keysetInfo.toString()).doesNotContain(keyValue);
209341
}
210342

211343
@Test
@@ -219,4 +351,20 @@ public void testAssertExceptionContains() throws Exception {
219351
e, "Got exception with message \"abc\", expected it to contain \"def\".");
220352
}
221353
}
354+
355+
@Test
356+
public void testReadAll() throws Exception {
357+
byte[] input = Random.randBytes(2000);
358+
InputStream stream = new ByteArrayInputStream(input);
359+
byte[] output = Util.readAll(stream);
360+
assertThat(output).isEqualTo(input);
361+
}
362+
363+
@Test
364+
public void testReadAllWithSlowInputStream() throws Exception {
365+
byte[] input = Random.randBytes(2000);
366+
InputStream stream = SlowInputStream.copyFrom(input);
367+
byte[] output = Util.readAll(stream);
368+
assertThat(output).isEqualTo(input);
369+
}
222370
}

0 commit comments

Comments
 (0)