-
Notifications
You must be signed in to change notification settings - Fork 1
/
messages_test.go
125 lines (98 loc) · 2.57 KB
/
messages_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
// SPDX-FileCopyrightText: 2023 Steffen Vogel <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package rosenpass
import (
"crypto/rand"
"testing"
"github.com/stretchr/testify/require"
)
func TestMessages(t *testing.T) {
rand := func(n int) []byte {
b := make([]byte, n)
if _, err := rand.Read(b); err != nil {
t.Fatalf("Failed to generate test data: %s", err)
}
return b
}
t.Run("InitHello", func(t *testing.T) {
require := require.New(t)
var err error
var m2 initHello
m1 := initHello{
sidi: sid(rand(sidSize)),
epki: epk(rand(epkSize)),
sctr: sct(rand(sctSize)),
pidiC: [pidSize + authSize]byte(rand(pidSize + authSize)),
auth: authTag(rand(authSize)),
}
buf := m1.MarshalBinary()
require.Len(buf, initHelloMsgSize)
sz, err := m2.UnmarshalBinary(buf)
require.NoError(err)
require.Equal(initHelloMsgSize, sz)
require.Equal(m1, m2)
})
t.Run("RespHello", func(t *testing.T) {
require := require.New(t)
var err error
var m2 respHello
m1 := respHello{
sidr: sid(rand(sidSize)),
sidi: sid(rand(sidSize)),
ecti: ect(rand(ectSize)),
scti: sct(rand(sctSize)),
auth: authTag(rand(authSize)),
biscuit: sealedBiscuit(rand(sealedBiscuitSize)),
}
buf := m1.MarshalBinary()
require.Len(buf, respHelloMsgSize)
sz, err := m2.UnmarshalBinary(buf)
require.NoError(err)
require.Equal(respHelloMsgSize, sz)
require.Equal(m1, m2)
})
t.Run("InitConf", func(t *testing.T) {
require := require.New(t)
var err error
var m2 initConf
m1 := initConf{
sidi: sid(rand(sidSize)),
sidr: sid(rand(sidSize)),
biscuit: sealedBiscuit(rand(sealedBiscuitSize)),
auth: authTag(rand(authSize)),
}
m1.sidi, err = generateSessionID()
require.NoError(err)
buf := m1.MarshalBinary()
require.Len(buf, initConfMsgSize)
sz, err := m2.UnmarshalBinary(buf)
require.NoError(err)
require.Equal(initConfMsgSize, sz)
require.Equal(m1, m2)
})
t.Run("EmptyData", func(t *testing.T) {
require := require.New(t)
var err error
var m2 emptyData
m1 := emptyData{
sid: sid(rand(sidSize)),
ctr: txNonce(rand(txNonceSize)),
auth: authTag(rand(authSize)),
}
m1.sid, err = generateSessionID()
require.NoError(err)
buf := m1.MarshalBinary()
require.Len(buf, emptyDataMsgSize)
sz, err := m2.UnmarshalBinary(buf)
require.NoError(err)
require.Equal(emptyDataMsgSize, sz)
require.Equal(m1, m2)
})
}
func FuzzEnvelope(f *testing.F) {
f.Add([]byte{})
f.Fuzz(func(_ *testing.T, b []byte) {
var e envelope
e.UnmarshalBinary(b) //nolint:errcheck
})
}