Skip to content

Commit 468625d

Browse files
chore: Add back removed CiphertextHeaders.deserialize method (#383)
1 parent d59c11c commit 468625d

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

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

+33-2
Original file line numberDiff line numberDiff line change
@@ -519,15 +519,36 @@ private int parseComplete(final byte[] b, final int off) throws ParseException {
519519
return 0;
520520
}
521521

522+
/**
523+
* Deserialize the provided bytes starting at the specified offset to
524+
* construct an instance of this class. Uses the default value for
525+
* maxEncryptedDataKeys, which results in no limit.
526+
*
527+
* <p>
528+
* This method parses the provided bytes for the individual fields in this
529+
* class. This methods also supports partial parsing where not all the bytes
530+
* required for parsing the fields successfully are available.
531+
*
532+
* @param b
533+
* the byte array to deserialize.
534+
* @param off
535+
* the offset in the byte array to use for deserialization.
536+
* @return
537+
* the number of bytes consumed in deserialization.
538+
*/
539+
public int deserialize(final byte[] b, final int off) throws ParseException {
540+
return deserialize(b, off, NO_MAX_ENCRYPTED_DATA_KEYS);
541+
}
542+
522543
/**
523544
* Deserialize the provided bytes starting at the specified offset to
524545
* construct an instance of this class.
525-
*
546+
*
526547
* <p>
527548
* This method parses the provided bytes for the individual fields in this
528549
* class. This methods also supports partial parsing where not all the bytes
529550
* required for parsing the fields successfully are available.
530-
*
551+
*
531552
* @param b
532553
* the byte array to deserialize.
533554
* @param off
@@ -869,6 +890,16 @@ public void setSuiteData(byte[] suiteData) {
869890
suiteData_ = suiteData.clone();
870891
}
871892

893+
/**
894+
* Return max encrypted data keys
895+
* Package scope for unit testing.
896+
*
897+
* @return int
898+
*/
899+
int getMaxEncryptedDataKeys() {
900+
return maxEncryptedDataKeys_;
901+
}
902+
872903
private static class PartialParseException extends Exception {
873904
private static final long serialVersionUID = 1L;
874905
final int bytesParsed_;

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

+22-1
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,9 +61,28 @@ public void serializeDeserialize() {
5961

6062
final byte[] headerBytes = ciphertextHeaders.toByteArray();
6163
final CiphertextHeaders reconstructedHeaders = new CiphertextHeaders();
62-
reconstructedHeaders.deserialize(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);
6383
final byte[] reconstructedHeaderBytes = reconstructedHeaders.toByteArray();
6484

85+
assertEquals(reconstructedHeaders.getMaxEncryptedDataKeys(), CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
6586
assertArrayEquals(headerBytes, reconstructedHeaderBytes);
6687
}
6788
}

0 commit comments

Comments
 (0)