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.

453 lines
10 KiB

7 years ago
  1. package list_test
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strings"
  6. "testing"
  7. "git.aiterp.net/gisle/irc/isupport"
  8. "git.aiterp.net/gisle/irc/list"
  9. )
  10. var testISupport isupport.ISupport
  11. func TestList(t *testing.T) {
  12. table := []struct {
  13. namestoken string
  14. shouldInsert bool
  15. user list.User
  16. order []string
  17. }{
  18. {
  19. "@+Test!~test@example.com", true,
  20. list.User{
  21. Nick: "Test",
  22. User: "~test",
  23. Host: "example.com",
  24. Modes: "ov",
  25. Prefixes: "@+",
  26. PrefixedNick: "@Test",
  27. },
  28. []string{"@Test"},
  29. },
  30. {
  31. "+@Test2!~test2@example.com", true,
  32. list.User{
  33. Nick: "Test2",
  34. User: "~test2",
  35. Host: "example.com",
  36. Modes: "ov",
  37. Prefixes: "@+",
  38. PrefixedNick: "@Test2",
  39. },
  40. []string{"@Test", "@Test2"},
  41. },
  42. {
  43. "+Gissleh", true,
  44. list.User{
  45. Nick: "Gissleh",
  46. User: "",
  47. Host: "",
  48. Modes: "v",
  49. Prefixes: "+",
  50. PrefixedNick: "+Gissleh",
  51. },
  52. []string{"@Test", "@Test2", "+Gissleh"},
  53. },
  54. {
  55. "Guest!~guest@10.72.3.15", true,
  56. list.User{
  57. Nick: "Guest",
  58. User: "~guest",
  59. Host: "10.72.3.15",
  60. Modes: "",
  61. Prefixes: "",
  62. PrefixedNick: "Guest",
  63. },
  64. []string{"@Test", "@Test2", "+Gissleh", "Guest"},
  65. },
  66. {
  67. "@AOP!actualIdent@10.32.8.174", true,
  68. list.User{
  69. Nick: "AOP",
  70. User: "actualIdent",
  71. Host: "10.32.8.174",
  72. Modes: "o",
  73. Prefixes: "@",
  74. PrefixedNick: "@AOP",
  75. },
  76. []string{"@AOP", "@Test", "@Test2", "+Gissleh", "Guest"},
  77. },
  78. {
  79. "@ZOP!actualIdent@10.32.8.174", true,
  80. list.User{
  81. Nick: "ZOP",
  82. User: "actualIdent",
  83. Host: "10.32.8.174",
  84. Modes: "o",
  85. Prefixes: "@",
  86. PrefixedNick: "@ZOP",
  87. },
  88. []string{"@AOP", "@Test", "@Test2", "@ZOP", "+Gissleh", "Guest"},
  89. },
  90. {
  91. "+ZVoice!~zv@10.32.8.174", true,
  92. list.User{
  93. Nick: "ZVoice",
  94. User: "~zv",
  95. Host: "10.32.8.174",
  96. Modes: "v",
  97. Prefixes: "+",
  98. PrefixedNick: "+ZVoice",
  99. },
  100. []string{"@AOP", "@Test", "@Test2", "@ZOP", "+Gissleh", "+ZVoice", "Guest"},
  101. },
  102. {
  103. "+ZVoice!~zv@10.32.8.174", false,
  104. list.User{},
  105. []string{"@AOP", "@Test", "@Test2", "@ZOP", "+Gissleh", "+ZVoice", "Guest"},
  106. },
  107. }
  108. list := list.New(&testISupport)
  109. for _, row := range table {
  110. t.Run("Insert_"+row.namestoken, func(t *testing.T) {
  111. ok := list.InsertFromNamesToken(row.namestoken)
  112. if ok && !row.shouldInsert {
  113. t.Error("Insert should have failed!")
  114. return
  115. }
  116. if !ok && row.shouldInsert {
  117. t.Error("Insert should NOT have failed!")
  118. return
  119. }
  120. if row.shouldInsert {
  121. user, ok := list.User(row.user.Nick)
  122. if !ok {
  123. t.Error("Could not find user.")
  124. return
  125. }
  126. jsonA, _ := json.MarshalIndent(user, "", " ")
  127. jsonB, _ := json.MarshalIndent(row.user, "", " ")
  128. t.Log("result =", string(jsonA))
  129. if string(jsonA) != string(jsonB) {
  130. t.Log("expectation =", string(jsonB))
  131. t.Error("Users did not match!")
  132. }
  133. }
  134. order := make([]string, 0, 16)
  135. for _, user := range list.Users() {
  136. order = append(order, user.PrefixedNick)
  137. }
  138. orderA := strings.Join(order, ", ")
  139. orderB := strings.Join(row.order, ", ")
  140. t.Log("order =", orderA)
  141. if orderA != orderB {
  142. t.Log("orderExpected =", orderB)
  143. t.Error("Order did not match!")
  144. }
  145. })
  146. }
  147. modeTable := []struct {
  148. add bool
  149. mode rune
  150. nick string
  151. ok bool
  152. order []string
  153. }{
  154. {
  155. true, 'o', "Gissleh", true,
  156. []string{"@AOP", "@Gissleh", "@Test", "@Test2", "@ZOP", "+ZVoice", "Guest"},
  157. },
  158. {
  159. false, 'o', "Gissleh", true,
  160. []string{"@AOP", "@Test", "@Test2", "@ZOP", "+Gissleh", "+ZVoice", "Guest"},
  161. },
  162. {
  163. true, 'o', "InvalidNick", false,
  164. []string{"@AOP", "@Test", "@Test2", "@ZOP", "+Gissleh", "+ZVoice", "Guest"},
  165. },
  166. {
  167. true, 'v', "AOP", true,
  168. []string{"@AOP", "@Test", "@Test2", "@ZOP", "+Gissleh", "+ZVoice", "Guest"},
  169. },
  170. {
  171. true, 'v', "ZOP", true,
  172. []string{"@AOP", "@Test", "@Test2", "@ZOP", "+Gissleh", "+ZVoice", "Guest"},
  173. },
  174. {
  175. true, 'v', "Guest", true,
  176. []string{"@AOP", "@Test", "@Test2", "@ZOP", "+Gissleh", "+Guest", "+ZVoice"},
  177. },
  178. {
  179. true, 'v', "Test", true,
  180. []string{"@AOP", "@Test", "@Test2", "@ZOP", "+Gissleh", "+Guest", "+ZVoice"},
  181. },
  182. {
  183. false, 'v', "Test", true,
  184. []string{"@AOP", "@Test", "@Test2", "@ZOP", "+Gissleh", "+Guest", "+ZVoice"},
  185. },
  186. {
  187. false, 'o', "Test", true,
  188. []string{"@AOP", "@Test2", "@ZOP", "+Gissleh", "+Guest", "+ZVoice", "Test"},
  189. },
  190. {
  191. false, 'o', "AOP", true,
  192. []string{"@Test2", "@ZOP", "+AOP", "+Gissleh", "+Guest", "+ZVoice", "Test"},
  193. },
  194. {
  195. true, 'x', "AOP", false,
  196. []string{"@Test2", "@ZOP", "+AOP", "+Gissleh", "+Guest", "+ZVoice", "Test"},
  197. },
  198. {
  199. false, 'x', "ZOP", false,
  200. []string{"@Test2", "@ZOP", "+AOP", "+Gissleh", "+Guest", "+ZVoice", "Test"},
  201. },
  202. {
  203. true, 'o', "UNKNOWN_USER", false,
  204. []string{"@Test2", "@ZOP", "+AOP", "+Gissleh", "+Guest", "+ZVoice", "Test"},
  205. },
  206. {
  207. false, 'o', "UNKNOWN_USER", false,
  208. []string{"@Test2", "@ZOP", "+AOP", "+Gissleh", "+Guest", "+ZVoice", "Test"},
  209. },
  210. }
  211. for i, row := range modeTable {
  212. t.Run(fmt.Sprintf("Mode_%d_%s", i, row.nick), func(t *testing.T) {
  213. var ok bool
  214. if row.add {
  215. ok = list.AddMode(row.nick, row.mode)
  216. } else {
  217. ok = list.RemoveMode(row.nick, row.mode)
  218. }
  219. if ok && !row.ok {
  220. t.Error("This should be not ok, but it is ok.")
  221. }
  222. if !ok && row.ok {
  223. t.Error("This is not ok.")
  224. }
  225. order := make([]string, 0, 16)
  226. for _, user := range list.Users() {
  227. order = append(order, user.PrefixedNick)
  228. }
  229. orderA := strings.Join(order, ", ")
  230. orderB := strings.Join(row.order, ", ")
  231. t.Log("order =", orderA)
  232. if orderA != orderB {
  233. t.Log("orderExpected =", orderB)
  234. t.Error("Order did not match!")
  235. }
  236. })
  237. }
  238. renameTable := []struct {
  239. from string
  240. to string
  241. ok bool
  242. order []string
  243. }{
  244. {
  245. "ZOP", "AAOP", true,
  246. []string{"@AAOP", "@Test2", "+AOP", "+Gissleh", "+Guest", "+ZVoice", "Test"},
  247. },
  248. {
  249. "Test", "ATest", true,
  250. []string{"@AAOP", "@Test2", "+AOP", "+Gissleh", "+Guest", "+ZVoice", "ATest"},
  251. },
  252. {
  253. "AOP", "ZOP", true,
  254. []string{"@AAOP", "@Test2", "+Gissleh", "+Guest", "+ZOP", "+ZVoice", "ATest"},
  255. },
  256. {
  257. "AOP", "ZOP", false,
  258. []string{"@AAOP", "@Test2", "+Gissleh", "+Guest", "+ZOP", "+ZVoice", "ATest"},
  259. },
  260. {
  261. "ATest", "Test", true,
  262. []string{"@AAOP", "@Test2", "+Gissleh", "+Guest", "+ZOP", "+ZVoice", "Test"},
  263. },
  264. {
  265. "Test2", "AAATest", true,
  266. []string{"@AAATest", "@AAOP", "+Gissleh", "+Guest", "+ZOP", "+ZVoice", "Test"},
  267. },
  268. {
  269. "ZOP", "AAATest", false,
  270. []string{"@AAATest", "@AAOP", "+Gissleh", "+Guest", "+ZOP", "+ZVoice", "Test"},
  271. },
  272. {
  273. "AAATest", "AAATest", true,
  274. []string{"@AAATest", "@AAOP", "+Gissleh", "+Guest", "+ZOP", "+ZVoice", "Test"},
  275. },
  276. }
  277. for i, row := range renameTable {
  278. t.Run(fmt.Sprintf("Rename_%d_%s_%s", i, row.from, row.to), func(t *testing.T) {
  279. ok := list.Rename(row.from, row.to)
  280. if ok && !row.ok {
  281. t.Error("This should be not ok, but it is ok.")
  282. }
  283. if !ok && row.ok {
  284. t.Error("This is not ok.")
  285. }
  286. order := make([]string, 0, 16)
  287. for _, user := range list.Users() {
  288. order = append(order, user.PrefixedNick)
  289. }
  290. orderA := strings.Join(order, ", ")
  291. orderB := strings.Join(row.order, ", ")
  292. t.Log("order =", orderA)
  293. if orderA != orderB {
  294. t.Log("orderExpected =", orderB)
  295. t.Error("Order did not match!")
  296. }
  297. })
  298. }
  299. removeTable := []struct {
  300. nick string
  301. ok bool
  302. order []string
  303. }{
  304. {
  305. "AAOP", true,
  306. []string{"@AAATest", "+Gissleh", "+Guest", "+ZOP", "+ZVoice", "Test"},
  307. },
  308. {
  309. "AAOP", false,
  310. []string{"@AAATest", "+Gissleh", "+Guest", "+ZOP", "+ZVoice", "Test"},
  311. },
  312. {
  313. "Guest", true,
  314. []string{"@AAATest", "+Gissleh", "+ZOP", "+ZVoice", "Test"},
  315. },
  316. {
  317. "ZOP", true,
  318. []string{"@AAATest", "+Gissleh", "+ZVoice", "Test"},
  319. },
  320. {
  321. "ATest", false,
  322. []string{"@AAATest", "+Gissleh", "+ZVoice", "Test"},
  323. },
  324. {
  325. "Test", true,
  326. []string{"@AAATest", "+Gissleh", "+ZVoice"},
  327. },
  328. }
  329. for i, row := range removeTable {
  330. t.Run(fmt.Sprintf("Rename_%d_%s", i, row.nick), func(t *testing.T) {
  331. ok := list.Remove(row.nick)
  332. if ok && !row.ok {
  333. t.Error("This should be not ok, but it is ok.")
  334. }
  335. if !ok && row.ok {
  336. t.Error("This is not ok.")
  337. }
  338. order := make([]string, 0, 16)
  339. for _, user := range list.Users() {
  340. order = append(order, user.PrefixedNick)
  341. }
  342. if _, ok := list.User(row.nick); ok {
  343. t.Error("User is still there")
  344. }
  345. orderA := strings.Join(order, ", ")
  346. orderB := strings.Join(row.order, ", ")
  347. t.Log("order =", orderA)
  348. if orderA != orderB {
  349. t.Log("orderExpected =", orderB)
  350. t.Error("Order did not match!")
  351. }
  352. })
  353. }
  354. t.Run("AutoSort", func(t *testing.T) {
  355. list.SetAutoSort(false)
  356. if ok := list.InsertFromNamesToken("@+AAAAAAAAA"); !ok {
  357. t.Error("Failed to insert user @+AAAAAAAAA")
  358. }
  359. users := list.Users()
  360. last := users[len(users)-1]
  361. if last.PrefixedNick != "@AAAAAAAAA" {
  362. t.Error("@+AAAAAAAAA isn't last, "+last.PrefixedNick, "is.")
  363. }
  364. list.SetAutoSort(true)
  365. users = list.Users()
  366. last = users[len(users)-1]
  367. if last.PrefixedNick == "@AAAAAAAAA" {
  368. t.Error("@+AAAAAAAAA is still last after autosort was enabled. That's not right.")
  369. }
  370. })
  371. t.Run("Clear", func(t *testing.T) {
  372. list.Clear()
  373. if len(list.Users()) != 0 {
  374. t.Error("Clear failed!")
  375. }
  376. })
  377. }
  378. func init() {
  379. isupportData := map[string]string{
  380. "FNC": "",
  381. "SAFELIST": "",
  382. "ELIST": "CTU",
  383. "MONITOR": "100",
  384. "WHOX": "",
  385. "ETRACE": "",
  386. "KNOCK": "",
  387. "CHANTYPES": "#&",
  388. "EXCEPTS": "",
  389. "INVEX": "",
  390. "CHANMODES": "eIbq,k,flj,CFLNPQcgimnprstz",
  391. "CHANLIMIT": "#&:15",
  392. "PREFIX": "(ov)@+",
  393. "MAXLIST": "bqeI:100",
  394. "MODES": "4",
  395. "NETWORK": "TestServer",
  396. "STATUSMSG": "@+",
  397. "CALLERID": "g",
  398. "CASEMAPPING": "rfc1459",
  399. "NICKLEN": "30",
  400. "MAXNICKLEN": "31",
  401. "CHANNELLEN": "50",
  402. "TOPICLEN": "390",
  403. "DEAF": "D",
  404. "TARGMAX": "NAMES:1,LIST:1,KICK:1,WHOIS:1,PRIVMSG:4,NOTICE:4,ACCEPT:,MONITOR:",
  405. "EXTBAN": "$,&acjmorsuxz|",
  406. "CLIENTVER": "3.0",
  407. }
  408. for key, value := range isupportData {
  409. testISupport.Set(key, value)
  410. }
  411. }