]> Cypherpunks.ru repositories - goircd.git/blobdiff - room_test.go
Unify copyright comment format
[goircd.git] / room_test.go
index 342e58ceecb121a162b2046b2b4065f50bdfd408..cbac7b76221b9350befc38161206ce89aca0c727 100644 (file)
@@ -1,20 +1,17 @@
-/*
-goircd -- minimalistic simple Internet Relay Chat (IRC) server
-Copyright (C) 2014-2015 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/>.
-*/
+// goircd -- minimalistic simple Internet Relay Chat (IRC) server
+// Copyright (C) 2014-2024 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, version 3 of the License.
+//
+// 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
 
@@ -47,9 +44,10 @@ func TestTwoUsers(t *testing.T) {
        host := "foohost"
        hostname = &host
        events := make(chan ClientEvent)
+       roomsLock.Lock()
        rooms = make(map[string]*Room)
+       roomsLock.Unlock()
        clients = make(map[*Client]struct{})
-       roomSinks = make(map[*Room]chan ClientEvent)
        finished := make(chan struct{})
        go Processor(events, finished)
        defer func() {
@@ -59,10 +57,8 @@ func TestTwoUsers(t *testing.T) {
 
        conn1 := NewTestingConn()
        conn2 := NewTestingConn()
-       client1 := NewClient(conn1)
-       client2 := NewClient(conn2)
-       go client1.Processor(events)
-       go client2.Processor(events)
+       client1 := NewClient(conn1, events)
+       NewClient(conn2, events)
 
        conn1.inbound <- "NICK nick1\r\nUSER foo1 bar1 baz1 :Long name1"
        conn2.inbound <- "NICK nick2\r\nUSER foo2 bar2 baz2 :Long name2"
@@ -71,7 +67,7 @@ func TestTwoUsers(t *testing.T) {
                <-conn2.outbound
        }
 
-       SendLusers(client1)
+       client1.SendLusers()
        if r := <-conn1.outbound; !strings.Contains(r, "There are 2 users") {
                t.Fatal("LUSERS", r)
        }
@@ -138,7 +134,6 @@ func TestJoin(t *testing.T) {
        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() {
@@ -146,8 +141,7 @@ func TestJoin(t *testing.T) {
                <-finished
        }()
        conn := NewTestingConn()
-       client := NewClient(conn)
-       go client.Processor(events)
+       NewClient(conn, events)
 
        conn.inbound <- "NICK nick2\r\nUSER foo2 bar2 baz2 :Long name2"
        for i := 0; i < 6; i++ {
@@ -182,12 +176,14 @@ func TestJoin(t *testing.T) {
        for i := 0; i < 4*2; i++ {
                <-conn.outbound
        }
+       roomsLock.RLock()
        if _, ok := rooms["#bar"]; !ok {
                t.Fatal("#bar does not exist")
        }
        if _, ok := rooms["#baz"]; !ok {
                t.Fatal("#baz does not exist")
        }
+       roomsLock.RUnlock()
        if r := <-logSink; (r.what != "joined") || (r.where != "#bar") || (r.who != "nick2") || (r.meta != true) {
                t.Fatal("invalid join log event #bar", r)
        }
@@ -199,12 +195,14 @@ func TestJoin(t *testing.T) {
        for i := 0; i < 4*2; i++ {
                <-conn.outbound
        }
-       if *rooms["#barenc"].key != "key1" {
+       roomsLock.RLock()
+       if rooms["#barenc"].key != "key1" {
                t.Fatal("no room with key1")
        }
-       if *rooms["#bazenc"].key != "key2" {
+       if rooms["#bazenc"].key != "key2" {
                t.Fatal("no room with key2")
        }
+       roomsLock.RUnlock()
        if r := <-logSink; (r.what != "joined") || (r.where != "#barenc") || (r.who != "nick2") || (r.meta != true) {
                t.Fatal("invalid join log event #barenc", r)
        }
@@ -222,9 +220,11 @@ func TestJoin(t *testing.T) {
        if r := <-conn.outbound; r != ":nick2!foo2@someclient MODE #barenc -k\r\n" {
                t.Fatal("remove #barenc key", r)
        }
-       if rooms["#barenc"].key != nil {
+       roomsLock.RLock()
+       if rooms["#barenc"].key != "" {
                t.Fatal("removing key from #barenc")
        }
+       roomsLock.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)
        }
@@ -233,6 +233,9 @@ func TestJoin(t *testing.T) {
        }
 
        conn.inbound <- "PART #bazenc\r\nMODE #bazenc -k"
+       if r := <-conn.outbound; r != ":nick2!foo2@someclient PART #bazenc :nick2\r\n" {
+               t.Fatal("part", r)
+       }
        if r := <-conn.outbound; r != ":foohost 442 #bazenc :You are not on that channel\r\n" {
                t.Fatal("not on that channel", r)
        }
@@ -249,7 +252,7 @@ func TestJoin(t *testing.T) {
        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) {
+       if r := <-logSink; (r.what != "set channel key") || (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") {