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.

DslTest.kt 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. package com.dmdirc.ktirc
  2. import org.junit.jupiter.api.Assertions.*
  3. import org.junit.jupiter.api.Test
  4. import org.junit.jupiter.api.assertThrows
  5. import java.time.Duration
  6. internal class IrcClientConfigBuilderTest {
  7. @Test
  8. fun `throws if server is defined twice`() {
  9. assertThrows<IllegalStateException> {
  10. IrcClientConfigBuilder().apply {
  11. server { host = "1" }
  12. server { host = "2" }
  13. }
  14. }
  15. }
  16. @Test
  17. fun `throws if no host is provided`() {
  18. assertThrows<IllegalStateException> {
  19. IrcClientConfigBuilder().apply {
  20. server {}
  21. }
  22. }
  23. }
  24. @Test
  25. fun `throws if empty host is provided`() {
  26. assertThrows<IllegalStateException> {
  27. IrcClientConfigBuilder().apply {
  28. server("")
  29. }
  30. }
  31. }
  32. @Test
  33. fun `throws if profile is defined twice`() {
  34. assertThrows<IllegalStateException> {
  35. IrcClientConfigBuilder().apply {
  36. profile { nickname = "acidBurn" }
  37. profile { nickname = "zeroCool" }
  38. }
  39. }
  40. }
  41. @Test
  42. fun `throws if no nickname is provided`() {
  43. assertThrows<IllegalStateException> {
  44. IrcClientConfigBuilder().apply {
  45. profile {}
  46. }
  47. }
  48. }
  49. @Test
  50. fun `throws if empty nickname is provided`() {
  51. assertThrows<IllegalStateException> {
  52. IrcClientConfigBuilder().apply {
  53. profile("")
  54. }
  55. }
  56. }
  57. @Test
  58. fun `throws if behaviour is defined twice`() {
  59. assertThrows<IllegalStateException> {
  60. IrcClientConfigBuilder().apply {
  61. behaviour {}
  62. behaviour {}
  63. }
  64. }
  65. }
  66. @Test
  67. fun `throws if sasl is defined twice`() {
  68. assertThrows<IllegalStateException> {
  69. IrcClientConfigBuilder().apply {
  70. sasl {}
  71. sasl {}
  72. }
  73. }
  74. }
  75. @Test
  76. fun `throws if server is not defined`() {
  77. assertThrows<IllegalStateException> {
  78. IrcClientConfigBuilder().apply {
  79. profile { nickname = "acidBurn" }
  80. }.build()
  81. }
  82. }
  83. @Test
  84. fun `throws if profile is not defined`() {
  85. assertThrows<IllegalStateException> {
  86. IrcClientConfigBuilder().apply {
  87. server { host = "thegibson.com" }
  88. }.build()
  89. }
  90. }
  91. @Test
  92. fun `applies server settings`() {
  93. val config = IrcClientConfigBuilder().apply {
  94. profile { nickname = "acidBurn" }
  95. server {
  96. host = "thegibson.com"
  97. port = 1337
  98. password = "h4cktheplan3t"
  99. useTls = true
  100. }
  101. }.build()
  102. assertEquals("thegibson.com", config.server.host)
  103. assertEquals(1337, config.server.port)
  104. assertEquals("h4cktheplan3t", config.server.password)
  105. assertTrue(config.server.useTls)
  106. }
  107. @Test
  108. fun `applies server settings with convenience function`() {
  109. val config = IrcClientConfigBuilder().apply {
  110. profile { nickname = "acidBurn" }
  111. server("thegibson.com", 1337, true, "h4cktheplan3t")
  112. }.build()
  113. assertEquals("thegibson.com", config.server.host)
  114. assertEquals(1337, config.server.port)
  115. assertEquals("h4cktheplan3t", config.server.password)
  116. assertTrue(config.server.useTls)
  117. }
  118. @Test
  119. fun `applies server settings with convenience function and block`() {
  120. val config = IrcClientConfigBuilder().apply {
  121. profile { nickname = "acidBurn" }
  122. server("thegibson.com", 1337) {
  123. password = "h4cktheplan3t"
  124. useTls = true
  125. }
  126. }.build()
  127. assertEquals("thegibson.com", config.server.host)
  128. assertEquals(1337, config.server.port)
  129. assertEquals("h4cktheplan3t", config.server.password)
  130. assertTrue(config.server.useTls)
  131. }
  132. @Test
  133. fun `applies profile settings`() {
  134. val config = IrcClientConfigBuilder().apply {
  135. profile {
  136. nickname = "acidBurn"
  137. username = "acidB"
  138. realName = "Kate"
  139. }
  140. server { host = "thegibson.com" }
  141. }.build()
  142. assertEquals("acidBurn", config.profile.nickname)
  143. assertEquals("acidB", config.profile.username)
  144. assertEquals("Kate", config.profile.realName)
  145. }
  146. @Test
  147. fun `applies profile settings with convenience function`() {
  148. val config = IrcClientConfigBuilder().apply {
  149. profile("acidBurn", "acidB", "Kate")
  150. server { host = "thegibson.com" }
  151. }.build()
  152. assertEquals("acidBurn", config.profile.nickname)
  153. assertEquals("acidB", config.profile.username)
  154. assertEquals("Kate", config.profile.realName)
  155. }
  156. @Test
  157. fun `applies profile settings with convenience function and block`() {
  158. val config = IrcClientConfigBuilder().apply {
  159. profile("acidBurn", "acidB") {
  160. realName = "Kate"
  161. }
  162. server { host = "thegibson.com" }
  163. }.build()
  164. assertEquals("acidBurn", config.profile.nickname)
  165. assertEquals("acidB", config.profile.username)
  166. assertEquals("Kate", config.profile.realName)
  167. }
  168. @Test
  169. fun `applies behaviour settings`() {
  170. val config = IrcClientConfigBuilder().apply {
  171. profile { nickname = "acidBurn" }
  172. server { host = "thegibson.com" }
  173. behaviour {
  174. requestModesOnJoin = true
  175. }
  176. }.build()
  177. assertTrue(config.behaviour.requestModesOnJoin)
  178. }
  179. @Test
  180. fun `falls back to default behaviour settings`() {
  181. val config = IrcClientConfigBuilder().apply {
  182. profile { nickname = "acidBurn" }
  183. server { host = "thegibson.com" }
  184. }.build()
  185. assertFalse(config.behaviour.requestModesOnJoin)
  186. }
  187. @Test
  188. fun `defaults to null ping timeouts if sendPings omitted`() {
  189. val config = IrcClientConfigBuilder().apply {
  190. profile { nickname = "acidBurn" }
  191. server { host = "thegibson.com" }
  192. behaviour {
  193. requestModesOnJoin = true
  194. }
  195. }.build()
  196. assertNull(config.behaviour.pingTimeouts)
  197. }
  198. @Test
  199. fun `throws if sendPings specified without send period`() {
  200. assertThrows<IllegalArgumentException> {
  201. IrcClientConfigBuilder().apply {
  202. profile { nickname = "acidBurn" }
  203. server { host = "thegibson.com" }
  204. behaviour {
  205. requestModesOnJoin = true
  206. sendPings {
  207. responseGracePeriod = Duration.ofSeconds(10)
  208. }
  209. }
  210. }.build()
  211. }
  212. }
  213. @Test
  214. fun `throws if sendPings specified without grace period`() {
  215. assertThrows<IllegalArgumentException> {
  216. IrcClientConfigBuilder().apply {
  217. profile { nickname = "acidBurn" }
  218. server { host = "thegibson.com" }
  219. behaviour {
  220. requestModesOnJoin = true
  221. sendPings {
  222. sendPeriod = Duration.ofSeconds(10)
  223. }
  224. }
  225. }.build()
  226. }
  227. }
  228. @Test
  229. fun `throws if sendPings specified twice`() {
  230. assertThrows<IllegalStateException> {
  231. IrcClientConfigBuilder().apply {
  232. profile { nickname = "acidBurn" }
  233. server { host = "thegibson.com" }
  234. behaviour {
  235. requestModesOnJoin = true
  236. sendPings {
  237. sendPeriod = Duration.ofSeconds(10)
  238. responseGracePeriod = Duration.ofSeconds(10)
  239. }
  240. sendPings {
  241. sendPeriod = Duration.ofSeconds(10)
  242. responseGracePeriod = Duration.ofSeconds(10)
  243. }
  244. }
  245. }.build()
  246. }
  247. }
  248. @Test
  249. fun `configures ping timeouts`() {
  250. val config = IrcClientConfigBuilder().apply {
  251. profile { nickname = "acidBurn" }
  252. server { host = "thegibson.com" }
  253. behaviour {
  254. requestModesOnJoin = true
  255. sendPings {
  256. sendPeriod = Duration.ofSeconds(50)
  257. responseGracePeriod = Duration.ofSeconds(100)
  258. incomingLinesResetTimer = true
  259. }
  260. }
  261. }.build()
  262. assertEquals(50, config.behaviour.pingTimeouts?.sendPeriod?.seconds)
  263. assertEquals(100, config.behaviour.pingTimeouts?.responseGracePeriod?.seconds)
  264. assertEquals(true, config.behaviour.pingTimeouts?.incomingLinesResetTimer)
  265. }
  266. @Test
  267. fun `applies sasl settings`() {
  268. val config = IrcClientConfigBuilder().apply {
  269. profile { nickname = "acidBurn" }
  270. server { host = "thegibson.com" }
  271. sasl {
  272. username = "acidBurn"
  273. password = "h4ckthepl@net"
  274. }
  275. }.build()
  276. assertEquals("acidBurn", config.sasl?.username)
  277. assertEquals("h4ckthepl@net", config.sasl?.password)
  278. }
  279. }
  280. internal class SaslConfigTest {
  281. @Test
  282. fun `mechanisms function clears all existing mechanisms`() {
  283. val config = SaslConfig().apply {
  284. mechanisms += "TEST"
  285. mechanisms("FOO", "BAR")
  286. }
  287. assertEquals(setOf("FOO", "BAR"), config.mechanisms)
  288. }
  289. @Test
  290. fun `defaults to plain and scram mechanisms`() {
  291. val config = SaslConfig()
  292. assertEquals(setOf("PLAIN", "SCRAM-SHA-1", "SCRAM-SHA-256"), config.mechanisms)
  293. }
  294. }