You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IrcClientTest.kt 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package com.dmdirc.ktirc
  2. import com.dmdirc.ktirc.events.*
  3. import com.dmdirc.ktirc.io.CaseMapping
  4. import com.dmdirc.ktirc.io.LineBufferedSocket
  5. import com.dmdirc.ktirc.model.*
  6. import com.dmdirc.ktirc.util.currentTimeProvider
  7. import com.nhaarman.mockitokotlin2.*
  8. import io.ktor.util.KtorExperimentalAPI
  9. import kotlinx.coroutines.*
  10. import kotlinx.coroutines.channels.Channel
  11. import kotlinx.coroutines.channels.filter
  12. import kotlinx.coroutines.channels.map
  13. import org.junit.jupiter.api.Assertions.*
  14. import org.junit.jupiter.api.BeforeEach
  15. import org.junit.jupiter.api.Test
  16. import org.junit.jupiter.api.assertThrows
  17. @KtorExperimentalAPI
  18. @ExperimentalCoroutinesApi
  19. internal class IrcClientImplTest {
  20. companion object {
  21. private const val HOST = "thegibson.com"
  22. private const val PORT = 12345
  23. private const val NICK = "AcidBurn"
  24. private const val REAL_NAME = "Kate Libby"
  25. private const val USER_NAME = "acidb"
  26. private const val PASSWORD = "HackThePlanet"
  27. }
  28. private val readLineChannel = Channel<ByteArray>(Channel.UNLIMITED)
  29. private val sendLineChannel = Channel<ByteArray>(Channel.UNLIMITED)
  30. private val mockSocket = mock<LineBufferedSocket> {
  31. on { receiveChannel } doReturn readLineChannel
  32. on { sendChannel } doReturn sendLineChannel
  33. }
  34. private val mockSocketFactory = mock<(CoroutineScope, String, Int, Boolean) -> LineBufferedSocket> {
  35. on { invoke(any(), eq(HOST), eq(PORT), any()) } doReturn mockSocket
  36. }
  37. private val mockEventHandler = mock<(IrcEvent) -> Unit>()
  38. @BeforeEach
  39. fun setUp() {
  40. currentTimeProvider = { TestConstants.time }
  41. }
  42. @Test
  43. fun `uses socket factory to create a new socket on connect`() {
  44. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  45. client.socketFactory = mockSocketFactory
  46. client.connect()
  47. verify(mockSocketFactory, timeout(500)).invoke(client, HOST, PORT, false)
  48. }
  49. @Test
  50. fun `uses socket factory to create a new tls on connect`() {
  51. val client = IrcClientImpl(Server(HOST, PORT, true), Profile(NICK, REAL_NAME, USER_NAME))
  52. client.socketFactory = mockSocketFactory
  53. client.connect()
  54. verify(mockSocketFactory, timeout(500)).invoke(client, HOST, PORT, true)
  55. }
  56. @Test
  57. fun `throws if socket already exists`() {
  58. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  59. client.socketFactory = mockSocketFactory
  60. client.connect()
  61. assertThrows<IllegalStateException> {
  62. client.connect()
  63. }
  64. }
  65. @Test
  66. fun `emits connection events with local time`() = runBlocking {
  67. currentTimeProvider = { TestConstants.time }
  68. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  69. client.socketFactory = mockSocketFactory
  70. client.onEvent(mockEventHandler)
  71. client.connect()
  72. val captor = argumentCaptor<IrcEvent>()
  73. verify(mockEventHandler, timeout(500).atLeast(2)).invoke(captor.capture())
  74. assertTrue(captor.firstValue is ServerConnecting)
  75. assertEquals(TestConstants.time, captor.firstValue.time)
  76. assertTrue(captor.secondValue is ServerConnected)
  77. assertEquals(TestConstants.time, captor.secondValue.time)
  78. }
  79. @Test
  80. fun `emits disconnected event with local time when read channel closed`() = runBlocking {
  81. currentTimeProvider = { TestConstants.time }
  82. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  83. client.socketFactory = mockSocketFactory
  84. client.connect()
  85. client.onEvent(mockEventHandler)
  86. readLineChannel.close()
  87. val captor = argumentCaptor<ServerDisconnected>()
  88. verify(mockEventHandler, timeout(500).atLeast(2)).invoke(captor.capture())
  89. assertEquals(TestConstants.time, captor.lastValue.time)
  90. }
  91. @Test
  92. fun `sends basic connection strings`() = runBlocking {
  93. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  94. client.socketFactory = mockSocketFactory
  95. client.connect()
  96. assertEquals("CAP LS 302", String(sendLineChannel.receive()))
  97. assertEquals("NICK :$NICK", String(sendLineChannel.receive()))
  98. assertEquals("USER $USER_NAME 0 * :$REAL_NAME", String(sendLineChannel.receive()))
  99. }
  100. @Test
  101. fun `sends password first, when present`() = runBlocking {
  102. val client = IrcClientImpl(Server(HOST, PORT, password = PASSWORD), Profile(NICK, REAL_NAME, USER_NAME))
  103. client.socketFactory = mockSocketFactory
  104. client.connect()
  105. assertEquals("CAP LS 302", String(sendLineChannel.receive()))
  106. assertEquals("PASS :$PASSWORD", String(sendLineChannel.receive()))
  107. }
  108. @Test
  109. fun `sends events to provided event handler`() {
  110. val client = IrcClientImpl(Server(HOST, PORT, password = PASSWORD), Profile(NICK, REAL_NAME, USER_NAME))
  111. client.socketFactory = mockSocketFactory
  112. client.onEvent(mockEventHandler)
  113. GlobalScope.launch {
  114. readLineChannel.send(":the.gibson 001 acidBurn :Welcome to the IRC!".toByteArray())
  115. }
  116. client.connect()
  117. verify(mockEventHandler, timeout(500)).invoke(isA<ServerWelcome>())
  118. }
  119. @Test
  120. fun `gets case mapping from server features`() {
  121. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  122. client.serverState.features[ServerFeature.ServerCaseMapping] = CaseMapping.RfcStrict
  123. assertEquals(CaseMapping.RfcStrict, client.caseMapping)
  124. }
  125. @Test
  126. fun `indicates if user is local user or not`() {
  127. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  128. client.serverState.localNickname = "[acidBurn]"
  129. assertTrue(client.isLocalUser(User("{acidBurn}", "libby", "root.localhost")))
  130. assertFalse(client.isLocalUser(User("acid-Burn", "libby", "root.localhost")))
  131. }
  132. @Test
  133. fun `indicates if nickname is local user or not`() {
  134. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  135. client.serverState.localNickname = "[acidBurn]"
  136. assertTrue(client.isLocalUser("{acidBurn}"))
  137. assertFalse(client.isLocalUser("acid-Burn"))
  138. }
  139. @Test
  140. fun `uses current case mapping to check local user`() {
  141. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  142. client.serverState.localNickname = "[acidBurn]"
  143. client.serverState.features[ServerFeature.ServerCaseMapping] = CaseMapping.Ascii
  144. assertFalse(client.isLocalUser(User("{acidBurn}", "libby", "root.localhost")))
  145. }
  146. @Test
  147. fun `sends text to socket`() = runBlocking {
  148. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  149. client.socketFactory = mockSocketFactory
  150. client.connect()
  151. client.send("testing 123")
  152. assertEquals(true, withTimeoutOrNull(500) {
  153. var found = false
  154. for (line in sendLineChannel) {
  155. if (String(line) == "testing 123") {
  156. found = true
  157. break
  158. }
  159. }
  160. found
  161. })
  162. }
  163. @Test
  164. fun `disconnects the socket`() = runBlocking {
  165. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  166. client.socketFactory = mockSocketFactory
  167. client.connect()
  168. client.disconnect()
  169. verify(mockSocket, timeout(500)).disconnect()
  170. }
  171. @Test
  172. fun `sends messages in order`() = runBlocking {
  173. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  174. client.socketFactory = mockSocketFactory
  175. client.connect()
  176. (0..100).forEach { client.send("TEST $it") }
  177. assertEquals(100, withTimeoutOrNull(500) {
  178. var next = 0
  179. for (line in sendLineChannel.map { String(it) }.filter { it.startsWith("TEST ") }) {
  180. assertEquals("TEST $next", line)
  181. if (++next == 100) {
  182. break
  183. }
  184. }
  185. next
  186. })
  187. }
  188. @Test
  189. fun `defaults local nickname to profile`() = runBlocking {
  190. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  191. assertEquals(NICK, client.serverState.localNickname)
  192. }
  193. @Test
  194. fun `defaults server name to host name`() = runBlocking {
  195. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  196. assertEquals(HOST, client.serverState.serverName)
  197. }
  198. @Test
  199. fun `reset clears all state`() {
  200. with (IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))) {
  201. userState += User("acidBurn")
  202. channelState += ChannelState("#thegibson") { CaseMapping.Rfc }
  203. serverState.serverName = "root.$HOST"
  204. reset()
  205. assertEquals(0, userState.count())
  206. assertEquals(0, channelState.count())
  207. assertEquals(HOST, serverState.serverName)
  208. }
  209. }
  210. }