Multiroom stage 1: Join another room, Add a room, the one-time copy (0.108.52)

From the approved design/multiroom and design/next:
- Welcome has Join another room beside Let's go (not in the tour). The new TV finds rooms
  that are adding one on the Wi-Fi (NSD) or takes a home-network address, then the code.
- Settings › Rooms and phones: this room's name, Add a room (a six-digit code for five
  minutes, once), Allow on the existing TV (with the Adult PIN if set), and rooms joined.
- The boxes agree a key (ECDH P-256) and prove the code both ways (HMAC); three wrong
  proofs end the code. The copy is sealed with AES-GCM under a key from the code.
- What travels, each part ticked on the new TV: channels on, your own channels and their
  choices; profiles, Kids and the PIN's salted hash; look and On screen settings; the
  Director's Chair's decisions; Arcade bests. Never a password, token or schedule: the
  new TV signs in for itself (its server filled in) and plans its own week. Workspace
  files are only ever added, never overwritten.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-06 11:30:25 +00:00
co-authored by Claude Opus 5.5
parent d3313e470f
commit b6fb7c2d5b
12 changed files with 674 additions and 14 deletions
@@ -0,0 +1,98 @@
package tv.orbit.classic
import org.json.JSONArray
import org.json.JSONObject
import org.junit.After
import org.junit.Assert.*
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import java.net.InetAddress
/** Multiroom stage 1 (Paul, 6 November 2026): pairing by code, the existing TV's say, and the one-time copy. */
class RoomsTest {
@get:Rule val folder = TemporaryFolder()
private val copy = JSONObject().put("version", 1).put("room", "Living Room").put("server", "https://emby.example.com")
.put(RoomPart.Channels.name, JSONObject().put("lineup", JSONArray(listOf(101, 605))).put("files", JSONObject()
.put("yours.json", """[{"number":701,"name":"90s Sci-Fi","style":"Standard","description":"","hours":null,"shows":[]}]""")
.put("../escape.json", "{}").put("choices-101.json", "{\"schema\":1}")))
private fun offer(code: String = "123456", until: Long = System.currentTimeMillis() + 60_000) = RoomOffer(code, until, "Living Room") { copy }
private fun direct(o: RoomOffer): (String, String, String) -> Pair<Int, String> = { m, p, b -> o.handle(m, p, b).let { (s, j) -> s to j.toString() } }
@After fun reset() { CustomChannels.clear() }
@Test fun theRightCodeAndTheTvsYesBringTheCopyAcrossSealed() {
val o = offer(); val join = RoomJoin(direct(o))
assertTrue(join.prove("123456", "Bedroom").isSuccess)
assertEquals(RoomOffer.State.Asking, o.state); assertEquals("Bedroom", o.joining); assertEquals("Living Room", join.otherRoom)
assertNull(join.collect().getOrThrow()) // the TV hasn't said yet
o.allow()
assertEquals("https://emby.example.com", join.collect().getOrThrow()!!.getString("server"))
assertEquals(RoomOffer.State.Sent, o.state)
assertTrue(join.collect().isFailure) // once only
}
@Test fun aWrongCodeFailsAndThreeEndTheOffer() {
val o = offer()
repeat(2) { assertTrue(RoomJoin(direct(o)).prove("000000", "x").isFailure); }
// Each attempt is a fresh hello, but only one joiner may say hello: the rest are turned away.
assertEquals(409, o.handle("POST", "/room/hello", JSONObject().put("pub", RoomCrypto.publicOf(RoomCrypto.keyPair())).toString()).first)
val fresh = offer(); val sneaky = RoomJoin(direct(fresh))
assertTrue(sneaky.prove("111111", "x").isFailure)
repeat(2) { fresh.handle("POST", "/room/proof", JSONObject().put("proof", "nope").toString()) }
assertEquals(RoomOffer.State.Ended, fresh.state)
assertTrue(RoomJoin(direct(fresh)).prove("123456", "x").isFailure)
}
@Test fun notNowAndTimeRunningOutSendNothing() {
val o = offer(); val join = RoomJoin(direct(o))
join.prove("123456", "Den").getOrThrow(); o.refuse()
assertTrue(join.collect().isFailure); assertNotEquals(RoomOffer.State.Sent, o.state)
val late = offer(until = System.currentTimeMillis() - 1)
assertTrue(RoomJoin(direct(late)).prove("123456", "Den").isFailure)
}
@Test fun someoneInTheMiddleWithoutTheCodeCantReadTheCopy() {
// A box that sees both keys but not the code has no seal key: the copy only opens with the code.
val o = offer(); RoomJoin(direct(o)).prove("123456", "Bedroom").getOrThrow(); o.allow()
val sealed = JSONObject(o.handle("GET", "/room/copy", "").second.toString()).getString("sealed")
assertTrue(runCatching { RoomCrypto.open(ByteArray(32), sealed) }.isFailure)
assertFalse(sealed.contains("emby.example.com"))
}
@Test fun overTheHomeNetworkThroughThePhoneLink() {
PhoneLink.initForTest(object : PhoneStore {
override fun all() = emptyMap<String, String>(); override fun get(id: String): String? = null
override fun put(id: String, value: String) {}; override fun remove(id: String) {} })
PhoneLink.bindTo = InetAddress.getLoopbackAddress(); PhoneLink.port = 18_099; PhoneLink.allowed = { it.isLoopbackAddress }
try {
val o = offer(); PhoneLink.offerRoom(o)
val call: (String, String, String) -> Pair<Int, String> = { m, p, b ->
val c = java.net.URL("http://127.0.0.1:18099$p").openConnection() as java.net.HttpURLConnection
c.requestMethod = m
if (m == "POST") { c.doOutput = true; c.outputStream.use { it.write(b.toByteArray()) } }
val s = c.responseCode; s to ((if (s < 400) c.inputStream else c.errorStream)?.use { it.readBytes().toString(Charsets.UTF_8) }.orEmpty())
}
val join = RoomJoin(call)
assertTrue(join.prove("123456", "Kitchen").isSuccess); o.allow()
assertEquals(1, join.collect().getOrThrow()!!.getInt("version"))
PhoneLink.offerRoom(null)
// With no offer and no phones, the server stops altogether.
assertTrue(runCatching { call("POST", "/room/hello", "{}") }.isFailure)
} finally { PhoneLink.stop(); PhoneLink.allowed = EmbyServer::isHomeAddress; PhoneLink.bindTo = null; PhoneLink.port = PhoneLink.PORT }
}
@Test fun theCopyLandsInTheWorkspaceWithoutOverwritingOrEscaping() {
val dir = folder.newFolder()
java.io.File(dir, "choices-101.json").writeText("mine")
val lineup = RoomBundle.applyToWorkspace(dir, copy, setOf(RoomPart.Channels))
assertEquals(listOf(101, 605), lineup)
assertEquals("mine", java.io.File(dir, "choices-101.json").readText()) // never overwritten
assertFalse(java.io.File(dir.parentFile, "escape.json").exists())
assertEquals("90s Sci-Fi", CustomChannels.of(701)?.name)
assertTrue(RoomBundle.applyToWorkspace(folder.newFolder(), copy, emptySet()).isEmpty()) // not ticked: nothing
}
@Test fun onlyHomeAddressesCanBeJoined() {
assertNotNull(roomTransport("192.168.0.20")); assertNull(roomTransport("8.8.8.8")); assertNull(roomTransport("living-room.example"))
}
}