1
0
mirror of https://gitlab.crans.org/nounous/ghostream.git synced 2024-12-23 05:02:20 +00:00
ghostream/stream/telnet/telnet_test.go

109 lines
2.8 KiB
Go
Raw Normal View History

2020-10-13 16:55:12 +00:00
package telnet
2020-10-14 17:42:23 +00:00
import (
"testing"
2020-10-17 16:12:25 +00:00
"gitlab.crans.org/nounous/ghostream/messaging"
2020-10-14 17:42:23 +00:00
)
// TestTelnetOutput creates a TCP client that connects to the server and get one image.
func TestTelnetOutput(t *testing.T) {
2020-10-14 18:01:07 +00:00
// Try to start Telnet server while it is disabled
streams := messaging.New()
2020-10-17 16:12:25 +00:00
go Serve(streams, &Options{Enabled: false})
// FIXME test connect
2020-10-14 18:01:07 +00:00
2020-10-14 17:42:23 +00:00
// Enable and start Telnet server
2020-10-17 16:12:25 +00:00
cfg := Options{
2020-10-14 17:42:23 +00:00
Enabled: true,
ListenAddress: "127.0.0.1:8023",
2020-10-17 16:12:25 +00:00
}
go Serve(streams, &cfg)
// FIXME test connect
2020-10-14 17:42:23 +00:00
// Generate a random image, that should be given by FFMPEG
2020-10-17 16:12:25 +00:00
/*sampleImage := make([]byte, cfg.Width*cfg.Height)
2020-10-14 17:42:23 +00:00
rand.Read(sampleImage)
reader := ioutil.NopCloser(bytes.NewBuffer(sampleImage))
// Connect to the Telnet server
2020-10-17 16:12:25 +00:00
client, err := net.Dial("tcp", cfg.ListenAddress)
2020-10-14 17:42:23 +00:00
if err != nil {
t.Fatalf("Error while connecting to the TCP server: %s", err)
}
2020-10-14 18:01:07 +00:00
// Say goodbye
_, err = client.Write([]byte("exit"))
if err != nil {
t.Fatalf("Error while closing TCP connection: %s", err)
}
2020-10-17 16:12:25 +00:00
client, err = net.Dial("tcp", cfg.ListenAddress)
2020-10-14 18:01:07 +00:00
if err != nil {
t.Fatalf("Error while connecting to the TCP server: %s", err)
}
2020-10-14 17:42:23 +00:00
// Create a sufficient large buffer
buff := make([]byte, 3*len(sampleImage))
// Read the input, and ensure that it is correct
// [GHOSTREAM]
// Enter stream ID:
n, err := client.Read(buff)
if err != nil {
t.Fatalf("Error while reading from TCP: %s", err)
}
if n != len("[GHOSTREAM]\nEnter stream ID: ") {
t.Fatalf("Read %d bytes from TCP, expected %d, read: %s", n, len("[GHOSTREAM]\nEnter stream ID: "), buff[:n])
}
2020-10-14 18:01:07 +00:00
// 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)
}
}
2020-10-14 17:42:23 +00:00
// Send stream ID
_, err = client.Write([]byte("demo"))
if err != nil {
t.Fatalf("Error while writing from TCP: %s", err)
}
// Read the generated image
n, err = client.Read(buff)
if err != nil {
t.Fatalf("Error while reading the image from TCP: %s", err)
}
// Ensure that the size of the image is correct
2020-10-16 18:53:38 +00:00
if n < 10800 {
t.Fatalf("Read %d from TCP, expected more than 10800", n)
2020-10-14 17:42:23 +00:00
}
if GetNumberConnectedSessions("demo") != 1 {
t.Fatalf("Expected one telnet client only, found %d", GetNumberConnectedSessions("demo"))
}
2020-10-14 18:01:07 +00:00
// 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"))
2020-10-17 16:12:25 +00:00
}*/
2020-10-14 17:42:23 +00:00
}