From 7206812e266df8d57cd6628e22b9049b6517ebb8 Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Thu, 27 Mar 2014 12:28:20 +0100 Subject: Forbid empty nicknames --- chat/chat_test.go | 4 ++++ chat/rooms.go | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/chat/chat_test.go b/chat/chat_test.go index 03cc948..c92b8e7 100644 --- a/chat/chat_test.go +++ b/chat/chat_test.go @@ -18,6 +18,10 @@ func TestJoining(t *testing.T) { t.Fatalf("Got error \"%s\", expected \"%s\"", err, NickAlreadyInUse) } + if _, _, err = Join("test", ""); err != EmptyNick { + t.Fatalf("Got error \"%s\", expected \"%s\"", err, EmptyNick) + } + b2, r2, err := Join("test", "Bar") if err != nil { t.Fatalf("Could not join room a second time: %s", err) diff --git a/chat/rooms.go b/chat/rooms.go index 1991d77..793f027 100644 --- a/chat/rooms.go +++ b/chat/rooms.go @@ -7,6 +7,7 @@ import ( var ( NickAlreadyInUse = errors.New("Nickname is already in use") RoomIsFull = errors.New("Room is full") + EmptyNick = errors.New("Nickname must not be empty") ) // Room represents a chatroom. @@ -31,6 +32,7 @@ func (r *Room) leave(nick string) { } delete(r.buddies, nick) + if len(r.buddies) == 0 { close(r.messages) delete(rooms, r.name) @@ -81,6 +83,10 @@ func Join(room, nick string) (*Buddy, *Room, error) { return nil, nil, NickAlreadyInUse } + if nick == "" { + return nil, nil, EmptyNick + } + if len(r.buddies) >= perroom { return nil, nil, RoomIsFull } -- cgit v1.2.3-70-g09d2