feat: add pushes seqnos and test for push content

This commit is contained in:
0x330a 2022-12-01 17:52:38 +11:00
parent 420edc931b
commit f0aea7aaa9
No known key found for this signature in database
GPG Key ID: 267811D6E6A2698C
4 changed files with 83 additions and 7 deletions

View File

@ -27,6 +27,23 @@ class ExampleInstrumentedTest {
userProfile.free() userProfile.free()
} }
@Test
fun jni_user_profile_c_api() {
val userProfile = UserProfile.newInstance()
assertFalse(userProfile.needsPush())
assertFalse(userProfile.needsDump())
val name = userProfile.getName()
assertNull(name)
val (toPush, seqNo) = userProfile.push()
assertEquals("d1:#i0e1:&de1:<le1:=dee", toPush)
assertEquals(0, seqNo)
userProfile.free()
}
@Test @Test
fun jni_setting_getting() { fun jni_setting_getting() {
val userProfile = UserProfile.newInstance() val userProfile = UserProfile.newInstance()

View File

@ -31,25 +31,26 @@ Java_network_loki_messenger_libsession_1util_UserProfile_00024Companion_newInsta
extern "C" JNIEXPORT void JNICALL extern "C" JNIEXPORT void JNICALL
Java_network_loki_messenger_libsession_1util_UserProfile_setName( Java_network_loki_messenger_libsession_1util_UserProfile_setName(
JNIEnv* env, JNIEnv* env,
jobject obj, jobject thiz,
jstring newName) { jstring newName) {
auto profile = ptrToProfile(env, obj); auto profile = ptrToProfile(env, thiz);
profile->set_name(env->GetStringUTFChars(newName, nullptr)); profile->set_name(env->GetStringUTFChars(newName, nullptr));
} }
extern "C" extern "C"
JNIEXPORT jstring JNICALL JNIEXPORT jstring JNICALL
Java_network_loki_messenger_libsession_1util_UserProfile_getName(JNIEnv *env, jobject obj) { Java_network_loki_messenger_libsession_1util_UserProfile_getName(JNIEnv *env, jobject thiz) {
auto profile = ptrToProfile(env, obj); auto profile = ptrToProfile(env, thiz);
auto name = profile->get_name(); auto name = profile->get_name();
if (name == nullptr) return nullptr;
jstring returnString = env->NewStringUTF(name->c_str()); jstring returnString = env->NewStringUTF(name->c_str());
return returnString; return returnString;
} }
extern "C" extern "C"
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_network_loki_messenger_libsession_1util_UserProfile_free(JNIEnv *env, jobject obj) { Java_network_loki_messenger_libsession_1util_UserProfile_free(JNIEnv *env, jobject thiz) {
auto profile = ptrToProfile(env, obj); auto profile = ptrToProfile(env, thiz);
delete profile; delete profile;
} }
@ -59,3 +60,28 @@ Java_network_loki_messenger_libsession_1util_ConfigBase_dirty(JNIEnv *env, jobje
auto* configBase = ptrToConfigBase(env, thiz); auto* configBase = ptrToConfigBase(env, thiz);
return configBase->is_dirty(); return configBase->is_dirty();
} }
extern "C"
JNIEXPORT jboolean JNICALL
Java_network_loki_messenger_libsession_1util_ConfigBase_needsPush(JNIEnv *env, jobject thiz) {
auto config = ptrToConfigBase(env, thiz);
return config->needs_push();
}
extern "C"
JNIEXPORT jboolean JNICALL
Java_network_loki_messenger_libsession_1util_ConfigBase_needsDump(JNIEnv *env, jobject thiz) {
auto config = ptrToConfigBase(env, thiz);
return config->needs_dump();
}
extern "C"
JNIEXPORT jobject JNICALL
Java_network_loki_messenger_libsession_1util_ConfigBase_push(JNIEnv *env, jobject thiz) {
auto config = ptrToConfigBase(env, thiz);
auto pair = config->push();
std::string to_push_str = pair.first;
jstring returnString = env->NewStringUTF(to_push_str.c_str());
jlong seqNo = pair.second;
jclass returnObjectClass = env->FindClass("network/loki/messenger/libsession_util/util/ConfigWithSeqNo");
jmethodID methodId = env->GetMethodID(returnObjectClass, "<init>", "(Ljava/lang/String;J)V");
jobject returnObject = env->NewObject(returnObjectClass, methodId, returnString, seqNo);
return returnObject;
}

View File

@ -1,5 +1,7 @@
package network.loki.messenger.libsession_util package network.loki.messenger.libsession_util
import network.loki.messenger.libsession_util.util.ConfigWithSeqNo
sealed class ConfigBase(protected val /* yucky */ pointer: Long) { sealed class ConfigBase(protected val /* yucky */ pointer: Long) {
companion object { companion object {
@ -8,6 +10,9 @@ sealed class ConfigBase(protected val /* yucky */ pointer: Long) {
} }
} }
external fun dirty(): Boolean external fun dirty(): Boolean
external fun needsPush(): Boolean
external fun needsDump(): Boolean
external fun push(): ConfigWithSeqNo
} }
class UserProfile(pointer: Long): ConfigBase(pointer) { class UserProfile(pointer: Long): ConfigBase(pointer) {
@ -18,6 +23,6 @@ class UserProfile(pointer: Long): ConfigBase(pointer) {
external fun newInstance(): UserProfile external fun newInstance(): UserProfile
} }
external fun setName(newName: String) external fun setName(newName: String)
external fun getName(): String external fun getName(): String?
external fun free() external fun free()
} }

View File

@ -0,0 +1,28 @@
package network.loki.messenger.libsession_util.util
data class StringWithLen(private val bytes: ByteArray, private val len: Long) { // We might not need this class, could be helpful though
override fun toString(): String = bytes.decodeToString()
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as StringWithLen
if (!bytes.contentEquals(other.bytes)) return false
if (len != other.len) return false
return true
}
override fun hashCode(): Int {
var result = bytes.contentHashCode()
result = 31 * result + len.hashCode()
return result
}
}
data class ConfigWithSeqNo(val config: String, val seqNo: Long)