commit 3719c2bb4b598acc28ea137ed8f03a7c8981ba18
parent 6f259f25167cd0e5f816f4bd17e9ac169d508f61
Author: t3sserakt <t3ss@posteo.de>
Date: Mon, 29 Dec 2025 15:42:55 +0100
WIP: database peer test
Diffstat:
2 files changed, 279 insertions(+), 13 deletions(-)
diff --git a/.idea/.gitignore b/.idea/.gitignore
@@ -0,0 +1,3 @@
+# Default ignored files
+/shelf/
+/workspace.xml
diff --git a/GNUnetMessenger/app/src/androidTest/java/org/gnunet/gnunetmessenger/ipc/GnunetChatRemoteTest.kt b/GNUnetMessenger/app/src/androidTest/java/org/gnunet/gnunetmessenger/ipc/GnunetChatRemoteTest.kt
@@ -2,12 +2,15 @@ package org.gnunet.gnunetmessenger.ipc
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.core.app.ApplicationProvider
+import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.test.runTest
+import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeout
import org.gnunet.gnunetmessenger.model.ChatAccount
import org.gnunet.gnunetmessenger.model.ChatContext
import org.gnunet.gnunetmessenger.model.ChatMessage
import org.gnunet.gnunetmessenger.model.ChatHandle
+import org.gnunet.gnunetmessenger.model.ChatGroup
import org.gnunet.gnunetmessenger.model.GnunetReturnValue
import org.gnunet.gnunetmessenger.model.MessengerApp
import org.gnunet.gnunetmessenger.service.GnunetChat
@@ -15,14 +18,23 @@ import org.gnunet.gnunetmessenger.service.boundimpl.GnunetChatBoundService
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
+import org.junit.Assert.assertNotNull
+import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
+import kotlinx.coroutines.delay
@RunWith(AndroidJUnit4::class)
class GnunetChatRemoteTest {
private val appContext = ApplicationProvider.getApplicationContext<android.content.Context>()
private val gnunetChat: GnunetChatBoundService = GnunetChatBoundService(appContext)
+ private var messageLog = mutableListOf<Pair<ChatContext, ChatMessage>>()
+
+ @Before
+ fun setUp() {
+ messageLog.clear()
+ }
@After
fun tearDown() {
@@ -35,17 +47,24 @@ class GnunetChatRemoteTest {
// 1. Chat starten – callback ignorieren wir erstmal
val handle: ChatHandle = gnunetChat.startChat(
messengerApp = MessengerApp()
- ) { _: ChatContext, _: ChatMessage ->
- // hier könntest du später Messages sammeln / asserten
+ ) { ctx: ChatContext, msg: ChatMessage ->
+ // Messages sammeln für Tests
+ messageLog.add(ctx to msg)
}
- // 2. Warten, bis der echte Handle vom Server da ist
- withTimeout(5_000) {
- gnunetChat.awaitReady(handle)
+ // 2. Wait for handle to be initialized with pointer from async operation
+ withContext(Dispatchers.Default) {
+ withTimeout(5_000) {
+ while (handle.pointer == 0L) {
+ delay(100)
+ }
+ }
}
+
+ // 3. Verify handle is ready
assertTrue("Handle.pointer sollte != 0 sein", handle.pointer != 0L)
- // 3. Remote-Call auf die Server-App: getProfileName()
+ // 4. Remote-Call auf die Server-App: getProfileName()
val profileName = gnunetChat.getProfileName(handle)
// Aktuell sollte der Default-Name aus deiner Session-Struktur kommen
@@ -54,8 +73,18 @@ class GnunetChatRemoteTest {
@Test
fun createAccount_then_iterateAccounts_sees_it() = runTest {
- val handle = gnunetChat.startChat(MessengerApp()) { _, _ -> }
- withTimeout(5_000) { gnunetChat.awaitReady(handle) }
+ val handle = gnunetChat.startChat(MessengerApp()) { ctx, msg ->
+ messageLog.add(ctx to msg)
+ }
+
+ // Wait for handle to be initialized
+ withContext(Dispatchers.Default) {
+ withTimeout(5_000) {
+ while (handle.pointer == 0L) {
+ delay(100)
+ }
+ }
+ }
val name = "MyTestAccount"
@@ -70,12 +99,247 @@ class GnunetChatRemoteTest {
accounts += acc
}
- withTimeout(3_000) {
- while (accounts.none { it.name == name }) {
- kotlinx.coroutines.delay(50)
+ withContext(Dispatchers.Default) {
+ withTimeout(3_000) {
+ while (accounts.none { it.name == name }) {
+ delay(50)
+ }
}
}
assertTrue(accounts.any { it.name == name })
}
-}
-\ No newline at end of file
+
+ @Test
+ fun testMultipleAccountCreation() = runTest {
+ val handle = gnunetChat.startChat(MessengerApp()) { ctx, msg ->
+ messageLog.add(ctx to msg)
+ }
+
+ // Wait for handle to be initialized
+ withTimeout(5_000) {
+ while (handle.pointer == 0L) {
+ delay(100)
+ }
+ }
+
+ val accountNames = listOf("Account1", "Account2", "Account3")
+
+ // Create multiple accounts
+ accountNames.forEach { name ->
+ val result = gnunetChat.createAccount(handle, name)
+ assertEquals("Failed to create account: $name", GnunetReturnValue.OK, result)
+ }
+
+ // Verify all accounts are returned
+ val accounts = mutableListOf<ChatAccount>()
+ gnunetChat.iterateAccounts(handle) { acc ->
+ accounts += acc
+ }
+
+ withContext(Dispatchers.Default) {
+ withTimeout(5_000) {
+ while (accounts.size < accountNames.size) {
+ delay(50)
+ }
+ }
+ }
+
+ accountNames.forEach { name ->
+ assertTrue("Account $name not found in iteration",
+ accounts.any { it.name == name })
+ }
+ }
+
+ @Test
+ fun testProfileNameUpdate() = runTest {
+ val handle = gnunetChat.startChat(MessengerApp()) { ctx, msg ->
+ messageLog.add(ctx to msg)
+ }
+
+ // Wait for handle to be initialized
+ withTimeout(5_000) {
+ while (handle.pointer == 0L) {
+ delay(100)
+ }
+ }
+
+ val newName = "TestProfileName"
+
+ // Update profile name
+ gnunetChat.setProfileName(handle, newName)
+
+ // Verify update
+ val retrievedName = gnunetChat.getProfileName(handle)
+ assertEquals(newName, retrievedName)
+ }
+
+ @Test
+ fun testGroupCreation() = runTest {
+ val handle = gnunetChat.startChat(MessengerApp()) { ctx, msg ->
+ messageLog.add(ctx to msg)
+ }
+
+ // Wait for handle to be initialized
+ withTimeout(5_000) {
+ while (handle.pointer == 0L) {
+ delay(100)
+ }
+ }
+
+ val groupName = "TestGroup"
+
+ // Create group
+ val group = gnunetChat.createGroup(handle, groupName)
+ assertNotNull("Group should not be null", group)
+ assertEquals(groupName, group.name)
+
+ // Verify group appears in iteration
+ val groups = mutableListOf<ChatGroup>()
+ gnunetChat.iterateGroups(handle) { grp ->
+ groups += grp
+ 0 // Return GNUNet_OK (Int as required by interface)
+ }
+
+ withContext(Dispatchers.Default) {
+ withTimeout(3_000) {
+ while (groups.none { it.name == groupName }) {
+ delay(50)
+ }
+ }
+ }
+
+ assertTrue("Group $groupName not found in iteration",
+ groups.any { it.name == groupName })
+ }
+
+ @Test
+ fun testContactsIteration() = runTest {
+ val handle = gnunetChat.startChat(MessengerApp()) { ctx, msg ->
+ messageLog.add(ctx to msg)
+ }
+
+ // Wait for handle to be initialized
+ withTimeout(5_000) {
+ while (handle.pointer == 0L) {
+ delay(100)
+ }
+ }
+
+ val contacts = mutableListOf<String>()
+
+ gnunetChat.iterateContacts(handle) { contact ->
+ contacts.add(contact.name ?: "Unknown")
+ 0 // Return GNUNet_OK (Int as required by interface)
+ }
+
+ withContext(Dispatchers.Default) {
+ withTimeout(3_000) {
+ while (contacts.isEmpty()) {
+ delay(50)
+ }
+ }
+ }
+
+ assertTrue("Should receive at least one contact", contacts.isNotEmpty())
+ }
+
+ @Test
+ fun testAttributeOperations() = runTest {
+ val handle = gnunetChat.startChat(MessengerApp()) { ctx, msg ->
+ messageLog.add(ctx to msg)
+ }
+
+ // Wait for handle to be initialized
+ withTimeout(5_000) {
+ while (handle.pointer == 0L) {
+ delay(100)
+ }
+ }
+
+ // Set attribute
+ val testKey = "test_key"
+ val testValue = "test_value"
+ gnunetChat.setAttribute(handle, testKey, testValue)
+
+ // Get attributes and verify
+ val attributes = mutableListOf<Pair<String, String>>()
+ gnunetChat.getAttributes(handle) { key, value ->
+ attributes.add(key to value)
+ }
+
+ withContext(Dispatchers.Default) {
+ withTimeout(3_000) {
+ while (attributes.none { it.first == testKey }) {
+ delay(50)
+ }
+ }
+ }
+
+ assertTrue("Attribute $testKey not found",
+ attributes.any { it.first == testKey && it.second == testValue })
+ }
+
+ @Test
+ fun testMessageReception() = runTest {
+ val handle = gnunetChat.startChat(MessengerApp()) { ctx, msg ->
+ messageLog.add(ctx to msg)
+ }
+
+ // Wait for handle to be initialized
+ withTimeout(5_000) {
+ while (handle.pointer == 0L) {
+ delay(100)
+ }
+ }
+
+ // Create account which should trigger message
+ val accountName = "MessageTestAccount"
+ gnunetChat.createAccount(handle, accountName)
+
+ // Wait for message to be received
+ withContext(Dispatchers.Default) {
+ withTimeout(3_000) {
+ while (messageLog.isEmpty()) {
+ delay(50)
+ }
+ }
+ }
+
+ assertTrue("Should receive at least one message", messageLog.isNotEmpty())
+ val (ctx, msg) = messageLog.first()
+ assertNotNull("Message context should not be null", ctx)
+ assertNotNull("Message should not be null", msg)
+ assertNotNull("Message text should not be null", msg.text)
+ }
+
+ @Test
+ fun testLobbyOperations() = runTest {
+ val handle = gnunetChat.startChat(MessengerApp()) { ctx, msg ->
+ messageLog.add(ctx to msg)
+ }
+
+ // Wait for handle to be initialized
+ withTimeout(5_000) {
+ while (handle.pointer == 0L) {
+ delay(100)
+ }
+ }
+
+ var lobbyUri = ""
+
+ gnunetChat.lobbyOpen(handle) { uri ->
+ lobbyUri = uri
+ }
+
+ withContext(Dispatchers.Default) {
+ withTimeout(3_000) {
+ while (lobbyUri.isEmpty()) {
+ delay(50)
+ }
+ }
+ }
+
+ assertTrue("Lobby URI should not be empty", lobbyUri.isNotEmpty())
+ }
+}