aboutsummaryrefslogtreecommitdiffstats
path: root/formats/pktline/decoder_test.go
blob: e3cd2f439df74e9fa9859765904882d4ddcc56cd (plain) (blame)
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
package pktline

import (
	"strings"
	"testing"

	. "gopkg.in/check.v1"
)

func Test(t *testing.T) { TestingT(t) }

type DecoderSuite struct{}

var _ = Suite(&DecoderSuite{})

func (s *DecoderSuite) TestReadLine(c *C) {
	j := NewDecoder(strings.NewReader("0006a\n"))

	line, err := j.ReadLine()
	c.Assert(err, IsNil)
	c.Assert(line, Equals, "a\n")
}

func (s *DecoderSuite) TestReadLineInvalidHeader(c *C) {
	j := NewDecoder(strings.NewReader("foo\n"))

	_, err := j.ReadLine()
	c.Assert(err, Equals, ErrInvalidHeader)
}

func (s *DecoderSuite) TestReadLineBufferUnderflow(c *C) {
	j := NewDecoder(strings.NewReader("00e7a\n"))

	line, err := j.ReadLine()
	c.Assert(err, Equals, ErrUnderflow)
	c.Assert(line, Equals, "")
}

func (s *DecoderSuite) TestReadLineInvalidLen(c *C) {
	j := NewDecoder(strings.NewReader("0001foo\n"))

	line, err := j.ReadLine()
	c.Assert(err, Equals, ErrInvalidLen)
	c.Assert(line, Equals, "")
}

func (s *DecoderSuite) TestReadBlock(c *C) {
	j := NewDecoder(strings.NewReader("0006a\n"))

	lines, err := j.ReadBlock()
	c.Assert(err, IsNil)
	c.Assert(lines, HasLen, 1)
	c.Assert(lines[0], Equals, "a\n")
}

func (s *DecoderSuite) TestReadBlockWithFlush(c *C) {
	j := NewDecoder(strings.NewReader("0006a\n0006b\n00000006c\n"))

	lines, err := j.ReadBlock()
	c.Assert(err, IsNil)
	c.Assert(lines, HasLen, 2)
	c.Assert(lines[0], Equals, "a\n")
	c.Assert(lines[1], Equals, "b\n")
}

func (s *DecoderSuite) TestReadBlockInvalidLen(c *C) {
	j := NewDecoder(strings.NewReader("0001foo\n"))

	lines, err := j.ReadBlock()
	c.Assert(err, Equals, ErrInvalidLen)
	c.Assert(lines, HasLen, 0)
}

func (s *DecoderSuite) TestReadAll(c *C) {
	j := NewDecoder(strings.NewReader("0006a\n0006b\n00000006c\n0006d\n0006e\n"))

	lines, err := j.ReadAll()
	c.Assert(err, IsNil)
	c.Assert(lines, HasLen, 5)
	c.Assert(lines[0], Equals, "a\n")
	c.Assert(lines[1], Equals, "b\n")
	c.Assert(lines[2], Equals, "c\n")
	c.Assert(lines[3], Equals, "d\n")
	c.Assert(lines[4], Equals, "e\n")
}