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.

209 lines
8.4 KiB

  1. package irc_test
  2. import (
  3. "context"
  4. "errors"
  5. "testing"
  6. "git.aiterp.net/gisle/irc"
  7. "git.aiterp.net/gisle/irc/internal/irctest"
  8. )
  9. func TestClient(t *testing.T) {
  10. client := irc.New(context.Background(), irc.Config{
  11. Nick: "Test",
  12. User: "Tester",
  13. RealName: "...",
  14. Alternatives: []string{"Test2", "Test3", "Test4", "Test768"},
  15. })
  16. t.Logf("Client.ID = %#+v", client.ID())
  17. if client.ID() == "" {
  18. t.Fail()
  19. }
  20. interaction := irctest.Interaction{
  21. Strict: false,
  22. Lines: []irctest.InteractionLine{
  23. {Kind: 'C', Data: "CAP LS 302"},
  24. {Kind: 'C', Data: "NICK Test"},
  25. {Kind: 'C', Data: "USER Tester 8 * :..."},
  26. {Kind: 'S', Data: ":testserver.example.com CAP * LS :multi-prefix userhost-in-names"},
  27. {Kind: 'C', Data: "CAP REQ :multi-prefix userhost-in-names"},
  28. {Kind: 'S', Data: ":testserver.example.com CAP * ACK :multi-prefix userhost-in-names"},
  29. {Kind: 'C', Data: "CAP END"},
  30. {Kind: 'S', Data: ":testserver.example.com 443 * Test :Nick is not available"},
  31. {Kind: 'C', Data: "NICK Test2"},
  32. {Kind: 'S', Data: ":testserver.example.com 443 * Test2 :Nick is not available"},
  33. {Kind: 'C', Data: "NICK Test3"},
  34. {Kind: 'S', Data: ":testserver.example.com 443 * Test3 :Nick is not available"},
  35. {Kind: 'C', Data: "NICK Test4"},
  36. {Kind: 'S', Data: ":testserver.example.com 443 * Test4 :Nick is not available"},
  37. {Kind: 'C', Data: "NICK Test768"},
  38. {Kind: 'S', Data: ":testserver.example.com 001 Test768 :Welcome to the TestServer Internet Relay Chat Network test"},
  39. {Kind: 'C', Data: "WHO Test768*"},
  40. {Kind: 'S', Data: ":testserver.example.com 002 Test768 :Your host is testserver.example.com[testserver.example.com/6667], running version charybdis-4-rc3"},
  41. {Kind: 'S', Data: ":testserver.example.com 003 Test768 :This server was created Fri Nov 25 2016 at 17:28:20 CET"},
  42. {Kind: 'S', Data: ":testserver.example.com 004 Test768 testserver.example.com charybdis-4-rc3 DQRSZagiloswxz CFILNPQbcefgijklmnopqrstvz bkloveqjfI"},
  43. {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"},
  44. {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"},
  45. {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"},
  46. {Kind: 'S', Data: ":testserver.example.com 251 Test768 :There are 0 users and 2 invisible on 1 servers"},
  47. {Kind: 'S', Data: ":testserver.example.com 254 Test768 1 :channels formed"},
  48. {Kind: 'S', Data: ":testserver.example.com 255 Test768 :I have 2 clients and 0 servers"},
  49. {Kind: 'S', Data: ":testserver.example.com 265 Test768 2 2 :Current local users 2, max 2"},
  50. {Kind: 'S', Data: ":testserver.example.com 266 Test768 2 2 :Current global users 2, max 2"},
  51. {Kind: 'S', Data: ":testserver.example.com 250 Test768 :Highest connection count: 2 (2 clients) (8 connections received)"},
  52. {Kind: 'S', Data: ":testserver.example.com 352 Test768 * ~Tester testclient.example.com testserver.example.com Test768 H :0 ..."},
  53. {Kind: 'S', Data: ":testserver.example.com 375 Test768 :- testserver.example.com Message of the Day - "},
  54. {Kind: 'S', Data: ":testserver.example.com 372 Test768 :- This server is only for testing irce, not chatting. If you happen"},
  55. {Kind: 'S', Data: ":testserver.example.com 372 Test768 :- to connect to it by accident, please disconnect immediately."},
  56. {Kind: 'S', Data: ":testserver.example.com 372 Test768 :- "},
  57. {Kind: 'S', Data: ":testserver.example.com 372 Test768 :- - #Test :: Test Channel"},
  58. {Kind: 'S', Data: ":testserver.example.com 372 Test768 :- - #Test2 :: Other Test Channel"},
  59. {Kind: 'S', Data: ":testserver.example.com 376 Test768 :End of /MOTD command."},
  60. {Kind: 'S', Data: ":Test768 MODE Test768 :+i"},
  61. {Kind: 'C', Data: "JOIN #Test"},
  62. {Kind: 'S', Data: ":Test768!~test@127.0.0.1 JOIN #Test *"},
  63. {Kind: 'S', Data: ":testserver.example.com 353 Test768 = #Test :Test768!~test@127.0.0.1 @+Gisle!gisle@gisle.me"},
  64. {Kind: 'S', Data: ":testserver.example.com 366 Test768 #Test :End of /NAMES list."},
  65. {Kind: 'S', Data: ":Gisle!~irce@10.32.0.1 MODE #Test +osv Test768 Test768"},
  66. {Kind: 'S', Data: ":Gisle!~irce@10.32.0.1 MODE #Test +N-s "},
  67. {Kind: 'S', Data: ":Test1234!~test2@172.17.37.1 JOIN #Test Test1234"},
  68. {Kind: 'S', Data: ":Gisle!~irce@10.32.0.1 MODE #Test +v Test1234"},
  69. {Kind: 'S', Data: "PING :archgisle.lan"}, // Ping/Pong to sync.
  70. {Kind: 'C', Data: "PONG :archgisle.lan"},
  71. {Callback: func() error {
  72. channel := client.Channel("#Test")
  73. if channel == nil {
  74. return errors.New("Channel #Test not found")
  75. }
  76. err := irctest.AssertUserlist(t, channel, "@Gisle", "@Test768", "+Test1234")
  77. if err != nil {
  78. return err
  79. }
  80. userTest1234, ok := channel.UserList().User("Test1234")
  81. if !ok {
  82. return errors.New("Test1234 not found")
  83. }
  84. if userTest1234.Account != "Test1234" {
  85. return errors.New("Test1234 did not get account from extended-join")
  86. }
  87. return nil
  88. }},
  89. {Kind: 'S', Data: ":Test1234!~test2@172.17.37.1 NICK Hunter2"},
  90. {Kind: 'S', Data: ":Hunter2!~test2@172.17.37.1 AWAY :Doing stuff"},
  91. {Kind: 'S', Data: ":Gisle!~irce@10.32.0.1 AWAY"},
  92. {Kind: 'S', Data: ":Gisle!~irce@10.32.0.1 PART #Test :Leaving the channel"},
  93. {Kind: 'S', Data: "PING :archgisle.lan"}, // Ping/Pong to sync.
  94. {Kind: 'C', Data: "PONG :archgisle.lan"},
  95. {Callback: func() error {
  96. channel := client.Channel("#Test")
  97. if channel == nil {
  98. return errors.New("Channel #Test not found")
  99. }
  100. err := irctest.AssertUserlist(t, channel, "@Test768", "+Hunter2")
  101. if err != nil {
  102. return err
  103. }
  104. _, ok := channel.UserList().User("Test1234")
  105. if ok {
  106. return errors.New("Test1234 is still there")
  107. }
  108. userHunter2, ok := channel.UserList().User("Hunter2")
  109. if !ok {
  110. return errors.New("Test1234 not found")
  111. }
  112. if userHunter2.Account != "Test1234" {
  113. return errors.New("Hunter2 did not persist account post nick change")
  114. }
  115. if !userHunter2.IsAway() {
  116. return errors.New("Hunter2 should be away")
  117. }
  118. if userHunter2.Away != "Doing stuff" {
  119. return errors.New("Hunter2 has the wrong away message: " + userHunter2.Away)
  120. }
  121. return nil
  122. }},
  123. {Kind: 'S', Data: ":Hunter2!~test2@172.17.37.1 PRIVMSG Test768 :Hello, World"},
  124. {Kind: 'S', Data: "PING :archgisle.lan"}, // Ping/Pong to sync.
  125. {Kind: 'C', Data: "PONG :archgisle.lan"},
  126. {Callback: func() error {
  127. query := client.Query("Hunter2")
  128. if query == nil {
  129. return errors.New("Did not find query")
  130. }
  131. return nil
  132. }},
  133. {Kind: 'S', Data: ":Hunter2!~test2@172.17.37.1 NICK SevenAsterisks"},
  134. {Kind: 'S', Data: "PING :archgisle.lan"}, // Ping/Pong to sync.
  135. {Kind: 'C', Data: "PONG :archgisle.lan"},
  136. {Callback: func() error {
  137. oldQuerry := client.Query("Hunter2")
  138. if oldQuerry != nil {
  139. return errors.New("Did find query by old name")
  140. }
  141. query := client.Query("SevenAsterisks")
  142. if query == nil {
  143. return errors.New("Did not find query by new name")
  144. }
  145. return nil
  146. }},
  147. },
  148. }
  149. addr, err := interaction.Listen()
  150. if err != nil {
  151. t.Fatal("Listen:", err)
  152. }
  153. irc.Handle(func(event *irc.Event, client *irc.Client) {
  154. if event.Name() == "packet.376" {
  155. client.SendQueued("JOIN #Test")
  156. }
  157. })
  158. err = client.Connect(addr, false)
  159. if err != nil {
  160. t.Fatal("Connect:", err)
  161. return
  162. }
  163. interaction.Wait()
  164. fail := interaction.Failure
  165. if fail != nil {
  166. t.Error("Index:", fail.Index)
  167. t.Error("NetErr:", fail.NetErr)
  168. t.Error("CBErr:", fail.CBErr)
  169. t.Error("Result:", fail.Result)
  170. if fail.Index >= 0 {
  171. t.Error("Line.Kind:", interaction.Lines[fail.Index].Kind)
  172. t.Error("Line.Data:", interaction.Lines[fail.Index].Data)
  173. }
  174. }
  175. if client.Nick() != "Test768" {
  176. t.Errorf("Nick: %#+v != %#+v (Expectation)", client.Nick(), "Test768")
  177. }
  178. if client.User() != "~Tester" {
  179. t.Errorf("User: %#+v != %#+v (Expectation)", client.User(), "~Tester")
  180. }
  181. if client.Host() != "testclient.example.com" {
  182. t.Errorf("Host: %#+v != %#+v (Expectation)", client.Host(), "testclient.example.com")
  183. }
  184. for i, logLine := range interaction.Log {
  185. t.Logf("Log[%d] = %#+v", i, logLine)
  186. }
  187. }