forked from bugst/go-serial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial_setserial_linux_test.go
More file actions
58 lines (49 loc) · 1.53 KB
/
serial_setserial_linux_test.go
File metadata and controls
58 lines (49 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//go:build linux
package serial
import (
"context"
"os/exec"
"testing"
"github.com/stretchr/testify/require"
)
func startSocatAndWaitForSetserialTest(t *testing.T, ctx context.Context) *exec.Cmd {
cmd := exec.CommandContext(ctx, "socat", "-D", "STDIO", "pty,link=/tmp/faketty_setserial")
r, err := cmd.StderrPipe()
require.NoError(t, err)
require.NoError(t, cmd.Start())
buf := make([]byte, 1024)
_, err = r.Read(buf)
require.NoError(t, err)
return cmd
}
func TestGetSerialStructOnFakeTty(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cmd := startSocatAndWaitForSetserialTest(t, ctx)
go cmd.Wait()
port, err := nativeOpen("/tmp/faketty", &Mode{})
require.NoError(t, err)
defer port.Close()
ser, err := port.GetSerialStruct()
// Note: socat's virtual TTY may not fully support TIOCGSERIAL.
// If not supported, skip the test (environment dependent).
if err != nil {
t.Skipf("ioctl TIOCGSERIAL not supported on socat pty: %v", err)
}
require.NotNil(t, ser)
}
func TestSetSerialPortModeOnFakeTty(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cmd := startSocatAndWaitForSetserialTest(t, ctx)
go cmd.Wait()
port, err := nativeOpen("/tmp/faketty", &Mode{})
require.NoError(t, err)
defer port.Close()
err = port.SetSerialPortMode(0)
// Note: socat's virtual TTY may not fully support TIOCSSERIAL.
// If not supported, skip the test (environment dependent).
if err != nil {
t.Skipf("ioctl TIOCSSERIAL not supported on socat pty: %v", err)
}
}