16
16
17
17
package com .google .crypto .tink ;
18
18
19
+ import static com .google .common .truth .Truth .assertThat ;
19
20
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 ;
21
22
import static org .junit .Assert .assertThrows ;
22
- import static org .junit .Assert .assertTrue ;
23
23
import static org .junit .Assert .fail ;
24
24
25
+ import com .google .crypto .tink .internal .SlowInputStream ;
25
26
import com .google .crypto .tink .proto .KeyData ;
26
27
import com .google .crypto .tink .proto .KeyStatusType ;
27
28
import com .google .crypto .tink .proto .Keyset ;
28
29
import com .google .crypto .tink .proto .KeysetInfo ;
29
30
import com .google .crypto .tink .proto .OutputPrefixType ;
31
+ import com .google .crypto .tink .subtle .Random ;
30
32
import com .google .crypto .tink .testing .TestUtil ;
33
+ import java .io .ByteArrayInputStream ;
34
+ import java .io .InputStream ;
31
35
import java .security .GeneralSecurityException ;
32
36
import org .junit .Test ;
33
37
import org .junit .runner .RunWith ;
34
38
import org .junit .runners .JUnit4 ;
35
39
36
- // TODO(b/74251398): add tests for other functions.
37
40
/** Tests for Util. */
38
41
@ RunWith (JUnit4 .class )
39
42
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
+
40
99
@ Test
41
100
public void testValidateKeyset_shouldWork () throws Exception {
42
- String keyValue = "01234567890123456 " ;
101
+ String keyValue = "0123456789012345 " ;
43
102
Keyset keyset =
44
103
TestUtil .createKeyset (
45
104
TestUtil .createKey (
46
- TestUtil .createHmacKeyData (keyValue .getBytes ("UTF-8" ), 16 ),
105
+ TestUtil .createHmacKeyData (keyValue .getBytes (UTF_8 ), 16 ),
47
106
-42 ,
48
107
KeyStatusType .ENABLED ,
49
108
OutputPrefixType .TINK ));
@@ -58,23 +117,23 @@ public void testValidateKeyset_shouldWork() throws Exception {
58
117
public void testValidateKeyset_emptyKeyset_shouldFail () throws Exception {
59
118
GeneralSecurityException e =
60
119
assertThrows (
61
- GeneralSecurityException .class , () -> Util .validateKeyset (Keyset .newBuilder (). build ()));
120
+ GeneralSecurityException .class , () -> Util .validateKeyset (Keyset .getDefaultInstance ()));
62
121
assertExceptionContains (e , "keyset must contain at least one ENABLED key" );
63
122
}
64
123
65
124
@ Test
66
125
public void testValidateKeyset_multiplePrimaryKeys_shouldFail () throws Exception {
67
- String keyValue = "01234567890123456 " ;
126
+ String keyValue = "0123456789012345 " ;
68
127
// Multiple primary keys.
69
128
Keyset invalidKeyset =
70
129
TestUtil .createKeyset (
71
130
TestUtil .createKey (
72
- TestUtil .createHmacKeyData (keyValue .getBytes ("UTF-8" ), 16 ),
131
+ TestUtil .createHmacKeyData (keyValue .getBytes (UTF_8 ), 16 ),
73
132
42 ,
74
133
KeyStatusType .ENABLED ,
75
134
OutputPrefixType .TINK ),
76
135
TestUtil .createKey (
77
- TestUtil .createHmacKeyData (keyValue .getBytes ("UTF-8" ), 16 ),
136
+ TestUtil .createHmacKeyData (keyValue .getBytes (UTF_8 ), 16 ),
78
137
42 ,
79
138
KeyStatusType .ENABLED ,
80
139
OutputPrefixType .TINK ));
@@ -85,17 +144,17 @@ public void testValidateKeyset_multiplePrimaryKeys_shouldFail() throws Exception
85
144
86
145
@ Test
87
146
public void testValidateKeyset_primaryKeyIsDisabled_shouldFail () throws Exception {
88
- String keyValue = "01234567890123456 " ;
147
+ String keyValue = "0123456789012345 " ;
89
148
// Primary key is disabled.
90
149
Keyset invalidKeyset =
91
150
TestUtil .createKeyset (
92
151
TestUtil .createKey (
93
- TestUtil .createHmacKeyData (keyValue .getBytes ("UTF-8" ), 16 ),
152
+ TestUtil .createHmacKeyData (keyValue .getBytes (UTF_8 ), 16 ),
94
153
42 ,
95
154
KeyStatusType .DISABLED ,
96
155
OutputPrefixType .TINK ),
97
156
TestUtil .createKey (
98
- TestUtil .createHmacKeyData (keyValue .getBytes ("UTF-8" ), 16 ),
157
+ TestUtil .createHmacKeyData (keyValue .getBytes (UTF_8 ), 16 ),
99
158
43 ,
100
159
KeyStatusType .ENABLED ,
101
160
OutputPrefixType .TINK ));
@@ -106,17 +165,17 @@ public void testValidateKeyset_primaryKeyIsDisabled_shouldFail() throws Exceptio
106
165
107
166
@ Test
108
167
public void testValidateKeyset_noEnabledKey_shouldFail () throws Exception {
109
- String keyValue = "01234567890123456 " ;
168
+ String keyValue = "0123456789012345 " ;
110
169
// No ENABLED key.
111
170
Keyset invalidKeyset =
112
171
TestUtil .createKeyset (
113
172
TestUtil .createKey (
114
- TestUtil .createHmacKeyData (keyValue .getBytes ("UTF-8" ), 16 ),
173
+ TestUtil .createHmacKeyData (keyValue .getBytes (UTF_8 ), 16 ),
115
174
42 ,
116
175
KeyStatusType .DISABLED ,
117
176
OutputPrefixType .TINK ),
118
177
TestUtil .createKey (
119
- TestUtil .createHmacKeyData (keyValue .getBytes ("UTF-8" ), 16 ),
178
+ TestUtil .createHmacKeyData (keyValue .getBytes (UTF_8 ), 16 ),
120
179
42 ,
121
180
KeyStatusType .DESTROYED ,
122
181
OutputPrefixType .TINK ));
@@ -127,13 +186,13 @@ public void testValidateKeyset_noEnabledKey_shouldFail() throws Exception {
127
186
128
187
@ Test
129
188
public void testValidateKeyset_noPrimaryKey_shouldFail () throws Exception {
130
- String keyValue = "01234567890123456 " ;
189
+ String keyValue = "0123456789012345 " ;
131
190
// No primary key.
132
191
Keyset invalidKeyset =
133
192
Keyset .newBuilder ()
134
193
.addKey (
135
194
Keyset .Key .newBuilder ()
136
- .setKeyData (TestUtil .createHmacKeyData (keyValue .getBytes ("UTF-8" ), 16 ))
195
+ .setKeyData (TestUtil .createHmacKeyData (keyValue .getBytes (UTF_8 ), 16 ))
137
196
.setKeyId (1 )
138
197
.setStatus (KeyStatusType .ENABLED )
139
198
.setOutputPrefixType (OutputPrefixType .TINK )
@@ -154,7 +213,7 @@ public void testValidateKeyset_noPrimaryKey_keysetContainsOnlyPublicKeys_shouldW
154
213
Keyset .Key .newBuilder ()
155
214
.setKeyData (
156
215
TestUtil .createKeyData (
157
- KeyData .newBuilder (). build (),
216
+ KeyData .getDefaultInstance (),
158
217
"typeUrl" ,
159
218
KeyData .KeyMaterialType .ASYMMETRIC_PUBLIC ))
160
219
.setKeyId (1 )
@@ -171,16 +230,16 @@ public void testValidateKeyset_noPrimaryKey_keysetContainsOnlyPublicKeys_shouldW
171
230
172
231
@ Test
173
232
public void testValidateKeyset_withDestroyedKey_shouldWork () throws Exception {
174
- String keyValue = "01234567890123456 " ;
233
+ String keyValue = "0123456789012345 " ;
175
234
Keyset validKeyset =
176
235
TestUtil .createKeyset (
177
236
TestUtil .createKey (
178
- TestUtil .createHmacKeyData (keyValue .getBytes ("UTF-8" ), 16 ),
237
+ TestUtil .createHmacKeyData (keyValue .getBytes (UTF_8 ), 16 ),
179
238
42 ,
180
239
KeyStatusType .ENABLED ,
181
240
OutputPrefixType .TINK ),
182
241
TestUtil .createKey (
183
- TestUtil .createHmacKeyData (keyValue .getBytes ("UTF-8" ), 16 ),
242
+ TestUtil .createHmacKeyData (keyValue .getBytes (UTF_8 ), 16 ),
184
243
42 ,
185
244
KeyStatusType .DESTROYED ,
186
245
OutputPrefixType .TINK ));
@@ -191,21 +250,94 @@ public void testValidateKeyset_withDestroyedKey_shouldWork() throws Exception {
191
250
}
192
251
}
193
252
194
- /** Tests that getKeysetInfo doesn't contain key material. */
195
253
@ Test
196
- public void testGetKeysetInfo () throws Exception {
197
- String keyValue = "01234567890123456 " ;
254
+ public void testValidateKeyset_withUnknownStatusKey_works () throws Exception {
255
+ String keyValue = "0123456789012345 " ;
198
256
Keyset keyset =
199
257
TestUtil .createKeyset (
258
+ /* primary= */ TestUtil .createKey (
259
+ TestUtil .createHmacKeyData (keyValue .getBytes (UTF_8 ), 16 ),
260
+ 42 ,
261
+ KeyStatusType .ENABLED ,
262
+ OutputPrefixType .TINK ),
200
263
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 ),
202
334
42 ,
203
335
KeyStatusType .ENABLED ,
204
336
OutputPrefixType .TINK ));
205
- assertTrue (keyset .toString ().contains (keyValue ) );
337
+ assertThat (keyset .toString ()) .contains (keyValue );
206
338
207
339
KeysetInfo keysetInfo = Util .getKeysetInfo (keyset );
208
- assertFalse (keysetInfo .toString (). contains (keyValue ) );
340
+ assertThat (keysetInfo .toString ()). doesNotContain (keyValue );
209
341
}
210
342
211
343
@ Test
@@ -219,4 +351,20 @@ public void testAssertExceptionContains() throws Exception {
219
351
e , "Got exception with message \" abc\" , expected it to contain \" def\" ." );
220
352
}
221
353
}
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
+ }
222
370
}
0 commit comments