diff --git a/pkg/pcic/chunk.go b/pkg/pcic/chunk.go index b48d929..993abaa 100644 --- a/pkg/pcic/chunk.go +++ b/pkg/pcic/chunk.go @@ -76,8 +76,11 @@ const ( func NewChunk(options ...ChunkOption) *Chunk { chunk := &Chunk{ - metadata: "{}", - data: []byte{}, + chunkSize: offsetOfData, + headerSize: offsetOfData, + headerVersion: 2, + metadata: "{}", + data: []byte{}, } // Apply options for _, opt := range options { @@ -120,7 +123,24 @@ func (c *Chunk) Bytes() []byte { // // The binary representation is encoded in the byte slice func (c *Chunk) MarshalBinary() (data []byte, err error) { - return []byte{}, nil + blob := make([]byte, offsetOfData) + binary.LittleEndian.PutUint32( + blob, + uint32(c.chunkType), + ) + binary.LittleEndian.PutUint32( + blob[offsetOfSize:offsetOfHeaderSize], + c.chunkSize, + ) + binary.LittleEndian.PutUint32( + blob[offsetOfHeaderSize:offsetOfHeaderVersion], + c.headerSize, + ) + binary.LittleEndian.PutUint32( + blob[offsetOfHeaderVersion:offsetOfWidth], + c.headerVersion, + ) + return blob, nil } // UnmarshalBinary creates a ChunkData from a byte slice @@ -216,9 +236,6 @@ func (c *Chunk) UnmarshalBinary(data []byte) error { src := data[offsetOfData : offsetOfData+(c.chunkSize-c.headerSize)] c.data = make([]byte, len(src)) copy(c.data, src) - if src == nil { - c.data = nil - } if (c.dataWidth * c.dataHeight * byteSizeLUT[c.dataFormat]) != uint32(len(c.data)) { return fmt.Errorf( diff --git a/pkg/pcic/chunk_test.go b/pkg/pcic/chunk_test.go index 880c4de..d683769 100644 --- a/pkg/pcic/chunk_test.go +++ b/pkg/pcic/chunk_test.go @@ -181,8 +181,81 @@ func TestUnmarshalBinary(t *testing.T) { c.Bytes(), "A data size mismatch occurred", ) + + assert.Error(t, + c.UnmarshalBinary([]byte{ + 0x69, 0x00, 0x00, 0x00, /* CHUNK_TYPE */ + 0x34, 0x00, 0x00, 0x00, /* CHUNK_SIZE */ + 0x30, 0x00, 0x00, 0x00, /* HEADER_SIZE */ + 0x02, 0x00, 0x00, 0x00, /* HEADER_VERSION */ + 0x04, 0x00, 0x00, 0x00, /* IMAGE_WIDTH */ + 0x01, 0x00, 0x00, 0x00, /* IMAGE_HEIGHT */ + 0x04, 0x00, 0x00, 0x00, /* DATA_FORMAT */ + 0x00, 0x00, 0x00, 0x00, /* TIME_STAMP */ + 0x00, 0x00, 0x00, 0x00, /* FRAME_COUNT */ + 0x00, 0x00, 0x00, 0x00, /* STATUS_CODE */ + 0x00, 0x01, 0x00, 0x00, /* TIME_STAMP_SEC */ + 0x01, 0x01, 0x00, 0x00, /* TIME_STAMP_NSEC */ + 0xFF, 0xFF, 0xFF, 0xBB, /* DATA */ + 0x69, 0x00, 0x00, 0x00, /* CHUNK_TYPE of second frame*/ + }), + "The (width * height * data format) does not match the data size", + ) + + assert.Error(t, + c.UnmarshalBinary([]byte{ + 0x69, 0x00, 0x00, 0x00, /* CHUNK_TYPE */ + 0x28, 0x00, 0x00, 0x00, /* CHUNK_SIZE */ + 0x30, 0x00, 0x00, 0x00, /* HEADER_SIZE */ + 0x02, 0x00, 0x00, 0x00, /* HEADER_VERSION */ + 0x04, 0x00, 0x00, 0x00, /* IMAGE_WIDTH */ + 0x01, 0x00, 0x00, 0x00, /* IMAGE_HEIGHT */ + 0x04, 0x00, 0x00, 0x00, /* DATA_FORMAT */ + 0x00, 0x00, 0x00, 0x00, /* TIME_STAMP */ + 0x00, 0x00, 0x00, 0x00, /* FRAME_COUNT */ + 0x00, 0x00, 0x00, 0x00, /* STATUS_CODE */ + 0x00, 0x01, 0x00, 0x00, /* TIME_STAMP_SEC */ + 0x01, 0x01, 0x00, 0x00, /* TIME_STAMP_NSEC */ + 0xFF, 0xFF, 0xFF, 0xBB, /* DATA */ + 0x69, 0x00, 0x00, 0x00, /* CHUNK_TYPE of second frame*/ + }), + "The Chunk size is smaller than the expected size", + ) + + assert.Error(t, + c.UnmarshalBinary([]byte{ + 0x69, 0x00, 0x00, 0x00, /* CHUNK_TYPE */ + 0x00, 0x01, 0x00, 0x00, /* CHUNK_SIZE */ + 0x30, 0x00, 0x00, 0x00, /* HEADER_SIZE */ + 0x02, 0x00, 0x00, 0x00, /* HEADER_VERSION */ + 0x04, 0x00, 0x00, 0x00, /* IMAGE_WIDTH */ + 0x01, 0x00, 0x00, 0x00, /* IMAGE_HEIGHT */ + 0x04, 0x00, 0x00, 0x00, /* DATA_FORMAT */ + 0x00, 0x00, 0x00, 0x00, /* TIME_STAMP */ + 0x00, 0x00, 0x00, 0x00, /* FRAME_COUNT */ + 0x00, 0x00, 0x00, 0x00, /* STATUS_CODE */ + 0x00, 0x01, 0x00, 0x00, /* TIME_STAMP_SEC */ + 0x01, 0x01, 0x00, 0x00, /* TIME_STAMP_NSEC */ + 0xFF, 0xFF, 0xFF, 0xBB, /* DATA */ + 0x69, 0x00, 0x00, 0x00, /* CHUNK_TYPE of second frame*/ + }), + "The Chunk size is bigger than the data size", + ) + } func TestRoundtrip(t *testing.T) { - + chunk := pcic.NewChunk(pcic.WithChunkType(pcic.RADIAL_DISTANCE_NOISE)) + chunkData, err := chunk.MarshalBinary() + assert.NoError(t, err, "No error expected when marshalling to binary") + roundTripChunk := pcic.NewChunk() + assert.NoError(t, + roundTripChunk.UnmarshalBinary(chunkData), + "No Error expected when unmarshalling from binary", + ) + assert.Equal(t, + chunk.Type(), + roundTripChunk.Type(), + "We expect the type to be the same after the round trip", + ) }