123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- package com.dmdirc.ktirc
-
- import com.dmdirc.ktirc.events.*
- import com.dmdirc.ktirc.io.CaseMapping
- import com.dmdirc.ktirc.io.LineBufferedSocket
- import com.dmdirc.ktirc.model.ChannelState
- import com.dmdirc.ktirc.model.ConnectionError
- import com.dmdirc.ktirc.model.ServerFeature
- import com.dmdirc.ktirc.model.User
- import com.dmdirc.ktirc.util.currentTimeProvider
- import com.nhaarman.mockitokotlin2.*
- import io.ktor.util.KtorExperimentalAPI
- import kotlinx.coroutines.*
- import kotlinx.coroutines.channels.Channel
- import kotlinx.coroutines.channels.filter
- import kotlinx.coroutines.channels.map
- import kotlinx.coroutines.sync.Mutex
- import org.junit.jupiter.api.Assertions.*
- import org.junit.jupiter.api.BeforeEach
- import org.junit.jupiter.api.Test
- import org.junit.jupiter.api.assertThrows
- import java.nio.channels.UnresolvedAddressException
- import java.security.cert.CertificateException
- import java.util.concurrent.atomic.AtomicReference
-
- @KtorExperimentalAPI
- @ExperimentalCoroutinesApi
- internal class IrcClientImplTest {
-
- companion object {
- private const val HOST = "thegibson.com"
- private const val PORT = 12345
- private const val NICK = "AcidBurn"
- private const val REAL_NAME = "Kate Libby"
- private const val USER_NAME = "acidb"
- private const val PASSWORD = "HackThePlanet"
- }
-
- private val readLineChannel = Channel<ByteArray>(Channel.UNLIMITED)
- private val sendLineChannel = Channel<ByteArray>(Channel.UNLIMITED)
-
- private val mockSocket = mock<LineBufferedSocket> {
- on { receiveChannel } doReturn readLineChannel
- on { sendChannel } doReturn sendLineChannel
- }
-
- private val mockSocketFactory = mock<(CoroutineScope, String, Int, Boolean) -> LineBufferedSocket> {
- on { invoke(any(), eq(HOST), eq(PORT), any()) } doReturn mockSocket
- }
-
- private val mockEventHandler = mock<(IrcEvent) -> Unit>()
-
- private val profileConfig = ProfileConfig().apply {
- nickname = NICK
- realName = REAL_NAME
- username = USER_NAME
- }
-
- private val normalConfig = IrcClientConfig(ServerConfig().apply {
- host = HOST
- port = PORT
- }, profileConfig, BehaviourConfig(), null)
-
- @BeforeEach
- fun setUp() {
- currentTimeProvider = { TestConstants.time }
- }
-
- @Test
- fun `uses socket factory to create a new socket on connect`() {
- val client = IrcClientImpl(normalConfig)
- client.socketFactory = mockSocketFactory
- client.connect()
-
- verify(mockSocketFactory, timeout(500)).invoke(client, HOST, PORT, false)
- }
-
- @Test
- fun `uses socket factory to create a new tls on connect`() {
- val client = IrcClientImpl(IrcClientConfig(ServerConfig().apply {
- host = HOST
- port = PORT
- useTls = true
- }, profileConfig, BehaviourConfig(), null))
- client.socketFactory = mockSocketFactory
- client.connect()
-
- verify(mockSocketFactory, timeout(500)).invoke(client, HOST, PORT, true)
- }
-
- @Test
- fun `throws if socket already exists`() {
- val client = IrcClientImpl(normalConfig)
- client.socketFactory = mockSocketFactory
- client.connect()
-
- assertThrows<IllegalStateException> {
- client.connect()
- }
- }
-
- @Test
- fun `emits connection events with local time`() = runBlocking {
- currentTimeProvider = { TestConstants.time }
- val client = IrcClientImpl(normalConfig)
- client.socketFactory = mockSocketFactory
- client.onEvent(mockEventHandler)
- client.connect()
-
- val captor = argumentCaptor<IrcEvent>()
- verify(mockEventHandler, timeout(500).atLeast(2)).invoke(captor.capture())
-
- assertTrue(captor.firstValue is ServerConnecting)
- assertEquals(TestConstants.time, captor.firstValue.time)
-
- assertTrue(captor.secondValue is ServerConnected)
- assertEquals(TestConstants.time, captor.secondValue.time)
- }
-
- @Test
- fun `sends basic connection strings`() = runBlocking {
- val client = IrcClientImpl(normalConfig)
- client.socketFactory = mockSocketFactory
- client.connect()
-
- assertEquals("CAP LS 302", String(sendLineChannel.receive()))
- assertEquals("NICK :$NICK", String(sendLineChannel.receive()))
- assertEquals("USER $USER_NAME 0 * :$REAL_NAME", String(sendLineChannel.receive()))
- }
-
- @Test
- fun `sends password first, when present`() = runBlocking {
- val client = IrcClientImpl(IrcClientConfig(ServerConfig().apply {
- host = HOST
- port = PORT
- password = PASSWORD
- }, profileConfig, BehaviourConfig(), null))
- client.socketFactory = mockSocketFactory
- client.connect()
-
- assertEquals("CAP LS 302", String(sendLineChannel.receive()))
- assertEquals("PASS :$PASSWORD", String(sendLineChannel.receive()))
- }
-
- @Test
- fun `sends events to provided event handler`() {
- val client = IrcClientImpl(normalConfig)
- client.socketFactory = mockSocketFactory
- client.onEvent(mockEventHandler)
-
- GlobalScope.launch {
- readLineChannel.send(":the.gibson 001 acidBurn :Welcome to the IRC!".toByteArray())
- }
-
- client.connect()
-
- verify(mockEventHandler, timeout(500)).invoke(isA<ServerWelcome>())
- }
-
- @Test
- fun `gets case mapping from server features`() {
- val client = IrcClientImpl(normalConfig)
- client.serverState.features[ServerFeature.ServerCaseMapping] = CaseMapping.RfcStrict
- assertEquals(CaseMapping.RfcStrict, client.caseMapping)
- }
-
- @Test
- fun `indicates if user is local user or not`() {
- val client = IrcClientImpl(normalConfig)
- client.serverState.localNickname = "[acidBurn]"
-
- assertTrue(client.isLocalUser(User("{acidBurn}", "libby", "root.localhost")))
- assertFalse(client.isLocalUser(User("acid-Burn", "libby", "root.localhost")))
- }
-
- @Test
- fun `indicates if nickname is local user or not`() {
- val client = IrcClientImpl(normalConfig)
- client.serverState.localNickname = "[acidBurn]"
-
- assertTrue(client.isLocalUser("{acidBurn}"))
- assertFalse(client.isLocalUser("acid-Burn"))
- }
-
- @Test
- fun `uses current case mapping to check local user`() {
- val client = IrcClientImpl(normalConfig)
- client.serverState.localNickname = "[acidBurn]"
- client.serverState.features[ServerFeature.ServerCaseMapping] = CaseMapping.Ascii
- assertFalse(client.isLocalUser(User("{acidBurn}", "libby", "root.localhost")))
- }
-
- @Test
- fun `sends text to socket`() = runBlocking {
- val client = IrcClientImpl(normalConfig)
- client.socketFactory = mockSocketFactory
- client.connect()
-
- client.send("testing 123")
-
- assertEquals(true, withTimeoutOrNull(500) {
- var found = false
- for (line in sendLineChannel) {
- if (String(line) == "testing 123") {
- found = true
- break
- }
- }
- found
- })
- }
-
- @Test
- fun `disconnects the socket`() = runBlocking {
- val client = IrcClientImpl(normalConfig)
- client.socketFactory = mockSocketFactory
- client.connect()
-
- client.disconnect()
-
- verify(mockSocket, timeout(500)).disconnect()
- }
-
- @Test
- @ObsoleteCoroutinesApi
- fun `sends messages in order`() = runBlocking {
- val client = IrcClientImpl(normalConfig)
- client.socketFactory = mockSocketFactory
- client.connect()
-
- (0..100).forEach { client.send("TEST $it") }
-
- assertEquals(100, withTimeoutOrNull(500) {
- var next = 0
- for (line in sendLineChannel.map { String(it) }.filter { it.startsWith("TEST ") }) {
- assertEquals("TEST $next", line)
- if (++next == 100) {
- break
- }
- }
- next
- })
- }
-
- @Test
- fun `defaults local nickname to profile`() {
- val client = IrcClientImpl(normalConfig)
- assertEquals(NICK, client.serverState.localNickname)
- }
-
- @Test
- fun `defaults server name to host name`() {
- val client = IrcClientImpl(normalConfig)
- assertEquals(HOST, client.serverState.serverName)
- }
-
- @Test
- fun `exposes behaviour config`() {
- val client = IrcClientImpl(IrcClientConfig(
- ServerConfig().apply { host = HOST },
- profileConfig,
- BehaviourConfig().apply { requestModesOnJoin = true },
- null))
-
- assertTrue(client.behaviour.requestModesOnJoin)
- }
-
- @Test
- fun `reset clears all state`() {
- with(IrcClientImpl(normalConfig)) {
- userState += User("acidBurn")
- channelState += ChannelState("#thegibson") { CaseMapping.Rfc }
- serverState.serverName = "root.$HOST"
- reset()
-
- assertEquals(0, userState.count())
- assertEquals(0, channelState.count())
- assertEquals(HOST, serverState.serverName)
- }
- }
-
- @Test
- fun `sends connect error when host is unresolvable`() = runBlocking {
- whenever(mockSocket.connect()).doThrow(UnresolvedAddressException())
- with(IrcClientImpl(normalConfig)) {
- socketFactory = mockSocketFactory
- withTimeout(500) {
- launch {
- delay(50)
- connect()
- }
- val event = waitForEvent<ServerConnectionError>()
- assertEquals(ConnectionError.UnresolvableAddress, event.error)
- }
- }
- }
-
- @Test
- fun `sends connect error when tls certificate is bad`() = runBlocking {
- whenever(mockSocket.connect()).doThrow(CertificateException("Boooo"))
- with(IrcClientImpl(normalConfig)) {
- socketFactory = mockSocketFactory
- withTimeout(500) {
- launch {
- delay(50)
- connect()
- }
- val event = waitForEvent<ServerConnectionError>()
- assertEquals(ConnectionError.BadTlsCertificate, event.error)
- assertEquals("Boooo", event.details)
- }
- }
- }
-
- @Test
- fun `identifies channels that have a prefix in the chantypes feature`() {
- with(IrcClientImpl(normalConfig)) {
- serverState.features[ServerFeature.ChannelTypes] = "&~"
- assertTrue(isChannel("&dumpsterdiving"))
- assertTrue(isChannel("~hacktheplanet"))
- assertFalse(isChannel("#root"))
- assertFalse(isChannel("acidBurn"))
- assertFalse(isChannel(""))
- assertFalse(isChannel("acidBurn#~"))
- }
- }
-
- private suspend inline fun <reified T : IrcEvent> IrcClient.waitForEvent(): T {
- val mutex = Mutex(true)
- val value = AtomicReference<T>()
- onEvent {
- if (it is T) {
- value.set(it)
- mutex.unlock()
- }
- }
- mutex.lock()
- return value.get()
- }
-
-
- }
|