-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmessage_test.go
143 lines (125 loc) · 4.21 KB
/
message_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
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
135
136
137
138
139
140
141
142
143
package coalago_test
import (
"bytes"
"encoding/binary"
"math/rand"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/coalalib/coalago"
)
var _ = Describe("Message", func() {
Describe("Serialize message", func() {
var (
message *CoAPMessage
datagram []byte
err error
)
BeforeEach(func() {
message = NewCoAPMessage(CON, GET)
datagram, err = Serialize(message)
Expect(err).NotTo(HaveOccurred())
})
AfterEach(func() {
message = nil
})
Context("With correct Message ID", func() {
It("Should correct serialize message id", func() {
uint16DatagramSlice := binary.BigEndian.Uint16(datagram[2:4])
Expect(uint16DatagramSlice).Should(Equal(message.MessageID))
})
})
Context("With correct Version", func() {
It("Should correct serialize version", func() {
Expect(datagram[0] >> 6).Should(Equal(uint8(1)))
})
})
Context("With Type", func() {
DescribeTable("Check each type",
func(expectedType CoapType) {
message.Type = expectedType
datagram, err = Serialize(message)
Expect(err).NotTo(HaveOccurred())
Expect(datagram[0] >> 4 & 3).Should(Equal(uint8(expectedType)))
},
Entry("CON", CON),
Entry("NON", NON),
Entry("ACK", ACK),
Entry("RST", RST),
)
})
Context("With Token Length", func() {
DescribeTable("Check each any length",
func(tokenLength int, isOk bool) {
token := make([]byte, tokenLength)
rand.Read(token)
message.Token = token
datagram, err = Serialize(message)
Expect(err == nil).Should(Equal(isOk))
},
Entry("Token length is zero", 0, true),
Entry("Token length is valid", 5, true),
Entry("Token length is maximum", 8, true),
// Entry("Token length is out of range", 9, false),
)
})
Context("With correct codes", func() {
DescribeTable("Check each code",
func(expectedCode CoapCode) {
message.Code = expectedCode
datagram, err = Serialize(message)
Expect(err).NotTo(HaveOccurred())
Expect(datagram[1]).Should(Equal(uint8(expectedCode)))
},
//methods
Entry("GET", GET),
Entry("POST", POST),
Entry("PUT", PUT),
Entry("DELETE", DELETE),
// Response
Entry("CoapCodeEmpty", CoapCodeEmpty),
Entry("CoapCodeCreated", CoapCodeCreated),
Entry("CoapCodeDeleted", CoapCodeDeleted),
Entry("CoapCodeValid", CoapCodeValid),
Entry("CoapCodeChanged", CoapCodeChanged),
Entry("CoapCodeContent", CoapCodeContent),
Entry("CoapCodeContinue", CoapCodeContinue),
// Errors
Entry("CoapCodeBadRequest", CoapCodeBadRequest),
Entry("CoapCodeUnauthorized", CoapCodeUnauthorized),
Entry("CoapCodeBadOption", CoapCodeBadOption),
Entry("CoapCodeForbidden", CoapCodeForbidden),
Entry("CoapCodeNotFound", CoapCodeNotFound),
Entry("CoapCodeMethodNotAllowed", CoapCodeMethodNotAllowed),
Entry("CoapCodeNotAcceptable", CoapCodeNotAcceptable),
Entry("CoapCodeRequestEntityIncomplete", CoapCodeRequestEntityIncomplete),
Entry("CoapCodeConflict", CoapCodeConflict),
Entry("CoapCodePreconditionFailed", CoapCodePreconditionFailed),
Entry("CoapCodeRequestEntityTooLarge", CoapCodeRequestEntityTooLarge),
Entry("CoapCodeUnsupportedContentFormat", CoapCodeUnsupportedContentFormat),
Entry("CoapCodeInternalServerError", CoapCodeInternalServerError),
Entry("CoapCodeNotImplemented", CoapCodeNotImplemented),
Entry("CoapCodeBadGateway", CoapCodeBadGateway),
Entry("CoapCodeServiceUnavailable", CoapCodeServiceUnavailable),
Entry("CoapCodeGatewayTimeout", CoapCodeGatewayTimeout),
Entry("CoapCodeProxyingNotSupported", CoapCodeProxyingNotSupported),
)
})
Context("With correct Token", func() {
DescribeTable("Check each token by length",
func(tokenLength int) {
token := make([]byte, tokenLength)
rand.Read(token)
message.Token = token
datagram, err = Serialize(message)
Expect(err).NotTo(HaveOccurred())
Expect(bytes.Equal(datagram[4:4+tokenLength], message.Token)).Should(BeTrue())
},
Entry("Token length is minimum", 1),
Entry("Token length is valid", 5),
Entry("Token length is maximum", 8),
// Entry("Token length is out of range", 9, false),
)
})
})
})