Skip to content

Commit f3bdc18

Browse files
committed
Add test vectors for KeyBlob negative field tests
1 parent 0f7f1e5 commit f3bdc18

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

src/test/java/com/amazonaws/encryptionsdk/model/KeyBlobTest.java

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,40 +173,65 @@ public void checkKeyLen() {
173173
assertEquals(mockDataKey_.getEncryptedDataKey().length, reconstructedKeyBlob.getEncryptedDataKeyLen());
174174
}
175175

176+
private byte[] negativeKeyProviderIdLenTestVector() {
177+
// key provider id len of -1, key provider info len of 2, and key len of 3
178+
return new byte[]{
179+
(byte)0xff, (byte)0xff, (byte)0x01, (byte)0x00, (byte)0x02, (byte)0x02, (byte)0x03,
180+
(byte)0x00, (byte)0x03, (byte)0x04, (byte)0x05, (byte)0x06
181+
};
182+
}
183+
184+
private byte[] negativeKeyProviderInfoLenTestVector() {
185+
// key provider id len of 1, key provider info len of -2, key len of 3
186+
return new byte[] {
187+
(byte)0x00, (byte)0x01, (byte)0x01, (byte)0xff, (byte)0xfe, (byte)0x02, (byte)0x03,
188+
(byte)0x00, (byte)0x03, (byte)0x04, (byte)0x05, (byte)0x06
189+
};
190+
}
191+
192+
private byte[] negativeKeyLenTestVector() {
193+
// key provider id len of 1, key provider info len of 2, key len of -3
194+
return new byte[] {
195+
(byte)0x00, (byte)0x01, (byte)0x01, (byte)0x00, (byte)0x00, (byte)0x02, (byte)0x03,
196+
(byte)0xff, (byte)0xfd, (byte)0x04, (byte)0x05, (byte)0x06
197+
};
198+
}
199+
200+
private void assertIncomplete(final byte[] vector) {
201+
assertFalse(deserialize(vector).isComplete());
202+
}
203+
176204
@Test
177205
public void checkNegativeKeyProviderIdLen() {
178-
final KeyBlob reconstruct = new KeyBlob();
179206
final byte[] keyBlobBytes = createKeyBlobBytes();
180207

181-
// we will manually set the keyProviderIdLen to negative
208+
// manually set the keyProviderIdLen to negative
182209
final byte[] negativeKeyProviderIdLen = ByteBuffer.allocate(Short.BYTES)
183210
.putShort((short) -1).array();
184211
System.arraycopy(negativeKeyProviderIdLen, 0, keyBlobBytes, 0, Short.BYTES);
185212

186-
reconstruct.deserialize(keyBlobBytes, 0);
187-
// negative key provider id len throws parse exception so deserialization is incomplete
188-
assertFalse(reconstruct.isComplete());
213+
// a negative field length throws a parse exception, so deserialization is incomplete
214+
assertIncomplete(keyBlobBytes);
215+
assertIncomplete(negativeKeyProviderIdLenTestVector());
189216
}
190217

191218
@Test
192219
public void checkNegativeKeyProviderInfoLen() {
193-
final KeyBlob reconstruct = new KeyBlob();
194220
final byte[] keyBlobBytes = createKeyBlobBytes();
195221

196-
// we will manually set the keyProviderInfoLen to negative
222+
// manually set the keyProviderInfoLen to negative
197223
final byte[] negativeKeyProviderInfoLen = ByteBuffer.allocate(Short.BYTES)
198224
.putShort((short) -1).array();
199225
int offset = Short.BYTES + providerId_.length();
200226
System.arraycopy(negativeKeyProviderInfoLen, 0, keyBlobBytes, offset, Short.BYTES);
201227

202-
reconstruct.deserialize(keyBlobBytes, 0);
203-
// negative key provider info len throws parse exception so deserialization is incomplete
204-
assertFalse(reconstruct.isComplete());
228+
// a negative field length throws a parse exception, so deserialization is incomplete
229+
assertIncomplete(keyBlobBytes);
230+
assertIncomplete(negativeKeyProviderInfoLenTestVector());
205231
}
206232

207233
@Test
208234
public void checkNegativeKeyLen() {
209-
final KeyBlob reconstruct = new KeyBlob();
210235
final byte[] keyBlobBytes = createKeyBlobBytes();
211236

212237
// we will manually set the keyLen to negative
@@ -215,8 +240,8 @@ public void checkNegativeKeyLen() {
215240
int offset = Short.BYTES + providerId_.length() + Short.BYTES + providerInfo_.length();
216241
System.arraycopy(negativeKeyLen, 0, keyBlobBytes, offset, Short.BYTES);
217242

218-
reconstruct.deserialize(keyBlobBytes, 0);
219243
// negative key len throws parse exception so deserialization is incomplete
220-
assertFalse(reconstruct.isComplete());
244+
assertIncomplete(keyBlobBytes);
245+
assertIncomplete(negativeKeyLenTestVector());
221246
}
222247
}

0 commit comments

Comments
 (0)