feat: replace iot adapter with mqtt command pipeline
This commit is contained in:
55
backend/iot-server/internal/protocol/frame_test.go
Normal file
55
backend/iot-server/internal/protocol/frame_test.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestFrameRoundTripEncrypted(t *testing.T) {
|
||||
keys := Keyring{2: []byte("0123456789abcdef")}
|
||||
payload, _ := ValveCommand(1, 2, 3, false)
|
||||
want := Frame{KeyID: 2, Version: 1, Control: 0x10, DeviceKind: 1, DeviceType: 1, PacketNumber: 7, Sequence: 1, Final: true, DeviceTime: time.Date(2025, 5, 7, 8, 9, 10, 0, time.Local), Payload: payload}
|
||||
copy(want.DeviceID[:], []byte{0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56})
|
||||
raw, err := Encode(want, keys)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := Decode(raw, keys)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(got.Payload, want.Payload) || got.DeviceID != want.DeviceID || got.PacketNumber != want.PacketNumber {
|
||||
t.Fatalf("round trip mismatch: %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeRejectsTampering(t *testing.T) {
|
||||
raw, err := Encode(Frame{Version: 1, Final: true, Payload: []byte{1, 0, 0}}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
raw[5] ^= 1
|
||||
if _, err = Decode(raw, nil); err != ErrChecksum {
|
||||
t.Fatalf("got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValveCommand(t *testing.T) {
|
||||
got, _ := ValveCommand(1, 2, 3, true)
|
||||
want := []byte{0x40, 1, 0x0A, 1, 2, 3, 0xA0, 0}
|
||||
if !bytes.Equal(got, want) {
|
||||
t.Fatalf("got %x want %x", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeRealtimeSensor(t *testing.T) {
|
||||
payload := []byte{0x50, 1, 0x01, 0, 1, 0, 0, 0, 1, 0x08, 0, 0xE4, 0, 123, 0x0C, 2, 1, 0}
|
||||
got, err := DecodeRealtime(payload)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got.Sensors) != 1 || got.Sensors[0].Number != 1 || got.Sensors[0].Value != 123 || got.Sensors[0].Decimal != 2 {
|
||||
t.Fatalf("unexpected result: %#v", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user