ironoxide 4.4.1

A pure-Rust SDK for accessing IronCore's privacy platform
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
mod common;

use crate::common::{create_id_all_classes, create_second_user, init_sdk_get_user, initialize_sdk};
use galvanic_assert::{matchers::collection::contains_in_any_order, *};
use ironoxide::document::file::{DocumentFileAdvancedOps, DocumentFileOps};
use ironoxide::{Result, prelude::*};
use std::convert::TryInto;
use std::io::Write;
use tempfile::{NamedTempFile, TempPath};

// Helper to create a temp file with given content.
// Returns a TempPath (not NamedTempFile) because on Windows, NamedTempFile holds
// the file handle open, preventing other code from opening it for writing.
fn create_temp_file_with_content(content: &[u8]) -> TempPath {
    let mut file = NamedTempFile::new().expect("Failed to create temp file");
    file.write_all(content).expect("Failed to write test data");
    file.flush().expect("Failed to flush test data");
    // Convert to TempPath which closes the handle but keeps path for cleanup
    file.into_temp_path()
}

// Helper to create an empty temp file path for output.
// Returns TempPath (handle closed) to avoid Windows file locking issues.
fn create_output_temp_file() -> TempPath {
    NamedTempFile::new()
        .expect("Failed to create temp file")
        .into_temp_path()
}

#[tokio::test]
async fn file_encrypt_decrypt_roundtrip() -> Result<()> {
    let sdk = initialize_sdk().await?;

    let plaintext = b"Hello, World! This is a test of file encryption.";
    let source_file = create_temp_file_with_content(plaintext);
    let encrypted_file = create_output_temp_file();
    let decrypted_file = create_output_temp_file();

    let source_path = source_file.to_str().unwrap();
    let encrypted_path = encrypted_file.to_str().unwrap();
    let decrypted_path = decrypted_file.to_str().unwrap();

    let encrypt_result = sdk
        .document_file_encrypt(source_path, encrypted_path, &Default::default())
        .await?;

    assert_eq!(encrypt_result.grants().len(), 1);
    assert_eq!(encrypt_result.access_errs().len(), 0);

    let decrypt_result = sdk
        .document_file_decrypt(encrypted_path, decrypted_path)
        .await?;

    assert_eq!(decrypt_result.id(), encrypt_result.id());

    let decrypted_content = std::fs::read(decrypted_path).expect("Failed to read decrypted file");
    assert_eq!(decrypted_content, plaintext);

    Ok(())
}

#[tokio::test]
async fn file_encrypt_decrypt_unmanaged_roundtrip() -> Result<()> {
    let sdk = initialize_sdk().await?;

    let plaintext = b"Unmanaged file encryption test data";
    let source_file = create_temp_file_with_content(plaintext);
    let encrypted_file = create_output_temp_file();
    let decrypted_file = create_output_temp_file();

    let source_path = source_file.to_str().unwrap();
    let encrypted_path = encrypted_file.to_str().unwrap();
    let decrypted_path = decrypted_file.to_str().unwrap();

    let encrypt_result = sdk
        .document_file_encrypt_unmanaged(source_path, encrypted_path, &Default::default())
        .await?;

    assert_eq!(encrypt_result.grants().len(), 1);
    assert_eq!(encrypt_result.access_errs().len(), 0);
    assert!(!encrypt_result.encrypted_deks().is_empty());

    let decrypt_result = sdk
        .document_file_decrypt_unmanaged(
            encrypted_path,
            decrypted_path,
            encrypt_result.encrypted_deks(),
        )
        .await?;

    assert_eq!(decrypt_result.id(), encrypt_result.id());

    let decrypted_content = std::fs::read(decrypted_path).expect("Failed to read decrypted file");
    assert_eq!(decrypted_content, plaintext);

    Ok(())
}

#[tokio::test]
async fn file_roundtrip_empty_data() -> Result<()> {
    let sdk = initialize_sdk().await?;

    let plaintext: &[u8] = &[];
    let source_file = create_temp_file_with_content(plaintext);
    let encrypted_file = create_output_temp_file();
    let decrypted_file = create_output_temp_file();

    let source_path = source_file.to_str().unwrap();
    let encrypted_path = encrypted_file.to_str().unwrap();
    let decrypted_path = decrypted_file.to_str().unwrap();

    let encrypt_result = sdk
        .document_file_encrypt(source_path, encrypted_path, &Default::default())
        .await?;

    let decrypt_result = sdk
        .document_file_decrypt(encrypted_path, decrypted_path)
        .await?;

    assert_eq!(decrypt_result.id(), encrypt_result.id());

    let decrypted_content = std::fs::read(decrypted_path).expect("Failed to read decrypted file");
    assert_eq!(decrypted_content, plaintext);

    Ok(())
}

#[tokio::test]
async fn file_roundtrip_large_data() -> Result<()> {
    let sdk = initialize_sdk().await?;

    // Create 1MB of random-ish data
    let plaintext: Vec<u8> = (0..1024 * 1024).map(|i| (i % 256) as u8).collect();
    let source_file = create_temp_file_with_content(&plaintext);
    let encrypted_file = create_output_temp_file();
    let decrypted_file = create_output_temp_file();

    let source_path = source_file.to_str().unwrap();
    let encrypted_path = encrypted_file.to_str().unwrap();
    let decrypted_path = decrypted_file.to_str().unwrap();

    let encrypt_result = sdk
        .document_file_encrypt(source_path, encrypted_path, &Default::default())
        .await?;

    assert_eq!(encrypt_result.grants().len(), 1);

    let decrypt_result = sdk
        .document_file_decrypt(encrypted_path, decrypted_path)
        .await?;

    assert_eq!(decrypt_result.id(), encrypt_result.id());

    let decrypted_content = std::fs::read(decrypted_path).expect("Failed to read decrypted file");
    assert_eq!(decrypted_content, plaintext);

    Ok(())
}

#[tokio::test]
async fn file_roundtrip_large_data_unmanaged() -> Result<()> {
    let sdk = initialize_sdk().await?;

    // Create 2MB of random-ish data
    let plaintext: Vec<u8> = (0..2 * 1024 * 1024).map(|i| (i % 256) as u8).collect();
    let source_file = create_temp_file_with_content(&plaintext);
    let encrypted_file = create_output_temp_file();
    let decrypted_file = create_output_temp_file();

    let source_path = source_file.to_str().unwrap();
    let encrypted_path = encrypted_file.to_str().unwrap();
    let decrypted_path = decrypted_file.to_str().unwrap();

    let encrypt_result = sdk
        .document_file_encrypt_unmanaged(source_path, encrypted_path, &Default::default())
        .await?;

    assert_eq!(encrypt_result.grants().len(), 1);

    let decrypt_result = sdk
        .document_file_decrypt_unmanaged(
            encrypted_path,
            decrypted_path,
            encrypt_result.encrypted_deks(),
        )
        .await?;

    assert_eq!(decrypt_result.id(), encrypt_result.id());

    let decrypted_content = std::fs::read(decrypted_path).expect("Failed to read decrypted file");
    assert_eq!(decrypted_content, plaintext);

    Ok(())
}

#[tokio::test]
async fn file_encrypt_with_explicit_grants() -> Result<()> {
    let sdk = initialize_sdk().await?;
    let second_user = create_second_user().await;

    let plaintext = b"File with explicit grants";
    let source_file = create_temp_file_with_content(plaintext);
    let encrypted_file = create_output_temp_file();

    let source_path = source_file.to_str().unwrap();
    let encrypted_path = encrypted_file.to_str().unwrap();

    let bad_user: UserId = "bad_user".try_into()?;
    let bad_group: GroupId = "bad_group".try_into()?;

    let opts = DocumentEncryptOpts::with_explicit_grants(
        None,
        Some("file with grants".try_into()?),
        true,
        vec![
            UserOrGroup::User {
                id: second_user.account_id().clone(),
            },
            UserOrGroup::User { id: bad_user },
            UserOrGroup::Group { id: bad_group },
        ],
    );

    let encrypt_result = sdk
        .document_file_encrypt(source_path, encrypted_path, &opts)
        .await?;

    // Should have 2 successful grants (self + second_user)
    assert_eq!(encrypt_result.grants().len(), 2);
    assert_that!(
        &encrypt_result
            .grants()
            .iter()
            .cloned()
            .collect::<Vec<UserOrGroup>>(),
        contains_in_any_order(vec![
            UserOrGroup::User {
                id: sdk.device().account_id().clone()
            },
            UserOrGroup::User {
                id: second_user.account_id().clone()
            },
        ])
    );
    // Should have 2 errors (bad_user + bad_group)
    assert_eq!(encrypt_result.access_errs().len(), 2);

    Ok(())
}

#[tokio::test]
async fn file_encrypt_with_explicit_grants_unmanaged() -> Result<()> {
    let sdk = initialize_sdk().await?;
    let second_user = create_second_user().await;

    let plaintext = b"Unmanaged file with explicit grants";
    let source_file = create_temp_file_with_content(plaintext);
    let encrypted_file = create_output_temp_file();

    let source_path = source_file.to_str().unwrap();
    let encrypted_path = encrypted_file.to_str().unwrap();

    let bad_user: UserId = "bad_user".try_into()?;
    let bad_group: GroupId = "bad_group".try_into()?;

    let opts = DocumentEncryptOpts::with_explicit_grants(
        None,
        None,
        true,
        vec![
            UserOrGroup::User {
                id: second_user.account_id().clone(),
            },
            UserOrGroup::User { id: bad_user },
            UserOrGroup::Group { id: bad_group },
        ],
    );

    let encrypt_result = sdk
        .document_file_encrypt_unmanaged(source_path, encrypted_path, &opts)
        .await?;

    // Should have 2 successful grants (self + second_user)
    assert_eq!(encrypt_result.grants().len(), 2);
    // Should have 2 errors (bad_user + bad_group)
    assert_eq!(encrypt_result.access_errs().len(), 2);

    Ok(())
}

#[tokio::test]
async fn file_encrypt_decrypt_with_document_id_and_name() -> Result<()> {
    let sdk = initialize_sdk().await?;

    let plaintext = b"File with explicit ID and name";
    let source_file = create_temp_file_with_content(plaintext);
    let encrypted_file = create_output_temp_file();
    let decrypted_file = create_output_temp_file();

    let source_path = source_file.to_str().unwrap();
    let encrypted_path = encrypted_file.to_str().unwrap();
    let decrypted_path = decrypted_file.to_str().unwrap();

    let doc_id: DocumentId = create_id_all_classes("file_doc_").try_into()?;
    let doc_name: DocumentName = "Test File Document".try_into()?;

    let opts = DocumentEncryptOpts::with_explicit_grants(
        Some(doc_id.clone()),
        Some(doc_name.clone()),
        true,
        vec![],
    );

    let encrypt_result = sdk
        .document_file_encrypt(source_path, encrypted_path, &opts)
        .await?;

    assert_eq!(encrypt_result.id(), &doc_id);
    assert_eq!(encrypt_result.name(), Some(&doc_name));

    let decrypt_result = sdk
        .document_file_decrypt(encrypted_path, decrypted_path)
        .await?;

    assert_eq!(decrypt_result.id(), &doc_id);
    assert_eq!(decrypt_result.name(), Some(&doc_name));

    let decrypted_content = std::fs::read(decrypted_path).expect("Failed to read decrypted file");
    assert_eq!(decrypted_content, plaintext);

    Ok(())
}

#[tokio::test]
async fn file_encrypt_without_self_grant() -> Result<()> {
    let sdk = initialize_sdk().await?;
    let second_user = create_second_user().await;

    let plaintext = b"File without self grant";
    let source_file = create_temp_file_with_content(plaintext);
    let encrypted_file = create_output_temp_file();
    let decrypted_file = create_output_temp_file();

    let source_path = source_file.to_str().unwrap();
    let encrypted_path = encrypted_file.to_str().unwrap();
    let decrypted_path = decrypted_file.to_str().unwrap();

    let opts = DocumentEncryptOpts::with_explicit_grants(
        None,
        None,
        false, // grant_to_author = false
        vec![UserOrGroup::User {
            id: second_user.account_id().clone(),
        }],
    );

    let encrypt_result = sdk
        .document_file_encrypt(source_path, encrypted_path, &opts)
        .await?;

    // Only second_user should have access
    assert_eq!(encrypt_result.grants().len(), 1);
    assert_eq!(
        encrypt_result.grants()[0],
        UserOrGroup::User {
            id: second_user.account_id().clone()
        }
    );

    // SDK user should NOT be able to decrypt
    let decrypt_result = sdk
        .document_file_decrypt(encrypted_path, decrypted_path)
        .await;

    assert!(decrypt_result.is_err());

    Ok(())
}

#[tokio::test]
async fn file_encrypt_source_not_found() -> Result<()> {
    let sdk = initialize_sdk().await?;

    let encrypted_file = create_output_temp_file();
    let encrypted_path = encrypted_file.to_str().unwrap();

    let result = sdk
        .document_file_encrypt(
            "/nonexistent/path/to/file.txt",
            encrypted_path,
            &Default::default(),
        )
        .await;

    assert!(result.is_err());
    assert_that!(&result.unwrap_err(), is_variant!(IronOxideErr::FileIoError));

    Ok(())
}

#[tokio::test]
async fn file_decrypt_source_not_found() -> Result<()> {
    let sdk = initialize_sdk().await?;

    let decrypted_file = create_output_temp_file();
    let decrypted_path = decrypted_file.to_str().unwrap();

    let result = sdk
        .document_file_decrypt("/nonexistent/path/to/encrypted.iron", decrypted_path)
        .await;

    assert!(result.is_err());
    assert_that!(&result.unwrap_err(), is_variant!(IronOxideErr::FileIoError));

    Ok(())
}

#[tokio::test]
async fn file_decrypt_invalid_encrypted_data() -> Result<()> {
    let sdk = initialize_sdk().await?;

    // Create a file with garbage data (not a valid encrypted document)
    let garbage_data = b"This is not encrypted data";
    let source_file = create_temp_file_with_content(garbage_data);
    let decrypted_file = create_output_temp_file();

    let source_path = source_file.to_str().unwrap();
    let decrypted_path = decrypted_file.to_str().unwrap();

    let result = sdk.document_file_decrypt(source_path, decrypted_path).await;

    assert!(result.is_err());
    // Should fail to parse the document header
    assert_that!(
        &result.unwrap_err(),
        is_variant!(IronOxideErr::DocumentHeaderParseFailure)
    );

    Ok(())
}

#[tokio::test]
async fn file_encrypt_invalid_destination_path() -> Result<()> {
    let sdk = initialize_sdk().await?;

    let plaintext = b"Test data for invalid destination";
    let source_file = create_temp_file_with_content(plaintext);
    let source_path = source_file.to_str().unwrap();

    // Try to write to a non-existent directory
    let result = sdk
        .document_file_encrypt(
            source_path,
            "/nonexistent/directory/output.iron",
            &Default::default(),
        )
        .await;

    assert!(result.is_err());
    assert_that!(&result.unwrap_err(), is_variant!(IronOxideErr::FileIoError));

    Ok(())
}

#[tokio::test]
async fn file_decrypt_invalid_destination_path() -> Result<()> {
    let sdk = initialize_sdk().await?;

    // First create a valid encrypted file
    let plaintext = b"Test data";
    let source_file = create_temp_file_with_content(plaintext);
    let encrypted_file = create_output_temp_file();

    let source_path = source_file.to_str().unwrap();
    let encrypted_path = encrypted_file.to_str().unwrap();

    sdk.document_file_encrypt(source_path, encrypted_path, &Default::default())
        .await?;

    // Try to decrypt to a non-existent directory
    let result = sdk
        .document_file_decrypt(encrypted_path, "/nonexistent/directory/output.txt")
        .await;

    assert!(result.is_err());
    assert_that!(&result.unwrap_err(), is_variant!(IronOxideErr::FileIoError));

    Ok(())
}

// Interoperability tests: file operations should produce output compatible with memory operations

#[tokio::test]
async fn interop_file_encrypt_memory_decrypt() -> Result<()> {
    let sdk = initialize_sdk().await?;

    let plaintext = b"Test interoperability: file encrypt, memory decrypt";
    let source_file = create_temp_file_with_content(plaintext);
    let encrypted_file = create_output_temp_file();

    let source_path = source_file.to_str().unwrap();
    let encrypted_path = encrypted_file.to_str().unwrap();

    // Encrypt with file API
    let _encrypt_result = sdk
        .document_file_encrypt(source_path, encrypted_path, &Default::default())
        .await?;

    // Read encrypted file into memory
    let encrypted_bytes = std::fs::read(encrypted_path).expect("Failed to read encrypted file");

    // Decrypt with memory API
    let decrypt_result = sdk.document_decrypt(&encrypted_bytes).await?;

    assert_eq!(decrypt_result.decrypted_data(), plaintext);

    Ok(())
}

#[tokio::test]
async fn interop_memory_encrypt_file_decrypt() -> Result<()> {
    let sdk = initialize_sdk().await?;

    let plaintext = b"Test interoperability: memory encrypt, file decrypt";

    // Encrypt with memory API
    let encrypt_result = sdk
        .document_encrypt(plaintext.to_vec(), &Default::default())
        .await?;

    // Write encrypted data to file
    let encrypted_file = create_output_temp_file();
    let encrypted_path = encrypted_file.to_str().unwrap();
    std::fs::write(encrypted_path, encrypt_result.encrypted_data())
        .expect("Failed to write encrypted file");

    let decrypted_file = create_output_temp_file();
    let decrypted_path = decrypted_file.to_str().unwrap();

    // Decrypt with file API
    let decrypt_result = sdk
        .document_file_decrypt(encrypted_path, decrypted_path)
        .await?;

    assert_eq!(decrypt_result.id(), encrypt_result.id());

    let decrypted_content = std::fs::read(decrypted_path).expect("Failed to read decrypted file");
    assert_eq!(decrypted_content, plaintext);

    Ok(())
}

#[tokio::test]
async fn interop_file_encrypt_unmanaged_memory_decrypt_unmanaged() -> Result<()> {
    let sdk = initialize_sdk().await?;

    let plaintext = b"Unmanaged interop test: file encrypt, memory decrypt";
    let source_file = create_temp_file_with_content(plaintext);
    let encrypted_file = create_output_temp_file();

    let source_path = source_file.to_str().unwrap();
    let encrypted_path = encrypted_file.to_str().unwrap();

    // Encrypt with file API (unmanaged)
    let encrypt_result = sdk
        .document_file_encrypt_unmanaged(source_path, encrypted_path, &Default::default())
        .await?;

    // Read encrypted file into memory
    let encrypted_bytes = std::fs::read(encrypted_path).expect("Failed to read encrypted file");

    // Decrypt with memory API (unmanaged)
    let decrypt_result = sdk
        .document_decrypt_unmanaged(&encrypted_bytes, encrypt_result.encrypted_deks())
        .await?;

    assert_eq!(decrypt_result.decrypted_data(), plaintext);

    Ok(())
}

#[tokio::test]
async fn interop_memory_encrypt_unmanaged_file_decrypt_unmanaged() -> Result<()> {
    let sdk = initialize_sdk().await?;

    let plaintext = b"Unmanaged interop test: memory encrypt, file decrypt";

    // Encrypt with memory API (unmanaged)
    let encrypt_result = sdk
        .document_encrypt_unmanaged(plaintext.to_vec(), &Default::default())
        .await?;

    // Write encrypted data to file
    let encrypted_file = create_output_temp_file();
    let encrypted_path = encrypted_file.to_str().unwrap();
    std::fs::write(encrypted_path, encrypt_result.encrypted_data())
        .expect("Failed to write encrypted file");

    let decrypted_file = create_output_temp_file();
    let decrypted_path = decrypted_file.to_str().unwrap();

    // Decrypt with file API (unmanaged)
    let decrypt_result = sdk
        .document_file_decrypt_unmanaged(
            encrypted_path,
            decrypted_path,
            encrypt_result.encrypted_deks(),
        )
        .await?;

    assert_eq!(decrypt_result.id(), encrypt_result.id());

    let decrypted_content = std::fs::read(decrypted_path).expect("Failed to read decrypted file");
    assert_eq!(decrypted_content, plaintext);

    Ok(())
}

// Cross-user decryption tests

#[tokio::test]
async fn file_encrypt_decrypt_by_different_user() -> Result<()> {
    let (_user1, sdk1) = init_sdk_get_user().await;
    let (user2, sdk2) = init_sdk_get_user().await;

    let plaintext = b"File shared between users";
    let source_file = create_temp_file_with_content(plaintext);
    let encrypted_file = create_output_temp_file();
    let decrypted_file = create_output_temp_file();

    let source_path = source_file.to_str().unwrap();
    let encrypted_path = encrypted_file.to_str().unwrap();
    let decrypted_path = decrypted_file.to_str().unwrap();

    // User1 encrypts to User2
    let opts = DocumentEncryptOpts::with_explicit_grants(
        None,
        None,
        false, // Don't grant to self
        vec![UserOrGroup::User { id: user2.clone() }],
    );

    let encrypt_result = sdk1
        .document_file_encrypt(source_path, encrypted_path, &opts)
        .await?;

    assert_eq!(encrypt_result.grants().len(), 1);
    assert_eq!(
        encrypt_result.grants()[0],
        UserOrGroup::User { id: user2.clone() }
    );

    // User2 decrypts
    let decrypt_result = sdk2
        .document_file_decrypt(encrypted_path, decrypted_path)
        .await?;

    assert_eq!(decrypt_result.id(), encrypt_result.id());

    let decrypted_content = std::fs::read(decrypted_path).expect("Failed to read decrypted file");
    assert_eq!(decrypted_content, plaintext);

    // User1 should NOT be able to decrypt (they didn't grant to self)
    let decrypted_file2 = create_output_temp_file();
    let decrypted_path2 = decrypted_file2.to_str().unwrap();

    let result = sdk1
        .document_file_decrypt(encrypted_path, decrypted_path2)
        .await;
    assert!(result.is_err());

    Ok(())
}

#[tokio::test]
async fn file_encrypt_to_group() -> Result<()> {
    let (_, sdk) = init_sdk_get_user().await;

    // Create a group
    let group = sdk.group_create(&Default::default()).await?;

    let plaintext = b"File encrypted to group";
    let source_file = create_temp_file_with_content(plaintext);
    let encrypted_file = create_output_temp_file();
    let decrypted_file = create_output_temp_file();

    let source_path = source_file.to_str().unwrap();
    let encrypted_path = encrypted_file.to_str().unwrap();
    let decrypted_path = decrypted_file.to_str().unwrap();

    let opts = DocumentEncryptOpts::with_explicit_grants(
        None,
        None,
        false, // Don't grant to self directly
        vec![UserOrGroup::Group {
            id: group.id().clone(),
        }],
    );

    let encrypt_result = sdk
        .document_file_encrypt(source_path, encrypted_path, &opts)
        .await?;

    assert_eq!(encrypt_result.grants().len(), 1);
    assert_eq!(
        encrypt_result.grants()[0],
        UserOrGroup::Group {
            id: group.id().clone()
        }
    );

    // Should be able to decrypt via group membership
    let _decrypt_result = sdk
        .document_file_decrypt(encrypted_path, decrypted_path)
        .await?;

    let decrypted_content = std::fs::read(decrypted_path).expect("Failed to read decrypted file");
    assert_eq!(decrypted_content, plaintext);

    Ok(())
}