Debian Codegraph @

protocol/protocol_test.go

raw
1 /*
2 Copyright 2013-2014 Canonical Ltd.
3
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License version 3, as published
6 by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranties of
10 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along
14 with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 package protocol
18
19 import (
20 "encoding/binary"
21 "encoding/json"
22 "io"
23 "net"
24 "testing"
25 "time"
26
27 . "gopkg.in/check.v1"
28 )
29
30 func TestProtocol(t *testing.T) { TestingT(t) }
31
32 type protocolSuite struct{}
33
34 var _ = Suite(&protocolSuite{})
35
36 type deadline struct {
37 kind string
38 deadAfter time.Duration
39 }
40
41 func (d *deadline) setDeadAfter(t time.Time) {
42 deadAfter := t.Sub(time.Now())
43 d.deadAfter = (deadAfter + time.Millisecond/2) / time.Millisecond * time.Millisecond
44 }
45
46 type rw struct {
47 buf []byte
48 n int
49 err error
50 }
51
52 type testConn struct {
53 deadlines []*deadline
54 reads []rw
55 writes []*rw
56 }
57
58 func (tc *testConn) LocalAddr() net.Addr {
59 return nil
60 }
61
62 func (tc *testConn) RemoteAddr() net.Addr {
63 return nil
64 }
65
66 func (tc *testConn) Close() error {
67 return nil
68 }
69
70 func (tc *testConn) SetDeadline(t time.Time) error {
71 deadline := tc.deadlines[0]
72 deadline.kind = "both"
73 deadline.setDeadAfter(t)
74 tc.deadlines = tc.deadlines[1:]
75 return nil
76 }
77
78 func (tc *testConn) SetReadDeadline(t time.Time) error {
79 deadline := tc.deadlines[0]
80 deadline.kind = "read"
81 deadline.setDeadAfter(t)
82 tc.deadlines = tc.deadlines[1:]
83 return nil
84 }
85
86 func (tc *testConn) SetWriteDeadline(t time.Time) error {
87 deadline := tc.deadlines[0]
88 deadline.kind = "write"
89 deadline.setDeadAfter(t)
90 tc.deadlines = tc.deadlines[1:]
91 return nil
92 }
93
94 func (tc *testConn) Read(buf []byte) (n int, err error) {
95 read := tc.reads[0]
96 copy(buf, read.buf)
97 tc.reads = tc.reads[1:]
98 return read.n, read.err
99 }
100
101 func (tc *testConn) Write(buf []byte) (n int, err error) {
102 write := tc.writes[0]
103 n = copy(write.buf, buf)
104 write.buf = write.buf[:n]
105 write.n = n
106 err = write.err
107 tc.writes = tc.writes[1:]
108 return
109 }
110
111 func (s *protocolSuite) TestReadWireFormatVersion(c *C) {
112 deadl := deadline{}
113 read1 := rw{buf: []byte{42}, n: 1}
114 tc := &testConn{reads: []rw{read1}, deadlines: []*deadline{&deadl}}
115 ver, err := ReadWireFormatVersion(tc, time.Minute)
116 c.Check(err, IsNil)
117 c.Check(ver, Equals, 42)
118 c.Check(deadl.kind, Equals, "read")
119 c.Check(deadl.deadAfter, Equals, time.Minute)
120 }
121
122 func (s *protocolSuite) TestReadWireFormatVersionError(c *C) {
123 deadl := deadline{}
124 read1 := rw{err: io.EOF}
125 tc := &testConn{reads: []rw{read1}, deadlines: []*deadline{&deadl}}
126 _, err := ReadWireFormatVersion(tc, time.Minute)
127 c.Check(err, Equals, io.EOF)
128 }
129
130 func (s *protocolSuite) TestSetDeadline(c *C) {
131 deadl := deadline{}
132 tc := &testConn{deadlines: []*deadline{&deadl}}
133 pc := NewProtocol0(tc)
134 pc.SetDeadline(time.Now().Add(time.Minute))
135 c.Check(deadl.kind, Equals, "both")
136 c.Check(deadl.deadAfter, Equals, time.Minute)
137 }
138
139 type testMsg struct {
140 Type string `json:"T"`
141 A uint64
142 }
143
144 func lengthAsBytes(length uint16) []byte {
145 var buf [2]byte
146 var res = buf[:]
147 binary.BigEndian.PutUint16(res, length)
148 return res
149 }
150
151 func (s *protocolSuite) TestReadMessage(c *C) {
152 msgBuf, _ := json.Marshal(testMsg{Type: "msg", A: 2000})
153 readMsgLen := rw{buf: lengthAsBytes(uint16(len(msgBuf))), n: 2}
154 readMsgBody := rw{buf: msgBuf, n: len(msgBuf)}
155 tc := &testConn{reads: []rw{readMsgLen, readMsgBody}}
156 pc := NewProtocol0(tc)
157 var recvMsg testMsg
158 err := pc.ReadMessage(&recvMsg)
159 c.Check(err, IsNil)
160 c.Check(recvMsg, DeepEquals, testMsg{Type: "msg", A: 2000})
161 }
162
163 func (s *protocolSuite) TestReadMessageBits(c *C) {
164 msgBuf, _ := json.Marshal(testMsg{Type: "msg", A: 2000})
165 readMsgLen := rw{buf: lengthAsBytes(uint16(len(msgBuf))), n: 2}
166 readMsgBody1 := rw{buf: msgBuf[:5], n: 5}
167 readMsgBody2 := rw{buf: msgBuf[5:], n: len(msgBuf) - 5}
168 tc := &testConn{reads: []rw{readMsgLen, readMsgBody1, readMsgBody2}}
169 pc := NewProtocol0(tc)
170 var recvMsg testMsg
171 err := pc.ReadMessage(&recvMsg)
172 c.Check(err, IsNil)
173 c.Check(recvMsg, DeepEquals, testMsg{Type: "msg", A: 2000})
174 }
175
176 func (s *protocolSuite) TestReadMessageIOErrors(c *C) {
177 msgBuf, _ := json.Marshal(testMsg{Type: "msg", A: 2000})
178 readMsgLenErr := rw{n: 1, err: io.ErrClosedPipe}
179 tc1 := &testConn{reads: []rw{readMsgLenErr}}
180 pc1 := NewProtocol0(tc1)
181 var recvMsg testMsg
182 err := pc1.ReadMessage(&recvMsg)
183 c.Check(err, Equals, io.ErrClosedPipe)
184
185 readMsgLen := rw{buf: lengthAsBytes(uint16(len(msgBuf))), n: 2}
186 readMsgBody1 := rw{buf: msgBuf[:5], n: 5}
187 readMsgBody2Err := rw{n: 2, err: io.EOF}
188 tc2 := &testConn{reads: []rw{readMsgLen, readMsgBody1, readMsgBody2Err}}
189 pc2 := NewProtocol0(tc2)
190 err = pc2.ReadMessage(&recvMsg)
191 c.Check(err, Equals, io.EOF)
192 }
193
194 func (s *protocolSuite) TestReadMessageBrokenJSON(c *C) {
195 msgBuf := []byte("{\"T\"}")
196 readMsgLen := rw{buf: lengthAsBytes(uint16(len(msgBuf))), n: 2}
197 readMsgBody := rw{buf: msgBuf, n: len(msgBuf)}
198 tc := &testConn{reads: []rw{readMsgLen, readMsgBody}}
199 pc := NewProtocol0(tc)
200 var recvMsg testMsg
201 err := pc.ReadMessage(&recvMsg)
202 c.Check(err, FitsTypeOf, &json.SyntaxError{})
203 }
204
205 func (s *protocolSuite) TestWriteMessage(c *C) {
206 writeMsg := rw{buf: make([]byte, 64)}
207 tc := &testConn{writes: []*rw{&writeMsg}}
208 pc := NewProtocol0(tc)
209 msg := testMsg{Type: "m", A: 9999}
210 err := pc.WriteMessage(&msg)
211 c.Check(err, IsNil)
212 var msgLen int = int(binary.BigEndian.Uint16(writeMsg.buf[:2]))
213 c.Check(msgLen, Equals, len(writeMsg.buf)-2)
214 var wroteMsg testMsg
215 formatErr := json.Unmarshal(writeMsg.buf[2:], &wroteMsg)
216 c.Check(formatErr, IsNil)
217 c.Check(wroteMsg, DeepEquals, testMsg{Type: "m", A: 9999})
218 }
219
220 func (s *protocolSuite) TestWriteMessageIOErrors(c *C) {
221 writeMsgErr := rw{buf: make([]byte, 0), err: io.ErrClosedPipe}
222 tc1 := &testConn{writes: []*rw{&writeMsgErr}}
223 pc1 := NewProtocol0(tc1)
224 msg := testMsg{Type: "m", A: 9999}
225 err := pc1.WriteMessage(&msg)
226 c.Check(err, Equals, io.ErrClosedPipe)
227 }