diff --git a/internal/encoders/chunk_test.go b/internal/encoders/chunk_test.go new file mode 100644 index 0000000..bada580 --- /dev/null +++ b/internal/encoders/chunk_test.go @@ -0,0 +1,69 @@ +package encoders + +import ( + "fmt" + "reflect" + "testing" + + "github.com/jwetzell/psn-go/internal/decoders" +) + +func TestChunkEncoding(t *testing.T) { + testCases := []struct { + description string + expected []byte + chunk decoders.Chunk + }{ + { + description: "info packet", + expected: []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, + }, + chunk: decoders.Chunk{ + Header: decoders.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", + expected: []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, + }, + chunk: decoders.Chunk{ + Header: decoders.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 := EncodeChunk(testCase.chunk.Header.Id, testCase.chunk.ChunkData, testCase.chunk.Header.HasSubchunks) + + if !reflect.DeepEqual(actual, testCase.expected) { + t.Errorf("Test '%s' failed to encode chunk properly", testCase.description) + fmt.Printf("expected: %v\n", testCase.expected) + fmt.Printf("actual: %v\n", actual) + } + } +}