2017-04-24 15:55:52 +00:00
|
|
|
// Copyright (c) Liam Stanley <me@liamstanley.io>. All rights reserved. Use
|
|
|
|
// of this source code is governed by the MIT license that can be found in
|
|
|
|
// the LICENSE file.
|
|
|
|
|
|
|
|
package girc
|
|
|
|
|
|
|
|
import (
|
2017-07-19 00:12:53 +00:00
|
|
|
"strings"
|
2017-04-24 15:55:52 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDisableTracking(t *testing.T) {
|
|
|
|
client := New(Config{
|
|
|
|
Server: "dummy.int",
|
|
|
|
Port: 6667,
|
|
|
|
Nick: "test",
|
|
|
|
User: "test",
|
|
|
|
Name: "Testing123",
|
|
|
|
})
|
|
|
|
|
|
|
|
if len(client.Handlers.internal) < 1 {
|
|
|
|
t.Fatal("Client.Handlers empty, though just initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
client.DisableTracking()
|
|
|
|
if _, ok := client.Handlers.internal[CAP]; ok {
|
|
|
|
t.Fatal("Client.Handlers contains capability tracking handlers, though disabled")
|
|
|
|
}
|
|
|
|
|
2017-07-12 04:44:44 +00:00
|
|
|
client.state.Lock()
|
|
|
|
defer client.state.Unlock()
|
2017-04-24 15:55:52 +00:00
|
|
|
|
|
|
|
if client.state.channels != nil {
|
|
|
|
t.Fatal("Client.DisableTracking() called but channel state still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-19 00:12:53 +00:00
|
|
|
func TestConfigValid(t *testing.T) {
|
|
|
|
conf := Config{
|
|
|
|
Server: "irc.example.com", Port: 6667,
|
|
|
|
Nick: "test", User: "test", Name: "Realname",
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if err = conf.isValid(); err != nil {
|
|
|
|
t.Fatalf("valid config failed Config.isValid() with: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.Server = ""
|
|
|
|
if err = conf.isValid(); err == nil {
|
|
|
|
t.Fatalf("invalid server passed validation check: %s", err)
|
|
|
|
}
|
|
|
|
conf.Server = "irc.example.com"
|
|
|
|
|
|
|
|
conf.Port = 100000
|
|
|
|
if err = conf.isValid(); err == nil {
|
|
|
|
t.Fatalf("invalid port passed validation check: %s", err)
|
|
|
|
}
|
|
|
|
conf.Port = 0 // Assumes "default".
|
|
|
|
if err = conf.isValid(); err != nil {
|
|
|
|
t.Fatalf("valid default failed validation check: %s", err)
|
|
|
|
}
|
|
|
|
if conf.Port != 6667 {
|
|
|
|
t.Fatal("irc port was not defaulted to 6667")
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.Nick = "invalid nick"
|
|
|
|
if err = conf.isValid(); err == nil {
|
|
|
|
t.Fatalf("invalid nick passed validation check: %s", err)
|
|
|
|
}
|
|
|
|
conf.User = "test"
|
|
|
|
|
|
|
|
conf.User = "invalid user"
|
|
|
|
if err = conf.isValid(); err == nil {
|
|
|
|
t.Fatalf("invalid user passed validation check: %s", err)
|
|
|
|
}
|
|
|
|
conf.User = "test"
|
|
|
|
}
|
|
|
|
|
2017-04-24 15:55:52 +00:00
|
|
|
func TestClientLifetime(t *testing.T) {
|
|
|
|
client := New(Config{
|
|
|
|
Server: "dummy.int",
|
|
|
|
Port: 6667,
|
|
|
|
Nick: "test",
|
|
|
|
User: "test",
|
|
|
|
Name: "Testing123",
|
|
|
|
})
|
|
|
|
|
|
|
|
tm := client.Lifetime()
|
|
|
|
|
2017-07-19 00:12:53 +00:00
|
|
|
if tm < 0 || tm > 2*time.Second {
|
2017-04-24 15:55:52 +00:00
|
|
|
t.Fatalf("Client.Lifetime() = %q, out of bounds", tm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientUptime(t *testing.T) {
|
|
|
|
c, conn, server := genMockConn()
|
|
|
|
defer conn.Close()
|
|
|
|
defer server.Close()
|
2017-08-19 11:55:18 +00:00
|
|
|
go mockReadBuffer(conn)
|
2017-04-24 15:55:52 +00:00
|
|
|
|
2017-08-19 11:55:18 +00:00
|
|
|
done := make(chan struct{}, 1)
|
|
|
|
c.Handlers.Add(INITIALIZED, func(c *Client, e Event) { close(done) })
|
2017-07-19 00:12:53 +00:00
|
|
|
|
2017-04-24 15:55:52 +00:00
|
|
|
go c.MockConnect(server)
|
2017-05-07 16:39:16 +00:00
|
|
|
defer c.Close()
|
2017-04-24 15:55:52 +00:00
|
|
|
|
2017-07-19 00:12:53 +00:00
|
|
|
select {
|
2017-08-19 11:55:18 +00:00
|
|
|
case <-done:
|
2017-07-19 00:12:53 +00:00
|
|
|
case <-time.After(2 * time.Second):
|
|
|
|
t.Fatal("Client.Uptime() timed out")
|
|
|
|
}
|
2017-04-24 15:55:52 +00:00
|
|
|
|
|
|
|
uptime, err := c.Uptime()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Client.Uptime() = %s, wanted time", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
since := time.Since(*uptime)
|
|
|
|
connsince, err := c.ConnSince()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Client.ConnSince() = %s, wanted time", err)
|
|
|
|
}
|
|
|
|
|
2017-07-19 00:12:53 +00:00
|
|
|
if since < 0 || since > 4*time.Second || *connsince < 0 || *connsince > 4*time.Second {
|
2017-04-24 15:55:52 +00:00
|
|
|
t.Fatalf("Client.Uptime() = %q (%q, connsince: %q), out of bounds", uptime, since, connsince)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the time we got from Client.Uptime() and Client.ConnSince() are
|
|
|
|
// within reach of eachother.
|
|
|
|
|
2017-07-19 00:12:53 +00:00
|
|
|
if *connsince-since > 2*time.Second {
|
2017-04-24 15:55:52 +00:00
|
|
|
t.Fatalf("Client.Uptime() (diff) = %q, Client.ConnSince() = %q, differ too much", since, connsince)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.IsConnected() {
|
|
|
|
t.Fatal("Client.IsConnected() = false, though mock should be true")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientGet(t *testing.T) {
|
|
|
|
c, conn, server := genMockConn()
|
|
|
|
defer conn.Close()
|
|
|
|
defer server.Close()
|
2017-08-19 11:55:18 +00:00
|
|
|
go mockReadBuffer(conn)
|
2017-04-24 15:55:52 +00:00
|
|
|
|
2017-08-19 11:55:18 +00:00
|
|
|
done := make(chan struct{}, 1)
|
|
|
|
c.Handlers.Add(INITIALIZED, func(c *Client, e Event) { close(done) })
|
2017-07-19 00:12:53 +00:00
|
|
|
|
2017-04-24 15:55:52 +00:00
|
|
|
go c.MockConnect(server)
|
2017-05-07 16:39:16 +00:00
|
|
|
defer c.Close()
|
2017-04-24 15:55:52 +00:00
|
|
|
|
2017-07-19 00:12:53 +00:00
|
|
|
select {
|
2017-08-19 11:55:18 +00:00
|
|
|
case <-done:
|
2017-07-19 00:12:53 +00:00
|
|
|
case <-time.After(2 * time.Second):
|
|
|
|
t.Fatal("timed out during connect")
|
|
|
|
}
|
2017-04-24 15:55:52 +00:00
|
|
|
|
|
|
|
if nick := c.GetNick(); nick != c.Config.Nick {
|
|
|
|
t.Fatalf("Client.GetNick() = %q though should be %q", nick, c.Config.Nick)
|
|
|
|
}
|
|
|
|
|
|
|
|
if user := c.GetIdent(); user != c.Config.User {
|
|
|
|
t.Fatalf("Client.GetIdent() = %q though should be %q", user, c.Config.User)
|
|
|
|
}
|
2017-07-19 00:12:53 +00:00
|
|
|
|
|
|
|
if !strings.Contains(c.String(), "connected:true") {
|
|
|
|
t.Fatalf("Client.String() == %q, doesn't contain 'connected:true'", c.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientClose(t *testing.T) {
|
|
|
|
c, conn, server := genMockConn()
|
|
|
|
defer server.Close()
|
2017-07-19 04:22:15 +00:00
|
|
|
defer conn.Close()
|
2017-08-19 11:55:18 +00:00
|
|
|
go mockReadBuffer(conn)
|
2017-07-19 00:12:53 +00:00
|
|
|
|
|
|
|
errchan := make(chan error, 1)
|
2017-08-19 11:55:18 +00:00
|
|
|
done := make(chan struct{}, 1)
|
2017-07-19 00:12:53 +00:00
|
|
|
|
2017-08-19 11:55:18 +00:00
|
|
|
c.Handlers.AddBg(STOPPED, func(c *Client, e Event) { close(done) })
|
|
|
|
c.Handlers.AddBg(INITIALIZED, func(c *Client, e Event) { c.Close() })
|
2017-07-19 00:12:53 +00:00
|
|
|
|
2017-08-19 11:55:18 +00:00
|
|
|
go func() { errchan <- c.MockConnect(server) }()
|
2017-07-19 00:12:53 +00:00
|
|
|
|
2017-08-19 11:55:18 +00:00
|
|
|
defer c.Close()
|
2017-07-19 00:12:53 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-errchan:
|
2017-08-19 11:55:18 +00:00
|
|
|
if err == nil {
|
|
|
|
break
|
2017-07-19 00:12:53 +00:00
|
|
|
}
|
2017-08-19 11:55:18 +00:00
|
|
|
|
|
|
|
t.Fatalf("connect returned with error when close was invoked: %s", err)
|
2017-07-19 04:22:15 +00:00
|
|
|
case <-time.After(5 * time.Second):
|
2017-08-19 11:55:18 +00:00
|
|
|
t.Fatal("Client.Close() timed out")
|
|
|
|
case <-done:
|
2017-07-19 00:12:53 +00:00
|
|
|
}
|
|
|
|
}
|