View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests for pragmas
34   */
35  public class ComposePermissionsTest extends JexlTestCase {
36      static final String SAMPLE_JSON = "src/test/scripts/sample.json";
37  
38      /**
39       * Create a new test case.
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          // will succeed because java.util.Map is allowed and gson LinkedTreeMap is one
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          // will fail if gson package is denied
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          // will fail since gson package is denied
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          // will not fail since gson objects
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  }