123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- package com.dmdirc.ktirc
-
- import com.dmdirc.ktirc.events.EventHandler
- import com.dmdirc.ktirc.events.ServerWelcome
- import com.dmdirc.ktirc.io.CaseMapping
- import com.dmdirc.ktirc.io.LineBufferedSocket
- import com.dmdirc.ktirc.model.*
- import com.nhaarman.mockitokotlin2.*
- import kotlinx.coroutines.channels.Channel
- import kotlinx.coroutines.launch
- import kotlinx.coroutines.runBlocking
- import org.junit.jupiter.api.Assertions.*
- import org.junit.jupiter.api.BeforeEach
- import org.junit.jupiter.api.Test
- import org.junit.jupiter.api.assertThrows
-
- 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>(10)
-
- private val mockSocket = mock<LineBufferedSocket> {
- on { readLines(any()) } doReturn readLineChannel
- }
-
- private val mockSocketFactory = mock<(String, Int, Boolean) -> LineBufferedSocket> {
- on { invoke(eq(HOST), eq(PORT), any()) } doReturn mockSocket
- }
-
- private val mockEventHandler = mock<EventHandler>()
-
- @BeforeEach
- fun setUp() {
- IrcMessage.currentTimeProvider = { TestConstants.time }
- }
-
- @Test
- fun `IrcClientImpl uses socket factory to create a new socket on connect`() {
- runBlocking {
- val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
- client.socketFactory = mockSocketFactory
- readLineChannel.close()
-
- client.connect()
-
- verify(mockSocketFactory).invoke(HOST, PORT, false)
- }
- }
-
- @Test
- fun `IrcClientImpl uses socket factory to create a new tls on connect`() {
- runBlocking {
- val client = IrcClientImpl(Server(HOST, PORT, true), Profile(NICK, REAL_NAME, USER_NAME))
- client.socketFactory = mockSocketFactory
- readLineChannel.close()
-
- client.connect()
-
- verify(mockSocketFactory).invoke(HOST, PORT, true)
- }
- }
-
- @Test
- fun `IrcClientImpl throws if socket already exists`() {
- runBlocking {
- val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
- client.socketFactory = mockSocketFactory
- readLineChannel.close()
-
- client.connect()
-
- assertThrows<IllegalStateException> {
- runBlocking {
- client.connect()
- }
- }
- }
- }
-
- @Test
- fun `IrcClientImpl sends basic connection strings`() {
- runBlocking {
- val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
- client.socketFactory = mockSocketFactory
- readLineChannel.close()
-
- client.connect()
-
- with(inOrder(mockSocket).verify(mockSocket)) {
- sendLine("CAP LS 302")
- sendLine("NICK :$NICK")
- sendLine("USER $USER_NAME localhost $HOST :$REAL_NAME")
- }
- }
- }
-
- @Test
- fun `IrcClientImpl sends password first, when present`() {
- runBlocking {
- val client = IrcClientImpl(Server(HOST, PORT, password = PASSWORD), Profile(NICK, REAL_NAME, USER_NAME))
- client.socketFactory = mockSocketFactory
- readLineChannel.close()
-
- client.connect()
-
- with(inOrder(mockSocket).verify(mockSocket)) {
- sendLine("CAP LS 302")
- sendLine("PASS :$PASSWORD")
- sendLine("NICK :$NICK")
- }
- }
- }
-
- @Test
- fun `IrcClientImpl sends events to provided event handler`() {
- runBlocking {
- val client = IrcClientImpl(Server(HOST, PORT, password = PASSWORD), Profile(NICK, REAL_NAME, USER_NAME))
- client.socketFactory = mockSocketFactory
- client.eventHandler = mockEventHandler
-
- launch {
- readLineChannel.send(":the.gibson 001 acidBurn :Welcome to the IRC!".toByteArray())
- readLineChannel.close()
- }
-
- client.connect()
-
- verify(mockEventHandler).processEvent(same(client), isA<ServerWelcome>())
- }
- }
-
- @Test
- fun `IrcClientImpl removes old event handlers when new one is added`() {
- runBlocking {
- val client = IrcClientImpl(Server(HOST, PORT, password = PASSWORD), Profile(NICK, REAL_NAME, USER_NAME))
- client.socketFactory = mockSocketFactory
- client.eventHandler = mockEventHandler
- client.eventHandler = mock()
-
- launch {
- readLineChannel.send(":the.gibson 001 acidBurn :Welcome to the IRC!".toByteArray())
- readLineChannel.close()
- }
-
- client.connect()
-
- verify(mockEventHandler, never()).processEvent(client, ServerWelcome(TestConstants.time, "acidBurn"))
- }
- }
-
- @Test
- fun `IrcClientImpl removes old event handlers when it is set to null`() {
- runBlocking {
- val client = IrcClientImpl(Server(HOST, PORT, password = PASSWORD), Profile(NICK, REAL_NAME, USER_NAME))
- client.socketFactory = mockSocketFactory
- client.eventHandler = mockEventHandler
- client.eventHandler = null
-
- launch {
- readLineChannel.send(":the.gibson 001 acidBurn :Welcome to the IRC!".toByteArray())
- readLineChannel.close()
- }
-
- client.connect()
-
- verify(mockEventHandler, never()).processEvent(client, ServerWelcome(TestConstants.time, "acidBurn"))
- }
- }
-
- @Test
- fun `IrcClient gets case mapping from server features`() {
- val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
- client.serverState.features[ServerFeature.ServerCaseMapping] = CaseMapping.RfcStrict
- assertEquals(CaseMapping.RfcStrict, client.caseMapping)
- }
-
- @Test
- fun `IrcClient indicates if user is local user or not`() {
- val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
- client.serverState.localNickname = "[acidBurn]"
-
- assertTrue(client.isLocalUser(User("{acidBurn}", "libby", "root.localhost")))
- assertFalse(client.isLocalUser(User("acid-Burn", "libby", "root.localhost")))
- }
-
- @Test
- fun `IrcClient uses current case mapping to check local user`() {
- val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
- client.serverState.localNickname = "[acidBurn]"
- client.serverState.features[ServerFeature.ServerCaseMapping] = CaseMapping.Ascii
- assertFalse(client.isLocalUser(User("{acidBurn}", "libby", "root.localhost")))
- }
-
- }
|