-
Notifications
You must be signed in to change notification settings - Fork 10
/
protocol_test.go
146 lines (124 loc) · 3.26 KB
/
protocol_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
// SPDX-FileCopyrightText: 2022 ANSSI
// SPDX-License-Identifier: Apache-2.0
package main
import (
"reflect"
"testing"
"github.com/fxamacker/cbor/v2"
)
func TestSendMsg_NormalCase(t *testing.T) {
var data = []int{4, 8, 15, 16, 23, 42}
var session = &Session {
ch: make(chan []byte),
}
// Emulate a successful client read on the characteristic
go func(ch chan []byte, t *testing.T) {
_, ok := <-ch
if !ok {
t.Fatal("The channel has been closed")
}
ch <- nil
}(session.ch, t)
// Check that no error occured
err := sendMsg(data, session)
if err != nil {
t.Errorf("Failed to send data %v", data)
}
}
func TestSendMsg_WithError(t *testing.T) {
var data = []int{4, 8, 15, 16, 23, 42}
var session = &Session {
ch: make(chan []byte),
}
// Emulate a client read on the characteristic that
// fails , in a goroutine.
go func(ch chan []byte, t *testing.T) {
_, ok := <-ch
if !ok {
t.Fatal("The channel has been closed")
}
close(ch) // This means an error occured
}(session.ch, t)
// Make sure an error occured
err := sendMsg(data, session)
if err == nil {
t.Error("sendMsg succeeded whereas the channel closed unexpectedly")
}
}
/*
// I'm sad to admit it, but I can't manage to make the cbor.Marshal function fail...
func TestSendMsg_EncodingFailure(t *testing.T) {
var data = []int {4, 8, 15, 16, 23, 42}
var ch = make(chan []byte)
// Emulate a client read on the characteristic
go func(ch chan []byte, t *testing.T) {
_, ok := <- ch
if !ok {
t.Fatal("The channel has been closed")
}
ch <- nil
}(ch , t)
// Make sure an error occured
err := sendMsg(data, ch)
if err == nil {
t.Error("sendMsg succeeded whereas the channel closed unexpectedly")
}
}
*/
func TestRecvMsg_NormalCase(t *testing.T) {
var data []int
var expected = []int{4, 8, 15, 16, 23, 42}
var session = &Session {
ch: make(chan []byte),
}
// Emulate a successful client write on the characteristic
go func(ch chan []byte, t *testing.T) {
var data = []int{4, 8, 15, 16, 23, 42}
var encoded, err = cbor.Marshal(data)
if err != nil {
t.Fatalf("Failed to encode %#v as CBOR", data)
}
ch <- encoded
}(session.ch, t)
// Check that no error occured
err := recvMsg(&data, session)
if err != nil {
t.Error("Failed to receive data")
}
// Check the received data
if !reflect.DeepEqual(data, expected) {
t.Errorf("Expected: %#v, Received: %#v", expected, data)
}
}
func TestRecvMsg_ChannelError(t *testing.T) {
var data []int
var session = &Session {
ch: make(chan []byte),
}
// Emulate a channel error while a client is writing on
// the characteristic.
go func(ch chan []byte, t *testing.T) {
close(ch)
}(session.ch, t)
// Make sure the error is caught
err := recvMsg(&data, session)
if err == nil {
t.Error("recvMsg succeeded whereas the channel closed unecpectedly.")
}
}
func TestRecvMsg_InvalidCBOR(t *testing.T) {
var data []int
var session = &Session {
ch: make(chan []byte),
}
// Emulate a successful client write on the characteristic
go func(ch chan []byte, t *testing.T) {
var encoded = []byte{0x38, 0x18, 0x12} // Invalid CBOR
ch <- encoded
}(session.ch, t)
// Make sure an error occured
err := recvMsg(&data, session)
if err == nil {
t.Error("recvMsg succeeded whereas invalid CBOR data was sent")
}
}