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.

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