package schutzaria import ( "sync" "testing" ) func TestLocalityAccessIntersectionAndGrantLimits(t *testing.T) { for _, parentRead := range []bool{false, true} { for _, parentWrite := range []bool{false, true} { for _, childRead := range []bool{false, true} { for _, childWrite := range []bool{false, true} { for _, grant := range []bool{false, true} { for _, basis := range []bool{false, true} { p := NewLocalityAccess(false, []AccessLayer{{Path: "/life", Read: parentRead, Write: parentWrite}, {Path: "/life/home", Read: childRead, Write: childWrite, Grant: grant}}) for _, cap := range []string{"read", "write", "create", "upload", "rename", "link", "delete", "manage"} { allowed := parentWrite && childWrite if cap == "read" { allowed = parentRead && childRead } positive := grant && (cap == "read" || cap == "write" || cap == "create" || cap == "upload") want := allowed && (basis || positive) if got := p.Decide("/life/home/page", cap, basis); got != want { t.Fatalf("parents=%v/%v children=%v/%v grant=%v base=%v cap=%s got=%v", parentRead, parentWrite, childRead, childWrite, grant, basis, cap, got) } } if got := p.Decide("/lifes/other", "read", basis); got != basis { t.Fatal("prefix boundary leaked") } } } } } } } // Multiple locality nodes may share a boundary; one denial still wins. p := NewLocalityAccess(false, []AccessLayer{{Path: "/same", Read: true, Write: true, Grant: true}, {Path: "/same"}}) if p.Decide("/same/child", "read", true) { t.Fatal("same-Path denial lost") } } func TestLocalityAdminAndAbsentCapsNeverCreateGrants(t *testing.T) { for _, p := range []*LocalityAccess{nil, NewLocalityAccess(false, nil), NewLocalityAccess(true, []AccessLayer{{Path: "/"}})} { for _, basis := range []bool{false, true} { if p.Decide("/a", "read", basis) != basis || p.Decide("relative", "read", basis) || p.Decide("", "read", basis) { t.Fatal("admin/absent cap created a grant or allowed non-Path") } } if p.RequiresPathChecks() { t.Fatal("unexpected alias checks") } } if !NewLocalityAccess(false, []AccessLayer{{Path: "/a"}}).RequiresPathChecks() { t.Fatal("Object alias checks skipped") } } func TestReadPolicyLiteralScopesAndImmutability(t *testing.T) { exact := []string{"/exact", "/group_%"} subtrees := []string{"/group_%"} layers := []AccessLayer{{Path: "/group_%/private"}} access := NewLocalityAccess(false, layers) p := NewReadPathPolicy(false, exact, subtrees, access) exact[0] = "/leak" subtrees[0] = "/" layers[0] = AccessLayer{Path: "/group_%/private", Read: true, Grant: true} for path, want := range map[string]bool{"/exact": true, "/exact/child": false, "/group_%": true, "/group_%/a/b": true, "/group_%/private": false, "/group_%/private/child": false, "/groupXX/a": false, "/group_%2": false, "/leak": false, "/": false, "": false, "relative": false} { if got := p.Allows(path); got != want { t.Fatalf("path=%q got=%v want=%v", path, got, want) } } for _, deny := range []*ReadPathPolicy{nil, {}} { if deny.Allows("/anything") { t.Fatal("empty policy failed open") } } all := NewReadPathPolicy(true, nil, nil, access) if !all.Allows("/other") || all.Allows("/group_%/private") || all.Allows("relative") { t.Fatal("broad grant bypassed caps") } var wg sync.WaitGroup for i := 0; i < 16; i++ { wg.Add(1) go func() { defer wg.Done() for j := 0; j < 100; j++ { if !p.Allows("/exact") || p.Allows("/leak") { t.Error("snapshot changed") } } }() } wg.Wait() } func TestGroupPermissionProfiles(t *testing.T) { wants := map[string]map[string]bool{ "read": {"read": true}, "read_write_once": {"read": true, "create": true, "upload": true}, "read_write": {"read": true, "create": true, "write": true, "upload": true, "rename": true, "link": true}, "upload_only": {"upload": true}, "unknown": {}, } for profile, want := range wants { for _, cap := range []string{"read", "create", "write", "upload", "rename", "link", "delete", "manage", ""} { for _, path := range []string{"/a_%", "/a_%/child"} { if GroupPermissionAllows("/a_%", profile, cap, path) != want[cap] { t.Fatal(profile, cap, path) } } if GroupPermissionAllows("/a_%", profile, cap, "/aXX/child") { t.Fatal("wildcard interpreted") } } } if p, err := NormalizeGroupPermission(" READ_WRITE_ONCE "); err != nil || p != "read_write_once" { t.Fatal(p, err) } for _, p := range []string{"", "delete", "group_admin", "read/write"} { if _, err := NormalizeGroupPermission(p); err == nil { t.Fatal("unknown profile", p) } } }