Skip to content

Commit c1f3e63

Browse files
chore: Add back removed CiphertextHeaders.deserialize method (#382)
1 parent 69e7914 commit c1f3e63

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/main/java/com/amazonaws/encryptionsdk/model/CiphertextHeaders.java

+26-1
Original file line numberDiff line numberDiff line change
@@ -503,11 +503,27 @@ private int parseComplete(final byte[] b, final int off) throws ParseException {
503503
return 0;
504504
}
505505

506+
/**
507+
* Deserialize the provided bytes starting at the specified offset to construct an instance of
508+
* this class. Uses the default value for maxEncryptedDataKeys, which results in no limit.
509+
*
510+
* <p>This method parses the provided bytes for the individual fields in this class. This method
511+
* also supports partial parsing where not all the bytes required for parsing the fields
512+
* successfully are available.
513+
*
514+
* @param b the byte array to deserialize.
515+
* @param off the offset in the byte array to use for deserialization.
516+
* @return the number of bytes consumed in deserialization.
517+
*/
518+
public int deserialize(final byte[] b, final int off) throws ParseException {
519+
return deserialize(b, off, NO_MAX_ENCRYPTED_DATA_KEYS);
520+
}
521+
506522
/**
507523
* Deserialize the provided bytes starting at the specified offset to construct an instance of
508524
* this class.
509525
*
510-
* <p>This method parses the provided bytes for the individual fields in this class. This methods
526+
* <p>This method parses the provided bytes for the individual fields in this class. This method
511527
* also supports partial parsing where not all the bytes required for parsing the fields
512528
* successfully are available.
513529
*
@@ -835,6 +851,15 @@ public void setSuiteData(byte[] suiteData) {
835851
suiteData_ = suiteData.clone();
836852
}
837853

854+
/**
855+
* Return max encrypted data keys. Package scope for unit testing.
856+
*
857+
* @return int
858+
*/
859+
int getMaxEncryptedDataKeys() {
860+
return maxEncryptedDataKeys_;
861+
}
862+
838863
private static class PartialParseException extends Exception {
839864
private static final long serialVersionUID = 1L;
840865
final int bytesParsed_;

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

+24-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public class CiphertextHeadersTest {
5151

5252
@Test
5353
public void serializeDeserialize() {
54+
int maxEncryptedDataKeys = 42;
55+
5456
Map<String, String> encryptionContext = new HashMap<String, String>(1);
5557
encryptionContext.put("ENC", "CiphertextHeader Test");
5658

@@ -59,10 +61,30 @@ public void serializeDeserialize() {
5961

6062
final byte[] headerBytes = ciphertextHeaders.toByteArray();
6163
final CiphertextHeaders reconstructedHeaders = new CiphertextHeaders();
62-
reconstructedHeaders.deserialize(
63-
headerBytes, 0, CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
64+
reconstructedHeaders.deserialize(headerBytes, 0, maxEncryptedDataKeys);
65+
final byte[] reconstructedHeaderBytes = reconstructedHeaders.toByteArray();
66+
67+
assertEquals(reconstructedHeaders.getMaxEncryptedDataKeys(), maxEncryptedDataKeys);
68+
assertArrayEquals(headerBytes, reconstructedHeaderBytes);
69+
}
70+
}
71+
72+
@Test
73+
public void serializeDeserializeDefaultMaxEncryptedDataKeys() {
74+
Map<String, String> encryptionContext = new HashMap<String, String>(1);
75+
encryptionContext.put("ENC", "CiphertextHeader Test");
76+
77+
for (CryptoAlgorithm alg : testAlgs) {
78+
final CiphertextHeaders ciphertextHeaders = createCiphertextHeaders(encryptionContext, alg);
79+
80+
final byte[] headerBytes = ciphertextHeaders.toByteArray();
81+
final CiphertextHeaders reconstructedHeaders = new CiphertextHeaders();
82+
reconstructedHeaders.deserialize(headerBytes, 0);
6483
final byte[] reconstructedHeaderBytes = reconstructedHeaders.toByteArray();
6584

85+
assertEquals(
86+
reconstructedHeaders.getMaxEncryptedDataKeys(),
87+
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
6688
assertArrayEquals(headerBytes, reconstructedHeaderBytes);
6789
}
6890
}

0 commit comments

Comments
 (0)