1
0
mirror of https://gitlab.crans.org/nounous/ghostream.git synced 2024-12-22 15:02:19 +00:00

More coverage on telnet package

This commit is contained in:
Yohann D'ANELLO 2020-10-14 20:01:07 +02:00
parent 1f0dc64c35
commit e53d5a02ab

View File

@ -6,10 +6,18 @@ import (
"math/rand" "math/rand"
"net" "net"
"testing" "testing"
"time"
) )
// TestTelnetOutput creates a TCP client that connects to the server and get one image. // TestTelnetOutput creates a TCP client that connects to the server and get one image.
func TestTelnetOutput(t *testing.T) { func TestTelnetOutput(t *testing.T) {
// Try to start Telnet server while it is disabled
Serve(&Options{Enabled: false})
StartASCIIArtStream("demo", ioutil.NopCloser(bytes.NewReader([]byte{})))
if GetNumberConnectedSessions("demo") != 0 {
t.Fatalf("Mysteriously found %d connected clients", GetNumberConnectedSessions("demo"))
}
// Enable and start Telnet server // Enable and start Telnet server
Serve(&Options{ Serve(&Options{
Enabled: true, Enabled: true,
@ -32,6 +40,17 @@ func TestTelnetOutput(t *testing.T) {
t.Fatalf("Error while connecting to the TCP server: %s", err) t.Fatalf("Error while connecting to the TCP server: %s", err)
} }
// Say goodbye
_, err = client.Write([]byte("exit"))
if err != nil {
t.Fatalf("Error while closing TCP connection: %s", err)
}
client, err = net.Dial("tcp", Cfg.ListenAddress)
if err != nil {
t.Fatalf("Error while connecting to the TCP server: %s", err)
}
// Create a sufficient large buffer // Create a sufficient large buffer
buff := make([]byte, 3*len(sampleImage)) buff := make([]byte, 3*len(sampleImage))
@ -46,6 +65,22 @@ func TestTelnetOutput(t *testing.T) {
t.Fatalf("Read %d bytes from TCP, expected %d, read: %s", n, len("[GHOSTREAM]\nEnter stream ID: "), buff[:n]) t.Fatalf("Read %d bytes from TCP, expected %d, read: %s", n, len("[GHOSTREAM]\nEnter stream ID: "), buff[:n])
} }
// Send wrong stream ID
_, err = client.Write([]byte("toto"))
if err != nil {
t.Fatalf("Error while writing from TCP: %s", err)
}
n, err = client.Read(buff)
if err != nil {
t.Fatalf("Error while reading from TCP: %s", err)
}
if n == len("Unknown stream ID.\n") {
_, err = client.Read(buff)
if err != nil {
t.Fatalf("Error while reading from TCP: %s", err)
}
}
// Send stream ID // Send stream ID
_, err = client.Write([]byte("demo")) _, err = client.Write([]byte("demo"))
if err != nil { if err != nil {
@ -65,4 +100,15 @@ func TestTelnetOutput(t *testing.T) {
if GetNumberConnectedSessions("demo") != 1 { if GetNumberConnectedSessions("demo") != 1 {
t.Fatalf("Expected one telnet client only, found %d", GetNumberConnectedSessions("demo")) t.Fatalf("Expected one telnet client only, found %d", GetNumberConnectedSessions("demo"))
} }
// Close connection, ensure that the counter got decremented
err = client.Close()
if err != nil {
t.Fatalf("Error while closing telnet connection: %s", err)
}
// Wait for timeout
time.Sleep(time.Second)
if GetNumberConnectedSessions("demo") != 0 {
t.Fatalf("Expected no telnet client, found %d", GetNumberConnectedSessions("demo"))
}
} }