|
| 1 | +// Copyright 2023 The casbin Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package xormadapter |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "testing" |
| 20 | + "time" |
| 21 | + |
| 22 | + "github.com/agiledragon/gomonkey/v2" |
| 23 | + "github.com/casbin/casbin/v2" |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "xorm.io/xorm" |
| 26 | +) |
| 27 | + |
| 28 | +func mockExecuteWithContextTimeOut(ctx context.Context, fn func() error) error { |
| 29 | + done := make(chan error) |
| 30 | + go func() { |
| 31 | + time.Sleep(500 * time.Microsecond) |
| 32 | + done <- fn() |
| 33 | + }() |
| 34 | + |
| 35 | + select { |
| 36 | + case <-ctx.Done(): |
| 37 | + return ctx.Err() |
| 38 | + case err := <-done: |
| 39 | + return err |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func clearDBPolicy() (*casbin.Enforcer, *ContextAdapter) { |
| 44 | + ca, err := NewContextAdapter("mysql", "root:@tcp(127.0.0.1:3306)/") |
| 45 | + if err != nil { |
| 46 | + panic(err) |
| 47 | + } |
| 48 | + |
| 49 | + e, err := casbin.NewEnforcer("examples/rbac_model.conf", ca) |
| 50 | + if err != nil { |
| 51 | + panic(err) |
| 52 | + } |
| 53 | + |
| 54 | + e.ClearPolicy() |
| 55 | + _ = e.SavePolicy() |
| 56 | + |
| 57 | + return e, ca |
| 58 | + |
| 59 | + return e, ca |
| 60 | +} |
| 61 | + |
| 62 | +func TestContextAdapter_LoadPolicyCtx(t *testing.T) { |
| 63 | + e, ca := clearDBPolicy() |
| 64 | + |
| 65 | + engine, _ := xorm.NewEngine("mysql", "root:@tcp(127.0.0.1:3306)/casbin") |
| 66 | + policy := &CasbinRule{ |
| 67 | + Ptype: "p", |
| 68 | + V0: "alice", |
| 69 | + V1: "data1", |
| 70 | + V2: "read", |
| 71 | + } |
| 72 | + _, err := engine.Insert(policy) |
| 73 | + if err != nil { |
| 74 | + panic(err) |
| 75 | + } |
| 76 | + |
| 77 | + assert.NoError(t, ca.LoadPolicyCtx(context.Background(), e.GetModel())) |
| 78 | + e, _ = casbin.NewEnforcer(e.GetModel(), ca) |
| 79 | + testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}}) |
| 80 | + |
| 81 | + var p = gomonkey.ApplyFunc(executeWithContext, mockExecuteWithContextTimeOut) |
| 82 | + defer p.Reset() |
| 83 | + ctx, cancel := context.WithTimeout(context.Background(), 300*time.Microsecond) |
| 84 | + defer cancel() |
| 85 | + |
| 86 | + assert.EqualError(t, ca.LoadPolicyCtx(ctx, e.GetModel()), "context deadline exceeded") |
| 87 | +} |
| 88 | + |
| 89 | +func TestContextAdapter_SavePolicyCtx(t *testing.T) { |
| 90 | + e, ca := clearDBPolicy() |
| 91 | + |
| 92 | + e.EnableAutoSave(false) |
| 93 | + _, _ = e.AddPolicy("alice", "data1", "read") |
| 94 | + assert.NoError(t, ca.SavePolicyCtx(context.Background(), e.GetModel())) |
| 95 | + _ = e.LoadPolicy() |
| 96 | + testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}}) |
| 97 | + |
| 98 | + var p = gomonkey.ApplyFunc(executeWithContext, mockExecuteWithContextTimeOut) |
| 99 | + defer p.Reset() |
| 100 | + ctx, cancel := context.WithTimeout(context.Background(), 300*time.Microsecond) |
| 101 | + defer cancel() |
| 102 | + assert.EqualError(t, ca.SavePolicyCtx(ctx, e.GetModel()), "context deadline exceeded") |
| 103 | + // Sleep, waiting for the completion of the transaction commit |
| 104 | + time.Sleep(2 * time.Second) |
| 105 | +} |
| 106 | + |
| 107 | +func TestContextAdapter_AddPolicyCtx(t *testing.T) { |
| 108 | + e, ca := clearDBPolicy() |
| 109 | + |
| 110 | + assert.NoError(t, ca.AddPolicyCtx(context.Background(), "p", "p", []string{"alice", "data1", "read"})) |
| 111 | + _ = e.LoadPolicy() |
| 112 | + testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}}) |
| 113 | + |
| 114 | + var p = gomonkey.ApplyFunc(executeWithContext, mockExecuteWithContextTimeOut) |
| 115 | + defer p.Reset() |
| 116 | + ctx, cancel := context.WithTimeout(context.Background(), 300*time.Microsecond) |
| 117 | + defer cancel() |
| 118 | + assert.EqualError(t, ca.AddPolicyCtx(ctx, "p", "p", []string{"alice", "data1", "read"}), "context deadline exceeded") |
| 119 | +} |
| 120 | + |
| 121 | +func TestContextAdapter_RemovePolicyCtx(t *testing.T) { |
| 122 | + e, ca := clearDBPolicy() |
| 123 | + |
| 124 | + _ = ca.AddPolicy("p", "p", []string{"alice", "data1", "read"}) |
| 125 | + _ = ca.AddPolicy("p", "p", []string{"alice", "data2", "read"}) |
| 126 | + assert.NoError(t, ca.RemovePolicyCtx(context.Background(), "p", "p", []string{"alice", "data1", "read"})) |
| 127 | + _ = e.LoadPolicy() |
| 128 | + testGetPolicy(t, e, [][]string{{"alice", "data2", "read"}}) |
| 129 | + |
| 130 | + var p = gomonkey.ApplyFunc(executeWithContext, mockExecuteWithContextTimeOut) |
| 131 | + defer p.Reset() |
| 132 | + ctx, cancel := context.WithTimeout(context.Background(), 300*time.Microsecond) |
| 133 | + defer cancel() |
| 134 | + assert.EqualError(t, ca.RemovePolicyCtx(ctx, "p", "p", []string{"alice", "data1", "read"}), "context deadline exceeded") |
| 135 | +} |
| 136 | + |
| 137 | +func TestContextAdapter_RemoveFilteredPolicyCtx(t *testing.T) { |
| 138 | + e, ca := clearDBPolicy() |
| 139 | + |
| 140 | + _ = ca.AddPolicy("p", "p", []string{"alice", "data1", "read"}) |
| 141 | + _ = ca.AddPolicy("p", "p", []string{"alice", "data1", "write"}) |
| 142 | + _ = ca.AddPolicy("p", "p", []string{"alice", "data2", "read"}) |
| 143 | + assert.NoError(t, ca.RemoveFilteredPolicyCtx(context.Background(), "p", "p", 1, "data1")) |
| 144 | + _ = e.LoadPolicy() |
| 145 | + testGetPolicy(t, e, [][]string{{"alice", "data2", "read"}}) |
| 146 | + |
| 147 | + var p = gomonkey.ApplyFunc(executeWithContext, mockExecuteWithContextTimeOut) |
| 148 | + defer p.Reset() |
| 149 | + ctx, cancel := context.WithTimeout(context.Background(), 300*time.Microsecond) |
| 150 | + defer cancel() |
| 151 | + assert.EqualError(t, ca.RemoveFilteredPolicyCtx(ctx, "p", "p", 1, "data1"), "context deadline exceeded") |
| 152 | +} |
0 commit comments