1// Copyright (c) 2019 Shivaram Lingamneni
2// released under the MIT license
3
4package irc
5
6import (
7	"testing"
8
9	"github.com/ergochat/ergo/irc/utils"
10)
11
12func TestGenerateBatchID(t *testing.T) {
13	var session Session
14	s := make(utils.StringSet)
15
16	count := 100000
17	for i := 0; i < count; i++ {
18		s.Add(session.generateBatchID())
19	}
20
21	if len(s) != count {
22		t.Error("duplicate batch ID detected")
23	}
24}
25
26func BenchmarkGenerateBatchID(b *testing.B) {
27	var session Session
28	for i := 0; i < b.N; i++ {
29		session.generateBatchID()
30	}
31}
32
33func TestUserMasks(t *testing.T) {
34	var um UserMaskSet
35
36	if um.Match("horse_!user@tor-network.onion") {
37		t.Error("bad match")
38	}
39
40	um.Add("_!*@*", "x", "x")
41	if !um.Match("_!user@tor-network.onion") {
42		t.Error("failure to match")
43	}
44	if um.Match("horse_!user@tor-network.onion") {
45		t.Error("bad match")
46	}
47
48	um.Add("beer*!*@*", "x", "x")
49	if !um.Match("beergarden!user@tor-network.onion") {
50		t.Error("failure to match")
51	}
52	if um.Match("horse_!user@tor-network.onion") {
53		t.Error("bad match")
54	}
55
56	um.Add("horse*!user@*", "x", "x")
57	if !um.Match("horse_!user@tor-network.onion") {
58		t.Error("failure to match")
59	}
60}
61
62func TestWhoFields(t *testing.T) {
63	var w whoxFields
64
65	if w.Has('a') {
66		t.Error("zero value of whoxFields must be empty")
67	}
68	w = w.Add('a')
69	if !w.Has('a') {
70		t.Error("failed to set and get")
71	}
72	if w.Has('A') {
73		t.Error("false positive")
74	}
75	if w.Has('o') {
76		t.Error("false positive")
77	}
78	w = w.Add('��')
79	if w.Has('��') {
80		t.Error("should not be able to set invalid who field")
81	}
82	w = w.Add('o')
83	if !w.Has('o') {
84		t.Error("failed to set and get")
85	}
86	w = w.Add('z')
87	if !w.Has('z') {
88		t.Error("failed to set and get")
89	}
90}
91