data_test.go 3.64 KB
Newer Older
Jeromy's avatar
Jeromy committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package frame

import (
	"bytes"
	"io/ioutil"
	"reflect"
	"testing"
)

type fakeTrans struct {
	*bytes.Buffer
}

func (c *fakeTrans) Close() error { return nil }

func loadedTrans(p []byte) (*fakeTrans, *BasicTransport) {
	trans := &fakeTrans{bytes.NewBuffer(p)}
	return trans, NewBasicTransport(trans)
}

type DataTestParams struct {
	streamId StreamId
	data     []byte
	fin      bool
}

func TestSerializeData(t *testing.T) {
	t.Parallel()

	cases := []struct {
		params   DataTestParams
		expected []byte
	}{
		{
			// test a generic data frame
			DataTestParams{0x49a1bb00, []byte{0x00, 0x01, 0x02, 0x03, 0x04}, false},
			[]byte{0x0, 0x5, 0x0, TypeStreamData, 0x49, 0xa1, 0xbb, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04},
		},
		{
			// test a a frame with fin
			DataTestParams{streamMask, []byte{0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00}, true},
			[]byte{0x00, 0x10, flagFin, TypeStreamData, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00},
		},
		{
			// test a zero-length frame
			DataTestParams{0x0, []byte{}, false},
			[]byte{0x0, 0x0, 0x0, TypeStreamData, 0x0, 0x0, 0x0, 0x0},
		},
	}

	for _, tcase := range cases {
		buf, trans := loadedTrans([]byte{})
		var f *WStreamData = NewWStreamData()
		if err := f.Set(tcase.params.streamId, tcase.params.data, tcase.params.fin); err != nil {
			t.Fatalf("Error while setting params %v!", tcase.params)
		}

		if err := f.writeTo(trans); err != nil {
			t.Fatalf("Error while writing %v!", tcase.params)
		}

		if !reflect.DeepEqual(tcase.expected, buf.Bytes()) {
			t.Errorf("Failed to serialize STREAM_DATA, expected: %v got %v", tcase.expected, buf.Bytes())
		}
	}
}

func TestDeserializeData(t *testing.T) {
	_, trans := loadedTrans([]byte{0x00, 0x10, flagFin, TypeStreamData, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00})

	h := newHeader()
	if err := h.readFrom(trans); err != nil {
		t.Fatalf("Failed to read header")
	}

	var f RStreamData
	f.Header = h
	if err := f.readFrom(trans); err != nil {
		t.Fatalf("Read failed with %v", err)
	}

	got, err := ioutil.ReadAll(f.Reader())
	if err != nil {
		t.Fatalf("Error %v while reading data", err)
	}

	expected := []byte{0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00}
	if !reflect.DeepEqual(expected, got) {
		t.Errorf("Wrong bytes read from transport. Expected %v, got %v", expected, got)
	}

	if !f.Fin() {
		t.Errorf("Fin flag was not deserialized")
	}
}

func TestTooLongSerializeData(t *testing.T) {
	t.Parallel()

	var f *WStreamData = NewWStreamData()
	if err := f.Set(0, make([]byte, lengthMask+1), true); err == nil {
		t.Errorf("Expected error when setting too long buffer, got none.")
	}
}

func TestLengthLimitationData(t *testing.T) {
	dataLen := 0x4
	_, trans := loadedTrans([]byte{0x00, byte(dataLen), 0x0, TypeStreamData, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00})

	h := newHeader()
	if err := h.readFrom(trans); err != nil {
		t.Fatalf("Failed to read header")
	}

	var f RStreamData
	f.Header = h
	if err := f.readFrom(trans); err != nil {
		t.Fatalf("Read failed with %v", err)
	}

	got, err := ioutil.ReadAll(f.Reader())
	if err != nil {
		t.Fatalf("Error %v while reading data", err)
	}

	if len(got) != dataLen {
		t.Errorf("Read with wrong number of bytes, got %d expected %d", len(got), 4)
	}

	expected := []byte{0xFF, 0xEE, 0xDD, 0xCC}
	if !reflect.DeepEqual(expected, got) {
		t.Errorf("Wrong bytes read from transport. Expected %v, got %v", expected, got)
	}
}