client_test.go 16 KB

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