mirror of
https://github.com/renorris/openfsd
synced 2026-04-20 09:55:43 +08:00
initial commit
This commit is contained in:
131
protocol/plane_info_request_test.go
Normal file
131
protocol/plane_info_request_test.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestParsePlaneInfoRequestPDU(t *testing.T) {
|
||||
V = validator.New(validator.WithRequiredStructEnabled())
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
packet string
|
||||
want *PlaneInfoRequestPDU
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "Valid input",
|
||||
packet: "#SBPILOT:ATC:PIR\r\n",
|
||||
want: &PlaneInfoRequestPDU{
|
||||
From: "PILOT",
|
||||
To: "ATC",
|
||||
},
|
||||
wantErr: nil,
|
||||
},
|
||||
{
|
||||
name: "Last element not PIR",
|
||||
packet: "#SBPILOT:ATC:PI\r\n",
|
||||
want: nil,
|
||||
wantErr: NewGenericFSDError(SyntaxError),
|
||||
},
|
||||
{
|
||||
name: "Missing To field",
|
||||
packet: "#SBPILOT::PIR\r\n",
|
||||
want: nil,
|
||||
wantErr: NewGenericFSDError(SyntaxError),
|
||||
},
|
||||
{
|
||||
name: "Missing From field",
|
||||
packet: "#SB:ATC:PIR\r\n",
|
||||
want: nil,
|
||||
wantErr: NewGenericFSDError(SyntaxError),
|
||||
},
|
||||
{
|
||||
name: "Extra fields",
|
||||
packet: "#SBPILOT:ATC:EXTRA:PIR\r\n",
|
||||
want: nil,
|
||||
wantErr: NewGenericFSDError(SyntaxError),
|
||||
},
|
||||
{
|
||||
name: "Invalid From field (non-alphanumerical)",
|
||||
packet: "#SBP!@#$:ATC:PIR\r\n",
|
||||
want: nil,
|
||||
wantErr: NewGenericFSDError(SyntaxError),
|
||||
},
|
||||
{
|
||||
name: "Invalid To field (too long)",
|
||||
packet: "#SBPILOT:12345678:PIR\r\n",
|
||||
want: nil,
|
||||
wantErr: NewGenericFSDError(SyntaxError),
|
||||
},
|
||||
{
|
||||
name: "Input with incorrect prefix",
|
||||
packet: "$DIPILOT:ATC:PIR\r\n",
|
||||
want: nil,
|
||||
wantErr: NewGenericFSDError(SyntaxError),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
// Perform the parsing
|
||||
result, err := ParsePlaneInfoRequestPDU(tc.packet)
|
||||
|
||||
// Check the error
|
||||
if tc.wantErr != nil {
|
||||
assert.EqualError(t, err, tc.wantErr.Error())
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
// Verify the result
|
||||
assert.Equal(t, tc.want, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlaneInfoRequestPDU_Serialize(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
pdu PlaneInfoRequestPDU
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "Valid serialization",
|
||||
pdu: PlaneInfoRequestPDU{
|
||||
From: "PILOT",
|
||||
To: "ATC",
|
||||
},
|
||||
want: "#SBPILOT:ATC:PIR\r\n", // assuming correct values for constants
|
||||
},
|
||||
{
|
||||
name: "Minimal length fields",
|
||||
pdu: PlaneInfoRequestPDU{
|
||||
From: "A",
|
||||
To: "B",
|
||||
},
|
||||
want: "#SBA:B:PIR\r\n", // assuming correct values for constants
|
||||
},
|
||||
{
|
||||
name: "Max length fields",
|
||||
pdu: PlaneInfoRequestPDU{
|
||||
From: "1234567",
|
||||
To: "7654321",
|
||||
},
|
||||
want: "#SB1234567:7654321:PIR\r\n", // assuming correct values for constants
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
// Perform serialization
|
||||
result := tc.pdu.Serialize()
|
||||
|
||||
// Verify the result
|
||||
assert.Equal(t, tc.want, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user