| // Licensed to the Apache Software Foundation (ASF) under one | |
| // or more contributor license agreements. See the NOTICE file | |
| // distributed with this work for additional information | |
| // regarding copyright ownership. The ASF licenses this file | |
| // to you under the Apache License, Version 2.0 (the | |
| // "License"); you may not use this file except in compliance | |
| // with the License. You may obtain a copy of the License at | |
| // | |
| // https://www.apache.org/licenses/LICENSE-2.0 | |
| // | |
| // Unless required by applicable law or agreed to in writing, | |
| // software distributed under the License is distributed on an | |
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| // KIND, either express or implied. See the License for the | |
| // specific language governing permissions and limitations | |
| // under the License. | |
| using System; | |
| using Casbin.Model; | |
| using Casbin.UnitTests.Util; | |
| using DynamicExpresso; | |
| using Xunit; | |
| namespace Casbin.UnitTests.GenericTests; | |
| public class GenericFunctionTest | |
| { | |
| [Fact] | |
| public void TestGenericFunction() | |
| { | |
| Interpreter interpreter = new(); | |
| RequestValues<string, int> r = Request.CreateValues("A", 1); | |
| PolicyValues<string, int> p = Policy.CreateValues("A", 1); | |
| interpreter.SetFunction("equal", new Func<string, string, bool>( | |
| (a, b) => a == b) | |
| ); | |
| interpreter.SetFunction("equal", new Func<int, int, bool>( | |
| (a, b) => a == b) | |
| ); | |
| Func<RequestValues<string, int>, PolicyValues<string, int>, bool> func1 = | |
| ExpressionUtil.Compile(interpreter, "equal(r.Value2, p.Value2) && equal(r.Value2, p.Value2)", | |
| nameof(r), in r, nameof(p), in p); | |
| Assert.True(func1(Request.CreateValues("A", 1), Policy.CreateValues("A", 1))); | |
| Assert.False(func1(Request.CreateValues("A", 1), Policy.CreateValues("A", 2))); | |
| Assert.False(func1(Request.CreateValues("B", 1), Policy.CreateValues("B", 2))); | |
| } | |
| #if !NET452 | |
| [Fact] | |
| public void TestGenericFunctionModel() | |
| { | |
| Enforcer e = new Enforcer(DefaultModel.NewModelFromText( | |
| """ | |
| [request_definition] | |
| r = obj1, obj2 | |
| [policy_definition] | |
| p = _ | |
| [policy_effect] | |
| e = some(where (p.eft == allow)) | |
| [matchers] | |
| m = max(r.obj1, r.obj2) > 2 | |
| """)); | |
| e.AddFunction("max", new Func<int, int, int>( | |
| // ReSharper disable once ConvertClosureToMethodGroup | |
| (a, b) => Math.Max(a, b) | |
| )); | |
| Assert.True(e.Enforce(1, 3)); | |
| Assert.False(e.Enforce(1, 2)); | |
| Assert.False(e.Enforce("1", "111")); | |
| e.AddFunction("max", new Func<string, string, int>( | |
| (a, b) => Math.Max(a.Length, b.Length) | |
| )); | |
| Assert.True(e.Enforce(1, 3)); | |
| Assert.False(e.Enforce(1, 2)); | |
| Assert.True(e.Enforce("1", "111")); | |
| } | |
| #endif | |
| } |