-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils_test.go
99 lines (84 loc) · 3.45 KB
/
utils_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
package p2pnet
import (
"bytes"
"encoding/binary"
"io"
"path"
"testing"
"github.com/stretchr/testify/require"
)
func TestGenerateAndSaveKey(t *testing.T) {
filePath := path.Join(t.TempDir(), "net.key")
key1, err := generateKey(filePath)
require.NoError(t, err)
// verify that we can't accidentally overwrite an existing key
_, err = generateKey(filePath)
require.ErrorContains(t, err, "file exists")
// create a key under a new path and ensure that it is different
key2, err := generateKey(path.Join(t.TempDir(), "net.key"))
require.NoError(t, err)
require.False(t, key1.Equals(key2))
}
func Test_stringsToAddrInfos(t *testing.T) {
bootnodes := []string{
"/ip4/192.168.0.101/udp/9934/quic-v1/p2p/12D3KooWC547RfLcveQi1vBxACjnT6Uv15V11ortDTuxRWuhubGv",
"/ip4/192.168.0.101/tcp/9934/p2p/12D3KooWC547RfLcveQi1vBxACjnT6Uv15V11ortDTuxRWuhubGv",
}
addrInfos, err := stringsToAddrInfos(bootnodes)
require.NoError(t, err)
require.Len(t, addrInfos, 1) // both were combined into one AddrInfo
require.Equal(t, "12D3KooWC547RfLcveQi1vBxACjnT6Uv15V11ortDTuxRWuhubGv",
addrInfos[0].ID.String())
require.Len(t, addrInfos[0].Addrs, 2)
require.Equal(t, "/ip4/192.168.0.101/udp/9934/quic-v1", addrInfos[0].Addrs[0].String())
require.Equal(t, "/ip4/192.168.0.101/tcp/9934", addrInfos[0].Addrs[1].String())
}
func Test_readStreamMessage(t *testing.T) {
msgBytes := []byte("testmessage")
var lenBytes [4]byte
binary.LittleEndian.PutUint32(lenBytes[:], uint32(len(msgBytes)))
streamData := append(lenBytes[:], msgBytes...)
stream := bytes.NewReader(streamData)
readMsg, err := ReadStreamMessage(stream, testMaxMessageSize)
require.NoError(t, err)
require.Equal(t, msgBytes, readMsg)
}
func Test_readStreamMessage_EOF(t *testing.T) {
// If the stream is closed before we read a length value, no message was truncated and
// the returned error is io.EOF
stream := bytes.NewReader(nil)
_, err := ReadStreamMessage(stream, testMaxMessageSize)
require.ErrorIs(t, err, io.EOF) // connection closed before we read any length
// If the message was truncated either in the length or body, the error is io.ErrUnexpectedEOF
serializedData := []byte{0x1} // truncated length
stream = bytes.NewReader(serializedData)
_, err = ReadStreamMessage(stream, testMaxMessageSize)
require.ErrorIs(t, err, io.ErrUnexpectedEOF) // connection after we read at least one byte
serializedData = []byte{0x1, 0, 0, 0} // truncated encoded message
stream = bytes.NewReader(serializedData)
_, err = ReadStreamMessage(stream, testMaxMessageSize)
require.ErrorIs(t, err, io.ErrUnexpectedEOF) // connection after we read at least one byte
}
func Test_readStreamMessage_TooLarge(t *testing.T) {
buf := make([]byte, 4+testMaxMessageSize+1)
binary.LittleEndian.PutUint32(buf, testMaxMessageSize+1)
_, err := ReadStreamMessage(bytes.NewReader(buf), testMaxMessageSize)
require.ErrorContains(t, err, "too large")
}
func Test_readStreamMessage_NilStream(t *testing.T) {
// Can our code actually trigger this error?
_, err := ReadStreamMessage(nil, testMaxMessageSize)
require.ErrorIs(t, err, errNilStream)
}
func Test_writeStreamMessage(t *testing.T) {
msg := []byte("testmessage")
stream := &bytes.Buffer{}
err := WriteStreamBytes(stream, msg)
require.NoError(t, err)
serializedData := stream.Bytes()
require.Greater(t, len(serializedData), 4)
lenMsg := binary.LittleEndian.Uint32(serializedData)
msgBytes := serializedData[4:]
require.Equal(t, int(lenMsg), len(msgBytes))
require.Equal(t, msg, msgBytes)
}