|
4 | 4 |
|
5 | 5 | import com.dylibso.chicory.runtime.ByteBufferMemory;
|
6 | 6 | import com.dylibso.chicory.wasm.types.MemoryLimits;
|
| 7 | +import com.fasterxml.jackson.core.JsonProcessingException; |
7 | 8 | import java.io.FileInputStream;
|
8 | 9 | import java.nio.file.Path;
|
| 10 | +import org.instancio.Instancio; |
| 11 | +import org.instancio.junit.Given; |
| 12 | +import org.instancio.junit.GivenProvider; |
| 13 | +import org.instancio.junit.InstancioExtension; |
| 14 | +import org.instancio.junit.InstancioSource; |
9 | 15 | import org.junit.jupiter.api.Assertions;
|
10 | 16 | import org.junit.jupiter.api.BeforeAll;
|
11 | 17 | import org.junit.jupiter.api.Test;
|
| 18 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 19 | +import org.junit.jupiter.params.ParameterizedTest; |
12 | 20 |
|
| 21 | +@ExtendWith(InstancioExtension.class) |
13 | 22 | public class OpaTest {
|
14 | 23 | static Path wasmFile;
|
15 | 24 | static Path issue69WasmFile;
|
| 25 | + static OpaPolicy issue107Policy; |
16 | 26 |
|
17 | 27 | @BeforeAll
|
18 | 28 | public static void beforeAll() throws Exception {
|
19 | 29 | wasmFile = OpaCli.compile("base", "opa/wasm/test/allowed").resolve("policy.wasm");
|
20 | 30 | issue69WasmFile = OpaCli.compile("issue69", "authz/allow").resolve("policy.wasm");
|
| 31 | + issue107Policy = |
| 32 | + OpaPolicy.builder() |
| 33 | + .withPolicy(OpaCli.compile("issue107", "test").resolve("policy.wasm")) |
| 34 | + .build(); |
21 | 35 | }
|
22 | 36 |
|
23 | 37 | @Test
|
@@ -106,6 +120,51 @@ public void issue69() throws Exception {
|
106 | 120 | Assertions.assertFalse(Utils.getResult(policy.evaluate()).asBoolean());
|
107 | 121 | }
|
108 | 122 |
|
| 123 | + @Test |
| 124 | + public void issue107() { |
| 125 | + var policy = issue107Policy; |
| 126 | + |
| 127 | + policy.input("{\"role\": \"admin\",\"name\": \"Doe\"}"); |
| 128 | + var result = policy.evaluate(); |
| 129 | + Assertions.assertTrue(Utils.getResult(result).get("allow").asBoolean()); |
| 130 | + |
| 131 | + var input = "{\"role\": \"admin\",\"name\": \"Štěpán\"}"; |
| 132 | + |
| 133 | + policy.input(input); |
| 134 | + result = policy.evaluate(); |
| 135 | + Assertions.assertTrue(Utils.getResult(result).get("allow").asBoolean()); |
| 136 | + } |
| 137 | + |
| 138 | + static class InputStringProvider implements GivenProvider { |
| 139 | + @Override |
| 140 | + public Object provide(ElementContext context) { |
| 141 | + String randomName = Instancio.gen().string().unicode().mixedCase().get(); |
| 142 | + try { |
| 143 | + String input = "{\"role\": \"admin\",\"name\": \"" + randomName + "\"}"; |
| 144 | + DefaultMappers.jsonMapper.readTree(input); |
| 145 | + return randomName; |
| 146 | + } catch (JsonProcessingException e) { |
| 147 | + // the generated input name breaks the json escaping |
| 148 | + return provide(context); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @InstancioSource(samples = 1000) |
| 154 | + @ParameterizedTest |
| 155 | + public void issue107Parametrized(@Given(InputStringProvider.class) String name) { |
| 156 | + var policy = issue107Policy; |
| 157 | + |
| 158 | + policy.input("{\"role\": \"admin\",\"name\": \"" + name + "\"}"); |
| 159 | + var result = policy.evaluate(); |
| 160 | + Assertions.assertTrue(Utils.getResult(result).get("allow").asBoolean()); |
| 161 | + |
| 162 | + policy.input("{\"role\": \"notadmin\",\"name\": \"" + name + "\"}"); |
| 163 | + |
| 164 | + result = policy.evaluate(); |
| 165 | + Assertions.assertFalse(Utils.getResult(result).get("allow").asBoolean()); |
| 166 | + } |
| 167 | + |
109 | 168 | @Test
|
110 | 169 | public void issue78Sprintf() throws Exception {
|
111 | 170 | var policyWasm =
|
|
0 commit comments