|
15 | 15 | package casbin |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "strings" |
18 | 19 | "sync" |
19 | 20 | "testing" |
20 | 21 |
|
| 22 | + "github.com/casbin/casbin/v3/detector" |
21 | 23 | "github.com/casbin/casbin/v3/model" |
22 | 24 | fileadapter "github.com/casbin/casbin/v3/persist/file-adapter" |
23 | 25 | "github.com/casbin/casbin/v3/util" |
@@ -724,3 +726,78 @@ func TestLinkConditionFunc(t *testing.T) { |
724 | 726 | testDomainEnforce(t, e, "alice", "domain5", "data5", "read", false) |
725 | 727 | testDomainEnforce(t, e, "alice", "domain5", "data5", "write", false) |
726 | 728 | } |
| 729 | + |
| 730 | +func TestEnforcerWithDefaultDetector(t *testing.T) { |
| 731 | + // Test that default detector is enabled and detects cycles |
| 732 | + _, err := NewEnforcer("examples/rbac_model.conf", "examples/rbac_with_cycle_policy.csv") |
| 733 | + |
| 734 | + // Expect an error because the policy contains a cycle |
| 735 | + if err == nil { |
| 736 | + t.Error("Expected cycle detection error when loading policy with cycle, but got nil") |
| 737 | + } else { |
| 738 | + errMsg := err.Error() |
| 739 | + if !strings.Contains(errMsg, "cycle detected") { |
| 740 | + t.Errorf("Expected error message to contain 'cycle detected', got: %s", errMsg) |
| 741 | + } |
| 742 | + } |
| 743 | +} |
| 744 | + |
| 745 | +func TestEnforcerRunDetections(t *testing.T) { |
| 746 | + // Test explicit RunDetections() call |
| 747 | + e, _ := NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv") |
| 748 | + |
| 749 | + // Should not error on valid policy |
| 750 | + err := e.RunDetections() |
| 751 | + if err != nil { |
| 752 | + t.Errorf("Expected no error when running detections on valid policy, but got: %v", err) |
| 753 | + } |
| 754 | + |
| 755 | + // Now add a cycle manually |
| 756 | + _, _ = e.AddGroupingPolicy("alice", "data2_admin") |
| 757 | + _, _ = e.AddGroupingPolicy("data2_admin", "super_admin") |
| 758 | + _, _ = e.AddGroupingPolicy("super_admin", "alice") |
| 759 | + |
| 760 | + // Should detect the cycle |
| 761 | + err = e.RunDetections() |
| 762 | + if err == nil { |
| 763 | + t.Error("Expected cycle detection error, but got nil") |
| 764 | + } else { |
| 765 | + errMsg := err.Error() |
| 766 | + if !strings.Contains(errMsg, "cycle detected") { |
| 767 | + t.Errorf("Expected error message to contain 'cycle detected', got: %s", errMsg) |
| 768 | + } |
| 769 | + } |
| 770 | +} |
| 771 | + |
| 772 | +func TestEnforcerSetDetector(t *testing.T) { |
| 773 | + // Test SetDetector() method |
| 774 | + e, _ := NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv") |
| 775 | + |
| 776 | + // Create a custom detector |
| 777 | + customDetector := detector.NewDefaultDetector() |
| 778 | + e.SetDetector(customDetector) |
| 779 | + |
| 780 | + // Should still work with custom detector |
| 781 | + err := e.RunDetections() |
| 782 | + if err != nil { |
| 783 | + t.Errorf("Expected no error with custom detector, but got: %v", err) |
| 784 | + } |
| 785 | +} |
| 786 | + |
| 787 | +func TestEnforcerSetDetectors(t *testing.T) { |
| 788 | + // Test SetDetectors() method |
| 789 | + e, _ := NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv") |
| 790 | + |
| 791 | + // Create multiple detectors |
| 792 | + detectors := []detector.Detector{ |
| 793 | + detector.NewDefaultDetector(), |
| 794 | + detector.NewDefaultDetector(), |
| 795 | + } |
| 796 | + e.SetDetectors(detectors) |
| 797 | + |
| 798 | + // Should work with multiple detectors |
| 799 | + err := e.RunDetections() |
| 800 | + if err != nil { |
| 801 | + t.Errorf("Expected no error with multiple detectors, but got: %v", err) |
| 802 | + } |
| 803 | +} |
0 commit comments