Volker Birk 7 years ago
parent
commit
047dabbf84
  1. 2
      android/build.gradle
  2. 2
      android/src/org/pEp/jniadapter/AndroidHelper.java
  3. 22
      src/AdapterTest.java
  4. 6
      src/Makefile
  5. 3
      src/Makefile.conf
  6. 14
      src/SyncCallbacks.java
  7. 1
      src/gen_cpp_Engine.ysl2
  8. 7
      src/gen_java_Engine.ysl2
  9. 26
      src/gen_java_Message.ysl2
  10. 14
      src/org/pEp/jniadapter/AbstractEngine.java
  11. 30
      src/org/pEp/jniadapter/Sync.java
  12. 71
      src/org_pEp_jniadapter_AbstractEngine.cc
  13. 16
      src/pEp.yml2
  14. 3
      test/Makefile
  15. 24
      test/SyncCallbacks.java
  16. 70
      test/Testing.java

2
android/build.gradle

@ -31,7 +31,7 @@ apply plugin: 'com.android.library'
android {
compileSdkVersion 21
buildToolsVersion '28.0.2'
buildToolsVersion '28.0.3'
defaultConfig {
minSdkVersion 18

2
android/src/org/pEp/jniadapter/AndroidHelper.java

@ -29,7 +29,7 @@ public class AndroidHelper {
private static File tmpDir;
// TODO : Increment when needed.
public static int ENGINE_VERSION_CODE = 3063;
public static int ENGINE_VERSION_CODE = 3104;
private static File shareDir;

22
src/AdapterTest.java

@ -1,21 +1,39 @@
import org.pEp.jniadapter.*;
class AdapterTest {
private final static String PEP_OWN_USER_ID = "pEp_own_userId";
public static void main(String[] args)
{
System.out.println("running...");
Engine pEp;
try {
System.out.println("Creating identity");
Identity alice = new Identity();
alice.username = "Alice Test";
alice.address = "pep.test.bla@bla.org";
alice.user_id = PEP_OWN_USER_ID;
System.out.println("Identity Created");
System.out.println("Create engine instance");
pEp = new Engine();
System.out.println("Setting callbacks");
SyncCallbacks callbacks = new SyncCallbacks();
pEp.setNotifyHandshakeCallback(callbacks);
pEp.setMessageToSendCallback(callbacks);
System.out.println("Calling myself");
alice = pEp.myself(alice);
System.out.println("Myself called : " + alice.fpr);
}
catch (pEpException e) {
System.out.println("ERROR: cannot initialize engine");
System.exit(1);
}
SyncCallbacks callbacks = new SyncCallbacks();
System.out.println("... shutting down");
}
}

6
src/Makefile

@ -3,7 +3,7 @@ SHARED=libpEpJNI.so
JAR=pEp.jar
include Makefile.conf
CXXFLAGS+=-std=c++14
CXXFLAGS+=-std=c++14 -fPIC
all: $(JAR) $(SHARED)
@ -50,8 +50,8 @@ $(LIBRARY): org_pEp_jniadapter_AbstractEngine.o org_pEp_jniadapter_Engine.o org_
ar -r $@ *.o
$(SHARED): $(LIBRARY)
$(CXX) $(CXXFLAGS) -shared -o $(SHARED) $(LDFLAGS) -lpEpEngine -lpEpAdapter *.o
$(CXX) $(CXXFLAGS) -shared -o $(SHARED) $(LDFLAGS) -lpEpEngine $(PEP_ADAPTER_OBJECT_LIB)/*.o *.o
#FIXME ^ use libpEpAdapter.a instad of the bunch of *.o
org/pEp/jniadapter/pEpException.java: pEp.yml2 gen_java_exceptions.ysl2
yml2proc -y gen_java_exceptions.ysl2 $< -o $@

3
src/Makefile.conf

@ -1,3 +1,4 @@
CXXFLAGS=-I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/darwin -I$(HOME)/include -fpermissive
CXXFLAGS=-I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/darwin -I$(HOME)/include -fpermissive -DDISABLE_SYNC
LDFLAGS=-L$(HOME)/lib
SHARED=libpEpJNI.dylib
PEP_ADAPTER_OBJECT_LIB=$(HOMEI)/pEp/libpEpAdapter

14
src/SyncCallbacks.java

@ -1,14 +0,0 @@
import org.pEp.jniadapter.*;
class SyncCallbacks implements Sync.MessageToSendCallback, Sync.notifyHandshakeCallback {
public void messageToSend(Message message)
{
System.out.println(message.getFrom());
}
public void notifyHandshake(Identity myself, Identity partner, SyncHandshakeSignal signal)
{
}
}

1
src/gen_cpp_Engine.ysl2

@ -13,6 +13,7 @@ tstylesheet {
#include <pEp/message_api.h>
#include <pEp/blacklist.h>
#include <pEp/openpgp_compat.h>
#include <pEp/key_reset.h>
#include <pEp/Adapter.hh>
#include "org_pEp_jniadapter_«@name».h"
#include "throw_pEp_exception.hh"

7
src/gen_java_Engine.ysl2

@ -278,7 +278,12 @@ tstylesheet {
when "$type = 'string' or $type = 'stringlist'"
| ¡$type _«$name» = AbstractEngine.toUTF8(«$name»);
when "$type = 'identity'"
| _Identity _«$name» = new _Identity(«$name»);
||
_Identity _«$name» = null;
if («$name» != null) {
_«$name» = new _Identity(«$name»);
}
||
}
}

26
src/gen_java_Message.ysl2

@ -16,8 +16,10 @@ tstylesheet {
import java.util.Vector;
import java.util.Date;
import java.util.HashMap;
import java.io.Serializable;
public class «$cname» implements AutoCloseable {
public class «$cname» implements AutoCloseable, Serializable {
private static final long serialVersionUID = 2119420428331150924L;
private long handle;
native long init();
@ -161,7 +163,27 @@ tstylesheet {
||
}
when "$itype != $type"
||
private native «$itype» _get«$name»();
private native void _set«$name»(«$itype» value);
public «$type» get«$name»() {
«$itype» res = _get«$name»();
if(res != null)
return AbstractEngine.toUTF16(res);
else
return null;
}
public void set«$name»(«$type» value) {
if(value != null)
_set«$name»(AbstractEngine.toUTF8(value));
else
_set«$name»(new byte[0]);
}
||
when "$itype != $type"
||
private native «$itype» _get«$name»();

14
src/org/pEp/jniadapter/AbstractEngine.java

@ -2,6 +2,9 @@ package org.pEp.jniadapter;
import java.util.ArrayList;
import java.util.Vector;
import org.pEp.jniadapter.Sync.DefaultCallback;
import java.io.UnsupportedEncodingException;
import java.text.Normalizer;
@ -11,9 +14,10 @@ abstract class AbstractEngine implements AutoCloseable {
}
private Sync.MessageToSendCallback messageToSendCallback;
private Sync.notifyHandshakeCallback notifyHandshakeCallback;
private Sync.NotifyHandshakeCallback notifyHandshakeCallback;
private Sync.NeedsFastPollCallback needsFastPollCallback;
private final static DefaultCallback defaultCallback = new DefaultCallback();
private native void init();
private native void release();
@ -144,7 +148,7 @@ abstract class AbstractEngine implements AutoCloseable {
this.messageToSendCallback = messageToSendCallback;
}
public void setnotifyHandshakeCallback(Sync.notifyHandshakeCallback notifyHandshakeCallback) {
public void setNotifyHandshakeCallback(Sync.NotifyHandshakeCallback notifyHandshakeCallback) {
this.notifyHandshakeCallback = notifyHandshakeCallback;
}
@ -156,7 +160,7 @@ abstract class AbstractEngine implements AutoCloseable {
if (needsFastPollCallback != null) {
needsFastPollCallback.needsFastPollCallFromC(fast_poll_needed);
} else {
throw new RuntimeException("Callback not set");
defaultCallback.needsFastPollCallFromC(fast_poll_needed);
}
return 0;
}
@ -168,7 +172,7 @@ abstract class AbstractEngine implements AutoCloseable {
if (notifyHandshakeCallback != null) {
notifyHandshakeCallback.notifyHandshake(myself, partner, _signal);
} else {
throw new RuntimeException("Callback not set");
defaultCallback.notifyHandshake(myself, partner, _signal);
}
return 0;
}
@ -177,7 +181,7 @@ abstract class AbstractEngine implements AutoCloseable {
if (messageToSendCallback != null) {
messageToSendCallback.messageToSend(message);
} else {
throw new RuntimeException("Callback not set");
defaultCallback.messageToSend(message);
}
return 0;
}

30
src/org/pEp/jniadapter/Sync.java

@ -4,10 +4,6 @@ import org.pEp.jniadapter.Identity;
import org.pEp.jniadapter.Message;
import org.pEp.jniadapter.pEpException;
/**
* Created by huss on 02/09/16.
*/
public interface Sync {
@ -19,8 +15,32 @@ public interface Sync {
void messageToSend(Message message);
}
interface notifyHandshakeCallback {
interface NotifyHandshakeCallback {
void notifyHandshake(Identity myself, Identity partner, SyncHandshakeSignal signal);
}
public class DefaultCallback
implements Sync.MessageToSendCallback, Sync.NotifyHandshakeCallback, Sync.NeedsFastPollCallback {
//FIXME: Find where this Exceptions ara catched as they are called but on
//Testing.java there is no crash not crashing
@Override
public void needsFastPollCallFromC(Boolean fast_poll_needed) {
System.out.println("Throwing illegal");
throw new RuntimeException("Fast poll Callback not set");
}
@Override
public void messageToSend(Message message) {
System.out.println("Throwing illegal");
throw new RuntimeException("Message to send not set");
}
@Override
public void notifyHandshake(Identity myself, Identity partner, SyncHandshakeSignal signal) {
System.out.println("Throwing illegal");
throw new RuntimeException("Notify Handshake not set");
}
}
}

71
src/org_pEp_jniadapter_AbstractEngine.cc

@ -19,6 +19,13 @@ std::ofstream debug_log("debug.log");
#include "throw_pEp_exception.hh"
#include "jniutils.hh"
#ifdef ANDROID
#define ATTACH_CURRENT_THREAD(env, args) jvm->AttachCurrentThread(&env, args);
#else
#define ATTACH_CURRENT_THREAD(env, args) jvm->AttachCurrentThread((void **) &env, args);
#endif
namespace pEp {
using namespace pEp::JNIAdapter;
using namespace pEp::Adapter;
@ -47,15 +54,15 @@ namespace pEp {
JNIEnv *_env;
if (on_sync_thread()) {
_env = first_env;
// FIXME: REview if this is the way to go or not
int getEnvStat = jvm->GetEnv((void **) &_env, JNI_VERSION_1_6);
if (getEnvStat == JNI_EDETACHED) {
ATTACH_CURRENT_THREAD(_env, nullptr);
}
}
else {
if (!thread_env) {
#ifdef ANDROID
jvm->AttachCurrentThread(&thread_env, nullptr);
#else
jvm->AttachCurrentThread((void **) &thread_env, nullptr);
#endif
ATTACH_CURRENT_THREAD(thread_env, nullptr);
}
_env = thread_env;
}
@ -65,22 +72,12 @@ namespace pEp {
void startup_sync()
{
needsFastPollMethodID = env()->GetMethodID(
_clazz,
"needsFastPollCallFromC",
"(Z)I");
assert(needsFastPollMethodID);
notifyHandShakeMethodID = env()->GetMethodID(
_clazz,
"notifyHandshakeCallFromC",
"(Lorg/pEp/jniadapter/_Identity;Lorg/pEp/jniadapter/_Identity;Lorg/pEp/jniadapter/SyncHandshakeSignal;)I");
assert(notifyHandShakeMethodID);
}
void shutdown_sync()
{
env()->DeleteLocalRef(messageClass);
env()->DeleteGlobalRef(messageClass);
jvm->DetachCurrentThread();
}
};
@ -92,6 +89,8 @@ namespace pEp {
jobject msg_ = nullptr;
jint result = 0;
if (!o)
o = new JNISync();
msg_ = o->env()->NewObject(messageClass, messageConstructorMethodID, (jlong) msg);
result = o->env()->CallIntMethod(obj, messageToSendMethodID, msg_);
@ -150,7 +149,7 @@ extern "C" {
env->GetJavaVM(&jvm);
thread_env = env;
obj = me;
obj = env->NewGlobalRef(me);
_clazz = env->GetObjectClass(obj);
if (!o)
@ -170,7 +169,7 @@ extern "C" {
if (!messageConstructorMethodID)
messageConstructorMethodID = env->GetMethodID(messageClass, "<init>", "(J)V");
if (!messageToSendMethodID) {
messageToSendMethodID = env->GetMethodID(
engineClass,
@ -179,7 +178,32 @@ extern "C" {
assert(messageToSendMethodID);
}
startup<JNISync>(messageToSend, notifyHandshake, o, &JNISync::startup_sync, &JNISync::shutdown_sync);
if (!needsFastPollMethodID) {
needsFastPollMethodID = env->GetMethodID(
engineClass,
"needsFastPollCallFromC",
"(Z)I");
assert(needsFastPollMethodID);
}
if (!notifyHandShakeMethodID) {
notifyHandShakeMethodID = env->GetMethodID(
engineClass,
"notifyHandshakeCallFromC",
"(Lorg/pEp/jniadapter/_Identity;Lorg/pEp/jniadapter/_Identity;Lorg/pEp/jniadapter/SyncHandshakeSignal;)I");
assert(notifyHandShakeMethodID);
}
thread_local static PEP_SESSION _session = nullptr;
PEP_STATUS status = PEP_STATUS_OK;
#ifdef DISABLE_SYNC
_messageToSend = messageToSend;
session();
#else
startup<JNISync>(messageToSend, notifyHandshake, o, &JNISync::startup_sync, &JNISync::shutdown_sync);
#endif
}
JNIEXPORT void JNICALL Java_org_pEp_jniadapter_AbstractEngine_release(
@ -188,7 +212,12 @@ extern "C" {
)
{
shutdown();
env->DeleteLocalRef(_clazz);
env->DeleteGlobalRef(identityClass);
env->DeleteGlobalRef(signalClass);
env->DeleteGlobalRef(engineClass);
env->DeleteGlobalRef(obj);
session(pEp::Adapter::release);
delete o;
}

16
src/pEp.yml2

@ -21,14 +21,17 @@ namespace pEp {
pEp_init_sqlite3_without_mutex > 0x0120
pEp_init_cannot_open_db > 0x0121
pEp_init_cannot_open_system_db > 0x0122
pEp_unknown_db_error > 0x01ff
pEp_key_not_found > 0x0201
pEp_key_has_ambig_name > 0x0202
pEp_get_key_failed > 0x0203
pEp_cannot_export_key > 0x0204
pEp_cannot_edit_key > 0x0205
pEp_key_unsuitable > 0x0206
pEp_malformed_key_reset_msg > 0x0210
pEp_key_not_reset > 0x0211
pEp_cannot_find_identity > 0x0301
pEp_cannot_set_person > 0x0381
pEp_cannot_set_pgp_keypair > 0x0382
@ -70,6 +73,7 @@ namespace pEp {
pEp_sync_no_inject_callback > 0x0903
pEp_sync_no_channel > 0x0904
pEp_sync_cannot_encrypt > 0x0905
pEp_sync_no_message_send_callback > 0x0906
pEp_cannot_increase_sequence > 0x0971
@ -80,6 +84,7 @@ namespace pEp {
pEp_statemachine_invalid_condition > 0x0984
pEp_statemachine_invalid_action > 0x0985
pEp_statemachine_inhibited_event > 0x0986
pEp_statemachine_cannot_send > 0x0987
pEp_commit_failed > 0xff01
pEp_message_consume > 0xff02
@ -257,7 +262,12 @@ namespace pEp {
returns string languagelist
);
basic string trustwords(identity ident);
method key_reset(
in string fpr,
in identity ident
);
basic string trustwords(identity ident);
basic identity myself(identity ident);
basic identity updateIdentity(identity ident);
basic identity setOwnKey(identity ident, string fpr);

3
test/Makefile

@ -1,7 +1,8 @@
CLASSPATH=.:../src
#VM=java -Xcheck:jni -Djava.library.path=../src
VM=java -Djava.library.path=../src
test: Testing.class
test: Testing.class SyncCallbacks.class
CLASSPATH=$(CLASSPATH) $(VM) Testing
%.class: %.java

24
test/SyncCallbacks.java

@ -0,0 +1,24 @@
import org.pEp.jniadapter.*;
class SyncCallbacks implements Sync.MessageToSendCallback, Sync.NotifyHandshakeCallback {
public void messageToSend(Message message)
{
System.out.println("================================");
System.out.println("Message to send called");
System.out.println("From: " + message.getFrom());
System.out.println("To: " + message.getTo());
System.out.println("Subject: " + message.getShortmsg());
System.out.println("================================");
}
public void notifyHandshake(Identity myself, Identity partner, SyncHandshakeSignal signal)
{
System.out.println("================================");
System.out.println("Notify handshake called");
System.out.println("Myself: " + myself);
System.out.println("Partner: " + partner);
System.out.println("Signal: " + signal);
System.out.println("================================");
}
}

70
test/Testing.java

@ -1,13 +1,27 @@
import org.pEp.jniadapter.*;
import java.util.Vector;
import java.net.URL;
import java.net.URLClassLoader;
class Testing {
public void printClassPath() {
ClassLoader cl = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader)cl).getURLs();
for(URL url: urls){
System.out.println(url.getFile());
}
}
public static void main(String[] args) {
Engine e;
// load
try {
e = new Engine();
SyncCallbacks callbacks = new SyncCallbacks();
e.setNotifyHandshakeCallback(callbacks);
e.setMessageToSendCallback(callbacks);
}
catch (pEpException ex) {
System.out.println("Cannot load");
@ -15,6 +29,18 @@ class Testing {
}
System.out.println("Test loaded");
// Keygen
System.out.println("Generating keys: ");
Identity user = new Identity();
user.user_id = "pEp_own_userId";
user.me = true;
user.username = "Test User";
user.address = "jniTestUser@peptest.ch";
user = e.myself(user);
System.out.print("Keys generated: ");
System.out.println(user.fpr);
// trustwords
Identity vb = new Identity();
vb.fpr = "DB4713183660A12ABAFA7714EBE90D44146F62F4";
@ -25,56 +51,70 @@ class Testing {
// message
Message msg = new Message();
Identity from = new Identity();
from.username = "Volker Birk";
from.address = "vb@dingens.org";
from.user_id = "23";
from.me = true;
msg.setFrom(from);
msg.setFrom(user);
Vector<Identity> to = new Vector<Identity>();
Identity to1 = new Identity();
to1.username = "Volker Birk";
to1.address = "vb@pep-project.org";
to1.user_id = "42";
to.add(to1);
//to1.username = "Volker Birk";
//to1.address = "vb@pep-project.org";
//to1.address = "android01@peptest.ch";
//to1.user_id = "42";
//to.add(to1);
to.add(user);
msg.setTo(to);
msg.setShortmsg("hello, world");
msg.setLongmsg("this is a test");
msg.setDir(Message.Direction.Outgoing);
Message enc = null;
try {
enc = e.encrypt_message(msg, null);
enc = e.encrypt_message(msg, null, Message.EncFormat.PEP);
System.out.println("encrypted");
}
catch (pEpException ex) {
System.out.println("cannot encrypt");
ex.printStackTrace();
}
System.out.println(enc.getLongmsg());
Vector<Blob> attachments = enc.getAttachments();
System.out.println(e.toUTF16(attachments.get(1).data));
msg.setDir(Message.Direction.Outgoing);
try {
System.out.println(e.outgoing_message_color(msg));
System.out.println(e.outgoing_message_rating(msg));
}
catch (pEpException ex) {
System.out.println("cannot measure outgoing message color");
System.out.println("cannot measure outgoing message rating");
}
Engine.decrypt_message_Return result = null;
try {
result = e.decrypt_message(enc);
result = e.decrypt_message(enc, new Vector<>(), 0);
System.out.println("decrypted");
}
catch (pEpException ex) {
System.out.println("cannot decrypt");
ex.printStackTrace();
}
System.out.println(result.dst.getShortmsg());
System.out.println(result.dst.getLongmsg());
System.out.println("TEST DONE - FINISHED");
try {
e.key_reset(null, null);
}
catch (pEpException ex) {
System.out.println("cannot reset all own keys");
ex.printStackTrace();
}
System.exit(0);
}
}

Loading…
Cancel
Save