mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 05:16:01 +00:00
Intergrated syncthing (#1442)
This commit is contained in:
1 parent
5b7e401b11
commit
8c09b2c6a8
34 files changed
+2492
-7
No files matched your search
@@ -0,0 +1,52 @@
|
||||
package com.droiddeck.launcher.sync
|
||||
|
||||
import org.json.JSONObject
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
class SyncSnapshotTest {
|
||||
private fun folder(state: String = "idle", global: Long = 100, need: Long = 0, blocked: Boolean = false, paused: Boolean = false, errors: Int = 0) =
|
||||
SyncFolder("id", "Saves", "/p", paused, blocked, state, global, need, errors, emptyList())
|
||||
|
||||
private fun snapshot(vararg folders: SyncFolder) = SyncSnapshot("me", folders.toList(), emptyList(), emptyList(), emptyList(), 0, 0)
|
||||
|
||||
@Test
|
||||
fun syncingReportsPercentOfActiveFolders() {
|
||||
val s = snapshot(folder("syncing", global = 1000, need = 360), folder())
|
||||
assertTrue(s.syncing)
|
||||
assertEquals(64, s.percent)
|
||||
assertEquals(360, s.needBytes)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun blockedOrFailingFoldersNeedAttention() {
|
||||
assertTrue(snapshot(folder(blocked = true, paused = true)).needsAttention)
|
||||
assertTrue(snapshot(folder(errors = 2)).needsAttention)
|
||||
assertTrue(snapshot(folder("idle", need = 5)).needsAttention)
|
||||
assertFalse(snapshot(folder(paused = true, errors = 2)).needsAttention)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun unreadableFolderStatusShowsAsError() {
|
||||
val config = JSONObject("""{"id":"saves","label":"Saves","path":"/p","paused":false,"devices":[{"deviceID":"DEV1"}]}""")
|
||||
val f = SyncthingParse.folder(config, SyncthingParse.unreadableStatus(), blocked = false, names = mapOf("DEV1" to "Desktop PC"))
|
||||
assertEquals("error", f.state)
|
||||
assertTrue(f.errors > 0)
|
||||
assertTrue(f.outOfSync)
|
||||
assertFalse(f.syncing)
|
||||
assertEquals(listOf("Desktop PC"), f.sharedWith)
|
||||
assertTrue(snapshot(f).needsAttention)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parsesPendingFoldersWithDeviceNames() {
|
||||
val json = JSONObject("""{"emu":{"offeredBy":{"DEV1":{"label":"Emulation saves","time":"2026-01-01T00:00:00Z"}}}}""")
|
||||
val pending = SyncthingParse.pendingFolders(json, mapOf("DEV1" to "Desktop PC"))
|
||||
assertEquals(listOf(SyncPendingFolder("emu", "Emulation saves", "Desktop PC", "DEV1")), pending)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.droiddeck.launcher.sync
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.rules.TemporaryFolder
|
||||
import java.io.File
|
||||
|
||||
class SyncthingConfigTest {
|
||||
@get:Rule val tmp = TemporaryFolder()
|
||||
|
||||
private val generated = """
|
||||
<configuration version="51">
|
||||
<folder id="abc" label="Camera" path="/storage/emulated/0/DCIM" type="sendreceive">
|
||||
<paused>false</paused>
|
||||
</folder>
|
||||
<folder id="def" label="Saves" path="/data/user/0/app/Sync/saves" type="sendreceive">
|
||||
<paused>false</paused>
|
||||
</folder>
|
||||
<device id="PEER" name="Desktop" compression="metadata">
|
||||
<address>dynamic</address>
|
||||
<paused>false</paused>
|
||||
</device>
|
||||
<gui enabled="true" tls="true" debugging="false">
|
||||
<address>127.0.0.1:8384</address>
|
||||
<user>droiddeck</user>
|
||||
<apikey>key123</apikey>
|
||||
</gui>
|
||||
<options>
|
||||
<listenAddress>default</listenAddress>
|
||||
<localAnnouncePort>21027</localAnnouncePort>
|
||||
<urAccepted>0</urAccepted>
|
||||
<startBrowser>true</startBrowser>
|
||||
</options>
|
||||
<defaults>
|
||||
<folder id="" label="" path="~" type="sendreceive"></folder>
|
||||
</defaults>
|
||||
</configuration>
|
||||
""".trimIndent()
|
||||
|
||||
private val ports = SyncthingPorts(22100, 21127)
|
||||
|
||||
@Test
|
||||
fun pausesDisallowedFoldersBeforeLaunch() {
|
||||
val out = SyncthingConfig.apply(generated, ports, "/data/st/gui.sock", "/ext/Sync") { !it.startsWith("/storage/emulated/0/DCIM") }
|
||||
val camera = out.substringAfter("id=\"abc\"").substringBefore("</folder>")
|
||||
val saves = out.substringAfter("id=\"def\"").substringBefore("</folder>")
|
||||
assertTrue(camera.contains("<paused>true</paused>"))
|
||||
assertTrue(saves.contains("<paused>false</paused>"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun pausesDevicesBeforeLaunchWhenPolicyRequires() {
|
||||
val paused = SyncthingConfig.apply(generated, ports, "/data/st/gui.sock", "/ext/Sync", pauseDevices = true) { true }
|
||||
assertTrue(paused.substringAfter("id=\"PEER\"").substringBefore("</device>").contains("<paused>true</paused>"))
|
||||
val open = SyncthingConfig.apply(generated, ports, "/data/st/gui.sock", "/ext/Sync", pauseDevices = false) { true }
|
||||
assertTrue(open.substringAfter("id=\"PEER\"").substringBefore("</device>").contains("<paused>false</paused>"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun replacesConfigFileWithoutLeavingStagedCopy() {
|
||||
val dir = tmp.newFolder()
|
||||
val file = File(dir, "config.xml").apply { writeText(generated) }
|
||||
SyncthingConfig.apply(file, ports, File("/data/st/gui.sock"), File("/ext/Sync"), false) { true }
|
||||
assertEquals(SyncthingConfig.apply(generated, ports, "/data/st/gui.sock", "/ext/Sync") { true }, file.readText())
|
||||
assertEquals(listOf("config.xml"), dir.list()!!.toList())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun applyIsStable() {
|
||||
val once = SyncthingConfig.apply(generated, ports, "/data/st/gui.sock", "/ext/Sync") { true }
|
||||
assertEquals(once, SyncthingConfig.apply(once, ports, "/data/st/gui.sock", "/ext/Sync") { true })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun guiListensOnlyOnPrivateSocket() {
|
||||
val gui = SyncthingConfig.apply(generated, ports, "/data/st/gui.sock", "/ext/Sync") { true }.substringAfter("<gui").substringBefore("</gui>")
|
||||
assertTrue(gui.contains("<address>/data/st/gui.sock</address>"))
|
||||
assertTrue(gui.contains("<unixSocketPermissions>600</unixSocketPermissions>"))
|
||||
assertTrue(gui.contains("tls=\"false\""))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.droiddeck.launcher.sync
|
||||
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.rules.TemporaryFolder
|
||||
import java.io.File
|
||||
import java.nio.file.Files
|
||||
|
||||
class SyncthingGuardTest {
|
||||
@get:Rule val tmp = TemporaryFolder()
|
||||
|
||||
private fun setup(): Triple<SyncthingGuard, File, File> {
|
||||
val data = tmp.newFolder("data")
|
||||
val external = tmp.newFolder("external")
|
||||
val home = File(external, "Sync").apply { mkdirs() }
|
||||
val config = File(data, "files/syncthing").apply { mkdirs() }
|
||||
return Triple(SyncthingGuard(home, listOf(data, external), listOf(config)), data, external)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun allowsFoldersInsideAppStorage() {
|
||||
val (guard, data, external) = setup()
|
||||
assertTrue(guard.allows(File(external, "Sync/Game saves").path))
|
||||
assertTrue(guard.allows(File(data, "files/linux/home").path))
|
||||
assertTrue(guard.allows("~/Shared"))
|
||||
assertTrue(guard.allows(external.path))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun blocksFoldersOutsideAppStorage() {
|
||||
val (guard, _, _) = setup()
|
||||
assertFalse(guard.allows(tmp.newFolder("DCIM").path))
|
||||
assertFalse(guard.allows("/storage/emulated/0/Download"))
|
||||
assertFalse(guard.allows(""))
|
||||
assertFalse(guard.allows("relative/path"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun blocksSyncthingsOwnConfig() {
|
||||
val (guard, data, _) = setup()
|
||||
assertFalse(guard.allows(File(data, "files/syncthing").path))
|
||||
assertFalse(guard.allows(File(data, "files/syncthing/keys").path))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun blocksFoldersHoldingSyncthingsConfig() {
|
||||
val (guard, data, _) = setup()
|
||||
assertFalse(guard.allows(data.path))
|
||||
assertFalse(guard.allows(File(data, "files").path))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun blocksSiblingWithSharedPrefix() {
|
||||
val (guard, _, external) = setup()
|
||||
assertFalse(guard.allows(external.path + "-other"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun blocksSymlinkPointingOutside() {
|
||||
val (guard, _, external) = setup()
|
||||
val outside = tmp.newFolder("outside")
|
||||
val link = File(external, "Sync/link")
|
||||
Files.createSymbolicLink(link.toPath(), outside.toPath())
|
||||
assertFalse(guard.allows(link.path))
|
||||
assertFalse(guard.allows(File(link, "child").path))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.droiddeck.launcher.sync
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import java.io.ByteArrayOutputStream
|
||||
|
||||
class SyncthingHttpTest {
|
||||
private fun response(raw: String) = SyncthingHttp.readResponse(raw.byteInputStream(Charsets.ISO_8859_1))
|
||||
|
||||
@Test
|
||||
fun readsContentLengthBody() {
|
||||
assertEquals(200 to """{"ping":"pong"}""", response("HTTP/1.1 200 OK\r\nContent-Length: 15\r\n\r\n{\"ping\":\"pong\"}"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun readsChunkedBody() {
|
||||
assertEquals(403 to "CSRF Error", response("HTTP/1.1 403 Forbidden\r\nTransfer-Encoding: chunked\r\n\r\n4\r\nCSRF\r\n6\r\n Error\r\n0\r\n\r\n"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun writesOneShotRequest() {
|
||||
val out = ByteArrayOutputStream()
|
||||
SyncthingHttp.writeRequest(out, "POST", "/rest/system/pause", listOf("X-API-Key" to "k"), null)
|
||||
val text = out.toString("ISO-8859-1")
|
||||
assertTrue(text.startsWith("POST /rest/system/pause HTTP/1.1\r\n"))
|
||||
assertTrue(text.contains("Connection: close\r\n"))
|
||||
assertTrue(text.endsWith("X-API-Key: k\r\nContent-Length: 0\r\n\r\n"))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.droiddeck.launcher.sync
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
|
||||
class SyncthingPortsTest {
|
||||
@Test
|
||||
fun skipsBusyPorts() {
|
||||
val busy = setOf(22100, 21127)
|
||||
val picked = SyncthingPortPicker.pick({ it !in busy }, { it !in busy })
|
||||
assertEquals(SyncthingPorts(22101, 21128), picked)
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException::class)
|
||||
fun failsWhenNoPortIsFree() {
|
||||
SyncthingPortPicker.pick({ false }, { true })
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user