mirror of
https://github.com/renorris/openfsd
synced 2026-03-22 14:35:36 +08:00
Changes:
- Implement bootstrapping library for managing several concurrent internal services
- Refactor concurrency model for connections/logical clients and their associated I/O
- Refactor server context singleton
- Refactor error handling
- Most errors are now gracefully sent to the FSD client directly encoded as an $ER packet,
enhancing visibility and debugging
- Most errors are now rightfully treated as non-fatal
- Refactor package/dependency graph
- Refactor calling conventions/interfaces for many packages
- Refactor database package
- Refactor post office
Features:
- Add VATSIM-esque HTTP/JSON "data feed"
- Add ephemeral in-memory database option
- Add user management REST API
- Add improved web interface
- Add MySQL support (drop SQLite support)
111 lines
2.2 KiB
Go
111 lines
2.2 KiB
Go
package protocol
|
|
|
|
import (
|
|
"github.com/go-playground/validator/v10"
|
|
"github.com/stretchr/testify/assert"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseDeletePilotPDU(t *testing.T) {
|
|
V = validator.New(validator.WithRequiredStructEnabled())
|
|
|
|
tests := []struct {
|
|
name string
|
|
packet string
|
|
want *DeletePilotPDU
|
|
wantErr error
|
|
}{
|
|
{
|
|
"Valid Packet",
|
|
"#DPCTRLLL:1234567\r\n",
|
|
&DeletePilotPDU{
|
|
From: "CTRLLL",
|
|
CID: 1234567,
|
|
},
|
|
nil,
|
|
},
|
|
{
|
|
"Invalid From Field (Too Long)",
|
|
"#DPCONTROLLER1CONTROLLER1CONTROLLER1CONTROLLER1:1234567\r\n",
|
|
&DeletePilotPDU{},
|
|
NewGenericFSDError(SyntaxError, "", "validation error"),
|
|
},
|
|
{
|
|
"Invalid CID Field (Non-Numeric)",
|
|
"#DPCTRLLL:ABCDEF1\r\n",
|
|
&DeletePilotPDU{},
|
|
NewGenericFSDError(SyntaxError, "ABCDEF1", "invalid CID"),
|
|
},
|
|
{
|
|
"Invalid CID Field (Wrong Len)",
|
|
"#DPCTRLLL:12345\r\n",
|
|
&DeletePilotPDU{},
|
|
NewGenericFSDError(SyntaxError, "", "validation error"),
|
|
},
|
|
{
|
|
"Extra Fields",
|
|
"#DPCTRLLL:1234567:ExtraData\r\n",
|
|
&DeletePilotPDU{},
|
|
NewGenericFSDError(SyntaxError, "", "invalid parameter count"),
|
|
},
|
|
{
|
|
"Missing CID",
|
|
"#DPCTRLLL:\r\n",
|
|
&DeletePilotPDU{},
|
|
NewGenericFSDError(SyntaxError, "", "invalid CID"),
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
// Perform the parsing
|
|
pdu := DeletePilotPDU{}
|
|
err := pdu.Parse(tc.packet)
|
|
|
|
// Check the error
|
|
if tc.wantErr != nil {
|
|
if strings.Contains(tc.wantErr.Error(), "validation error") {
|
|
assert.Contains(t, err.Error(), "validation error")
|
|
} else {
|
|
assert.EqualError(t, err, tc.wantErr.Error())
|
|
}
|
|
} else {
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
// Verify the result
|
|
assert.Equal(t, tc.want, &pdu)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDeletePilotPDU_Serialize(t *testing.T) {
|
|
V = validator.New(validator.WithRequiredStructEnabled())
|
|
|
|
tests := []struct {
|
|
name string
|
|
pdu DeletePilotPDU
|
|
want string
|
|
}{
|
|
{
|
|
name: "Valid Serialization",
|
|
pdu: DeletePilotPDU{
|
|
From: "N123",
|
|
CID: 7654321,
|
|
},
|
|
want: "#DPN123:7654321\r\n",
|
|
},
|
|
}
|
|
|
|
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)
|
|
})
|
|
}
|
|
}
|