Mirror of github.com/gissleh/irc
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.

310 lines
12 KiB

  1. package irc_test
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "testing"
  7. "git.aiterp.net/gisle/irc"
  8. "git.aiterp.net/gisle/irc/handlers"
  9. "git.aiterp.net/gisle/irc/internal/irctest"
  10. )
  11. // Integration test below, brace yourself.
  12. func TestClient(t *testing.T) {
  13. irc.Handle(handlers.Input)
  14. irc.Handle(handlers.MRoleplay)
  15. client := irc.New(context.Background(), irc.Config{
  16. Nick: "Test",
  17. User: "Tester",
  18. RealName: "...",
  19. Alternatives: []string{"Test2", "Test3", "Test4", "Test768"},
  20. SendRate: 1000,
  21. })
  22. t.Logf("Client.ID = %#+v", client.ID())
  23. if client.ID() == "" {
  24. t.Fail()
  25. }
  26. interaction := irctest.Interaction{
  27. Strict: false,
  28. Lines: []irctest.InteractionLine{
  29. {Kind: 'C', Data: "CAP LS 302"},
  30. {Kind: 'C', Data: "NICK Test"},
  31. {Kind: 'C', Data: "USER Tester 8 * :..."},
  32. {Kind: 'S', Data: ":testserver.example.com CAP * LS :multi-prefix chghost userhost-in-names vendorname/custom-stuff echo-message =malformed vendorname/advanced-custom-stuff=things,and,items"},
  33. {Kind: 'C', Data: "CAP REQ :multi-prefix chghost userhost-in-names"},
  34. {Kind: 'S', Data: ":testserver.example.com CAP * ACK :multi-prefix userhost-in-names"},
  35. {Kind: 'C', Data: "CAP END"},
  36. {Callback: func() error {
  37. if !client.CapEnabled("multi-prefix") {
  38. return errors.New("multi-prefix cap should be enabled.")
  39. }
  40. if !client.CapEnabled("userhost-in-names") {
  41. return errors.New("userhost-in-names cap should be enabled.")
  42. }
  43. if client.CapEnabled("echo-message") {
  44. return errors.New("echo-message cap should not be enabled.")
  45. }
  46. if client.CapEnabled("") {
  47. return errors.New("(blank) cap should be enabled.")
  48. }
  49. return nil
  50. }},
  51. {Kind: 'S', Data: ":testserver.example.com 433 * Test :Nick is not available"},
  52. {Kind: 'C', Data: "NICK Test2"},
  53. {Kind: 'S', Data: ":testserver.example.com 433 * Test2 :Nick is not available"},
  54. {Kind: 'C', Data: "NICK Test3"},
  55. {Kind: 'S', Data: ":testserver.example.com 433 * Test3 :Nick is not available"},
  56. {Kind: 'C', Data: "NICK Test4"},
  57. {Kind: 'S', Data: ":testserver.example.com 433 * Test4 :Nick is not available"},
  58. {Kind: 'C', Data: "NICK Test768"},
  59. {Kind: 'S', Data: ":testserver.example.com 001 Test768 :Welcome to the TestServer Internet Relay Chat Network test"},
  60. {Kind: 'C', Data: "WHO Test768*"},
  61. {Kind: 'S', Data: ":testserver.example.com 002 Test768 :Your host is testserver.example.com[testserver.example.com/6667], running version charybdis-4-rc3"},
  62. {Kind: 'S', Data: ":testserver.example.com 003 Test768 :This server was created Fri Nov 25 2016 at 17:28:20 CET"},
  63. {Kind: 'S', Data: ":testserver.example.com 004 Test768 testserver.example.com charybdis-4-rc3 DQRSZagiloswxz CFILNPQbcefgijklmnopqrstvz bkloveqjfI"},
  64. {Kind: 'S', Data: ":testserver.example.com 005 Test768 FNC SAFELIST ELIST=CTU MONITOR=100 WHOX ETRACE KNOCK CHANTYPES=#& EXCEPTS INVEX CHANMODES=eIbq,k,flj,CFLNPQcgimnprstz CHANLIMIT=#&:15 :are supported by this server"},
  65. {Kind: 'S', Data: ":testserver.example.com 005 Test768 PREFIX=(ov)@+ MAXLIST=bqeI:100 MODES=4 NETWORK=TestServer STATUSMSG=@+ CALLERID=g CASEMAPPING=rfc1459 NICKLEN=30 MAXNICKLEN=31 CHANNELLEN=50 TOPICLEN=390 DEAF=D :are supported by this server"},
  66. {Kind: 'S', Data: ":testserver.example.com 005 Test768 TARGMAX=NAMES:1,LIST:1,KICK:1,WHOIS:1,PRIVMSG:4,NOTICE:4,ACCEPT:,MONITOR: EXTBAN=$,&acjmorsuxz| CLIENTVER=3.0 :are supported by this server"},
  67. {Kind: 'S', Data: ":testserver.example.com 251 Test768 :There are 0 users and 2 invisible on 1 servers"},
  68. {Kind: 'S', Data: ":testserver.example.com 254 Test768 1 :channels formed"},
  69. {Kind: 'S', Data: ":testserver.example.com 255 Test768 :I have 2 clients and 0 servers"},
  70. {Kind: 'S', Data: ":testserver.example.com 265 Test768 2 2 :Current local users 2, max 2"},
  71. {Kind: 'S', Data: ":testserver.example.com 266 Test768 2 2 :Current global users 2, max 2"},
  72. {Kind: 'S', Data: ":testserver.example.com 250 Test768 :Highest connection count: 2 (2 clients) (8 connections received)"},
  73. {Kind: 'S', Data: ":testserver.example.com 375 Test768 :- testserver.example.com Message of the Day - "},
  74. {Kind: 'S', Data: ":testserver.example.com 372 Test768 :- This server is only for testing irce, not chatting. If you happen"},
  75. {Kind: 'S', Data: ":testserver.example.com 372 Test768 :- to connect to it by accident, please disconnect immediately."},
  76. {Kind: 'S', Data: ":testserver.example.com 372 Test768 :- "},
  77. {Kind: 'S', Data: ":testserver.example.com 372 Test768 :- - #Test :: Test Channel"},
  78. {Kind: 'S', Data: ":testserver.example.com 372 Test768 :- - #Test2 :: Other Test Channel"},
  79. {Kind: 'S', Data: ":testserver.example.com 376 Test768 :End of /MOTD command."},
  80. {Kind: 'S', Data: ":testserver.example.com 352 Test768 * ~Tester testclient.example.com testserver.example.com Test768 H :0 ..."},
  81. {Kind: 'S', Data: ":Test768 MODE Test768 :+i"},
  82. {Kind: 'S', Data: "PING :testserver.example.com"}, // Ping/Pong to sync.
  83. {Kind: 'C', Data: "PONG :testserver.example.com"},
  84. {Callback: func() error {
  85. if client.Nick() != "Test768" {
  86. return errors.New("client.Nick shouldn't be " + client.Nick())
  87. }
  88. if client.User() != "~Tester" {
  89. return errors.New("client.User shouldn't be " + client.User())
  90. }
  91. if client.Host() != "testclient.example.com" {
  92. return errors.New("client.Host shouldn't be " + client.Host())
  93. }
  94. return nil
  95. }},
  96. {Callback: func() error {
  97. err := client.Join("#Test")
  98. if err != nil {
  99. return fmt.Errorf("Failed to join #Test: %s", err)
  100. }
  101. return nil
  102. }},
  103. {Kind: 'C', Data: "JOIN #Test"},
  104. {Kind: 'S', Data: ":Test768!~test@127.0.0.1 JOIN #Test *"},
  105. {Kind: 'S', Data: ":testserver.example.com 353 Test768 = #Test :Test768!~test@127.0.0.1 @+Gisle!gisle@gisle.me"},
  106. {Kind: 'S', Data: ":testserver.example.com 366 Test768 #Test :End of /NAMES list."},
  107. {Kind: 'S', Data: ":Gisle!~irce@10.32.0.1 MODE #Test +osv Test768 Test768"},
  108. {Kind: 'S', Data: ":Gisle!~irce@10.32.0.1 MODE #Test +N-s "},
  109. {Kind: 'S', Data: ":Test1234!~test2@172.17.37.1 JOIN #Test Test1234"},
  110. {Kind: 'S', Data: ":Test4321!~test2@172.17.37.1 JOIN #Test Test1234"},
  111. {Kind: 'S', Data: ":Gisle!~irce@10.32.0.1 MODE #Test +v Test1234"},
  112. {Kind: 'S', Data: "PING :testserver.example.com"}, // Ping/Pong to sync.
  113. {Kind: 'C', Data: "PONG :testserver.example.com"},
  114. {Callback: func() error {
  115. channel := client.Channel("#Test")
  116. if channel == nil {
  117. return errors.New("Channel #Test not found")
  118. }
  119. err := irctest.AssertUserlist(t, channel, "@Gisle", "@Test768", "+Test1234", "Test4321")
  120. if err != nil {
  121. return err
  122. }
  123. userTest1234, ok := channel.UserList().User("Test1234")
  124. if !ok {
  125. return errors.New("Test1234 not found")
  126. }
  127. if userTest1234.Account != "Test1234" {
  128. return errors.New("Test1234 did not get account from extended-join")
  129. }
  130. return nil
  131. }},
  132. {Kind: 'S', Data: ":Test1234!~test2@172.17.37.1 NICK Hunter2"},
  133. {Kind: 'S', Data: ":Hunter2!~test2@172.17.37.1 AWAY :Doing stuff"},
  134. {Kind: 'S', Data: ":Gisle!~irce@10.32.0.1 AWAY"},
  135. {Kind: 'S', Data: ":Gisle!~irce@10.32.0.1 PART #Test :Leaving the channel"},
  136. {Kind: 'S', Data: ":Hunter2!~test2@172.17.37.1 CHGHOST test2 some.awesome.virtual.host"},
  137. {Kind: 'S', Data: "@account=Hunter2 :Test4321!~test2@172.17.37.1 PRIVMSG #Test :Hello World."},
  138. {Kind: 'S', Data: "PING :testserver.example.com"}, // Ping/Pong to sync.
  139. {Kind: 'C', Data: "PONG :testserver.example.com"},
  140. {Callback: func() error {
  141. channel := client.Channel("#Test")
  142. if channel == nil {
  143. return errors.New("Channel #Test not found")
  144. }
  145. err := irctest.AssertUserlist(t, channel, "@Test768", "+Hunter2", "Test4321")
  146. if err != nil {
  147. return err
  148. }
  149. _, ok := channel.UserList().User("Test1234")
  150. if ok {
  151. return errors.New("Test1234 is still there")
  152. }
  153. userHunter2, ok := channel.UserList().User("Hunter2")
  154. if !ok {
  155. return errors.New("Test1234 not found")
  156. }
  157. if userHunter2.Account != "Test1234" {
  158. return errors.New("Hunter2 did not persist account post nick change")
  159. }
  160. if !userHunter2.IsAway() {
  161. return errors.New("Hunter2 should be away")
  162. }
  163. if userHunter2.Away != "Doing stuff" {
  164. return errors.New("Hunter2 has the wrong away message: " + userHunter2.Away)
  165. }
  166. if userHunter2.Host != "some.awesome.virtual.host" {
  167. return errors.New("Hunter2 should have changed the host: " + userHunter2.Host)
  168. }
  169. return nil
  170. }},
  171. {Kind: 'S', Data: ":Hunter2!~test2@172.17.37.1 PRIVMSG Test768 :Hello, World"},
  172. {Kind: 'S', Data: "PING :testserver.example.com"}, // Ping/Pong to sync.
  173. {Kind: 'C', Data: "PONG :testserver.example.com"},
  174. {Callback: func() error {
  175. query := client.Query("Hunter2")
  176. if query == nil {
  177. return errors.New("Did not find query")
  178. }
  179. return nil
  180. }},
  181. {Kind: 'S', Data: ":Hunter2!~test2@172.17.37.1 NICK SevenAsterisks"},
  182. {Kind: 'S', Data: "PING :testserver.example.com"}, // Ping/Pong to sync.
  183. {Kind: 'C', Data: "PONG :testserver.example.com"},
  184. {Callback: func() error {
  185. oldQuerry := client.Query("Hunter2")
  186. if oldQuerry != nil {
  187. return errors.New("Did find query by old name")
  188. }
  189. query := client.Query("SevenAsterisks")
  190. if query == nil {
  191. return errors.New("Did not find query by new name")
  192. }
  193. return nil
  194. }},
  195. {Callback: func() error {
  196. client.EmitInput("/invalidcommand stuff and things", nil)
  197. return nil
  198. }},
  199. {Kind: 'C', Data: "INVALIDCOMMAND stuff and things"},
  200. {Kind: 'S', Data: ":testserver.example.com 421 Test768 INVALIDCOMMAND :Unknown command"},
  201. {Callback: func() error {
  202. channel := client.Channel("#Test")
  203. if channel == nil {
  204. return errors.New("Channel #Test not found")
  205. }
  206. client.EmitInput("/me does stuff", channel)
  207. client.EmitInput("/describe #Test describes stuff", channel)
  208. client.EmitInput("/text Hello, World", channel)
  209. client.EmitInput("Hello again", channel)
  210. return nil
  211. }},
  212. {Kind: 'C', Data: "PRIVMSG #Test :\x01ACTION does stuff\x01"},
  213. {Kind: 'C', Data: "PRIVMSG #Test :\x01ACTION describes stuff\x01"},
  214. {Kind: 'C', Data: "PRIVMSG #Test :Hello, World"},
  215. {Kind: 'C', Data: "PRIVMSG #Test :Hello again"},
  216. {Kind: 'S', Data: ":Test768!~test@127.0.0.1 PRIVMSG #Test :\x01ACTION does stuff\x01"},
  217. {Kind: 'S', Data: ":Test768!~test@127.0.0.1 PRIVMSG #Test :\x01ACTION describes stuff\x01"},
  218. {Kind: 'S', Data: ":Test768!~test@127.0.0.1 PRIVMSG #Test :Hello, World"},
  219. {Kind: 'S', Data: ":Test768!~test@127.0.0.1 PRIVMSG #Test :Hello again"},
  220. {Callback: func() error {
  221. channel := client.Channel("#Test")
  222. if channel == nil {
  223. return errors.New("Channel #Test not found")
  224. }
  225. client.EmitInput("/m +N", channel)
  226. client.EmitInput("/npcac Test_NPC stuffs things", channel)
  227. return nil
  228. }},
  229. {Kind: 'C', Data: "MODE #Test +N"},
  230. {Kind: 'C', Data: "NPCA #Test Test_NPC :stuffs things"},
  231. {Kind: 'S', Data: ":Test768!~test@127.0.0.1 MODE #Test +N"},
  232. {Kind: 'S', Data: ":\x1FTest_NPC\x1F!Test768@npc.fakeuser.invalid PRIVMSG #Test :\x01ACTION stuffs things\x01"},
  233. {Callback: func() error {
  234. channel := client.Channel("#Test")
  235. if channel == nil {
  236. return errors.New("Channel #Test not found")
  237. }
  238. client.Describef(channel.Name(), "does stuff with %d things", 42)
  239. client.Sayf(channel.Name(), "Hello, %s", "World")
  240. return nil
  241. }},
  242. {Kind: 'C', Data: "PRIVMSG #Test :\x01ACTION does stuff with 42 things\x01"},
  243. {Kind: 'C', Data: "PRIVMSG #Test :Hello, World"},
  244. {Callback: func() error {
  245. err := client.Part("#Test")
  246. if err != nil {
  247. return fmt.Errorf("Failed to part #Test: %s", err)
  248. }
  249. return nil
  250. }},
  251. {Kind: 'C', Data: "PART #Test"},
  252. },
  253. }
  254. addr, err := interaction.Listen()
  255. if err != nil {
  256. t.Fatal("Listen:", err)
  257. }
  258. if err := client.Disconnect(); err != irc.ErrNoConnection {
  259. t.Errorf("It should fail to disconnect, got: %s", err)
  260. }
  261. err = client.Connect(addr, false)
  262. if err != nil {
  263. t.Fatal("Connect:", err)
  264. return
  265. }
  266. interaction.Wait()
  267. fail := interaction.Failure
  268. if fail != nil {
  269. t.Error("Index:", fail.Index)
  270. t.Error("NetErr:", fail.NetErr)
  271. t.Error("CBErr:", fail.CBErr)
  272. t.Error("Result:", fail.Result)
  273. if fail.Index >= 0 {
  274. t.Error("Line.Kind:", interaction.Lines[fail.Index].Kind)
  275. t.Error("Line.Data:", interaction.Lines[fail.Index].Data)
  276. }
  277. }
  278. for i, logLine := range interaction.Log {
  279. t.Logf("Log[%d] = %#+v", i, logLine)
  280. }
  281. }