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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package com.dmdirc.ktirc
  2. import com.dmdirc.ktirc.io.KtorLineBufferedSocket
  3. import com.dmdirc.ktirc.io.LineBufferedSocket
  4. import com.dmdirc.ktirc.model.Profile
  5. import com.dmdirc.ktirc.model.Server
  6. import com.nhaarman.mockitokotlin2.*
  7. import kotlinx.coroutines.channels.ArrayChannel
  8. import kotlinx.coroutines.channels.Channel
  9. import kotlinx.coroutines.channels.ReceiveChannel
  10. import kotlinx.coroutines.runBlocking
  11. import org.junit.jupiter.api.BeforeEach
  12. import org.junit.jupiter.api.Test
  13. import org.junit.jupiter.api.assertThrows
  14. import java.lang.IllegalStateException
  15. internal class IrcClientTest {
  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) -> LineBufferedSocket> {
  29. on { invoke(HOST, PORT) } doReturn mockSocket
  30. }
  31. @Test
  32. fun `IrcClient uses socket factory to create a new socket on connect`() {
  33. runBlocking {
  34. val client = IrcClient(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  35. client.socketFactory = mockSocketFactory
  36. readLineChannel.close()
  37. client.connect()
  38. verify(mockSocketFactory).invoke(HOST, PORT)
  39. }
  40. }
  41. @Test
  42. fun `IrcClient throws if socket already exists`() {
  43. runBlocking {
  44. val client = IrcClient(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  45. client.socketFactory = mockSocketFactory
  46. readLineChannel.close()
  47. client.connect()
  48. assertThrows<IllegalStateException> {
  49. runBlocking {
  50. client.connect()
  51. }
  52. }
  53. }
  54. }
  55. @Test
  56. fun `IrcClient sends basic connection strings`() {
  57. runBlocking {
  58. val client = IrcClient(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
  59. client.socketFactory = mockSocketFactory
  60. readLineChannel.close()
  61. client.connect()
  62. with(inOrder(mockSocket).verify(mockSocket)) {
  63. sendLine("NICK :$NICK")
  64. sendLine("USER $USER_NAME localhost $HOST :$REAL_NAME")
  65. }
  66. }
  67. }
  68. @Test
  69. fun `IrcClient sends password first, when present`() {
  70. runBlocking {
  71. val client = IrcClient(Server(HOST, PORT, password = PASSWORD), Profile(NICK, REAL_NAME, USER_NAME))
  72. client.socketFactory = mockSocketFactory
  73. readLineChannel.close()
  74. client.connect()
  75. with(inOrder(mockSocket).verify(mockSocket)) {
  76. sendLine("PASS :$PASSWORD")
  77. sendLine("NICK :$NICK")
  78. }
  79. }
  80. }
  81. }