2017-04-17 02:22:45 +08:00
|
|
|
package mavlink
|
|
|
|
|
|
|
|
import (
|
2017-04-19 00:19:38 +08:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
2017-04-17 20:20:49 +08:00
|
|
|
"net"
|
2017-04-17 02:22:45 +08:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/gobottest"
|
2017-04-19 00:19:38 +08:00
|
|
|
mavlink "gobot.io/x/gobot/platforms/mavlink/common"
|
2017-04-17 02:22:45 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ gobot.Adaptor = (*UDPAdaptor)(nil)
|
|
|
|
|
2017-04-17 20:20:49 +08:00
|
|
|
type MockUDPConnection struct {
|
|
|
|
TestClose func() error
|
|
|
|
TestReadFromUDP func([]byte) (int, *net.UDPAddr, error)
|
|
|
|
TestWriteTo func([]byte, net.Addr) (int, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockUDPConnection) Close() error {
|
|
|
|
return m.TestClose()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockUDPConnection) ReadFromUDP(b []byte) (int, *net.UDPAddr, error) {
|
|
|
|
return m.TestReadFromUDP(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockUDPConnection) WriteTo(b []byte, a net.Addr) (int, error) {
|
|
|
|
return m.TestWriteTo(b, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMockUDPConnection() *MockUDPConnection {
|
|
|
|
return &MockUDPConnection{
|
|
|
|
TestClose: func() error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
TestReadFromUDP: func([]byte) (int, *net.UDPAddr, error) {
|
|
|
|
return 0, nil, nil
|
|
|
|
},
|
|
|
|
TestWriteTo: func([]byte, net.Addr) (int, error) {
|
|
|
|
return 0, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 02:22:45 +08:00
|
|
|
func initTestMavlinkUDPAdaptor() *UDPAdaptor {
|
|
|
|
m := NewUDPAdaptor(":14550")
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMavlinkUDPAdaptor(t *testing.T) {
|
|
|
|
a := initTestMavlinkUDPAdaptor()
|
|
|
|
gobottest.Assert(t, a.Port(), ":14550")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMavlinkUDPAdaptorName(t *testing.T) {
|
|
|
|
a := initTestMavlinkUDPAdaptor()
|
|
|
|
gobottest.Assert(t, strings.HasPrefix(a.Name(), "Mavlink"), true)
|
|
|
|
a.SetName("NewName")
|
|
|
|
gobottest.Assert(t, a.Name(), "NewName")
|
|
|
|
}
|
|
|
|
|
2017-04-17 18:36:36 +08:00
|
|
|
func TestMavlinkUDPAdaptorConnectAndFinalize(t *testing.T) {
|
2017-04-17 02:22:45 +08:00
|
|
|
a := initTestMavlinkUDPAdaptor()
|
|
|
|
gobottest.Assert(t, a.Connect(), nil)
|
2017-04-17 18:36:36 +08:00
|
|
|
gobottest.Assert(t, a.Finalize(), nil)
|
2017-04-17 02:22:45 +08:00
|
|
|
}
|
|
|
|
|
2017-04-17 18:36:36 +08:00
|
|
|
func TestMavlinkUDPAdaptorWrite(t *testing.T) {
|
2017-04-17 02:22:45 +08:00
|
|
|
a := initTestMavlinkUDPAdaptor()
|
2017-04-17 18:36:36 +08:00
|
|
|
a.Connect()
|
|
|
|
defer a.Finalize()
|
|
|
|
|
2017-04-17 20:20:49 +08:00
|
|
|
m := NewMockUDPConnection()
|
|
|
|
m.TestWriteTo = func([]byte, net.Addr) (int, error) {
|
|
|
|
return 3, nil
|
|
|
|
}
|
|
|
|
a.sock = m
|
|
|
|
|
2017-04-17 18:36:36 +08:00
|
|
|
i, err := a.Write([]byte{0x01, 0x02, 0x03})
|
|
|
|
gobottest.Assert(t, i, 3)
|
|
|
|
gobottest.Assert(t, err, nil)
|
2017-04-17 02:22:45 +08:00
|
|
|
}
|
2017-04-19 00:19:38 +08:00
|
|
|
|
|
|
|
func TestMavlinkReadMAVLinkReadDefaultPacket(t *testing.T) {
|
|
|
|
a := initTestMavlinkUDPAdaptor()
|
|
|
|
a.Connect()
|
|
|
|
defer a.Finalize()
|
|
|
|
|
|
|
|
m := NewMockUDPConnection()
|
|
|
|
|
|
|
|
m.TestReadFromUDP = func(b []byte) (int, *net.UDPAddr, error) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.Write([]byte{mavlink.MAVLINK_10_STX, 0x02, 0x03})
|
|
|
|
copy(b, buf.Bytes())
|
|
|
|
return buf.Len(), nil, nil
|
|
|
|
}
|
|
|
|
a.sock = m
|
|
|
|
|
|
|
|
p, _ := a.ReadMAVLinkPacket()
|
|
|
|
gobottest.Assert(t, p.Protocol, uint8(254))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMavlinkReadMAVLinkPacketReadError(t *testing.T) {
|
|
|
|
a := initTestMavlinkUDPAdaptor()
|
|
|
|
a.Connect()
|
|
|
|
defer a.Finalize()
|
|
|
|
|
|
|
|
m := NewMockUDPConnection()
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
m.TestReadFromUDP = func(b []byte) (int, *net.UDPAddr, error) {
|
|
|
|
switch i {
|
|
|
|
case 0:
|
|
|
|
i = 1
|
|
|
|
return 1, nil, nil
|
|
|
|
case 1:
|
|
|
|
i = 2
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.Write([]byte{0x01, 0x02, 0x03})
|
|
|
|
copy(b, buf.Bytes())
|
|
|
|
return buf.Len(), nil, nil
|
|
|
|
case 2:
|
|
|
|
i = 3
|
2017-04-19 00:28:51 +08:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.Write([]byte{mavlink.MAVLINK_10_STX, 255})
|
|
|
|
copy(b, buf.Bytes())
|
|
|
|
return buf.Len(), nil, nil
|
2017-04-19 00:19:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0, nil, errors.New("read error")
|
|
|
|
}
|
|
|
|
a.sock = m
|
|
|
|
|
|
|
|
_, err := a.ReadMAVLinkPacket()
|
|
|
|
gobottest.Assert(t, err, errors.New("read error"))
|
|
|
|
}
|