You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.0 KiB
72 lines
2.0 KiB
|
2 months ago
|
package serial
|
||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestNormalizeLineConfigAppliesSafeDefaults(t *testing.T) {
|
||
|
|
config, err := NormalizeLineConfig(LineConfig{Device: "/dev/ttyS1"})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("normalize line config: %v", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
if config.BaudRate != 9600 {
|
||
|
|
t.Fatalf("baud rate = %d, want 9600", config.BaudRate)
|
||
|
|
}
|
||
|
|
if config.DataBits != 8 {
|
||
|
|
t.Fatalf("data bits = %d, want 8", config.DataBits)
|
||
|
|
}
|
||
|
|
if config.Parity != "none" {
|
||
|
|
t.Fatalf("parity = %q, want none", config.Parity)
|
||
|
|
}
|
||
|
|
if config.StopBits != 1 {
|
||
|
|
t.Fatalf("stop bits = %d, want 1", config.StopBits)
|
||
|
|
}
|
||
|
|
if config.StopBitsMode != "1" {
|
||
|
|
t.Fatalf("stop bits mode = %q, want 1", config.StopBitsMode)
|
||
|
|
}
|
||
|
|
if config.FlowControl != "none" {
|
||
|
|
t.Fatalf("flow control = %q, want none", config.FlowControl)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestNormalizeLineConfigAcceptsTargetDeviceProfile(t *testing.T) {
|
||
|
|
config, err := NormalizeLineConfig(LineConfig{
|
||
|
|
Device: "/dev/serial/by-path/platform-pcie-uart-port1",
|
||
|
|
BaudRate: 115200,
|
||
|
|
DataBits: 7,
|
||
|
|
Parity: "Even",
|
||
|
|
StopBits: 2,
|
||
|
|
FlowControl: "RTSCTS",
|
||
|
|
})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("normalize line config: %v", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
if config.Parity != "even" {
|
||
|
|
t.Fatalf("parity = %q, want even", config.Parity)
|
||
|
|
}
|
||
|
|
if config.FlowControl != "rtscts" {
|
||
|
|
t.Fatalf("flow control = %q, want rtscts", config.FlowControl)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestNormalizeLineConfigRejectsUnsafeOrUnsupportedValues(t *testing.T) {
|
||
|
|
for _, item := range []LineConfig{
|
||
|
|
{Device: ""},
|
||
|
|
{Device: "COM1"},
|
||
|
|
{Device: "/dev/ttyS1", BaudRate: 12345},
|
||
|
|
{Device: "/dev/ttyS1", DataBits: 9},
|
||
|
|
{Device: "/dev/ttyS1", Parity: "invalid"},
|
||
|
|
{Device: "/dev/ttyS1", StopBits: 3},
|
||
|
|
{Device: "/dev/ttyS1", FlowControl: "invalid"},
|
||
|
|
{Device: "/dev/ttyS1", FlowControl: "rtscts", RTSControl: "on"},
|
||
|
|
{Device: "/dev/ttyS1", DataBits: 8, StopBitsMode: "1.5"},
|
||
|
|
} {
|
||
|
|
if _, err := NormalizeLineConfig(item); !errors.Is(err, ErrInvalidLineConfig) {
|
||
|
|
t.Fatalf("NormalizeLineConfig(%#v) error = %v, want ErrInvalidLineConfig", item, err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|