-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbus_test.go
More file actions
382 lines (313 loc) · 8.4 KB
/
Copy pathbus_test.go
File metadata and controls
382 lines (313 loc) · 8.4 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package voidbus
import (
"errors"
"testing"
"time"
"github.com/Script-OS/VoidBus/codec/plain"
)
// === New and NewWithConfig Tests ===
func TestNew(t *testing.T) {
bus, err := New(nil)
if err != nil {
t.Fatalf("expected New() to succeed, got error: %v", err)
}
if bus == nil {
t.Fatal("expected non-nil bus")
}
if bus.config == nil {
t.Error("expected config to be set")
}
}
func TestNewWithConfig_Valid(t *testing.T) {
config := DefaultBusConfig()
bus, err := New(config)
if err != nil {
t.Fatalf("expected New(config) to succeed, got error: %v", err)
}
if bus == nil {
t.Fatal("expected non-nil bus")
}
}
func TestNewWithConfig_InvalidMaxCodecDepth(t *testing.T) {
config := DefaultBusConfig()
config.MaxCodecDepth = 0 // Invalid
_, err := New(config)
if err == nil {
t.Fatal("expected error for invalid MaxCodecDepth")
}
// Verify error type
var voidBusErr *VoidBusError
if !errors.As(err, &voidBusErr) {
t.Errorf("expected VoidBusError type, got %T", err)
}
}
func TestNewWithConfig_InvalidMTU(t *testing.T) {
config := DefaultBusConfig()
config.DefaultMTU = 10 // Less than MinMTU
_, err := New(config)
if err == nil {
t.Fatal("expected error for invalid MTU")
}
}
func TestNewWithConfig_InvalidTimeout(t *testing.T) {
config := DefaultBusConfig()
config.FragmentTimeout = 0 // Invalid
_, err := New(config)
if err == nil {
t.Fatal("expected error for invalid FragmentTimeout")
}
}
// === SetKey Tests ===
func TestSetKey_ValidKey(t *testing.T) {
bus, err := New(nil)
if err != nil {
t.Fatalf("New() failed: %v", err)
}
// 32-byte key for AES-256-GCM
key := []byte("32-byte-secret-key-for-aes-256!!")
err = bus.SetKey(key)
if err != nil {
t.Fatalf("expected SetKey() to succeed, got error: %v", err)
}
if bus.keyProvider == nil {
t.Error("expected keyProvider to be set")
}
}
func TestSetKey_InvalidKey(t *testing.T) {
bus, err := New(nil)
if err != nil {
t.Fatalf("New() failed: %v", err)
}
// Test nil key
err = bus.SetKey(nil)
if err == nil {
t.Fatal("expected error for nil key")
}
// Verify error is wrapped
var voidBusErr *VoidBusError
if !errors.As(err, &voidBusErr) {
t.Errorf("expected VoidBusError type, got %T", err)
}
if GetModule(err) != "keyprovider" {
t.Errorf("expected module 'keyprovider', got '%s'", GetModule(err))
}
// Test empty key
err = bus.SetKey([]byte{})
if err == nil {
t.Fatal("expected error for empty key")
}
}
func TestSetKey_EmptyKey(t *testing.T) {
bus, err := New(nil)
if err != nil {
t.Fatalf("New() failed: %v", err)
}
err = bus.SetKey([]byte{})
if err == nil {
t.Fatal("expected error for empty key")
}
}
func TestSetKeyProvider(t *testing.T) {
bus, err := New(nil)
if err != nil {
t.Fatalf("New() failed: %v", err)
}
// SetKey now accepts a byte slice directly
err = bus.SetKey(nil)
if err == nil {
t.Fatal("expected error for nil key")
}
}
// === Codec Tests ===
func TestAddCodec(t *testing.T) {
bus, err := New(nil)
if err != nil {
t.Fatalf("New() failed: %v", err)
}
// Add plain codec (no key required)
plainCodec := plain.New()
err = bus.RegisterCodec(plainCodec)
if err != nil {
t.Fatalf("expected RegisterCodec() to succeed, got error: %v", err)
}
// Verify codec was added
availableCodes := bus.codecManager.GetAvailableCodes()
if len(availableCodes) != 1 {
t.Errorf("expected 1 codec, got %d", len(availableCodes))
}
if availableCodes[0] != "plain" {
t.Errorf("expected code 'plain', got '%s'", availableCodes[0])
}
}
func TestAddCodec_DuplicateCode(t *testing.T) {
bus, err := New(nil)
if err != nil {
t.Fatalf("New() failed: %v", err)
}
plainCodec1 := plain.New()
err = bus.RegisterCodec(plainCodec1)
if err != nil {
t.Fatalf("first RegisterCodec() failed: %v", err)
}
// Add duplicate code (same codec instance is ok, different instance with same code is error)
plainCodec2 := plain.New()
plainCodec2.SetCode("plain") // Same code as plainCodec1
err = bus.RegisterCodec(plainCodec2)
if err == nil {
t.Fatal("expected error for duplicate codec code")
}
}
func TestSetMaxCodecDepth(t *testing.T) {
bus, err := New(nil)
if err != nil {
t.Fatalf("New() failed: %v", err)
}
err = bus.SetMaxCodecDepth(3)
if err != nil {
t.Fatalf("expected SetMaxCodecDepth() to succeed, got error: %v", err)
}
if bus.codecManager.GetMaxDepth() != 3 {
t.Errorf("expected max depth 3, got %d", bus.codecManager.GetMaxDepth())
}
}
func TestSetMaxCodecDepth_Invalid(t *testing.T) {
bus, err := New(nil)
if err != nil {
t.Fatalf("New() failed: %v", err)
}
// Invalid depth (too large)
err = bus.SetMaxCodecDepth(100)
if err == nil {
t.Fatal("expected error for invalid max depth")
}
}
// === Validate Tests ===
// TestValidate_NoCodec - Validate method removed
// TestValidate_NoChannel - Validate method removed
// TestValidate_Valid - Validate method removed
// === Stats Tests ===
// TestStats - Stats method removed
// TestModuleStats - ModuleStats method removed
// === Lifecycle Tests ===
// TestBus_Name - Name method removed
// TestBus_IsRunning - IsRunning method removed
// TestBus_IsConnected - IsConnected method removed
// TestBus_GetConfig - GetConfig method removed
// === GetNegotiationInfo Tests ===
// TestBus_GetNegotiationInfo - GetNegotiationInfo method removed
// === Connect Tests ===
// TestBus_Connect - Connect method removed
// TestBus_Connect_AlreadyConnected - Connect method removed
// === Error Handler Tests ===
// TestBus_OnMessage - OnMessage method removed
// TestBus_OnError - OnError method removed
// === Config Tests ===
func TestDefaultBusConfig(t *testing.T) {
config := DefaultBusConfig()
if config.MaxCodecDepth != 2 {
t.Errorf("expected MaxCodecDepth=2, got %d", config.MaxCodecDepth)
}
if config.DefaultMTU != 1024 {
t.Errorf("expected DefaultMTU=1024, got %d", config.DefaultMTU)
}
if config.ReceiveMode != ReceiveModeBlocking {
t.Errorf("expected ReceiveModeBlocking, got %d", config.ReceiveMode)
}
}
func TestBusConfig_Validate(t *testing.T) {
// Valid config
validConfig := DefaultBusConfig()
err := validConfig.Validate()
if err != nil {
t.Fatalf("expected valid config to pass: %v", err)
}
// Invalid MaxCodecDepth (too small)
invalidConfig1 := DefaultBusConfig()
invalidConfig1.MaxCodecDepth = 0
err = invalidConfig1.Validate()
if err == nil {
t.Error("expected error for MaxCodecDepth=0")
}
// Invalid MaxCodecDepth (too large)
invalidConfig2 := DefaultBusConfig()
invalidConfig2.MaxCodecDepth = 10
err = invalidConfig2.Validate()
if err == nil {
t.Error("expected error for MaxCodecDepth=10")
}
// Invalid MTU
invalidConfig3 := DefaultBusConfig()
invalidConfig3.DefaultMTU = 10 // Less than MinMTU
err = invalidConfig3.Validate()
if err == nil {
t.Error("expected error for invalid MTU")
}
// Invalid FragmentTimeout
invalidConfig4 := DefaultBusConfig()
invalidConfig4.FragmentTimeout = 0
err = invalidConfig4.Validate()
if err == nil {
t.Error("expected error for invalid FragmentTimeout")
}
// Invalid MaxRetransmit
invalidConfig5 := DefaultBusConfig()
invalidConfig5.MaxRetransmit = -1
err = invalidConfig5.Validate()
if err == nil {
t.Error("expected error for negative MaxRetransmit")
}
}
func TestNegotiationConfig_Validate(t *testing.T) {
// Valid config
validConfig := &NegotiationConfig{
SupportedCodes: []string{"A", "B"},
MaxDepth: 2,
NegotiatedAt: time.Now(),
}
err := validConfig.Validate()
if err != nil {
t.Fatalf("expected valid config to pass: %v", err)
}
// Invalid - empty codes
invalidConfig1 := &NegotiationConfig{
SupportedCodes: []string{},
MaxDepth: 2,
}
err = invalidConfig1.Validate()
if err == nil {
t.Error("expected error for empty SupportedCodes")
}
// Invalid - depth too small
invalidConfig2 := &NegotiationConfig{
SupportedCodes: []string{"A"},
MaxDepth: 0,
}
err = invalidConfig2.Validate()
if err == nil {
t.Error("expected error for MaxDepth=0")
}
}
// === Module Registry Tests ===
// All module registry tests removed - Bus no longer implements Module interface
// === Benchmark Tests ===
func BenchmarkNew(b *testing.B) {
for i := 0; i < b.N; i++ {
New(nil)
}
}
func BenchmarkSetKey(b *testing.B) {
bus, _ := New(nil)
key := []byte("32-byte-secret-key-for-aes-256!!")
b.ResetTimer()
for i := 0; i < b.N; i++ {
bus.SetKey(key)
}
}
func BenchmarkAddCodec(b *testing.B) {
bus, _ := New(nil)
plainCodec := plain.New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
bus.RegisterCodec(plainCodec)
}
}