Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add back removed CiphertextHeaders.deserialize method #382

Merged
merged 2 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -503,11 +503,27 @@ private int parseComplete(final byte[] b, final int off) throws ParseException {
return 0;
}

/**
* Deserialize the provided bytes starting at the specified offset to construct an instance of
* this class. Uses the default value for maxEncryptedDataKeys, which results in no limit.
*
* <p>This method parses the provided bytes for the individual fields in this class. This method
* also supports partial parsing where not all the bytes required for parsing the fields
* successfully are available.
*
* @param b the byte array to deserialize.
* @param off the offset in the byte array to use for deserialization.
* @return the number of bytes consumed in deserialization.
*/
public int deserialize(final byte[] b, final int off) throws ParseException {
return deserialize(b, off, NO_MAX_ENCRYPTED_DATA_KEYS);
}

/**
* Deserialize the provided bytes starting at the specified offset to construct an instance of
* this class.
*
* <p>This method parses the provided bytes for the individual fields in this class. This methods
* <p>This method parses the provided bytes for the individual fields in this class. This method
* also supports partial parsing where not all the bytes required for parsing the fields
* successfully are available.
*
Expand Down Expand Up @@ -835,6 +851,15 @@ public void setSuiteData(byte[] suiteData) {
suiteData_ = suiteData.clone();
}

/**
* Return max encrypted data keys. Package scope for unit testing.
*
* @return int
*/
int getMaxEncryptedDataKeys() {
return maxEncryptedDataKeys_;
}

private static class PartialParseException extends Exception {
private static final long serialVersionUID = 1L;
final int bytesParsed_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public class CiphertextHeadersTest {

@Test
public void serializeDeserialize() {
int maxEncryptedDataKeys = 42;

Map<String, String> encryptionContext = new HashMap<String, String>(1);
encryptionContext.put("ENC", "CiphertextHeader Test");

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

final byte[] headerBytes = ciphertextHeaders.toByteArray();
final CiphertextHeaders reconstructedHeaders = new CiphertextHeaders();
reconstructedHeaders.deserialize(
headerBytes, 0, CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
reconstructedHeaders.deserialize(headerBytes, 0, maxEncryptedDataKeys);
final byte[] reconstructedHeaderBytes = reconstructedHeaders.toByteArray();

assertEquals(reconstructedHeaders.getMaxEncryptedDataKeys(), maxEncryptedDataKeys);
assertArrayEquals(headerBytes, reconstructedHeaderBytes);
}
}

@Test
public void serializeDeserializeDefaultMaxEncryptedDataKeys() {
Map<String, String> encryptionContext = new HashMap<String, String>(1);
encryptionContext.put("ENC", "CiphertextHeader Test");

for (CryptoAlgorithm alg : testAlgs) {
final CiphertextHeaders ciphertextHeaders = createCiphertextHeaders(encryptionContext, alg);

final byte[] headerBytes = ciphertextHeaders.toByteArray();
final CiphertextHeaders reconstructedHeaders = new CiphertextHeaders();
reconstructedHeaders.deserialize(headerBytes, 0);
final byte[] reconstructedHeaderBytes = reconstructedHeaders.toByteArray();

assertEquals(
reconstructedHeaders.getMaxEncryptedDataKeys(),
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
assertArrayEquals(headerBytes, reconstructedHeaderBytes);
}
}
Expand Down