Skip to content

Commit

Permalink
Better padding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gurpreet- committed Jul 25, 2021
1 parent 82ed606 commit ebea30f
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/test/java/com/goterl/lazysodium/PaddingTest.java
Expand Up @@ -13,6 +13,8 @@
import junit.framework.TestCase;
import org.junit.Test;

import static junit.framework.TestCase.*;

public class PaddingTest extends BaseTest {


Expand Down Expand Up @@ -58,23 +60,31 @@ private void pad(String padThis, int blockSize, int maxBufLen, int expectedPadLe
lazySodium.getSodium().sodium_pad(finalPaddedLength, p, contentsLength, blockSize, maxBufLen);

// Test
TestCase.assertEquals(expectedPadLength, finalPaddedLength.getValue());
assertEquals(expectedPadLength, finalPaddedLength.getValue());

// Test unpadding of this padded string
int finalLength = finalPaddedLength.getValue();
printString(p, finalLength);
unPad(p, finalLength, blockSize, contentsLength);

// Uncomment to print the string at this point
//printString(p, finalLength);

unPad(padThis, p, finalLength, blockSize, contentsLength);
}

public void unPad(Pointer paddedPointer, int lengthOfArray, int blockSize, int expectedUnpaddedLength) {
public void unPad(String startingString, Pointer paddedPointer, int lengthOfArray, int blockSize, int expectedUnpaddedLength) {
IntByReference unpadRef = new IntByReference();
lazySodium.getSodium().sodium_unpad(unpadRef, paddedPointer, lengthOfArray, blockSize);
TestCase.assertEquals(expectedUnpaddedLength, unpadRef.getValue());
printString(paddedPointer, unpadRef.getValue());
assertEquals(expectedUnpaddedLength, unpadRef.getValue());

String finishingString = pointerToString(paddedPointer, unpadRef.getValue());
assertEquals(startingString, finishingString);
}

private String pointerToString(Pointer p, int length) {
return new String(p.getByteArray(0, length));
}

private void printString(Pointer p, int length) {
String paddedString = new String(p.getByteArray(0, length));
System.out.println(paddedString);
System.out.println(pointerToString(p, length));
}
}

0 comments on commit ebea30f

Please sign in to comment.