-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathintegration_stash_http_new_test.go
More file actions
545 lines (463 loc) · 17.6 KB
/
Copy pathintegration_stash_http_new_test.go
File metadata and controls
545 lines (463 loc) · 17.6 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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
package nara
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"testing"
"time"
"github.com/eljojo/nara/types"
)
// TestStashDistribution_Integration tests the complete stash distribution workflow.
//
// This test creates a real mesh network with 4 naras and verifies:
// 1. Stash data is successfully distributed to 3 confidants via mesh HTTP
// 2. Confidants receive and store the encrypted stash
// 3. Owner can retrieve the stash from confidants
// 4. Recovery successfully retrieves stash from confidants
// 5. HTTP endpoints return correct data throughout the workflow
//
// This is a true end-to-end integration test that verifies the entire stash system works.
func TestStashDistribution_Integration(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("skipping integration test in short mode")
}
// Create mesh with 4 naras using dynamic port
mqttPort := getFreePort(t)
names := []string{"owner", "confidant-a", "confidant-b", "confidant-c"}
mesh := testCreateMeshNetwork(t, names, 50, 1000, mqttPort)
owner := mesh.Get(0)
confidantA := mesh.Get(1)
confidantB := mesh.Get(2)
confidantC := mesh.Get(3)
// Initialize runtime and stash services for all naras
for i := 0; i < 4; i++ {
nara := mesh.Naras[i]
if err := nara.Network.initRuntime(); err != nil {
t.Fatalf("Failed to initialize runtime for %s: %v", nara.Me.Name, err)
}
if err := nara.Network.startRuntime(); err != nil {
t.Fatalf("Failed to start runtime for %s: %v", nara.Me.Name, err)
}
if nara.Network.stashService == nil {
t.Fatalf("Stash service not initialized for %s", nara.Me.Name)
}
// Start confidants (indices 1, 2, 3) so they connect to MQTT and can process events
// Owner (index 0) is started later with specific HTTP config
// TODO: Make this cleaner in future refactor
if i > 0 {
go nara.Start(false, false, "", nil, TransportHybrid)
}
}
// Verify the system can discover peers (prerequisite for automatic selection)
peerCount := len(owner.Network.Neighbourhood)
if peerCount < 3 {
t.Fatalf("Need at least 3 known peers for confidant selection, found %d", peerCount)
}
t.Logf("✅ Owner knows %d peers (peer discovery working)", peerCount)
// Verify owner has 0 confidants initially
initialConfidants := owner.Network.stashService.Confidants()
if len(initialConfidants) != 0 {
t.Fatalf("Owner should have 0 confidants initially, found %d", len(initialConfidants))
}
// Owner creates stash data
stashData := map[string]interface{}{
"preferences": map[string]interface{}{
"theme": "dark",
"language": "en",
},
"bookmarks": []string{
"https://example.com",
"https://nara.test",
},
"notes": "test integration data",
}
stashJSON, err := json.Marshal(stashData)
if err != nil {
t.Fatalf("Failed to marshal stash data: %v", err)
}
// Owner distributes stash to confidants
t.Logf("Distributing stash from %s to 3 confidants...", owner.Me.Name)
err = owner.Network.stashService.SetStashData(stashJSON)
if err != nil {
t.Fatalf("Failed to set stash data: %v. Stash distribution should succeed with real mesh!", err)
}
// Wait for distribution to complete
time.Sleep(300 * time.Millisecond)
// Verify: Owner has the stash locally
ownerStashData, ownerStashTimestamp := owner.Network.stashService.GetStashData()
if len(ownerStashData) == 0 {
t.Fatal("Owner should have stash data locally")
}
if ownerStashTimestamp == 0 {
t.Error("Owner stash timestamp should be set")
}
t.Logf("✅ Owner has stash data locally (timestamp: %d)", ownerStashTimestamp)
// Verify: All 3 confidants received and stored the encrypted stash
confidantServices := []*LocalNara{confidantA, confidantB, confidantC}
successCount := 0
for i, confidant := range confidantServices {
hasStash := confidant.Network.stashService.HasStashFor(owner.Me.Status.ID)
if !hasStash {
t.Errorf("Confidant %d (%s) should have stored the owner's stash (ID: %s)",
i, confidant.Me.Name, owner.Me.Status.ID)
} else {
successCount++
t.Logf("✅ Confidant %s successfully stored owner's stash", confidant.Me.Name)
}
}
if successCount == 0 {
t.Fatal("No confidants received the stash - distribution completely failed!")
}
if successCount < 3 {
t.Logf("⚠️ Only %d/3 confidants received the stash", successCount)
} else {
t.Logf("✅ Stash successfully distributed to all 3 confidants")
}
// Test HTTP endpoints
httpPort := getFreePort(t)
httpAddr := fmt.Sprintf(":%d", httpPort)
go owner.Start(true, false, httpAddr, nil, TransportHybrid)
time.Sleep(200 * time.Millisecond)
baseURL := fmt.Sprintf("http://localhost:%d", httpPort)
// Test GET /api/stash/status
t.Run("http_status_endpoint", func(t *testing.T) {
resp, err := http.Get(baseURL + "/api/stash/status")
if err != nil {
t.Fatalf("GET /api/stash/status failed: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("Expected status 200, got %d", resp.StatusCode)
}
var result map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
t.Fatalf("Failed to decode response: %v", err)
}
// Verify has_stash is true
hasStash, ok := result["has_stash"].(bool)
if !ok || !hasStash {
t.Error("Expected has_stash: true")
}
// Verify my_stash contains data
myStash, ok := result["my_stash"].(map[string]interface{})
if !ok || myStash == nil {
t.Fatal("Expected my_stash to be present")
}
if _, ok := myStash["timestamp"]; !ok {
t.Error("my_stash missing timestamp")
}
stashDataField, ok := myStash["data"].(map[string]interface{})
if !ok || stashDataField == nil {
t.Fatal("my_stash missing data field")
}
// Verify actual data matches what we stored
if prefs, ok := stashDataField["preferences"].(map[string]interface{}); ok {
if theme, ok := prefs["theme"].(string); !ok || theme != "dark" {
t.Errorf("Expected theme 'dark', got %v", prefs["theme"])
}
} else {
t.Error("Stash data missing preferences")
}
t.Logf("✅ HTTP /api/stash/status returns correct data")
})
// Test GET /api/stash/confidants
t.Run("http_confidants_endpoint", func(t *testing.T) {
resp, err := http.Get(baseURL + "/api/stash/confidants")
if err != nil {
t.Fatalf("GET /api/stash/confidants failed: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("Expected status 200, got %d", resp.StatusCode)
}
var result map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
t.Fatalf("Failed to decode response: %v", err)
}
confidants, ok := result["confidants"].([]interface{})
if !ok {
t.Fatal("Expected confidants array")
}
// Should have 3 confidants
if len(confidants) != 3 {
t.Errorf("Expected 3 confidants, got %d", len(confidants))
}
t.Logf("✅ HTTP /api/stash/confidants returns 3 confidants")
})
// Test stash recovery workflow - simulates a complete reboot with automatic recovery
t.Run("recovery_workflow", func(t *testing.T) {
// Simulate complete restart: shutdown and recreate with same identity but empty state
rebooted := mesh.RestartNara(0) // owner is at index 0
// Initialize runtime and stash service for rebooted nara
if err := rebooted.Network.initRuntime(); err != nil {
t.Fatalf("Failed to initialize runtime for rebooted nara: %v", err)
}
if err := rebooted.Network.startRuntime(); err != nil {
t.Fatalf("Failed to start runtime for rebooted nara: %v", err)
}
t.Logf("Rebooted nara with same identity, checking for empty stash...")
// Verify: Rebooted nara has no local stash
localData, _ := rebooted.Network.stashService.GetStashData()
if len(localData) > 0 {
t.Error("Rebooted nara should start with no local stash")
}
// Verify no confidants configured (empty list after reboot)
stateBytes, _ := rebooted.Network.stashService.MarshalState()
var state struct {
Confidants []string `json:"confidants"`
}
_ = json.Unmarshal(stateBytes, &state)
if len(state.Confidants) != 0 {
t.Errorf("Rebooted nara should have empty confidant list, got %d", len(state.Confidants))
}
t.Logf("✅ Rebooted nara has empty stash and empty confidant list")
// Start HTTP server on rebooted nara
rebootedPort := getFreePort(t)
rebootedAddr := fmt.Sprintf(":%d", rebootedPort)
go rebooted.Start(true, false, rebootedAddr, nil, TransportHybrid)
time.Sleep(300 * time.Millisecond)
rebootedURL := fmt.Sprintf("http://localhost:%d", rebootedPort)
// Send hey-there announcement (in real system, this happens automatically on boot)
// Confidants detect the hey-there and automatically push stash back
t.Logf("Sending hey-there so confidants push stash back...")
rebooted.Network.heyThere()
t.Logf("Sent hey-there message from rebooted nara (heyThere)")
// With TransportHybrid, MQTT will handle the hey-there broadcast
// so we don't need manual propagation
// Poll status endpoint to wait for recovery to complete
var recoveredData []byte
var recoveredTimestamp int64
for i := 0; i < 15; i++ {
time.Sleep(500 * time.Millisecond)
// Check via HTTP status endpoint
statusResp, err := http.Get(rebootedURL + "/api/stash/status")
if err != nil {
continue
}
var status map[string]interface{}
if err := json.NewDecoder(statusResp.Body).Decode(&status); err != nil {
statusResp.Body.Close()
continue
}
statusResp.Body.Close()
hasStash, _ := status["has_stash"].(bool)
if hasStash {
// Got it! Extract data
if myStash, ok := status["my_stash"].(map[string]interface{}); ok {
if ts, ok := myStash["timestamp"].(float64); ok {
recoveredTimestamp = int64(ts)
}
if data, ok := myStash["data"]; ok {
dataJSON, _ := json.Marshal(data)
recoveredData = dataJSON
}
}
t.Logf("✅ Stash recovered after %d polls", i+1)
break
}
}
// Verify: Rebooted nara now has the stash data
if len(recoveredData) == 0 {
t.Fatal("Rebooted nara should have recovered stash data from confidants")
}
if recoveredTimestamp == 0 {
t.Error("Recovered stash should have timestamp")
}
// Verify the recovered data matches original
var recovered map[string]interface{}
if err := json.Unmarshal(recoveredData, &recovered); err != nil {
t.Fatalf("Failed to unmarshal recovered data: %v", err)
}
if prefs, ok := recovered["preferences"].(map[string]interface{}); ok {
if theme, ok := prefs["theme"].(string); !ok || theme != "dark" {
t.Errorf("Expected theme 'dark' in recovered data, got %v", prefs["theme"])
}
} else {
t.Error("Recovered data missing preferences")
}
t.Logf("✅ Rebooted nara successfully recovered stash via simulated hey-there workflow")
})
t.Logf("✅ End-to-end stash distribution test completed successfully!")
}
// TestStashUpdate_HTTPWorkflow tests updating stash via HTTP endpoint.
func TestStashUpdate_HTTPWorkflow(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("skipping integration test in short mode")
}
// Create mesh with 4 naras using dynamic port
mqttPort := getFreePort(t)
names := []string{"owner", "conf-1", "conf-2", "conf-3"}
mesh := testCreateMeshNetwork(t, names, 50, 1000, mqttPort)
owner := mesh.Get(0)
// Initialize runtime for all naras
for i := 0; i < 4; i++ {
nara := mesh.Naras[i]
if err := nara.Network.initRuntime(); err != nil {
t.Fatalf("Failed to init runtime: %v", err)
}
if err := nara.Network.startRuntime(); err != nil {
t.Fatalf("Failed to start runtime: %v", err)
}
}
// Manually configure confidants (automatic selection needs additional setup during test init)
owner.Network.stashService.SetConfidants([]types.NaraID{
mesh.Get(1).Me.Status.ID,
mesh.Get(2).Me.Status.ID,
mesh.Get(3).Me.Status.ID,
})
// Start HTTP server
httpPort := getFreePort(t)
ownerAddr := fmt.Sprintf(":%d", httpPort)
go owner.Start(true, false, ownerAddr, nil, TransportHybrid)
baseURL := fmt.Sprintf("http://localhost:%d", httpPort)
// Wait for HTTP server to be ready
if !waitForHTTPReady(t, baseURL+"/api/stash/status", 5*time.Second) {
t.Fatal("timeout waiting for HTTP server to start")
}
// Initial stash data
stashData := map[string]interface{}{
"counter": 1,
"name": "test",
}
jsonData, _ := json.Marshal(stashData)
// Update stash via HTTP
resp, err := http.Post(
baseURL+"/api/stash/update",
"application/json",
bytes.NewReader(jsonData),
)
if err != nil {
t.Fatalf("POST /api/stash/update failed: %v", err)
}
defer resp.Body.Close()
var result map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
t.Fatalf("Failed to decode response: %v", err)
}
success, ok := result["success"].(bool)
if !ok || !success {
t.Fatalf("Update should succeed with 3 confidants. Got: %v", result)
}
// Update again with different data
stashData["counter"] = 2
stashData["updated"] = true
jsonData, _ = json.Marshal(stashData)
resp2, err := http.Post(
baseURL+"/api/stash/update",
"application/json",
bytes.NewReader(jsonData),
)
if err != nil {
t.Fatalf("Second POST failed: %v", err)
}
defer resp2.Body.Close()
var result2 map[string]interface{}
if err := json.NewDecoder(resp2.Body).Decode(&result2); err != nil {
t.Fatalf("Failed to decode second response: %v", err)
}
success2, ok := result2["success"].(bool)
if !ok || !success2 {
t.Fatalf("Second update should succeed. Got: %v", result2)
}
// Verify final state via status endpoint
resp3, err := http.Get(baseURL + "/api/stash/status")
if err != nil {
t.Fatalf("GET status failed: %v", err)
}
defer resp3.Body.Close()
var status map[string]interface{}
if err := json.NewDecoder(resp3.Body).Decode(&status); err != nil {
t.Fatalf("Failed to decode status response: %v", err)
}
myStash := status["my_stash"].(map[string]interface{})
data := myStash["data"].(map[string]interface{})
if counter, ok := data["counter"].(float64); !ok || int(counter) != 2 {
t.Errorf("Expected counter=2, got %v", data["counter"])
}
if updated, ok := data["updated"].(bool); !ok || !updated {
t.Errorf("Expected updated=true, got %v", data["updated"])
}
t.Logf("✅ Successfully updated stash multiple times via HTTP")
}
// TestStashAutoSelect_UnregisteredPeer tests the race condition where a peer is
// marked as online (via observations) but not registered in meshClient.
// This reproduces the production issue: "peer not registered: <soul>"
//
// Root cause: OnlinePeers() returns peers based on neighborhood observations,
// but mesh IPs are registered asynchronously during boot. If stash auto-selection
// runs before mesh IPs are registered, it fails.
//
// BUG: Currently, OnlinePeers() returns peers that are "online" according to
// MQTT observations, but aren't mesh-reachable. The stash service then tries
// to contact these peers and fails with "peer not registered".
//
// EXPECTED: OnlinePeers() should only return peers that are BOTH online AND
// mesh-reachable, so the stash service never tries to contact unreachable peers.
func TestStashAutoSelect_UnregisteredPeer(t *testing.T) {
// This test verifies that OnlinePeers() filters out peers that aren't mesh-registered.
// It does NOT need actual mesh servers - we just check the filtering behavior.
// Create owner nara with full setup
owner := testNara(t, "owner", WithParams(50, 1000))
// Initialize runtime
if err := owner.Network.initRuntime(); err != nil {
t.Fatalf("Failed to init runtime: %v", err)
}
if err := owner.Network.startRuntime(); err != nil {
t.Fatalf("Failed to start runtime: %v", err)
}
// Create 4 peer identities
peerSouls := []string{
testSoul("peer1"),
testSoul("peer2"),
testSoul("peer3"),
testSoul("peer4"), // This one won't have mesh IP registered
}
// Import all 4 peers into neighborhood and mark all as online.
// Only 3 peers have MeshIP set - peer4 simulates the race condition where
// a peer is discovered via MQTT without mesh IP info.
for i, soul := range peerSouls {
name := types.NaraName(fmt.Sprintf("peer%d", i+1))
neighbor := NewNara(name)
neighbor.Status.ID = types.NaraID(soul)
neighbor.ID = types.NaraID(soul)
// Only first 3 peers have mesh IPs - peer4 simulates the race condition
if i < 3 {
neighbor.Status.MeshIP = fmt.Sprintf("100.64.0.%d", i+1)
}
owner.Network.importNara(neighbor)
// Mark as online via observation
owner.setObservation(name, NaraObservation{
Online: "ONLINE",
LastSeen: time.Now().Unix(),
LastRestart: time.Now().Add(-1 * time.Hour).Unix(),
})
}
t.Logf("peer4 (%s) is marked ONLINE but has no mesh IP (race condition)", peerSouls[3][:8])
// Verify: Peers with MeshIP set during import should be automatically registered
// This is the key fix - importNara() now registers mesh IPs automatically.
for i := 0; i < 3; i++ {
_, registered := owner.Network.meshClient.GetPeerBaseURL(types.NaraID(peerSouls[i]))
if !registered {
t.Fatalf("BUG: peer%d has MeshIP but was NOT auto-registered in meshClient. "+
"importNara() should register mesh IPs automatically.", i+1)
}
}
// peer4 should NOT be registered (no MeshIP was set)
_, peer4Registered := owner.Network.meshClient.GetPeerBaseURL(types.NaraID(peerSouls[3]))
if peer4Registered {
t.Fatal("peer4 should NOT be mesh-registered (no MeshIP was set)")
}
t.Logf("✅ Peers 1-3 auto-registered via importNara(). Peer 4 not registered (no MeshIP).")
// Get online peers - should return all 4 (all are online per observations)
peers := owner.Network.runtime.OnlinePeers()
t.Logf("OnlinePeers() returned %d peers", len(peers))
// OnlinePeers returns all peers that are online (per observations).
// The stash service will filter out unreachable peers when it tries to contact them.
if len(peers) != 4 {
t.Fatalf("Expected 4 online peers, got %d", len(peers))
}
t.Logf("✅ importNara() auto-registers mesh IPs, fixing the 'peer not registered' race condition")
}