-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_test.go
More file actions
531 lines (453 loc) · 13.1 KB
/
agent_test.go
File metadata and controls
531 lines (453 loc) · 13.1 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
package agent
import (
"context"
"testing"
"time"
"github.com/peerclaw/peerclaw-agent/peer"
"github.com/peerclaw/peerclaw-agent/security"
"github.com/peerclaw/peerclaw-core/envelope"
"github.com/peerclaw/peerclaw-core/identity"
"github.com/peerclaw/peerclaw-core/protocol"
)
func TestNew_GeneratesKeypair(t *testing.T) {
a, err := New(Options{
Name: "TestAgent",
ServerURL: "http://localhost:8080",
})
if err != nil {
t.Fatalf("New: %v", err)
}
if a.PublicKey() == "" {
t.Error("expected non-empty public key")
}
}
func TestNew_WithKeypairPath(t *testing.T) {
dir := t.TempDir()
path := dir + "/keypair.seed"
a1, err := New(Options{
Name: "TestAgent",
ServerURL: "http://localhost:8080",
KeypairPath: path,
})
if err != nil {
t.Fatalf("New (create): %v", err)
}
pk1 := a1.PublicKey()
a2, err := New(Options{
Name: "TestAgent",
ServerURL: "http://localhost:8080",
KeypairPath: path,
})
if err != nil {
t.Fatalf("New (load): %v", err)
}
pk2 := a2.PublicKey()
if pk1 != pk2 {
t.Error("loaded keypair should produce same public key")
}
}
func TestAgent_E2EEncryptionRoundTrip(t *testing.T) {
// Create two agents.
a1, err := New(Options{
Name: "Agent1",
ServerURL: "http://localhost:8080",
})
if err != nil {
t.Fatalf("New Agent1: %v", err)
}
a2, err := New(Options{
Name: "Agent2",
ServerURL: "http://localhost:8080",
})
if err != nil {
t.Fatalf("New Agent2: %v", err)
}
// Exchange X25519 public keys and establish sessions.
a1X25519, err := a1.X25519PublicKeyString()
if err != nil {
t.Fatalf("a1.X25519PublicKeyString: %v", err)
}
a2X25519, err := a2.X25519PublicKeyString()
if err != nil {
t.Fatalf("a2.X25519PublicKeyString: %v", err)
}
// Use public keys as peer IDs for this test.
peer1ID := a1.PublicKey()
peer2ID := a2.PublicKey()
if err := a1.EstablishSession(peer2ID, a2X25519); err != nil {
t.Fatalf("a1.EstablishSession: %v", err)
}
if err := a2.EstablishSession(peer1ID, a1X25519); err != nil {
t.Fatalf("a2.EstablishSession: %v", err)
}
// Create an envelope from a1 to a2.
payload := []byte(`{"message": "hello from agent 1"}`)
env := envelope.New(peer1ID, peer2ID, protocol.ProtocolA2A, payload)
// Send encrypts the payload.
ctx := context.Background()
// We can't actually send (no peer connected), but we can test the encrypt flow directly.
env.Signature = "test-sig" // skip real signing for this test
a1.mu.Lock()
sk := a1.sessionKeys[peer2ID]
a1.mu.Unlock()
aad := []byte(env.Source + "|" + env.Destination + "|" + env.Nonce)
encrypted, err := sk.EncryptWithAAD(env.Payload, aad)
if err != nil {
t.Fatalf("Encrypt: %v", err)
}
env.Payload = encrypted
env.Encrypted = true
env.SenderX25519 = a1X25519
// Agent 2 receives and decrypts.
decrypted, err := a2.DecryptEnvelope(env)
if err != nil {
t.Fatalf("DecryptEnvelope: %v", err)
}
if decrypted.Encrypted {
t.Error("envelope should be decrypted")
}
if string(decrypted.Payload) != string(payload) {
t.Errorf("payload = %q, want %q", string(decrypted.Payload), string(payload))
}
// Test HandleIncomingEnvelope.
// Whitelist peer1ID in a2's trust store and register peer with public key.
a2.AddContact(peer1ID)
a2.peerManager.AddPeer(&peer.Peer{
ID: peer1ID,
PublicKey: a1.PublicKey(),
})
var received *envelope.Envelope
a2.OnMessage(func(_ context.Context, e *envelope.Envelope) {
received = e
})
env2 := envelope.New(peer1ID, peer2ID, protocol.ProtocolA2A, payload)
env2.Nonce = "e2e-nonce-test-001"
env2.Timestamp = time.Now()
// Encrypt-then-sign: encrypt payload first, then sign the ciphertext.
aad2 := []byte(env2.Source + "|" + env2.Destination + "|" + env2.Nonce)
encrypted2, _ := sk.EncryptWithAAD(env2.Payload, aad2)
env2.Payload = encrypted2
env2.Encrypted = true
env2.SenderX25519 = a1X25519
if err := identity.SignEnvelope(env2, a1.keypair.PrivateKey); err != nil {
t.Fatalf("SignEnvelope: %v", err)
}
a2.HandleIncomingEnvelope(ctx, env2)
if received == nil {
t.Fatal("handler was not called")
}
if string(received.Payload) != string(payload) {
t.Errorf("received payload = %q, want %q", string(received.Payload), string(payload))
}
}
func TestAgent_DecryptEnvelope_NoSessionKey(t *testing.T) {
a, _ := New(Options{
Name: "Agent",
ServerURL: "http://localhost:8080",
})
env := &envelope.Envelope{
Source: "unknown-peer",
Encrypted: true,
Payload: []byte("encrypted-data"),
}
_, err := a.DecryptEnvelope(env)
if err == nil {
t.Error("expected error when no session key exists")
}
}
func TestAgent_DecryptEnvelope_Unencrypted(t *testing.T) {
a, _ := New(Options{
Name: "Agent",
ServerURL: "http://localhost:8080",
})
payload := []byte("plaintext")
env := &envelope.Envelope{
Encrypted: false,
Payload: payload,
}
result, err := a.DecryptEnvelope(env)
if err != nil {
t.Fatalf("DecryptEnvelope: %v", err)
}
if string(result.Payload) != string(payload) {
t.Error("unencrypted envelope should pass through unchanged")
}
}
func TestHandleIncomingEnvelope_RejectsNonWhitelisted(t *testing.T) {
a, _ := New(Options{
Name: "Agent",
ServerURL: "http://localhost:8080",
})
var received bool
a.OnMessage(func(_ context.Context, _ *envelope.Envelope) {
received = true
})
env := &envelope.Envelope{
Source: "untrusted-peer",
Payload: []byte("hello"),
Timestamp: time.Now(),
}
a.HandleIncomingEnvelope(context.Background(), env)
if received {
t.Error("handler should not be called for non-whitelisted peer")
}
}
func TestHandleIncomingEnvelope_AcceptsWhitelisted(t *testing.T) {
a, _ := New(Options{
Name: "Agent",
ServerURL: "http://localhost:8080",
})
kp, err := identity.GenerateKeypair()
if err != nil {
t.Fatalf("GenerateKeypair: %v", err)
}
// Whitelist the peer and register its public key.
a.AddContact("trusted-peer")
a.peerManager.AddPeer(&peer.Peer{
ID: "trusted-peer",
PublicKey: kp.PublicKeyString(),
})
var received bool
a.OnMessage(func(_ context.Context, _ *envelope.Envelope) {
received = true
})
env := envelope.New("trusted-peer", "Agent", protocol.ProtocolA2A, []byte("hello"))
env.Nonce = "whitelist-nonce-1"
env.Timestamp = time.Now()
if err := identity.SignEnvelope(env, kp.PrivateKey); err != nil {
t.Fatalf("SignEnvelope: %v", err)
}
a.HandleIncomingEnvelope(context.Background(), env)
if !received {
t.Error("handler should be called for whitelisted peer")
}
}
func TestHandleIncomingEnvelope_RejectsExpiredTimestamp(t *testing.T) {
a, _ := New(Options{
Name: "Agent",
ServerURL: "http://localhost:8080",
})
a.AddContact("peer-1")
var received bool
a.OnMessage(func(_ context.Context, _ *envelope.Envelope) {
received = true
})
env := &envelope.Envelope{
Source: "peer-1",
Payload: []byte("hello"),
Timestamp: time.Now().Add(-10 * time.Minute), // way outside 2-min skew
}
a.HandleIncomingEnvelope(context.Background(), env)
if received {
t.Error("handler should not be called for expired timestamp")
}
}
func TestHandleIncomingEnvelope_RejectsReplayedNonce(t *testing.T) {
a, _ := New(Options{
Name: "Agent",
ServerURL: "http://localhost:8080",
})
kp, err := identity.GenerateKeypair()
if err != nil {
t.Fatalf("GenerateKeypair: %v", err)
}
a.AddContact("peer-1")
a.peerManager.AddPeer(&peer.Peer{
ID: "peer-1",
PublicKey: kp.PublicKeyString(),
})
callCount := 0
a.OnMessage(func(_ context.Context, _ *envelope.Envelope) {
callCount++
})
env1 := envelope.New("peer-1", "Agent", protocol.ProtocolA2A, []byte("hello"))
env1.Nonce = "unique-nonce-123"
env1.Timestamp = time.Now()
if err := identity.SignEnvelope(env1, kp.PrivateKey); err != nil {
t.Fatalf("SignEnvelope: %v", err)
}
a.HandleIncomingEnvelope(context.Background(), env1)
if callCount != 1 {
t.Fatalf("expected 1 call, got %d", callCount)
}
// Replay same envelope (same nonce).
a.HandleIncomingEnvelope(context.Background(), env1)
if callCount != 1 {
t.Errorf("replayed message should be rejected, got %d calls", callCount)
}
}
func TestHandleIncomingEnvelope_RejectsBlockedPeer(t *testing.T) {
a, _ := New(Options{
Name: "Agent",
ServerURL: "http://localhost:8080",
})
a.BlockAgent("blocked-peer")
var received bool
a.OnMessage(func(_ context.Context, _ *envelope.Envelope) {
received = true
})
env := &envelope.Envelope{
Source: "blocked-peer",
Payload: []byte("hello"),
Timestamp: time.Now(),
}
a.HandleIncomingEnvelope(context.Background(), env)
if received {
t.Error("handler should not be called for blocked peer")
}
}
func TestSend_RejectsNonWhitelistedDestination(t *testing.T) {
a, _ := New(Options{
Name: "Agent",
ServerURL: "http://localhost:8080",
})
a.agentID = "test-agent"
env := envelope.New("test-agent", "unknown-dest", protocol.ProtocolA2A, []byte("hello"))
err := a.Send(context.Background(), env)
if err == nil {
t.Error("expected error sending to non-whitelisted destination")
}
}
func TestNewSimple(t *testing.T) {
a, err := NewSimple("simple-agent", "http://localhost:8080")
if err != nil {
t.Fatalf("NewSimple: %v", err)
}
if a.PublicKey() == "" {
t.Error("expected non-empty public key")
}
if a.opts.Name != "simple-agent" {
t.Errorf("name = %q, want %q", a.opts.Name, "simple-agent")
}
if a.opts.ServerURL != "http://localhost:8080" {
t.Errorf("serverURL = %q, want %q", a.opts.ServerURL, "http://localhost:8080")
}
}
func TestNewSimple_WithCapabilities(t *testing.T) {
a, err := NewSimple("cap-agent", "http://localhost:8080", "process-invoice", "query-status")
if err != nil {
t.Fatalf("NewSimple: %v", err)
}
caps := a.Capabilities()
if len(caps) != 2 {
t.Fatalf("expected 2 capabilities, got %d", len(caps))
}
if caps[0] != "process-invoice" || caps[1] != "query-status" {
t.Errorf("capabilities = %v, want [process-invoice query-status]", caps)
}
}
func TestImportContacts(t *testing.T) {
a, _ := NewSimple("agent", "http://localhost:8080")
a.ImportContacts([]string{"agent-billing", "agent-audit", "agent-notify"})
entries := a.ListContacts()
if len(entries) != 3 {
t.Fatalf("expected 3 contacts, got %d", len(entries))
}
for _, e := range entries {
if e.Level != security.TrustVerified {
t.Errorf("contact %s: level = %d, want TrustVerified", e.PublicKey, e.Level)
}
}
}
func TestImportContacts_Dedup(t *testing.T) {
a, _ := NewSimple("agent", "http://localhost:8080")
a.ImportContacts([]string{"agent-a", "agent-b", "agent-a"})
entries := a.ListContacts()
if len(entries) != 2 {
t.Errorf("expected 2 unique contacts, got %d", len(entries))
}
// Verify all are TrustVerified.
for _, e := range entries {
if e.Level != security.TrustVerified {
t.Errorf("contact %s: level = %d, want TrustVerified", e.PublicKey, e.Level)
}
}
}
func TestSend_FallsBackToMailbox(t *testing.T) {
a, _ := New(Options{
Name: "Agent",
ServerURL: "http://localhost:8080",
InboxRelays: []string{"wss://inbox.test"},
})
a.agentID = "test-agent"
// Verify mailbox options are stored.
if len(a.opts.InboxRelays) != 1 {
t.Fatalf("expected 1 inbox relay, got %d", len(a.opts.InboxRelays))
}
if a.opts.InboxRelays[0] != "wss://inbox.test" {
t.Errorf("inbox relay = %q, want %q", a.opts.InboxRelays[0], "wss://inbox.test")
}
}
func TestSend_NoMailboxFallsBackToCache(t *testing.T) {
dir := t.TempDir()
a, _ := New(Options{
Name: "Agent",
ServerURL: "http://localhost:8080",
MessageCachePath: dir + "/cache.json",
})
a.agentID = "test-agent"
// Verify no mailbox is configured.
if a.mailbox != nil {
t.Fatal("mailbox should be nil when InboxRelays is not set")
}
// Verify message cache is initialized.
if a.msgCache == nil {
t.Fatal("message cache should be initialized")
}
}
func TestAgent_PeerInboxCache(t *testing.T) {
a, _ := New(Options{
Name: "Agent",
ServerURL: "http://localhost:8080",
})
// Verify peerInboxCache is initialized.
if a.peerInboxCache == nil {
t.Fatal("peerInboxCache should be initialized")
}
// Add an entry.
a.mu.Lock()
a.peerInboxCache["agent-b"] = &peerInboxInfo{
InboxRelays: []string{"wss://relay.test"},
NostrPubKey: "abc123",
}
a.mu.Unlock()
// Verify retrieval.
a.mu.Lock()
info, ok := a.peerInboxCache["agent-b"]
a.mu.Unlock()
if !ok {
t.Fatal("expected to find cached peer inbox info")
}
if info.NostrPubKey != "abc123" {
t.Errorf("nostr pubkey = %q, want %q", info.NostrPubKey, "abc123")
}
}
func TestContactManagement(t *testing.T) {
a, _ := New(Options{
Name: "Agent",
ServerURL: "http://localhost:8080",
})
// Initially no contacts.
if len(a.ListContacts()) != 0 {
t.Errorf("expected 0 contacts, got %d", len(a.ListContacts()))
}
// Add contact.
a.AddContact("peer-1")
entries := a.ListContacts()
if len(entries) != 1 {
t.Fatalf("expected 1 contact, got %d", len(entries))
}
if entries[0].Level != security.TrustVerified {
t.Errorf("expected TrustVerified, got %d", entries[0].Level)
}
// Block agent.
a.BlockAgent("peer-2")
if a.trustStore.Check("peer-2") != security.TrustBlocked {
t.Error("peer-2 should be blocked")
}
// Remove contact.
a.RemoveContact("peer-1")
if a.trustStore.IsAllowed("peer-1") {
t.Error("peer-1 should be removed from whitelist")
}
}