125 lines
2.8 KiB
Go
125 lines
2.8 KiB
Go
package transport_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.lang-dieter.de/rs2322tcp/internal/config"
|
|
"git.lang-dieter.de/rs2322tcp/internal/transport"
|
|
)
|
|
|
|
func TestNewHello(t *testing.T) {
|
|
message := transport.NewHello()
|
|
|
|
if message.Version != transport.ProtocolVersion {
|
|
t.Errorf("Version = %d, want %d",
|
|
message.Version, transport.ProtocolVersion)
|
|
}
|
|
|
|
if message.Type != transport.MessageHello {
|
|
t.Errorf("Type = %q, want %q",
|
|
message.Type, transport.MessageHello)
|
|
}
|
|
}
|
|
|
|
func TestNewGetDevices(t *testing.T) {
|
|
message := transport.NewGetDevices()
|
|
|
|
if message.Version != transport.ProtocolVersion {
|
|
t.Errorf("Version = %d, want %d",
|
|
message.Version, transport.ProtocolVersion)
|
|
}
|
|
|
|
if message.Type != transport.MessageGetDevices {
|
|
t.Errorf("Type = %q, want %q",
|
|
message.Type, transport.MessageGetDevices)
|
|
}
|
|
}
|
|
|
|
func TestDeviceListJSON(t *testing.T) {
|
|
message := transport.NewDeviceList([]transport.RemoteDeviceInfo{
|
|
{
|
|
ID: "radio",
|
|
Name: "Funkgerät",
|
|
BaudRate: 9600,
|
|
DataBits: 8,
|
|
Parity: "none",
|
|
StopBits: 1,
|
|
DataPort: 43721,
|
|
},
|
|
})
|
|
|
|
data, err := transport.EncodeMessage(message)
|
|
if err != nil {
|
|
t.Fatalf("EncodeMessage() failed: %v", err)
|
|
}
|
|
|
|
jsonText := string(data)
|
|
|
|
if !strings.Contains(jsonText, `"type":"device_list"`) {
|
|
t.Fatalf("JSON does not contain device_list: %s", jsonText)
|
|
}
|
|
|
|
if !strings.Contains(jsonText, `"data_port":43721`) {
|
|
t.Fatalf("JSON does not contain data port: %s", jsonText)
|
|
}
|
|
|
|
if strings.Contains(jsonText, "serial_port") {
|
|
t.Fatalf("JSON contains server-internal serial_port")
|
|
}
|
|
}
|
|
|
|
func TestNewRemoteDeviceInfo(t *testing.T) {
|
|
device := config.RemoteDevice{
|
|
ID: "radio",
|
|
Name: "Funkgerät",
|
|
BaudRate: 9600,
|
|
DataBits: 8,
|
|
Parity: "none",
|
|
StopBits: 1,
|
|
}
|
|
|
|
info := transport.NewRemoteDeviceInfo(device, 43721)
|
|
|
|
if info.ID != "radio" {
|
|
t.Errorf("ID = %q, want %q", info.ID, "radio")
|
|
}
|
|
|
|
if info.DataPort != 43721 {
|
|
t.Errorf("DataPort = %d, want %d", info.DataPort, 43721)
|
|
}
|
|
}
|
|
|
|
func TestErrorMessageJSON(t *testing.T) {
|
|
message := transport.NewError(
|
|
transport.ErrorUnknownMessage,
|
|
"unknown message type",
|
|
)
|
|
|
|
data, err := transport.EncodeMessage(message)
|
|
if err != nil {
|
|
t.Fatalf("EncodeMessage() failed: %v", err)
|
|
}
|
|
|
|
var decoded map[string]interface{}
|
|
|
|
if err := json.Unmarshal(data, &decoded); err != nil {
|
|
t.Fatalf("json.Unmarshal() failed: %v", err)
|
|
}
|
|
|
|
if decoded["type"] != string(transport.MessageError) {
|
|
t.Errorf("type = %v, want %q",
|
|
decoded["type"], transport.MessageError)
|
|
}
|
|
|
|
if decoded["code"] != string(transport.ErrorUnknownMessage) {
|
|
t.Errorf("code = %v, want %q",
|
|
decoded["code"], transport.ErrorUnknownMessage)
|
|
}
|
|
|
|
if decoded["message"] != "unknown message type" {
|
|
t.Errorf("message = %v, want %q",
|
|
decoded["message"], "unknown message type")
|
|
}
|
|
}
|