Test telnet package

This commit is contained in:
Yohann D'ANELLO 2020-10-14 19:42:23 +02:00
parent e5f4c3a7e6
commit 8590f23039
2 changed files with 64 additions and 2 deletions

View File

@ -57,8 +57,7 @@ func Serve(config *Options) {
streamID := ""
// Request for stream ID
for {
_, _ = s.Write([]byte("[GHOSTREAM]\n"))
_, err = s.Write([]byte("Enter stream ID: "))
_, err = s.Write([]byte("[GHOSTREAM]\nEnter stream ID: "))
if err != nil {
log.Println("Error while requesting stream ID to telnet client")
_ = s.Close()

View File

@ -1 +1,64 @@
package telnet
import (
"bytes"
"io/ioutil"
"math/rand"
"net"
"testing"
)
// TestTelnetOutput creates a TCP client that connects to the server and get one image.
func TestTelnetOutput(t *testing.T) {
// Enable and start Telnet server
Serve(&Options{
Enabled: true,
ListenAddress: "127.0.0.1:8023",
Width: 80,
Height: 45,
Delay: 50,
})
// Generate a random image, that should be given by FFMPEG
sampleImage := make([]byte, Cfg.Width*Cfg.Height)
rand.Read(sampleImage)
reader := ioutil.NopCloser(bytes.NewBuffer(sampleImage))
// Send the image to the server
StartASCIIArtStream("demo", reader)
// Connect to the Telnet server
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
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])
}
// 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
if n != 42+(2*Cfg.Width+1)*Cfg.Height {
t.Fatalf("Read %d from TCP, expected %d", n, 42+(2*Cfg.Width+1)*Cfg.Height)
}
}