Files
psn-go/internal/decoders/chunk_test.go

104 lines
3.0 KiB
Go

package decoders
import (
"fmt"
"reflect"
"strings"
"testing"
)
func TestGoodChunkDecoding(t *testing.T) {
testCases := []struct {
description string
bytes []byte
expected 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: Chunk{
Header: 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: Chunk{
Header: 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 {
actual, err := DecodeChunk(testCase.bytes)
if err != nil {
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description)
fmt.Println(err)
}
if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description)
fmt.Printf("expected: %v\n", testCase.expected)
fmt.Printf("actual: %v\n", 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 {
_, err := DecodeChunk(testCase.bytes)
if err == nil {
t.Errorf("Test '%s' should have failed fail to decode chunk properly", testCase.description)
}
if !strings.Contains(err.Error(), testCase.errorShouldContain) {
t.Errorf("Test '%s' did not return the correct error", testCase.description)
fmt.Printf("expected: %v\n", testCase.errorShouldContain)
fmt.Printf("actual: %v\n", err.Error())
}
}
}