Skip to content

Commit 6bd2b33

Browse files
committed
test: Add a live node bootstrap test.
1 parent 9a28155 commit 6bd2b33

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

cspell.config.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
version: "0.2"
2+
dictionaryDefinitions: []
3+
dictionaries: []
4+
ignoreWords: []
5+
flagWords: []
6+
import: []
7+
ignorePaths:
8+
- "*.json"
9+
- "*.yaml"
10+
- "*.yml"
11+
- "BUILD.bazel"
12+
- "LICENSE*"
13+
words:
14+
- "abilinski"
15+
- "kotlinx"
16+
- "norequest"
17+
- "stdlib"
18+
- "toxes"

lib/src/test/java/im/tox/tox4j/core/ToxCoreTest.kt

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package im.tox.tox4j.core
22

33
import im.tox.tox4j.core.callbacks.ToxCoreEventListener
4+
import im.tox.tox4j.core.data.Port
45
import im.tox.tox4j.core.data.ToxFriendNumber
6+
import im.tox.tox4j.core.data.ToxPublicKey
57
import im.tox.tox4j.core.enums.ToxConnection
68
import im.tox.tox4j.core.exceptions.ToxBootstrapException
79
import im.tox.tox4j.core.options.ToxOptions
@@ -11,20 +13,48 @@ import kotlin.test.assertFailsWith
1113

1214
@kotlin.ExperimentalStdlibApi
1315
class ToxCoreTest {
16+
private val options = ToxOptions(localDiscoveryEnabled = false)
17+
1418
@Test
1519
fun bootstrap_withWrongHost_shouldFail() =
1620
runTox {
17-
val tox = newToxCore(ToxOptions())
21+
val tox = newToxCore(options)
1822
assertFailsWith<ToxBootstrapException> {
1923
tox.bootstrap("host-does-not-exist", tox.getUdpPort, tox.getDhtId)
2024
}
2125
}
2226

27+
@Test
28+
fun bootstrap_withCorrectHost_shouldSucceed() =
29+
runTox {
30+
val tox = newToxCore(options)
31+
tox.bootstrap(
32+
"tox.abilinski.com",
33+
Port(33445.toUShort()),
34+
ToxPublicKey(fromHex("10C00EB250C3233E343E2AEBA07115A5C28920E9C8D29492F6D00B29049EDC7E")),
35+
)
36+
37+
var connected = false
38+
39+
val isConnected =
40+
object : ToxCoreEventListener<Boolean> {
41+
override fun selfConnectionStatus(
42+
connectionStatus: ToxConnection,
43+
state: Boolean,
44+
) = connectionStatus != ToxConnection.NONE
45+
}
46+
47+
while (!connected) {
48+
connected = tox.iterate(isConnected, connected)
49+
delay(tox.iterationInterval.toLong())
50+
}
51+
}
52+
2353
@Test
2454
fun addFriendNorequest_shouldConnectTwoToxes() =
2555
runTox {
26-
val tox1 = newToxCore(ToxOptions())
27-
val tox2 = newToxCore(ToxOptions())
56+
val tox1 = newToxCore(options)
57+
val tox2 = newToxCore(options)
2858

2959
tox2.bootstrap("localhost", tox1.getUdpPort, tox1.getDhtId)
3060

@@ -49,4 +79,14 @@ class ToxCoreTest {
4979
delay(tox1.iterationInterval.toLong())
5080
}
5181
}
82+
83+
private companion object {
84+
fun fromHex(hex: String): ByteArray {
85+
val bytes = ByteArray(hex.length / 2)
86+
for (i in bytes.indices) {
87+
bytes[i] = hex.substring(i * 2, i * 2 + 2).toInt(16).toByte()
88+
}
89+
return bytes
90+
}
91+
}
5292
}

0 commit comments

Comments
 (0)