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.

client_test.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright (c) 2019 Shivaram Lingamneni
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "fmt"
  6. "testing"
  7. "github.com/ergochat/ergo/irc/languages"
  8. "github.com/ergochat/ergo/irc/utils"
  9. )
  10. func TestGenerateBatchID(t *testing.T) {
  11. var session Session
  12. s := make(utils.HashSet[string])
  13. count := 100000
  14. for i := 0; i < count; i++ {
  15. s.Add(session.generateBatchID())
  16. }
  17. if len(s) != count {
  18. t.Error("duplicate batch ID detected")
  19. }
  20. }
  21. func BenchmarkGenerateBatchID(b *testing.B) {
  22. var session Session
  23. for i := 0; i < b.N; i++ {
  24. session.generateBatchID()
  25. }
  26. }
  27. func BenchmarkNames(b *testing.B) {
  28. channelSize := 1024
  29. server := &Server{
  30. name: "ergo.test",
  31. }
  32. lm, err := languages.NewManager(false, "", "")
  33. if err != nil {
  34. b.Fatal(err)
  35. }
  36. server.config.Store(&Config{
  37. languageManager: lm,
  38. })
  39. for i := 0; i < b.N; i++ {
  40. channel := &Channel{
  41. name: "#test",
  42. nameCasefolded: "#test",
  43. server: server,
  44. members: make(MemberSet),
  45. }
  46. for j := 0; j < channelSize; j++ {
  47. nick := fmt.Sprintf("client_%d", j)
  48. client := &Client{
  49. server: server,
  50. nick: nick,
  51. nickCasefolded: nick,
  52. }
  53. channel.members.Add(client)
  54. channel.regenerateMembersCache()
  55. session := &Session{
  56. client: client,
  57. }
  58. rb := NewResponseBuffer(session)
  59. channel.Names(client, rb)
  60. if len(rb.messages) < 2 {
  61. b.Fatalf("not enough messages: %d", len(rb.messages))
  62. }
  63. // to inspect the messages: line, _ := rb.messages[0].Line()
  64. }
  65. }
  66. }
  67. func TestUserMasks(t *testing.T) {
  68. var um UserMaskSet
  69. if um.Match("horse_!user@tor-network.onion") {
  70. t.Error("bad match")
  71. }
  72. um.Add("_!*@*", "x", "x")
  73. if !um.Match("_!user@tor-network.onion") {
  74. t.Error("failure to match")
  75. }
  76. if um.Match("horse_!user@tor-network.onion") {
  77. t.Error("bad match")
  78. }
  79. um.Add("beer*!*@*", "x", "x")
  80. if !um.Match("beergarden!user@tor-network.onion") {
  81. t.Error("failure to match")
  82. }
  83. if um.Match("horse_!user@tor-network.onion") {
  84. t.Error("bad match")
  85. }
  86. um.Add("horse*!user@*", "x", "x")
  87. if !um.Match("horse_!user@tor-network.onion") {
  88. t.Error("failure to match")
  89. }
  90. }
  91. func TestWhoFields(t *testing.T) {
  92. var w whoxFields
  93. if w.Has('a') {
  94. t.Error("zero value of whoxFields must be empty")
  95. }
  96. w = w.Add('a')
  97. if !w.Has('a') {
  98. t.Error("failed to set and get")
  99. }
  100. if w.Has('A') {
  101. t.Error("false positive")
  102. }
  103. if w.Has('o') {
  104. t.Error("false positive")
  105. }
  106. w = w.Add('🐬')
  107. if w.Has('🐬') {
  108. t.Error("should not be able to set invalid who field")
  109. }
  110. w = w.Add('o')
  111. if !w.Has('o') {
  112. t.Error("failed to set and get")
  113. }
  114. w = w.Add('z')
  115. if !w.Has('z') {
  116. t.Error("failed to set and get")
  117. }
  118. }