-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathclient_test.go
More file actions
198 lines (173 loc) · 4.53 KB
/
client_test.go
File metadata and controls
198 lines (173 loc) · 4.53 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
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
package fulamobile
import (
"crypto/rand"
"testing"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestNewClient tests client creation with various configurations
func TestNewClient(t *testing.T) {
tests := []struct {
name string
config *Config
wantErr bool
errString string
}{
{
name: "valid_noop_config",
config: &Config{
Exchange: "noop",
StorePath: t.TempDir(),
PoolName: "1",
SyncWrites: false,
},
wantErr: false,
},
{
name: "missing_blox_addr",
config: &Config{
Exchange: "fx",
StorePath: t.TempDir(),
PoolName: "1",
BloxAddr: "", // Missing BloxAddr for non-noop exchange
},
wantErr: true,
errString: "BloxAddr must be specified",
},
{
name: "empty_store_path_uses_temp",
config: &Config{
Exchange: "noop",
StorePath: "",
PoolName: "1",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client, err := NewClient(tt.config)
// Always try to cleanup client if it was created, even on error
defer func() {
if client != nil {
if shutdownErr := client.Shutdown(); shutdownErr != nil {
t.Logf("Warning: failed to shutdown client: %v", shutdownErr)
}
}
}()
if tt.wantErr {
assert.Error(t, err)
if tt.errString != "" {
assert.Contains(t, err.Error(), tt.errString)
}
assert.Nil(t, client)
} else {
assert.NoError(t, err)
assert.NotNil(t, client)
// Test basic client properties
assert.NotEmpty(t, client.ID())
assert.NotNil(t, client.h)
assert.NotNil(t, client.ds)
assert.NotNil(t, client.ex)
}
})
}
}
// TestClientFlush tests the flush operation
func TestClientFlush(t *testing.T) {
config := &Config{
Exchange: "noop",
StorePath: t.TempDir(),
PoolName: "1",
SyncWrites: false, // Disable sync writes to test flush
}
client, err := NewClient(config)
require.NoError(t, err)
defer func() {
if shutdownErr := client.Shutdown(); shutdownErr != nil {
t.Logf("Warning: failed to shutdown client: %v", shutdownErr)
}
}()
// Test flush operation
err = client.Flush()
assert.NoError(t, err)
}
// TestClientSetAuth tests authorization operations
func TestClientSetAuth(t *testing.T) {
config := &Config{
Exchange: "noop",
StorePath: t.TempDir(),
PoolName: "1",
SyncWrites: true,
}
client, err := NewClient(config)
require.NoError(t, err)
defer func() {
if shutdownErr := client.Shutdown(); shutdownErr != nil {
t.Logf("Warning: failed to shutdown client: %v", shutdownErr)
}
}()
// Generate test peer IDs
priv1, _, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, rand.Reader)
require.NoError(t, err)
priv2, _, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, rand.Reader)
require.NoError(t, err)
h1, err := libp2p.New(libp2p.Identity(priv1))
require.NoError(t, err)
defer h1.Close()
h2, err := libp2p.New(libp2p.Identity(priv2))
require.NoError(t, err)
defer h2.Close()
// Test setting authorization
err = client.SetAuth(h1.ID().String(), h2.ID().String(), true)
assert.NoError(t, err)
// Test removing authorization
err = client.SetAuth(h1.ID().String(), h2.ID().String(), false)
assert.NoError(t, err)
}
// TestClientInvalidSetAuth tests error handling for invalid SetAuth inputs
func TestClientInvalidSetAuth(t *testing.T) {
config := &Config{
Exchange: "noop",
StorePath: t.TempDir(),
PoolName: "1",
SyncWrites: true,
}
client, err := NewClient(config)
require.NoError(t, err)
defer func() {
if shutdownErr := client.Shutdown(); shutdownErr != nil {
t.Logf("Warning: failed to shutdown client: %v", shutdownErr)
}
}()
// Test SetAuth with invalid peer IDs
err = client.SetAuth("invalid-peer-id", "another-invalid-peer-id", true)
assert.Error(t, err)
}
// TestClientID tests peer ID functionality
func TestClientID(t *testing.T) {
config := &Config{
Exchange: "noop",
StorePath: t.TempDir(),
PoolName: "1",
SyncWrites: true,
}
client, err := NewClient(config)
require.NoError(t, err)
defer func() {
if shutdownErr := client.Shutdown(); shutdownErr != nil {
t.Logf("Warning: failed to shutdown client: %v", shutdownErr)
}
}()
// Test that ID returns a valid peer ID string
id := client.ID()
assert.NotEmpty(t, id)
// Test that the ID is consistent
id2 := client.ID()
assert.Equal(t, id, id2)
// Test that we can decode the peer ID
_, err = client.h.ID().MarshalText()
assert.NoError(t, err)
}