| 1 |
package frame |
| 2 |
|
| 3 |
import ( |
| 4 |
"reflect" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
type HeaderParams struct { |
| 9 |
ftype FrameType |
| 10 |
length int |
| 11 |
streamId StreamId |
| 12 |
flags flagsType |
| 13 |
} |
| 14 |
|
| 15 |
func (params *HeaderParams) checkDeserialize(t *testing.T, h Header) { |
| 16 |
if h.Type() != params.ftype { |
| 17 |
t.Errorf("Failed deserialization. Expected type %x, got: %x", params.ftype, h.Type()) |
| 18 |
} |
| 19 |
|
| 20 |
if h.Length() != uint16(params.length) { |
| 21 |
t.Errorf("Failed deserialization. Expected length %x, got: %x", params.length, h.Length()) |
| 22 |
} |
| 23 |
|
| 24 |
if h.Flags() != params.flags { |
| 25 |
t.Errorf("Failed deserialization. Expected flags %x, got: %x", params.flags, h.Flags()) |
| 26 |
} |
| 27 |
|
| 28 |
if h.StreamId() != params.streamId { |
| 29 |
t.Errorf("Failed deserialization. Expected stream id %x, got: %x", params.streamId, h.StreamId()) |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
func TestHeaderSerialization(t *testing.T) { |
| 34 |
t.Parallel() |
| 35 |
|
| 36 |
testCases := []struct { |
| 37 |
input HeaderParams |
| 38 |
expectedOutput []byte |
| 39 |
}{ |
| 40 |
{ |
| 41 |
HeaderParams{ |
| 42 |
ftype: TypeStreamRst, |
| 43 |
length: 0x4, |
| 44 |
streamId: 0x2843, |
| 45 |
flags: 0, |
| 46 |
}, |
| 47 |
[]byte{0, 0x4, 0, 0x2, 0, 0, 0x28, 0x43}, |
| 48 |
}, |
| 49 |
{ |
| 50 |
HeaderParams{ |
| 51 |
ftype: 0x1F, |
| 52 |
length: 0x37BD, |
| 53 |
streamId: 0x0, |
| 54 |
flags: 0x9, |
| 55 |
}, |
| 56 |
[]byte{0x37, 0xBD, 0x9, 0x1F, 0, 0, 0, 0}, |
| 57 |
}, |
| 58 |
{ |
| 59 |
HeaderParams{ |
| 60 |
ftype: 0, |
| 61 |
length: 0, |
| 62 |
streamId: 0, |
| 63 |
flags: 0, |
| 64 |
}, |
| 65 |
[]byte{0, 0, 0, 0, 0, 0, 0, 0}, |
| 66 |
}, |
| 67 |
{ |
| 68 |
HeaderParams{ |
| 69 |
ftype: typeMask, |
| 70 |
length: lengthMask, |
| 71 |
streamId: streamMask, |
| 72 |
flags: flagsMask, |
| 73 |
}, |
| 74 |
[]byte{0x3F, 0xFF, 0xFF, 0x1F, 0x7F, 0xFF, 0xFF, 0xFF}, |
| 75 |
}, |
| 76 |
{ |
| 77 |
HeaderParams{ |
| 78 |
ftype: 0x1e, |
| 79 |
length: 0x1DAA, |
| 80 |
streamId: 0x4F224719, |
| 81 |
flags: 0x17, |
| 82 |
}, |
| 83 |
[]byte{0x1D, 0xAA, 0x17, 0x1E, 0x4F, 0x22, 0x47, 0x19}, |
| 84 |
}, |
| 85 |
} |
| 86 |
|
| 87 |
for _, test := range testCases { |
| 88 |
var h Header = Header(make([]byte, headerSize)) |
| 89 |
h.SetAll(test.input.ftype, test.input.length, test.input.streamId, test.input.flags) |
| 90 |
output := []byte(h) |
| 91 |
if !reflect.DeepEqual(output, test.expectedOutput) { |
| 92 |
t.Errorf("Failed serialization of %v. Expected %x, got: %x", test.input, output, test.expectedOutput) |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
func TestHeaderDeserialization(t *testing.T) { |
| 98 |
t.Parallel() |
| 99 |
|
| 100 |
testCases := []struct { |
| 101 |
input []byte |
| 102 |
expectedOutput HeaderParams |
| 103 |
}{ |
| 104 |
{ |
| 105 |
[]byte{0, 0x4, 0, 0x2, 0, 0, 0x28, 0x43}, |
| 106 |
HeaderParams{ |
| 107 |
ftype: TypeStreamRst, |
| 108 |
length: 0x4, |
| 109 |
streamId: 0x2843, |
| 110 |
flags: 0, |
| 111 |
}, |
| 112 |
}, |
| 113 |
{ |
| 114 |
[]byte{0x37, 0xBD, 0x9, 0x1F, 0, 0, 0, 0}, |
| 115 |
HeaderParams{ |
| 116 |
ftype: 0x1F, |
| 117 |
length: 0x37BD, |
| 118 |
streamId: 0x0, |
| 119 |
flags: 0x9, |
| 120 |
}, |
| 121 |
}, |
| 122 |
{ |
| 123 |
[]byte{0, 0, 0, 0, 0, 0, 0, 0}, |
| 124 |
HeaderParams{ |
| 125 |
ftype: 0, |
| 126 |
length: 0, |
| 127 |
streamId: 0, |
| 128 |
flags: 0, |
| 129 |
}, |
| 130 |
}, |
| 131 |
{ |
| 132 |
[]byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, |
| 133 |
HeaderParams{ |
| 134 |
ftype: typeMask, |
| 135 |
length: lengthMask, |
| 136 |
streamId: streamMask, |
| 137 |
flags: flagsMask, |
| 138 |
}, |
| 139 |
}, |
| 140 |
{ |
| 141 |
[]byte{0x9D, 0xAA, 0x17, 0xF0, 0xCF, 0x22, 0x47, 0x19}, |
| 142 |
HeaderParams{ |
| 143 |
ftype: 0x10, |
| 144 |
length: 0x1DAA, |
| 145 |
streamId: 0x4F224719, |
| 146 |
flags: 0x17, |
| 147 |
}, |
| 148 |
}, |
| 149 |
} |
| 150 |
|
| 151 |
for _, test := range testCases { |
| 152 |
test.expectedOutput.checkDeserialize(t, Header(test.input)) |
| 153 |
|
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
func TestHeaderRoundTrip(t *testing.T) { |
| 158 |
t.Parallel() |
| 159 |
|
| 160 |
headers := []HeaderParams{ |
| 161 |
HeaderParams{ |
| 162 |
ftype: TypeStreamRst, |
| 163 |
length: 0x4, |
| 164 |
streamId: 0x2843, |
| 165 |
flags: 0, |
| 166 |
}, |
| 167 |
HeaderParams{ |
| 168 |
ftype: 0x1F, |
| 169 |
length: 0x37BD, |
| 170 |
streamId: 0x0, |
| 171 |
flags: 0x9, |
| 172 |
}, |
| 173 |
HeaderParams{ |
| 174 |
ftype: 0, |
| 175 |
length: 0, |
| 176 |
streamId: 0, |
| 177 |
flags: 0, |
| 178 |
}, |
| 179 |
HeaderParams{ |
| 180 |
ftype: typeMask, |
| 181 |
length: lengthMask, |
| 182 |
streamId: streamMask, |
| 183 |
flags: flagsMask, |
| 184 |
}, |
| 185 |
HeaderParams{ |
| 186 |
ftype: 0x1e, |
| 187 |
length: 0x1DAA, |
| 188 |
streamId: 0x4F224719, |
| 189 |
flags: 0x17, |
| 190 |
}, |
| 191 |
} |
| 192 |
|
| 193 |
for _, input := range headers { |
| 194 |
var h Header = Header(make([]byte, headerSize)) |
| 195 |
h.SetAll(input.ftype, input.length, input.streamId, input.flags) |
| 196 |
input.checkDeserialize(t, h) |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
func TestValidStreamIds(t *testing.T) { |
| 201 |
t.Parallel() |
| 202 |
|
| 203 |
validStreamIds := []StreamId{ |
| 204 |
0x0, |
| 205 |
0xFF, |
| 206 |
0x23C10A8F, |
| 207 |
0x7FFFFFFF, |
| 208 |
} |
| 209 |
|
| 210 |
for _, validStreamId := range validStreamIds { |
| 211 |
var h Header = Header(make([]byte, headerSize)) |
| 212 |
err := h.SetAll(TypeStreamSyn, 0, validStreamId, 0) |
| 213 |
if err != nil { |
| 214 |
t.Errorf("Failed to create frame header with valid stream id %d.", validStreamId) |
| 215 |
} |
| 216 |
|
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
func TestInvalidStreamId(t *testing.T) { |
| 221 |
t.Parallel() |
| 222 |
|
| 223 |
invalidStreamIds := []StreamId{ |
| 224 |
0xF0000000, |
| 225 |
0xB012CA8E, |
| 226 |
0x80000000, |
| 227 |
0xFFFFFFFF, |
| 228 |
} |
| 229 |
|
| 230 |
for _, invalidStreamId := range invalidStreamIds { |
| 231 |
var h Header = Header(make([]byte, headerSize)) |
| 232 |
err := h.SetAll(TypeStreamSyn, 0, invalidStreamId, 0) |
| 233 |
if err == nil { |
| 234 |
t.Errorf("Failed to error on invalid stream id %d.", invalidStreamId) |
| 235 |
} |
| 236 |
|
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
func TestValidLengths(t *testing.T) { |
| 241 |
t.Parallel() |
| 242 |
|
| 243 |
validLengths := []int{ |
| 244 |
0x0, |
| 245 |
0x2FF, |
| 246 |
0x301A, |
| 247 |
0x3FFF, |
| 248 |
} |
| 249 |
|
| 250 |
for _, validLength := range validLengths { |
| 251 |
var h Header = Header(make([]byte, headerSize)) |
| 252 |
err := h.SetAll(TypeStreamSyn, validLength, 0, 0) |
| 253 |
if err != nil { |
| 254 |
t.Errorf("Failed to create frame header with valid length %d.", validLength) |
| 255 |
} |
| 256 |
|
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
func TestInvalidLengths(t *testing.T) { |
| 261 |
t.Parallel() |
| 262 |
|
| 263 |
invalidLengths := []int{ |
| 264 |
-1, |
| 265 |
0x4000, |
| 266 |
0xB012, |
| 267 |
0x8000, |
| 268 |
0xFFFF, |
| 269 |
} |
| 270 |
|
| 271 |
for _, invalidLength := range invalidLengths { |
| 272 |
var h Header = Header(make([]byte, headerSize)) |
| 273 |
err := h.SetAll(TypeStreamSyn, invalidLength, 0, 0) |
| 274 |
if err == nil { |
| 275 |
t.Errorf("Failed to error on invalid length %d.", invalidLength) |
| 276 |
} |
| 277 |
|
| 278 |
} |
| 279 |
} |