-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathintegration_backup_test.go
More file actions
295 lines (241 loc) · 8.06 KB
/
Copy pathintegration_backup_test.go
File metadata and controls
295 lines (241 loc) · 8.06 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
package nara
import (
"bytes"
"crypto/sha256"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/eljojo/nara/identity"
)
// TestIntegration_EventImport_ValidSoul tests importing events with matching soul
func TestIntegration_EventImport_ValidSoul(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("skipping integration test")
}
// Create test nara with known soul
soulStr := testSoul("test-nara")
ln := testNara(t, "test-nara", WithSoul(soulStr))
// Start HTTP server
server := httptest.NewServer(ln.Network.createHTTPMux(false))
defer server.Close()
// Create some test events
events := []SyncEvent{
NewPingSyncEvent("alice", "bob", 10.5),
NewPingSyncEvent("alice", "carol", 20.3),
}
// Create import request signed with the same soul
soul, err := identity.ParseSoul(soulStr)
if err != nil {
t.Fatalf("Failed to parse soul: %v", err)
}
keypair := identity.DeriveKeypair(soul)
request := EventImportRequest{
Events: events,
Timestamp: time.Now().Unix(),
}
// Sign: sha256(timestamp:event_ids) - soul not sent in request
hasher := sha256.New()
hasher.Write([]byte(fmt.Sprintf("%d:", request.Timestamp)))
for _, e := range request.Events {
hasher.Write([]byte(e.ID))
}
data := hasher.Sum(nil)
request.Signature = keypair.SignBase64(data)
// POST to /api/events/import
jsonBody, _ := json.Marshal(request)
resp, err := http.Post(server.URL+"/api/events/import", "application/json", bytes.NewReader(jsonBody))
if err != nil {
t.Fatalf("Request failed: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("Expected status 200, got %d", resp.StatusCode)
}
// Parse response
var importResp EventImportResponse
if err := json.NewDecoder(resp.Body).Decode(&importResp); err != nil {
t.Fatalf("Failed to decode response: %v", err)
}
if !importResp.Success {
t.Fatalf("Import failed: %s", importResp.Error)
}
if importResp.Imported != 2 {
t.Errorf("Expected 2 imported events, got %d", importResp.Imported)
}
// Verify events are in ledger
ledgerEvents := ln.SyncLedger.Events
if len(ledgerEvents) != 2 {
t.Errorf("Expected 2 events in ledger, got %d", len(ledgerEvents))
}
t.Logf("✅ Successfully imported %d events", importResp.Imported)
}
// TestIntegration_EventImport_InvalidSoul tests that wrong soul is rejected
func TestIntegration_EventImport_InvalidSoul(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("skipping integration test")
}
// Create test nara with one soul
soulStr := testSoul("test-nara")
ln := testNara(t, "test-nara", WithSoul(soulStr))
// Start HTTP server
server := httptest.NewServer(ln.Network.createHTTPMux(false))
defer server.Close()
// Create import request signed with a DIFFERENT soul
differentSoulStr := testSoul("different-nara")
differentSoul, err := identity.ParseSoul(differentSoulStr)
if err != nil {
t.Fatalf("Failed to parse different soul: %v", err)
}
differentKeypair := identity.DeriveKeypair(differentSoul)
events := []SyncEvent{
NewPingSyncEvent("alice", "bob", 10.5),
}
request := EventImportRequest{
Events: events,
Timestamp: time.Now().Unix(),
}
// Sign with the different soul's keypair (will fail since server expects its own soul's signature)
hasher := sha256.New()
hasher.Write([]byte(fmt.Sprintf("%d:", request.Timestamp)))
for _, e := range request.Events {
hasher.Write([]byte(e.ID))
}
data := hasher.Sum(nil)
request.Signature = differentKeypair.SignBase64(data)
// POST to /api/events/import
jsonBody, _ := json.Marshal(request)
resp, err := http.Post(server.URL+"/api/events/import", "application/json", bytes.NewReader(jsonBody))
if err != nil {
t.Fatalf("Request failed: %v", err)
}
defer resp.Body.Close()
// Should be rejected with 403 Forbidden
if resp.StatusCode != http.StatusForbidden {
t.Errorf("Expected status 403, got %d", resp.StatusCode)
}
// Verify no events were imported
if len(ln.SyncLedger.Events) != 0 {
t.Errorf("Expected 0 events in ledger, got %d", len(ln.SyncLedger.Events))
}
t.Log("✅ Wrong soul correctly rejected")
}
// TestIntegration_EventImport_ExpiredTimestamp tests replay protection
func TestIntegration_EventImport_ExpiredTimestamp(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("skipping integration test")
}
soulStr := testSoul("test-nara")
ln := testNara(t, "test-nara", WithSoul(soulStr))
server := httptest.NewServer(ln.Network.createHTTPMux(false))
defer server.Close()
soul, _ := identity.ParseSoul(soulStr)
keypair := identity.DeriveKeypair(soul)
events := []SyncEvent{
NewPingSyncEvent("alice", "bob", 10.5),
}
// Create request with timestamp from 10 minutes ago (> 5 min window)
request := EventImportRequest{
Events: events,
Timestamp: time.Now().Unix() - 600, // 10 minutes ago
}
// Sign with old timestamp
hasher := sha256.New()
hasher.Write([]byte(fmt.Sprintf("%d:", request.Timestamp)))
for _, e := range request.Events {
hasher.Write([]byte(e.ID))
}
data := hasher.Sum(nil)
request.Signature = keypair.SignBase64(data)
// POST to /api/events/import
jsonBody, _ := json.Marshal(request)
resp, err := http.Post(server.URL+"/api/events/import", "application/json", bytes.NewReader(jsonBody))
if err != nil {
t.Fatalf("Request failed: %v", err)
}
defer resp.Body.Close()
// Should be rejected with 403 Forbidden
if resp.StatusCode != http.StatusForbidden {
t.Errorf("Expected status 403, got %d", resp.StatusCode)
}
t.Log("✅ Expired timestamp correctly rejected")
}
// TestIntegration_EventImport_Deduplication tests that duplicate events are handled
func TestIntegration_EventImport_Deduplication(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("skipping integration test")
}
soulStr := testSoul("test-nara")
ln := testNara(t, "test-nara", WithSoul(soulStr))
server := httptest.NewServer(ln.Network.createHTTPMux(false))
defer server.Close()
soul, _ := identity.ParseSoul(soulStr)
keypair := identity.DeriveKeypair(soul)
// Create events
events := []SyncEvent{
NewPingSyncEvent("alice", "bob", 10.5),
NewPingSyncEvent("alice", "carol", 20.3),
}
// First import
request := EventImportRequest{
Events: events,
Timestamp: time.Now().Unix(),
}
hasher := sha256.New()
hasher.Write([]byte(fmt.Sprintf("%d:", request.Timestamp)))
for _, e := range request.Events {
hasher.Write([]byte(e.ID))
}
data := hasher.Sum(nil)
request.Signature = keypair.SignBase64(data)
jsonBody, _ := json.Marshal(request)
resp, _ := http.Post(server.URL+"/api/events/import", "application/json", bytes.NewReader(jsonBody))
resp.Body.Close()
// Verify first import
if len(ln.SyncLedger.Events) != 2 {
t.Fatalf("Expected 2 events after first import, got %d", len(ln.SyncLedger.Events))
}
// Second import with same events (duplicates)
request2 := EventImportRequest{
Events: events,
Timestamp: time.Now().Unix(),
}
hasher2 := sha256.New()
hasher2.Write([]byte(fmt.Sprintf("%d:", request2.Timestamp)))
for _, e := range request2.Events {
hasher2.Write([]byte(e.ID))
}
data2 := hasher2.Sum(nil)
request2.Signature = keypair.SignBase64(data2)
jsonBody2, _ := json.Marshal(request2)
resp2, err := http.Post(server.URL+"/api/events/import", "application/json", bytes.NewReader(jsonBody2))
if err != nil {
t.Fatalf("Failed to post second import: %v", err)
}
defer resp2.Body.Close()
var importResp EventImportResponse
if err := json.NewDecoder(resp2.Body).Decode(&importResp); err != nil {
t.Fatalf("Failed to decode second import response: %v", err)
}
if !importResp.Success {
t.Fatalf("Second import failed: %s", importResp.Error)
}
// Should report 0 imported, 2 duplicates
if importResp.Imported != 0 {
t.Errorf("Expected 0 new imports, got %d", importResp.Imported)
}
if importResp.Duplicates != 2 {
t.Errorf("Expected 2 duplicates, got %d", importResp.Duplicates)
}
// Ledger should still have only 2 events
if len(ln.SyncLedger.Events) != 2 {
t.Errorf("Expected 2 events in ledger after deduplication, got %d", len(ln.SyncLedger.Events))
}
t.Logf("✅ Deduplication works: %d imported, %d duplicates", importResp.Imported, importResp.Duplicates)
}