feat: add config merging

This commit is contained in:
0x330a 2022-12-08 17:29:15 +11:00
parent 09a9fce480
commit a6ebfd61c5
No known key found for this signature in database
GPG Key ID: 267811D6E6A2698C
3 changed files with 62 additions and 7 deletions

View File

@ -41,7 +41,8 @@ class InstrumentedTests {
@Test @Test
fun jni_user_profile_c_api() { fun jni_user_profile_c_api() {
val userProfile = UserProfile.newInstance(keyPair.secretKey) val edSk = keyPair.secretKey
val userProfile = UserProfile.newInstance(edSk)
// these should be false as empty config // these should be false as empty config
assertFalse(userProfile.needsPush()) assertFalse(userProfile.needsPush())
@ -123,7 +124,46 @@ class InstrumentedTests {
assertArrayEquals(expectedDump, dump) assertArrayEquals(expectedDump, dump)
val newConf = UserProfile.newInstance(edSk)
val accepted = newConf.merge(arrayOf(expectedPush1Encrypted))
assertEquals(1, accepted)
assertTrue(newConf.needsDump())
assertFalse(newConf.needsPush())
val _ignore = newConf.dump()
assertFalse(newConf.needsDump())
userProfile.setName("Nibbler")
newConf.setName("Raz")
newConf.setPic(UserPic("http://new.example.com/pic", "qwertyuio".encodeToByteArray()))
val conf = userProfile.push()
val conf2 = newConf.push()
val dump1 = userProfile.dump()
val dump2 = userProfile.dump()
assertFalse(conf.config.contentEquals(conf2.config))
newConf.merge(arrayOf(conf.config))
userProfile.merge(arrayOf(conf2.config))
assertTrue(newConf.needsPush())
assertTrue(userProfile.needsPush())
val newSeq1 = userProfile.push()
val newSeq2 = newConf.push()
assertEquals(3, newSeq1.seqNo)
assertEquals(3, newSeq2.seqNo)
assertEquals("Nibbler", newConf.getName())
assertEquals("Nibbler", userProfile.getName())
userProfile.free() userProfile.free()
newConf.free()
} }
@Test @Test

View File

@ -186,9 +186,23 @@ Java_network_loki_messenger_libsession_1util_ConfigBase_encryptionDomain(JNIEnv
return env->NewStringUTF(conf->encryption_domain()); return env->NewStringUTF(conf->encryption_domain());
} }
extern "C" extern "C"
JNIEXPORT jbyteArray JNICALL JNIEXPORT void JNICALL
Java_network_loki_messenger_libsession_1util_ConfigBase_decrypt(JNIEnv *env, jobject thiz, Java_network_loki_messenger_libsession_1util_ConfigBase_confirmPushed(JNIEnv *env, jobject thiz,
jbyteArray encrypted) { jlong seq_no) {
auto profile = ptrToProfile(env, thiz); auto conf = ptrToConfigBase(env, thiz);
auto encrypted_bytes = ustring_from_bytes(env, encrypted); conf->confirm_pushed(seq_no);
}
extern "C"
JNIEXPORT jint JNICALL
Java_network_loki_messenger_libsession_1util_ConfigBase_merge(JNIEnv *env, jobject thiz,
jobjectArray to_merge) {
auto conf = ptrToConfigBase(env, thiz);
size_t number = env->GetArrayLength(to_merge);
std::vector<session::ustring> configs = {};
for (int i = 0; i < number; i++) {
auto jArr = (jbyteArray) env->GetObjectArrayElement(to_merge, i);
auto bytes = ustring_from_bytes(env, jArr);
configs.push_back(bytes);
}
return conf->merge(configs);
} }

View File

@ -16,7 +16,8 @@ sealed class ConfigBase(protected val /* yucky */ pointer: Long) {
external fun push(): ConfigWithSeqNo external fun push(): ConfigWithSeqNo
external fun dump(): ByteArray external fun dump(): ByteArray
external fun encryptionDomain(): String external fun encryptionDomain(): String
external fun decrypt(encrypted: ByteArray): ByteArray external fun confirmPushed(seqNo: Long)
external fun merge(toMerge: Array<ByteArray>): Int
} }
class UserProfile(pointer: Long): ConfigBase(pointer) { class UserProfile(pointer: Long): ConfigBase(pointer) {