Browse Source

Tests: "encrypt msg to self using imported key with passphrase"

JNI-111
heck 2 years ago
parent
commit
0eb4e3469b
3 changed files with 98 additions and 19 deletions
  1. +63
    -8
      test/java/foundation/pEp/jniadapter/test/jni111/TestAlice.java
  2. +25
    -7
      test/java/foundation/pEp/jniadapter/test/utils/AdapterBaseTestContext.java
  3. +10
    -4
      test/java/foundation/pEp/pitytest/utils/TestUtils.java

+ 63
- 8
test/java/foundation/pEp/jniadapter/test/jni111/TestAlice.java View File

@ -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<AdapterBaseTestContext>("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<AdapterBaseTestContext>("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<AdapterBaseTestContext>("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<AdapterBaseTestContext>("config_passphrase() wrong", jni111Ctx, ctx -> {
ctx.engine.config_passphrase("WRONG PASSPHRASE");
});
new TestUnit<AdapterBaseTestContext>("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<AdapterBaseTestContext>("config_passphrase", jni111Ctx, ctx -> {
ctx.engine.config_passphrase("chocolate");
ctx.engine.config_passphrase("Bar");
ctx.engine.config_passphrase("Foo");
new TestUnit<AdapterBaseTestContext>("config_passphrase() correct", jni111Ctx, ctx -> {
ctx.engine.config_passphrase("passphrase_alice");
});
new TestUnit<AdapterBaseTestContext>("config_passphrase_for_new_keys", jni111Ctx, ctx -> {
ctx.engine.config_passphrase_for_new_keys(true, "SUPERCOMPLICATEDPASSPHRASE");
new TestUnit<AdapterBaseTestContext>("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();


+ 25
- 7
test/java/foundation/pEp/jniadapter/test/utils/AdapterBaseTestContext.java View File

@ -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);
}
}

+ 10
- 4
test/java/foundation/pEp/pitytest/utils/TestUtils.java View File

@ -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;
}


Loading…
Cancel
Save