From 7b65930322f76657f59c47932778c3b9268a657d Mon Sep 17 00:00:00 2001 From: heck Date: Mon, 8 Mar 2021 02:26:22 +0100 Subject: [PATCH] Test: add JNI-143 - "Add Attachments Enc/Dec Tests" --- .../pEp/jniadapter/test/jni143/Makefile | 37 ++++++++ .../pEp/jniadapter/test/jni143/TestAlice.java | 94 +++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 test/java/foundation/pEp/jniadapter/test/jni143/Makefile create mode 100644 test/java/foundation/pEp/jniadapter/test/jni143/TestAlice.java diff --git a/test/java/foundation/pEp/jniadapter/test/jni143/Makefile b/test/java/foundation/pEp/jniadapter/test/jni143/Makefile new file mode 100644 index 0000000..e888880 --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/jni143/Makefile @@ -0,0 +1,37 @@ + include ../../../../../../../Makefile.conf +include ../Makefile.conf + +TEST_UNIT_NAME=jni143 + +JAVA_CLASSES = \ + TestAlice.class \ + ../utils/AdapterBaseTestContext.class \ + ../utils/AdapterTestUtils.class \ + ../utils/TestCallbacks.class + +.PHONY: pitytest compile alice test clean + +all: alice compile + +pitytest: + $(MAKE) -C $(PITYTEST_DIR) + +alice: compile clean-pep-home-alice + cd $(JAVA_CWD);pwd;HOME=$(JAVA_PEP_HOME_DIR_ALICE) $(JAVA) $(JAVA_PKG_BASENAME).$(TEST_UNIT_NAME).TestAlice + +compile: $(JAVA_CLASSES) pitytest + +%.class: %.java + cd $(JAVA_CWD);javac -cp $(CLASSPATH) $(JAVA_PKG_BASEPATH)/$(TEST_UNIT_NAME)/$< + +clean: + rm -f $(JAVA_CLASSES) + rm -f *.class + rm -f *.log + rm -Rf .gnupg + rm -Rf .lldb + +clean-pep-home: clean-pep-home-alice + +clean-pep-home-alice: + rm -rf $(PEP_HOME_DIR_ALICE)/.pEp diff --git a/test/java/foundation/pEp/jniadapter/test/jni143/TestAlice.java b/test/java/foundation/pEp/jniadapter/test/jni143/TestAlice.java new file mode 100644 index 0000000..49bc99a --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/jni143/TestAlice.java @@ -0,0 +1,94 @@ +package foundation.pEp.jniadapter.test.jni143; + +import foundation.pEp.jniadapter.Blob; +import foundation.pEp.jniadapter.Message; +import foundation.pEp.jniadapter.decrypt_message_Return; +import foundation.pEp.jniadapter.test.utils.AdapterBaseTestContext; +import foundation.pEp.jniadapter.test.utils.AdapterTestUtils; +import foundation.pEp.pitytest.TestSuite; +import foundation.pEp.pitytest.TestUnit; +import foundation.pEp.pitytest.utils.TestUtils; + +import java.util.Arrays; +import java.util.Vector; + +import static foundation.pEp.pitytest.TestLogger.log; +import static foundation.pEp.pitytest.TestLogger.logH2; + +class Jni143TestContext extends AdapterBaseTestContext { + @Override + public AdapterBaseTestContext init() throws Throwable { + super.init(); + return this; + } +} + +class TestAlice { + public static String diff(byte[] left, byte[] right, boolean verbose) { + String ret = ""; + String diffString = ""; + int diffCount = 0; + for (int i = 0; i < left.length; i++) { + byte bLeft = left[i]; + byte bRight = right[i]; + String diffIndicator = ""; + if (bLeft != bRight) { + diffCount++; + diffString += "Byte[" + i + "]:\t\t " + bLeft + "\t" + bRight + "\t" + "\n"; + } + } + if (verbose) { + ret = diffString + "\n"; + ret += Integer.toString(diffCount) + "\t Different bytes"; + } else { + ret = Integer.toString(diffCount); + } + + return ret; + } + + public static void main(String[] args) throws Exception { + TestSuite.getDefault().setVerbose(true); + TestSuite.getDefault().setTestColor(TestUtils.TermColor.GREEN); +// TestUtils.readKey(); + AdapterBaseTestContext jni143Ctx = new Jni143TestContext(); + + new TestUnit("Attachement sizes", new Jni143TestContext(), ctx -> { + ctx.alice = ctx.engine.myself(ctx.alice); + ctx.bob = ctx.engine.myself(ctx.bob); + + int attachmentSizeBytes = 1; + while (true) { + Message msg1Plain = AdapterTestUtils.makeNewTestMessage(ctx.alice, ctx.bob, Message.Direction.Outgoing); + Blob origBlob = AdapterTestUtils.makeNewTestBlob(attachmentSizeBytes, "atti1", null); + Vector atts = new Vector(); + atts.add(origBlob); + msg1Plain.setAttachments(atts); + + logH2("attachment size: " + attachmentSizeBytes); + Message msg1Enc = ctx.engine.encrypt_message(msg1Plain, null, Message.EncFormat.PEP); + + decrypt_message_Return decRet = ctx.engine.decrypt_message(msg1Enc, null, 0); + assert decRet != null : "could not decrypt message"; + if (decRet != null) { + assert decRet.dst.getAttachments().size() == 1 : "more than 1 attachment"; + byte[] decBlobData = decRet.dst.getAttachments().get(0).data; + boolean attachmentsDiffer = !Arrays.equals(origBlob.data, decBlobData); + if (attachmentsDiffer) { +// log(new String(decBlobData)); + log("attachments decrypted dont equal original"); + log(diff(origBlob.data, decBlobData, false) + "\t\tdiffing bytes"); + } + assert !attachmentsDiffer : "attachments decrypted dont equal original"; + assert decRet.dst.getLongmsg().equals(msg1Plain.getLongmsg()) : "LongMessage decrypted dont equal original"; + } + + attachmentSizeBytes *= 2; + } + }).run(); + +// TestSuite.getDefault().run(); + } +} + +