irc/irc_test.go

141 lines
3.7 KiB
Go
Raw Normal View History

2020-01-22 09:53:44 -05:00
package irc
import (
2021-10-27 21:10:18 -04:00
"fmt"
2020-01-22 09:53:44 -05:00
"log"
2021-10-27 21:25:41 -04:00
"strconv"
2021-10-27 21:10:18 -04:00
"strings"
2020-01-22 09:53:44 -05:00
"testing"
"time"
)
2021-10-27 19:51:18 -04:00
const (
2025-03-06 11:38:30 -05:00
channel = "#testing"
2021-10-27 19:51:18 -04:00
)
2021-10-27 20:09:03 -04:00
func TestJoinChannel(t *testing.T) {
2021-10-27 19:51:18 -04:00
con := NewConnection("irc.ouch.chat:6667", "irc-go", "irc-go", nil, []string{channel})
2020-01-22 09:53:44 -05:00
go con.Run()
2021-10-27 20:09:03 -04:00
time.Sleep(5 * time.Second)
2025-03-06 11:38:30 -05:00
t.Logf("%d users in channel %s\n", len(con.userList[channel]), channel)
con.SendPrivmsg(channel, fmt.Sprintf("\x0356FUCK ALL %d OF YOU BITCHES", len(con.userList[channel])))
i := 0
for _, v := range con.userList[channel] {
con.SendPrivmsg(channel, fmt.Sprintf("yo %s fuck you", v))
if i >= 5 {
time.Sleep(1000 * time.Millisecond)
i = 0
2021-10-27 19:51:18 -04:00
}
2025-03-06 11:38:30 -05:00
}
time.Sleep(1 * time.Second)
con.SendQuit("go-irc: Test Join Channel")
2020-01-22 09:53:44 -05:00
}
func TestJoinCallback(t *testing.T) {
con := NewConnection("irc.ouch.chat:6667", "irc-go", "irc-go", nil, []string{channel})
i := 0
con.JoinCallback = func(from, target string) {
i++
for _, x := range con.userList[channel] {
con.SendPrivmsg(channel, x)
}
if i == 3 {
con.SendQuit("")
}
}
go con.Run()
time.Sleep(10 * time.Second)
t1 := NewConnection("irc.ouch.chat:6667", "abc", "abc", nil, []string{channel})
t2 := NewConnection("irc.ouch.chat:6667", "def", "def", nil, []string{channel})
go t1.Run()
time.Sleep(5 * time.Second)
go t2.Run()
time.Sleep(10 * time.Second)
t1.SendQuit("")
t2.SendQuit("")
}
2021-10-27 20:09:03 -04:00
func TestLusers(t *testing.T) {
con := NewConnection("irc.ouch.chat:6667", "irc-go", "irc-go", nil, []string{channel})
con.NumericCallback = func(from string, i int, args string) {
if i == RPL_LUSERCLIENT {
2021-10-27 21:10:18 -04:00
con.SendJoin(channel)
s := strings.Split(args, ":")
if len(s) > 1 {
users := 0
invis := 0
servers := 0
fmt.Sscanf(s[1], "There are %d users and %d invisible on %d servers", &users, &invis, &servers)
con.SendPrivmsg(channel, fmt.Sprintf("total users on network: %d, invisible %d, servers %d", users, invis, servers))
}
2025-03-06 11:38:30 -05:00
con.SendQuit("go-irc: Test luser scan")
2021-10-27 20:09:03 -04:00
}
}
con.Run()
}
2021-10-27 21:25:41 -04:00
func TestLusersChannels(t *testing.T) {
con := NewConnection("irc.ouch.chat:6667", "irc-go", "irc-go", nil, []string{channel})
con.NumericCallback = func(from string, i int, args string) {
if i == RPL_LUSERCHANNELS {
con.SendJoin(channel)
s := strings.TrimSpace(strings.Split(args, " ")[1])
val, err := strconv.Atoi(s)
if err == nil {
con.SendPrivmsg(channel, fmt.Sprintf("There are %d channels formed", val))
} else {
t.Fatal(err.Error())
}
2025-03-06 11:38:30 -05:00
con.SendQuit("go-irc: test luser channels")
2021-10-27 21:25:41 -04:00
}
}
con.Run()
}
2021-10-27 21:49:24 -04:00
func TestGetServerInfo(t *testing.T) {
2021-10-27 21:34:05 -04:00
con := NewConnection("irc.ouch.chat:6667", "irc-go", "irc-go", nil, []string{channel})
con.NumericCallback = func(from string, i int, args string) {
if i == RPL_ENDOFMOTD {
2021-10-27 21:53:28 -04:00
go func() {
res := GetServerInfo("irc.libera.chat:6667", "irc-go4240")
con.SendPrivmsg(channel, fmt.Sprintf("Libera has %d users, %d channels, %d servers, %d invisible", res.Users, res.Channels, res.Servers, res.Invisible))
res = GetServerInfo("irc.choopa.net:6667", "irc-go4240")
con.SendPrivmsg(channel, fmt.Sprintf("EFNet has %d users, %d channels, %d servers, %d invisible", res.Users, res.Channels, res.Servers, res.Invisible))
2021-10-27 21:56:00 -04:00
res = GetServerInfo("irc.us.ircnet.net:6667", "irc-go4240")
con.SendPrivmsg(channel, fmt.Sprintf("IRCnet has %d users, %d channels, %d servers, %d invisible", res.Users, res.Channels, res.Servers, res.Invisible))
2025-03-06 11:38:30 -05:00
res = GetServerInfo("irc.letspiss.net:6667", "irc-go4240")
con.SendPrivmsg(channel, fmt.Sprintf("pissnet has %d users, %d channels, %d servers, %d invisible", res.Users, res.Channels, res.Servers, res.Invisible))
con.SendQuit("go-irc: test GetServerInfo")
2021-10-27 21:53:28 -04:00
}()
2021-10-27 21:34:05 -04:00
}
}
con.Run()
}
2020-01-22 09:53:44 -05:00
func TestParser(t *testing.T) {
log.Printf("testing %s\n", t.Name())
}