-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstate_test.go
160 lines (131 loc) · 3.14 KB
/
state_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
159
160
// SPDX-FileCopyrightText: 2022 ANSSI
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"testing"
"github.com/go-ble/ble"
)
/*
All the following pain, only to implement the Conn interface
of the go-ble package, which doesn't provide a test type.
*/
type FakeConn struct {
context context.Context
}
func (fc *FakeConn) Context() context.Context {
return fc.context
}
func (fc *FakeConn) SetContext(ctx context.Context) {
fc.context = ctx
}
func (fc *FakeConn) LocalAddr() ble.Addr {
return ble.NewAddr("42:42:42:42:42:42")
}
func (fc *FakeConn) RemoteAddr() ble.Addr {
return ble.NewAddr("42:42:42:42:42:42")
}
func (fc *FakeConn) RxMTU() int {
return 0
}
func (fc *FakeConn) SetRxMTU(mtu int) {
}
func (fc *FakeConn) TxMTU() int {
return 0
}
func (fc *FakeConn) SetTxMTU(mtu int) {
}
func (fc *FakeConn) ReadRSSI() int {
return 0
}
func (fc *FakeConn) Close() error {
return nil
}
func (fc *FakeConn) Write(p []byte) (int, error) {
return len(p), nil
}
func (fc *FakeConn) Read(p []byte) (int, error) {
return len(p), nil
}
func (fc *FakeConn) Disconnected() <-chan struct{} {
return make(chan struct{})
}
/*
Tests that the getConnectionState function well
sets the stateKey value for the connection
context on the first call
*/
func TestGetConnectionState(t *testing.T) {
var conn = FakeConn{
context: context.Background(),
}
var stateKey key
if conn.Context().Value(stateKey) != nil {
t.Errorf("Context has value for stateKey key: %+v", conn.Context())
}
_ = getConnectionState(&conn)
if conn.Context().Value(stateKey) == nil {
t.Errorf("Context value for stateKey key is nil: %+v", conn.Context())
}
}
func TestStartOperation_NormalCase(t *testing.T) {
var s = State{}
s.reset()
err := s.StartOperation(Read)
if err != nil {
t.Error("StartOperation failed, whereas no operation is in progress")
}
}
func TestStartOperation_AnotherOperationInProgress(t *testing.T) {
var s = State{
operation: Read,
Msglen: 0,
Offset: -1,
}
err := s.StartOperation(Write)
if err == nil {
t.Error("StartOperation succeeded whereas an operation is running")
}
}
func TestEndOperation_WholeMessageWritten(t *testing.T) {
var s = State{
operation: Write,
Buf: make([]byte, 8),
Offset: 8,
Msglen: -1,
}
err := s.EndOperation()
if err != nil {
t.Errorf("EndOperation failed, whereas an operation completed successfully: %+v", s)
}
if s.Buf != nil {
t.Errorf("The buffer hasn't been reset correctly: %+v", s)
}
if s.Offset != -1 {
t.Errorf("The offset hasn't been reset correctly: %+v", s)
}
}
func TestEndOperation_BufBiggerThanMsglen(t *testing.T) {
var s = State{
operation: Read,
Buf: make([]byte, 16),
Msglen: 12,
Offset: -1,
}
err := s.EndOperation()
if err == nil {
t.Errorf("EndOperation succeeded whereas the running operation wasn't valid: %+v", s)
}
}
func TestEndOperation_ReadingNotComplete(t *testing.T) {
var s = State{
operation: Read,
Buf: make([]byte, 16),
Msglen: 20,
Offset: -1,
}
err := s.EndOperation()
if err == nil {
t.Errorf("EndOperation succeeded whereas the running operation wasn't complete: %+v", s)
}
}