github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/primitive/streamid_test.go (about) 1 // Copyright 2020 DataStax 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package primitive 16 17 import ( 18 "bytes" 19 "fmt" 20 "math" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 ) 25 26 func TestReadStreamId(t *testing.T) { 27 for _, version := range SupportedProtocolVersionsLesserThanOrEqualTo(ProtocolVersion2) { 28 t.Run(version.String(), func(t *testing.T) { 29 tests := []struct { 30 name string 31 source []byte 32 expected int16 33 err error 34 }{ 35 { 36 "zero stream id", 37 []byte{0}, 38 int16(0), 39 nil, 40 }, 41 { 42 "positive stream id", 43 []byte{0x7f}, 44 int16(127), 45 nil, 46 }, 47 { 48 "negative stream id", 49 []byte{0x80}, 50 int16(-128), 51 nil, 52 }, 53 } 54 for _, tt := range tests { 55 t.Run(tt.name, func(t *testing.T) { 56 buf := bytes.NewReader(tt.source) 57 actual, err := ReadStreamId(buf, version) 58 assert.Equal(t, tt.expected, actual) 59 assert.Equal(t, tt.err, err) 60 }) 61 } 62 }) 63 } 64 for _, version := range SupportedProtocolVersionsGreaterThanOrEqualTo(ProtocolVersion3) { 65 t.Run(version.String(), func(t *testing.T) { 66 tests := []struct { 67 name string 68 source []byte 69 expected int16 70 err error 71 }{ 72 { 73 "zero stream id", 74 []byte{0, 0}, 75 int16(0), 76 nil, 77 }, 78 { 79 "positive stream id", 80 []byte{0x7f, 0xff}, // MaxInt16 = 32767 81 math.MaxInt16, 82 nil, 83 }, 84 { 85 "negative stream id", 86 []byte{0x80, 0x00}, // MinInt16 = -32768 87 math.MinInt16, 88 nil, 89 }, 90 } 91 for _, tt := range tests { 92 t.Run(tt.name, func(t *testing.T) { 93 buf := bytes.NewReader(tt.source) 94 actual, err := ReadStreamId(buf, version) 95 assert.Equal(t, tt.expected, actual) 96 assert.Equal(t, tt.err, err) 97 }) 98 } 99 }) 100 } 101 } 102 103 func TestWriteStreamId(t *testing.T) { 104 for _, version := range SupportedProtocolVersionsLesserThanOrEqualTo(ProtocolVersion2) { 105 t.Run(version.String(), func(t *testing.T) { 106 tests := []struct { 107 name string 108 input int16 109 expected []byte 110 err error 111 }{ 112 { 113 "zero stream id", 114 int16(0), 115 []byte{0}, 116 nil, 117 }, 118 { 119 "positive stream id", 120 int16(127), 121 []byte{0x7f}, 122 nil, 123 }, 124 { 125 "negative stream id", 126 int16(-128), 127 []byte{0x80}, 128 nil, 129 }, 130 { 131 "stream id out of range 1", 132 int16(128), 133 nil, 134 fmt.Errorf("stream id out of range for %v: 128", version), 135 }, 136 { 137 "stream id out of range 2", 138 int16(-129), 139 nil, 140 fmt.Errorf("stream id out of range for %v: -129", version), 141 }, 142 } 143 for _, tt := range tests { 144 t.Run(tt.name, func(t *testing.T) { 145 buf := &bytes.Buffer{} 146 err := WriteStreamId(tt.input, buf, version) 147 assert.Equal(t, tt.expected, buf.Bytes()) 148 assert.Equal(t, tt.err, err) 149 }) 150 } 151 }) 152 } 153 for _, version := range SupportedProtocolVersionsGreaterThanOrEqualTo(ProtocolVersion3) { 154 t.Run(version.String(), func(t *testing.T) { 155 tests := []struct { 156 name string 157 input int16 158 expected []byte 159 err error 160 }{ 161 { 162 "zero stream id", 163 int16(0), 164 []byte{0, 0}, 165 nil, 166 }, 167 { 168 "positive stream id", 169 math.MaxInt16, 170 []byte{0x7f, 0xff}, // MaxInt16 = 32767 171 nil, 172 }, 173 { 174 "negative stream id", 175 math.MinInt16, 176 []byte{0x80, 0x00}, // MinInt16 = -32768 177 nil, 178 }, 179 } 180 for _, tt := range tests { 181 t.Run(tt.name, func(t *testing.T) { 182 buf := &bytes.Buffer{} 183 err := WriteStreamId(tt.input, buf, version) 184 assert.Equal(t, tt.expected, buf.Bytes()) 185 assert.Equal(t, tt.err, err) 186 }) 187 } 188 }) 189 } 190 }