From 611e06ba479a0805f43e52dd6ffd60f107ea8f8b Mon Sep 17 00:00:00 2001 From: heck Date: Sun, 13 Sep 2020 21:11:42 +0200 Subject: [PATCH] Test: JNI-118 basic --- .../pEp/jniadapter/test/jni118/Makefile | 37 ++++++ .../pEp/jniadapter/test/jni118/TestAlice.java | 107 ++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 test/java/foundation/pEp/jniadapter/test/jni118/Makefile create mode 100644 test/java/foundation/pEp/jniadapter/test/jni118/TestAlice.java diff --git a/test/java/foundation/pEp/jniadapter/test/jni118/Makefile b/test/java/foundation/pEp/jniadapter/test/jni118/Makefile new file mode 100644 index 0000000..d9d921c --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/jni118/Makefile @@ -0,0 +1,37 @@ +include ../../../../../../../Makefile.conf +include ../Makefile.conf + +TEST_UNIT_NAME=jni118 + +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_CMD) -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/jni118/TestAlice.java b/test/java/foundation/pEp/jniadapter/test/jni118/TestAlice.java new file mode 100644 index 0000000..7f3380d --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/jni118/TestAlice.java @@ -0,0 +1,107 @@ +package foundation.pEp.jniadapter.test.jni118; + +import static foundation.pEp.pitytest.TestLogger.*; + +import foundation.pEp.jniadapter.*; +import foundation.pEp.pitytest.*; +import foundation.pEp.pitytest.utils.TestUtils; +import foundation.pEp.jniadapter.test.utils.*; + +import java.util.ArrayList; +import java.util.Vector; + +// re_evaluate_message_rating(Message m) +// needs a msg that holds the OptFields: +// * X-EncStatus - containing the pEpEngine internal string value of the rating +// * X-KeyList - containing the FPR's of all the receivers of the msg +// +// Handling of PEP_Rating +// When app needs to add optional fields like X-EncStatus, the value has to be the string rep for a rating. +// These internal string representations can be obtained with the method: +// * Rating.getInternalStringValue() - returning the pEpEngine internal string value +// For human readable description of all the enums, use: +// * Rating.toString() +// +// A convenience method Message.addRatingToOptFields(Rating r) would be desirable +// +// X-KeyList +// New method to generate X-Keylist formatted FPR list: +// * String Identity.toXKeyList(List ids) +// +// A convenience method for adding X-KeyList for a message would be desirable +// * method Message.addIdentitiesToOptFields() + +// Test objectives +// re_evaluate_message_rating() equal to decrypt_message_result.rating when Message has correct OptFields +// re_evaluate_message_rating() equal to decrypt_message_result.rating when Message has random rating string on XEncStatus + + + +class JNI1118Context extends AdapterBaseTestContext { + public Message msgToBobEncrypted; + public Message msgToBobDecrypted; + public decrypt_message_Return msgToBobDecryptResult; + + @Override + public JNI1118Context init() throws Throwable { + super.init(); + alice = engine.myself(alice); + engine.importKey(keyBobPub); + + Vector msgToBobRcpts = new Vector<>(); + msgToBobRcpts.add(bob); + msgToBob.setTo(msgToBobRcpts); + + msgToBobEncrypted = engine.encrypt_message(msgToBob, null, Message.EncFormat.PEP); + msgToBobDecrypted = msgToBobEncrypted; + msgToBobDecryptResult = engine.decrypt_message(msgToBobDecrypted, new Vector(), 0); + if (msgToBobEncrypted == null) { + throw new RuntimeException("Context failure, error decrypting message"); + } + return this; + } + + public void addRatingToOptFields(Message msg, String ratingStr) { + ArrayList> opts = msg.getOptFields(); + opts.add(new Pair("X-EncStatus",ratingStr)); + msg.setOptFields(opts); + } + + public void addRcptsToOptFields(Message msg, String fprs) { + ArrayList> opts = msg.getOptFields(); + opts.add(new Pair("X-KeyList", fprs)); + msg.setOptFields(opts); + } + +} + +class TestAlice { + public static void main(String[] args) throws Throwable { + TestSuite.getDefault().setVerbose(true); + TestSuite.getDefault().setTestColor(TestUtils.TermColor.GREEN); + + new TestUnit("re_evaluate_message_rating() equal to decrypt_message_result.rating when Message has correct OptFields", new JNI1118Context(), ctx -> { + ctx.addRatingToOptFields(ctx.msgToBobDecrypted,ctx.msgToBobDecryptResult.rating.getInternalStringValue()); + ctx.addRcptsToOptFields(ctx.msgToBobDecrypted,Identity.toXKeyList(ctx.msgToBobDecrypted.getTo())); + log("running re_evaluate_message_rating() on:\n" + AdapterTestUtils.msgToString(ctx.msgToBobDecrypted, false)); + Rating rat = ctx.engine.re_evaluate_message_rating(ctx.msgToBobDecrypted); + log("re_evaluate_message_rating() result: " + rat.toString()); + assert rat == ctx.msgToBobDecryptResult.rating : "Rating is " + rat.toString() + ",but should be " + ctx.msgToBobDecryptResult.rating.toString(); + }); + + new TestUnit("re_evaluate_message_rating() equal to decrypt_message_result.rating when Message has random rating string on XEncStatus", new JNI1118Context(), ctx -> { + ctx.addRatingToOptFields(ctx.msgToBobDecrypted, TestUtils.randomASCIIString(TestUtils.CharClass.Unbounded, TestUtils.randomInt(0,42))); + ctx.addRcptsToOptFields(ctx.msgToBobDecrypted,Identity.toXKeyList(ctx.msgToBobDecrypted.getTo())); + log("running re_evaluate_message_rating() on:\n" + AdapterTestUtils.msgToString(ctx.msgToBobDecrypted, false)); + Rating rat = ctx.engine.re_evaluate_message_rating(ctx.msgToBobDecrypted); + log("re_evaluate_message_rating() result: " + rat.toString()); + assert rat == ctx.msgToBobDecryptResult.rating : "Rating is " + rat.toString() + ",but should be " + ctx.msgToBobDecryptResult.rating.toString(); + }); + + + + TestSuite.getDefault().run(); + } +} + +