Browse Source

Testing via "echo" user.

The echo user returns whatever is sent to it.
Steve Thielemann 3 years ago
parent
commit
52f3727c7f
1 changed files with 95 additions and 2 deletions
  1. 95 2
      client_test.go

+ 95 - 2
client_test.go

@@ -291,7 +291,7 @@ func ircServer(listener net.Listener, t *testing.T, config *IRCConfig) {
 		t.Error("Expected to pass part2 (ns identify/+r)")
 	}
 
-	time.AfterFunc(time.Millisecond*time.Duration(50), func() { server.Close() })
+	time.AfterFunc(time.Millisecond*time.Duration(100), func() { server.Close() })
 
 	t.Log("Ok, Identified...")
 
@@ -302,7 +302,26 @@ func ircServer(listener net.Listener, t *testing.T, config *IRCConfig) {
 			// process the received line here
 			parts = strings.Split(line, " ")
 			t.Logf("<< %s", line)
-
+			switch parts[0] {
+			case "JOIN":
+				for _, channel := range strings.Split(parts[1], ",") {
+					output = fmt.Sprintf(":%s JOIN :%s", config.MyNick, channel)
+					ircWrite(server, output, t)
+					output = fmt.Sprintf(":irc.server 332 %s %s :Topic for (%s)", config.MyNick, channel, channel)
+					ircWrite(server, output, t)
+					output = fmt.Sprintf(":irc.server 333 %s %s user %d", config.MyNick, channel, time.Now().Unix())
+					ircWrite(server, output, t)
+				}
+			}
+			switch parts[0] {
+			case "PRIVMSG", "NOTICE":
+				if parts[1] == "echo" {
+					parts[2] = parts[2][1:]
+					// echo user, return whatever was sent back to them.
+					output = fmt.Sprintf(":%s %s %s :%s", "echo", parts[0], config.MyNick, strings.Join(parts[2:], " "))
+					ircWrite(server, output, t)
+				}
+			}
 		} else {
 			t.Log("Read Error:", err)
 			return
@@ -409,3 +428,77 @@ func TestConnectTLS(t *testing.T) {
 	}
 
 }
+
+func TestConnectAutojoin(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"},
+		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 == "ACTION") || (Msg.Cmd == "NOTICE") {
+			t.Log(Msg)
+		}
+		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 == 2 {
+				// messages set to echo are returned to us
+				config.WriteTo("echo", "PRIVMSG echo :\x01VERSION\x01")
+				config.WriteTo("echo", "PRIVMSG echo :\x01TIME\x01")
+				config.WriteTo("echo", "PRIVMSG echo :\x01ACTION dances.\x01")
+				config.WriteTo("#test", "PRIVMSG #test :Message 1")
+				config.WriteTo("#test", "PRIVMSG #test :Message 2")
+			}
+		}
+	}
+
+	if joins != 2 {
+		t.Errorf("Expected to autojoin 2 channels, got %d", joins)
+	}
+	if !motd {
+		t.Error("Missing EndMOTD")
+	}
+	if !identify {
+		t.Error("Missing Identified")
+	}
+
+	if config.MyNick != config.Nick {
+		t.Errorf("Got %s, Expected %s", config.MyNick, config.Nick)
+	}
+
+}