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
59
60
61
62
63
64
65
66
67
68
69
|
package transport
import (
"bytes"
pb "github.com/crypto-smoke/meshtastic-go/meshtastic"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/proto"
"net"
"testing"
)
func TestStreamConn(t *testing.T) {
radioNetConn, clientNetConn := net.Pipe()
var client *StreamConn
var radio *StreamConn
// Test client -> radio
sent := &pb.ToRadio{
PayloadVariant: &pb.ToRadio_WantConfigId{
WantConfigId: 123,
},
}
received := &pb.ToRadio{}
eg := errgroup.Group{}
eg.Go(func() error {
var err error
client, err = NewClientStreamConn(clientNetConn)
require.NoError(t, err)
return client.Write(sent)
})
eg.Go(func() error {
radio = NewRadioStreamConn(radioNetConn)
return radio.Read(received)
})
require.NoError(t, eg.Wait())
require.True(t, proto.Equal(sent, received))
// Test radio -> client
replySent := &pb.FromRadio{
Id: 123,
PayloadVariant: &pb.FromRadio_Config{
Config: &pb.Config{
PayloadVariant: &pb.Config_Device{
Device: &pb.Config_DeviceConfig{
Role: pb.Config_DeviceConfig_ROUTER,
},
},
},
},
}
replyReceived := &pb.FromRadio{}
eg = errgroup.Group{}
eg.Go(func() error {
return radio.Write(replySent)
})
eg.Go(func() error {
return client.Read(replyReceived)
})
require.NoError(t, eg.Wait())
require.True(t, proto.Equal(replySent, replyReceived))
}
func Test_writeStreamHeader(t *testing.T) {
out := bytes.NewBuffer(nil)
err := writeStreamHeader(out, 257)
require.NoError(t, err)
require.Equal(t, []byte{Start1, Start2, 0x01, 0x01}, out.Bytes())
}
|