12 Commits

Author SHA1 Message Date
Joel Wetzell 57f7387820 return errors not log 2026-08-28 10:06:05 -05:00
Joel Wetzell 237943431d add some basic tracker tests 2026-08-28 10:01:32 -05:00
Joel Wetzell 550aaddd2f Add Codecov badge to README 2026-08-28 09:53:04 -05:00
Joel Wetzell 8c53142012 add codecov 2026-08-28 09:51:19 -05:00
Joel Wetzell e0d4b58606 mess around with fuzzing 2026-05-20 17:44:05 -05:00
Joel Wetzell 2df4cd56f7 add size check to chunk datalen 2026-05-20 17:44:01 -05:00
Joel Wetzell 145c13a976 some linting things 2026-05-17 09:25:27 -05:00
Joel Wetzell e7bdeb46ff add basic chunk benchmark 2026-05-14 21:17:51 -05:00
Joel Wetzell a79bca637c chore: go fix 2026-05-09 14:21:06 -05:00
Joel Wetzell eb8c6aba19 use t.Run for tests 2026-02-27 22:30:52 -06:00
Joel Wetzell 0b20f3ffb9 update examples 2025-12-04 17:46:58 -06:00
Joel Wetzell 98a458ca43 switch to using pointers to trackers 2025-12-04 17:46:51 -06:00
42 changed files with 579 additions and 402 deletions
+24
View File
@@ -0,0 +1,24 @@
name: Test
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- name: Set up Go
uses: actions/setup-go@v7
with:
go-version-file: "./go.mod"
- name: Run tests
run: go test -coverprofile=coverage.txt ./...
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v7
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: coverage.txt
-31
View File
@@ -1,31 +0,0 @@
name: "Test Decoders"
on:
pull_request:
types: [opened, reopened, synchronize]
paths:
- 'internal/decoders/**'
push:
branches:
- 'main'
jobs:
test_decoders:
name: "test decoders"
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version-file: "./go.mod"
- name: Test
run: go test -cover -coverprofile=decoders_coverage.txt ./internal/decoders/...
- name: Archive code coverage results
uses: actions/upload-artifact@v4
with:
name: decoders-code-coverage
path: decoders_coverage.txt
-31
View File
@@ -1,31 +0,0 @@
name: "Test Encoders"
on:
pull_request:
types: [opened, reopened, synchronize]
paths:
- 'internal/encoders/**'
push:
branches:
- 'main'
jobs:
test_encoders:
name: "test encoders"
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version-file: "./go.mod"
- name: Test
run: go test -cover -coverprofile=encoders_coverage.txt ./internal/encoders/...
- name: Archive code coverage results
uses: actions/upload-artifact@v4
with:
name: encoders-code-coverage
path: encoders_coverage.txt
+1
View File
@@ -1,2 +1,3 @@
[![codecov](https://codecov.io/gh/jwetzell/psn-go/graph/badge.svg?token=SEW8PCSPY9)](https://codecov.io/gh/jwetzell/psn-go)
# psn-go # psn-go
Golang implementation of the [PosiStageNet protocol](https://github.com/vyv/psn-cpp/blob/master/doc/PosiStageNetprotocol_v2.03_2019_09_09.pdf) Golang implementation of the [PosiStageNet protocol](https://github.com/vyv/psn-cpp/blob/master/doc/PosiStageNetprotocol_v2.03_2019_09_09.pdf)
+3 -4
View File
@@ -6,8 +6,6 @@ import (
) )
type Decoder struct { type Decoder struct {
lastInfoPacketHeader *chunks.PacketHeaderChunk
lastDataPacketHeader *chunks.PacketHeaderChunk
infoPacketFrames map[uint8][]chunks.InfoPacketChunk infoPacketFrames map[uint8][]chunks.InfoPacketChunk
dataPacketFrames map[uint8][]chunks.DataPacketChunk dataPacketFrames map[uint8][]chunks.DataPacketChunk
Trackers map[uint16]*Tracker Trackers map[uint16]*Tracker
@@ -58,7 +56,8 @@ func (d *Decoder) Decode(bytes []byte) error {
return err return err
} }
if chunk.Header.Id == 0x6756 { switch chunk.Header.Id {
case 0x6756:
infoPacket, err := decoders.DecodeInfoPacketChunk(bytes) infoPacket, err := decoders.DecodeInfoPacketChunk(bytes)
if err != nil { if err != nil {
return err return err
@@ -76,7 +75,7 @@ func (d *Decoder) Decode(bytes []byte) error {
d.updateInfo(d.infoPacketFrames[currentInfoPacketHeader.Data.FrameId]) d.updateInfo(d.infoPacketFrames[currentInfoPacketHeader.Data.FrameId])
delete(d.infoPacketFrames, currentInfoPacketHeader.Data.FrameId) delete(d.infoPacketFrames, currentInfoPacketHeader.Data.FrameId)
} }
} else if chunk.Header.Id == 0x6755 { case 0x6755:
dataPacket, err := decoders.DecodeDataPacketChunk(bytes) dataPacket, err := decoders.DecodeDataPacketChunk(bytes)
if err != nil { if err != nil {
return err return err
+2 -2
View File
@@ -18,7 +18,7 @@ func (e *Encoder) ResetInfoFrameId() {
e.infoFrameId = 1 e.infoFrameId = 1
} }
func (e *Encoder) GetInfoPackets(timestamp uint64, trackers []Tracker) [][]byte { func (e *Encoder) GetInfoPackets(timestamp uint64, trackers []*Tracker) [][]byte {
systemNameChunk := encoders.EncodeInfoSystemNameChunk(e.SystemName) systemNameChunk := encoders.EncodeInfoSystemNameChunk(e.SystemName)
trackerChunks := [][]byte{} trackerChunks := [][]byte{}
@@ -58,7 +58,7 @@ func (e *Encoder) GetInfoPackets(timestamp uint64, trackers []Tracker) [][]byte
return infoPackets return infoPackets
} }
func (e *Encoder) GetDataPackets(timestamp uint64, trackers []Tracker) [][]byte { func (e *Encoder) GetDataPackets(timestamp uint64, trackers []*Tracker) [][]byte {
trackerChunks := [][]byte{} trackerChunks := [][]byte{}
for _, tracker := range trackers { for _, tracker := range trackers {
+16 -10
View File
@@ -7,11 +7,11 @@ import (
"github.com/jwetzell/psn-go" "github.com/jwetzell/psn-go"
) )
func getNTrackers(n int) []psn.Tracker { func getNTrackers(n int) []*psn.Tracker {
trackers := []psn.Tracker{} trackers := []*psn.Tracker{}
for index := 0; index < n; index++ { for index := range n {
tracker := psn.Tracker{Id: uint16(index), Name: "Tracker"} tracker := &psn.Tracker{Id: uint16(index), Name: "Tracker"}
tracker.SetPos(0, 0, 0) tracker.SetPos(0, 0, 0)
tracker.SetSpeed(0, 0, 0) tracker.SetSpeed(0, 0, 0)
tracker.SetOri(0, 0, 0) tracker.SetOri(0, 0, 0)
@@ -61,31 +61,37 @@ func benchmark(trackerCount int, iterations int, encoder psn.Encoder, decoder ps
latestEncodedPackets := [][]byte{} latestEncodedPackets := [][]byte{}
for index := 0; index < iterations; index++ { for range iterations {
latestEncodedPackets = encoder.GetDataPackets(uint64(timestamp), trackers) latestEncodedPackets = encoder.GetDataPackets(uint64(timestamp), trackers)
} }
benchmarkResults.data.encode = float64(time.Now().UnixMicro()-dataEncoderStart) / 1000.0 benchmarkResults.data.encode = float64(time.Now().UnixMicro()-dataEncoderStart) / 1000.0
_ = latestEncodedPackets _ = latestEncodedPackets
dataDecodedStart := time.Now().UnixMicro() dataDecodedStart := time.Now().UnixMicro()
for index := 0; index < iterations; index++ { for range iterations {
for _, packet := range latestEncodedPackets { for _, packet := range latestEncodedPackets {
decoder.Decode(packet) err := decoder.Decode(packet)
if err != nil {
fmt.Printf("failed to decode packet, error: %v\n", err)
}
} }
} }
benchmarkResults.data.decode = float64(time.Now().UnixMicro()-dataDecodedStart) / 1000.0 benchmarkResults.data.decode = float64(time.Now().UnixMicro()-dataDecodedStart) / 1000.0
// INFO // INFO
infoEncoderStart := time.Now().UnixMicro() infoEncoderStart := time.Now().UnixMicro()
for index := 0; index < iterations; index++ { for range iterations {
latestEncodedPackets = encoder.GetInfoPackets(uint64(timestamp), trackers) latestEncodedPackets = encoder.GetInfoPackets(uint64(timestamp), trackers)
} }
benchmarkResults.info.encode = float64(time.Now().UnixMicro()-infoEncoderStart) / 1000.0 benchmarkResults.info.encode = float64(time.Now().UnixMicro()-infoEncoderStart) / 1000.0
infoDecodeStart := time.Now().UnixMicro() infoDecodeStart := time.Now().UnixMicro()
for index := 0; index < iterations; index++ { for range iterations {
for _, packet := range latestEncodedPackets { for _, packet := range latestEncodedPackets {
decoder.Decode(packet) err := decoder.Decode(packet)
if err != nil {
fmt.Printf("failed to decode packet, error: %v\n", err)
}
} }
} }
benchmarkResults.info.decode = float64(time.Now().UnixMicro()-infoDecodeStart) / 1000.0 benchmarkResults.info.decode = float64(time.Now().UnixMicro()-infoDecodeStart) / 1000.0
+2 -2
View File
@@ -13,14 +13,14 @@ func main() {
VersionLow: 3, VersionLow: 3,
} }
tracker := psn.Tracker{ tracker := &psn.Tracker{
Id: 1, Id: 1,
Name: "Tracker 1", Name: "Tracker 1",
} }
tracker.SetPos(1.0, 1.0, 1.0) tracker.SetPos(1.0, 1.0, 1.0)
trackers := []psn.Tracker{ trackers := []*psn.Tracker{
tracker, tracker,
} }
+43 -35
View File
@@ -23,62 +23,70 @@ func main() {
VersionLow: 0, VersionLow: 0,
} }
trackers := []psn.Tracker{} trackers := []*psn.Tracker{}
trackers = append(trackers, psn.Tracker{Id: 0, Name: "Sun"}) trackers = append(trackers, &psn.Tracker{Id: 0, Name: "Sun"})
trackers = append(trackers, psn.Tracker{Id: 1, Name: "Mercury"}) trackers = append(trackers, &psn.Tracker{Id: 1, Name: "Mercury"})
trackers = append(trackers, psn.Tracker{Id: 2, Name: "Venus"}) trackers = append(trackers, &psn.Tracker{Id: 2, Name: "Venus"})
trackers = append(trackers, psn.Tracker{Id: 3, Name: "Earth"}) trackers = append(trackers, &psn.Tracker{Id: 3, Name: "Earth"})
trackers = append(trackers, psn.Tracker{Id: 4, Name: "Mars"}) trackers = append(trackers, &psn.Tracker{Id: 4, Name: "Mars"})
trackers = append(trackers, psn.Tracker{Id: 5, Name: "Jupiter"}) trackers = append(trackers, &psn.Tracker{Id: 5, Name: "Jupiter"})
trackers = append(trackers, psn.Tracker{Id: 6, Name: "Saturn"}) trackers = append(trackers, &psn.Tracker{Id: 6, Name: "Saturn"})
trackers = append(trackers, psn.Tracker{Id: 7, Name: "Uranus"}) trackers = append(trackers, &psn.Tracker{Id: 7, Name: "Uranus"})
trackers = append(trackers, psn.Tracker{Id: 8, Name: "Neptune"}) trackers = append(trackers, &psn.Tracker{Id: 8, Name: "Neptune"})
trackers = append(trackers, psn.Tracker{Id: 9, Name: "Pluto"}) trackers = append(trackers, &psn.Tracker{Id: 9, Name: "Pluto"})
orbits := []float32{1.0, 88.0, 224.7, 365.2, 687, 4332, 10760, 30700, 60200, 90600} orbits := []float32{1.0, 88.0, 224.7, 365.2, 687, 4332, 10760, 30700, 60200, 90600}
distFromSun := []float32{0, 0.58, 1.08, 1.5, 2.28, 7.78, 14.29, 28.71, 45.04, 59.13} distFromSun := []float32{0, 0.58, 1.08, 1.5, 2.28, 7.78, 14.29, 28.71, 45.04, 59.13}
timestamp := 0 timestamp := 0
lastInfoMillis := time.Now().UnixMilli() dataTicker := time.NewTicker(time.Millisecond * 16)
lastDataMillis := time.Now().UnixMilli() infoTicker := time.NewTicker(time.Millisecond * 1000)
go func() {
for { for {
if (time.Now().UnixMilli() - lastInfoMillis) > 500 { for index, tracker := range trackers {
slog.Info("Sending Info Packets") orbit := orbits[index]
infoPackets := encoder.GetInfoPackets(uint64(timestamp), trackers)
for _, infoPacket := range infoPackets {
client.Write(infoPacket)
}
lastInfoMillis = time.Now().UnixMilli()
}
if (time.Now().UnixMilli() - lastDataMillis) > 5 {
for index, orbit := range orbits {
a := 1.0 / orbit a := 1.0 / orbit
b := distFromSun[index] b := distFromSun[index]
x := timestamp x := timestamp
cb := math.Cos(float64(a*float32(x))) * float64(b) cb := math.Cos(float64(a*float32(x))) * float64(b)
sb := math.Sin(float64(a*float32(x))) * float64(b) sb := math.Sin(float64(a*float32(x))) * float64(b)
trackers[index].SetPos(float32(sb), 0, float32(cb)) tracker.SetPos(float32(sb), 0, float32(cb))
trackers[index].SetSpeed(a*float32(cb), 0, -a*float32(sb)) tracker.SetSpeed(a*float32(cb), 0, -a*float32(sb))
trackers[index].SetOri(0, float32(x)/1000.0, 0) tracker.SetOri(0, float32(x)/1000.0, 0)
trackers[index].SetAccel(-a*a*float32(sb), 0, -a*a*float32(cb)) tracker.SetAccel(-a*a*float32(sb), 0, -a*a*float32(cb))
trackers[index].SetTrgtPos(3, 14, 16) tracker.SetTrgtPos(3, 14, 16)
trackers[index].SetStatus(float32(index) / 10.0) tracker.SetStatus(float32(index) / 10.0)
trackers[index].SetTimestamp(uint64(timestamp)) tracker.SetTimestamp(uint64(timestamp))
} }
time.Sleep(time.Millisecond * 16)
}
}()
for {
select {
case <-infoTicker.C:
slog.Info("Sending Info Packets")
infoPackets := encoder.GetInfoPackets(uint64(timestamp), trackers)
for _, infoPacket := range infoPackets {
_, err := client.Write(infoPacket)
if err != nil {
slog.Error("failed to send info packet", "error", err)
}
}
case <-dataTicker.C:
slog.Info("Sending Data Packets") slog.Info("Sending Data Packets")
dataPackets := encoder.GetDataPackets(uint64(timestamp), trackers) dataPackets := encoder.GetDataPackets(uint64(timestamp), trackers)
for _, DataPacket := range dataPackets { for _, dataPacket := range dataPackets {
client.Write(DataPacket) _, err := client.Write(dataPacket)
if err != nil {
slog.Error("failed to send data packet", "error", err)
}
} }
lastDataMillis = time.Now().UnixMilli()
timestamp += 1 timestamp += 1
} }
} }
} }
+4
View File
@@ -30,6 +30,10 @@ func DecodeChunk(bytes []byte) (chunks.Chunk, error) {
HasSubchunks: has_subchunks, HasSubchunks: has_subchunks,
} }
if len(bytes) < 4+int(header.DataLen) {
return chunks.Chunk{}, errors.New("chunk data length is greater than the number of bytes")
}
chunk_data := bytes[4 : 4+header.DataLen] chunk_data := bytes[4 : 4+header.DataLen]
return chunks.Chunk{ return chunks.Chunk{
+44 -12
View File
@@ -1,7 +1,6 @@
package decoders package decoders
import ( import (
"fmt"
"reflect" "reflect"
"strings" "strings"
"testing" "testing"
@@ -58,19 +57,17 @@ func TestGoodChunkDecoding(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
actual, err := DecodeChunk(testCase.bytes) actual, err := DecodeChunk(testCase.bytes)
if err != nil { if err != nil {
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description) t.Errorf("failed to decode chunk properly, error: %v", err)
fmt.Println(err)
} }
if !reflect.DeepEqual(actual, testCase.expected) { if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description) t.Errorf("failed to decode chunk properly, expected: %+v, actual: %+v", testCase.expected, actual)
fmt.Printf("expected: %v\n", testCase.expected)
fmt.Printf("actual: %v\n", actual)
} }
})
} }
} }
@@ -88,18 +85,53 @@ func TestBadChunkDecoding(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
_, err := DecodeChunk(testCase.bytes) _, err := DecodeChunk(testCase.bytes)
if err == nil { if err == nil {
t.Errorf("Test '%s' should have failed fail to decode chunk properly", testCase.description) t.Error("should have failed fail to decode chunk properly")
} }
if !strings.Contains(err.Error(), testCase.errorShouldContain) { if !strings.Contains(err.Error(), testCase.errorShouldContain) {
t.Errorf("Test '%s' did not return the correct error", testCase.description) t.Errorf("did not return the correct error expected: %s, got: %s", testCase.errorShouldContain, err.Error())
fmt.Printf("expected: %v\n", testCase.errorShouldContain) }
fmt.Printf("actual: %v\n", err.Error()) })
}
} }
func BenchmarkChunkDecoding(b *testing.B) {
data := []byte{
0x56, 0x67, 0x34, 0x80, 0x00, 0x00, 0x0c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x01,
0x01, 0x01, 0x00, 0x0b, 0x00, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x4e, 0x61, 0x6d, 0x65, 0x02, 0x00, 0x11,
0x80, 0x01, 0x00, 0x0d, 0x80, 0x00, 0x00, 0x09, 0x00, 0x54, 0x72, 0x61, 0x63, 0x6b, 0x65, 0x72, 0x20, 0x31,
}
for b.Loop() {
_, err := DecodeChunk(data)
if err != nil {
b.Errorf("failed to decode chunk properly, error: %v", err)
} }
} }
}
func FuzzChunkDecoding(f *testing.F) {
seedCases := [][]byte{
{
0x56, 0x67, 0x34, 0x80, 0x00, 0x00, 0x0c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x01,
0x01, 0x01, 0x00, 0x0b, 0x00, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x4e, 0x61, 0x6d, 0x65, 0x02, 0x00, 0x11,
0x80, 0x01, 0x00, 0x0d, 0x80, 0x00, 0x00, 0x09, 0x00, 0x54, 0x72, 0x61, 0x63, 0x6b, 0x65, 0x72, 0x20, 0x31,
},
{
0x55, 0x67, 0x28, 0x80, 0x00, 0x00, 0x0c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x01,
0x01, 0x01, 0x00, 0x14, 0x80, 0x01, 0x00, 0x10, 0x80, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x00, 0x00,
0x80, 0x3f, 0x00, 0x00, 0x80, 0x3f,
},
}
for _, seed := range seedCases {
f.Add(seed)
}
f.Fuzz(func(t *testing.T, bytes []byte) {
_, _ = DecodeChunk(bytes)
})
}
+2 -2
View File
@@ -2,7 +2,7 @@ package decoders
import ( import (
"encoding/binary" "encoding/binary"
"log/slog" "fmt"
"github.com/jwetzell/psn-go/internal/chunks" "github.com/jwetzell/psn-go/internal/chunks"
) )
@@ -43,7 +43,7 @@ func DecodeDataPacketChunk(bytes []byte) (chunks.DataPacketChunk, error) {
} }
default: default:
slog.Error("unhandled info packet id", "id", id) return chunks.DataPacketChunk{}, fmt.Errorf("unknown data packet ID: 0x%04x", id)
} }
} }
} }
+5 -8
View File
@@ -1,7 +1,6 @@
package decoders package decoders
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@@ -155,18 +154,16 @@ func TestGoodDataPacketChunkDecoding(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
actual, err := DecodeDataPacketChunk(testCase.bytes) actual, err := DecodeDataPacketChunk(testCase.bytes)
if err != nil { if err != nil {
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description) t.Errorf("failed to decode chunk properly, error: %v", err)
fmt.Println(err)
} }
if !reflect.DeepEqual(actual, testCase.expected) { if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description) t.Errorf("failed to decode chunk properly, expected: %+v, actual: %+v", testCase.expected, actual)
fmt.Printf("expected: %+v\n", testCase.expected) }
fmt.Printf("actual: %+v\n", actual) })
}
} }
} }
+2 -2
View File
@@ -2,7 +2,7 @@ package decoders
import ( import (
"encoding/binary" "encoding/binary"
"log/slog" "fmt"
"github.com/jwetzell/psn-go/internal/chunks" "github.com/jwetzell/psn-go/internal/chunks"
) )
@@ -93,7 +93,7 @@ func DecodeDataTrackerChunk(bytes []byte) (chunks.DataTrackerChunk, error) {
} }
default: default:
offset = int(chunk.Header.DataLen) offset = int(chunk.Header.DataLen)
slog.Error("unhandled data tracker packet chunk id", "id", id) return chunks.DataTrackerChunk{}, fmt.Errorf("unknown data tracker chunk ID: 0x%04x", id)
} }
} }
} }
+5 -8
View File
@@ -1,7 +1,6 @@
package decoders package decoders
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@@ -111,18 +110,16 @@ func TestGoodDataTrackerChunkDecoding(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
actual, err := DecodeDataTrackerChunk(testCase.bytes) actual, err := DecodeDataTrackerChunk(testCase.bytes)
if err != nil { if err != nil {
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description) t.Errorf("failed to decode chunk properly, error: %v", err)
fmt.Println(err)
} }
if !reflect.DeepEqual(actual, testCase.expected) { if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description) t.Errorf("failed to decode chunk properly, expected: %+v, actual: %+v", testCase.expected, actual)
fmt.Printf("expected: %+v\n", testCase.expected) }
fmt.Printf("actual: %+v\n", actual) })
}
} }
} }
@@ -1,7 +1,6 @@
package decoders package decoders
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@@ -126,18 +125,16 @@ func TestGoodDataTrackerListChunkDecoding(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
actual, err := DecodeDataTrackerListChunk(testCase.bytes) actual, err := DecodeDataTrackerListChunk(testCase.bytes)
if err != nil { if err != nil {
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description) t.Errorf("failed to decode chunk properly, error: %v", err)
fmt.Println(err)
} }
if !reflect.DeepEqual(actual, testCase.expected) { if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description) t.Errorf("failed to decode chunk properly, expected: %+v, actual: %+v", testCase.expected, actual)
fmt.Printf("expected: %+v\n", testCase.expected) }
fmt.Printf("actual: %+v\n", actual) })
}
} }
} }
@@ -1,7 +1,6 @@
package decoders package decoders
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@@ -31,19 +30,17 @@ func TestGoodDataTrackerStatusChunk(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
actual, err := DecodeDataTrackerStatusChunk(testCase.bytes) actual, err := DecodeDataTrackerStatusChunk(testCase.bytes)
if err != nil { if err != nil {
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description) t.Errorf("failed to decode chunk properly, error: %v", err)
fmt.Println(err)
} }
if !reflect.DeepEqual(actual, testCase.expected) { if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description) t.Errorf("failed to decode chunk properly, expected: %+v, actual: %+v", testCase.expected, actual)
fmt.Printf("expected: %v\n", testCase.expected)
fmt.Printf("actual: %v\n", actual)
} }
})
} }
} }
@@ -1,7 +1,6 @@
package decoders package decoders
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@@ -32,18 +31,17 @@ func TestGoodDataTrackerTimestampChunk(t *testing.T) {
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
actual, err := DecodeDataTrackerTimestampChunk(testCase.bytes) actual, err := DecodeDataTrackerTimestampChunk(testCase.bytes)
if err != nil { if err != nil {
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description) t.Errorf("failed to decode chunk properly, error: %v", err)
fmt.Println(err)
} }
if !reflect.DeepEqual(actual, testCase.expected) { if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description) t.Errorf("failed to decode chunk properly, expected: %+v, actual: %+v", testCase.expected, actual)
fmt.Printf("expected: %v\n", testCase.expected)
fmt.Printf("actual: %v\n", actual)
} }
})
} }
} }
@@ -1,7 +1,6 @@
package decoders package decoders
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@@ -93,19 +92,17 @@ func TestGoodDataTrackerXYZChunk(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
actual, err := DecodeDataTrackerXYZChunk(testCase.bytes) actual, err := DecodeDataTrackerXYZChunk(testCase.bytes)
if err != nil { if err != nil {
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description) t.Errorf("failed to decode chunk properly, error: %v", err)
fmt.Println(err)
} }
if !reflect.DeepEqual(actual, testCase.expected) { if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description) t.Errorf("failed to decode chunk properly, expected: %+v, actual: %+v", testCase.expected, actual)
fmt.Printf("expected: %v\n", testCase.expected)
fmt.Printf("actual: %v\n", actual)
} }
})
} }
} }
+2 -2
View File
@@ -2,7 +2,7 @@ package decoders
import ( import (
"encoding/binary" "encoding/binary"
"log/slog" "fmt"
"github.com/jwetzell/psn-go/internal/chunks" "github.com/jwetzell/psn-go/internal/chunks"
) )
@@ -51,7 +51,7 @@ func DecodeInfoPacketChunk(bytes []byte) (chunks.InfoPacketChunk, error) {
} }
default: default:
slog.Error("unhandled info packet id", "id", id) return chunks.InfoPacketChunk{}, fmt.Errorf("unknown info packet ID: 0x%04x", id)
} }
} }
} }
+5 -9
View File
@@ -1,7 +1,6 @@
package decoders package decoders
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@@ -134,19 +133,16 @@ func TestGoodInfoPacketChunkDecoding(t *testing.T) {
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
actual, err := DecodeInfoPacketChunk(testCase.bytes) actual, err := DecodeInfoPacketChunk(testCase.bytes)
if err != nil { if err != nil {
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description) t.Errorf("failed to decode chunk properly, error: %v", err)
fmt.Println(err)
} }
if !reflect.DeepEqual(actual, testCase.expected) { if !reflect.DeepEqual(actual, testCase.expected) {
fmt.Printf("%+v\n", actual.Data.TrackerList) t.Errorf("failed to decode chunk properly, expected: %+v, actual: %+v", testCase.expected, actual)
fmt.Printf("%+v\n", testCase.expected.Data.TrackerList) }
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description) })
// fmt.Printf("expected: %+v\n", testCase.expected)
// fmt.Printf("actual: %+v\n", actual)
}
} }
} }
@@ -1,7 +1,6 @@
package decoders package decoders
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@@ -32,18 +31,16 @@ func TestGoodInfoSystemNameChunkDecoding(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
actual, err := DecodeInfoSystemNameChunk(testCase.bytes) actual, err := DecodeInfoSystemNameChunk(testCase.bytes)
if err != nil { if err != nil {
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description) t.Errorf("failed to decode chunk properly, error: %v", err)
fmt.Println(err)
} }
if !reflect.DeepEqual(actual, testCase.expected) { if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description) t.Errorf("failed to decode chunk properly, expected: %+v, actual: %+v", testCase.expected, actual)
fmt.Printf("expected: %+v\n", testCase.expected) }
fmt.Printf("actual: %+v\n", actual) })
}
} }
} }
+2 -2
View File
@@ -2,7 +2,7 @@ package decoders
import ( import (
"encoding/binary" "encoding/binary"
"log/slog" "fmt"
"github.com/jwetzell/psn-go/internal/chunks" "github.com/jwetzell/psn-go/internal/chunks"
) )
@@ -30,7 +30,7 @@ func DecodeInfoTrackerChunk(bytes []byte) (chunks.InfoTrackerChunk, error) {
offset = offset + int(tracker_name.Chunk.Header.DataLen) offset = offset + int(tracker_name.Chunk.Header.DataLen)
} }
default: default:
slog.Error("unhandled info tracker chunk id", "id", id) return chunks.InfoTrackerChunk{}, fmt.Errorf("unknown info tracker chunk ID: 0x%04x", id)
} }
} }
} }
+5 -8
View File
@@ -1,7 +1,6 @@
package decoders package decoders
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@@ -40,18 +39,16 @@ func TestGoodInfoTrackerChunkDecoding(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
actual, err := DecodeInfoTrackerChunk(testCase.bytes) actual, err := DecodeInfoTrackerChunk(testCase.bytes)
if err != nil { if err != nil {
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description) t.Errorf("failed to decode chunk properly, error: %v", err)
fmt.Println(err)
} }
if !reflect.DeepEqual(actual, testCase.expected) { if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description) t.Errorf("failed to decode chunk properly, expected: %+v, actual: %+v", testCase.expected, actual)
fmt.Printf("expected: %+v\n", testCase.expected) }
fmt.Printf("actual: %+v\n", actual) })
}
} }
} }
@@ -1,7 +1,6 @@
package decoders package decoders
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@@ -50,18 +49,16 @@ func TestGoodInfoTrackerListChunkDecoding(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
actual, err := DecodeInfoTrackerListChunk(testCase.bytes) actual, err := DecodeInfoTrackerListChunk(testCase.bytes)
if err != nil { if err != nil {
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description) t.Errorf("failed to decode chunk properly, error: %v", err)
fmt.Println(err)
} }
if !reflect.DeepEqual(actual, testCase.expected) { if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description) t.Errorf("failed to decode chunk properly, expected: %+v, actual: %+v", testCase.expected, actual)
fmt.Printf("expected: %+v\n", testCase.expected) }
fmt.Printf("actual: %+v\n", actual) })
}
} }
} }
@@ -1,7 +1,6 @@
package decoders package decoders
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@@ -33,17 +32,16 @@ func TestGoodInfoTrackerNameChunkDecoding(t *testing.T) {
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
actual, err := DecodeInfoTrackerNameChunk(testCase.bytes) actual, err := DecodeInfoTrackerNameChunk(testCase.bytes)
if err != nil { if err != nil {
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description) t.Errorf("failed to decode chunk properly, error: %v", err)
fmt.Println(err)
} }
if !reflect.DeepEqual(actual, testCase.expected) { if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description) t.Errorf("failed to decode chunk properly, expected: %+v, actual: %+v", testCase.expected, actual)
fmt.Printf("expected: %+v\n", testCase.expected) }
fmt.Printf("actual: %+v\n", actual) })
}
} }
} }
@@ -1,7 +1,6 @@
package decoders package decoders
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@@ -36,18 +35,16 @@ func TestGoodPacketHeaderChunkDecoding(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
actual, err := DecodePacketHeaderChunk(testCase.bytes) actual, err := DecodePacketHeaderChunk(testCase.bytes)
if err != nil { if err != nil {
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description) t.Errorf("failed to decode chunk properly, error: %v", err)
fmt.Println(err)
} }
if !reflect.DeepEqual(actual, testCase.expected) { if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description) t.Errorf("failed to decode chunk properly, expected: %+v, actual: %+v", testCase.expected, actual)
fmt.Printf("expected: %+v\n", testCase.expected) }
fmt.Printf("actual: %+v\n", actual) })
}
} }
} }
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0000")
+21 -5
View File
@@ -1,7 +1,6 @@
package encoders package encoders
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@@ -57,13 +56,30 @@ func TestChunkEncoding(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
actual := EncodeChunk(testCase.chunk.Header.Id, testCase.chunk.ChunkData, testCase.chunk.Header.HasSubchunks) actual := EncodeChunk(testCase.chunk.Header.Id, testCase.chunk.ChunkData, testCase.chunk.Header.HasSubchunks)
if !reflect.DeepEqual(actual, testCase.expected) { if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to encode chunk properly", testCase.description) t.Errorf("failed to encode chunk properly, expected: %v, actual: %v\n", testCase.expected, actual)
fmt.Printf("expected: %v\n", testCase.expected) }
fmt.Printf("actual: %v\n", actual) })
} }
} }
func BenchmarkChunkEncoding(b *testing.B) {
chunk := chunks.Chunk{
Header: chunks.ChunkHeader{
Id: uint16(26453),
DataLen: uint16(40),
HasSubchunks: true,
},
ChunkData: []byte{
0x00, 0x00, 0x0c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x01,
0x01, 0x01, 0x00, 0x14, 0x80, 0x01, 0x00, 0x10, 0x80, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x00, 0x00,
0x80, 0x3f, 0x00, 0x00, 0x80, 0x3f,
},
}
for b.Loop() {
EncodeChunk(chunk.Header.Id, chunk.ChunkData, chunk.Header.HasSubchunks)
}
} }
@@ -1,7 +1,6 @@
package encoders package encoders
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@@ -28,13 +27,12 @@ func TestDataTrackerAccelChunkEncoding(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
actual := EncodeDataTrackerAccelChunk(testCase.data.X, testCase.data.Y, testCase.data.Z) actual := EncodeDataTrackerAccelChunk(testCase.data.X, testCase.data.Y, testCase.data.Z)
if !reflect.DeepEqual(actual, testCase.expected) { if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to encode chunk properly", testCase.description) t.Errorf("failed to encode chunk properly, expected: %v, actual: %v\n", testCase.expected, actual)
fmt.Printf("expected: %v\n", testCase.expected) }
fmt.Printf("actual: %v\n", actual) })
}
} }
} }
@@ -1,7 +1,6 @@
package encoders package encoders
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@@ -28,13 +27,12 @@ func TestDataTrackerOriChunkEncoding(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
actual := EncodeDataTrackerOriChunk(testCase.data.X, testCase.data.Y, testCase.data.Z) actual := EncodeDataTrackerOriChunk(testCase.data.X, testCase.data.Y, testCase.data.Z)
if !reflect.DeepEqual(actual, testCase.expected) { if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to encode chunk properly", testCase.description) t.Errorf("failed to encode chunk properly, expected: %v, actual: %v\n", testCase.expected, actual)
fmt.Printf("expected: %v\n", testCase.expected) }
fmt.Printf("actual: %v\n", actual) })
}
} }
} }
@@ -1,7 +1,6 @@
package encoders package encoders
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@@ -28,13 +27,12 @@ func TestDataTrackerPosChunkEncoding(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
actual := EncodeDataTrackerPosChunk(testCase.data.X, testCase.data.Y, testCase.data.Z) actual := EncodeDataTrackerPosChunk(testCase.data.X, testCase.data.Y, testCase.data.Z)
if !reflect.DeepEqual(actual, testCase.expected) { if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to encode chunk properly", testCase.description) t.Errorf("failed to encode chunk properly, expected: %v, actual: %v\n", testCase.expected, actual)
fmt.Printf("expected: %v\n", testCase.expected) }
fmt.Printf("actual: %v\n", actual) })
}
} }
} }
@@ -1,7 +1,6 @@
package encoders package encoders
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@@ -28,13 +27,12 @@ func TestDataTrackerSpeedChunkEncoding(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
actual := EncodeDataTrackerSpeedChunk(testCase.data.X, testCase.data.Y, testCase.data.Z) actual := EncodeDataTrackerSpeedChunk(testCase.data.X, testCase.data.Y, testCase.data.Z)
if !reflect.DeepEqual(actual, testCase.expected) { if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to encode chunk properly", testCase.description) t.Errorf("failed to encode chunk properly, expected: %v, actual: %v\n", testCase.expected, actual)
fmt.Printf("expected: %v\n", testCase.expected) }
fmt.Printf("actual: %v\n", actual) })
}
} }
} }
@@ -1,7 +1,6 @@
package encoders package encoders
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@@ -26,13 +25,12 @@ func TestDataTrackerStatusChunkEncoding(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
actual := EncodeDataTrackerStatusChunk(testCase.data.Validity) actual := EncodeDataTrackerStatusChunk(testCase.data.Validity)
if !reflect.DeepEqual(actual, testCase.expected) { if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to encode chunk properly", testCase.description) t.Errorf("failed to encode chunk properly, expected: %v, actual: %v\n", testCase.expected, actual)
fmt.Printf("expected: %v\n", testCase.expected) }
fmt.Printf("actual: %v\n", actual) })
}
} }
} }
@@ -1,7 +1,6 @@
package encoders package encoders
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@@ -26,13 +25,12 @@ func TestDataTrackerTimestampChunkEncoding(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
actual := EncodeDataTrackerTimestampChunk(testCase.data.Timestamp) actual := EncodeDataTrackerTimestampChunk(testCase.data.Timestamp)
if !reflect.DeepEqual(actual, testCase.expected) { if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to encode chunk properly", testCase.description) t.Errorf("failed to encode chunk properly, expected: %v, actual: %v\n", testCase.expected, actual)
fmt.Printf("expected: %v\n", testCase.expected) }
fmt.Printf("actual: %v\n", actual) })
}
} }
} }
@@ -1,7 +1,6 @@
package encoders package encoders
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@@ -28,13 +27,13 @@ func TestDataTrackerTrgtPosChunkEncoding(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
actual := EncodeDataTrackerTrgtPosChunk(testCase.data.X, testCase.data.Y, testCase.data.Z) actual := EncodeDataTrackerTrgtPosChunk(testCase.data.X, testCase.data.Y, testCase.data.Z)
if !reflect.DeepEqual(actual, testCase.expected) { if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to encode chunk properly", testCase.description) t.Errorf("failed to encode chunk properly, expected: %v, actual: %v\n", testCase.expected, actual)
fmt.Printf("expected: %v\n", testCase.expected) }
fmt.Printf("actual: %v\n", actual) })
}
} }
} }
@@ -1,7 +1,6 @@
package encoders package encoders
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@@ -26,13 +25,12 @@ func TestInfoSystemNameChunkEncoding(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
actual := EncodeInfoSystemNameChunk(testCase.data.SystemName) actual := EncodeInfoSystemNameChunk(testCase.data.SystemName)
if !reflect.DeepEqual(actual, testCase.expected) { if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to encode chunk properly", testCase.description) t.Errorf("failed to encode chunk properly, expected: %v, actual: %v\n", testCase.expected, actual)
fmt.Printf("expected: %v\n", testCase.expected) }
fmt.Printf("actual: %v\n", actual) })
}
} }
} }
+4 -6
View File
@@ -1,7 +1,6 @@
package encoders package encoders
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@@ -40,13 +39,12 @@ func TestInfoTrackerChunkEncoding(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
actual := EncodeInfoTrackerChunk(testCase.chunk.Chunk.Header.Id, EncodeInfoTrackerNameChunk(testCase.chunk.Data.TrackerName.Data.TrackerName)) actual := EncodeInfoTrackerChunk(testCase.chunk.Chunk.Header.Id, EncodeInfoTrackerNameChunk(testCase.chunk.Data.TrackerName.Data.TrackerName))
if !reflect.DeepEqual(actual, testCase.expected) { if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to encode chunk properly", testCase.description) t.Errorf("failed to encode chunk properly, expected: %v, actual: %v\n", testCase.expected, actual)
fmt.Printf("expected: %v\n", testCase.expected) }
fmt.Printf("actual: %v\n", actual) })
}
} }
} }
@@ -1,7 +1,6 @@
package encoders package encoders
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@@ -50,7 +49,7 @@ func TestInfoTrackerListChunkEncoding(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
trackerChunks := [][]byte{} trackerChunks := [][]byte{}
for _, tracker := range testCase.chunk.Data.Trackers { for _, tracker := range testCase.chunk.Data.Trackers {
trackerChunks = append(trackerChunks, EncodeInfoTrackerChunk(tracker.Chunk.Header.Id, EncodeInfoTrackerNameChunk(tracker.Data.TrackerName.Data.TrackerName))) trackerChunks = append(trackerChunks, EncodeInfoTrackerChunk(tracker.Chunk.Header.Id, EncodeInfoTrackerNameChunk(tracker.Data.TrackerName.Data.TrackerName)))
@@ -59,9 +58,8 @@ func TestInfoTrackerListChunkEncoding(t *testing.T) {
actual := EncodeInfoTrackerListChunk(trackerChunks) actual := EncodeInfoTrackerListChunk(trackerChunks)
if !reflect.DeepEqual(actual, testCase.expected) { if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to encode chunk properly", testCase.description) t.Errorf("failed to encode chunk properly, expected: %v, actual: %v\n", testCase.expected, actual)
fmt.Printf("expected: %v\n", testCase.expected) }
fmt.Printf("actual: %v\n", actual) })
}
} }
} }
@@ -1,7 +1,6 @@
package encoders package encoders
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@@ -26,13 +25,12 @@ func TestInfoTrackerNameChunkEncoding(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
actual := EncodeInfoTrackerNameChunk(testCase.data.TrackerName) actual := EncodeInfoTrackerNameChunk(testCase.data.TrackerName)
if !reflect.DeepEqual(actual, testCase.expected) { if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to encode chunk properly", testCase.description) t.Errorf("failed to encode chunk properly, expected: %v, actual: %v\n", testCase.expected, actual)
fmt.Printf("expected: %v\n", testCase.expected) }
fmt.Printf("actual: %v\n", actual) })
}
} }
} }
@@ -1,7 +1,6 @@
package encoders package encoders
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@@ -30,13 +29,12 @@ func TestPacketHeaderChunkEncoding(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
actual := EncodePacketHeaderChunk(testCase.data.PacketTimestamp, testCase.data.VersionHigh, testCase.data.VersionLow, testCase.data.FrameId, testCase.data.FramePacketCount) actual := EncodePacketHeaderChunk(testCase.data.PacketTimestamp, testCase.data.VersionHigh, testCase.data.VersionLow, testCase.data.FrameId, testCase.data.FramePacketCount)
if !reflect.DeepEqual(actual, testCase.expected) { if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to encode chunk properly", testCase.description) t.Errorf("failed to encode chunk properly, expected: %v, actual: %v\n", testCase.expected, actual)
fmt.Printf("expected: %v\n", testCase.expected) }
fmt.Printf("actual: %v\n", actual) })
}
} }
} }
+205
View File
@@ -0,0 +1,205 @@
package psn_test
import (
"testing"
"github.com/jwetzell/psn-go"
)
func TestTrackerSetPosNilPos(t *testing.T) {
tracker := &psn.Tracker{}
tracker.SetPos(1.0, 2.0, 3.0)
if tracker.Pos == nil {
t.Errorf("Expected Pos to be initialized, but it is nil")
} else {
if tracker.Pos.X != 1.0 || tracker.Pos.Y != 2.0 || tracker.Pos.Z != 3.0 {
t.Errorf("Expected Pos to be (1.0, 2.0, 3.0), but got (%f, %f, %f)", tracker.Pos.X, tracker.Pos.Y, tracker.Pos.Z)
}
}
}
func TestTrackerSetPosNonNilPos(t *testing.T) {
tracker := &psn.Tracker{
Pos: &psn.XYZData{X: 0.0, Y: 0.0, Z: 0.0},
}
tracker.SetPos(4.0, 5.0, 6.0)
if tracker.Pos == nil {
t.Errorf("Expected Pos to be initialized, but it is nil")
} else {
if tracker.Pos.X != 4.0 || tracker.Pos.Y != 5.0 || tracker.Pos.Z != 6.0 {
t.Errorf("Expected Pos to be (4.0, 5.0, 6.0), but got (%f, %f, %f)", tracker.Pos.X, tracker.Pos.Y, tracker.Pos.Z)
}
}
}
func TestTrackerSetSpeedNilSpeed(t *testing.T) {
tracker := &psn.Tracker{}
tracker.SetSpeed(1.0, 2.0, 3.0)
if tracker.Speed == nil {
t.Errorf("Expected Speed to be initialized, but it is nil")
} else {
if tracker.Speed.X != 1.0 || tracker.Speed.Y != 2.0 || tracker.Speed.Z != 3.0 {
t.Errorf("Expected Speed to be (1.0, 2.0, 3.0), but got (%f, %f, %f)", tracker.Speed.X, tracker.Speed.Y, tracker.Speed.Z)
}
}
}
func TestTrackerSetSpeedNonNilSpeed(t *testing.T) {
tracker := &psn.Tracker{
Speed: &psn.XYZData{X: 0.0, Y: 0.0, Z: 0.0},
}
tracker.SetSpeed(4.0, 5.0, 6.0)
if tracker.Speed == nil {
t.Errorf("Expected Speed to be initialized, but it is nil")
} else {
if tracker.Speed.X != 4.0 || tracker.Speed.Y != 5.0 || tracker.Speed.Z != 6.0 {
t.Errorf("Expected Speed to be (4.0, 5.0, 6.0), but got (%f, %f, %f)", tracker.Speed.X, tracker.Speed.Y, tracker.Speed.Z)
}
}
}
func TestTrackerSetOriNilOri(t *testing.T) {
tracker := &psn.Tracker{}
tracker.SetOri(1.0, 2.0, 3.0)
if tracker.Ori == nil {
t.Errorf("Expected Ori to be initialized, but it is nil")
} else {
if tracker.Ori.X != 1.0 || tracker.Ori.Y != 2.0 || tracker.Ori.Z != 3.0 {
t.Errorf("Expected Ori to be (1.0, 2.0, 3.0), but got (%f, %f, %f)", tracker.Ori.X, tracker.Ori.Y, tracker.Ori.Z)
}
}
}
func TestTrackerSetOriNonNilOri(t *testing.T) {
tracker := &psn.Tracker{
Ori: &psn.XYZData{X: 0.0, Y: 0.0, Z: 0.0},
}
tracker.SetOri(4.0, 5.0, 6.0)
if tracker.Ori == nil {
t.Errorf("Expected Ori to be initialized, but it is nil")
} else {
if tracker.Ori.X != 4.0 || tracker.Ori.Y != 5.0 || tracker.Ori.Z != 6.0 {
t.Errorf("Expected Ori to be (4.0, 5.0, 6.0), but got (%f, %f, %f)", tracker.Ori.X, tracker.Ori.Y, tracker.Ori.Z)
}
}
}
func TestTrackerSetStatusNilStatus(t *testing.T) {
tracker := &psn.Tracker{}
tracker.SetStatus(0.75)
if tracker.Validity == nil {
t.Errorf("Expected Validity to be initialized, but it is nil")
} else {
if *tracker.Validity != 0.75 {
t.Errorf("Expected Validity to be 0.75, but got %f", *tracker.Validity)
}
}
}
func TestTrackerSetStatusNonNilStatus(t *testing.T) {
tracker := &psn.Tracker{
Validity: new(float32),
}
*tracker.Validity = 0.5
tracker.SetStatus(0.85)
if tracker.Validity == nil {
t.Errorf("Expected Validity to be initialized, but it is nil")
} else {
if *tracker.Validity != 0.85 {
t.Errorf("Expected Validity to be 0.85, but got %f", *tracker.Validity)
}
}
}
func TestTrackerSetAccelNilAccel(t *testing.T) {
tracker := &psn.Tracker{}
tracker.SetAccel(1.0, 2.0, 3.0)
if tracker.Accel == nil {
t.Errorf("Expected Accel to be initialized, but it is nil")
} else {
if tracker.Accel.X != 1.0 || tracker.Accel.Y != 2.0 || tracker.Accel.Z != 3.0 {
t.Errorf("Expected Accel to be (1.0, 2.0, 3.0), but got (%f, %f, %f)", tracker.Accel.X, tracker.Accel.Y, tracker.Accel.Z)
}
}
}
func TestTrackerSetAccelNonNilAccel(t *testing.T) {
tracker := &psn.Tracker{
Accel: &psn.XYZData{X: 0.0, Y: 0.0, Z: 0.0},
}
tracker.SetAccel(4.0, 5.0, 6.0)
if tracker.Accel == nil {
t.Errorf("Expected Accel to be initialized, but it is nil")
} else {
if tracker.Accel.X != 4.0 || tracker.Accel.Y != 5.0 || tracker.Accel.Z != 6.0 {
t.Errorf("Expected Accel to be (4.0, 5.0, 6.0), but got (%f, %f, %f)", tracker.Accel.X, tracker.Accel.Y, tracker.Accel.Z)
}
}
}
func TestTrackerSetTrgtPosNilTrgtPos(t *testing.T) {
tracker := &psn.Tracker{}
tracker.SetTrgtPos(1.0, 2.0, 3.0)
if tracker.TrgtPos == nil {
t.Errorf("Expected TrgtPos to be initialized, but it is nil")
} else {
if tracker.TrgtPos.X != 1.0 || tracker.TrgtPos.Y != 2.0 || tracker.TrgtPos.Z != 3.0 {
t.Errorf("Expected TrgtPos to be (1.0, 2.0, 3.0), but got (%f, %f, %f)", tracker.TrgtPos.X, tracker.TrgtPos.Y, tracker.TrgtPos.Z)
}
}
}
func TestTrackerSetTrgtPosNonNilTrgtPos(t *testing.T) {
tracker := &psn.Tracker{
TrgtPos: &psn.XYZData{X: 0.0, Y: 0.0, Z: 0.0},
}
tracker.SetTrgtPos(4.0, 5.0, 6.0)
if tracker.TrgtPos == nil {
t.Errorf("Expected TrgtPos to be initialized, but it is nil")
} else {
if tracker.TrgtPos.X != 4.0 || tracker.TrgtPos.Y != 5.0 || tracker.TrgtPos.Z != 6.0 {
t.Errorf("Expected TrgtPos to be (4.0, 5.0, 6.0), but got (%f, %f, %f)", tracker.TrgtPos.X, tracker.TrgtPos.Y, tracker.TrgtPos.Z)
}
}
}
func TestTrackerSetTimestampNilTimestamp(t *testing.T) {
tracker := &psn.Tracker{}
tracker.SetTimestamp(1)
if tracker.Timestamp == nil {
t.Errorf("Expected Timestamp to be initialized, but it is nil")
} else {
if *tracker.Timestamp != 1 {
t.Errorf("Expected Timestamp to be 1, but got %d", *tracker.Timestamp)
}
}
}
func TestTrackerSetTimestampNonNilTimestamp(t *testing.T) {
tracker := &psn.Tracker{
Timestamp: new(uint64),
}
*tracker.Timestamp = 5
tracker.SetTimestamp(8)
if tracker.Timestamp == nil {
t.Errorf("Expected Timestamp to be initialized, but it is nil")
} else {
if *tracker.Timestamp != 8 {
t.Errorf("Expected Timestamp to be 8, but got %d", *tracker.Timestamp)
}
}
}