package org.meshbay.client import org.json.JSONArray import org.junit.Assert.assertEquals import org.junit.Assert.assertThrows import org.junit.Test import org.meshbay.client.bridge.Channels import org.meshbay.client.bridge.Refused import org.meshbay.client.hub.HubClient import java.net.InetAddress import java.net.UnknownHostException class ChannelsTest { private val channels = Channels(HubClient(FakePrefs()), onHubChanged = {}, hasCatalogue = { it == "fr" || it == "pt-BR" }) @Test fun `a channel that is not enumerated is refused`() { for (ch in listOf("node:op", "root:choose", "window:minimize-to-tray", "keys:sign", "", "hub:fetch2")) { assertThrows(ch, Refused::class.java) { channels.call(ch, JSONArray()) } } } @Test fun `the locale is a code with a catalogue, never text`() { assertEquals("fr", channels.call("ui:locale", JSONArray().put("fr"))) assertEquals("fr", channels.call("ui:locale", JSONArray().put("de"))) // no catalogue assertEquals("fr", channels.call("ui:locale", JSONArray().put("../en"))) // not a code assertEquals("pt-BR", channels.call("ui:locale", JSONArray().put("pt-BR"))) } @Test fun `stun hostnames are resolved, literals kept, failures dropped`() { val lookup: (String) -> Array = { host -> if (host == "stun.example") arrayOf(InetAddress.getByAddress(host, byteArrayOf(192.toByte(), 0, 2, 7))) else throw UnknownHostException(host) } val out = Channels.resolveStun(JSONArray(listOf("stun:stun.example:3478", "stun:198.51.100.1:3478", "stun:[2001:db8::1]:3478", "stun:gone.example:3478", "turn:x")), lookup) assertEquals(listOf("stun:192.0.2.7:3478", "stun:198.51.100.1:3478", "stun:[2001:db8::1]:3478", "turn:x"), (0 until out.length()).map { out.getString(it) }) } }