-
Notifications
You must be signed in to change notification settings - Fork 41
/
message_test.go
143 lines (114 loc) · 5.01 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 canopus
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMessage(t *testing.T) {
assert.NotNil(t, NewMessage(MessageConfirmable, Get, 12345))
assert.NotNil(t, NewEmptyMessage(12345))
}
func TestInvalidMessage(t *testing.T) {
_, err := BytesToMessage(make([]byte, 0))
assert.NotNil(t, err, "Message should be invalid")
_, err = BytesToMessage(make([]byte, 4))
assert.NotNil(t, err, "Message should be invalid")
}
func TestMessagValidation(t *testing.T) {
// ValidateMessage()
}
func TestMessageConversion(t *testing.T) {
msg := NewBasicConfirmableMessage()
msgId := msg.GetMessageId()
// Byte 1
msg.AddOption(OptionContentFormat, MediaTypeApplicationLinkFormat)
// Convert Message to BYte
b, err := MessageToBytes(msg)
// Reconvert Back Bytes to Message
newMsg, err := BytesToMessage(b)
assert.Nil(t, err, "An error occured converting bytes to message")
assert.Equal(t, 0, int(newMsg.GetMessageType())) // 0x0: Type Confirmable
assert.Equal(t, "abcd1234", bytes.NewBuffer(newMsg.GetToken()).String(), "Token String not the same after reconversion")
assert.Equal(t, int(msgId), int(newMsg.GetMessageId()), "Message ID not the same after reconversion")
assert.NotEqual(t, 0, len(newMsg.GetAllOptions()), "Options should not be 0")
}
func TestMessageBadOptions(t *testing.T) {
// testMsg := NewBasicConfirmableMessage()
// Unknown Critical Option
// unk := OptionCode(99)
// testMsg.AddOption(unk, 0)
// testMsg.AddOption(OPTION_CONTENT_FORMAT, MEDIATYPE_APPLICATION_LINK_FORMAT)
// _, err := MessageToBytes(testMsg)
// assert.NotNil(t, err, "Should throw ERR_UNKNOWN_CRITICAL_OPTION")
}
func TestMessageObject(t *testing.T) {
msg := &CoapMessage{}
assert.Equal(t, 0, len(msg.Options))
msg.AddOptions(NewPathOptions("/example"))
msg.AddOption(OptionAccept, MediaTypeApplicationXML)
msg.AddOption(OptionContentFormat, MediaTypeApplicationJSON)
assert.Equal(t, 3, len(msg.Options))
opt := msg.GetOption(OptionAccept)
assert.NotNil(t, opt)
msg.RemoveOptions(OptionURIPath)
assert.Equal(t, 2, len(msg.Options))
}
func TestOptionConversion(t *testing.T) {
preMsg := NewBasicConfirmableMessage()
preMsg.AddOption(OptionIfMatch, "")
preMsg.AddOptions(NewPathOptions("/test"))
preMsg.AddOption(OptionEtag, "1234567890")
preMsg.AddOption(OptionIfNoneMatch, nil)
preMsg.AddOption(OptionObserve, 0)
preMsg.AddOption(OptionURIPort, 1234)
preMsg.AddOption(OptionLocationPath, "/aaa")
preMsg.AddOption(OptionContentFormat, 1)
preMsg.AddOption(OptionMaxAge, 1)
preMsg.AddOption(OptionProxyURI, "http://www.google.com")
preMsg.AddOption(OptionProxyScheme, "http://proxy.scheme")
converted, _ := MessageToBytes(preMsg)
postMsg, _ := BytesToMessage(converted)
PrintMessage(postMsg)
}
func TestNewMessageHelpers(t *testing.T) {
var messageID uint16 = 12345
testData := []struct {
msg Message
code CoapCode
}{
{EmptyMessage(messageID, MessageAcknowledgment), CoapCodeEmpty},
{CreatedMessage(messageID, MessageAcknowledgment), CoapCodeCreated},
{DeletedMessage(messageID, MessageAcknowledgment), CoapCodeDeleted},
{ValidMessage(messageID, MessageAcknowledgment), CoapCodeValid},
{ChangedMessage(messageID, MessageAcknowledgment), CoapCodeChanged},
{ContentMessage(messageID, MessageAcknowledgment), CoapCodeContent},
{BadRequestMessage(messageID, MessageAcknowledgment), CoapCodeBadRequest},
{UnauthorizedMessage(messageID, MessageAcknowledgment), CoapCodeUnauthorized},
{BadOptionMessage(messageID, MessageAcknowledgment), CoapCodeBadOption},
{ForbiddenMessage(messageID, MessageAcknowledgment), CoapCodeForbidden},
{NotFoundMessage(messageID, MessageAcknowledgment, nil), CoapCodeNotFound},
{MethodNotAllowedMessage(messageID, MessageAcknowledgment), CoapCodeMethodNotAllowed},
{NotAcceptableMessage(messageID, MessageAcknowledgment), CoapCodeNotAcceptable},
{ConflictMessage(messageID, MessageAcknowledgment), CoapCodeConflict},
{PreconditionFailedMessage(messageID, MessageAcknowledgment), CoapCodePreconditionFailed},
{RequestEntityTooLargeMessage(messageID, MessageAcknowledgment), CoapCodeRequestEntityTooLarge},
{UnsupportedContentFormatMessage(messageID, MessageAcknowledgment), CoapCodeUnsupportedContentFormat},
{InternalServerErrorMessage(messageID, MessageAcknowledgment), CoapCodeInternalServerError},
{NotImplementedMessage(messageID, MessageAcknowledgment), CoapCodeNotImplemented},
{BadGatewayMessage(messageID, MessageAcknowledgment), CoapCodeBadGateway},
{ServiceUnavailableMessage(messageID, MessageAcknowledgment), CoapCodeServiceUnavailable},
{GatewayTimeoutMessage(messageID, MessageAcknowledgment), CoapCodeGatewayTimeout},
{ProxyingNotSupportedMessage(messageID, MessageAcknowledgment), CoapCodeProxyingNotSupported},
}
for _, td := range testData {
assert.NotNil(t, td.msg)
assert.Equal(t, td.code, td.msg.GetCode())
}
}
func NewBasicConfirmableMessage() *CoapMessage {
msg := NewMessageOfType(MessageConfirmable, GenerateMessageID(), nil).(*CoapMessage)
msg.Code = Get
msg.Token = []byte("abcd1234")
msg.SetStringPayload("xxxxx")
return msg
}