openmls 0.9.1

A Rust implementation of the Messaging Layer Security (MLS) protocol, as defined in RFC 9420.
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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
//! This module contains tests regarding the use of [`MessageSecretsStore`]
//!
//! Tests for `MlsGroup::delete_past_epoch_secrets()`
//! - `max_epochs_policy_with_duration()`
//!     - test deletion using a duration, when the group keeps at most `n` past epoch secret
//!       entries by default
//! - `max_epochs_policy_with_timestamp()`
//!     - test deletion using a timestamp, when the group keeps at most `n` past epoch secret
//!       entries by default
//! - `keep_all_policy_with_duration()`
//!     - test deletion using a duration, when the group keeps all past epoch
//!       secrets by default
//! - `keep_all_policy_with_timestamp()`
//!     - test deletion using a timestamp, when the group keeps all past epoch
//!       secrets by default
//! - `delete_all()`:
//!     - test deletion of past epoch secrets using `PastEpochDeletion::delete_all()`
//!
//! Additional tests for the message secrets store:
//! - `test_secret_tree_store()`
//! - `test_empty_secret_tree_store()`

use openmls_traits::signatures::Signer;

use crate::{
    binary_tree::LeafNodeIndex,
    group::past_secrets::MessageSecretsStore,
    prelude::{tests_and_kats::utils::generate_credential_with_key, *},
    schedule::message_secrets::MessageSecrets,
};
use std::time::Duration;

#[cfg(not(target_arch = "wasm32"))]
use std::time::{Instant, SystemTime};
#[cfg(target_arch = "wasm32")]
use web_time::{Instant, SystemTime};

const INTERVAL: Duration = Duration::from_millis(100);

/// Helper function to set up an MlsGroup configured with the provided past epoch deletion policy
fn setup<Provider: OpenMlsProvider + Default>(
    ciphersuite: Ciphersuite,
    policy: PastEpochDeletionPolicy,
) -> (Provider, impl Signer, MlsGroup) {
    let alice_provider = Provider::default();
    let alice_credential_with_keys = generate_credential_with_key(
        b"Alice".to_vec(),
        ciphersuite.signature_algorithm(),
        &alice_provider,
    );
    let alice_signer = alice_credential_with_keys.signer;
    // Define the MlsGroup configuration
    let mls_group_create_config = MlsGroupCreateConfig::builder()
        .set_past_epoch_deletion_policy(policy)
        .ciphersuite(ciphersuite)
        .build();

    // === Alice creates a group ===
    let alice_group = MlsGroup::new(
        &alice_provider,
        &alice_signer,
        &mls_group_create_config,
        alice_credential_with_keys.credential_with_key.clone(),
    )
    .expect("An unexpected error occurred.");

    (alice_provider, alice_signer, alice_group)
}

/// Helper function for applying and merging commits, as part of testing past epoch deletion
/// This function also ensures that the number of past epoch trees available is what is expected
fn apply_and_merge_commits<Provider: OpenMlsProvider>(
    num_commits: usize,
    provider: &Provider,
    signer: &impl Signer,
    group: &mut MlsGroup,
    policy: PastEpochDeletionPolicy,
) {
    for _ in 0..num_commits {
        // create and stage sample commit
        // for simplicity, this is a commit that updates the GroupContextExtensions
        group
            .update_group_context_extensions(provider, Extensions::empty(), signer)
            .expect("error building commit");
        group
            .merge_pending_commit(provider)
            .expect("error merging commit");

        if let PastEpochDeletionPolicy::MaxEpochs(epochs) = policy {
            // assert nothing was deleted yet, and that the number of
            // past epoch secrets is the number of all past epoch secrets
            assert!(group.message_secrets_store().num_past_epoch_trees() <= epochs);
        } else {
            // assert nothing was deleted yet, and that the number of
            // past epoch secrets is the number of all past epoch secrets
            assert_eq!(
                group.message_secrets_store().num_past_epoch_trees(),
                group.epoch().as_u64() as usize
            );
        }
    }
}

/// This test checks the case where:
/// - max_past_epochs = 2, and time-based cleanup using a duration
#[openmls_test::openmls_test]
fn max_epochs_with_duration<Provider: crate::storage::OpenMlsProvider>(
    ciphersuite: Ciphersuite,
    provider: &Provider,
) {
    let policy = PastEpochDeletionPolicy::MaxEpochs(2);
    // set up a provider, signer and group
    let (alice_provider, alice_signer, mut alice_group) =
        setup::<Provider>(ciphersuite, policy.clone());

    let start = Instant::now();

    // apply and merge commits to advance the group epoch
    apply_and_merge_commits(4, &alice_provider, &alice_signer, &mut alice_group, policy);

    // sleep for INTERVAL + elapsed time, to ensure all secrets will be removed
    std::thread::sleep(INTERVAL + start.elapsed());
    // manually delete all before INTERVAL (early)
    alice_group
        .delete_past_epoch_secrets(
            &alice_provider,
            PastEpochDeletion::older_than_duration(INTERVAL),
        )
        .expect("error deleting past epoch secrets");

    // assert all past secrets deleted
    assert_eq!(
        alice_group.message_secrets_store().num_past_epoch_trees(),
        0
    );

    // load from storage to check persistence
    let alice_group_stored = MlsGroup::load(alice_provider.storage(), alice_group.group_id())
        .expect("error loading group")
        .expect("no group for id");
    assert_eq!(
        alice_group_stored
            .message_secrets_store()
            .num_past_epoch_trees(),
        0
    );
}

/// This test checks the case where:
/// - max_past_epochs = 2, and time-based cleanup using a timestamp
#[openmls_test::openmls_test]
fn max_epochs_policy_with_timestamp<Provider: crate::storage::OpenMlsProvider>() {
    let policy = PastEpochDeletionPolicy::MaxEpochs(2);
    // set up a provider, signer and group
    let (alice_provider, alice_signer, mut alice_group) =
        setup::<Provider>(ciphersuite, policy.clone());

    // === Test the `delete_past_epoch_secrets()` API with a timestamp ===

    let start = SystemTime::now();

    // apply and merge commits to advance the group epoch
    apply_and_merge_commits(4, &alice_provider, &alice_signer, &mut alice_group, policy);

    // manually delete all before start, leaving at most 3 entries
    // NOTE: all entries were inserted after `start`
    alice_group
        .delete_past_epoch_secrets(
            &alice_provider,
            PastEpochDeletion::before_timestamp(start).max_past_epochs(3),
        )
        .expect("error deleting past epoch secrets");
    // assert no past secrets deleted
    assert_eq!(
        alice_group.message_secrets_store().num_past_epoch_trees(),
        2
    );

    // manually delete all before start, leaving at most 1 entry
    // NOTE: all entries were inserted after `start`
    alice_group
        .delete_past_epoch_secrets(
            &alice_provider,
            PastEpochDeletion::before_timestamp(start).max_past_epochs(1),
        )
        .expect("error deleting past epoch secrets");
    // assert one past secret was kept
    assert_eq!(
        alice_group.message_secrets_store().num_past_epoch_trees(),
        1
    );

    // manually delete all before SystemTime::now()
    alice_group
        .delete_past_epoch_secrets(
            &alice_provider,
            PastEpochDeletion::before_timestamp(SystemTime::now()),
        )
        .expect("error deleting past epoch secrets");
    // assert all past secrets deleted
    assert_eq!(
        alice_group.message_secrets_store().num_past_epoch_trees(),
        0
    );

    // load from storage to check persistence
    let alice_group_stored = MlsGroup::load(alice_provider.storage(), alice_group.group_id())
        .expect("error loading group")
        .expect("no group for id");
    assert_eq!(
        alice_group_stored
            .message_secrets_store()
            .num_past_epoch_trees(),
        0
    );
}

/// This test checks the case where:
/// - epoch deletion policy: KeepAll, and time-based cleanup with duration
#[openmls_test::openmls_test]
fn keep_all_policy_with_duration<Provider: crate::storage::OpenMlsProvider>(
    ciphersuite: Ciphersuite,
    provider: &Provider,
) {
    let policy = PastEpochDeletionPolicy::KeepAll;
    // set up a provider, signer and group
    let (alice_provider, alice_signer, mut alice_group) =
        setup::<Provider>(ciphersuite, policy.clone());

    let start = Instant::now();

    // apply and merge commits to advance the group epoch
    apply_and_merge_commits(4, &alice_provider, &alice_signer, &mut alice_group, policy);

    // sleep for INTERVAL + elapsed time`
    std::thread::sleep(INTERVAL + start.elapsed());

    // manually delete all before
    alice_group
        .delete_past_epoch_secrets(
            &alice_provider,
            PastEpochDeletion::older_than_duration(INTERVAL),
        )
        .expect("error deleting past epoch secrets");
    // assert all past secrets deleted
    assert_eq!(
        alice_group.message_secrets_store().num_past_epoch_trees(),
        0
    );

    // load from storage to check persistence
    let alice_group_stored = MlsGroup::load(alice_provider.storage(), alice_group.group_id())
        .expect("error loading group")
        .expect("no group for id");
    assert_eq!(
        alice_group_stored
            .message_secrets_store()
            .num_past_epoch_trees(),
        0
    );
}

/// This test checks the case where:
/// - epoch deletion policy: KeepAll, and time-based cleanup with timestamp
#[openmls_test::openmls_test]
fn keep_all_policy_with_timestamp<Provider: crate::storage::OpenMlsProvider>(
    ciphersuite: Ciphersuite,
    provider: &Provider,
) {
    let policy = PastEpochDeletionPolicy::KeepAll;

    // set up a provider, signer and group
    let (alice_provider, alice_signer, mut alice_group) =
        setup::<Provider>(ciphersuite, policy.clone());

    // apply and merge commits to advance the group epoch
    apply_and_merge_commits(4, &alice_provider, &alice_signer, &mut alice_group, policy);

    // manually delete all before UNIX_EPOCH, leaving at most 5 entries
    alice_group
        .delete_past_epoch_secrets(
            &alice_provider,
            PastEpochDeletion::before_timestamp(SystemTime::UNIX_EPOCH).max_past_epochs(5),
        )
        .expect("error deleting past epoch secrets");
    // assert no past secrets deleted
    assert_eq!(
        alice_group.message_secrets_store().num_past_epoch_trees(),
        4
    );

    // manually delete all before UNIX_EPOCH, leaving at most 3 entries
    alice_group
        .delete_past_epoch_secrets(
            &alice_provider,
            PastEpochDeletion::before_timestamp(SystemTime::UNIX_EPOCH).max_past_epochs(3),
        )
        .expect("error deleting past epoch secrets");
    // assert no past secrets deleted
    assert_eq!(
        alice_group.message_secrets_store().num_past_epoch_trees(),
        3
    );

    // manually delete all before UNIX_EPOCH, leaving at most 1 entry
    alice_group
        .delete_past_epoch_secrets(
            &alice_provider,
            PastEpochDeletion::before_timestamp(SystemTime::UNIX_EPOCH).max_past_epochs(1),
        )
        .expect("error deleting past epoch secrets");
    // assert one past secret was kept
    assert_eq!(
        alice_group.message_secrets_store().num_past_epoch_trees(),
        1
    );

    // manually delete all before SystemTime::now()
    alice_group
        .delete_past_epoch_secrets(
            &alice_provider,
            PastEpochDeletion::before_timestamp(SystemTime::now()),
        )
        .expect("error deleting past epoch secrets");
    // assert all past secrets deleted
    assert_eq!(
        alice_group.message_secrets_store().num_past_epoch_trees(),
        0
    );

    // load from storage to check persistence
    let alice_group_stored = MlsGroup::load(alice_provider.storage(), alice_group.group_id())
        .expect("error loading group")
        .expect("no group for id");
    assert_eq!(
        alice_group_stored
            .message_secrets_store()
            .num_past_epoch_trees(),
        0
    );
}

/// This test checks the manual cleanup of all past epoch secrets.
#[openmls_test::openmls_test]
fn delete_all<Provider: crate::storage::OpenMlsProvider>() {
    // test several policies
    for policy in [
        PastEpochDeletionPolicy::KeepAll,
        PastEpochDeletionPolicy::MaxEpochs(2),
        PastEpochDeletionPolicy::MaxEpochs(0),
    ] {
        // set up a provider, signer and group
        let (alice_provider, alice_signer, mut alice_group) =
            setup::<Provider>(ciphersuite, policy.clone());

        // apply and merge commits to advance the group epoch
        apply_and_merge_commits(4, &alice_provider, &alice_signer, &mut alice_group, policy);

        // manually delete all before start, leaving at most 3 entries
        // NOTE: all entries were inserted after `start`
        alice_group
            .delete_past_epoch_secrets(&alice_provider, PastEpochDeletion::delete_all())
            .expect("error deleting past epoch secrets");
        // assert all past secrets deleted
        assert_eq!(
            alice_group.message_secrets_store().num_past_epoch_trees(),
            0
        );
        // load from storage to check persistence
        let alice_group_stored = MlsGroup::load(alice_provider.storage(), alice_group.group_id())
            .expect("error loading group")
            .expect("no group for id");
        assert_eq!(
            alice_group_stored
                .message_secrets_store()
                .num_past_epoch_trees(),
            0
        );
    }
}

/// Helper function to create a message secrets store with mixed
/// `Some` and `None` timestamp entries
fn setup_tree_store_with_timestamps<Provider: OpenMlsProvider>(
    ciphersuite: Ciphersuite,
    provider: &Provider,
    entries: &[Option<SystemTime>],
) -> MessageSecretsStore {
    // Create a store
    let mut message_secrets_store = MessageSecretsStore::new_with_secret(
        &PastEpochDeletionPolicy::KeepAll,
        MessageSecrets::random(ciphersuite, provider.rand(), LeafNodeIndex::new(0)),
    );

    // Populate with past epoch trees
    // use the index as the epoch
    for (epoch, timestamp) in entries.iter().enumerate() {
        let message_secrets = MessageSecrets::random(
            ciphersuite,
            provider.rand(),
            LeafNodeIndex::new(epoch as u32),
        );

        let message_secrets = match timestamp {
            Some(timestamp) => message_secrets.with_timestamp(*timestamp),
            None => message_secrets.without_timestamp(),
        };

        message_secrets_store.add_past_epoch_tree(epoch as u64, message_secrets, Vec::new());
    }

    message_secrets_store
}

/// Test persistence of an update to the past epoch deletion policy.
#[openmls_test::openmls_test]
fn test_update_policy_persistence<Provider: OpenMlsProvider>(ciphersuite: Ciphersuite) {
    // initial policy
    let initial_policy = PastEpochDeletionPolicy::MaxEpochs(3);
    // set up a provider, signer and group
    let (alice_provider, _alice_signer, mut alice_group) =
        setup::<Provider>(ciphersuite, initial_policy.clone());

    // load group from storage
    let alice_group_stored = MlsGroup::load(alice_provider.storage(), alice_group.group_id())
        .expect("error loading group")
        .expect("no group for id");
    // check policy
    assert_eq!(
        alice_group_stored.past_epoch_deletion_policy(),
        &initial_policy
    );

    for new_policy in [
        PastEpochDeletionPolicy::KeepAll,
        PastEpochDeletionPolicy::MaxEpochs(3),
        PastEpochDeletionPolicy::MaxEpochs(2),
    ] {
        alice_group
            .set_past_epoch_deletion_policy(&alice_provider, new_policy.clone())
            .unwrap();
        // load group from storage
        let alice_group_stored = MlsGroup::load(alice_provider.storage(), alice_group.group_id())
            .expect("error loading group")
            .expect("no group for id");
        // check policy
        assert_eq!(alice_group_stored.past_epoch_deletion_policy(), &new_policy);
    }
}

/// Test changing the past epoch deletion policy on a group.
#[openmls_test::openmls_test]
fn test_update_policy<Provider: OpenMlsProvider>(ciphersuite: Ciphersuite) {
    // initial policy
    let policy = PastEpochDeletionPolicy::MaxEpochs(3);
    // set up a provider, signer and group
    let (alice_provider, alice_signer, mut alice_group) =
        setup::<Provider>(ciphersuite, policy.clone());

    // apply and merge commits to advance the group epoch
    apply_and_merge_commits(4, &alice_provider, &alice_signer, &mut alice_group, policy);
    assert_eq!(
        alice_group.message_secrets_store().num_past_epoch_trees(),
        3
    );

    // update the policy
    let new_policy = PastEpochDeletionPolicy::MaxEpochs(5);
    alice_group
        .set_past_epoch_deletion_policy(&alice_provider, new_policy.clone())
        .expect("error updating policy");

    // apply and merge additional commits to advance the group epoch
    for _ in 0..10 {
        alice_group
            .update_group_context_extensions(&alice_provider, Extensions::empty(), &alice_signer)
            .expect("error building commit");
        alice_group
            .merge_pending_commit(&alice_provider)
            .expect("error merging commit");
    }
    // check that the policy was updated on the group
    assert_eq!(alice_group.past_epoch_deletion_policy(), &new_policy);
    assert_eq!(
        alice_group.message_secrets_store().num_past_epoch_trees(),
        5
    );

    // update the policy
    let new_policy = PastEpochDeletionPolicy::MaxEpochs(2);
    alice_group
        .set_past_epoch_deletion_policy(&alice_provider, new_policy.clone())
        .expect("error updating policy");
    // check that the policy was updated on the group
    assert_eq!(alice_group.past_epoch_deletion_policy(), &new_policy);
    // check that the store was resized correctly
    assert_eq!(
        alice_group.message_secrets_store().num_past_epoch_trees(),
        2
    );

    // update the policy
    let new_policy = PastEpochDeletionPolicy::KeepAll;
    alice_group
        .set_past_epoch_deletion_policy(&alice_provider, new_policy.clone())
        .expect("error updating policy");

    // apply and merge additional commits to advance the group epoch
    for _ in 0..8 {
        alice_group
            .update_group_context_extensions(&alice_provider, Extensions::empty(), &alice_signer)
            .expect("error building commit");
        alice_group
            .merge_pending_commit(&alice_provider)
            .expect("error merging commit");
    }
    // check that the policy was updated on the group
    assert_eq!(alice_group.past_epoch_deletion_policy(), &new_policy);
    assert_eq!(
        alice_group.message_secrets_store().num_past_epoch_trees(),
        10
    );

    // delete all past epoch secrets
    alice_group
        .delete_past_epoch_secrets(&alice_provider, PastEpochDeletion::delete_all())
        .expect("error deleting past epoch secrets");
    assert_eq!(
        alice_group.message_secrets_store().num_past_epoch_trees(),
        0
    );

    // apply and merge an additional commit to advance the group epoch
    alice_group
        .update_group_context_extensions(&alice_provider, Extensions::empty(), &alice_signer)
        .expect("error building commit");
    alice_group
        .merge_pending_commit(&alice_provider)
        .expect("error merging commit");
    // NOTE: now, the number of past epoch secrets is less than the maximum
    // configured by the policy that will be applied next.
    assert_eq!(
        alice_group.message_secrets_store().num_past_epoch_trees(),
        1
    );

    // update the policy
    let new_policy = PastEpochDeletionPolicy::MaxEpochs(2);
    alice_group
        .set_past_epoch_deletion_policy(&alice_provider, new_policy.clone())
        .expect("error updating policy");
    // check that the store was resized correctly
    assert_eq!(
        alice_group.message_secrets_store().num_past_epoch_trees(),
        1
    );

    // load from storage to check persistence
    let alice_group_stored = MlsGroup::load(alice_provider.storage(), alice_group.group_id())
        .expect("error loading group")
        .expect("no group for id");
    assert_eq!(
        alice_group_stored
            .message_secrets_store()
            .num_past_epoch_trees(),
        1
    );
    assert_eq!(alice_group_stored.past_epoch_deletion_policy(), &new_policy);
}

/// Test that changing the past epoch deletion policy via `set_configuration`
/// resizes and persists the message secrets store, just like
/// `set_past_epoch_deletion_policy` does.
#[openmls_test::openmls_test]
fn test_update_policy_via_set_configuration<Provider: OpenMlsProvider>(ciphersuite: Ciphersuite) {
    // initial policy
    let policy = PastEpochDeletionPolicy::MaxEpochs(3);
    // set up a provider, signer and group
    let (alice_provider, alice_signer, mut alice_group) =
        setup::<Provider>(ciphersuite, policy.clone());

    // apply and merge commits to advance the group epoch
    apply_and_merge_commits(4, &alice_provider, &alice_signer, &mut alice_group, policy);
    assert_eq!(
        alice_group.message_secrets_store().num_past_epoch_trees(),
        3
    );

    // raise the policy via `set_configuration`
    let new_policy = PastEpochDeletionPolicy::MaxEpochs(5);
    let mut new_config = alice_group.configuration().clone();
    new_config.past_epoch_deletion_policy = new_policy.clone();
    alice_group
        .set_configuration(alice_provider.storage(), &new_config)
        .expect("error updating configuration");

    // apply and merge additional commits to advance the group epoch
    for _ in 0..10 {
        alice_group
            .update_group_context_extensions(&alice_provider, Extensions::empty(), &alice_signer)
            .expect("error building commit");
        alice_group
            .merge_pending_commit(&alice_provider)
            .expect("error merging commit");
    }
    // check that the policy was updated on the group and the store was resized
    assert_eq!(alice_group.past_epoch_deletion_policy(), &new_policy);
    assert_eq!(
        alice_group.message_secrets_store().num_past_epoch_trees(),
        5
    );

    // lower the policy via `set_configuration`
    let new_policy = PastEpochDeletionPolicy::MaxEpochs(2);
    let mut new_config = alice_group.configuration().clone();
    new_config.past_epoch_deletion_policy = new_policy.clone();
    alice_group
        .set_configuration(alice_provider.storage(), &new_config)
        .expect("error updating configuration");
    // check that the policy was updated and the store was shrunk
    assert_eq!(alice_group.past_epoch_deletion_policy(), &new_policy);
    assert_eq!(
        alice_group.message_secrets_store().num_past_epoch_trees(),
        2
    );

    // load from storage to check the resized store was persisted
    let alice_group_stored = MlsGroup::load(alice_provider.storage(), alice_group.group_id())
        .expect("error loading group")
        .expect("no group for id");
    assert_eq!(
        alice_group_stored
            .message_secrets_store()
            .num_past_epoch_trees(),
        2
    );
    assert_eq!(alice_group_stored.past_epoch_deletion_policy(), &new_policy);
}

/// Test a secret tree store with all legacy timestamps, where a timestamp is available for the
/// current MessageSecrets
#[openmls_test::openmls_test]
fn test_secret_tree_store_migration_next_epoch_timestamp() {
    let provider = &Provider::default();

    // Set up a secret tree store with mixed Some and None timestamps
    // NOTE: For completeness, this sequence of epoch tree timestamps is tested.
    // In practice, Some and None timestamps should not occur in this pattern.
    let mut message_secrets_store = setup_tree_store_with_timestamps(
        ciphersuite,
        provider,
        &[
            None, //0
            None, //1
        ],
    );

    // test deletion of all message secrets before the timestamp
    message_secrets_store
        .delete_past_epoch_secrets(PastEpochDeletion::before_timestamp(SystemTime::UNIX_EPOCH));
    assert_eq!(message_secrets_store.num_past_epoch_trees(), 2);

    // test deletion of all message secrets before the timestamp
    message_secrets_store
        .delete_past_epoch_secrets(PastEpochDeletion::before_timestamp(SystemTime::now()));
    assert_eq!(message_secrets_store.num_past_epoch_trees(), 0);
}

/// Test a secret tree store with all legacy timestamps, where a timestamp is available for the
/// current MessageSecrets
#[openmls_test::openmls_test]
fn test_secret_tree_store_migration_next_epoch_duration() {
    let provider = &Provider::default();

    // Set up a secret tree store with mixed Some and None timestamps
    // NOTE: For completeness, this sequence of epoch tree timestamps is tested.
    // In practice, Some and None timestamps should not occur in this pattern.
    let mut message_secrets_store = setup_tree_store_with_timestamps(
        ciphersuite,
        provider,
        &[
            None, //0
            None, //1
        ],
    );

    // test deletion of all message secrets before a longer duration
    message_secrets_store.delete_past_epoch_secrets(PastEpochDeletion::older_than_duration(
        Duration::from_hours(2),
    ));
    assert_eq!(message_secrets_store.num_past_epoch_trees(), 2);

    // test deletion of all message secrets before the timestamp
    message_secrets_store.delete_past_epoch_secrets(PastEpochDeletion::older_than_duration(
        Duration::from_nanos(0),
    ));
    assert_eq!(message_secrets_store.num_past_epoch_trees(), 0);
}

/// Test a secret tree store with a mix of legacy and current timestamps,
/// deleting by a provided timestamp
#[openmls_test::openmls_test]
fn test_secret_tree_store_mixed_delete_by_timestamp() {
    let provider = &Provider::default();

    let timestamp_before = SystemTime::now();

    // Set up a secret tree store with mixed Some and None timestamps
    // NOTE: For completeness, this sequence of epoch tree timestamps is tested.
    // In practice, Some and None timestamps should not occur in this pattern.
    let mut message_secrets_store = setup_tree_store_with_timestamps(
        ciphersuite,
        provider,
        &[
            None,                         //0
            Some(SystemTime::UNIX_EPOCH), //1
            None,                         //2
            Some(SystemTime::UNIX_EPOCH), //3
            None,                         //4
            Some(SystemTime::now()),      //5
            None,                         //6
        ],
    );

    // test deletion of all message secrets before the timestamp
    message_secrets_store
        .delete_past_epoch_secrets(PastEpochDeletion::before_timestamp(timestamp_before));

    // ensure that the past epoch secrets are now empty
    // assert all past secrets deleted
    // NOTE: the `None` tree at the end will not be deleted
    assert_eq!(message_secrets_store.num_past_epoch_trees(), 3);
    assert!(message_secrets_store.secrets_for_epoch(4).is_some());
    assert!(message_secrets_store.secrets_for_epoch(5).is_some());
    assert!(message_secrets_store.secrets_for_epoch(6).is_some());
}

/// Test a secret tree store with a mix of legacy and current timestamps
/// deleting by a provided duration
#[openmls_test::openmls_test]
fn test_secret_tree_store_mixed_delete_by_duration() {
    let provider = &Provider::default();

    // Set up a secret tree store with mixed Some and None timestamps
    // NOTE: For completeness, this sequence of epoch tree timestamps is tested.
    // In practice, Some and None timestamps should not occur in this pattern.
    let mut message_secrets_store = setup_tree_store_with_timestamps(
        ciphersuite,
        provider,
        &[
            None,                         //0
            Some(SystemTime::UNIX_EPOCH), //1
            None,                         //2
            Some(SystemTime::UNIX_EPOCH), //3
            None,                         //4
            Some(SystemTime::now()),      //5
            None,                         //6
        ],
    );

    // test deletion of all message secrets before `now()`
    message_secrets_store.delete_past_epoch_secrets(PastEpochDeletion::older_than_duration(
        Duration::from_hours(3),
    ));

    // ensure that the past epoch secrets are now empty
    // assert all past secrets deleted
    // NOTE: the `None` tree at epoch 3 will not be deleted
    assert_eq!(message_secrets_store.num_past_epoch_trees(), 3);
    assert!(message_secrets_store.secrets_for_epoch(4).is_some());
    assert!(message_secrets_store.secrets_for_epoch(5).is_some());
    assert!(message_secrets_store.secrets_for_epoch(6).is_some());
}

/// Basic test for the message secrets store
#[openmls_test::openmls_test]
fn test_secret_tree_store() {
    let provider = &Provider::default();
    // Create a store that keeps up to 3 epochs
    let mut message_secrets_store = MessageSecretsStore::new_with_secret(
        &PastEpochDeletionPolicy::MaxEpochs(3),
        MessageSecrets::random(ciphersuite, provider.rand(), LeafNodeIndex::new(0)),
    );

    // Add message secrets to the store
    message_secrets_store.add_past_epoch_tree(
        0,
        MessageSecrets::random(ciphersuite, provider.rand(), LeafNodeIndex::new(0))
            .with_timestamp(SystemTime::now()),
        Vec::new(),
    );

    // Make sure we can access the message secrets we just stored
    assert!(message_secrets_store.secrets_for_epoch_mut(0).is_some());

    // Add 5 more message secrets, this should drop trees from earlier epochs
    for i in 1..6u64 {
        message_secrets_store.add_past_epoch_tree(
            i,
            MessageSecrets::random(ciphersuite, provider.rand(), LeafNodeIndex::new(0))
                .with_timestamp(SystemTime::now()),
            Vec::new(),
        );
    }

    // These epochs should be in the store
    assert!(message_secrets_store.secrets_for_epoch_mut(3).is_some());
    assert!(message_secrets_store.secrets_for_epoch_mut(4).is_some());
    assert!(message_secrets_store.secrets_for_epoch_mut(5).is_some());

    // These epochs should not be in the store
    assert!(message_secrets_store.secrets_for_epoch_mut(0).is_none());
    assert!(message_secrets_store.secrets_for_epoch_mut(1).is_none());
    assert!(message_secrets_store.secrets_for_epoch_mut(2).is_none());
    assert!(message_secrets_store.secrets_for_epoch_mut(6).is_none());
}

#[openmls_test::openmls_test]
fn test_empty_secret_tree_store() {
    let provider = &Provider::default();
    // Create a store that keeps no epochs
    let mut message_secrets_store = MessageSecretsStore::new_with_secret(
        &PastEpochDeletionPolicy::MaxEpochs(0),
        MessageSecrets::random(ciphersuite, provider.rand(), LeafNodeIndex::new(0)),
    );

    // Add message secrets to the store
    message_secrets_store.add_past_epoch_tree(
        0,
        MessageSecrets::random(ciphersuite, provider.rand(), LeafNodeIndex::new(0))
            .with_timestamp(SystemTime::now()),
        Vec::new(),
    );

    // Make sure we cannot access the message secrets we just stored
    assert!(message_secrets_store.secrets_for_epoch_mut(0).is_none());
}