From 0eb4e3469be75b403d85677b393eb892c5a99e34 Mon Sep 17 00:00:00 2001 From: heck Date: Tue, 7 Jul 2020 01:26:59 +0200 Subject: [PATCH] Tests: "encrypt msg to self using imported key with passphrase" --- .../pEp/jniadapter/test/jni111/TestAlice.java | 71 ++++++++++++++++--- .../test/utils/AdapterBaseTestContext.java | 32 +++++++-- .../pEp/pitytest/utils/TestUtils.java | 14 ++-- 3 files changed, 98 insertions(+), 19 deletions(-) diff --git a/test/java/foundation/pEp/jniadapter/test/jni111/TestAlice.java b/test/java/foundation/pEp/jniadapter/test/jni111/TestAlice.java index 74bbf98..429c361 100644 --- a/test/java/foundation/pEp/jniadapter/test/jni111/TestAlice.java +++ b/test/java/foundation/pEp/jniadapter/test/jni111/TestAlice.java @@ -1,29 +1,84 @@ 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.pitytest.*; import foundation.pEp.pitytest.utils.TestUtils; import foundation.pEp.jniadapter.test.utils.*; +import java.util.Vector; + // https://pep.foundation/jira/browse/JNI-111 +class JNI111TestContext extends AdapterBaseTestContext { + @Override + public void init() throws Throwable { + super.init(); + alice = null; + bob = null; + } +} + class TestAlice { public static void main(String[] args) throws Exception { - TestSuite.getDefault().setVerbose(true); +// readKey(); + TestSuite.getDefault().setVerbose(false); TestSuite.getDefault().setTestColor(TestUtils.TermColor.GREEN); - AdapterBaseTestContext jni111Ctx = new AdapterBaseTestContext(); + AdapterBaseTestContext jni111Ctx = new JNI111TestContext(); + + new TestUnit("importKey()", jni111Ctx, ctx -> { + assert ctx.alice == null: "Alice is not null"; + ctx.alice = ctx.engine.importKey(ctx.keyAliceSecPassphrase).get(0); + log(AdapterTestUtils.identityToString(ctx.alice, true)); + assert ctx.alice != null: "Keyimport failed"; + }); + + new TestUnit("setOwnKey()", jni111Ctx, ctx -> { + ctx.alice.user_id = "23"; + ctx.alice = ctx.engine.setOwnKey(ctx.alice, ctx.alice.fpr); + log(AdapterTestUtils.identityToString(ctx.alice, true)); + assert ctx.alice.me == true; + assert ctx.alice.comm_type == CommType.PEP_ct_pEp; + }); + + new TestUnit("encrypt_message() -> pEpPassphraseRequired ", jni111Ctx, ctx -> { + try { + Message enc = ctx.engine.encrypt_message(ctx.msgToSelf, new Vector<>(), Message.EncFormat.PEP); + } catch (pEpException e) { + assert e instanceof pEpPassphraseRequired: "wrong exception type"; + return; + } + assert false:"encrypt_message() should have failed"; + }); + + new TestUnit("config_passphrase() wrong", jni111Ctx, ctx -> { + ctx.engine.config_passphrase("WRONG PASSPHRASE"); + }); + + new TestUnit("encrypt_message() -> pEpWrongPassphrase ", jni111Ctx, ctx -> { + try { + Message enc = ctx.engine.encrypt_message(ctx.msgToSelf, new Vector<>(), Message.EncFormat.PEP); + } catch (pEpException e) { + assert e instanceof pEpWrongPassphrase: "wrong exception type"; + return; + } + assert false:"encrypt_message() should have failed"; + }); - new TestUnit("config_passphrase", jni111Ctx, ctx -> { - ctx.engine.config_passphrase("chocolate"); - ctx.engine.config_passphrase("Bar"); - ctx.engine.config_passphrase("Foo"); + new TestUnit("config_passphrase() correct", jni111Ctx, ctx -> { + ctx.engine.config_passphrase("passphrase_alice"); }); - new TestUnit("config_passphrase_for_new_keys", jni111Ctx, ctx -> { - ctx.engine.config_passphrase_for_new_keys(true, "SUPERCOMPLICATEDPASSPHRASE"); + new TestUnit("encrypt_message() -> success", jni111Ctx, ctx -> { + assert ctx.msgToSelf.getEncFormat() == Message.EncFormat.None : "Orig msg not plain"; + Message enc = ctx.engine.encrypt_message(ctx.msgToSelf, new Vector<>(), Message.EncFormat.PEP); + assert enc.getEncFormat() == Message.EncFormat.PGPMIME :"Message not encrypted"; + assert !enc.getLongmsg().contains(ctx.msgToSelf.getLongmsg()): "Message not encrypted"; + log(AdapterTestUtils.msgToString(enc, false)); }); TestSuite.getDefault().run(); diff --git a/test/java/foundation/pEp/jniadapter/test/utils/AdapterBaseTestContext.java b/test/java/foundation/pEp/jniadapter/test/utils/AdapterBaseTestContext.java index aa38878..5c3fa10 100644 --- a/test/java/foundation/pEp/jniadapter/test/utils/AdapterBaseTestContext.java +++ b/test/java/foundation/pEp/jniadapter/test/utils/AdapterBaseTestContext.java @@ -37,17 +37,23 @@ public class AdapterBaseTestContext extends AbstractTestContext { public Identity carol; // Keys - public byte[] keyBobSec; - private String filenameBobSec = "../resources/test_keys/bob-sec.asc"; + public byte[] keyAlicePub; + private String filenameAlicePub = "../resources/test_keys/alice-pub-DE5DF92A358DCE5F.asc"; + + public byte[] keyAliceSec; + private String filenameAliceSec = "../resources/test_keys/alice-sec-DE5DF92A358DCE5F.asc"; + + public byte[] keyAlicePubPassphrase; + private String filenameAlicePubPassphrase = "../resources/test_keys/alice-passphrase-pub-BCBAC48800026D6F.asc"; + + public byte[] keyAliceSecPassphrase; + private String filenameAliceSecPassphrase = "../resources/test_keys/alice-passphrase-sec-BCBAC48800026D6F.asc"; public byte[] keyBobPub; private String filenameBobPub = "../resources/test_keys/bob-pub.asc"; - public byte[] keyAlicePub; - private String filenameAlicePub = "../resources/test_keys/alicenew-pub.asc"; - - public byte[] keyAliceSec; - private String filenameAliceSec = "../resources/test_keys/alicenew-sec.asc"; + public byte[] keyBobSec; + private String filenameBobSec = "../resources/test_keys/bob-sec.asc"; // Messages public Message msgToSelf; @@ -66,6 +72,12 @@ public class AdapterBaseTestContext extends AbstractTestContext { engine.setMessageToSendCallback(callbacks); engine.setNotifyHandshakeCallback(callbacks); + TestLogger.logH2("Machine directory: "); + TestLogger.log(engine.getMachineDirectory()); + TestLogger.logH2("User directory:" ); + TestLogger.log(engine.getUserDirectory()); + + alice = new Identity(); alice.user_id = "23"; alice.address = "alice@peptest.org"; @@ -94,6 +106,12 @@ public class AdapterBaseTestContext extends AbstractTestContext { path = Paths.get(filenameAliceSec); keyAliceSec = Files.readAllBytes(path); + + path = Paths.get(filenameAlicePubPassphrase); + keyAlicePubPassphrase = Files.readAllBytes(path); + + path = Paths.get(filenameAliceSecPassphrase); + keyAliceSecPassphrase = Files.readAllBytes(path); } } \ No newline at end of file diff --git a/test/java/foundation/pEp/pitytest/utils/TestUtils.java b/test/java/foundation/pEp/pitytest/utils/TestUtils.java index 2f89ed6..f6df12c 100644 --- a/test/java/foundation/pEp/pitytest/utils/TestUtils.java +++ b/test/java/foundation/pEp/pitytest/utils/TestUtils.java @@ -18,7 +18,8 @@ import java.util.stream.Collectors; public class TestUtils { // Pure static class - private TestUtils() { } + private TestUtils() { + } /* System @@ -79,13 +80,18 @@ public class TestUtils { public static boolean envVarExists(String var) { boolean ret = false; - if(System.getenv(var) != null) { + if (System.getenv(var) != null) { ret = true; } return ret; } + public static String readKey() { + String ret = null; + ret = System.console().readLine(); + return ret; + } /* Time Utils @@ -157,8 +163,8 @@ public class TestUtils { public static int stringLenMinusEscSeq(String str) { int ret; int escSeqCount = substringOccurencesCount(str, "\u001B["); - ret = str.length() - (escSeqCount * 3); - if(ret < 0) ret = 0; + ret = str.length() - (escSeqCount * 3); + if (ret < 0) ret = 0; return ret; }