-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunp_test.go
89 lines (71 loc) · 2.4 KB
/
unp_test.go
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
package unp
import (
"bytes"
"testing"
. "gopkg.in/check.v1"
)
func Test(t *testing.T) { TestingT(t) }
type MySuite struct{}
var _ = Suite(&MySuite{})
func (s *MySuite) TestDeconstructSize1(c *C) {
var buffer bytes.Buffer
unpi := New(1, &buffer)
//empty payload
frame := &Frame{C_SRSP, S_SAPI, 0, []byte{}}
unpi.WriteFrame(frame)
c.Assert(buffer.Bytes(), DeepEquals, []byte{0xfe, 0x00, 0x66, 0x00, 0x66})
//nil payload
frame = &Frame{C_SRSP, S_SAPI, 0, nil}
buffer.Reset()
unpi.WriteFrame(frame)
c.Assert(buffer.Bytes(), DeepEquals, []byte{0xfe, 0x00, 0x66, 0x00, 0x66})
//non empty payload
frame = &Frame{C_SREQ, S_SAPI, 0, []byte{0x00, 0x01, 0x02}}
buffer.Reset()
unpi.WriteFrame(frame)
c.Assert(buffer.Bytes(), DeepEquals, []byte{0xfe, 0x03, 0x26, 0x00, 0x00, 0x01, 0x02, 0x26})
}
func (s *MySuite) TestConstructSize1(c *C) {
var buffer bytes.Buffer
unpi := New(1, &buffer)
//empty payload
buffer.Write([]byte{0xfe, 0x00, 0x66, 0x00, 0x66})
frame, _ := unpi.ReadFrame()
c.Assert(frame, DeepEquals, &Frame{C_SRSP, S_SAPI, 0, []byte{}})
//non empty payload
buffer.Reset()
buffer.Write([]byte{0xfe, 0x03, 0x26, 0x00, 0x00, 0x01, 0x02, 0x26})
frame, _ = unpi.ReadFrame()
c.Assert(frame, DeepEquals, &Frame{C_SREQ, S_SAPI, 0, []byte{0x00, 0x01, 0x02}})
}
func (s *MySuite) TestDeconstructSize2(c *C) {
var buffer bytes.Buffer
unpi := New(2, &buffer)
//empty payload
frame := &Frame{C_SRSP, S_SAPI, 0, []byte{}}
unpi.WriteFrame(frame)
c.Assert(buffer.Bytes(), DeepEquals, []byte{0xfe, 0x00, 0x00, 0x66, 0x00, 0x66})
//nil payload
frame = &Frame{C_SRSP, S_SAPI, 0, nil}
buffer.Reset()
unpi.WriteFrame(frame)
c.Assert(buffer.Bytes(), DeepEquals, []byte{0xfe, 0x00, 0x00, 0x66, 0x00, 0x66})
//non empty payload
frame = &Frame{C_SREQ, S_SAPI, 0, []byte{0x00, 0x01, 0x02}}
buffer.Reset()
unpi.WriteFrame(frame)
c.Assert(buffer.Bytes(), DeepEquals, []byte{0xfe, 0x00, 0x03, 0x26, 0x00, 0x00, 0x01, 0x02, 0x26})
}
func (s *MySuite) TestConstructSize2(c *C) {
var buffer bytes.Buffer
unpi := New(2, &buffer)
//empty payload
buffer.Write([]byte{0xfe, 0x00, 0x00, 0x66, 0x00, 0x66})
frame, _ := unpi.ReadFrame()
c.Assert(frame, DeepEquals, &Frame{C_SRSP, S_SAPI, 0, []byte{}})
//non empty payload
buffer.Reset()
buffer.Write([]byte{0xfe, 0x00, 0x03, 0x26, 0x00, 0x00, 0x01, 0x02, 0x26})
frame, _ = unpi.ReadFrame()
c.Assert(frame, DeepEquals, &Frame{C_SREQ, S_SAPI, 0, []byte{0x00, 0x01, 0x02}})
}