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.

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