瀏覽代碼

Added Kick/rejoin test.

Steve Thielemann 2 年之前
父節點
當前提交
71757ae8a5
共有 1 個文件被更改,包括 70 次插入0 次删除
  1. 70 0
      client_test.go

+ 70 - 0
client_test.go

@@ -103,6 +103,9 @@ func ircWrite(server net.Conn, output string, t *testing.T) {
 var abortAfter int = 150 // Milliseconds to abort part 3
 
 // mock up an irc server
+// part 1 : nick, user
+// part 2 : identify to services (if not SASL/SASL failed)
+// part 3 : quit after abortAfter ms
 func ircServer(listener net.Listener, t *testing.T, config *IRCConfig) {
 	var server net.Conn
 	var err error
@@ -305,6 +308,7 @@ func ircServer(listener net.Listener, t *testing.T, config *IRCConfig) {
 	time.AfterFunc(time.Millisecond*time.Duration(abortAfter), func() { server.Close() })
 
 	t.Log("Ok, Identified...")
+	var Kicked bool
 
 	for {
 		line, err = reader.ReadString('\n')
@@ -322,6 +326,13 @@ func ircServer(listener net.Listener, t *testing.T, config *IRCConfig) {
 					ircWrite(server, output, t)
 					output = fmt.Sprintf(":irc.server 333 %s %s user %d", config.MyNick, channel, time.Now().Unix())
 					ircWrite(server, output, t)
+					if strings.Contains(channel, "kick") {
+						if !Kicked {
+							Kicked = true
+							output = fmt.Sprintf("user KICK %s %s :Get out", channel, config.MyNick)
+							ircWrite(server, output, t)
+						}
+					}
 				}
 			}
 			switch parts[0] {
@@ -629,3 +640,62 @@ func TestConnectAutojoin(t *testing.T) {
 	}
 
 }
+
+func TestConnectKick(t *testing.T) {
+	var config IRCConfig = IRCConfig{Nick: "test",
+		Username:    "test",
+		Realname:    "testing",
+		Password:    "12345",
+		UseTLS:      true,
+		UseSASL:     true,
+		Insecure:    true,
+		AutoJoin:    []string{"#chat", "#test", "#kick"},
+		RejoinDelay: 1,
+		Flood_Num:   2,
+		Flood_Delay: 10,
+	}
+	var listen net.Listener
+	var address string
+
+	listen, address = setupTLSSocket()
+	var parts []string = strings.Split(address, ":")
+
+	config.Hostname = parts[0]
+	config.Port, _ = strconv.Atoi(parts[1])
+	go ircServer(listen, t, &config)
+	var FromIRC chan IRCMsg
+
+	FromIRC = make(chan IRCMsg)
+	config.ReadChannel = FromIRC
+
+	config.Connect()
+	defer config.Close()
+
+	var Msg IRCMsg
+	var motd, identify bool
+	var joins int
+
+	for Msg = range FromIRC {
+		if Msg.Cmd == "EndMOTD" {
+			t.Log("Got EndMOTD")
+			motd = true
+		}
+		if Msg.Cmd == "Identified" {
+			t.Log("Identified")
+			identify = true
+		}
+		if Msg.Cmd == "JOIN" {
+			joins++
+		}
+	}
+
+	if joins != 4 {
+		t.Errorf("Expected to join 4 times, got %d", joins)
+	}
+	if !motd {
+		t.Error("Missing EndMOTD")
+	}
+	if !identify {
+		t.Error("Missing Identified")
+	}
+}