Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package com.dmdirc.ktirc
  2. import com.dmdirc.ktirc.events.IrcEvent
  3. import com.dmdirc.ktirc.events.ServerWelcome
  4. import com.dmdirc.ktirc.io.CaseMapping
  5. import com.dmdirc.ktirc.io.LineBufferedSocket
  6. import com.dmdirc.ktirc.model.*
  7. import com.nhaarman.mockitokotlin2.*
  8. import kotlinx.coroutines.channels.Channel
  9. import kotlinx.coroutines.launch
  10. import kotlinx.coroutines.runBlocking
  11. import org.junit.jupiter.api.Assertions.*
  12. import org.junit.jupiter.api.BeforeEach
  13. import org.junit.jupiter.api.Test
  14. import org.junit.jupiter.api.assertThrows
  15. internal class IrcClientImplTest {
  16. companion object {
  17. private const val HOST = "thegibson.com"
  18. private const val PORT = 12345
  19. private const val NICK = "AcidBurn"
  20. private const val REAL_NAME = "Kate Libby"
  21. private const val USER_NAME = "acidb"
  22. private const val PASSWORD = "HackThePlanet"
  23. }
  24. private val readLineChannel = Channel<ByteArray>(10)
  25. private val mockSocket = mock<LineBufferedSocket> {
  26. on { readLines(any()) } doReturn readLineChannel
  27. }
  28. private val mockSocketFactory = mock<(String, Int, Boolean) -> LineBufferedSocket> {
  29. on { invoke(eq(HOST), eq(PORT), any()) } doReturn mockSocket
  30. }
  31. private val mockEventHandler = mock<(IrcEvent) -> Unit>()
  32. @BeforeEach
  33. fun setUp() {
  34. IrcMessage.currentTimeProvider = { TestConstants.time }
  35. }
  36. @Test
  37. fun `IrcClientImpl uses socket factory to create a new socket on connect`() {
  38. runBlocking {
  39. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  40. client.socketFactory = mockSocketFactory
  41. readLineChannel.close()
  42. client.connect()
  43. verify(mockSocketFactory).invoke(HOST, PORT, false)
  44. }
  45. }
  46. @Test
  47. fun `IrcClientImpl uses socket factory to create a new tls on connect`() {
  48. runBlocking {
  49. val client = IrcClientImpl(Server(HOST, PORT, true), Profile(NICK, REAL_NAME, USER_NAME))
  50. client.socketFactory = mockSocketFactory
  51. readLineChannel.close()
  52. client.connect()
  53. verify(mockSocketFactory).invoke(HOST, PORT, true)
  54. }
  55. }
  56. @Test
  57. fun `IrcClientImpl throws if socket already exists`() {
  58. runBlocking {
  59. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  60. client.socketFactory = mockSocketFactory
  61. readLineChannel.close()
  62. client.connect()
  63. assertThrows<IllegalStateException> {
  64. runBlocking {
  65. client.connect()
  66. }
  67. }
  68. }
  69. }
  70. @Test
  71. fun `IrcClientImpl sends basic connection strings`() {
  72. runBlocking {
  73. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  74. client.socketFactory = mockSocketFactory
  75. readLineChannel.close()
  76. client.connect()
  77. with(inOrder(mockSocket).verify(mockSocket)) {
  78. sendLine("CAP LS 302")
  79. sendLine("NICK :$NICK")
  80. sendLine("USER $USER_NAME localhost $HOST :$REAL_NAME")
  81. }
  82. }
  83. }
  84. @Test
  85. fun `IrcClientImpl sends password first, when present`() {
  86. runBlocking {
  87. val client = IrcClientImpl(Server(HOST, PORT, password = PASSWORD), Profile(NICK, REAL_NAME, USER_NAME))
  88. client.socketFactory = mockSocketFactory
  89. readLineChannel.close()
  90. client.connect()
  91. with(inOrder(mockSocket).verify(mockSocket)) {
  92. sendLine("CAP LS 302")
  93. sendLine("PASS :$PASSWORD")
  94. sendLine("NICK :$NICK")
  95. }
  96. }
  97. }
  98. @Test
  99. fun `IrcClientImpl sends events to provided event handler`() {
  100. runBlocking {
  101. val client = IrcClientImpl(Server(HOST, PORT, password = PASSWORD), Profile(NICK, REAL_NAME, USER_NAME))
  102. client.socketFactory = mockSocketFactory
  103. client.onEvent(mockEventHandler)
  104. launch {
  105. readLineChannel.send(":the.gibson 001 acidBurn :Welcome to the IRC!".toByteArray())
  106. readLineChannel.close()
  107. }
  108. client.connect()
  109. verify(mockEventHandler).invoke(isA<ServerWelcome>())
  110. }
  111. }
  112. @Test
  113. fun `IrcClient gets case mapping from server features`() {
  114. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  115. client.serverState.features[ServerFeature.ServerCaseMapping] = CaseMapping.RfcStrict
  116. assertEquals(CaseMapping.RfcStrict, client.caseMapping)
  117. }
  118. @Test
  119. fun `IrcClient indicates if user is local user or not`() {
  120. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  121. client.serverState.localNickname = "[acidBurn]"
  122. assertTrue(client.isLocalUser(User("{acidBurn}", "libby", "root.localhost")))
  123. assertFalse(client.isLocalUser(User("acid-Burn", "libby", "root.localhost")))
  124. }
  125. @Test
  126. fun `IrcClient uses current case mapping to check local user`() {
  127. val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  128. client.serverState.localNickname = "[acidBurn]"
  129. client.serverState.features[ServerFeature.ServerCaseMapping] = CaseMapping.Ascii
  130. assertFalse(client.isLocalUser(User("{acidBurn}", "libby", "root.localhost")))
  131. }
  132. }