mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
Add C# test examples (#397)
This commit is contained in:
@@ -4,11 +4,20 @@
|
|||||||
"name": "Rust",
|
"name": "Rust",
|
||||||
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
|
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
|
||||||
"image": "mcr.microsoft.com/devcontainers/rust:1-1-bullseye",
|
"image": "mcr.microsoft.com/devcontainers/rust:1-1-bullseye",
|
||||||
|
"customizations": {
|
||||||
|
"vscode": {
|
||||||
|
"extensions": [
|
||||||
|
"ms-dotnettools.csharp",
|
||||||
|
"ms-dotnettools.csdevkit"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
"features": {
|
"features": {
|
||||||
"ghcr.io/devcontainers/features/dotnet:2": {},
|
"ghcr.io/devcontainers/features/dotnet:2": {
|
||||||
|
"version": "8.0"
|
||||||
|
},
|
||||||
"ghcr.io/devcontainers/features/python:1": {}
|
"ghcr.io/devcontainers/features/python:1": {}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use 'mounts' to make the cargo cache persistent in a Docker Volume.
|
// Use 'mounts' to make the cargo cache persistent in a Docker Volume.
|
||||||
// "mounts": [
|
// "mounts": [
|
||||||
// {
|
// {
|
||||||
|
|||||||
19
.github/workflows/test-csharp.yml
vendored
19
.github/workflows/test-csharp.yml
vendored
@@ -121,17 +121,12 @@ jobs:
|
|||||||
uses: actions/download-artifact@v4
|
uses: actions/download-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: regorus-nuget
|
name: regorus-nuget
|
||||||
path: ./bindings/csharp/TestApp/regorus-nuget/
|
path: ./bindings/csharp/Regorus.Tests/regorus-nuget/
|
||||||
|
|
||||||
- name: Restore Test App
|
|
||||||
run: dotnet restore /p:RestoreSources=./regorus-nuget/
|
|
||||||
working-directory: ./bindings/csharp/TestApp
|
|
||||||
|
|
||||||
- name: Build Test App 8.0
|
- name: Restore Regorus.Tests
|
||||||
run: dotnet build --framework net8.0
|
run: dotnet restore /p:RestoreAdditionalProjectSources=./regorus-nuget
|
||||||
working-directory: ./bindings/csharp/TestApp
|
working-directory: ./bindings/csharp/Regorus.Tests
|
||||||
|
|
||||||
- name: Run Test App 8.0
|
- name: Run Regorus.Tests
|
||||||
run: dotnet run --framework net8.0
|
run: dotnet test --no-restore
|
||||||
working-directory: ./bindings/csharp/TestApp
|
working-directory: ./bindings/csharp/Regorus.Tests
|
||||||
|
|
||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -27,5 +27,4 @@ bindings/*/target
|
|||||||
|
|
||||||
# C# build folders
|
# C# build folders
|
||||||
**bin
|
**bin
|
||||||
**obj
|
**obj
|
||||||
*.sln
|
|
||||||
22
bindings/csharp/Regorus.Tests/Regorus.Tests.csproj
Normal file
22
bindings/csharp/Regorus.Tests/Regorus.Tests.csproj
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<PropertyGroup>
|
||||||
|
<Nullable>Enable</Nullable>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<EnableMSTestRunner>true</EnableMSTestRunner>
|
||||||
|
<!-- More info about dotnet test integration https://learn.microsoft.com/dotnet/core/testing/unit-testing-platform-integration-dotnet-test -->
|
||||||
|
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
|
||||||
|
<TestingPlatformShowTestsFailure>true</TestingPlatformShowTestsFailure>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Include="../../../tests/**/*.*" Link="tests/%(RecursiveDir)%(Filename)%(Extension)" CopyToOutputDirectory="PreserveNewest" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="MSTest" Version="3.8.2" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Regorus" Version="0.5.0"/>
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
||||||
176
bindings/csharp/Regorus.Tests/RegorusTests.cs
Normal file
176
bindings/csharp/Regorus.Tests/RegorusTests.cs
Normal file
@@ -0,0 +1,176 @@
|
|||||||
|
// Copyright (c) Microsoft Corporation.
|
||||||
|
// Licensed under the MIT License.
|
||||||
|
|
||||||
|
namespace Regorus.Tests;
|
||||||
|
|
||||||
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
[TestClass]
|
||||||
|
public class RegorusTests
|
||||||
|
{
|
||||||
|
[TestMethod]
|
||||||
|
public void Basic_evaluation_succeeds()
|
||||||
|
{
|
||||||
|
using var engine = new Engine();
|
||||||
|
engine.AddPolicy(
|
||||||
|
"test.rego",
|
||||||
|
"package test\nx = 1\nmessage = `Hello`");
|
||||||
|
|
||||||
|
var result = engine.EvalRule("data.test.message");
|
||||||
|
|
||||||
|
Assert.AreEqual("\"Hello\"", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Evaluation_using_file_policies_succeeds()
|
||||||
|
{
|
||||||
|
using var engine = new Engine();
|
||||||
|
engine.SetRegoV0(true);
|
||||||
|
|
||||||
|
// Load policies and data.
|
||||||
|
engine.AddPolicyFromFile("tests/aci/framework.rego");
|
||||||
|
engine.AddPolicyFromFile("tests/aci/api.rego");
|
||||||
|
engine.AddPolicyFromFile("tests/aci/policy.rego");
|
||||||
|
engine.AddDataFromJsonFile("tests/aci/data.json");
|
||||||
|
|
||||||
|
// Set input and eval rule.
|
||||||
|
engine.SetInputFromJsonFile("tests/aci/input.json");
|
||||||
|
var result = engine.EvalRule("data.framework.mount_overlay");
|
||||||
|
|
||||||
|
var expected = """
|
||||||
|
{
|
||||||
|
"allowed": true,
|
||||||
|
"metadata": [
|
||||||
|
{
|
||||||
|
"action": "add",
|
||||||
|
"key": "container0",
|
||||||
|
"name": "matches",
|
||||||
|
"value": [
|
||||||
|
{
|
||||||
|
"allow_elevated": true,
|
||||||
|
"allow_stdio_access": false,
|
||||||
|
"capabilities": {
|
||||||
|
"ambient": [
|
||||||
|
"CAP_SYS_ADMIN"
|
||||||
|
],
|
||||||
|
"bounding": [
|
||||||
|
"CAP_SYS_ADMIN"
|
||||||
|
],
|
||||||
|
"effective": [
|
||||||
|
"CAP_SYS_ADMIN"
|
||||||
|
],
|
||||||
|
"inheritable": [
|
||||||
|
"CAP_SYS_ADMIN"
|
||||||
|
],
|
||||||
|
"permitted": [
|
||||||
|
"CAP_SYS_ADMIN"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"command": [
|
||||||
|
"rustc",
|
||||||
|
"--help"
|
||||||
|
],
|
||||||
|
"env_rules": [
|
||||||
|
{
|
||||||
|
"pattern": "PATH=/usr/local/cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
|
||||||
|
"required": true,
|
||||||
|
"strategy": "string"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"pattern": "RUSTUP_HOME=/usr/local/rustup",
|
||||||
|
"required": true,
|
||||||
|
"strategy": "string"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"pattern": "CARGO_HOME=/usr/local/cargo",
|
||||||
|
"required": true,
|
||||||
|
"strategy": "string"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"pattern": "RUST_VERSION=1.52.1",
|
||||||
|
"required": true,
|
||||||
|
"strategy": "string"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"pattern": "TERM=xterm",
|
||||||
|
"required": false,
|
||||||
|
"strategy": "string"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"pattern": "PREFIX_.+=.+",
|
||||||
|
"required": false,
|
||||||
|
"strategy": "re2"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"exec_processes": [
|
||||||
|
{
|
||||||
|
"command": [
|
||||||
|
"top"
|
||||||
|
],
|
||||||
|
"signals": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"layers": [
|
||||||
|
"fe84c9d5bfddd07a2624d00333cf13c1a9c941f3a261f13ead44fc6a93bc0e7a",
|
||||||
|
"4dedae42847c704da891a28c25d32201a1ae440bce2aecccfa8e6f03b97a6a6c",
|
||||||
|
"41d64cdeb347bf236b4c13b7403b633ff11f1cf94dbc7cf881a44d6da88c5156",
|
||||||
|
"eb36921e1f82af46dfe248ef8f1b3afb6a5230a64181d960d10237a08cd73c79",
|
||||||
|
"e769d7487cc314d3ee748a4440805317c19262c7acd2fdbdb0d47d2e4613a15c",
|
||||||
|
"1b80f120dbd88e4355d6241b519c3e25290215c469516b49dece9cf07175a766"
|
||||||
|
],
|
||||||
|
"mounts": [
|
||||||
|
{
|
||||||
|
"destination": "/container/path/one",
|
||||||
|
"options": [
|
||||||
|
"rbind",
|
||||||
|
"rshared",
|
||||||
|
"rw"
|
||||||
|
],
|
||||||
|
"source": "sandbox:///host/path/one",
|
||||||
|
"type": "bind"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"destination": "/container/path/two",
|
||||||
|
"options": [
|
||||||
|
"rbind",
|
||||||
|
"rshared",
|
||||||
|
"ro"
|
||||||
|
],
|
||||||
|
"source": "sandbox:///host/path/two",
|
||||||
|
"type": "bind"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"no_new_privileges": true,
|
||||||
|
"seccomp_profile_sha256": "",
|
||||||
|
"signals": [],
|
||||||
|
"user": {
|
||||||
|
"group_idnames": [
|
||||||
|
{
|
||||||
|
"pattern": "",
|
||||||
|
"strategy": "any"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"umask": "0022",
|
||||||
|
"user_idname": {
|
||||||
|
"pattern": "",
|
||||||
|
"strategy": "any"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"working_dir": "/home/user"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"action": "add",
|
||||||
|
"key": "/run/gcs/c/container0/rootfs",
|
||||||
|
"name": "overlayTargets",
|
||||||
|
"value": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
|
||||||
|
Assert.IsTrue(JsonNode.DeepEquals(JsonNode.Parse(expected), JsonNode.Parse(result!)), $"Actual: {result}");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
},
|
},
|
||||||
"sdk": {
|
"sdk": {
|
||||||
"allowPrerelease": false,
|
"allowPrerelease": false,
|
||||||
"version": "8.0.407",
|
"version": "8.0.408",
|
||||||
"rollForward": "disable"
|
"rollForward": "disable"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user