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 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 `sends basic connection strings`() = runBlocking {
  81. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  82. client.socketFactory = mockSocketFactory
  83. client.connect()
  84. assertEquals("CAP LS 302", String(sendLineChannel.receive()))
  85. assertEquals("NICK :$NICK", String(sendLineChannel.receive()))
  86. assertEquals("USER $USER_NAME 0 * :$REAL_NAME", String(sendLineChannel.receive()))
  87. }
  88. @Test
  89. fun `sends password first, when present`() = runBlocking {
  90. val client = IrcClientImpl(Server(HOST, PORT, password = PASSWORD), Profile(NICK, REAL_NAME, USER_NAME))
  91. client.socketFactory = mockSocketFactory
  92. client.connect()
  93. assertEquals("CAP LS 302", String(sendLineChannel.receive()))
  94. assertEquals("PASS :$PASSWORD", String(sendLineChannel.receive()))
  95. }
  96. @Test
  97. fun `sends events to provided event handler`() {
  98. val client = IrcClientImpl(Server(HOST, PORT, password = PASSWORD), Profile(NICK, REAL_NAME, USER_NAME))
  99. client.socketFactory = mockSocketFactory
  100. client.onEvent(mockEventHandler)
  101. GlobalScope.launch {
  102. readLineChannel.send(":the.gibson 001 acidBurn :Welcome to the IRC!".toByteArray())
  103. }
  104. client.connect()
  105. verify(mockEventHandler, timeout(500)).invoke(isA<ServerWelcome>())
  106. }
  107. @Test
  108. fun `gets case mapping from server features`() {
  109. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  110. client.serverState.features[ServerFeature.ServerCaseMapping] = CaseMapping.RfcStrict
  111. assertEquals(CaseMapping.RfcStrict, client.caseMapping)
  112. }
  113. @Test
  114. fun `indicates if user is local user or not`() {
  115. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  116. client.serverState.localNickname = "[acidBurn]"
  117. assertTrue(client.isLocalUser(User("{acidBurn}", "libby", "root.localhost")))
  118. assertFalse(client.isLocalUser(User("acid-Burn", "libby", "root.localhost")))
  119. }
  120. @Test
  121. fun `indicates if nickname is local user or not`() {
  122. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  123. client.serverState.localNickname = "[acidBurn]"
  124. assertTrue(client.isLocalUser("{acidBurn}"))
  125. assertFalse(client.isLocalUser("acid-Burn"))
  126. }
  127. @Test
  128. fun `uses current case mapping to check local user`() {
  129. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  130. client.serverState.localNickname = "[acidBurn]"
  131. client.serverState.features[ServerFeature.ServerCaseMapping] = CaseMapping.Ascii
  132. assertFalse(client.isLocalUser(User("{acidBurn}", "libby", "root.localhost")))
  133. }
  134. @Test
  135. fun `sends text to socket`() = runBlocking {
  136. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  137. client.socketFactory = mockSocketFactory
  138. client.connect()
  139. client.send("testing 123")
  140. assertEquals(true, withTimeoutOrNull(500) {
  141. var found = false
  142. for (line in sendLineChannel) {
  143. if (String(line) == "testing 123") {
  144. found = true
  145. break
  146. }
  147. }
  148. found
  149. })
  150. }
  151. @Test
  152. fun `disconnects the socket`() = runBlocking {
  153. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  154. client.socketFactory = mockSocketFactory
  155. client.connect()
  156. client.disconnect()
  157. verify(mockSocket, timeout(500)).disconnect()
  158. }
  159. @Test
  160. fun `sends messages in order`() = runBlocking {
  161. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  162. client.socketFactory = mockSocketFactory
  163. client.connect()
  164. (0..100).forEach { client.send("TEST $it") }
  165. assertEquals(100, withTimeoutOrNull(500) {
  166. var next = 0
  167. for (line in sendLineChannel.map { String(it) }.filter { it.startsWith("TEST ") }) {
  168. assertEquals("TEST $next", line)
  169. if (++next == 100) {
  170. break
  171. }
  172. }
  173. next
  174. })
  175. }
  176. @Test
  177. fun `defaults local nickname to profile`() = runBlocking {
  178. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  179. assertEquals(NICK, client.serverState.localNickname)
  180. }
  181. @Test
  182. fun `defaults server name to host name`() = runBlocking {
  183. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  184. assertEquals(HOST, client.serverState.serverName)
  185. }
  186. @Test
  187. fun `reset clears all state`() {
  188. with (IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))) {
  189. userState += User("acidBurn")
  190. channelState += ChannelState("#thegibson") { CaseMapping.Rfc }
  191. serverState.serverName = "root.$HOST"
  192. reset()
  193. assertEquals(0, userState.count())
  194. assertEquals(0, channelState.count())
  195. assertEquals(HOST, serverState.serverName)
  196. }
  197. }
  198. }