Files
2026-05-20 17:44:05 -05:00

138 lines
4.3 KiB
Go

package decoders
import (
"reflect"
"strings"
"testing"
"github.com/jwetzell/psn-go/internal/chunks"
)
func TestGoodChunkDecoding(t *testing.T) {
testCases := []struct {
description string
bytes []byte
expected chunks.Chunk
}{
{
description: "info packet",
bytes: []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,
},
expected: chunks.Chunk{
Header: chunks.ChunkHeader{
Id: uint16(26454),
DataLen: uint16(52),
HasSubchunks: true,
},
ChunkData: []byte{
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,
},
},
},
{
description: "data packet",
bytes: []byte{
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,
},
expected: 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 _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
actual, err := DecodeChunk(testCase.bytes)
if err != nil {
t.Errorf("failed to decode chunk properly, error: %v", err)
}
if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("failed to decode chunk properly, expected: %+v, actual: %+v", testCase.expected, actual)
}
})
}
}
func TestBadChunkDecoding(t *testing.T) {
testCases := []struct {
description string
bytes []byte
errorShouldContain string
}{
{
description: "empty packet",
bytes: []byte{},
errorShouldContain: "must be at least 4 bytes",
},
}
for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
_, err := DecodeChunk(testCase.bytes)
if err == nil {
t.Error("should have failed fail to decode chunk properly")
}
if !strings.Contains(err.Error(), testCase.errorShouldContain) {
t.Errorf("did not return the correct error expected: %s, got: %s", testCase.errorShouldContain, 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)
})
}