1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jexl3;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNotNull;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22
23 import java.io.File;
24 import java.io.FileReader;
25 import java.util.Collections;
26
27 import org.apache.commons.jexl3.introspection.JexlPermissions;
28 import org.junit.jupiter.api.Test;
29
30 import com.google.gson.Gson;
31
32
33
34
35 public class ComposePermissionsTest extends JexlTestCase {
36 static final String SAMPLE_JSON = "src/test/scripts/sample.json";
37
38
39
40
41 public ComposePermissionsTest() {
42 super("PermissionsTest");
43 }
44
45 void runComposePermissions(final JexlPermissions p) throws Exception {
46 final String check = "http://example.com/content.jpg";
47 final File jsonFile = new File(SAMPLE_JSON);
48 final Gson gson = new Gson();
49 final Object json;
50 try (final FileReader reader = new FileReader(jsonFile)) {
51 json = gson.fromJson(reader, Object.class);
52 assertNotNull(json);
53 }
54
55
56 final JexlEngine j0 = createEngine(false, p);
57 final JexlScript s0 = j0.createScript("json.pageInfo.pagePic", "json");
58 final Object r0 = s0.execute(null, json);
59 assertEquals(check, r0);
60
61
62 JexlEngine j1 = createEngine(false, p.compose("com.google.gson.internal {}"));
63 final JexlScript s1 = j1.createScript("json.pageInfo.pagePic", "json");
64 JexlException.Property xproperty = assertThrows(JexlException.Property.class, () -> s1.execute(null, json));
65 assertEquals("pageInfo", xproperty.getProperty());
66
67
68 j1 = createEngine(false, p.compose("com.google.gson.internal { LinkedTreeMap {} }"));
69 final JexlScript s2 = j1.createScript("json.pageInfo.pagePic", "json");
70 xproperty = assertThrows(JexlException.Property.class, () -> s2.execute(null, json));
71 assertEquals("pageInfo", xproperty.getProperty());
72
73
74 j1 = createEngine(false, JexlPermissions.RESTRICTED);
75 final JexlScript s3 = j1.createScript("json.pageInfo.pagePic", "json");
76 s3.execute(null, json);
77 assertEquals(check, r0);
78 }
79
80 @Test
81 public void testComposePermissions() throws Exception {
82 runComposePermissions(JexlPermissions.UNRESTRICTED);
83 }
84
85 @Test
86 public void testComposePermissions1() throws Exception {
87 runComposePermissions(new JexlPermissions.Delegate(JexlPermissions.UNRESTRICTED) {
88 @Override
89 public String toString() {
90 return "delegate:" + base.toString();
91 }
92 });
93 }
94
95 @Test
96 public void testComposePermissions2() throws Exception {
97 runComposePermissions(new JexlPermissions.ClassPermissions(JexlPermissions.UNRESTRICTED, Collections.emptySet()));
98 }
99 }