File: frame-reader_test.go

package info (click to toggle)
golang-github-bifurcation-mint 0.0~git20200214.93c820e-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 632 kB
  • sloc: makefile: 3
file content (75 lines) | stat: -rw-r--r-- 1,702 bytes parent folder | download
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
package mint

import (
	"testing"
)

var kTestFrame = []byte{0x00, 0x05, 'a', 'b', 'c', 'd', 'e'}
var kTestEmptyFrame = []byte{0x00, 0x00}

type simpleHeader struct{}

func (h simpleHeader) headerLen() int {
	return 2
}

func (h simpleHeader) defaultReadLen() int {
	return 1024
}

func (h simpleHeader) frameLen(hdr []byte) (int, error) {
	if len(hdr) != 2 {
		panic("Assert!")
	}

	return (int(hdr[0]) << 8) | int(hdr[1]), nil
}

func checkFrame(t *testing.T, hdr []byte, body []byte) {
	assertByteEquals(t, hdr, kTestFrame[:2])
	assertByteEquals(t, body, kTestFrame[2:])
}

func TestFrameReaderFullFrame(t *testing.T) {
	r := newFrameReader(simpleHeader{})
	r.addChunk(kTestFrame)
	hdr, body, err := r.process()
	assertNotError(t, err, "Couldn't read frame 1")
	checkFrame(t, hdr, body)

	r.addChunk(kTestFrame)
	hdr, body, err = r.process()
	assertNotError(t, err, "Couldn't read frame 2")
	checkFrame(t, hdr, body)
}

func TestFrameReaderTwoFrames(t *testing.T) {
	r := newFrameReader(simpleHeader{})
	r.addChunk(kTestFrame)
	r.addChunk(kTestFrame)
	hdr, body, err := r.process()
	assertNotError(t, err, "Couldn't read frame 1")
	checkFrame(t, hdr, body)

	hdr, body, err = r.process()
	assertNotError(t, err, "Couldn't read frame 2")
	checkFrame(t, hdr, body)
}

func TestFrameReaderTrickle(t *testing.T) {
	r := newFrameReader(simpleHeader{})

	var hdr, body []byte
	var err error
	for i := 0; i <= len(kTestFrame); i += 1 {
		hdr, body, err = r.process()
		if i < len(kTestFrame) {
			assertEquals(t, err, AlertWouldBlock)
			assertEquals(t, 0, len(hdr))
			assertEquals(t, 0, len(body))
			r.addChunk(kTestFrame[i : i+1])
		}
	}
	assertNil(t, err, "Error reading")
	checkFrame(t, hdr, body)
}