forked from plgd-dev/go-coap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiotivity_test.go
158 lines (143 loc) · 3.84 KB
/
iotivity_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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package coap
/*
var path = "/oic/d"
var udpServer = "127.0.0.1:52593"
var tcpServer = "127.0.0.1:40993"
func decodeMsg(resp Message) {
var m interface{}
fmt.Printf("--------------------------------------\n")
fmt.Printf("path: %v\n", resp.PathString())
fmt.Printf("code: %v\n", resp.Code())
fmt.Printf("type: %v\n", resp.Type())
contentFormat := TextPlain
if resp.Option(ContentFormat) != nil {
contentFormat = resp.Option(ContentFormat).(MediaType)
fmt.Printf("content format: %v\n", contentFormat)
}
if resp.Payload() != nil && len(resp.Payload()) > 0 {
switch contentFormat {
case AppCBOR:
err := codec.NewDecoderBytes(resp.Payload(), new(codec.CborHandle)).Decode(&m)
if err != nil {
fmt.Printf("cannot decode payload: %v!!!\n", err)
} else {
fmt.Printf("payload type: %T\n", m)
fmt.Printf("payload value: %v\n", m)
}
case AppJSON:
err := codec.NewDecoderBytes(resp.Payload(), new(codec.JsonHandle)).Decode(&m)
if err != nil {
fmt.Printf("cannot decode payload: %v!!!\n", err)
} else {
fmt.Printf("payload type: %T\n", m)
fmt.Printf("payload value: %v\n", m)
}
}
}
fmt.Printf("resp raw: %v\n", resp)
}
func observe(w ResponseWriter, req *Request) {
fmt.Printf("OBSERVE : %v\n", req.Client.RemoteAddr())
decodeMsg(req.Msg)
}
func TestBlockWisePostBlock16(t *testing.T) {
szx := BlockWiseSzx16
client := &Client{Net: "udp", Handler: observe, BlockWiseTransferSzx: &szx}
co, err := client.Dial(udpServer)
if err != nil {
t.Fatalf("Error dialing: %v", err)
}
fmt.Printf("conn: %v\n", co.LocalAddr())
payload := map[string]interface{}{
"binaryAttribute": make([]byte, 33),
}
bw := new(bytes.Buffer)
h := new(codec.CborHandle)
enc := codec.NewEncoder(bw, h)
err = enc.Encode(&payload)
if err != nil {
t.Fatalf("Cannot encode: %v", err)
}
resp, err := co.Post(path, AppCBOR, bw)
if err != nil {
t.Fatalf("Cannot post exchange")
}
decodeMsg(resp)
}
func TestBlockWiseGetBlock16(t *testing.T) {
szx := BlockWiseSzx16
client := &Client{Net: "udp", Handler: observe, BlockWiseTransferSzx: &szx}
co, err := client.Dial(udpServer)
if err != nil {
t.Fatalf("Error dialing: %v", err)
}
req, err := co.NewGetRequest(path)
if err != nil {
t.Fatalf("Cannot create %v", err)
}
block2, err := MarshalBlockOption(BlockWiseSzx16, 0, false)
if err != nil {
t.Fatalf("Cannot marshal block %v", err)
}
req.SetOption(Block2, block2)
decodeMsg(req)
resp, err := co.Exchange(req)
if err != nil {
t.Fatalf("Cannot post exchange")
}
decodeMsg(resp)
}
func TestBlockWiseObserveBlock16(t *testing.T) {
szx := BlockWiseSzx16
sync := make(chan bool)
client := &Client{Net: "udp", Handler: func(w ResponseWriter, req *Request) {
observe(w, req)
t.Fatalf("unexpected called handler")
sync <- true
}, BlockWiseTransferSzx: &szx}
co, err := client.Dial(udpServer)
if err != nil {
t.Fatalf("Error dialing: %v", err)
}
_, err = co.Observe(path, func(req *Request) {
decodeMsg(req.Msg)
sync <- true
})
if err != nil {
t.Fatalf("Unexpected error '%v'", err)
}
<-sync
co.Close()
}
func TestBlockWiseMulticastBlock16(t *testing.T) {
szx := BlockWiseSzx16
client := &MulticastClient{Net: "udp", Handler: observe, BlockWiseTransferSzx: &szx}
co, err := client.Dial("224.0.1.187:5683")
if err != nil {
t.Fatalf("Error dialing: %v", err)
}
sync := make(chan bool)
_, err = co.Publish("/oic/res", func(req *Request) {
decodeMsg(req.Msg)
sync <- true
})
if err != nil {
t.Fatalf("Unexpected error '%v'", err)
}
<-sync
}
func TestGetBlock16(t *testing.T) {
szx := BlockWiseSzx16
bw := false
client := &Client{Net: "tcp", Handler: observe, BlockWiseTransfer: &bw, BlockWiseTransferSzx: &szx}
co, err := client.Dial(tcpServer)
if err != nil {
t.Fatalf("Error dialing: %v", err)
}
resp, err := co.Get("/oic/res")
if err != nil {
t.Fatalf("Cannot post exchange")
}
decodeMsg(resp)
}
*/