-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathutils_test.go
129 lines (102 loc) · 3.81 KB
/
utils_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
package canopus
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGenerateMessageId(t *testing.T) {
var id, id2 uint16
id = GenerateMessageID()
for i := 0; i < 100; i++ {
id2 = id + 1
id = GenerateMessageID()
assert.NotEqual(t, 65535, id)
assert.Equal(t, id2, id)
}
CurrentMessageID = 65535
id = GenerateMessageID()
assert.Equal(t, uint16(1), id)
}
func TestGenerateToken(t *testing.T) {
assert.Equal(t, "", GenerateToken(0))
for i := 1; i < 10; i++ {
tok := GenerateToken(i)
assert.NotEqual(t, "", tok)
assert.Equal(t, i, len(tok))
}
}
func TestCoreResourceUtil(t *testing.T) {
var resources []*CoreResource
resources = CoreResourcesFromString("")
assert.Equal(t, 0, len(resources))
resources = CoreResourcesFromString("</sensors/temp>;ct=41;rt=\"temperature-c\";if=\"sensor\", </sensors/light>;ct=41;rt=\"light-lux\";if=\"sensor\"")
assert.Equal(t, 2, len(resources))
resource1 := resources[0]
assert.Equal(t, "/sensors/temp", resource1.Target)
assert.Equal(t, 3, len(resource1.Attributes))
assert.Nil(t, resource1.GetAttribute("invalid_attr"))
assert.NotNil(t, resource1.GetAttribute("ct"))
assert.Equal(t, "ct", resource1.GetAttribute("ct").Key)
assert.Equal(t, "41", resource1.GetAttribute("ct").Value)
assert.NotNil(t, resource1.GetAttribute("rt"))
assert.Equal(t, "rt", resource1.GetAttribute("rt").Key)
assert.Equal(t, "temperature-c", resource1.GetAttribute("rt").Value)
assert.NotNil(t, resource1.GetAttribute("if"))
assert.Equal(t, "if", resource1.GetAttribute("if").Key)
assert.Equal(t, "sensor", resource1.GetAttribute("if").Value)
resource2 := resources[1]
assert.Equal(t, "/sensors/light", resource2.Target)
assert.Equal(t, 3, len(resource2.Attributes))
assert.NotNil(t, resource2.GetAttribute("ct"))
assert.Equal(t, "ct", resource2.GetAttribute("ct").Key)
assert.Equal(t, "41", resource2.GetAttribute("ct").Value)
assert.NotNil(t, resource2.GetAttribute("rt"))
assert.Equal(t, "rt", resource2.GetAttribute("rt").Key)
assert.Equal(t, "light-lux", resource2.GetAttribute("rt").Value)
assert.NotNil(t, resource2.GetAttribute("if"))
assert.Equal(t, "if", resource2.GetAttribute("if").Key)
assert.Equal(t, "sensor", resource2.GetAttribute("if").Value)
}
func TestCoapCodeToString(t *testing.T) {
testData := []struct {
coapCode CoapCode
codeString string
}{
{Get, "GET"},
{Post, "POST"},
{Put, "PUT"},
{Delete, "DELETE"},
{CoapCodeEmpty, "0 Empty"},
{CoapCodeCreated, "201 Created"},
{CoapCodeDeleted, "202 Deleted"},
{CoapCodeValid, "203 Valid"},
{CoapCodeChanged, "204 Changed"},
{CoapCodeContent, "205 Content"},
{CoapCodeBadRequest, "400 Bad Request"},
{CoapCodeUnauthorized, "401 Unauthorized"},
{CoapCodeBadOption, "402 Bad Option"},
{CoapCodeForbidden, "403 Forbidden"},
{CoapCodeNotFound, "404 Not Found"},
{CoapCodeMethodNotAllowed, "405 Method Not Allowed"},
{CoapCodeNotAcceptable, "406 Not Acceptable"},
{CoapCodePreconditionFailed, "412 Precondition Failed"},
{CoapCodeRequestEntityTooLarge, "413 Request Entity Too Large"},
{CoapCodeUnsupportedContentFormat, "415 Unsupported Content Format"},
{CoapCodeInternalServerError, "500 Internal Server Error"},
{CoapCodeNotImplemented, "501 Not Implemented"},
{CoapCodeBadGateway, "502 Bad Gateway"},
{CoapCodeServiceUnavailable, "503 Service Unavailable"},
{CoapCodeGatewayTimeout, "504 Gateway Timeout"},
{CoapCodeProxyingNotSupported, "505 Proxying Not Supported"},
{CoapCode(255), "Unknown"},
}
for _, td := range testData {
assert.Equal(t, td.codeString, CoapCodeToString(td.coapCode))
}
}
func TestRouteMatching(t *testing.T) {
}
func TestMediaTypeUtils(t *testing.T) {
assert.True(t, ValidCoapMediaTypeCode(MediaTypeTextPlain))
assert.True(t, ValidCoapMediaTypeCode(MediaTypeOpaqueVndOmaLwm2m))
assert.False(t, ValidCoapMediaTypeCode(MediaType(9999)))
}