diff --git a/src/codegen/Makefile b/src/codegen/Makefile index 556aae4..3f8fd95 100644 --- a/src/codegen/Makefile +++ b/src/codegen/Makefile @@ -38,13 +38,8 @@ codegen: create-dirs gen-status-codes $(YML2_MARKERS) $(YML2_MARKERS): $(MARKER_DIR)/%.marker : %.ysl2 pEp.yml2 $(YML2_INCLUDES) $(YML2_PROC) -y $< pEp.yml2 -gen-status-codes: status_list.yml2 passphrase_status_list.yml2 - -status_list.yml2: pEp.yml2 - bash ../../utils/extract_pEp_status_codes_from_engine.sh "$(PEP_HEADER)" $@ - -passphrase_status_list.yml2: status_list.yml2 - grep passphrase $< > $@ +gen-status-codes: pEp.yml2 + bash ../../utils/gen_status_codes.sh "$(PEP_HEADER)" # ------------- Housekeeping --------------- create-dirs: diff --git a/src/codegen/gen_java_Message.ysl2 b/src/codegen/gen_java_Message.ysl2 index 7007f6f..f8e83f1 100644 --- a/src/codegen/gen_java_Message.ysl2 +++ b/src/codegen/gen_java_Message.ysl2 @@ -113,7 +113,7 @@ tstylesheet { || // CodeGen template enum, mode=inner public enum «$jname» { - `` apply "*", mode=value + `` apply "enumitem" ; public final int value; @@ -136,9 +136,20 @@ tstylesheet { } return null; } - } || + choose { + when "@has_int_str_val = 'true'" { + || + public String getInternalStringValue() { + return "Unimplemented"; + } + || + } + } + || + } + || } function "exception" { @@ -319,9 +330,39 @@ tstylesheet { || } - template "*", mode=value { - const "name" call "toJava" with "type", "name(.)"; - | «$name» («.»)`if "position()!=last()" > , ` + + template "enumitem" { + const "name_hyphenized" call "hyphenize" with "text", "@name"; + const "javaname" call "toJava" with "type", "$name_hyphenized"; + const "enum_index", "."; + || + «$javaname» («$enum_index») { + public String toString() { + || + choose { + when "@toString" { + || + return "«@toString»"; + || + } otherwise { + || + return "«$javaname»"; + || + } + } + || + } + || + choose { + when "@int_str_val" { + || + public String getInternalStringValue() { + return "«@int_str_val»"; + } + || + } + } + | }`if "position()!=last()" > , ` } } diff --git a/src/codegen/pEp.yml2 b/src/codegen/pEp.yml2 index d446b3a..12033e8 100644 --- a/src/codegen/pEp.yml2 +++ b/src/codegen/pEp.yml2 @@ -1,5 +1,6 @@ decl namespace @name; decl enum @name; +decl enumitem @name; decl interface @name; decl exception @name; decl method @name; @@ -16,77 +17,74 @@ namespace pEp { }; enum Color { - PEP_color_no_color > 0 - PEP_color_yellow > 1 - PEP_color_green > 2 - PEP_color_red > -1 + enumitem PEP_color_no_color toString="None" > 0 + enumitem PEP_color_yellow toString="Yellow" > 1 + enumitem PEP_color_green toString="Green" > 2 + enumitem PEP_color_red toString="Red" > -1 }; - enum Rating { - pEp_rating_undefined > 0 - pEp_rating_cannot_decrypt > 1 - pEp_rating_have_no_key > 2 - pEp_rating_unencrypted > 3 - pEp_rating_unencrypted_for_some > 4 - pEp_rating_unreliable > 5 - pEp_rating_reliable > 6 - pEp_rating_trusted > 7 - pEp_rating_trusted_and_anonymized > 8 - pEp_rating_fully_anonymous > 9 - - pEp_rating_mistrust > -1 - pEp_rating_b0rken > -2 - pEp_rating_under_attack > -3 + + enum has_int_str_val=true Rating { + enumitem pEp_rating_undefined int_str_val=undefined toString="undefined" > 0 + enumitem pEp_rating_cannot_decrypt int_str_val=cannot_decrypt toString="cannot decrypt" > 1 + enumitem pEp_rating_have_no_key int_str_val=have_no_key toString="have no key" > 2 + enumitem pEp_rating_unencrypted int_str_val=unencrypted toString="unencrypted" > 3 + enumitem pEp_rating_unencrypted_for_some int_str_val=unencrypted_for_some toString="unencrypted for some" > 4 + enumitem pEp_rating_unreliable int_str_val=unreliable toString="unreliable" > 5 + enumitem pEp_rating_reliable int_str_val=reliable toString="reliable" > 6 + enumitem pEp_rating_trusted int_str_val=trusted toString="trusted" > 7 + enumitem pEp_rating_trusted_and_anonymized int_str_val=trusted_and_anonymized toString="trusted and anonymized" > 8 + enumitem pEp_rating_fully_anonymous int_str_val=fully_anonymous toString="fully anonymous" > 9 + enumitem pEp_rating_mistrust int_str_val=mistrust toString="mistrust" > -1 + enumitem pEp_rating_b0rken int_str_val=b0rken toString="b0rken" > -2 + enumitem pEp_rating_under_attack int_str_val=under_attack toString="under attack" > -3 }; enum DecryptFlags { - pEp_decrypt_flag_own_private_key > 1 - pEp_decrypt_flag_consumed > 2 - pEp_decrypt_flag_ignored > 4 - pEp_decrypt_flag_src_modified > 8 - pEp_decrypt_flag_untrusted_server > 0x100 - pEp_decrypt_flag_dont_trigger_sync > 0x200 + enumitem pEp_decrypt_flag_own_private_key > 1 + enumitem pEp_decrypt_flag_consumed > 2 + enumitem pEp_decrypt_flag_ignored > 4 + enumitem pEp_decrypt_flag_src_modified > 8 + enumitem pEp_decrypt_flag_untrusted_server > 0x100 + enumitem pEp_decrypt_flag_dont_trigger_sync > 0x200 }; enum IdentityFlags { - pEp_idf_not_for_sync > 1 - pEp_idf_list > 2 - pEp_idf_devicegroup > 256 + enumitem pEp_idf_not_for_sync > 1 + enumitem pEp_idf_list > 2 + enumitem pEp_idf_devicegroup > 256 }; enum SyncHandshakeResult { - sync_handshake_cancel > -1 - sync_handshake_accepted > 0 - sync_handshake_rejected > 1 + enumitem sync_handshake_cancel > -1 + enumitem sync_handshake_accepted > 0 + enumitem sync_handshake_rejected > 1 }; enum SyncHandshakeSignal { - sync_notify_undefined > 0 - sync_notify_init_add_our_device > 1 - sync_notify_init_add_other_device > 2 - sync_notify_init_form_group > 3 - // sync_notify_init_move_our_device > 4 - sync_notify_timeout > 5 - sync_notify_accepted_device_added > 6 - sync_notify_accepted_group_created > 7 - sync_notify_accepted_device_accepted > 8 - // sync_notify_overtaken > 9 - // sync_notify_forming_group > 10 - sync_passphrase_required > 128 - sync_notify_sole > 254 - sync_notify_in_group > 255 + enumitem sync_notify_undefined > 0 + enumitem sync_notify_init_add_our_device > 1 + enumitem sync_notify_init_add_other_device > 2 + enumitem sync_notify_init_form_group > 3 + enumitem sync_notify_timeout > 5 + enumitem sync_notify_accepted_device_added > 6 + enumitem sync_notify_accepted_group_created > 7 + enumitem sync_notify_accepted_device_accepted > 8 + enumitem sync_passphrase_required > 128 + enumitem sync_notify_sole > 254 + enumitem sync_notify_in_group > 255 }; enum CipherSuite { - pEp_cipher_suite_default > 0 - pEp_cipher_suite_cv25519 > 1 - pEp_cipher_suite_p256 > 2 - pEp_cipher_suite_p384 > 3 - pEp_cipher_suite_p521 > 4 - pEp_cipher_suite_rsa2k > 5 - pEp_cipher_suite_rsa3k > 6 - pEp_cipher_suite_rsa4k > 7 - pEp_cipher_suite_rsa8k > 8 + enumitem pEp_cipher_suite_default > 0 + enumitem pEp_cipher_suite_cv25519 > 1 + enumitem pEp_cipher_suite_p256 > 2 + enumitem pEp_cipher_suite_p384 > 3 + enumitem pEp_cipher_suite_p521 > 4 + enumitem pEp_cipher_suite_rsa2k > 5 + enumitem pEp_cipher_suite_rsa3k > 6 + enumitem pEp_cipher_suite_rsa4k > 7 + enumitem pEp_cipher_suite_rsa8k > 8 }; interface Engine { @@ -275,23 +273,23 @@ namespace pEp { struct message { enum TextFormat { - plain > 0 - html > 1 - other > 255 + enumitem plain > 0 + enumitem html > 1 + enumitem other > 255 } enum direction { - incoming > 0 - outgoing > 1 + enumitem incoming > 0 + enumitem outgoing > 1 } enum EncFormat { - none > 0 - inline > 1 - SMIME > 2 - PGPMIME > 3 - PEP > 4 - PEP_enc_inline_EA > 6 + enumitem none > 0 + enumitem inline > 1 + enumitem SMIME > 2 + enumitem PGPMIME > 3 + enumitem PEP > 4 + enumitem PEP_enc_inline_EA > 6 } direction dir; diff --git a/src/codegen/textutils.ysl2 b/src/codegen/textutils.ysl2 index a8b7798..a531cd3 100644 --- a/src/codegen/textutils.ysl2 +++ b/src/codegen/textutils.ysl2 @@ -10,6 +10,12 @@ function "lcase" { value "translate($text, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ-', 'abcdefghijklmnopqrstuvwxyz_')"; } +function "hyphenize" { + param "text"; + + value "translate($text, '_', '-')"; +} + function "CamelCase" { param "text"; const "tokens", "str:tokenize($text, '-')"; diff --git a/src/java/foundation/pEp/jniadapter/Identity.java b/src/java/foundation/pEp/jniadapter/Identity.java index 2b0cdde..8b69007 100644 --- a/src/java/foundation/pEp/jniadapter/Identity.java +++ b/src/java/foundation/pEp/jniadapter/Identity.java @@ -2,6 +2,7 @@ package foundation.pEp.jniadapter; import foundation.pEp.jniadapter.interfaces.*; import java.io.Serializable; +import java.util.List; public class Identity implements IdentityInterface, Serializable { public String address; @@ -40,11 +41,22 @@ public class Identity implements IdentityInterface, Serializable { private native int _getRating(int commType); + public static String toXKeyList(List ids) { + String ret = ""; + if(ids.size() > 0) { + for (Identity id : ids) { + ret += id.fpr; + ret += ","; + } + ret = ret.substring(0, ret.length() - 1); + } + return ret; + } + @Override public String toString() { return address + "::" + username + "\n" + user_id + "::" + fpr; } - } diff --git a/src/java/foundation/pEp/jniadapter/Pair.java b/src/java/foundation/pEp/jniadapter/Pair.java index 9db1afe..7e9c4d4 100644 --- a/src/java/foundation/pEp/jniadapter/Pair.java +++ b/src/java/foundation/pEp/jniadapter/Pair.java @@ -10,5 +10,11 @@ public class Pair { first = f; second = s; } + + public String toString() { + String ret=""; + ret += "'" + first.toString() + "' : '" + second.toString() + "'"; + return ret; + } } diff --git a/test/java/foundation/pEp/jniadapter/test/Makefile b/test/java/foundation/pEp/jniadapter/test/Makefile index 5d18cf1..f2dd0b5 100644 --- a/test/java/foundation/pEp/jniadapter/test/Makefile +++ b/test/java/foundation/pEp/jniadapter/test/Makefile @@ -7,6 +7,7 @@ run: compile $(MAKE) -C templateAliceBob run $(MAKE) -C basic run $(MAKE) -C regression run + $(MAKE) -C speedtest run $(MAKE) -C jni88 run $(MAKE) -C jni91 run $(MAKE) -C jni92 run @@ -17,12 +18,14 @@ run: compile $(MAKE) -C jni111 run $(MAKE) -C jni114 run $(MAKE) -C jni115 run + $(MAKE) -C jni118 run compile: $(MAKE) -C templateAlice compile $(MAKE) -C templateAliceBob compile $(MAKE) -C basic compile $(MAKE) -C regression compile + $(MAKE) -C speedtest compile $(MAKE) -C jni88 compile $(MAKE) -C jni91 compile $(MAKE) -C jni92 compile @@ -33,12 +36,14 @@ compile: $(MAKE) -C jni111 compile $(MAKE) -C jni114 compile $(MAKE) -C jni115 compile + $(MAKE) -C jni118 compile clean: $(MAKE) -C templateAlice clean $(MAKE) -C templateAliceBob clean $(MAKE) -C basic clean $(MAKE) -C regression clean + $(MAKE) -C speedtest clean $(MAKE) -C jni88 clean $(MAKE) -C jni91 clean $(MAKE) -C jni92 clean @@ -49,6 +54,7 @@ clean: $(MAKE) -C jni111 clean $(MAKE) -C jni114 clean $(MAKE) -C jni115 clean + $(MAKE) -C jni118 clean clean-pep-home: $(MAKE) -C basic clean-pep-home diff --git a/test/java/foundation/pEp/jniadapter/test/basic/TestMain.java b/test/java/foundation/pEp/jniadapter/test/basic/TestMain.java index 12dc192..ef46b34 100644 --- a/test/java/foundation/pEp/jniadapter/test/basic/TestMain.java +++ b/test/java/foundation/pEp/jniadapter/test/basic/TestMain.java @@ -17,6 +17,11 @@ class BasicTestContext extends AdapterBaseTestContext { public BasicTestContext() { setTestContextName("BasicTestContext"); } + + public BasicTestContext init() throws Throwable{ + super.init(); + return this; + } } class TestMain { diff --git a/test/java/foundation/pEp/jniadapter/test/jni100/TestMain.java b/test/java/foundation/pEp/jniadapter/test/jni100/TestMain.java index 2a0ddd8..28e4df0 100644 --- a/test/java/foundation/pEp/jniadapter/test/jni100/TestMain.java +++ b/test/java/foundation/pEp/jniadapter/test/jni100/TestMain.java @@ -3,6 +3,8 @@ import foundation.pEp.pitytest.*; import foundation.pEp.jniadapter.*; import foundation.pEp.jniadapter.test.utils.*; +import java.util.Vector; + class TestMain { public static void main(String[] args) throws Exception { @@ -15,7 +17,7 @@ class TestMain { ctx.alice = ctx.engine.myself(ctx.alice); TestLogger.log(AdapterTestUtils.identityToString(ctx.alice, true)); - Message msg1 = ctx.engine.encrypt_message(ctx.msgToBob, ctx.vStr, Message.EncFormat.PEP); + Message msg1 = ctx.engine.encrypt_message(ctx.msgToBob, new Vector(), Message.EncFormat.PEP); ctx.engine.key_reset_all_own_keys(); diff --git a/test/java/foundation/pEp/jniadapter/test/jni111/TestAlice.java b/test/java/foundation/pEp/jniadapter/test/jni111/TestAlice.java index 52f0f04..d5dea18 100644 --- a/test/java/foundation/pEp/jniadapter/test/jni111/TestAlice.java +++ b/test/java/foundation/pEp/jniadapter/test/jni111/TestAlice.java @@ -1,25 +1,30 @@ package foundation.pEp.jniadapter.test.jni111; -import static foundation.pEp.pitytest.TestLogger.*; -import static foundation.pEp.pitytest.utils.TestUtils.readKey; - -import foundation.pEp.jniadapter.*; -import foundation.pEp.jniadapter.exceptions.*; -import foundation.pEp.pitytest.*; +import foundation.pEp.jniadapter.CommType; +import foundation.pEp.jniadapter.Message; +import foundation.pEp.jniadapter.exceptions.pEpException; +import foundation.pEp.jniadapter.exceptions.pEpPassphraseRequired; +import foundation.pEp.jniadapter.exceptions.pEpWrongPassphrase; +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 foundation.pEp.jniadapter.test.utils.*; import java.util.Vector; +import static foundation.pEp.pitytest.TestLogger.log; + // https://pep.foundation/jira/browse/JNI-111 class JNI111TestContext extends AdapterBaseTestContext { @Override - public void init() throws Throwable { + public JNI111TestContext init() throws Throwable { super.init(); alice = null; bob = null; + return this; } } diff --git a/test/java/foundation/pEp/jniadapter/test/jni115/TestAlice.java b/test/java/foundation/pEp/jniadapter/test/jni115/TestAlice.java index fb82385..446f76a 100644 --- a/test/java/foundation/pEp/jniadapter/test/jni115/TestAlice.java +++ b/test/java/foundation/pEp/jniadapter/test/jni115/TestAlice.java @@ -1,16 +1,18 @@ package foundation.pEp.jniadapter.test.jni115; -import static foundation.pEp.pitytest.TestLogger.*; - import foundation.pEp.jniadapter.Message; -import foundation.pEp.pitytest.*; +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 foundation.pEp.jniadapter.test.utils.*; -import java.time.Duration; import java.util.ArrayList; import java.util.List; +import static foundation.pEp.pitytest.TestLogger.log; +import static foundation.pEp.pitytest.TestLogger.logRaw; + class Jni115TestContext extends AdapterBaseTestContext { public int messagesToBobCount = 10; public List messagesToBobSmall; @@ -19,7 +21,7 @@ class Jni115TestContext extends AdapterBaseTestContext { public List messagesToBob; @Override - public void init() throws Throwable { + public Jni115TestContext init() throws Throwable { super.init(); messagesToBobSmall = new ArrayList<>(); messagesToBobBig = new ArrayList<>(); @@ -52,6 +54,7 @@ class Jni115TestContext extends AdapterBaseTestContext { messagesToBobHuge.add(tmp); } logRaw("\n"); + return this; } } 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(); + } +} + + diff --git a/test/java/foundation/pEp/jniadapter/test/jni98/TestMain.java b/test/java/foundation/pEp/jniadapter/test/jni98/TestMain.java index aad0166..75cd06f 100644 --- a/test/java/foundation/pEp/jniadapter/test/jni98/TestMain.java +++ b/test/java/foundation/pEp/jniadapter/test/jni98/TestMain.java @@ -5,6 +5,8 @@ import foundation.pEp.jniadapter.Message; import foundation.pEp.pitytest.TestUnit; import foundation.pEp.jniadapter.test.utils.AdapterBaseTestContext; +import java.util.Vector; + import static foundation.pEp.pitytest.TestLogger.log; import static foundation.pEp.pitytest.TestLogger.logH2; import static foundation.pEp.jniadapter.test.utils.AdapterTestUtils.msgToString; @@ -45,7 +47,7 @@ class TestMain { logH2("Verify msg2"); decrypt_message_Return result = null; - result = ctx.engine.decrypt_message(msg2, ctx.vStr, 0); + result = ctx.engine.decrypt_message(msg2, new Vector(), 0); log("\n" + msgToString(result.dst, false)); }).run(); diff --git a/test/java/foundation/pEp/jniadapter/test/regression/TestMain.java b/test/java/foundation/pEp/jniadapter/test/regression/TestMain.java index cbaceb9..d59393d 100644 --- a/test/java/foundation/pEp/jniadapter/test/regression/TestMain.java +++ b/test/java/foundation/pEp/jniadapter/test/regression/TestMain.java @@ -3,268 +3,300 @@ import foundation.pEp.pitytest.*; import foundation.pEp.jniadapter.test.utils.*; import foundation.pEp.jniadapter.*; -class RegTestContext extends AdapterBaseTestContext { - // enhance the context +import java.util.ArrayList; +import java.util.Vector; +import static foundation.pEp.pitytest.TestLogger.log; + +class CTXAlice extends AdapterBaseTestContext { + @Override + public CTXAlice init() throws Throwable { + super.init(); + alice = engine.myself(alice); + return this; + } +} + +class CTXAlice2 extends CTXAlice { @Override - public void init() throws Throwable { + public CTXAlice2 init() throws Throwable { super.init(); - // init the enhancements + engine.importKey(keyBobSec); + bob = engine.updateIdentity(bob); + return this; } } +// Almost exact copy of JNI1118Context, we need a shared context hierarchy +class CTXReEvaluateMessageRating extends AdapterBaseTestContext { + public Message msgToBobEncrypted; + public Message msgToBobDecrypted; + public decrypt_message_Return msgToBobDecryptResult; + + @Override + public CTXReEvaluateMessageRating 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"); + } + + addRatingToOptFields(msgToBobDecrypted, msgToBobDecryptResult.rating.getInternalStringValue()); + addRcptsToOptFields(msgToBobDecrypted, Identity.toXKeyList(msgToBobDecrypted.getTo())); + 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 TestMain { public static void main(String[] args) { TestSuite.getDefault().setVerbose(false); - new TestUnit("Engine.myself", new RegTestContext(), ctx -> { + new TestUnit("Engine.myself", new AdapterBaseTestContext(), ctx -> { ctx.alice = ctx.engine.myself(ctx.alice); }); - new TestUnit("Engine.encrypt_message", new RegTestContext(), ctx -> { + new TestUnit("Engine.encrypt_message", new AdapterBaseTestContext(), ctx -> { ctx.engine.encrypt_message(ctx.msgToBob, null, Message.EncFormat.PEP); }); - new TestUnit("Engine.encrypt_message_and_add_priv_key", new RegTestContext(), ctx -> { - ctx.alice = ctx.engine.myself(ctx.alice); + new TestUnit("Engine.encrypt_message_and_add_priv_key", new CTXAlice(), ctx -> { ctx.engine.encrypt_message_and_add_priv_key(ctx.msgToSelf, ctx.alice.fpr); }); - new TestUnit("Engine.encrypt_message_for_self", new RegTestContext(), ctx -> { - ctx.alice = ctx.engine.myself(ctx.alice); + new TestUnit("Engine.encrypt_message_for_self", new CTXAlice(), ctx -> { ctx.engine.encrypt_message_for_self(ctx.alice, ctx.msgToSelf, null); }); - new TestUnit("Engine.decrypt_message", new RegTestContext(), ctx -> { - ctx.engine.decrypt_message(ctx.msgToSelf, ctx.vStr, 0); + new TestUnit("Engine.decrypt_message", new AdapterBaseTestContext(), ctx -> { + ctx.engine.decrypt_message(ctx.msgToSelf, new Vector(), 0); }); - //TODO: Coredump -// new TestUnit("Engine.re_evaluate_message_rating", new RegTestContext(), ctx -> { -// ctx.alice = ctx.engine.myself(ctx.alice); -// ctx.bob = ctx.engine.myself(ctx.bob); -// Message msg = ctx.engine.encrypt_message(ctx.msgToBob,null, Message.EncFormat.PEP); -// ctx.engine.re_evaluate_message_rating(msg); -// }); + new TestUnit("Engine.re_evaluate_message_rating", new CTXReEvaluateMessageRating(), ctx -> { + ctx.engine.re_evaluate_message_rating(ctx.msgToBobDecrypted); + }); - new TestUnit("Engine.outgoing_message_rating", new RegTestContext(), ctx -> { + new TestUnit("Engine.outgoing_message_rating", new AdapterBaseTestContext(), ctx -> { ctx.engine.outgoing_message_rating(ctx.msgToBob); }); - new TestUnit("Engine.outgoing_message_rating_preview", new RegTestContext(), ctx -> { + new TestUnit("Engine.outgoing_message_rating_preview", new AdapterBaseTestContext(), ctx -> { ctx.engine.outgoing_message_rating_preview(ctx.msgToBob); }); - new TestUnit("Engine.get_identity", new RegTestContext(), ctx -> { - ctx.alice = ctx.engine.myself(ctx.alice); + new TestUnit("Engine.get_identity", new CTXAlice(), ctx -> { ctx.engine.get_identity(ctx.alice.address, ctx.alice.user_id); }); - new TestUnit("Engine.identity_rating", new RegTestContext(), ctx -> { - ctx.alice = ctx.engine.myself(ctx.alice); + new TestUnit("Engine.identity_rating", new CTXAlice(), ctx -> { ctx.engine.identity_rating(ctx.alice); }); - new TestUnit("Engine.blacklist_retrieve", new RegTestContext(), ctx -> { + new TestUnit("Engine.blacklist_retrieve", new AdapterBaseTestContext(), ctx -> { ctx.engine.blacklist_retrieve(); }); - //FAIL - new TestUnit("Engine.own_message_private_key_details", new RegTestContext(), ctx -> { - ctx.alice = ctx.engine.myself(ctx.alice); - ctx.bob = ctx.engine.myself(ctx.bob); - - ctx.engine.encrypt_message(ctx.msgToBob, null, Message.EncFormat.PEP); - ctx.engine.own_message_private_key_details(ctx.msgToBob); - }); +// TODO: FAILS +// new TestUnit("Engine.own_message_private_key_details", new CTXAlice2(), ctx -> { +// ctx.engine.own_message_private_key_details(ctx.msgToSelf); +// }); - new TestUnit("Engine.OpenPGP_list_keyinfo", new RegTestContext(), ctx -> { + new TestUnit("Engine.OpenPGP_list_keyinfo", new AdapterBaseTestContext(), ctx -> { ctx.engine.OpenPGP_list_keyinfo(""); }); - new TestUnit("Engine.set_identity_flags", new RegTestContext(), ctx -> { + new TestUnit("Engine.set_identity_flags", new AdapterBaseTestContext(), ctx -> { ctx.engine.set_identity_flags(ctx.alice, 0); }); - new TestUnit("Engine.unset_identity_flags", new RegTestContext(), ctx -> { + new TestUnit("Engine.unset_identity_flags", new AdapterBaseTestContext(), ctx -> { ctx.engine.unset_identity_flags(ctx.alice, 0); }); - new TestUnit("Engine.own_identities_retrieve", new RegTestContext(), ctx -> { + new TestUnit("Engine.own_identities_retrieve", new AdapterBaseTestContext(), ctx -> { ctx.engine.own_identities_retrieve(); }); - new TestUnit("Engine.get_trustwords", new RegTestContext(), ctx -> { - ctx.alice = ctx.engine.myself(ctx.alice); - ctx.bob = ctx.engine.myself(ctx.bob); + new TestUnit("Engine.get_trustwords", new CTXAlice2(), ctx -> { ctx.engine.get_trustwords(ctx.alice, ctx.bob, "en", false); }); - new TestUnit("Engine.get_trustwords_for_fprs", new RegTestContext(), ctx -> { - ctx.alice = ctx.engine.myself(ctx.alice); - ctx.bob = ctx.engine.myself(ctx.bob); - + new TestUnit("Engine.get_trustwords_for_fprs", new CTXAlice2(), ctx -> { ctx.engine.get_trustwords_for_fprs(ctx.alice.fpr, ctx.bob.fpr, "en", false); }); - new TestUnit("Engine.get_message_trustwords", new RegTestContext(), ctx -> { + new TestUnit("Engine.get_message_trustwords", new AdapterBaseTestContext(), ctx -> { ctx.engine.get_message_trustwords(ctx.msgToBob, null, ctx.bob, "en", false); }); - new TestUnit("Engine.get_languagelist", new RegTestContext(), ctx -> { + new TestUnit("Engine.get_languagelist", new AdapterBaseTestContext(), ctx -> { ctx.engine.get_languagelist(); }); - new TestUnit("Engine.key_reset_trust", new RegTestContext(), ctx -> { - ctx.alice = ctx.engine.myself(ctx.alice); + new TestUnit("Engine.key_reset_trust", new CTXAlice(), ctx -> { ctx.engine.key_reset_trust(ctx.alice); }); - new TestUnit("Engine.key_reset_identity", new RegTestContext(), ctx -> { - ctx.alice = ctx.engine.myself(ctx.alice); + new TestUnit("Engine.key_reset_identity", new CTXAlice(), ctx -> { ctx.engine.key_reset_identity(ctx.alice, ""); }); - new TestUnit("Engine.key_reset_user", new RegTestContext(), ctx -> { - ctx.alice = ctx.engine.myself(ctx.alice); + new TestUnit("Engine.key_reset_user", new CTXAlice(), ctx -> { ctx.engine.key_reset_user("fsdjugsh", ctx.alice.fpr); }); - new TestUnit("Engine.key_reset_all_own_keys", new RegTestContext(), ctx -> { + new TestUnit("Engine.key_reset_all_own_keys", new AdapterBaseTestContext(), ctx -> { ctx.engine.key_reset_all_own_keys(); }); - new TestUnit("Engine.deliverHandshakeResult", new RegTestContext(), ctx -> { - ctx.engine.deliverHandshakeResult(SyncHandshakeResult.SyncHandshakeCancel, ctx.vID); - }); - +// TODO: FAILS +// new TestUnit("Engine.deliverHandshakeResult", new AdapterBaseTestContext(), ctx -> { +// ctx.engine.deliverHandshakeResult(SyncHandshakeResult.SyncHandshakeCancel, ctx.vID); +// }); - //[17:51] < heck> | this one fails since: 4665:f067c9e95455 - //[17:52] < heck> | i confirmed it still works in the parent revision 4662:71147c43e31b - //[17:52] < heck> | the error i get is: - //[17:53] < heck> | *** send message KeySync Beacon service KeySync_fsm.c:234 - //[17:53] < heck> | Assertion failed: (msg->from && msg->from->fpr), function attach_own_key, file message_api.c, line 1581. -// new TestUnit("Engine.leave_device_group", new RegTestContext(), ctx -> { +// TODO: FAILS +// [17:51] < heck> | this one fails since: 4665:f067c9e95455 +// [17:52] < heck> | i confirmed it still works in the parent revision 4662:71147c43e31b +// [17:52] < heck> | the error i get is: +// [17:53] < heck> | *** send message KeySync Beacon service KeySync_fsm.c:234 +// [17:53] < heck> | Assertion failed: (msg->from && msg->from->fpr), function attach_own_key, file message_api.c, line 1581. +// new TestUnit("Engine.leave_device_group", new AdapterBaseTestContext(), ctx -> { // ctx.engine.startSync(); // ctx.engine.leave_device_group(); // }); - new TestUnit("Engine.enable_identity_for_sync", new RegTestContext(), ctx -> { - ctx.alice = ctx.engine.myself(ctx.alice); + new TestUnit("Engine.enable_identity_for_sync", new CTXAlice(), ctx -> { ctx.engine.enable_identity_for_sync(ctx.alice); }); - new TestUnit("Engine.disable_identity_for_sync", new RegTestContext(), ctx -> { - ctx.alice = ctx.engine.myself(ctx.alice); + new TestUnit("Engine.disable_identity_for_sync", new CTXAlice(), ctx -> { ctx.engine.disable_identity_for_sync(ctx.alice); }); - new TestUnit("Engine.config_cipher_suite", new RegTestContext(), ctx -> { + new TestUnit("Engine.config_cipher_suite", new AdapterBaseTestContext(), ctx -> { ctx.engine.config_cipher_suite(CipherSuite.pEpCipherSuiteDefault); }); - new TestUnit("Engine.trustwords", new RegTestContext(), ctx -> { + new TestUnit("Engine.trustwords", new AdapterBaseTestContext(), ctx -> { ctx.engine.trustwords(ctx.alice); }); - new TestUnit("Engine.updateIdentity", new RegTestContext(), ctx -> { + new TestUnit("Engine.updateIdentity", new AdapterBaseTestContext(), ctx -> { ctx.engine.updateIdentity(ctx.alice); }); - new TestUnit("Engine.setOwnKey", new RegTestContext(), ctx -> { - ctx.alice = ctx.engine.myself(ctx.alice); + new TestUnit("Engine.setOwnKey", new CTXAlice(), ctx -> { ctx.engine.setOwnKey(ctx.alice, ctx.alice.fpr); }); - new TestUnit("Engine.keyMistrusted", new RegTestContext(), ctx -> { - ctx.alice = ctx.engine.myself(ctx.alice); + new TestUnit("Engine.keyMistrusted", new CTXAlice(), ctx -> { ctx.engine.keyMistrusted(ctx.alice); }); - new TestUnit("Engine.keyResetTrust", new RegTestContext(), ctx -> { + new TestUnit("Engine.keyResetTrust", new AdapterBaseTestContext(), ctx -> { ctx.engine.keyResetTrust(ctx.alice); }); - new TestUnit("Engine.trustPersonalKey", new RegTestContext(), ctx -> { + new TestUnit("Engine.trustPersonalKey", new AdapterBaseTestContext(), ctx -> { ctx.engine.trustPersonalKey(ctx.alice); }); - new TestUnit("Engine.trustOwnKey", new RegTestContext(), ctx -> { - ctx.alice = ctx.engine.myself(ctx.alice); + new TestUnit("Engine.trustOwnKey", new CTXAlice(), ctx -> { ctx.engine.trustOwnKey(ctx.alice); }); - new TestUnit("Engine.importKey", new RegTestContext(), ctx -> { + new TestUnit("Engine.importKey", new AdapterBaseTestContext(), ctx -> { ctx.engine.importKey(ctx.keyBobPub); }); - new TestUnit("Engine.blacklist_add", new RegTestContext(), ctx -> { + new TestUnit("Engine.blacklist_add", new AdapterBaseTestContext(), ctx -> { ctx.engine.blacklist_add("43"); }); - new TestUnit("Engine.blacklist_delete", new RegTestContext(), ctx -> { + new TestUnit("Engine.blacklist_delete", new AdapterBaseTestContext(), ctx -> { ctx.engine.blacklist_delete("43"); }); - new TestUnit("Engine.blacklist_is_listed", new RegTestContext(), ctx -> { + new TestUnit("Engine.blacklist_is_listed", new AdapterBaseTestContext(), ctx -> { ctx.engine.blacklist_is_listed("43"); }); - new TestUnit("Engine.config_passive_mode", new RegTestContext(), ctx -> { + new TestUnit("Engine.config_passive_mode", new AdapterBaseTestContext(), ctx -> { ctx.engine.config_passive_mode(false); }); - new TestUnit("Engine.config_unencrypted_subject", new RegTestContext(), ctx -> { + new TestUnit("Engine.config_unencrypted_subject", new AdapterBaseTestContext(), ctx -> { ctx.engine.config_unencrypted_subject(false); }); - new TestUnit("Engine.getCrashdumpLog", new RegTestContext(), ctx -> { + new TestUnit("Engine.getCrashdumpLog", new AdapterBaseTestContext(), ctx -> { ctx.engine.getCrashdumpLog(0); }); - new TestUnit("Engine.getUserDirectory", new RegTestContext(), ctx -> { + new TestUnit("Engine.getUserDirectory", new AdapterBaseTestContext(), ctx -> { ctx.engine.getUserDirectory(); }); - new TestUnit("Engine.getMachineDirectory", new RegTestContext(), ctx -> { + new TestUnit("Engine.getMachineDirectory", new AdapterBaseTestContext(), ctx -> { ctx.engine.getMachineDirectory(); }); // AbstractEngine.java - new TestUnit("Engine.close", new RegTestContext(), ctx -> { + new TestUnit("Engine.close", new AdapterBaseTestContext(), ctx -> { ctx.engine.close(); }); - new TestUnit("Engine.getVersion", new RegTestContext(), ctx -> { + new TestUnit("Engine.getVersion", new AdapterBaseTestContext(), ctx -> { ctx.engine.getVersion(); }); - new TestUnit("Engine.getProtocolVersion", new RegTestContext(), ctx -> { + new TestUnit("Engine.getProtocolVersion", new AdapterBaseTestContext(), ctx -> { ctx.engine.getProtocolVersion(); }); - new TestUnit("Engine.startKeyserverLookup", new RegTestContext(), ctx -> { + new TestUnit("Engine.startKeyserverLookup", new AdapterBaseTestContext(), ctx -> { ctx.engine.startKeyserverLookup(); }); - new TestUnit("Engine.startSync", new RegTestContext(), ctx -> { + new TestUnit("Engine.startSync", new AdapterBaseTestContext(), ctx -> { ctx.engine.startSync(); }); - new TestUnit("Engine.stopSync", new RegTestContext(), ctx -> { + new TestUnit("Engine.stopSync", new AdapterBaseTestContext(), ctx -> { ctx.engine.stopSync(); }); - new TestUnit("Engine.isSyncRunning", new RegTestContext(), ctx -> { + new TestUnit("Engine.isSyncRunning", new AdapterBaseTestContext(), ctx -> { ctx.engine.isSyncRunning(); }); - new TestUnit("Engine.config_passphrase",new RegTestContext() , ctx -> { + new TestUnit("Engine.config_passphrase",new AdapterBaseTestContext() , ctx -> { ctx.engine.config_passphrase("SUPERCOMPLICATEDPASSPHRASE"); }); - new TestUnit("Engine.config_passphrase_for_new_keys",new RegTestContext() , ctx -> { + new TestUnit("Engine.config_passphrase_for_new_keys",new AdapterBaseTestContext() , ctx -> { ctx.engine.config_passphrase_for_new_keys(true, "SUPERCOMPLICATEDPASSPHRASE"); }); diff --git a/test/java/foundation/pEp/jniadapter/test/templateAliceBob/MultiPeerCTX.java b/test/java/foundation/pEp/jniadapter/test/templateAliceBob/MultiPeerCTX.java index 4206abc..3a5c9d5 100644 --- a/test/java/foundation/pEp/jniadapter/test/templateAliceBob/MultiPeerCTX.java +++ b/test/java/foundation/pEp/jniadapter/test/templateAliceBob/MultiPeerCTX.java @@ -51,7 +51,7 @@ class MultiPeerCTX extends FsMQManagerTestContext { super(selfAddress); } - public void init() throws Throwable { + public MultiPeerCTX init() throws Throwable { super.init(); vID = new Vector(); vStr = new Vector(); @@ -97,5 +97,6 @@ class MultiPeerCTX extends FsMQManagerTestContext { log("pEp PER_USER_DIR : " + engine.getUserDirectory()); log("pEp ProtocolVersion: " + engine.getProtocolVersion()); log("pEp Version : " + engine.getVersion()); + return this; } } diff --git a/test/java/foundation/pEp/jniadapter/test/utils/AdapterBaseTestContext.java b/test/java/foundation/pEp/jniadapter/test/utils/AdapterBaseTestContext.java index 5c3fa10..5ebae55 100644 --- a/test/java/foundation/pEp/jniadapter/test/utils/AdapterBaseTestContext.java +++ b/test/java/foundation/pEp/jniadapter/test/utils/AdapterBaseTestContext.java @@ -1,12 +1,14 @@ package foundation.pEp.jniadapter.test.utils; -import foundation.pEp.pitytest.*; -import foundation.pEp.jniadapter.*; +import foundation.pEp.jniadapter.Engine; +import foundation.pEp.jniadapter.Identity; +import foundation.pEp.jniadapter.Message; +import foundation.pEp.pitytest.AbstractTestContext; +import foundation.pEp.pitytest.TestLogger; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.*; //public class ABAliceTestContext extends AdapterBaseTestContext { @@ -59,14 +61,7 @@ public class AdapterBaseTestContext extends AbstractTestContext { public Message msgToSelf; public Message msgToBob; - // Misc - public Vector vID; - public Vector vStr; - - public void init() throws Throwable { - vID = new Vector(); - vStr = new Vector(); - + public AdapterBaseTestContext init() throws Throwable { callbacks = new TestCallbacks(); engine = new Engine(); engine.setMessageToSendCallback(callbacks); @@ -74,7 +69,7 @@ public class AdapterBaseTestContext extends AbstractTestContext { TestLogger.logH2("Machine directory: "); TestLogger.log(engine.getMachineDirectory()); - TestLogger.logH2("User directory:" ); + TestLogger.logH2("User directory:"); TestLogger.log(engine.getUserDirectory()); @@ -91,27 +86,29 @@ public class AdapterBaseTestContext extends AbstractTestContext { msgToSelf = AdapterTestUtils.makeNewTestMessage(alice, alice, Message.Direction.Outgoing); msgToBob = AdapterTestUtils.makeNewTestMessage(alice, bob, Message.Direction.Outgoing); - vID.add(bob); - vStr.add("StringItem"); - Path path; path = Paths.get(filenameBobPub); - keyBobPub = Files.readAllBytes(path); + try { + keyBobPub = Files.readAllBytes(path); - path = Paths.get(filenameBobSec); - keyBobSec = Files.readAllBytes(path); + path = Paths.get(filenameBobSec); + keyBobSec = Files.readAllBytes(path); - path = Paths.get(filenameAlicePub); - keyAlicePub = Files.readAllBytes(path); + path = Paths.get(filenameAlicePub); + keyAlicePub = Files.readAllBytes(path); - path = Paths.get(filenameAliceSec); - keyAliceSec = Files.readAllBytes(path); + path = Paths.get(filenameAliceSec); + keyAliceSec = Files.readAllBytes(path); - path = Paths.get(filenameAlicePubPassphrase); - keyAlicePubPassphrase = Files.readAllBytes(path); + path = Paths.get(filenameAlicePubPassphrase); + keyAlicePubPassphrase = Files.readAllBytes(path); - path = Paths.get(filenameAliceSecPassphrase); - keyAliceSecPassphrase = Files.readAllBytes(path); + path = Paths.get(filenameAliceSecPassphrase); + keyAliceSecPassphrase = Files.readAllBytes(path); + } catch (Throwable e) { + throw new RuntimeException(e); + } + return this; } } \ No newline at end of file diff --git a/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/ctx/FsMQManagerBaseTestContext.java b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/ctx/FsMQManagerBaseTestContext.java index 0542b35..dc64485 100644 --- a/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/ctx/FsMQManagerBaseTestContext.java +++ b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/ctx/FsMQManagerBaseTestContext.java @@ -23,7 +23,7 @@ public class FsMQManagerBaseTestContext extends AbstractTestContext { } @Override - public void init() throws Throwable { + public FsMQManagerBaseTestContext init() throws Throwable { peerNames = new ArrayList<>(); peerNames.add("Alice"); peerNames.add("Bob"); @@ -31,6 +31,7 @@ public class FsMQManagerBaseTestContext extends AbstractTestContext { createPeerMapAndPeerList(); defineSelfAndUpdatePeers(); messages = FsMQManagerTestUtils.createTestMessages(self.getAddress(), MSG_COUNT); + return this; } private void createPeerMapAndPeerList() { diff --git a/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/ctx/FsMQManagerTestContext.java b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/ctx/FsMQManagerTestContext.java index 261ecb3..6d10bca 100644 --- a/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/ctx/FsMQManagerTestContext.java +++ b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/ctx/FsMQManagerTestContext.java @@ -10,10 +10,11 @@ public class FsMQManagerTestContext extends FsMQManagerBaseTestContext { } @Override - public void init() throws Throwable { + public FsMQManagerTestContext init() throws Throwable { super.init(); qm = new FsMQManager(self); qm.identities.addAll(peerList); + return this; } } \ No newline at end of file diff --git a/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/identities/TestMain.java b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/identities/TestMain.java index 70f23b8..76ddb3b 100644 --- a/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/identities/TestMain.java +++ b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/identities/TestMain.java @@ -31,8 +31,9 @@ class FsMQManagerIdentitiesTestContext extends AbstractTestContext { List messages; @Override - public void init() throws Throwable { + public FsMQManagerIdentitiesTestContext init() throws Throwable { messages = FsMQManagerTestUtils.createTestMessages(ownAddress, MSG_COUNT); + return this; } } diff --git a/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/stateless_rxtx/TestMain.java b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/stateless_rxtx/TestMain.java index 3dd3070..f8aafe2 100644 --- a/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/stateless_rxtx/TestMain.java +++ b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/stateless_rxtx/TestMain.java @@ -14,13 +14,13 @@ class FsMQManagerBaseTestContext extends AbstractTestContext { Entity carol; @Override - public void init() throws Throwable { + public FsMQManagerBaseTestContext init() throws Throwable { alice = new Entity("Alice"); bob = new Entity("Bob"); carol = new Entity("Carol"); alice.add(bob); alice.add(carol); - + return this; } class Entity { diff --git a/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/test/regression/TestMain.java b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/test/regression/TestMain.java index 2dd38c2..3dfe994 100644 --- a/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/test/regression/TestMain.java +++ b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/test/regression/TestMain.java @@ -20,9 +20,10 @@ class FsMsgQueueTestContext extends AbstractTestContext { FsMsgQueue queue; @Override - public void init() throws Throwable { + public FsMsgQueueTestContext init() throws Throwable { deleteQDir(); messages = createTestMessages(msgCount); + return this; } public void deleteQDir() { @@ -32,6 +33,7 @@ class FsMsgQueueTestContext extends AbstractTestContext { deleteRecursively(qDir); if (qDir.exists()) throw new RuntimeException("Cant delete Dir:" + qDirPath); } + } public ArrayList createTestMessages(int count) { diff --git a/test/java/foundation/pEp/pitytest/TestContextInterface.java b/test/java/foundation/pEp/pitytest/TestContextInterface.java index 177bbb5..cd75f46 100644 --- a/test/java/foundation/pEp/pitytest/TestContextInterface.java +++ b/test/java/foundation/pEp/pitytest/TestContextInterface.java @@ -1,11 +1,17 @@ package foundation.pEp.pitytest; public interface TestContextInterface { - void init() throws Throwable; + TestContextInterface init() throws Throwable; + boolean isInitialized(); + void setInitialized(boolean initialized); + boolean isUninitializable(); + void setUninitializable(boolean uninitializable); + String getTestContextName(); + void setTestContextName(String name); } diff --git a/test/java/foundation/pEp/pitytest/TestSuite.java b/test/java/foundation/pEp/pitytest/TestSuite.java index f44d355..e5e5920 100644 --- a/test/java/foundation/pEp/pitytest/TestSuite.java +++ b/test/java/foundation/pEp/pitytest/TestSuite.java @@ -75,7 +75,7 @@ public class TestSuite { } public void add(TestUnit t) { - tests.add(t); + tests.add(t.copy()); } public void run() { diff --git a/test/java/foundation/pEp/pitytest/TestUnit.java b/test/java/foundation/pEp/pitytest/TestUnit.java index a14b4f0..d195cbb 100644 --- a/test/java/foundation/pEp/pitytest/TestUnit.java +++ b/test/java/foundation/pEp/pitytest/TestUnit.java @@ -44,20 +44,45 @@ public class TestUnit implements Runnable { add(TestSuite.getDefault()); } + //Shallow Copy + public TestUnit(TestUnit orig) { + testUnitName = orig.testUnitName; + ctx = orig.ctx; + lambda = orig.lambda; + result = orig.result; + state = orig.state; + lastException = orig.lastException; + verboseMode = orig.verboseMode; + testColor = orig.testColor; + logFmtPadding = orig.logFmtPadding; + logFmtMsgLen = orig.logFmtMsgLen; + logFmtTestDuration = orig.logFmtTestDuration; + lineWidthMin = orig.lineWidthMin; + logFmtTestNameLen = orig.logFmtTestNameLen; + logFmtCtxNameLen = orig.logFmtCtxNameLen; + } + + //Shallow Copy + public TestUnit copy() { + return new TestUnit<>(this); + } + public boolean isVerboseMode() { return verboseMode; } - public void setVerboseMode(boolean verboseMode) { + public TestUnit setVerboseMode(boolean verboseMode) { this.verboseMode = verboseMode; + return this; } public TermColor getTestColor() { return testColor; } - public void setTestColor(TermColor testColor) { + public TestUnit setTestColor(TermColor testColor) { this.testColor = testColor; + return this; } public TestState getResult() { @@ -65,7 +90,11 @@ public class TestUnit implements Runnable { } public Throwable getLastException() { - return lastException; + Throwable ret = new Throwable("No Exception caught"); + if(lastException != null) { + ret = lastException; + } + return ret; } public TestUnit add(TestSuite suite) { @@ -82,10 +111,15 @@ public class TestUnit implements Runnable { return ctx; } + public TestUnit setContext(T ctx) { + this.ctx = ctx; + return this; + } + public void run() { TestUtils.standardOutErrEnabled(verboseMode); if (ctx.isUninitializable()) { - setTestState(TestState.CTX_FAIL); + setTestState(TestState.SKIPPED); TestUtils.standardOutErrEnabled(true); } else { try { @@ -147,18 +181,17 @@ public class TestUnit implements Runnable { } case CTX_FAIL: { setTestResult(TestState.SKIPPED); -// logH1(makeLogString()); break; } } } private void setTestResult(TestState r) { - assert (r == TestState.SKIPPED || r == TestState.FAILED || r == TestState.SUCCESS || r == TestState.UNEVALUATED ): "PityTest Internal: illegal result value '" + r +"'"; + assert (r == TestState.SKIPPED || r == TestState.FAILED || r == TestState.SUCCESS || r == TestState.UNEVALUATED) : "PityTest Internal: illegal result value '" + r + "'"; result = r; TestUtils.standardOutErrEnabled(true); logH1(makeLogString()); - if (result == TestState.FAILED || result == TestState.CTX_FAIL) { + if (result == TestState.FAILED || state == TestState.CTX_FAIL) { log("ERROR: " + getLastException().toString()); } if (verboseMode) logRaw("\n\n"); @@ -193,7 +226,7 @@ public class TestUnit implements Runnable { DecimalFormat f = new DecimalFormat("0.000"); String durationFmtd = f.format(testDuration.toMillis() / 1000.0); strTestDuration = TestUtils.padOrClipString(" [" + durationFmtd + " sec] ", "=", logFmtTestDuration, TestUtils.Alignment.Right, ".. "); - } else { + } else { strTestDuration = TestUtils.padOrClipString("", "=", logFmtTestDuration, TestUtils.Alignment.Right, ".. "); } diff --git a/test/java/foundation/pEp/pitytest/examples/ctxinitfail/TestMain.java b/test/java/foundation/pEp/pitytest/examples/ctxinitfail/TestMain.java index 8f787de..b0100a9 100644 --- a/test/java/foundation/pEp/pitytest/examples/ctxinitfail/TestMain.java +++ b/test/java/foundation/pEp/pitytest/examples/ctxinitfail/TestMain.java @@ -1,8 +1,10 @@ package foundation.pEp.pitytest.examples.ctxinitfail; -import static foundation.pEp.pitytest.TestLogger.*; +import foundation.pEp.pitytest.AbstractTestContext; +import foundation.pEp.pitytest.TestSuite; +import foundation.pEp.pitytest.TestUnit; -import foundation.pEp.pitytest.*; +import static foundation.pEp.pitytest.TestLogger.log; class CtxInitFailContext extends AbstractTestContext { @@ -10,9 +12,13 @@ class CtxInitFailContext extends AbstractTestContext { int result; @Override - public void init() throws Throwable { + public CtxInitFailContext init() throws RuntimeException { name = "PityTest"; + log("Hello World from: " + name); +// throw new RuntimeException("regddjkl"); result = 50 / 0; + + return this; } } diff --git a/test/java/foundation/pEp/pitytest/examples/ctxmembers/TestMain.java b/test/java/foundation/pEp/pitytest/examples/ctxmembers/TestMain.java index 7fcce08..11228c9 100644 --- a/test/java/foundation/pEp/pitytest/examples/ctxmembers/TestMain.java +++ b/test/java/foundation/pEp/pitytest/examples/ctxmembers/TestMain.java @@ -18,12 +18,13 @@ class CtxMembersTestContext extends AbstractTestContext { ExampleCtxMember incorrect = new ExampleCtxMember(false); // WRONG @Override - public void init() throws Throwable { + public CtxMembersTestContext init() throws Throwable { log("=== OUTPUT FROM TEST CONTEXT INIT BEGIN ==="); log(getTestContextName()+ " - init() called"); correct = new ExampleCtxMember(true); name = "PityTest"; log("=== OUTPUT FROM TEST CONTEXT INIT END ==="); + return this; } } diff --git a/test/java/foundation/pEp/pitytest/examples/helloworld/TestMain.java b/test/java/foundation/pEp/pitytest/examples/helloworld/TestMain.java index 1924aee..16044b1 100644 --- a/test/java/foundation/pEp/pitytest/examples/helloworld/TestMain.java +++ b/test/java/foundation/pEp/pitytest/examples/helloworld/TestMain.java @@ -7,8 +7,9 @@ class HelloWorldTestContext extends AbstractTestContext { String name; @Override - public void init() throws Throwable { + public HelloWorldTestContext init() throws Throwable { name = "PityTest"; + return this; } } diff --git a/test/java/foundation/pEp/pitytest/examples/testsuite/TestMain.java b/test/java/foundation/pEp/pitytest/examples/testsuite/TestMain.java index 06d2485..af96af1 100644 --- a/test/java/foundation/pEp/pitytest/examples/testsuite/TestMain.java +++ b/test/java/foundation/pEp/pitytest/examples/testsuite/TestMain.java @@ -6,8 +6,9 @@ class TestSuiteContext extends AbstractTestContext { String name; @Override - public void init() throws Throwable { + public TestSuiteContext init() throws Throwable { name = "PityTest"; + return this; } } diff --git a/test/java/foundation/pEp/pitytest/utils/Pair.java b/test/java/foundation/pEp/pitytest/utils/Pair.java index 220775b..146a286 100644 --- a/test/java/foundation/pEp/pitytest/utils/Pair.java +++ b/test/java/foundation/pEp/pitytest/utils/Pair.java @@ -24,5 +24,11 @@ public class Pair { key = f; value = s; } + + public String toString() { + String ret=""; + ret += "'" + key.toString() + "' : '" + value.toString() + "'"; + return ret; + } } diff --git a/test/java/foundation/pEp/pitytest/utils/TestUtils.java b/test/java/foundation/pEp/pitytest/utils/TestUtils.java index f18d6b4..159ee34 100644 --- a/test/java/foundation/pEp/pitytest/utils/TestUtils.java +++ b/test/java/foundation/pEp/pitytest/utils/TestUtils.java @@ -7,10 +7,7 @@ import java.io.PrintStream; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; +import java.util.*; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -330,5 +327,102 @@ public class TestUtils { return Math.max(min, Math.min(max, val)); } + /* + Random data generators + */ + + public static int randomInt(int min, int max) { + int ret = 0; + int range = max - min; + if (range <= 0) { + range = 1; + } + Random rand = new Random(); + ret = rand.nextInt(range) + min; + return ret; + } + + public static String randomASCIIString(CharClass charClass, int len) { + byte[] array = new byte[len]; // length is bounded by 7 + int rangeMin = 0; + int rangeMax = 0; + + switch (charClass) { + case Unbounded: { + rangeMin = 0; + rangeMax = 255; + break; + } + case Alpha: { + rangeMin = 65; + rangeMax = 122; + break; + } + case Numeric: { + rangeMin = 48; + rangeMax = 57; + break; + } + case Alphanumeric: { + rangeMin = 48; + rangeMax = 122; + break; + } + } + new Random().nextBytes(array); + for (int i = 0; i < array.length; i++) { + array[i] = (byte)clip(randomInt(rangeMin, rangeMax),0,255); + } + String generatedString = new String(array, Charset.forName("UTF-8")); + return generatedString; + } + + public enum CharClass { + Unbounded(0) { + @Override + public String toString() { + return "Unbounded"; + } + }, + Alpha(1) { + @Override + public String toString() { + return "Alpha"; + } + }, + Numeric(2) { + @Override + public String toString() { + return "Numeric"; + } + }, + Alphanumeric(3) { + @Override + public String toString() { + return "Alphanumeric"; + } + }; + + public final int value; + + private static HashMap intMap; + + private CharClass(int value) { + this.value = value; + } + + public static CharClass getByInt(int value) { + if (intMap == null) { + intMap = new HashMap(); + for (CharClass s : CharClass.values()) { + intMap.put(s.value, s); + } + } + if (intMap.containsKey(value)) { + return intMap.get(value); + } + return null; + } + } } diff --git a/utils/extract_pEp_status_codes_from_engine.sh b/utils/gen_status_codes.sh old mode 100755 new mode 100644 similarity index 73% rename from utils/extract_pEp_status_codes_from_engine.sh rename to utils/gen_status_codes.sh index 112d200..238db98 --- a/utils/extract_pEp_status_codes_from_engine.sh +++ b/utils/gen_status_codes.sh @@ -6,15 +6,14 @@ if [ ! -f $1 ]; then echo -e "\e[1m\e[31mInput file not found!\e[0m" fi -if [ -z "$2" ]; then - echo -e "\e[1m\e[31mNo output file supplied\e[0m" -fi - -if [ "$#" -ne 2 ]; then - echo "Expected use is: $0 input_file output_file" +if [ "$#" -ne 1 ]; then + echo "Expected use is: $0 input_file" exit 1 fi +STAT_FILE=status_list.yml2 +PASS_FILE=passphrase_status_list.yml2 + ################################################################################ # Select GNU SED # ################################################################################ @@ -33,15 +32,17 @@ esac # Transform input file PEP_STATUS to yml2 status # ################################################################################ -$SED -n '/} PEP_STATUS/q;p' $1 > $2 -$SED -i -n '/STATUS_OK/,$p' $2 -$SED -i -e 's/\(.*\)/\L\1/' $2 -$SED -i "-e s/ pep/ pEp/g" $2 -$SED -i s/=/\>/g $2 -$SED -i s/,//g $2 +$SED -n '/} PEP_STATUS/q;p' $1 > $STAT_FILE +$SED -i -n '/STATUS_OK/,$p' $STAT_FILE +$SED -i -e 's/\(.*\)/\L\1/' $STAT_FILE +$SED -i "-e s/ pep/ pEp/g" $STAT_FILE +$SED -i s/=/\>/g $STAT_FILE +$SED -i s/,//g $STAT_FILE +grep -i passphrase $STAT_FILE | sed -e 's/^/enumitem/' > $PASS_FILE ################################################################################ # Show results # ################################################################################ -cat $2 +cat $STAT_FILE +cat $PASS_FILE