network/frame/data_frame_test.go

40 lines
1.2 KiB
Go

package frame
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestDataFrameEncode(t *testing.T) {
var userDataTag byte = 0x15
d := NewDataFrame()
d.SetCarriage(userDataTag, []byte("hpds"))
tidBuf := []byte(d.TransactionId())
result := []byte{
0x80 | byte(TagOfDataFrame), byte(len(tidBuf) + 4 + 8 + 2),
0x80 | byte(TagOfMetaFrame), byte(len(tidBuf) + 2 + 2),
byte(TagOfTransactionId), byte(len(tidBuf))}
result = append(result, tidBuf...)
result = append(result, byte(TagOfSourceId), 0x0)
result = append(result, 0x80|byte(TagOfPayloadFrame), 0x06,
userDataTag, 0x04, 0x68, 0x70, 0x64, 0x73)
assert.Equal(t, result, d.Encode())
}
func TestDataFrameDecode(t *testing.T) {
var userDataTag byte = 0x15
buf := []byte{
0x80 | byte(TagOfDataFrame), 0x10,
0x80 | byte(TagOfMetaFrame), 0x06,
byte(TagOfTransactionId), 0x04, 0x31, 0x32, 0x33, 0x34,
0x80 | byte(TagOfPayloadFrame), 0x06,
userDataTag, 0x04, 0x68, 0x70, 0x64, 0x73}
data, err := DecodeToDataFrame(buf)
assert.NoError(t, err)
assert.EqualValues(t, "1234", data.TransactionId())
assert.EqualValues(t, userDataTag, data.GetDataTag())
assert.EqualValues(t, []byte("hpds"), data.GetCarriage())
}