|
| 1 | +package wire |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func TestCreateStream(t *testing.T) { |
| 13 | + pver := ProtocolVersion |
| 14 | + |
| 15 | + assocID := []byte{ |
| 16 | + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 17 | + 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, |
| 18 | + } |
| 19 | + msg := NewMsgCreateStream(assocID, StreamTypeData1, "BlockPriority") |
| 20 | + |
| 21 | + assert.Equal(t, assocID, msg.AssociationID) |
| 22 | + assert.Equal(t, StreamTypeData1, msg.StreamType) |
| 23 | + assert.Equal(t, "BlockPriority", msg.StreamPolicyName) |
| 24 | + |
| 25 | + assertCommand(t, msg, "createstrm") |
| 26 | + |
| 27 | + wantPayload := uint64(MaxVarIntPayload + MaxAssociationIDLen + 1 + MaxVarIntPayload + MaxUserAgentLen) |
| 28 | + assertMaxPayload(t, msg, pver, wantPayload) |
| 29 | + |
| 30 | + // Roundtrip |
| 31 | + dst := &MsgCreateStream{} |
| 32 | + assertWireRoundTrip(t, msg, dst, pver, BaseEncoding) |
| 33 | +} |
| 34 | + |
| 35 | +func TestCreateStreamEncodeDecode(t *testing.T) { |
| 36 | + pver := ProtocolVersion |
| 37 | + enc := BaseEncoding |
| 38 | + |
| 39 | + assocID := []byte{ |
| 40 | + 0x01, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, |
| 41 | + 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, |
| 42 | + } |
| 43 | + |
| 44 | + msg := NewMsgCreateStream(assocID, StreamTypeData1, "BlockPriority") |
| 45 | + |
| 46 | + var buf bytes.Buffer |
| 47 | + require.NoError(t, msg.BsvEncode(&buf, pver, enc)) |
| 48 | + |
| 49 | + decoded := &MsgCreateStream{} |
| 50 | + require.NoError(t, decoded.Bsvdecode(&buf, pver, enc)) |
| 51 | + |
| 52 | + assert.Equal(t, msg.AssociationID, decoded.AssociationID) |
| 53 | + assert.Equal(t, msg.StreamType, decoded.StreamType) |
| 54 | + assert.Equal(t, msg.StreamPolicyName, decoded.StreamPolicyName) |
| 55 | +} |
| 56 | + |
| 57 | +func TestCreateStreamEmptyAssocID(t *testing.T) { |
| 58 | + pver := ProtocolVersion |
| 59 | + enc := BaseEncoding |
| 60 | + |
| 61 | + msg := NewMsgCreateStream(nil, StreamTypeData1, "BlockPriority") |
| 62 | + |
| 63 | + var buf bytes.Buffer |
| 64 | + err := msg.BsvEncode(&buf, pver, enc) |
| 65 | + assert.Error(t, err) |
| 66 | +} |
| 67 | + |
| 68 | +func TestCreateStreamLargeAssocID(t *testing.T) { |
| 69 | + pver := ProtocolVersion |
| 70 | + enc := BaseEncoding |
| 71 | + |
| 72 | + largeID := make([]byte, MaxAssociationIDLen+1) |
| 73 | + msg := NewMsgCreateStream(largeID, StreamTypeData1, "BlockPriority") |
| 74 | + |
| 75 | + var buf bytes.Buffer |
| 76 | + err := msg.BsvEncode(&buf, pver, enc) |
| 77 | + assert.Error(t, err) |
| 78 | +} |
| 79 | + |
| 80 | +func TestCreateStreamWireErrors(t *testing.T) { |
| 81 | + pver := ProtocolVersion |
| 82 | + |
| 83 | + assocID := []byte{0x01, 0x02, 0x03} |
| 84 | + msg := NewMsgCreateStream(assocID, StreamTypeGeneral, "Default") |
| 85 | + |
| 86 | + tests := []struct { |
| 87 | + in *MsgCreateStream |
| 88 | + buf []byte |
| 89 | + pver uint32 |
| 90 | + enc MessageEncoding |
| 91 | + max int |
| 92 | + writeErr error |
| 93 | + readErr error |
| 94 | + }{ |
| 95 | + // Short write/read at association ID varint. |
| 96 | + {msg, []byte{}, pver, BaseEncoding, 0, io.ErrShortWrite, io.EOF}, |
| 97 | + } |
| 98 | + |
| 99 | + for _, test := range tests { |
| 100 | + assertWireError(t, test.in, &MsgCreateStream{}, test.buf, test.pver, |
| 101 | + test.enc, test.max, test.writeErr, test.readErr) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func TestCreateStreamMakeEmptyMessage(t *testing.T) { |
| 106 | + msg, err := makeEmptyMessage(CmdCreateStream) |
| 107 | + require.NoError(t, err) |
| 108 | + _, ok := msg.(*MsgCreateStream) |
| 109 | + assert.True(t, ok) |
| 110 | +} |
0 commit comments