Files
selfcare/proxy_go/public/go-telnet@v0.0.0-20180421082511-9ff0b2ab096e/telsh/telnet_handler_test.go
2025-03-03 11:40:37 +08:00

111 lines
1.7 KiB
Go

package telsh
import (
"github.com/reiver/go-telnet"
"bytes"
"strings"
"testing"
)
func TestServeTELNETCommandNotFound(t *testing.T) {
tests := []struct{
ClientSends string
Expected string
}{
{
ClientSends: "\r\n",
Expected: "",
},
{
ClientSends: "apple\r\n",
Expected: "apple: command not found\r\n",
},
{
ClientSends: "banana\r\n",
Expected: "banana: command not found\r\n",
},
{
ClientSends: "cherry\r\n",
Expected: "cherry: command not found\r\n",
},
{
ClientSends: "\t\r\n",
Expected: "",
},
{
ClientSends: "\t\t\r\n",
Expected: "",
},
{
ClientSends: "\t\t\t\r\n",
Expected: "",
},
{
ClientSends: " \r\n",
Expected: "",
},
{
ClientSends: " \r\n",
Expected: "",
},
{
ClientSends: " \r\n",
Expected: "",
},
{
ClientSends: " \t\r\n",
Expected: "",
},
{
ClientSends: "\t \r\n",
Expected: "",
},
{
ClientSends: "ls -alF\r\n",
Expected: "ls: command not found\r\n",
},
}
for testNumber, test := range tests {
shellHandler := NewShellHandler()
if nil == shellHandler {
t.Errorf("For test #%d, did not expect to get nil, but actually got it: %v; for client sent: %q", testNumber, shellHandler, test.ClientSends)
continue
}
ctx := telnet.NewContext()
var buffer bytes.Buffer
shellHandler.ServeTELNET(ctx, &buffer, strings.NewReader(test.ClientSends))
if expected, actual := shellHandler.WelcomeMessage+shellHandler.Prompt+test.Expected+shellHandler.Prompt+shellHandler.ExitMessage, buffer.String(); expected != actual {
t.Errorf("For test #%d, expect %q, but actually got %q; for client sent: %q", testNumber, expected, actual, test.ClientSends)
continue
}
}
}