forked from BitBoxSwiss/bitbox02-api-go
/
device_test.go
351 lines (313 loc) · 10.4 KB
/
device_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Copyright 2018-2019 Shift Cryptosecurity AG
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package firmware
import (
"crypto/rand"
"errors"
"fmt"
"testing"
"github.com/digitalbitbox/bitbox02-api-go/api/common"
"github.com/digitalbitbox/bitbox02-api-go/api/firmware/messages"
"github.com/digitalbitbox/bitbox02-api-go/api/firmware/mocks"
"github.com/digitalbitbox/bitbox02-api-go/util/semver"
"github.com/flynn/noise"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
)
// newDevice creates a device to test with, with init/pairing already processed.
func newDevice(
t *testing.T,
version *semver.SemVer,
product common.Product,
communication *mocks.Communication,
onRequest func(*messages.Request) *messages.Response,
) *Device {
t.Helper()
device := NewDevice(
version,
&product,
&mocks.Config{}, communication, &mocks.Logger{},
)
cipherSuite := noise.NewCipherSuite(noise.DH25519, noise.CipherChaChaPoly, noise.HashSHA256)
keypair, err := cipherSuite.GenerateKeypair(rand.Reader)
require.NoError(t, err)
handshake, err := noise.NewHandshakeState(noise.Config{
CipherSuite: cipherSuite,
Random: rand.Reader,
Pattern: noise.HandshakeXX,
StaticKeypair: keypair,
Prologue: []byte("Noise_XX_25519_ChaChaPoly_SHA256"),
Initiator: false,
})
require.NoError(t, err)
var sendCipher, receiveCipher *noise.CipherState
shakingHands := false
var handleRequest func(request *messages.Request) *messages.Response
// upgrades query with HWW_* framing for >=v7.0.0 versions
v7_0_0Query := func(query func(msg []byte) ([]byte, error)) func(msg []byte) ([]byte, error) {
if !version.AtLeast(semver.NewSemVer(7, 0, 0)) {
return query
}
return func(msg []byte) ([]byte, error) {
// TODO: modularize and unit test the full hww* arbitration / canceling.
// 0x00 = HWW_REQ_NEW
require.Equal(t, byte(0x00), msg[0])
msg = msg[1:]
response, err := query(msg)
if err != nil {
return nil, err
}
// prepend HWW_RSP_ACK
response = append([]byte{0x00}, response...)
return response, nil
}
}
communication.MockQuery = v7_0_0Query(func(msg []byte) ([]byte, error) {
if shakingHands {
//nolint:misspell
if version.AtLeast(semver.NewSemVer(7, 0, 0)) {
// 'H' = OP_HER_COMEZ_TEH_HANDSHAEK
require.Equal(t, byte('H'), msg[0])
msg = msg[1:]
}
var err error
_, receiveCipher, sendCipher, err = handshake.ReadMessage(nil, msg)
require.NoError(t, err)
require.Equal(t, sendCipher == nil, receiveCipher == nil)
if sendCipher != nil { // handshake done
shakingHands = false
return []byte{0}, nil // 0 = do not require pairing verification
}
msgSend, _, _, err := handshake.WriteMessage(nil, nil)
require.NoError(t, err)
if version.AtLeast(semver.NewSemVer(7, 0, 0)) {
// prepend OP_STATUS_SUCCESS
msgSend = append([]byte{0x00}, msgSend...)
}
return msgSend, nil
}
handleProtobufMsg := func(msg []byte) []byte {
decrypted, err := receiveCipher.Decrypt(nil, nil, msg)
require.NoError(t, err)
request := &messages.Request{}
require.NoError(t, proto.Unmarshal(decrypted, request))
require.NotNil(t, request)
response := handleRequest(request)
responseBytes, err := proto.Marshal(response)
require.NoError(t, err)
encrypted, err := sendCipher.Encrypt(nil, nil, responseBytes)
require.NoError(t, err)
if version.AtLeast(semver.NewSemVer(7, 0, 0)) {
// prepend OP_STATUS_SUCCESS
encrypted = append([]byte{0x00}, encrypted...)
}
return encrypted
}
switch msg[0] {
case byte('a'): // OP_ATTESTATION
if !version.AtLeast(semver.NewSemVer(2, 0, 0)) {
break
}
return make([]byte, 1+32+64+64+32+64), nil
case byte('u'): // OP_UNLOCK
if !version.AtLeast(semver.NewSemVer(2, 0, 0)) {
break
}
return []byte{0x02}, nil // OP_STATUS_FAILURE_UNINITIALIZED
case byte('h'): // OP_I_CAN_HAS_HANDSHAKE
shakingHands = true
return []byte{0x00}, nil // OP_STATUS_SUCCESS
case byte('v'): // OP_I_CAN_HAS_PAIRIN_VERIFICASHUN
// confirm pairing
return []byte{0x00}, nil // OP_STATUS_SUCCESS
case byte('n'): // OP_NOISE_MSG
if !version.AtLeast(semver.NewSemVer(4, 0, 0)) {
break
}
return handleProtobufMsg(msg[1:]), nil
}
return handleProtobufMsg(msg), nil
})
require.NoError(t, device.Init())
if version.AtLeast(lowestNonSupportedFirmwareVersion) {
require.Equal(t, StatusRequireAppUpgrade, device.Status())
return nil
}
// ChannelHashVerify calls DeviceInfo() to figure out if the device is initialized or not.
handleRequest = func(request *messages.Request) *messages.Response {
_, ok := request.Request.(*messages.Request_DeviceInfo)
require.True(t, ok)
return &messages.Response{
Response: &messages.Response_DeviceInfo{
DeviceInfo: &messages.DeviceInfoResponse{
Initialized: false,
},
},
}
}
device.ChannelHashVerify(true)
{ // Test upgrade required and actual upgrade, which for the firmware only means to reboot into the bootloader.
lowestSupported := map[common.Product]*semver.SemVer{
common.ProductBitBox02Multi: lowestSupportedFirmwareVersion,
common.ProductBitBox02BTCOnly: lowestSupportedFirmwareVersionBTCOnly,
common.ProductBitBoxBaseStandard: lowestSupportedFirmwareVersionBitBoxBaseStandard,
}
lowestSupportedFirmwareVersion, ok := lowestSupported[product]
require.True(t, ok)
if !version.AtLeast(lowestSupportedFirmwareVersion) {
require.Equal(t, StatusRequireFirmwareUpgrade, device.Status())
// Test upgrade.
// Expecting reboot command (with no response)
called := false
communication.MockQuery = v7_0_0Query(func(msg []byte) ([]byte, error) {
called = true
if version.AtLeast(semver.NewSemVer(4, 0, 0)) {
require.Equal(t, "n", string(msg[:1]), version) // OP_NOISE
msg = msg[1:]
}
decrypted, err := receiveCipher.Decrypt(nil, nil, msg)
require.NoError(t, err)
request := &messages.Request{}
require.NoError(t, proto.Unmarshal(decrypted, request))
require.NotNil(t, request)
_, ok := request.Request.(*messages.Request_Reboot)
require.True(t, ok)
return msg[:1], nil
})
// Actually, before v4.0.0 there was no opNoise, so the encrypted reboot command could
// by chance start with opUnlock or opAttestation, in which case those api endpoints
// would be called instead, resulting in an error in UpgradeFirmware(). We do not test
// this explicitly now, a re-try usually solves the issue for the user.
require.NoError(t, device.UpgradeFirmware())
require.True(t, called)
return nil
}
}
handleRequest = onRequest
return device
}
var testDeviceResponseOK = &messages.Response{
Response: &messages.Response_Success{
Success: &messages.Success{},
},
}
type testEnv struct {
version *semver.SemVer
product common.Product
communication *mocks.Communication
device *Device
onRequest func(*messages.Request) *messages.Response
}
func testConfigurations(t *testing.T, run func(*testing.T, *testEnv)) {
t.Helper()
versions := []*semver.SemVer{
semver.NewSemVer(1, 0, 0),
semver.NewSemVer(2, 0, 0),
semver.NewSemVer(3, 0, 0),
semver.NewSemVer(4, 1, 0),
semver.NewSemVer(4, 1, 1),
semver.NewSemVer(4, 2, 0),
semver.NewSemVer(4, 2, 1),
semver.NewSemVer(4, 3, 0),
semver.NewSemVer(5, 0, 0),
semver.NewSemVer(6, 0, 0),
semver.NewSemVer(7, 0, 0),
semver.NewSemVer(8, 0, 0),
semver.NewSemVer(9, 1, 0),
semver.NewSemVer(9, 2, 0),
semver.NewSemVer(9, 4, 0),
semver.NewSemVer(9, 5, 0),
lowestNonSupportedFirmwareVersion,
}
products := []common.Product{
common.ProductBitBox02Multi,
common.ProductBitBox02BTCOnly,
common.ProductBitBoxBaseStandard,
}
for _, version := range versions {
for _, product := range products {
var env testEnv
env.version = version
env.product = product
env.communication = &mocks.Communication{}
env.device = newDevice(
t,
env.version,
product,
env.communication,
func(request *messages.Request) *messages.Response { return env.onRequest(request) },
)
// Device could not be initialized (unit tests for this in `newDevice()`), so there is
// nothing more to do.
if env.device == nil {
continue
}
t.Run(fmt.Sprintf("%v %s", env, env.version), func(t *testing.T) {
run(t, &env)
})
}
}
}
func TestVersion(t *testing.T) {
testConfigurations(t, func(t *testing.T, env *testEnv) {
t.Helper()
require.Equal(t, env.version, env.device.Version())
})
}
func TestProduct(t *testing.T) {
testConfigurations(t, func(t *testing.T, env *testEnv) {
t.Helper()
require.Equal(t, env.product, env.device.Product())
})
}
func TestClose(t *testing.T) {
testConfigurations(t, func(t *testing.T, env *testEnv) {
t.Helper()
called := false
env.communication.MockClose = func() { called = true }
env.device.Close()
require.True(t, called)
})
}
func TestSetDeviceName(t *testing.T) {
testConfigurations(t, func(t *testing.T, env *testEnv) {
t.Helper()
// Name too long.
require.Error(t, env.device.SetDeviceName(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
expected := "Satoshi"
env.onRequest = func(request *messages.Request) *messages.Response {
setDeviceName, ok := request.Request.(*messages.Request_DeviceName)
require.True(t, ok)
require.Equal(t, expected, setDeviceName.DeviceName.Name)
return testDeviceResponseOK
}
require.NoError(t, env.device.SetDeviceName(expected))
// Wrong response.
env.onRequest = func(request *messages.Request) *messages.Response {
return &messages.Response{
Response: &messages.Response_DeviceInfo{
DeviceInfo: &messages.DeviceInfoResponse{},
},
}
}
require.Error(t, env.device.SetDeviceName(expected))
// Query error.
expectedErr := errors.New("error")
env.communication.MockQuery = func(msg []byte) ([]byte, error) {
return nil, expectedErr
}
require.Equal(t, expectedErr, env.device.SetDeviceName(expected))
})
}