client_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. package ircclient
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net"
  6. "reflect"
  7. "strconv"
  8. "strings"
  9. "syscall"
  10. "testing"
  11. )
  12. func TestMarshal(t *testing.T) {
  13. var config IRCConfig = IRCConfig{
  14. Port: 6667,
  15. Hostname: "irc.red.green.com",
  16. UseTLS: false,
  17. Nick: "test",
  18. Username: "testuser",
  19. Realname: "Marshal Test",
  20. Password: "12345",
  21. AutoJoin: []string{"#test", "#chat"},
  22. RejoinDelay: 2000,
  23. Version: "Test v3.14",
  24. Debug_Output: true,
  25. }
  26. var buffer []byte
  27. var err error
  28. buffer, err = json.Marshal(&config)
  29. if err != nil {
  30. t.Error("json.Marshal:", err)
  31. }
  32. var verify IRCConfig
  33. err = json.Unmarshal(buffer, &verify)
  34. if err != nil {
  35. t.Error("json.Unmarshal:", err)
  36. }
  37. var fields []reflect.StructField
  38. fields = reflect.VisibleFields(reflect.TypeOf(config))
  39. var config_value reflect.Value = reflect.ValueOf(config)
  40. var verify_value reflect.Value = reflect.ValueOf(verify)
  41. for _, field := range fields {
  42. var name string = field.Name
  43. var got string
  44. var expect string
  45. got = verify_value.FieldByName(name).String()
  46. expect = config_value.FieldByName(name).String()
  47. if got != expect {
  48. t.Errorf("For %s Got %s, Expected %s", name, got, expect)
  49. }
  50. }
  51. }
  52. func TestParse(t *testing.T) {
  53. var Msg IRCMsg
  54. var Expect map[string]IRCMsg = map[string]IRCMsg{
  55. "PING :78629A0F5F3F164F": IRCMsg{
  56. Cmd: "PING", Msg: "78629A0F5F3F164F", MsgParts: []string{"PING"}},
  57. ":irc.red-green.com 001 test :Welcome to the RedGreen IRC Network": IRCMsg{
  58. From: "irc.red-green.com", Cmd: "001", To: "test",
  59. Msg: "Welcome to the RedGreen IRC Network",
  60. MsgParts: []string{":irc.red-green.com", "001", "test"}},
  61. ":irc.red-green.com 375 test :- irc.red-green.com Message of the Day -": IRCMsg{
  62. From: "irc.red-green.com", Cmd: "375", To: "test",
  63. Msg: "- irc.red-green.com Message of the Day -",
  64. MsgParts: []string{":irc.red-green.com", "375", "test"}},
  65. ":irc.red-green.com 433 :Nick already in use": IRCMsg{
  66. From: "irc.red-green.com", Cmd: "433",
  67. Msg: "Nick already in use",
  68. MsgParts: []string{":irc.red-green.com", "433"}},
  69. ":NickServ!services@services.red-green.com NOTICE test :Password accepted - you are now recognized.": IRCMsg{
  70. From: "NickServ", Cmd: "NOTICE", To: "test",
  71. Msg: "Password accepted - you are now recognized.",
  72. MsgParts: []string{":NickServ!services@services.red-green.com",
  73. "NOTICE", "test"}},
  74. ":irc.red-green.com 005 meow-bot HCN INVEX KICKLEN=307 KNOCK MAP MAXCHANNELS=10 MAXLIST=b:60,e:60,I:60 MAXNICKLEN=30 MINNICKLEN=0 MODES=12 NAMESX NETWORK=RedGreen :are supported by this server": IRCMsg{
  75. From: "irc.red-green.com", Cmd: "005", To: "meow-bot",
  76. Msg: "are supported by this server",
  77. MsgParts: []string{":irc.red-green.com", "005", "meow-bot", "HCN", "INVEX",
  78. "KICKLEN=307", "KNOCK", "MAP", "MAXCHANNELS=10", "MAXLIST=b:60,e:60,I:60",
  79. "MAXNICKLEN=30", "MINNICKLEN=0", "MODES=12", "NAMESX", "NETWORK=RedGreen"}},
  80. ":irc.red-green.com 353 meow-bot = #backstage :meow-bot performer5 performer4 performer3 performer1 performer2 Stage_Manager Apollo ~bugz": IRCMsg{
  81. From: "irc.red-green.com", Cmd: "353", To: "meow-bot",
  82. Msg: "meow-bot performer5 performer4 performer3 performer1 performer2 Stage_Manager Apollo ~bugz",
  83. MsgParts: []string{":irc.red-green.com", "353", "meow-bot", "=", "#backstage"}},
  84. }
  85. for line, msgout := range Expect {
  86. Msg = IRCParse(line)
  87. var fields []string = []string{"To", "From", "Cmd", "Msg"}
  88. msg_value := reflect.ValueOf(Msg)
  89. msgout_value := reflect.ValueOf(msgout)
  90. for _, field := range fields {
  91. var got string
  92. var expect string
  93. got = msg_value.FieldByName(field).String()
  94. expect = msgout_value.FieldByName(field).String()
  95. if got != expect {
  96. t.Errorf("%s %s: got %s, expected %s", line, field, got, expect)
  97. }
  98. }
  99. /*
  100. if Msg.To != msgout.To {
  101. t.Errorf("%s To: got %s, expected %s", line, Msg.To, msgout.To)
  102. }
  103. if Msg.From != msgout.From {
  104. t.Errorf("%s From: got %s, expected %s", line, Msg.From, msgout.From)
  105. }
  106. if Msg.Cmd != msgout.Cmd {
  107. t.Errorf("%s Cmd: got %s, expected %s", line, Msg.Cmd, msgout.Cmd)
  108. }
  109. if Msg.Msg != msgout.Msg {
  110. t.Errorf("%s Msg: got %s, expected %s", line, Msg.Msg, msgout.Msg)
  111. }
  112. */
  113. if len(Msg.MsgParts) != len(msgout.MsgParts) {
  114. t.Errorf("%s MsgParts got len %d, expected len %d", line, len(Msg.MsgParts), len(msgout.MsgParts))
  115. // Length didn't match, don't need to compare each part.
  116. } else {
  117. // Length matches, compare each part
  118. for idx := range Msg.MsgParts {
  119. if Msg.MsgParts[idx] != msgout.MsgParts[idx] {
  120. t.Errorf("%s MsgParts[%d] %s, expected %s", line, idx, Msg.MsgParts[idx], msgout.MsgParts[idx])
  121. }
  122. }
  123. }
  124. }
  125. }
  126. func TestConnect(t *testing.T) {
  127. var config IRCConfig = IRCConfig{Nick: "test",
  128. Username: "test",
  129. Realname: "testing",
  130. Password: "12345",
  131. ServerPassword: "allow"}
  132. var listen net.Listener
  133. var address string
  134. listen, address = setupSocket()
  135. var parts []string = strings.Split(address, ":")
  136. config.Hostname = parts[0]
  137. config.Port, _ = strconv.Atoi(parts[1])
  138. go ircServer(listen, t, &config)
  139. var FromIRC chan IRCMsg = make(chan IRCMsg)
  140. config.ReadChannel = FromIRC
  141. config.Connect()
  142. defer config.Close()
  143. var Msg IRCMsg
  144. var motd, identify, support bool
  145. var isupport map[string]string = map[string]string{
  146. "AWAYLEN": "307", "BOT": "B", "CASEMAPPING": "ascii",
  147. "CHANLIMIT": "#:10", "CHANMODES": "beI,kLf,lH,psmntirzMQNRTOVKDdGPZSCc",
  148. "CHANNELLEN": "32", "CHANTYPES": "#", "CLIENTTAGDENY": "*,-draft/typing,-typing",
  149. "DEAF": "d", "ELIST": "MNUCT", "EXCEPTS": "", "EXTBAN": "~,GptmTSOcarnqjf",
  150. "HCN": "", "INVEX": "", "KICKLEN": "307", "KNOCK": "", "MAP": "", "MAXCHANNELS": "10",
  151. "MAXLIST": "b:60,e:60,I:60", "MAXNICKLEN": "30", "MINNICKLEN": "0", "MODES": "12",
  152. "NAMESX": "", "NETWORK": "RedGreen", "NICKLEN": "30", "PREFIX": "(qaohv)~&@%+",
  153. "QUITLEN": "307", "SAFELIST": "", "SILENCE": "15", "STATUSMSG": "~&@%+",
  154. "TARGMAX": "DCCALLOW:,ISON:,JOIN:,KICK:4,KILL:,LIST:,NAMES:1,NOTICE:1,PART:,PRIVMSG:4,SAJOIN:,SAPART:,TAGMSG:1,USERHOST:,USERIP:,WATCH:,WHOIS:1,WHOWAS:1",
  155. "TOPICLEN": "360", "UHNAMES": "", "USERIP": "", "WALLCHOPS": "", "WATCH": "128",
  156. "WATCHOPTS": "A", "WHOX": "",
  157. }
  158. for Msg = range FromIRC {
  159. if Msg.Cmd == "EndMOTD" {
  160. t.Log("Got EndMOTD")
  161. motd = true
  162. support = true
  163. // Verify we parsed 005 ISupport:
  164. for key, value := range isupport {
  165. var got string
  166. var has bool
  167. got, has = config.ISupport[key]
  168. if !has {
  169. t.Errorf("Missing ISUPPORT[%s] expected (%s)", key, value)
  170. support = false
  171. } else {
  172. if got != value {
  173. t.Errorf("ISUPPORT[%s] got %s, expected %s", key, got, value)
  174. support = false
  175. }
  176. }
  177. }
  178. }
  179. if Msg.Cmd == "Identified" {
  180. t.Log("Identified")
  181. identify = true
  182. }
  183. }
  184. if !motd {
  185. t.Error("Missing EndMOTD")
  186. }
  187. if !identify {
  188. t.Error("Missing Identified")
  189. }
  190. if !support {
  191. t.Error("Missing ISupport")
  192. }
  193. if config.MyNick != config.Nick {
  194. t.Errorf("Got %s, Expected %s", config.MyNick, config.Nick)
  195. }
  196. }
  197. func TestConnectNickInUse(t *testing.T) {
  198. var config IRCConfig = IRCConfig{Nick: "bad",
  199. Username: "test",
  200. Realname: "testing",
  201. Flood_Num: 1,
  202. Flood_Delay: 20,
  203. }
  204. var listen net.Listener
  205. var address string
  206. listen, address = setupSocket()
  207. var parts []string = strings.Split(address, ":")
  208. config.Hostname = parts[0]
  209. config.Port, _ = strconv.Atoi(parts[1])
  210. go ircServer(listen, t, &config)
  211. var FromIRC chan IRCMsg = make(chan IRCMsg)
  212. config.ReadChannel = FromIRC
  213. config.Connect()
  214. defer config.Close()
  215. var Msg IRCMsg
  216. var motd, identify bool
  217. var missing int
  218. for Msg = range FromIRC {
  219. if Msg.Cmd == "EndMOTD" {
  220. t.Log("Got EndMOTD")
  221. motd = true
  222. config.WriteTo("missing", "PRIVMSG missing :Missing user")
  223. config.WriteTo("missing", "PRIVMSG missing :Missing user")
  224. config.WriteTo("missing", "PRIVMSG missing :Missing user")
  225. config.WriteTo("#missing", "PRIVMSG #missing :Missing channel")
  226. }
  227. if Msg.Cmd == "Identified" {
  228. t.Log("Identified")
  229. identify = true
  230. }
  231. if Msg.Cmd == "404" || Msg.Cmd == "401" {
  232. missing++
  233. }
  234. }
  235. if !motd {
  236. t.Error("Missing EndMOTD")
  237. }
  238. if identify {
  239. t.Error("Should not have been Identified")
  240. }
  241. if missing < 2 {
  242. t.Errorf("Missing should have been 2, was %d", missing)
  243. }
  244. if config.MyNick == config.Nick {
  245. t.Errorf("Nick should be different: Got %s, Didn't Expect %s", config.MyNick, config.Nick)
  246. }
  247. }
  248. func TestConnectTLS(t *testing.T) {
  249. var config IRCConfig = IRCConfig{Nick: "test",
  250. Username: "test",
  251. Realname: "testing",
  252. Password: "12345",
  253. UseTLS: true,
  254. UseSASL: true,
  255. Insecure: true,
  256. ServerPassword: "allow"}
  257. var listen net.Listener
  258. var address string
  259. listen, address = setupTLSSocket()
  260. var parts []string = strings.Split(address, ":")
  261. config.Hostname = parts[0]
  262. config.Port, _ = strconv.Atoi(parts[1])
  263. go ircServer(listen, t, &config)
  264. var FromIRC chan IRCMsg = make(chan IRCMsg)
  265. config.ReadChannel = FromIRC
  266. config.Connect()
  267. defer config.Close()
  268. var Msg IRCMsg
  269. var motd, identify bool
  270. for Msg = range FromIRC {
  271. if Msg.Cmd == "EndMOTD" {
  272. t.Log("Got EndMOTD")
  273. motd = true
  274. }
  275. if Msg.Cmd == "Identified" {
  276. t.Log("Identified")
  277. identify = true
  278. }
  279. }
  280. if !motd {
  281. t.Error("Missing EndMOTD")
  282. }
  283. if !identify {
  284. t.Error("Missing Identified")
  285. }
  286. if config.MyNick != config.Nick {
  287. t.Errorf("Got %s, Expected %s", config.MyNick, config.Nick)
  288. }
  289. }
  290. func TestConnectAutojoin(t *testing.T) {
  291. var config IRCConfig = IRCConfig{Nick: "test",
  292. Username: "test",
  293. Realname: "testing",
  294. Password: "12345",
  295. UseTLS: true,
  296. UseSASL: true,
  297. Insecure: true,
  298. AutoJoin: []string{"#chat", "#test"},
  299. Flood_Num: 2,
  300. Flood_Delay: 10,
  301. Version: "Test",
  302. }
  303. var listen net.Listener
  304. var address string
  305. listen, address = setupTLSSocket()
  306. var parts []string = strings.Split(address, ":")
  307. config.Hostname = parts[0]
  308. config.Port, _ = strconv.Atoi(parts[1])
  309. go ircServer(listen, t, &config)
  310. var FromIRC chan IRCMsg = make(chan IRCMsg)
  311. config.ReadChannel = FromIRC
  312. config.Connect()
  313. defer config.Close()
  314. var Msg IRCMsg
  315. var motd, identify bool
  316. var joins int
  317. var expect string
  318. var ctcpExpect []string = []string{"VERSION",
  319. "TIME",
  320. "PING 12345",
  321. }
  322. var noticeExpect []string = []string{"Testing",
  323. fmt.Sprintf("VERSION %s red-green.com/irc-client", config.Version),
  324. "TIME ",
  325. "PING 12345",
  326. }
  327. for Msg = range FromIRC {
  328. /*
  329. if (Msg.Cmd == "ACTION") || (Msg.Cmd == "NOTICE") {
  330. t.Log(Msg)
  331. }
  332. */
  333. if Msg.Cmd == "EndMOTD" {
  334. t.Log("Got EndMOTD")
  335. motd = true
  336. }
  337. if Msg.Cmd == "Identified" {
  338. t.Log("Identified")
  339. identify = true
  340. }
  341. if Msg.Cmd == "JOIN" {
  342. joins++
  343. if joins == 2 {
  344. // messages set to echo are returned to us
  345. config.WriteTo("echo", "PRIVMSG echo :\x01VERSION\x01")
  346. config.WriteTo("echo", "PRIVMSG echo :\x01TIME\x01")
  347. config.WriteTo("echo", "PRIVMSG echo :\x01PING 12345\x01")
  348. config.Action("echo", "dances.")
  349. config.Notice("echo", "Testing")
  350. config.Msg("#test", "Message 1")
  351. config.Msg("#test", "Message 2")
  352. config.PriorityWrite("PRIVMSG #test :Message")
  353. }
  354. }
  355. if Msg.Cmd == "CTCP" {
  356. expect = ctcpExpect[0]
  357. ctcpExpect = ctcpExpect[1:]
  358. if Msg.Msg != expect {
  359. t.Errorf("CTCP Got %s, Expected %s", Msg.Msg, expect)
  360. }
  361. }
  362. if Msg.Cmd == "NOTICE" {
  363. expect = noticeExpect[0]
  364. if expect != "Testing" {
  365. expect = "\x01" + expect
  366. }
  367. noticeExpect = noticeExpect[1:]
  368. if !strings.HasPrefix(Msg.Msg, expect) {
  369. t.Errorf("NOTICE Got [%s], Expected [%s]", Msg.Msg, expect)
  370. }
  371. }
  372. if Msg.Cmd == "ACTION" {
  373. expect = "dances."
  374. if Msg.Msg != expect {
  375. t.Errorf("ACTION Got %s, Expected %s", Msg.Msg, expect)
  376. }
  377. }
  378. }
  379. if joins != 2 {
  380. t.Errorf("Expected to autojoin 2 channels, got %d", joins)
  381. }
  382. if !motd {
  383. t.Error("Missing EndMOTD")
  384. }
  385. if !identify {
  386. t.Error("Missing Identified")
  387. }
  388. if len(noticeExpect) != 0 {
  389. t.Errorf("Expected more NOTICEs (%d)", len(noticeExpect))
  390. }
  391. if len(ctcpExpect) != 0 {
  392. t.Errorf("Expected more CTCPs (%d)", len(ctcpExpect))
  393. }
  394. if config.MyNick != config.Nick {
  395. t.Errorf("Got %s, Expected %s", config.MyNick, config.Nick)
  396. }
  397. }
  398. func TestConnectKick(t *testing.T) {
  399. var config IRCConfig = IRCConfig{Nick: "test",
  400. Username: "test",
  401. Realname: "testing",
  402. Password: "12345",
  403. UseTLS: true,
  404. UseSASL: true,
  405. Insecure: true,
  406. AutoJoin: []string{"#chat", "#test", "#kick"},
  407. RejoinDelay: 1,
  408. Flood_Num: 2,
  409. Flood_Delay: 10,
  410. }
  411. var listen net.Listener
  412. var address string
  413. listen, address = setupTLSSocket()
  414. var parts []string = strings.Split(address, ":")
  415. config.Hostname = parts[0]
  416. config.Port, _ = strconv.Atoi(parts[1])
  417. // Save and Restore abortAfter
  418. var abortPrev = abortAfter
  419. defer func() { abortAfter = abortPrev }()
  420. abortAfter = 500
  421. go ircServer(listen, t, &config)
  422. var FromIRC chan IRCMsg = make(chan IRCMsg)
  423. config.ReadChannel = FromIRC
  424. config.Connect()
  425. defer config.Close()
  426. var Msg IRCMsg
  427. var motd, identify bool
  428. var joins int
  429. for Msg = range FromIRC {
  430. if Msg.Cmd == "EndMOTD" {
  431. t.Log("Got EndMOTD")
  432. motd = true
  433. }
  434. if Msg.Cmd == "Identified" {
  435. t.Log("Identified")
  436. identify = true
  437. }
  438. if Msg.Cmd == "JOIN" {
  439. joins++
  440. if joins == 4 {
  441. config.PriorityWrite("NICK something")
  442. }
  443. }
  444. }
  445. // 3 channels joined, 1 kick, +1 join=4.
  446. if joins != 4 {
  447. t.Errorf("Expected to join 4 times, got %d", joins)
  448. }
  449. if !motd {
  450. t.Error("Missing EndMOTD")
  451. }
  452. if !identify {
  453. t.Error("Missing Identified")
  454. }
  455. if config.MyNick != "something" {
  456. t.Errorf("Expected nick to be something, not %s", config.MyNick)
  457. }
  458. }
  459. func TestConnectSignal(t *testing.T) {
  460. var exit bool
  461. var onexit func() = func() { exit = true }
  462. var config IRCConfig = IRCConfig{Nick: "test",
  463. Username: "test",
  464. Realname: "testing",
  465. Password: "12345",
  466. UseTLS: true,
  467. UseSASL: true,
  468. Insecure: true,
  469. AutoJoin: []string{"#chat"},
  470. Flood_Num: 2,
  471. Flood_Delay: 10,
  472. OnExit: onexit,
  473. }
  474. var listen net.Listener
  475. var address string
  476. listen, address = setupTLSSocket()
  477. var parts []string = strings.Split(address, ":")
  478. config.Hostname = parts[0]
  479. config.Port, _ = strconv.Atoi(parts[1])
  480. go ircServer(listen, t, &config)
  481. var FromIRC chan IRCMsg = make(chan IRCMsg)
  482. config.ReadChannel = FromIRC
  483. config.Connect()
  484. // defer config.Close()
  485. var Msg IRCMsg
  486. var motd, identify bool
  487. for Msg = range FromIRC {
  488. if Msg.Cmd == "EndMOTD" {
  489. t.Log("Got EndMOTD")
  490. motd = true
  491. // config.PriorityWrite("NICK something")
  492. }
  493. if Msg.Cmd == "Identified" {
  494. t.Log("Identified")
  495. identify = true
  496. }
  497. if Msg.Cmd == "JOIN" {
  498. // Ok, we've joined. Test the signal
  499. e := syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
  500. t.Log("Kill:", e)
  501. }
  502. }
  503. if !motd {
  504. t.Error("Missing EndMOTD")
  505. }
  506. if !identify {
  507. t.Error("Missing Identified")
  508. }
  509. config.Close()
  510. if !exit {
  511. t.Error("AtExit wasn't called.")
  512. }
  513. }
  514. func TestMatch(t *testing.T) {
  515. var testdata map[string]string = map[string]string{"TEST": "test",
  516. "TEst": "teST", "[bugz]": "{BUGZ}"}
  517. for nick1, nick2 := range testdata {
  518. if !Match(nick1, nick2) {
  519. t.Errorf("Match %s [%s] != %s [%s] Failed match!", nick1, NameLower(nick1), nick2, NameLower(nick2))
  520. }
  521. }
  522. }