summaryrefslogtreecommitdiff
path: root/room_test.go
blob: 2e9549b2ccb4179d80400e1daf0a492d18a0fdf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
goircd -- minimalistic simple Internet Relay Chat (IRC) server
Copyright (C) 2014-2018 | wn Sergey Matveev <stargrave@stargrave.org>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package main

import (
	"strings"
	"testing"
)

func noNickchan(t *testing.T, c *TestingConn) {
	if r := <-c.outbound; !strings.HasPrefix(r, ":foohost 401") {
		t.Fatal("no nick/channel", r)
	}
}

func noChan(t *testing.T, c *TestingConn) {
	if r := <-c.outbound; !strings.HasPrefix(r, ":foohost 403") {
		t.Fatal("no channel", r)
	}
}

func notEnoughParams(t *testing.T, c *TestingConn) {
	if r := <-c.outbound; !strings.HasPrefix(r, ":foohost 461") {
		t.Fatal("not enough params", r)
	}
}

func TestTwoUsers(t *testing.T) {
	logSink = make(chan LogEvent, 8)
	stateSink = make(chan StateEvent, 8)
	host := "foohost"
	hostname = &host
	events := make(chan ClientEvent)
	roomsM.Lock()
	rooms = make(map[string]*Room)
	roomSinks = make(map[*Room]chan ClientEvent)
	roomsM.Unlock()
	clients = make(map[*Client]struct{})
	finished := make(chan struct{})
	go Processor(events, finished)
	defer func() {
		events <- ClientEvent{eventType: EventTerm}
		<-finished
	}()

	conn1 := NewTestingConn()
	conn2 := NewTestingConn()
	client1 := NewClient(conn1)
	client2 := NewClient(conn2)
	go client1.Processor(events)
	go client2.Processor(events)

	conn1.inbound <- "NICK nick1\r\nUSER foo1 bar1 baz1 :Long name1"
	conn2.inbound <- "NICK nick2\r\nUSER foo2 bar2 baz2 :Long name2"
	for i := 0; i < 6; i++ {
		<-conn1.outbound
		<-conn2.outbound
	}

	SendLusers(client1)
	if r := <-conn1.outbound; !strings.Contains(r, "There are 2 users") {
		t.Fatal("LUSERS", r)
	}

	conn1.inbound <- "WHOIS"
	notEnoughParams(t, conn1)
	conn1.inbound <- "WHOIS nick3"
	noNickchan(t, conn1)
	conn1.inbound <- "WHOIS nick2"
	if r := <-conn1.outbound; r != ":foohost 311 nick1 nick2 foo2 Unknown * :Long name2\r\n" {
		t.Fatal("first WHOIS 311", r)
	}
	if r := <-conn1.outbound; r != ":foohost 312 nick1 nick2 foohost :foohost\r\n" {
		t.Fatal("first WHOIS 312", r)
	}
	if r := <-conn1.outbound; r != ":foohost 319 nick1 nick2 :\r\n" {
		t.Fatal("first WHOIS 319", r)
	}
	if r := <-conn1.outbound; r != ":foohost 318 nick1 nick2 :End of /WHOIS list\r\n" {
		t.Fatal("first WHOIS 318", r)
	}

	conn1.inbound <- "LIST"
	if r := <-conn1.outbound; r != ":foohost 323 nick1 :End of /LIST\r\n" {
		t.Fatal("first LIST", r)
	}

	conn1.inbound <- "WHO"
	notEnoughParams(t, conn1)
	conn1.inbound <- "WHO #fooroom"
	noChan(t, conn1)

	conn1.inbound <- "JOIN #foo"
	conn2.inbound <- "JOIN #foo"
	for i := 0; i < 4; i++ {
		<-conn1.outbound
		<-conn2.outbound
	}
	conn1.inbound <- "PRIVMSG nick2 :Hello"
	conn1.inbound <- "PRIVMSG #foo :world"
	conn1.inbound <- "NOTICE #foo :world"
	m1 := <-conn2.outbound
	m2 := <-conn2.outbound
	mNeeded := ":nick1!foo1@someclient PRIVMSG nick2 :Hello\r\n"
	if !(m1 == mNeeded || m2 == mNeeded) {
		t.Fatal("first message", m1, m2)
	}
	if m2 == mNeeded {
		m2 = <-conn2.outbound
	}
	if m2 != ":nick1!foo1@someclient PRIVMSG #foo :world\r\n" {
		t.Fatal("second message", m2)
	}
	if m2 = <-conn2.outbound; m2 != ":nick1!foo1@someclient NOTICE #foo :world\r\n" {
		t.Fatal("third message", m2)
	}
}

func TestJoin(t *testing.T) {
	logSink = make(chan LogEvent, 8)
	stateSink = make(chan StateEvent, 8)
	host := "foohost"
	hostname = &host
	events := make(chan ClientEvent)
	rooms = make(map[string]*Room)
	clients = make(map[*Client]struct{})
	roomSinks = make(map[*Room]chan ClientEvent)
	finished := make(chan struct{})
	go Processor(events, finished)
	defer func() {
		events <- ClientEvent{eventType: EventTerm}
		<-finished
	}()
	conn := NewTestingConn()
	client := NewClient(conn)
	go client.Processor(events)

	conn.inbound <- "NICK nick2\r\nUSER foo2 bar2 baz2 :Long name2"
	for i := 0; i < 6; i++ {
		<-conn.outbound
	}

	conn.inbound <- "JOIN"
	notEnoughParams(t, conn)
	conn.inbound <- "JOIN bla/bla/bla"
	noChan(t, conn)
	conn.inbound <- "JOIN bla:bla:bla"
	noChan(t, conn)

	conn.inbound <- "JOIN #foo"
	if r := <-conn.outbound; r != ":foohost 331 nick2 #foo :No topic is set\r\n" {
		t.Fatal("no topic is set", r)
	}
	if r := <-conn.outbound; r != ":nick2!foo2@someclient JOIN #foo\r\n" {
		t.Fatal("no JOIN message", r)
	}
	if r := <-conn.outbound; r != ":foohost 353 nick2 = #foo :nick2\r\n" {
		t.Fatal("no NAMES list", r)
	}
	if r := <-conn.outbound; r != ":foohost 366 nick2 #foo :End of NAMES list\r\n" {
		t.Fatal("no end of NAMES list", r)
	}
	if r := <-logSink; (r.what != "joined") || (r.where != "#foo") || (r.who != "nick2") || (r.meta != true) {
		t.Fatal("invalid join log event", r)
	}

	conn.inbound <- "JOIN #bar,#baz"
	for i := 0; i < 4*2; i++ {
		<-conn.outbound
	}
	roomsM.RLock()
	if _, ok := rooms["#bar"]; !ok {
		t.Fatal("#bar does not exist")
	}
	if _, ok := rooms["#baz"]; !ok {
		t.Fatal("#baz does not exist")
	}
	roomsM.RUnlock()
	if r := <-logSink; (r.what != "joined") || (r.where != "#bar") || (r.who != "nick2") || (r.meta != true) {
		t.Fatal("invalid join log event #bar", r)
	}
	if r := <-logSink; (r.what != "joined") || (r.where != "#baz") || (r.who != "nick2") || (r.meta != true) {
		t.Fatal("invalid join log event #baz", r)
	}

	conn.inbound <- "JOIN #barenc,#bazenc key1,key2"
	for i := 0; i < 4*2; i++ {
		<-conn.outbound
	}
	roomsM.RLock()
	if *rooms["#barenc"].key != "key1" {
		t.Fatal("no room with key1")
	}
	if *rooms["#bazenc"].key != "key2" {
		t.Fatal("no room with key2")
	}
	roomsM.RUnlock()
	if r := <-logSink; (r.what != "joined") || (r.where != "#barenc") || (r.who != "nick2") || (r.meta != true) {
		t.Fatal("invalid join log event #barenc", r)
	}
	if r := <-logSink; (r.what != "joined") || (r.where != "#bazenc") || (r.who != "nick2") || (r.meta != true) {
		t.Fatal("invalid join log event #bazenc", r)
	}
	if r := <-stateSink; (r.topic != "") || (r.where != "#barenc") || (r.key != "key1") {
		t.Fatal("set channel key1 state", r)
	}
	if r := <-stateSink; (r.topic != "") || (r.where != "#bazenc") || (r.key != "key2") {
		t.Fatal("set channel key2 state", r)
	}

	conn.inbound <- "MODE #barenc -k"
	if r := <-conn.outbound; r != ":nick2!foo2@someclient MODE #barenc -k\r\n" {
		t.Fatal("remove #barenc key", r)
	}
	roomsM.RLock()
	if *rooms["#barenc"].key != "" {
		t.Fatal("removing key from #barenc")
	}
	roomsM.RUnlock()
	if r := <-logSink; (r.what != "removed channel key") || (r.where != "#barenc") || (r.who != "nick2") || (r.meta != true) {
		t.Fatal("removed channel key log", r)
	}
	if r := <-stateSink; (r.topic != "") || (r.where != "#barenc") || (r.key != "") {
		t.Fatal("removed channel key state", r)
	}

	conn.inbound <- "PART #bazenc\r\nMODE #bazenc -k"
	if r := <-conn.outbound; r != ":foohost 442 #bazenc :You are not on that channel\r\n" {
		t.Fatal("not on that channel", r)
	}
	if r := <-logSink; (r.what != "left") || (r.where != "#bazenc") || (r.who != "nick2") || (r.meta != true) {
		t.Fatal("left #bazenc log", r)
	}

	conn.inbound <- "MODE #barenc +i"
	if r := <-conn.outbound; r != ":foohost 472 nick2 +i :Unknown MODE flag\r\n" {
		t.Fatal("unknown MODE flag", r)
	}

	conn.inbound <- "MODE #barenc +k newkey"
	if r := <-conn.outbound; r != ":nick2!foo2@someclient MODE #barenc +k newkey\r\n" {
		t.Fatal("+k MODE setting", r)
	}
	if r := <-logSink; (r.what != "set channel key to newkey") || (r.where != "#barenc") || (r.who != "nick2") || (r.meta != true) {
		t.Fatal("set channel key", r)
	}
	if r := <-stateSink; (r.topic != "") || (r.where != "#barenc") || (r.key != "newkey") {
		t.Fatal("set channel newkey state", r)
	}

	conn.inbound <- "TOPIC #barenc :New topic"
	if r := <-conn.outbound; r != ":nick2!foo2@someclient TOPIC #barenc :New topic\r\n" {
		t.Fatal("set TOPIC", r)
	}
	if r := <-logSink; (r.what != "set topic to New topic") || (r.where != "#barenc") || (r.who != "nick2") || (r.meta != true) {
		t.Fatal("set TOPIC log", r)
	}
	if r := <-stateSink; (r.topic != "New topic") || (r.where != "#barenc") || (r.key != "newkey") {
		t.Fatal("set channel TOPIC state", r)
	}

	conn.inbound <- "WHO #barenc"
	if r := <-conn.outbound; r != ":foohost 352 nick2 #barenc foo2 someclient foohost nick2 H :0 Long name2\r\n" {
		t.Fatal("WHO", r)
	}
	if r := <-conn.outbound; r != ":foohost 315 nick2 #barenc :End of /WHO list\r\n" {
		t.Fatal("end of WHO", r)
	}
}