forked from 42wim/mm-go-irckit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
193 lines (164 loc) · 6.28 KB
/
server_test.go
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
package irckit
import (
"regexp"
"testing"
"time"
"github.com/sorcix/irc"
)
const testServerName = "testserver"
var expectTimeout = time.Second * 1
func expectReply(t *testing.T, conn *mockConn, expect string) {
select {
case msg := <-conn.send:
if !regexp.MustCompile(expect).MatchString(msg.String()) {
t.Errorf("\ngot\t\t%q\nwant\t%q", msg, expect)
}
case <-time.After(expectTimeout):
t.Fatalf("timed out waiting for %q", expect)
}
}
func expectEvent(t *testing.T, events <-chan Event, expect EventKind) Event {
select {
case evt := <-events:
if evt.Kind() != expect {
t.Errorf("got %q; expected %s", evt, expect)
}
return evt
case <-time.After(expectTimeout):
t.Fatalf("timed out waiting for %q", expect)
}
return nil
}
func TestServerWelcome(t *testing.T) {
events := make(chan Event, 10)
srv := NewServer(testServerName)
srv.Subscribe(events)
defer srv.Close()
send, receive := make(chan *irc.Message, 10), make(chan *irc.Message, 10)
u := NewUserMock(send, receive)
go srv.Connect(u)
receive <- irc.ParseMessage("NICK foo")
receive <- irc.ParseMessage("USER root 0 * :Foo Bar")
if msg := <-send; msg.Command != irc.RPL_WELCOME {
t.Errorf("got %v; want %v", msg, irc.RPL_WELCOME)
}
expectEvent(t, events, ConnectEvent)
}
func TestServerMultiUser(t *testing.T) {
events := make(chan Event, 10)
srv := ServerConfig{
Name: testServerName,
Motd: []string{"I serve, therefore I am."},
}.Server()
srv.Subscribe(events)
defer srv.Close()
c1 := NewConnMock("client1", 10)
c2 := NewConnMock("client2", 10)
go srv.Connect(NewUser(c1))
go srv.Connect(NewUser(c2))
c1.receive <- irc.ParseMessage("NICK foo")
c1.receive <- irc.ParseMessage("USER root 0 * :Foo Bar")
expectEvent(t, events, ConnectEvent)
c2.receive <- irc.ParseMessage("NICK baz")
c2.receive <- irc.ParseMessage("USER root 0 * :Baz Quux")
expectEvent(t, events, ConnectEvent)
expectReply(t, c1, ":testserver 001 foo :Welcome! foo!root@client1")
expectReply(t, c1, ":testserver 002 foo :Your host is .*")
expectReply(t, c1, ":testserver 003 foo :This server was created .*")
expectReply(t, c1, ":testserver 004 foo :.*")
expectReply(t, c1, ":testserver 251 foo :There are 1 users and 0 services on 1 server.")
expectReply(t, c1, ":testserver 375 foo :- testserver Message of the Day -")
expectReply(t, c1, ":testserver 372 foo :- I serve, therefore I am.")
expectReply(t, c1, ":testserver 376 foo :End of /MOTD command.")
expectReply(t, c2, ":testserver 001 baz :Welcome! baz!root@client2")
expectReply(t, c2, ":testserver 002 baz :Your host is .*")
expectReply(t, c2, ":testserver 003 baz :This server was created .*")
expectReply(t, c2, ":testserver 004 baz :.*")
expectReply(t, c2, ":testserver 251 baz :There are 2 users and 0 services on 1 server.")
expectReply(t, c2, ":testserver 375 baz :- testserver Message of the Day -")
expectReply(t, c2, ":testserver 372 baz :- I serve, therefore I am.")
expectReply(t, c2, ":testserver 376 baz :End of /MOTD command.")
c1.receive <- irc.ParseMessage("JOIN #chat")
expectReply(t, c1, ":foo!root@client1 JOIN #chat")
expectReply(t, c1, ":testserver 353 foo = #chat :foo")
expectReply(t, c1, ":testserver 366 foo #chat :End of /NAMES list.")
expectEvent(t, events, NewChanEvent)
expectEvent(t, events, JoinEvent)
channel := srv.Channel("#chat")
if channel.Len() != 1 {
t.Errorf("expected #chat to be len 1; got: %v", channel.Users())
}
channel.Topic(srv, "so topical")
expectReply(t, c1, ":testserver TOPIC #chat :so topical")
c2.receive <- irc.ParseMessage("JOIN #chat")
expectReply(t, c2, ":baz!root@client2 JOIN #chat")
expectReply(t, c2, ":testserver 332 baz #chat :so topical")
expectReply(t, c2, ":testserver 353 baz = #chat :baz foo")
expectReply(t, c2, ":testserver 366 baz #chat :End of /NAMES list.")
expectEvent(t, events, JoinEvent)
// c1 notification of c2
expectReply(t, c1, ":baz!root@client2 JOIN #chat")
u1, ok := srv.HasUser("foo")
if !ok {
t.Fatal("server did not recognize user with nick: foo")
}
if len(u1.Channels()) != 1 {
t.Errorf("expected 1 channel for foo; got: %v", u1.Channels())
}
if channel.Len() != 2 {
t.Errorf("expected #chat to be len 2; got: %v", channel.Users())
}
users := u1.VisibleTo()
if len(users) != 1 {
t.Fatalf("expected foo to be visible to 1 user; got: %v", users)
}
if users[0].Nick != "baz" {
t.Errorf("expected foo to be visible to baz; got: %v", users[0])
}
c1.receive <- irc.ParseMessage("NICK foo_")
expectReply(t, c1, ":foo!root@client1 NICK foo_")
expectReply(t, c2, ":foo!root@client1 NICK foo_")
c2.receive <- irc.ParseMessage("PRIVMSG #chat :hello")
expectReply(t, c1, ":baz!root@client2 PRIVMSG #chat :hello")
expectEvent(t, events, ChanMsgEvent)
// Note: baz doesn't get an echo back here
c1.receive <- irc.ParseMessage("PRIVMSG baz :sup?")
expectReply(t, c2, ":foo_!root@client1 PRIVMSG baz :sup?")
expectEvent(t, events, UserMsgEvent)
c1.receive <- irc.ParseMessage("PART #chat")
expectReply(t, c1, ":foo_!root@client1 PART #chat")
expectReply(t, c2, ":foo_!root@client1 PART #chat")
expectEvent(t, events, PartEvent)
if len(u1.Channels()) != 0 {
t.Errorf("expected 0 channel for foo; got: %v", u1.Channels())
}
if channel.Len() != 1 {
t.Errorf("expected #chat to be len 1; got: %v", channel.Users())
}
c2.receive <- irc.ParseMessage("JOIN #blah")
expectReply(t, c2, ":baz!root@client2 JOIN #blah")
expectReply(t, c2, ":testserver 353 baz = #blah :baz")
expectReply(t, c2, ":testserver 366 baz #blah :End of /NAMES list.")
expectEvent(t, events, NewChanEvent)
expectEvent(t, events, JoinEvent)
u2, _ := srv.HasUser("baz")
channel2 := srv.Channel("#blah")
if len(u2.Channels()) != 2 {
t.Errorf("expected 2 channels for baz; got: %v", u2.Channels())
}
if channel2.Len() != 1 {
t.Errorf("expected #chat to be len 1; got: %v", channel2.Users())
}
c2.receive <- irc.ParseMessage("WHO #blah")
expectReply(t, c2, ":testserver 352 baz #blah root client2 \\* baz H :0 Baz Quux")
expectReply(t, c2, ":testserver 315 baz #blah :End of /WHO list.")
c2.receive <- irc.ParseMessage("PART #blah")
expectReply(t, c2, ":baz!root@client2 PART #blah")
expectEvent(t, events, PartEvent)
if len(u2.Channels()) != 1 {
t.Errorf("expected 1 channel for baz; got: %v", u2.Channels())
}
if channel2.Len() != 0 {
t.Errorf("expected #chat to be len 1; got: %v", channel2.Users())
}
}