Skip to content

Commit 7b4b616

Browse files
committed
KeyBlobTest: don't use random bytes to make strings; test lengths with max unsigned short value
1 parent 8552c32 commit 7b4b616

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static org.junit.Assert.assertEquals;
1818

1919
import java.nio.charset.StandardCharsets;
20+
import java.util.Arrays;
2021
import java.util.HashMap;
2122
import java.util.Map;
2223

@@ -173,13 +174,9 @@ public void checkKeyLen() {
173174
}
174175

175176
private KeyBlob generateRandomKeyBlob(int idLen, int infoLen, int keyLen) {
176-
final byte[] idBytes = RandomBytesGenerator.generate(idLen);
177-
// negative bytes translate into U+FFFD, so no thanks
178-
for (int i = 0; i < idBytes.length; i++) {
179-
if (idBytes[i] < 0) {
180-
idBytes[i] = (byte) (idBytes[i] - Byte.MIN_VALUE);
181-
}
182-
}
177+
final byte[] idBytes = new byte[idLen];
178+
Arrays.fill(idBytes, (byte) 'A');
179+
183180
final byte[] infoBytes = RandomBytesGenerator.generate(infoLen);
184181
final byte[] keyBytes = RandomBytesGenerator.generate(keyLen);
185182

@@ -196,7 +193,7 @@ private void assertKeyBlobsEqual(KeyBlob b1, KeyBlob b2) {
196193
@Test
197194
public void checkKeyProviderIdLenUnsigned() {
198195
// provider id length is too large for a signed short but fits in unsigned
199-
final KeyBlob blob = generateRandomKeyBlob(Short.MAX_VALUE + 1, Short.MAX_VALUE, Short.MAX_VALUE);
196+
final KeyBlob blob = generateRandomKeyBlob(Constants.UNSIGNED_SHORT_MAX_VAL, Short.MAX_VALUE, Short.MAX_VALUE);
200197
final byte[] arr = blob.toByteArray();
201198

202199
assertKeyBlobsEqual(deserialize(arr), blob);
@@ -205,7 +202,7 @@ public void checkKeyProviderIdLenUnsigned() {
205202
@Test
206203
public void checkKeyProviderInfoLenUnsigned() {
207204
// provider info length is too large for a signed short but fits in unsigned
208-
final KeyBlob blob = generateRandomKeyBlob(Short.MAX_VALUE, Short.MAX_VALUE + 2, Short.MAX_VALUE);
205+
final KeyBlob blob = generateRandomKeyBlob(Short.MAX_VALUE, Constants.UNSIGNED_SHORT_MAX_VAL, Short.MAX_VALUE);
209206
final byte[] arr = blob.toByteArray();
210207

211208
assertKeyBlobsEqual(deserialize(arr), blob);
@@ -214,7 +211,7 @@ public void checkKeyProviderInfoLenUnsigned() {
214211
@Test
215212
public void checkKeyLenUnsigned() {
216213
// key length is too large for a signed short but fits in unsigned
217-
final KeyBlob blob = generateRandomKeyBlob(Short.MAX_VALUE, Short.MAX_VALUE, Short.MAX_VALUE + 3);
214+
final KeyBlob blob = generateRandomKeyBlob(Short.MAX_VALUE, Short.MAX_VALUE, Constants.UNSIGNED_SHORT_MAX_VAL);
218215
final byte[] arr = blob.toByteArray();
219216

220217
assertKeyBlobsEqual(deserialize(arr), blob);

0 commit comments

Comments
 (0)