2015-02-10 13:38:21 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-02-11 12:19:25 -07:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2015-02-11 13:32:17 -07:00
|
|
|
"strings"
|
2015-02-10 13:38:21 -07:00
|
|
|
"testing"
|
2015-02-11 12:19:25 -07:00
|
|
|
"time"
|
2015-02-10 13:38:21 -07:00
|
|
|
)
|
|
|
|
|
2015-02-11 12:19:25 -07:00
|
|
|
func writeFile(fn string, data string) {
|
|
|
|
ioutil.WriteFile(fn, []byte(data), os.ModePerm)
|
|
|
|
}
|
|
|
|
|
2015-02-11 15:54:23 -07:00
|
|
|
func createNetwork (t *testing.T, parent string) (base string, current string) {
|
|
|
|
base, err := ioutil.TempDir(parent, "spongy-test")
|
2015-02-11 12:19:25 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-02-11 13:32:17 -07:00
|
|
|
writeFile(path.Join(base, "nick"), "SpongyTest")
|
2015-02-11 12:19:25 -07:00
|
|
|
writeFile(path.Join(base, "server"), "moo.slashnet.org:6697")
|
2015-02-11 13:32:17 -07:00
|
|
|
writeFile(path.Join(base, "channels"), "#SpongyTest")
|
2015-02-11 12:19:25 -07:00
|
|
|
os.Mkdir(path.Join(base, "outq"), os.ModePerm)
|
|
|
|
os.Mkdir(path.Join(base, "log"), os.ModePerm)
|
|
|
|
|
2015-02-11 13:32:17 -07:00
|
|
|
current = path.Join(base, "log", "current")
|
|
|
|
|
2015-02-11 12:19:25 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-11 13:32:17 -07:00
|
|
|
func expect(t *testing.T, fpath string, needle string) {
|
|
|
|
for i := 0; i < 8; i += 1 {
|
|
|
|
if i > 0 {
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
fpBytes, err := ioutil.ReadFile(fpath)
|
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fpString := string(fpBytes)
|
|
|
|
if strings.Contains(fpString, needle) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Errorf("Could not find %#v in %s", needle, fpath)
|
|
|
|
}
|
|
|
|
|
2015-02-11 12:19:25 -07:00
|
|
|
func TestCreateNetwork(t *testing.T) {
|
2015-02-11 15:54:23 -07:00
|
|
|
base, _ := createNetwork(t, "")
|
2015-02-11 12:19:25 -07:00
|
|
|
|
|
|
|
if fi, err := os.Stat(path.Join(base, "nick")); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else if fi.IsDir() {
|
|
|
|
t.Error("%s is not a regular file", path.Join(base, "nick"))
|
|
|
|
}
|
|
|
|
|
|
|
|
os.RemoveAll(base)
|
|
|
|
if _, err := os.Stat(path.Join(base, "outq")); err == nil {
|
|
|
|
t.Error("Didn't unlink outq")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConnect(t *testing.T) {
|
2015-02-11 15:54:23 -07:00
|
|
|
base, current := createNetwork(t, "")
|
2015-02-11 12:19:25 -07:00
|
|
|
defer os.RemoveAll(base)
|
|
|
|
|
|
|
|
n := NewNetwork(base)
|
|
|
|
go n.Connect()
|
|
|
|
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
2015-02-11 13:32:17 -07:00
|
|
|
expect(t, current, " 001 ")
|
|
|
|
expect(t, current, " JOIN " + n.Nick + " #SpongyTest")
|
2015-02-11 12:19:25 -07:00
|
|
|
|
2015-02-11 13:40:53 -07:00
|
|
|
ioutil.WriteFile(path.Join(base, "outq", "merf"), []byte("PART #SpongyTest\n"), os.ModePerm)
|
|
|
|
expect(t, current, " PART ")
|
|
|
|
|
2015-02-11 12:19:25 -07:00
|
|
|
n.Close()
|
2015-02-10 13:38:21 -07:00
|
|
|
return
|
|
|
|
}
|