From 9709e82373c3304bb8e7fb5d62fd61f70e28d651 Mon Sep 17 00:00:00 2001 From: heck Date: Fri, 12 Mar 2021 03:00:08 +0100 Subject: [PATCH] JNI-142: add Tests --- .../pEp/jniadapter/test/jni148/Makefile | 34 ++++++++ .../pEp/jniadapter/test/jni148/TestAlice.java | 86 +++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 test/java/foundation/pEp/jniadapter/test/jni148/Makefile create mode 100644 test/java/foundation/pEp/jniadapter/test/jni148/TestAlice.java diff --git a/test/java/foundation/pEp/jniadapter/test/jni148/Makefile b/test/java/foundation/pEp/jniadapter/test/jni148/Makefile new file mode 100644 index 0000000..77f8f0f --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/jni148/Makefile @@ -0,0 +1,34 @@ +include ../../../../../../../Makefile.conf +include ../Makefile.conf + +TEST_UNIT_NAME=jni148 + +JAVA_CLASSES+= \ + TestAlice.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/jni148/TestAlice.java b/test/java/foundation/pEp/jniadapter/test/jni148/TestAlice.java new file mode 100644 index 0000000..6c71b9d --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/jni148/TestAlice.java @@ -0,0 +1,86 @@ +package foundation.pEp.jniadapter.test.jni148; + + +import foundation.pEp.jniadapter.Identity; +import foundation.pEp.jniadapter.Message; +import foundation.pEp.jniadapter.decrypt_message_Return; +import foundation.pEp.jniadapter.test.utils.AdapterTestUtils; +import foundation.pEp.jniadapter.test.utils.CTXBase; +import foundation.pEp.pitytest.TestSuite; +import foundation.pEp.pitytest.TestUnit; +import foundation.pEp.pitytest.utils.TestUtils; + +import java.util.Vector; + +import static foundation.pEp.pitytest.TestLogger.log; + + +/* +JNI-148 - Mem-mgmt: Defined behaviour of Message.close() + +Required behaviour of Message.close() +* Free the memory of the c-struct Message (not proven here) +* Idempotent (double free safe) +* Accessing Message after close() results in a IllegalStateException + +*/ + +class CTX148 extends CTXBase { + + Message msgAliceToBobEnc; + Message msgAliceToBobEncDec; + + @Override + public CTXBase init() throws Throwable { + super.init(); + alice = engine.myself(alice); + bob = engine.myself(bob); + + msgAliceToBob.setAttachments(attachmentList.getAttachments()); + msgAliceToBob.setFrom(alice); + Vector to = new Vector<>(); + to.add(bob); + msgAliceToBob.setTo(to); + + msgAliceToBobEnc = engine.encrypt_message(msgAliceToBob, null, Message.EncFormat.PEP); + decrypt_message_Return decRet = engine.decrypt_message(msgAliceToBobEnc, null, 0); + if (decRet.dst != null) { + msgAliceToBobEncDec = decRet.dst; + } + log(AdapterTestUtils.msgToString(msgAliceToBobEncDec, false)); + return this; + } + +} + +class TestAlice { + public static void main(String[] args) throws Exception { + TestSuite.getDefault().setVerbose(true); + TestSuite.getDefault().setTestColor(TestUtils.TermColor.GREEN); + + new TestUnit("close() idempotent / double free safe ", new CTX148(), ctx -> { + assert ctx.msgAliceToBobEncDec.getAttachments() != null; + ctx.msgAliceToBobEncDec.close(); + ctx.msgAliceToBobEncDec.close(); + ctx.msgAliceToBobEncDec.close(); + ctx.msgAliceToBobEncDec.close(); + ctx.msgAliceToBobEncDec.close(); + }); + + new TestUnit("after close(): exception on access", new CTX148(), ctx -> { + assert ctx.msgAliceToBobEncDec.getAttachments() != null; + ctx.msgAliceToBobEncDec.close(); + boolean exceptionThrown = false; + try { + assert ctx.msgAliceToBobEncDec.getAttachments() != null; + } catch (IllegalStateException e) { + exceptionThrown = true; + } + assert exceptionThrown: "no exception thrown"; + }); + + TestSuite.getDefault().run(); + } +} + +