-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmsg_auth_ch_test.go
More file actions
122 lines (109 loc) · 2.9 KB
/
msg_auth_ch_test.go
File metadata and controls
122 lines (109 loc) · 2.9 KB
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
package wire
import (
"bytes"
"errors"
"io"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestNewMsgAuthchSetsFields verifies the constructor and basic accessors.
func TestNewMsgAuthchSetsFields(t *testing.T) {
msg := NewMsgAuthch("hello")
assert.Equal(t, int32(1), msg.Version)
assert.Equal(t, uint32(5), msg.Length)
assert.Equal(t, []byte("hello"), msg.Challenge)
assert.Equal(t, CmdAuthch, msg.Command())
assert.Equal(t, uint64(40), msg.MaxPayloadLength(ProtocolVersion))
}
// TestMsgAuthchWire tests encode and decode round trip.
func TestMsgAuthchWire(t *testing.T) {
orig := NewMsgAuthch("challenge")
var buf bytes.Buffer
require.NoError(t, orig.BsvEncode(&buf, ProtocolVersion, BaseEncoding))
var decoded MsgAuthch
require.NoError(t, decoded.Bsvdecode(&buf, ProtocolVersion, BaseEncoding))
assert.Equal(t, orig.Version, decoded.Version)
assert.Equal(t, uint32(len(decoded.Challenge)), decoded.Length)
assert.NotEmpty(t, decoded.Challenge)
}
// TestMsgAuthchWireErrors exercises error paths for encoding and decoding.
func TestMsgAuthchWireErrors(t *testing.T) {
base := NewMsgAuthch("abcde")
var b bytes.Buffer
require.NoError(t, base.BsvEncode(&b, ProtocolVersion, BaseEncoding))
encoded := b.Bytes()
longMsg := NewMsgAuthch(strings.Repeat("z", 41))
var bLong bytes.Buffer
require.NoError(t, longMsg.BsvEncode(&bLong, ProtocolVersion, BaseEncoding))
longEncoded := bLong.Bytes()
wireErr := &MessageError{}
tests := []struct {
name string
in *MsgAuthch
buf []byte
max int
writeErr error
readErr error
}{
{
name: "short write version",
in: base,
buf: encoded,
max: 0,
writeErr: io.ErrShortWrite,
readErr: io.EOF,
},
{
name: "short write length",
in: base,
buf: encoded,
max: 4,
writeErr: io.ErrShortWrite,
readErr: io.EOF,
},
{
name: "short write challenge",
in: base,
buf: encoded,
max: 8,
writeErr: io.ErrShortWrite,
readErr: io.ErrUnexpectedEOF,
},
{
name: "challenge too large",
in: longMsg,
buf: longEncoded,
max: len(longEncoded),
writeErr: nil,
readErr: wireErr,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := newFixedWriter(tt.max)
err := tt.in.BsvEncode(w, ProtocolVersion, BaseEncoding)
if tt.writeErr != nil {
require.Error(t, err)
require.ErrorIs(t, err, tt.writeErr)
} else {
require.NoError(t, err)
}
var msg MsgAuthch
r := newFixedReader(tt.max, tt.buf)
err = msg.Bsvdecode(r, ProtocolVersion, BaseEncoding)
if tt.readErr != nil {
require.Error(t, err)
var mErr *MessageError
if errors.As(tt.readErr, &mErr) {
assert.ErrorAs(t, err, &mErr)
} else {
assert.ErrorIs(t, err, tt.readErr)
}
} else {
require.NoError(t, err)
}
})
}
}