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.

401 lines
15 KiB

  1. package irc_test
  2. import (
  3. "context"
  4. "errors"
  5. "github.com/gissleh/irc/handlers"
  6. "testing"
  7. "github.com/gissleh/irc"
  8. "github.com/gissleh/irc/internal/irctest"
  9. )
  10. // Integration test below, brace yourself.
  11. func TestClient(t *testing.T) {
  12. client := irc.New(context.Background(), irc.Config{
  13. Nick: "Test",
  14. User: "Tester",
  15. RealName: "...",
  16. Alternatives: []string{"Test2", "Test3", "Test4", "Test768"},
  17. SendRate: 1000,
  18. })
  19. client.AddHandler(handlers.Input)
  20. client.AddHandler(handlers.MRoleplay)
  21. t.Logf("Client.ID = %#+v", client.ID())
  22. if client.ID() == "" {
  23. t.Fail()
  24. }
  25. interaction := irctest.Interaction{
  26. Strict: false,
  27. Lines: []irctest.InteractionLine{
  28. {Client: "CAP LS 302"},
  29. {Client: "NICK Test"},
  30. {Client: "USER Tester 8 * :..."},
  31. {Server: ":testserver.example.com CAP * LS :multi-prefix chghost userhost-in-names vendorname/custom-stuff echo-message =malformed vendorname/advanced-custom-stuff=things,and,items"},
  32. {Client: "CAP REQ :multi-prefix chghost userhost-in-names echo-message"},
  33. {Server: ":testserver.example.com CAP * ACK :multi-prefix userhost-in-names"},
  34. {Client: "CAP END"},
  35. {Callback: func() error {
  36. if !client.CapEnabled("multi-prefix") {
  37. return errors.New("multi-prefix cap should be enabled.")
  38. }
  39. if !client.CapEnabled("userhost-in-names") {
  40. return errors.New("userhost-in-names cap should be enabled.")
  41. }
  42. if client.CapEnabled("echo-message") {
  43. return errors.New("echo-message cap should not be enabled.")
  44. }
  45. if client.CapEnabled("") {
  46. return errors.New("(blank) cap should be enabled.")
  47. }
  48. return nil
  49. }},
  50. {Server: ":testserver.example.com 433 * Test :Nick is not available"},
  51. {Client: "NICK Test2"},
  52. {Server: ":testserver.example.com 433 * Test2 :Nick is not available"},
  53. {Client: "NICK Test3"},
  54. {Server: ":testserver.example.com 433 * Test3 :Nick is not available"},
  55. {Client: "NICK Test4"},
  56. {Server: ":testserver.example.com 433 * Test4 :Nick is not available"},
  57. {Client: "NICK Test768"},
  58. {Server: ":testserver.example.com 001 Test768 :Welcome to the TestServer Internet Relay Chat Network test"},
  59. {Client: "WHO Test768*"},
  60. {Server: ":testserver.example.com 002 Test768 :Your host is testserver.example.com[testserver.example.com/6667], running version charybdis-4-rc3"},
  61. {Server: ":testserver.example.com 003 Test768 :This server was created Fri Nov 25 2016 at 17:28:20 CET"},
  62. {Server: ":testserver.example.com 004 Test768 testserver.example.com charybdis-4-rc3 DQRSZagiloswxz CFILNPQbcefgijklmnopqrstvz bkloveqjfI"},
  63. {Server: ":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"},
  64. {Server: ":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"},
  65. {Server: ":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"},
  66. {Server: ":testserver.example.com 251 Test768 :There are 0 users and 2 invisible on 1 servers"},
  67. {Server: ":testserver.example.com 254 Test768 1 :channels formed"},
  68. {Server: ":testserver.example.com 255 Test768 :I have 2 clients and 0 servers"},
  69. {Server: ":testserver.example.com 265 Test768 2 2 :Current local users 2, max 2"},
  70. {Server: ":testserver.example.com 266 Test768 2 2 :Current global users 2, max 2"},
  71. {Server: ":testserver.example.com 250 Test768 :Highest connection count: 2 (2 clients) (8 connections received)"},
  72. {Server: ":testserver.example.com 375 Test768 :- testserver.example.com Message of the Day - "},
  73. {Server: ":testserver.example.com 372 Test768 :- This server is only for testing irce, not chatting. If you happen"},
  74. {Server: ":testserver.example.com 372 Test768 :- to connect to it by accident, please disconnect immediately."},
  75. {Server: ":testserver.example.com 372 Test768 :- "},
  76. {Server: ":testserver.example.com 372 Test768 :- - #Test :: Test Channel"},
  77. {Server: ":testserver.example.com 372 Test768 :- - #Test2 :: Other Test Channel"},
  78. {Server: ":testserver.example.com 376 Test768 :End of /MOTD command."},
  79. {Server: ":testserver.example.com 352 Test768 * ~Tester testclient.example.com testserver.example.com Test768 H :0 ..."},
  80. {Server: ":Test768 MODE Test768 :+i"},
  81. {Server: "PING :testserver.example.com"}, // Ping/Pong to sync.
  82. {Client: "PONG :testserver.example.com"},
  83. {Callback: func() error {
  84. if client.Nick() != "Test768" {
  85. return errors.New("client.Nick shouldn't be " + client.Nick())
  86. }
  87. if client.User() != "~Tester" {
  88. return errors.New("client.User shouldn't be " + client.User())
  89. }
  90. if client.Host() != "testclient.example.com" {
  91. return errors.New("client.Host shouldn't be " + client.Host())
  92. }
  93. return nil
  94. }},
  95. {Callback: func() error {
  96. client.Join("#Test")
  97. return nil
  98. }},
  99. {Client: "JOIN #Test"},
  100. {Server: ":Test768!~Tester@127.0.0.1 JOIN #Test *"},
  101. {Server: ":testserver.example.com 353 Test768 = #Test :Test768!~Tester@127.0.0.1 @+Gisle!irce@10.32.0.1"},
  102. {Server: ":testserver.example.com 366 Test768 #Test :End of /NAMES list."},
  103. {Server: "PING :testserver.example.com"}, // Ping/Pong to sync.
  104. {Client: "PONG :testserver.example.com"},
  105. {Callback: func() error {
  106. if client.Channel("#Test") == nil {
  107. return errors.New("Channel #Test not found")
  108. }
  109. return nil
  110. }},
  111. {Server: ":Gisle!~irce@10.32.0.1 MODE #Test +osv Test768 Test768"},
  112. {Server: ":Gisle!~irce@10.32.0.1 MODE #Test +N-s "},
  113. {Server: ":Test1234!~test2@172.17.37.1 JOIN #Test Test1234"},
  114. {Server: ":Test4321!~test2@172.17.37.1 JOIN #Test Test1234"},
  115. {Server: ":Gisle!~irce@10.32.0.1 MODE #Test +v Test1234"},
  116. {Server: "PING :testserver.example.com"}, // Ping/Pong to sync.
  117. {Client: "PONG :testserver.example.com"},
  118. {Callback: func() error {
  119. channel := client.Channel("#Test")
  120. if channel == nil {
  121. return errors.New("Channel #Test not found")
  122. }
  123. err := irctest.AssertUserlist(t, channel, "@Gisle", "@Test768", "+Test1234", "Test4321")
  124. if err != nil {
  125. return err
  126. }
  127. userTest1234, ok := channel.UserList().User("Test1234")
  128. if !ok {
  129. return errors.New("Test1234 not found")
  130. }
  131. if userTest1234.Account != "Test1234" {
  132. return errors.New("Test1234 did not get account from extended-join")
  133. }
  134. return nil
  135. }},
  136. {Server: ":Test1234!~test2@172.17.37.1 NICK Hunter2"},
  137. {Server: ":Hunter2!~test2@172.17.37.1 AWAY :Doing stuff"},
  138. {Server: ":Gisle!~irce@10.32.0.1 AWAY"},
  139. {Server: ":Gisle!~irce@10.32.0.1 PART #Test :Leaving the channel"},
  140. {Server: ":Hunter2!~test2@172.17.37.1 CHGHOST test2 some.awesome.virtual.host"},
  141. {Server: "@account=Hunter2 :Test4321!~test2@172.17.37.1 PRIVMSG #Test :Hello World."},
  142. {Server: "PING :testserver.example.com"}, // Ping/Pong to sync.
  143. {Client: "PONG :testserver.example.com"},
  144. {Callback: func() error {
  145. channel := client.Channel("#Test")
  146. if channel == nil {
  147. return errors.New("Channel #Test not found")
  148. }
  149. err := irctest.AssertUserlist(t, channel, "@Test768", "+Hunter2", "Test4321")
  150. if err != nil {
  151. return err
  152. }
  153. _, ok := channel.UserList().User("Test1234")
  154. if ok {
  155. return errors.New("Test1234 is still there")
  156. }
  157. userHunter2, ok := channel.UserList().User("Hunter2")
  158. if !ok {
  159. return errors.New("Test1234 not found")
  160. }
  161. if userHunter2.Account != "Test1234" {
  162. return errors.New("Hunter2 did not persist account post nick change")
  163. }
  164. if !userHunter2.IsAway() {
  165. return errors.New("Hunter2 should be away")
  166. }
  167. if userHunter2.Away != "Doing stuff" {
  168. return errors.New("Hunter2 has the wrong away message: " + userHunter2.Away)
  169. }
  170. if userHunter2.Host != "some.awesome.virtual.host" {
  171. return errors.New("Hunter2 should have changed the host: " + userHunter2.Host)
  172. }
  173. return nil
  174. }},
  175. {Server: ":Hunter2!~test2@172.17.37.1 PRIVMSG Test768 :Hello, World"},
  176. {Server: "PING :testserver.example.com"}, // Ping/Pong to sync.
  177. {Client: "PONG :testserver.example.com"},
  178. {Callback: func() error {
  179. query := client.Query("Hunter2")
  180. if query == nil {
  181. return errors.New("Did not find query")
  182. }
  183. return nil
  184. }},
  185. {Server: ":Hunter2!~test2@172.17.37.1 NICK SevenAsterisks"},
  186. {Server: "PING :testserver.example.com"}, // Ping/Pong to sync.
  187. {Client: "PONG :testserver.example.com"},
  188. {Callback: func() error {
  189. oldQuery := client.Query("Hunter2")
  190. if oldQuery != nil {
  191. return errors.New("Did find query by old name")
  192. }
  193. query := client.Query("SevenAsterisks")
  194. if query == nil {
  195. return errors.New("Did not find query by new name")
  196. }
  197. return nil
  198. }},
  199. {Callback: func() error {
  200. client.EmitInput("/invalidcommand stuff and things", nil)
  201. return nil
  202. }},
  203. {Client: "INVALIDCOMMAND stuff and things"},
  204. {Server: ":testserver.example.com 421 Test768 INVALIDCOMMAND :Unknown command"},
  205. {Callback: func() error {
  206. channel := client.Channel("#Test")
  207. if channel == nil {
  208. return errors.New("Channel #Test not found")
  209. }
  210. client.EmitInput("/me does stuff", channel)
  211. client.EmitInput("/describe #Test describes stuff", channel)
  212. client.EmitInput("/text Hello, World", channel)
  213. client.EmitInput("Hello again", channel)
  214. return nil
  215. }},
  216. {Client: "PRIVMSG #Test :\x01ACTION does stuff\x01"},
  217. {Client: "PRIVMSG #Test :\x01ACTION describes stuff\x01"},
  218. {Client: "PRIVMSG #Test :Hello, World"},
  219. {Client: "PRIVMSG #Test :Hello again"},
  220. {Server: ":Test768!~Tester@127.0.0.1 PRIVMSG #Test :\x01ACTION does stuff\x01"},
  221. {Server: ":Test768!~Tester@127.0.0.1 PRIVMSG #Test :\x01ACTION describes stuff\x01"},
  222. {Server: ":Test768!~Tester@127.0.0.1 PRIVMSG #Test :Hello, World"},
  223. {Server: ":Test768!~Tester@127.0.0.1 PRIVMSG #Test :Hello again"},
  224. {Callback: func() error {
  225. channel := client.Channel("#Test")
  226. if channel == nil {
  227. return errors.New("Channel #Test not found")
  228. }
  229. client.EmitInput("/m +N", channel)
  230. client.EmitInput("/npcac Test_NPC stuffs things", channel)
  231. return nil
  232. }},
  233. {Client: "MODE #Test +N"},
  234. {Client: "NPCA #Test Test_NPC :stuffs things"},
  235. {Server: ":Test768!~Tester@127.0.0.1 MODE #Test +N"},
  236. {Server: ":\x1FTest_NPC\x1F!Test768@npc.fakeuser.invalid PRIVMSG #Test :\x01ACTION stuffs things\x01"},
  237. {Callback: func() error {
  238. channel := client.Channel("#Test")
  239. if channel == nil {
  240. return errors.New("Channel #Test not found")
  241. }
  242. client.Describef(channel.Name(), "does stuff with %d things", 42)
  243. client.Sayf(channel.Name(), "Hello, %s", "World")
  244. return nil
  245. }},
  246. {Client: "PRIVMSG #Test :\x01ACTION does stuff with 42 things\x01"},
  247. {Client: "PRIVMSG #Test :Hello, World"},
  248. {Callback: func() error {
  249. channel := client.Channel("#Test")
  250. if channel == nil {
  251. return errors.New("#Test doesn't exist")
  252. }
  253. _, err := client.RemoveTarget(channel)
  254. return err
  255. }},
  256. {Client: "PART #Test"},
  257. {Server: ":Test768!~Tester@127.0.0.1 PART #Test"},
  258. {Server: "PING :testserver.example.com"}, // Ping/Pong to sync.
  259. {Client: "PONG :testserver.example.com"},
  260. {Callback: func() error {
  261. if client.Channel("#Test") != nil {
  262. return errors.New("#Test is still there.")
  263. }
  264. return nil
  265. }},
  266. {Callback: func() error {
  267. client.Join("#Test2")
  268. return nil
  269. }},
  270. {Client: "JOIN #Test2"},
  271. {Server: ":Test768!~Tester@127.0.0.1 JOIN #Test2 *"},
  272. {Server: ":testserver.example.com 353 Test768 = #Test2 :Test768!~Tester@127.0.0.1 +DoomedUser!doom@example.com @+ZealousMod!zeal@example.com"},
  273. {Server: ":testserver.example.com 366 Test768 #Test2 :End of /NAMES list."},
  274. {Server: "PING :testserver.example.com"}, // Ping/Pong to sync.
  275. {Client: "PONG :testserver.example.com"},
  276. {Callback: func() error {
  277. channel := client.Channel("#Test2")
  278. if channel == nil {
  279. return errors.New("Channel #Test2 not found")
  280. }
  281. return irctest.AssertUserlist(t, channel, "@ZealousMod", "+DoomedUser", "Test768")
  282. }},
  283. {Server: ":ZealousMod!zeal@example.com KICK #Test2 DoomedUser :Kickety kick"},
  284. {Server: "PING :testserver.example.com sync"}, // Ping/Pong to sync.
  285. {Client: "PONG :testserver.example.com sync"},
  286. {Callback: func() error {
  287. channel := client.Channel("#Test2")
  288. if channel == nil {
  289. return errors.New("Channel #Test2 not found")
  290. }
  291. return irctest.AssertUserlist(t, channel, "@ZealousMod", "Test768")
  292. }},
  293. {Server: ":ZealousMod!zeal@example.com KICK #Test2 Test768 :Kickety kick"},
  294. {Server: "PING :testserver.example.com sync"}, // Ping/Pong to sync.
  295. {Client: "PONG :testserver.example.com sync"},
  296. {Callback: func() error {
  297. if client.Channel("#Test2") != nil {
  298. return errors.New("#Test2 is still there.")
  299. }
  300. return nil
  301. }},
  302. },
  303. }
  304. addr, err := interaction.Listen()
  305. if err != nil {
  306. t.Fatal("Listen:", err)
  307. }
  308. if err := client.Disconnect(); err != irc.ErrNoConnection {
  309. t.Errorf("It should fail to disconnect, got: %s", err)
  310. }
  311. err = client.Connect(addr, false)
  312. if err != nil {
  313. t.Fatal("Connect:", err)
  314. return
  315. }
  316. interaction.Wait()
  317. fail := interaction.Failure
  318. if fail != nil {
  319. t.Error("Index:", fail.Index)
  320. t.Error("NetErr:", fail.NetErr)
  321. t.Error("CBErr:", fail.CBErr)
  322. t.Error("Result:", fail.Result)
  323. if fail.Index >= 0 {
  324. if interaction.Lines[fail.Index].Server != "" {
  325. t.Error("Line.Server:", interaction.Lines[fail.Index].Server)
  326. }
  327. if interaction.Lines[fail.Index].Client != "" {
  328. t.Error("Line.Client:", interaction.Lines[fail.Index].Client)
  329. }
  330. }
  331. }
  332. for i, logLine := range interaction.Log {
  333. t.Logf("Log[%d] = %#+v", i, logLine)
  334. }
  335. }
  336. // TestParenthesesBug tests that the bugfix causing `((Remove :01 goofs!*))` to be parsed as an empty message. It was
  337. // initially thought to be caused by the parentheses (like a hidden m_roleplay NPC attribution removal), hence the name
  338. // for this function.
  339. func TestParenthesesBug(t *testing.T) {
  340. gotMessage := true
  341. client := irc.New(context.Background(), irc.Config{
  342. Nick: "Stuff",
  343. })
  344. client.AddHandler(func(event *irc.Event, client *irc.Client) {
  345. if event.Name() == "packet.privmsg" || event.Nick == "Beans" {
  346. gotMessage = true
  347. if event.Text != "((Remove :01 goofs!*))" {
  348. t.Errorf("Expected: %#+v", "((Remove :01 goofs!*))")
  349. t.Errorf("Result: %#+v", event.Text)
  350. }
  351. }
  352. })
  353. packet, err := irc.ParsePacket("@example/tag=32; :Beans!beans@beans.example.com PRIVMSG Stuff :((Remove :01 goofs!*))")
  354. if err != nil {
  355. t.Error("Parse", err)
  356. }
  357. client.EmitSync(context.Background(), packet)
  358. if !gotMessage {
  359. t.Error("Message was not received")
  360. }
  361. }