guardian-db 0.19.0

High-performance, local-first decentralized database built on Rust and Iroh
Documentation
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
546
547
548
549
550
551
552
553
554
555
556
557
558
//! Testes robustos para IrohAccessController
//!
//! Este módulo testa:
//! - Serialização/deserialização CBOR
//! - Grant/revoke de permissões
//! - Validação de acesso (can_append)
//! - Persistência (save/load)
//! - Concorrência
//! - Edge cases e tratamento de erros

use crate::access_control::{
    acl_iroh::IrohAccessController,
    manifest::{CreateAccessControllerOptions, ManifestParams},
};
use crate::guardian::error::Result;
use crate::log::{
    access_control::{CanAppendAdditionalContext, LogEntry},
    identity::{Identity, Signatures},
    identity_provider::GuardianDBIdentityProvider,
};
use crate::p2p::network::{client::IrohClient, config::ClientConfig};
use std::sync::Arc;
use tokio;

// ============= TEST HELPERS =============

/// Gera um caminho único para cada teste
fn generate_test_path() -> String {
    use std::time::{SystemTime, UNIX_EPOCH};
    let timestamp = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_nanos();
    let thread_id = std::thread::current().id();
    format!("./tmp/test_iroh_{}_{:?}", timestamp, thread_id)
}

/// Mock para LogEntry usado nos testes
struct MockLogEntry {
    identity: Identity,
    payload: Vec<u8>,
}

impl MockLogEntry {
    fn new(identity_id: &str) -> Self {
        let signatures = Signatures::new("", "");
        Self {
            identity: Identity::new(identity_id, "", signatures),
            payload: vec![],
        }
    }
}

impl LogEntry for MockLogEntry {
    fn get_identity(&self) -> &Identity {
        &self.identity
    }

    fn get_payload(&self) -> &[u8] {
        &self.payload
    }
}

/// Mock para CanAppendAdditionalContext
struct MockAdditionalContext;

impl CanAppendAdditionalContext for MockAdditionalContext {
    fn get_log_entries(&self) -> Vec<Box<dyn LogEntry>> {
        vec![]
    }
}

/// Cria um IrohClient de teste
async fn create_test_iroh_client() -> Result<Arc<IrohClient>> {
    let test_path = generate_test_path();
    let mut client_config = ClientConfig::offline();
    client_config.data_store_path = Some(test_path.into());
    let client = IrohClient::new(client_config).await?;
    Ok(Arc::new(client))
}

/// Cria um IrohAccessController de teste
async fn create_test_controller(
    client: Arc<IrohClient>,
    identity_id: &str,
) -> Result<IrohAccessController> {
    let params = CreateAccessControllerOptions::default();
    IrohAccessController::new(client, identity_id.to_string(), params)
}

// ============= BASIC FUNCTIONALITY TESTS =============

#[tokio::test]
async fn test_controller_creation() {
    let client = create_test_iroh_client().await.unwrap();
    let controller = create_test_controller(client, "test_user").await;

    assert!(controller.is_ok());
    let controller = controller.unwrap();
    assert_eq!(controller.get_type(), "iroh");
}

#[tokio::test]
async fn test_controller_default_permissions() {
    let client = create_test_iroh_client().await.unwrap();
    let controller = create_test_controller(client, "test_user").await.unwrap();

    let write_access = controller.get_authorized_by_role("write").await.unwrap();
    assert_eq!(write_access.len(), 1);
    assert_eq!(write_access[0], "test_user");
}

// ============= GRANT/REVOKE TESTS =============

#[tokio::test]
async fn test_grant_permission() {
    let client = create_test_iroh_client().await.unwrap();
    let controller = create_test_controller(client, "admin").await.unwrap();

    // Grant permission to new user
    controller.grant("write", "user1").await.unwrap();

    let write_access = controller.get_authorized_by_role("write").await.unwrap();
    assert_eq!(write_access.len(), 2);
    assert!(write_access.contains(&"admin".to_string()));
    assert!(write_access.contains(&"user1".to_string()));
}

#[tokio::test]
async fn test_grant_duplicate_permission() {
    let client = create_test_iroh_client().await.unwrap();
    let controller = create_test_controller(client, "admin").await.unwrap();

    // Grant same permission twice
    controller.grant("write", "user1").await.unwrap();
    controller.grant("write", "user1").await.unwrap();

    let write_access = controller.get_authorized_by_role("write").await.unwrap();
    // Should still have only 2 users (admin + user1, no duplicates)
    assert_eq!(write_access.len(), 2);
}

#[tokio::test]
async fn test_revoke_permission() {
    let client = create_test_iroh_client().await.unwrap();
    let controller = create_test_controller(client, "admin").await.unwrap();

    // Grant and then revoke
    controller.grant("write", "user1").await.unwrap();
    controller.grant("write", "user2").await.unwrap();

    let before_revoke = controller.get_authorized_by_role("write").await.unwrap();
    assert_eq!(before_revoke.len(), 3);

    controller.revoke("write", "user1").await.unwrap();

    let after_revoke = controller.get_authorized_by_role("write").await.unwrap();
    assert_eq!(after_revoke.len(), 2);
    assert!(after_revoke.contains(&"admin".to_string()));
    assert!(after_revoke.contains(&"user2".to_string()));
    assert!(!after_revoke.contains(&"user1".to_string()));
}

#[tokio::test]
async fn test_revoke_nonexistent_permission() {
    let client = create_test_iroh_client().await.unwrap();
    let controller = create_test_controller(client, "admin").await.unwrap();

    // Revoke permission that doesn't exist (should not error)
    let result = controller.revoke("write", "nonexistent").await;
    assert!(result.is_ok());

    let write_access = controller.get_authorized_by_role("write").await.unwrap();
    assert_eq!(write_access.len(), 1);
}

#[tokio::test]
async fn test_grant_invalid_capability() {
    let client = create_test_iroh_client().await.unwrap();
    let controller = create_test_controller(client, "admin").await.unwrap();

    // Try to grant invalid capability
    let result = controller.grant("invalid", "user1").await;
    assert!(result.is_err());
}

#[tokio::test]
async fn test_revoke_invalid_capability() {
    let client = create_test_iroh_client().await.unwrap();
    let controller = create_test_controller(client, "admin").await.unwrap();

    // Try to revoke invalid capability
    let result = controller.revoke("invalid", "user1").await;
    assert!(result.is_err());
}

// ============= CAN_APPEND TESTS =============

#[tokio::test]
async fn test_can_append_authorized_user() {
    let client = create_test_iroh_client().await.unwrap();
    let controller = create_test_controller(client, "admin").await.unwrap();

    controller.grant("write", "user1").await.unwrap();

    let entry = MockLogEntry::new("user1");
    let identity_provider = GuardianDBIdentityProvider::new();
    let additional_context = MockAdditionalContext;

    let result = controller
        .can_append(&entry, &identity_provider, &additional_context)
        .await;

    // Nota: pode falhar na verificação de identidade, mas não deve ser erro de permissão
    // Se falhar, a mensagem não deve ser sobre permissão
    if let Err(e) = result {
        let error_msg = format!("{}", e);
        assert!(!error_msg.contains("não tem permissão de escrita"));
    }
}

#[tokio::test]
async fn test_can_append_unauthorized_user() {
    let client = create_test_iroh_client().await.unwrap();
    let controller = create_test_controller(client, "admin").await.unwrap();

    let entry = MockLogEntry::new("unauthorized_user");
    let identity_provider = GuardianDBIdentityProvider::new();
    let additional_context = MockAdditionalContext;

    let result = controller
        .can_append(&entry, &identity_provider, &additional_context)
        .await;

    assert!(result.is_err());
    let error_msg = format!("{}", result.unwrap_err());
    assert!(
        error_msg.contains("does not have write permission")
            || error_msg.contains("not authorized for write")
    );
}

#[tokio::test]
async fn test_can_append_wildcard() {
    let client = create_test_iroh_client().await.unwrap();
    let controller = create_test_controller(client, "admin").await.unwrap();

    // Grant wildcard permission
    controller.grant("write", "*").await.unwrap();

    let entry = MockLogEntry::new("any_user");
    let identity_provider = GuardianDBIdentityProvider::new();
    let additional_context = MockAdditionalContext;

    let result = controller
        .can_append(&entry, &identity_provider, &additional_context)
        .await;

    // Com wildcard, o erro (se houver) não deve ser de permissão
    if let Err(e) = result {
        let error_msg = format!("{}", e);
        assert!(!error_msg.contains("não tem permissão de escrita"));
    }
}

// ============= SAVE/LOAD TESTS =============

#[tokio::test]
async fn test_save_controller() {
    let client = create_test_iroh_client().await.unwrap();
    let controller = create_test_controller(client, "admin").await.unwrap();

    controller.grant("write", "user1").await.unwrap();
    controller.grant("write", "user2").await.unwrap();

    let result = controller.save().await;
    assert!(result.is_ok());

    let options = result.unwrap();
    assert_eq!(options.get_type(), "iroh");
    // O hash não deve ser zero
    assert_ne!(
        hex::encode(options.address().as_bytes()),
        "0000000000000000000000000000000000000000000000000000000000000000"
    );
}

#[tokio::test]
async fn test_save_load_roundtrip() {
    let client = create_test_iroh_client().await.unwrap();
    let controller1 = create_test_controller(client.clone(), "admin")
        .await
        .unwrap();

    // Add permissions
    controller1.grant("write", "user1").await.unwrap();
    controller1.grant("write", "user2").await.unwrap();
    controller1.grant("write", "user3").await.unwrap();

    // Save
    let options = controller1.save().await.unwrap();
    let hash_str = hex::encode(options.address().as_bytes());

    // Create new controller and load
    let _controller2 = create_test_controller(client.clone(), "admin")
        .await
        .unwrap();

    // For now, we can't fully test load without a proper manifest
    // But we can verify the save produced valid data
    assert!(!hash_str.is_empty());
    assert_ne!(
        hash_str,
        "0000000000000000000000000000000000000000000000000000000000000000"
    );
}

// ============= ROLE-BASED ACCESS TESTS =============

#[tokio::test]
async fn test_get_authorized_by_role_write() {
    let client = create_test_iroh_client().await.unwrap();
    let controller = create_test_controller(client, "admin").await.unwrap();

    controller.grant("write", "user1").await.unwrap();

    let write_users = controller.get_authorized_by_role("write").await.unwrap();
    assert_eq!(write_users.len(), 2);
    assert!(write_users.contains(&"admin".to_string()));
    assert!(write_users.contains(&"user1".to_string()));
}

#[tokio::test]
async fn test_get_authorized_by_role_read() {
    let client = create_test_iroh_client().await.unwrap();
    let controller = create_test_controller(client, "admin").await.unwrap();

    controller.grant("write", "user1").await.unwrap();

    // Note: the direct method get_authorized_by_role returns empty for "read"
    // only "write" and "admin" return the write_access list
    let read_users = controller.get_authorized_by_role("read").await.unwrap();
    assert_eq!(read_users.len(), 0);
}

#[tokio::test]
async fn test_get_authorized_by_role_admin() {
    let client = create_test_iroh_client().await.unwrap();
    let controller = create_test_controller(client, "admin").await.unwrap();

    // Admin role returns same as write
    let admin_users = controller.get_authorized_by_role("admin").await.unwrap();
    assert_eq!(admin_users.len(), 1);
    assert!(admin_users.contains(&"admin".to_string()));
}

#[tokio::test]
async fn test_get_authorized_by_role_unknown() {
    let client = create_test_iroh_client().await.unwrap();
    let controller = create_test_controller(client, "admin").await.unwrap();

    // Unknown roles return empty list
    let unknown_users = controller
        .get_authorized_by_role("unknown_role")
        .await
        .unwrap();
    assert_eq!(unknown_users.len(), 0);
}

// ============= CONCURRENCY TESTS =============

#[tokio::test]
async fn test_concurrent_grants() {
    let client = create_test_iroh_client().await.unwrap();
    let controller = Arc::new(create_test_controller(client, "admin").await.unwrap());

    let mut handles = vec![];

    // Spawn 10 concurrent grant operations
    for i in 0..10 {
        let controller_clone = controller.clone();
        let handle = tokio::spawn(async move {
            controller_clone
                .grant("write", &format!("user{}", i))
                .await
                .unwrap();
        });
        handles.push(handle);
    }

    // Wait for all operations
    for handle in handles {
        handle.await.unwrap();
    }

    // Verify all users were added
    let write_access = controller.get_authorized_by_role("write").await.unwrap();
    // admin + 10 users
    assert_eq!(write_access.len(), 11);
}

#[tokio::test]
async fn test_concurrent_grant_and_revoke() {
    let client = create_test_iroh_client().await.unwrap();
    let controller = Arc::new(create_test_controller(client, "admin").await.unwrap());

    // Pre-add some users
    for i in 0..5 {
        controller
            .grant("write", &format!("user{}", i))
            .await
            .unwrap();
    }

    let mut handles = vec![];

    // Concurrent grants
    for i in 5..10 {
        let controller_clone = controller.clone();
        let handle = tokio::spawn(async move {
            controller_clone
                .grant("write", &format!("user{}", i))
                .await
                .unwrap();
        });
        handles.push(handle);
    }

    // Concurrent revokes
    for i in 0..3 {
        let controller_clone = controller.clone();
        let handle = tokio::spawn(async move {
            controller_clone
                .revoke("write", &format!("user{}", i))
                .await
                .unwrap();
        });
        handles.push(handle);
    }

    // Wait for all operations
    for handle in handles {
        handle.await.unwrap();
    }

    let write_access = controller.get_authorized_by_role("write").await.unwrap();
    // admin + (5 initial - 3 revoked + 5 new) = admin + 7
    assert_eq!(write_access.len(), 8);
}

// ============= CBOR SERIALIZATION TESTS =============

#[tokio::test]
async fn test_cbor_serialization_simple() {
    use serde::{Deserialize, Serialize};

    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    struct CborWriteAccess {
        write: Vec<String>,
    }

    let data = CborWriteAccess {
        write: vec!["user1".to_string(), "user2".to_string()],
    };

    let serialized = serde_cbor::to_vec(&data).unwrap();
    let deserialized: CborWriteAccess = serde_cbor::from_slice(&serialized).unwrap();

    assert_eq!(data, deserialized);
}

#[tokio::test]
async fn test_cbor_serialization_empty() {
    use serde::{Deserialize, Serialize};

    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    struct CborWriteAccess {
        write: Vec<String>,
    }

    let data = CborWriteAccess { write: vec![] };

    let serialized = serde_cbor::to_vec(&data).unwrap();
    let deserialized: CborWriteAccess = serde_cbor::from_slice(&serialized).unwrap();

    assert_eq!(data, deserialized);
}

#[tokio::test]
async fn test_cbor_serialization_special_chars() {
    use serde::{Deserialize, Serialize};

    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    struct CborWriteAccess {
        write: Vec<String>,
    }

    let data = CborWriteAccess {
        write: vec![
            "user@domain.com".to_string(),
            "user-with-dashes".to_string(),
            "user_with_underscores".to_string(),
            "*.wildcard".to_string(),
            "unicode_中文_🎉".to_string(),
        ],
    };

    let serialized = serde_cbor::to_vec(&data).unwrap();
    let deserialized: CborWriteAccess = serde_cbor::from_slice(&serialized).unwrap();

    assert_eq!(data, deserialized);
}

// ============= EDGE CASES =============

#[tokio::test]
async fn test_close_controller() {
    let client = create_test_iroh_client().await.unwrap();
    let controller = create_test_controller(client, "admin").await.unwrap();

    let result = controller.close().await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn test_controller_with_empty_identity() {
    let client = create_test_iroh_client().await.unwrap();
    let controller = create_test_controller(client, "").await.unwrap();

    let write_access = controller.get_authorized_by_role("write").await.unwrap();
    assert_eq!(write_access.len(), 1);
    assert_eq!(write_access[0], "");
}

#[tokio::test]
async fn test_grant_many_permissions() {
    let client = create_test_iroh_client().await.unwrap();
    let controller = create_test_controller(client, "admin").await.unwrap();

    // Grant 100 permissions
    for i in 0..100 {
        controller
            .grant("write", &format!("user{}", i))
            .await
            .unwrap();
    }

    let write_access = controller.get_authorized_by_role("write").await.unwrap();
    assert_eq!(write_access.len(), 101); // admin + 100 users
}

#[tokio::test]
async fn test_controller_address_returns_none() {
    let client = create_test_iroh_client().await.unwrap();
    let controller = create_test_controller(client, "admin").await.unwrap();

    assert!(controller.address().is_none());
}