Skip to content

Commit

Permalink
added resize of ByteContainer if the new required length is bigger th…
Browse files Browse the repository at this point in the history
…an existing one
  • Loading branch information
petrs authored and Karsten Ohme committed Dec 14, 2021
1 parent 339e8a1 commit d6f79f7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/com/licel/jcardsim/crypto/ByteContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void setBytes(byte[] buff) {
* @param length length of data in byte array
*/
public void setBytes(byte[] buff, short offset, short length) {
if (data == null) {
if (data == null || (short) data.length < length) {
switch (memoryType) {
case JCSystem.MEMORY_TYPE_TRANSIENT_DESELECT:
data = JCSystem.makeTransientByteArray(length, JCSystem.CLEAR_ON_DESELECT);
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/com/licel/jcardsim/crypto/ByteContainerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import junit.framework.TestCase;

import java.math.BigInteger;
import javacard.framework.JCSystem;

public class ByteContainerTest extends TestCase {
public ByteContainerTest(String name) {
Expand Down Expand Up @@ -52,4 +53,23 @@ private void checkRoundTrip(BigInteger expected) {

assertEquals(expected, new ByteContainer(expected).getBigInteger());
}

public void testSetBytes() {
short TEST_DATA_LEN = (short) 32;
byte[] expected = new byte[TEST_DATA_LEN];
ByteContainer container = new ByteContainer(expected, (short) 0, (short) expected.length);
byte[] dataResult = container.getBytes(JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
assertEquals(expected.length, dataResult.length);

// Try to set with shorter and longer array - length should change accordingly
byte[] shorter = new byte[(short) (TEST_DATA_LEN - 2)];
container.setBytes(shorter);
dataResult = container.getBytes(JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
assertEquals(shorter.length, dataResult.length);
byte[] longer = new byte[(short) (TEST_DATA_LEN + 2)];
container.setBytes(longer);
dataResult = container.getBytes(JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
assertEquals(longer.length, dataResult.length);
}

}

0 comments on commit d6f79f7

Please sign in to comment.