garrison 0.7.2

Garrison - 面向 Rust 生态的一站式身份认证鉴权框架
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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
//! Copyright (c) 2026 Kirky.X. All rights reserved.
//! See LICENSE for full license text.

//! `strategy` 模块的 inline tests。
//!
//! 从 `mod.rs` 迁移而出(规则 25:mod.rs 接口隔离)。
//! 覆盖权限校验、角色层级、权限缓存、插件钩子、防火墙安全钩子等场景。
//!
//! 注意:引用 `GarrisonFirewallCheckHook` / `LoginContext` 的测试需 cfg 门控
//! (依赖 limiteron / firewall-* / oauth2-server feature)。

use super::mock::MockCacheDao;
use super::*;
use crate::error::GarrisonError;
#[cfg(any(
    feature = "sms-rate-limit",
    feature = "firewall-ratelimit",
    feature = "firewall-bruteforce",
    feature = "firewall-ddos",
    feature = "firewall",
    feature = "oauth2-server"
))]
use crate::strategy::hooks::{
    GarrisonFirewallCheckHook, GarrisonFirewallCheckHookDefault, LoginContext,
};
use std::collections::HashMap;

// ------------------------------------------------------------------------
// MockInterface:模拟业务方实现 GarrisonInterface 回调
// ------------------------------------------------------------------------

/// 测试用 GarrisonInterface mock,基于 HashMap 存储 login_id → 权限/角色列表。
struct MockInterface {
    permissions: HashMap<String, Vec<String>>,
    roles: HashMap<String, Vec<String>>,
}

impl MockInterface {
    fn new() -> Self {
        Self {
            permissions: HashMap::new(),
            roles: HashMap::new(),
        }
    }

    /// 设置指定 login_id 的权限列表。
    fn set_permissions(&mut self, login_id: &str, perms: &[&str]) {
        self.permissions.insert(
            login_id.to_string(),
            perms.iter().map(|s| s.to_string()).collect(),
        );
    }

    /// 设置指定 login_id 的角色列表。
    fn set_roles(&mut self, login_id: &str, roles: &[&str]) {
        self.roles.insert(
            login_id.to_string(),
            roles.iter().map(|s| s.to_string()).collect(),
        );
    }
}

#[async_trait]
impl GarrisonInterface for MockInterface {
    async fn get_permission_list(&self, login_id: &str) -> GarrisonResult<Vec<String>> {
        Ok(self.permissions.get(login_id).cloned().unwrap_or_default())
    }

    async fn get_role_list(&self, login_id: &str) -> GarrisonResult<Vec<String>> {
        Ok(self.roles.get(login_id).cloned().unwrap_or_default())
    }
}

/// 辅助函数:创建 GarrisonPermissionStrategyDefault 实例。
fn make_firewall(interface: MockInterface) -> GarrisonPermissionStrategyDefault {
    GarrisonPermissionStrategyDefault::new(Arc::new(interface))
}

// ------------------------------------------------------------------------
// 持有权限返回 true / 未持有返回 false
// ------------------------------------------------------------------------

/// 验证主体持有指定权限时 check_permission 返回 true。
#[tokio::test]
async fn check_permission_held_returns_true() {
    let mut iface = MockInterface::new();
    iface.set_permissions("1001", &["user:read", "user:write"]);
    let fw = make_firewall(iface);

    assert!(
        fw.check_permission("1001", "user:read").await.unwrap(),
        "持有权限应返回 true"
    );
}

/// 验证主体未持有指定权限时 check_permission 返回 false。
#[tokio::test]
async fn check_permission_not_held_returns_false() {
    let mut iface = MockInterface::new();
    iface.set_permissions("1001", &["user:read"]);
    let fw = make_firewall(iface);

    assert!(
        !fw.check_permission("1001", "user:delete").await.unwrap(),
        "未持有权限应返回 false"
    );
}

/// 空字符串抛 InvalidParam。
#[tokio::test]
async fn check_permission_empty_string_errors() {
    let iface = MockInterface::new();
    let fw = make_firewall(iface);

    let result = fw.check_permission("1001", "").await;
    assert!(
        matches!(result, Err(GarrisonError::InvalidParam(_))),
        "空权限字符串应抛 InvalidParam"
    );
}

/// 验证主体无任何权限记录时 check_permission 返回 false(不抛错)。
#[tokio::test]
async fn check_permission_no_record_returns_false() {
    let iface = MockInterface::new();
    let fw = make_firewall(iface);

    assert!(
        !fw.check_permission("9999", "user:read").await.unwrap(),
        "无权限记录的 login_id 应返回 false"
    );
}

// ------------------------------------------------------------------------
// 持有角色返回 true / 未持有返回 false
// ------------------------------------------------------------------------

/// 验证主体持有指定角色时 check_role 返回 true。
#[tokio::test]
async fn check_role_held_returns_true() {
    let mut iface = MockInterface::new();
    iface.set_roles("1001", &["admin", "user"]);
    let fw = make_firewall(iface);

    assert!(
        fw.check_role("1001", "admin").await.unwrap(),
        "持有角色应返回 true"
    );
}

/// 验证主体未持有指定角色时 check_role 返回 false。
#[tokio::test]
async fn check_role_not_held_returns_false() {
    let mut iface = MockInterface::new();
    iface.set_roles("1001", &["user"]);
    let fw = make_firewall(iface);

    assert!(
        !fw.check_role("1001", "admin").await.unwrap(),
        "未持有角色应返回 false"
    );
}

/// 验证空角色字符串返回 Err。
///
/// 与 `check_permission_empty_string_errors` 对称:
/// 空角色字符串应抛 `InvalidParam` 错误。
#[tokio::test]
async fn check_role_empty_string_errors() {
    let iface = MockInterface::new();
    let fw = make_firewall(iface);

    let result = fw.check_role("1001", "").await;
    assert!(
        matches!(result, Err(GarrisonError::InvalidParam(_))),
        "空角色字符串应抛 InvalidParam"
    );
}

// ------------------------------------------------------------------------
// 多角色任一匹配 / 全部匹配
// ------------------------------------------------------------------------

/// 验证 check_role_any:主体持有 roles 中任意一个即返回 true。
#[tokio::test]
async fn check_role_any_match_returns_true() {
    let mut iface = MockInterface::new();
    iface.set_roles("1001", &["admin"]);
    let fw = make_firewall(iface);

    assert!(
        fw.check_role_any("1001", &["admin", "superadmin"])
            .await
            .unwrap(),
        "持有任一角色应返回 true"
    );
}

/// 验证 check_role_any:主体不持有 roles 中任何一个则返回 false。
#[tokio::test]
async fn check_role_any_no_match_returns_false() {
    let mut iface = MockInterface::new();
    iface.set_roles("1001", &["user"]);
    let fw = make_firewall(iface);

    assert!(
        !fw.check_role_any("1001", &["admin", "superadmin"])
            .await
            .unwrap(),
        "不持有任一角色应返回 false"
    );
}

/// 验证 check_role_all:主体持有 roles 中所有角色才返回 true。
#[tokio::test]
async fn check_role_all_all_held_returns_true() {
    let mut iface = MockInterface::new();
    iface.set_roles("1001", &["admin", "user"]);
    let fw = make_firewall(iface);

    assert!(
        fw.check_role_all("1001", &["admin", "user"]).await.unwrap(),
        "持有所有角色应返回 true"
    );
}

/// 主体仅持有部分角色时返回 false。
#[tokio::test]
async fn check_role_all_partial_held_returns_false() {
    let mut iface = MockInterface::new();
    iface.set_roles("1001", &["admin"]);
    let fw = make_firewall(iface);

    assert!(
        !fw.check_role_all("1001", &["admin", "user"]).await.unwrap(),
        "仅持有部分角色应返回 false"
    );
}

/// 验证 check_role_all:空 roles 切片返回 true(空集平凡满足)。
#[tokio::test]
async fn check_role_all_empty_roles_returns_true() {
    let mut iface = MockInterface::new();
    iface.set_roles("1001", &["admin"]);
    let fw = make_firewall(iface);

    assert!(
        fw.check_role_all("1001", &[]).await.unwrap(),
        "空 roles 切片应平凡返回 true"
    );
}

// ------------------------------------------------------------------------
// get_permission_list / get_role_list 回调委托验证
// ------------------------------------------------------------------------

/// 验证 get_permission_list 委托 GarrisonInterface 回调。
#[tokio::test]
async fn get_permission_list_delegates_to_interface() {
    let mut iface = MockInterface::new();
    iface.set_permissions("1001", &["user:read", "user:write"]);
    let fw = make_firewall(iface);

    let perms = fw.get_permission_list("1001").await.unwrap();
    assert_eq!(perms, vec!["user:read", "user:write"]);
}

/// 验证 get_role_list 委托 GarrisonInterface 回调。
#[tokio::test]
async fn get_role_list_delegates_to_interface() {
    let mut iface = MockInterface::new();
    iface.set_roles("1001", &["admin", "user"]);
    let fw = make_firewall(iface);

    let roles = fw.get_role_list("1001").await.unwrap();
    assert_eq!(roles, vec!["admin", "user"]);
}

/// 验证未配置权限的 login_id 返回空列表(不抛错)。
#[tokio::test]
async fn get_permission_list_unknown_login_id_returns_empty() {
    let iface = MockInterface::new();
    let fw = make_firewall(iface);

    let perms = fw.get_permission_list("9999").await.unwrap();
    assert!(perms.is_empty(), "未配置权限的 login_id 应返回空列表");
}

// ------------------------------------------------------------------------
// PermissionChecker 集成测试
// ------------------------------------------------------------------------

/// 可配置的 MockPermissionChecker,返回预设的权限/角色校验结果。
struct MockPermissionChecker {
    perm_result: bool,
}

#[async_trait]
impl PermissionChecker for MockPermissionChecker {
    async fn has_permission(&self, _login_id: &str, _permission: &str) -> GarrisonResult<bool> {
        Ok(self.perm_result)
    }
    async fn has_role(&self, _login_id: &str, _role: &str) -> GarrisonResult<bool> {
        Ok(false)
    }
    async fn check_permission(&self, _login_id: &str, _permission: &str) -> GarrisonResult<()> {
        Ok(())
    }
    async fn check_role(&self, _login_id: &str, _role: &str) -> GarrisonResult<()> {
        Ok(())
    }
    async fn has_any_permission(&self, _login_id: &str, _perms: &[&str]) -> bool {
        false
    }
    async fn has_all_permissions(&self, _login_id: &str, _perms: &[&str]) -> bool {
        false
    }
}

/// 验证注入 PermissionChecker 后 check_permission 委托到它。
#[tokio::test]
async fn check_permission_delegates_to_permission_checker() {
    let iface = MockInterface::new();
    let pc = Arc::new(MockPermissionChecker { perm_result: true });
    let fw = GarrisonPermissionStrategyDefault::new(Arc::new(iface)).with_permission_checker(pc);

    // PermissionChecker 返回 true,即使 interface 中无权限记录
    assert!(
        fw.check_permission("1001", "user:read").await.unwrap(),
        "注入 PermissionChecker 后应委托校验,返回 true"
    );
}

/// 验证 PermissionChecker 返回 false 时 check_permission 返回 false。
#[tokio::test]
async fn check_permission_delegates_returns_false() {
    let iface = MockInterface::new();
    let pc = Arc::new(MockPermissionChecker { perm_result: false });
    let fw = GarrisonPermissionStrategyDefault::new(Arc::new(iface)).with_permission_checker(pc);

    assert!(
        !fw.check_permission("1001", "user:read").await.unwrap(),
        "PermissionChecker 返回 false 时应返回 false"
    );
}

/// 验证未注入 PermissionChecker 时回退到 默认行为(直接查 interface)。
#[tokio::test]
async fn check_permission_without_checker_falls_back_to_interface() {
    let mut iface = MockInterface::new();
    iface.set_permissions("1001", &["user:read"]);
    let fw = GarrisonPermissionStrategyDefault::new(Arc::new(iface));

    assert!(
        fw.check_permission("1001", "user:read").await.unwrap(),
        "未注入 PermissionChecker 时应回退到 interface 查询"
    );
}

// ------------------------------------------------------------------------
// 层级角色测试
// ------------------------------------------------------------------------

/// 辅助函数:创建带角色层级的 firewall。
fn make_firewall_with_hierarchy(
    interface: MockInterface,
    hierarchy: HashMap<String, Vec<String>>,
) -> GarrisonPermissionStrategyDefault {
    GarrisonPermissionStrategyDefault::new(Arc::new(interface)).with_role_hierarchy(hierarchy)
}

/// 验证层级角色:admin 隐含持有 user。
#[tokio::test]
async fn check_role_hierarchy_admin_implies_user() {
    let mut iface = MockInterface::new();
    iface.set_roles("1001", &["admin"]);
    let mut hierarchy = HashMap::new();
    hierarchy.insert("admin".to_string(), vec!["user".to_string()]);
    let fw = make_firewall_with_hierarchy(iface, hierarchy);

    assert!(
        fw.check_role("1001", "user").await.unwrap(),
        "admin 应隐含持有 user"
    );
    assert!(
        !fw.check_role("1001", "superadmin").await.unwrap(),
        "admin 不隐含 superadmin"
    );
}

/// 验证层级角色多层传递:superadmin → admin → user。
#[tokio::test]
async fn check_role_hierarchy_transitive() {
    let mut iface = MockInterface::new();
    iface.set_roles("1001", &["superadmin"]);
    let mut hierarchy = HashMap::new();
    hierarchy.insert("admin".to_string(), vec!["user".to_string()]);
    hierarchy.insert("superadmin".to_string(), vec!["admin".to_string()]);
    let fw = make_firewall_with_hierarchy(iface, hierarchy);

    assert!(
        fw.check_role("1001", "user").await.unwrap(),
        "superadmin 应多层传递隐含 user"
    );
    assert!(
        fw.check_role("1001", "admin").await.unwrap(),
        "superadmin 应隐含 admin"
    );
}

/// 验证未配置 role_hierarchy 时保持 默认行为。
#[tokio::test]
async fn check_role_without_hierarchy_keeps_legacy_behavior() {
    let mut iface = MockInterface::new();
    iface.set_roles("1001", &["admin"]);
    let fw = make_firewall(iface); // 无 hierarchy

    assert!(
        !fw.check_role("1001", "user").await.unwrap(),
        "未配置 hierarchy 时 admin 不隐含 user(0.1.0 行为)"
    );
}

/// 验证 check_role_any / check_role_all 在层级角色下的行为。
#[tokio::test]
async fn check_role_any_all_with_hierarchy() {
    let mut iface = MockInterface::new();
    iface.set_roles("1001", &["admin"]);
    let mut hierarchy = HashMap::new();
    hierarchy.insert("admin".to_string(), vec!["user".to_string()]);
    let fw = make_firewall_with_hierarchy(iface, hierarchy);

    // admin 隐含 user,所以 check_role_any(["user", "guest"]) 应返回 true
    assert!(
        fw.check_role_any("1001", &["user", "guest"]).await.unwrap(),
        "层级展开后应持有 user,check_role_any 应返回 true"
    );
    // admin 隐含 user,但不含 superadmin,check_role_all 应返回 false
    assert!(
        !fw.check_role_all("1001", &["user", "superadmin"])
            .await
            .unwrap(),
        "层级展开后不含 superadmin,check_role_all 应返回 false"
    );
}

// ------------------------------------------------------------------------
// 插件钩子测试
// ------------------------------------------------------------------------

/// 验证注入 PluginManager 后 check_permission 触发插件钩子。
#[tokio::test]
async fn check_permission_triggers_plugin_hook() {
    let mut iface = MockInterface::new();
    iface.set_permissions("1001", &["user:read"]);
    // GarrisonPluginManager::new() 收集所有 inventory 注册的插件
    let pm = Arc::new(GarrisonPluginManager::new());
    let fw = GarrisonPermissionStrategyDefault::new(Arc::new(iface)).with_plugin_manager(pm);

    // 插件钩子不应中断主流程,校验结果应正常返回
    assert!(
        fw.check_permission("1001", "user:read").await.unwrap(),
        "插件钩子不应影响校验结果"
    );
}

/// 验证插件失败不中断 check_permission 主流程。
///
/// 注意:当前实现遵循 task 21.3(Err → warn 不中断),不实现 spec 的 Override 机制。
#[tokio::test]
async fn check_permission_plugin_failure_does_not_interrupt() {
    let mut iface = MockInterface::new();
    iface.set_permissions("1001", &["user:read"]);
    // PluginManager 包含 ErrPlugin(on_permission_check 返回 Err),
    // 但主流程不应被中断
    let pm = Arc::new(GarrisonPluginManager::new());
    let fw = GarrisonPermissionStrategyDefault::new(Arc::new(iface)).with_plugin_manager(pm);

    assert!(
        fw.check_permission("1001", "user:read").await.unwrap(),
        "插件失败不应中断主流程,校验结果应正常返回 true"
    );
}

// ------------------------------------------------------------------------
// 权限缓存测试
// ------------------------------------------------------------------------

/// 验证 cache_permission 写入 DAO,后续 get_cached_permission 返回缓存值。
#[tokio::test]
async fn cache_permission_writes_and_reads_back() {
    let dao = Arc::new(MockCacheDao::new());
    let iface = MockInterface::new();
    let fw = GarrisonPermissionStrategyDefault::new(Arc::new(iface)).with_dao(dao.clone());

    fw.cache_permission("1001", "user:read", true, 300)
        .await
        .unwrap();

    let cached = fw.get_cached_permission("1001", "user:read").await.unwrap();
    assert_eq!(cached, Some(true), "缓存应命中并返回 true");

    // 验证 key 格式
    let key = "garrison:perm:cache:1001:user:read";
    assert_eq!(
        dao.get(key).await.unwrap(),
        Some("true".to_string()),
        "DAO 中应存储 key {}",
        key
    );
}

/// 验证 get_cached_permission 未命中时返回 None。
#[tokio::test]
async fn get_cached_permission_miss_returns_none() {
    let dao = Arc::new(MockCacheDao::new());
    let iface = MockInterface::new();
    let fw = GarrisonPermissionStrategyDefault::new(Arc::new(iface)).with_dao(dao);

    let cached = fw
        .get_cached_permission("1001", "user:delete")
        .await
        .unwrap();
    assert!(cached.is_none(), "未缓存的权限应返回 None");
}

/// 验证缓存覆盖:相同 key 的第二次写入覆盖第一次。
#[tokio::test]
async fn cache_permission_overwrite() {
    let dao = Arc::new(MockCacheDao::new());
    let iface = MockInterface::new();
    let fw = GarrisonPermissionStrategyDefault::new(Arc::new(iface)).with_dao(dao);

    // 第一次缓存 true
    fw.cache_permission("1001", "user:read", true, 300)
        .await
        .unwrap();
    assert_eq!(
        fw.get_cached_permission("1001", "user:read").await.unwrap(),
        Some(true)
    );

    // 覆盖为 false
    fw.cache_permission("1001", "user:read", false, 300)
        .await
        .unwrap();
    assert_eq!(
        fw.get_cached_permission("1001", "user:read").await.unwrap(),
        Some(false),
        "覆盖后应返回 false"
    );
}

/// 验证 check_permission 优先读取缓存(短路优化)。
#[tokio::test]
async fn check_permission_uses_cache_short_circuit() {
    let dao = Arc::new(MockCacheDao::new());
    let mut iface = MockInterface::new();
    // interface 中无 user:read 权限
    iface.set_permissions("1001", &[]);
    let fw = GarrisonPermissionStrategyDefault::new(Arc::new(iface)).with_dao(dao.clone());

    // 预先写入缓存 true(与 interface 实际权限矛盾)
    fw.cache_permission("1001", "user:read", true, 300)
        .await
        .unwrap();

    // check_permission 应短路返回缓存值 true,不查询 interface
    assert!(
        fw.check_permission("1001", "user:read").await.unwrap(),
        "应优先返回缓存结果 true,而非查询 interface"
    );
}

// ------------------------------------------------------------------------
// 防火墙安全钩子集成测试
// ------------------------------------------------------------------------

/// 验证未注入 firewall_hook 时 check_login_hooks 为 no-op(向后兼容 0.2.x)。
#[cfg(any(
    feature = "sms-rate-limit",
    feature = "firewall-ratelimit",
    feature = "firewall-bruteforce",
    feature = "firewall-ddos",
    feature = "firewall",
    feature = "oauth2-server"
))]
#[tokio::test]
async fn check_login_hooks_noop_without_hook() {
    let iface = MockInterface::new();
    let fw = make_firewall(iface);
    let ctx = LoginContext::new("1001");

    // 未注入 hook,应直接返回 Ok
    assert!(
        fw.check_login_hooks("1001", &ctx).await.is_ok(),
        "未注入 firewall_hook 时 check_login_hooks 应为 no-op"
    );
}

/// 验证注入 hook 且所有检查通过时 check_login_hooks 返回 Ok。
#[cfg(any(
    feature = "sms-rate-limit",
    feature = "firewall-ratelimit",
    feature = "firewall-bruteforce",
    feature = "firewall-ddos",
    feature = "firewall",
    feature = "oauth2-server"
))]
#[tokio::test]
async fn check_login_hooks_passes_with_hook() {
    let iface = MockInterface::new();
    let hook = Arc::new(GarrisonFirewallCheckHookDefault::new());
    let fw = GarrisonPermissionStrategyDefault::new(Arc::new(iface)).with_firewall_hook(hook);
    let ctx = LoginContext::new("1001");

    // hook 计数器为空,所有检查应通过
    assert!(
        fw.check_login_hooks("1001", &ctx).await.is_ok(),
        "注入 hook 且无失败记录时 check_login_hooks 应返回 Ok"
    );
}

/// 验证 hook 在登录频率超限时阻断 check_login_hooks。
#[cfg(any(
    feature = "sms-rate-limit",
    feature = "firewall-ratelimit",
    feature = "firewall-bruteforce",
    feature = "firewall-ddos",
    feature = "firewall",
    feature = "oauth2-server"
))]
#[tokio::test]
async fn check_login_hooks_blocks_on_frequency_exceeded() {
    let iface = MockInterface::new();
    let hook = Arc::new(GarrisonFirewallCheckHookDefault::new());
    let fw =
        GarrisonPermissionStrategyDefault::new(Arc::new(iface)).with_firewall_hook(hook.clone());
    let ctx = LoginContext::new("1001").with_ip("1.2.3.4");

    // 记录 10 次失败(达到阈值)
    for _ in 0..10 {
        hook.record_failure(&ctx).await.unwrap();
    }

    // check_login_hooks 应被 login_frequency hook 阻断
    let result = fw.check_login_hooks("1001", &ctx).await;
    assert!(result.is_err(), "登录频率超限时应被 check_login_hooks 阻断");
    assert!(
        matches!(result.unwrap_err(), GarrisonError::Session(_)),
        "阻断错误应为 Session 类型"
    );
}

/// 验证 hook 在暴力破解超限时阻断 check_login_hooks。
#[cfg(any(
    feature = "sms-rate-limit",
    feature = "firewall-ratelimit",
    feature = "firewall-bruteforce",
    feature = "firewall-ddos",
    feature = "firewall",
    feature = "oauth2-server"
))]
#[tokio::test]
async fn check_login_hooks_blocks_on_brute_force_exceeded() {
    let iface = MockInterface::new();
    let hook = Arc::new(GarrisonFirewallCheckHookDefault::new());
    let fw =
        GarrisonPermissionStrategyDefault::new(Arc::new(iface)).with_firewall_hook(hook.clone());
    let ctx = LoginContext::new("1001"); // 无 IP,仅触发暴力破解检测

    // 记录 5 次失败(达到阈值)
    for _ in 0..5 {
        hook.record_failure(&ctx).await.unwrap();
    }

    // check_login_hooks 应被 brute_force hook 阻断
    let result = fw.check_login_hooks("1001", &ctx).await;
    assert!(result.is_err(), "暴力破解超限时应被 check_login_hooks 阻断");
}

/// 验证 with_firewall_hook builder 方法正确注入 hook。
#[cfg(any(
    feature = "sms-rate-limit",
    feature = "firewall-ratelimit",
    feature = "firewall-bruteforce",
    feature = "firewall-ddos",
    feature = "firewall",
    feature = "oauth2-server"
))]
#[tokio::test]
async fn with_firewall_hook_injects_hook() {
    let iface = MockInterface::new();
    let hook = Arc::new(GarrisonFirewallCheckHookDefault::new());
    let fw =
        GarrisonPermissionStrategyDefault::new(Arc::new(iface)).with_firewall_hook(hook.clone());

    // 注入后,记录失败并触发检测应能阻断
    let ctx = LoginContext::new("1001").with_ip("9.9.9.9");
    for _ in 0..10 {
        hook.record_failure(&ctx).await.unwrap();
    }
    let result = fw.check_login_hooks("1001", &ctx).await;
    assert!(result.is_err(), "注入 hook 后应能检测到频率超限并阻断");
}

/// 验证 check_login_hooks 按 5 个 hook 顺序调用(login_frequency 先于 brute_force)。
///
/// 当 IP 维度先达阈值时,应优先返回 login_frequency 错误。
#[cfg(any(
    feature = "sms-rate-limit",
    feature = "firewall-ratelimit",
    feature = "firewall-bruteforce",
    feature = "firewall-ddos",
    feature = "firewall",
    feature = "oauth2-server"
))]
#[tokio::test]
async fn check_login_hooks_calls_in_order() {
    use std::sync::atomic::{AtomicU8, Ordering};

    /// 记录调用顺序的测试 hook。
    struct OrderTrackingHook {
        order: Arc<AtomicU8>,
    }

    #[async_trait]
    impl GarrisonFirewallCheckHook for OrderTrackingHook {
        async fn check_login_frequency(&self, _ctx: &LoginContext) -> GarrisonResult<()> {
            self.order.fetch_add(1, Ordering::SeqCst);
            Ok(())
        }
        async fn check_brute_force(&self, _ctx: &LoginContext) -> GarrisonResult<()> {
            self.order.fetch_add(2, Ordering::SeqCst);
            Ok(())
        }
        async fn check_geo_anomaly(&self, _ctx: &LoginContext) -> GarrisonResult<()> {
            self.order.fetch_add(4, Ordering::SeqCst);
            Ok(())
        }
        async fn check_token_reuse(&self, _ctx: &LoginContext) -> GarrisonResult<()> {
            self.order.fetch_add(8, Ordering::SeqCst);
            Ok(())
        }
        async fn check_device_anomaly(&self, _ctx: &LoginContext) -> GarrisonResult<()> {
            self.order.fetch_add(16, Ordering::SeqCst);
            Ok(())
        }
    }

    let order = Arc::new(AtomicU8::new(0));
    let hook = Arc::new(OrderTrackingHook {
        order: order.clone(),
    });
    let iface = MockInterface::new();
    let fw = GarrisonPermissionStrategyDefault::new(Arc::new(iface)).with_firewall_hook(hook);
    let ctx = LoginContext::new("1001");

    fw.check_login_hooks("1001", &ctx).await.unwrap();

    // 5 个 hook 按序调用:1 + 2 + 4 + 8 + 16 = 31
    assert_eq!(order.load(Ordering::SeqCst), 31, "5 个 hook 应全部按序调用");
}

/// 验证 check_login_hooks 任一 hook Err 立即阻断后续 hook。
#[cfg(any(
    feature = "sms-rate-limit",
    feature = "firewall-ratelimit",
    feature = "firewall-bruteforce",
    feature = "firewall-ddos",
    feature = "firewall",
    feature = "oauth2-server"
))]
#[tokio::test]
async fn check_login_hooks_short_circuits_on_err() {
    use std::sync::atomic::{AtomicU8, Ordering};

    struct ShortCircuitHook {
        called: Arc<AtomicU8>,
    }

    #[async_trait]
    impl GarrisonFirewallCheckHook for ShortCircuitHook {
        async fn check_login_frequency(&self, _ctx: &LoginContext) -> GarrisonResult<()> {
            self.called.fetch_add(1, Ordering::SeqCst);
            Err(GarrisonError::Session("frequency blocked".to_string()))
        }
        async fn check_brute_force(&self, _ctx: &LoginContext) -> GarrisonResult<()> {
            self.called.fetch_add(2, Ordering::SeqCst);
            Ok(())
        }
        async fn check_geo_anomaly(&self, _ctx: &LoginContext) -> GarrisonResult<()> {
            self.called.fetch_add(4, Ordering::SeqCst);
            Ok(())
        }
        async fn check_token_reuse(&self, _ctx: &LoginContext) -> GarrisonResult<()> {
            self.called.fetch_add(8, Ordering::SeqCst);
            Ok(())
        }
        async fn check_device_anomaly(&self, _ctx: &LoginContext) -> GarrisonResult<()> {
            self.called.fetch_add(16, Ordering::SeqCst);
            Ok(())
        }
    }

    let called = Arc::new(AtomicU8::new(0));
    let hook = Arc::new(ShortCircuitHook {
        called: called.clone(),
    });
    let iface = MockInterface::new();
    let fw = GarrisonPermissionStrategyDefault::new(Arc::new(iface)).with_firewall_hook(hook);
    let ctx = LoginContext::new("1001");

    let result = fw.check_login_hooks("1001", &ctx).await;
    assert!(result.is_err(), "应在第一个 hook Err 时阻断");

    // 仅第一个 hook 被调用(值为 1),后续 4 个未调用
    assert_eq!(
        called.load(Ordering::SeqCst),
        1,
        "第一个 hook Err 后应短路,后续 hook 不应被调用"
    );
}

// ========================================================================
// 覆盖率补充:with_listener_manager、缓存写入失败、多 hook 失败
// ========================================================================

/// `with_listener_manager` 注入后 listener_manager 字段为 Some。
///
/// 覆盖行 275-277(builder 方法体)。
#[cfg(feature = "listener")]
#[test]
fn with_listener_manager_sets_field() {
    use crate::listener::GarrisonListenerManager;
    let lm = Arc::new(GarrisonListenerManager::new());
    let fw = GarrisonPermissionStrategyDefault::new(Arc::new(MockInterface::new()))
        .with_listener_manager(lm);
    assert!(
        fw.listener_manager.is_some(),
        "with_listener_manager 后 listener_manager 应为 Some"
    );
}

/// `check_permission` 缓存写入失败时仅 warn 不中断,仍返回正确结果。
///
/// 覆盖行 394-396, 398(缓存写入失败 warn 分支)。
///
/// 使用 FailingDao(set 方法返回 Err)触发缓存写入失败。
#[tokio::test]
async fn check_permission_cache_write_failure_warns_but_returns_result() {
    /// 所有写操作都失败的 DAO
    struct FailingDao;
    #[async_trait]
    impl crate::dao::GarrisonDao for FailingDao {
        async fn get(&self, _key: &str) -> GarrisonResult<Option<String>> {
            Ok(None)
        }
        async fn set(&self, _key: &str, _value: &str, _ttl: u64) -> GarrisonResult<()> {
            Err(GarrisonError::Dao("simulated set failure".to_string()))
        }
        async fn update(&self, _key: &str, _value: &str) -> GarrisonResult<()> {
            Err(GarrisonError::Dao("simulated update failure".to_string()))
        }
        async fn expire(&self, _key: &str, _seconds: u64) -> GarrisonResult<()> {
            Err(GarrisonError::Dao("simulated expire failure".to_string()))
        }
        async fn delete(&self, _key: &str) -> GarrisonResult<()> {
            Ok(())
        }
    }

    let mut iface = MockInterface::new();
    iface.set_permissions("1001", &["user:read"]);
    let fw = GarrisonPermissionStrategyDefault::new(Arc::new(iface)).with_dao(Arc::new(FailingDao));
    // 缓存写入失败但 check_permission 仍应返回 true(持有权限)
    let result = fw.check_permission("1001", "user:read").await;
    assert!(
        result.is_ok(),
        "缓存写入失败不应中断 check_permission,实际: {:?}",
        result
    );
    assert!(
        result.unwrap(),
        "持有权限应返回 true(缓存写入失败不影响结果)"
    );
}

/// `check_login_hooks` 第 3 个 hook(check_geo_anomaly)失败时广播 FirewallBlock 并阻断。
///
/// 覆盖行 467-468(第 3 个 hook 失败)+ 490, 492(broadcast_firewall_block)。
///
/// 注意:此测试同时引用 `GarrisonFirewallCheckHook` / `LoginContext`(依赖 firewall/oauth2
/// feature)和 `GarrisonListenerManager`(依赖 listener feature),需双重 cfg 门控。
/// 之前仅 `#[cfg(feature = "listener")]` 导致 listener + 无 firewall 时编译失败。
#[cfg(all(
    feature = "listener",
    any(
        feature = "sms-rate-limit",
        feature = "firewall-ratelimit",
        feature = "firewall-bruteforce",
        feature = "firewall-ddos",
        feature = "firewall",
        feature = "oauth2-server"
    )
))]
#[tokio::test]
async fn check_login_hooks_geo_anomaly_failure_broadcasts_firewall_block() {
    use crate::listener::GarrisonListenerManager;
    let iface = MockInterface::new();
    let lm = Arc::new(GarrisonListenerManager::new());

    struct GeoFailHook;
    #[async_trait]
    impl crate::strategy::GarrisonFirewallCheckHook for GeoFailHook {
        async fn check_login_frequency(&self, _ctx: &LoginContext) -> GarrisonResult<()> {
            Ok(())
        }
        async fn check_brute_force(&self, _ctx: &LoginContext) -> GarrisonResult<()> {
            Ok(())
        }
        async fn check_geo_anomaly(&self, _ctx: &LoginContext) -> GarrisonResult<()> {
            Err(GarrisonError::Session("geo blocked".to_string()))
        }
    }

    let fw = GarrisonPermissionStrategyDefault::new(Arc::new(iface))
        .with_firewall_hook(Arc::new(GeoFailHook))
        .with_listener_manager(lm);
    let ctx = LoginContext::new("1001");
    let result = fw.check_login_hooks("1001", &ctx).await;
    assert!(result.is_err(), "geo_anomaly 失败应阻断");
    assert!(
        matches!(result.unwrap_err(), GarrisonError::Session(_)),
        "应返回 Session 错误"
    );
}

/// `check_login_hooks` 第 4 个 hook(check_token_reuse)失败时广播并阻断。
///
/// 覆盖行 471-472(第 4 个 hook 失败)。
///
/// 注意:双重 cfg 门控同上(listener + firewall/oauth2)。
#[cfg(all(
    feature = "listener",
    any(
        feature = "sms-rate-limit",
        feature = "firewall-ratelimit",
        feature = "firewall-bruteforce",
        feature = "firewall-ddos",
        feature = "firewall",
        feature = "oauth2-server"
    )
))]
#[tokio::test]
async fn check_login_hooks_token_reuse_failure_broadcasts() {
    use crate::listener::GarrisonListenerManager;
    let iface = MockInterface::new();
    let lm = Arc::new(GarrisonListenerManager::new());

    struct TokenReuseFailHook;
    #[async_trait]
    impl crate::strategy::GarrisonFirewallCheckHook for TokenReuseFailHook {
        async fn check_login_frequency(&self, _ctx: &LoginContext) -> GarrisonResult<()> {
            Ok(())
        }
        async fn check_brute_force(&self, _ctx: &LoginContext) -> GarrisonResult<()> {
            Ok(())
        }
        async fn check_geo_anomaly(&self, _ctx: &LoginContext) -> GarrisonResult<()> {
            Ok(())
        }
        async fn check_token_reuse(&self, _ctx: &LoginContext) -> GarrisonResult<()> {
            Err(GarrisonError::Session("token reuse blocked".to_string()))
        }
    }

    let fw = GarrisonPermissionStrategyDefault::new(Arc::new(iface))
        .with_firewall_hook(Arc::new(TokenReuseFailHook))
        .with_listener_manager(lm);
    let ctx = LoginContext::new("1001");
    let result = fw.check_login_hooks("1001", &ctx).await;
    assert!(result.is_err(), "token_reuse 失败应阻断");
}

/// `check_login_hooks` 第 5 个 hook(check_device_anomaly)失败时广播并阻断。
///
/// 覆盖行 475-476(第 5 个 hook 失败)。
///
/// 注意:双重 cfg 门控同上(listener + firewall/oauth2)。
#[cfg(all(
    feature = "listener",
    any(
        feature = "sms-rate-limit",
        feature = "firewall-ratelimit",
        feature = "firewall-bruteforce",
        feature = "firewall-ddos",
        feature = "firewall",
        feature = "oauth2-server"
    )
))]
#[tokio::test]
async fn check_login_hooks_device_anomaly_failure_broadcasts() {
    use crate::listener::GarrisonListenerManager;
    let iface = MockInterface::new();
    let lm = Arc::new(GarrisonListenerManager::new());

    struct DeviceFailHook;
    #[async_trait]
    impl crate::strategy::GarrisonFirewallCheckHook for DeviceFailHook {
        async fn check_login_frequency(&self, _ctx: &LoginContext) -> GarrisonResult<()> {
            Ok(())
        }
        async fn check_brute_force(&self, _ctx: &LoginContext) -> GarrisonResult<()> {
            Ok(())
        }
        async fn check_geo_anomaly(&self, _ctx: &LoginContext) -> GarrisonResult<()> {
            Ok(())
        }
        async fn check_token_reuse(&self, _ctx: &LoginContext) -> GarrisonResult<()> {
            Ok(())
        }
        async fn check_device_anomaly(&self, _ctx: &LoginContext) -> GarrisonResult<()> {
            Err(GarrisonError::Session("device anomaly blocked".to_string()))
        }
    }

    let fw = GarrisonPermissionStrategyDefault::new(Arc::new(iface))
        .with_firewall_hook(Arc::new(DeviceFailHook))
        .with_listener_manager(lm);
    let ctx = LoginContext::new("1001");
    let result = fw.check_login_hooks("1001", &ctx).await;
    assert!(result.is_err(), "device_anomaly 失败应阻断");
}