1 /*Copyright (C) 2014 Red Hat, Inc.
2 
3 This file is part of IcedTea.
4 
5 IcedTea is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 2.
8 
9 IcedTea is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with IcedTea; see the file COPYING.  If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301 USA.
18 
19 Linking this library statically or dynamically with other modules is
20 making a combined work based on this library.  Thus, the terms and
21 conditions of the GNU General Public License cover the whole
22 combination.
23 
24 As a special exception, the copyright holders of this library give you
25 permission to link this library with independent modules to produce an
26 executable, regardless of the license terms of these independent
27 modules, and to copy and distribute the resulting executable under
28 terms of your choice, provided that you also meet, for each linked
29 independent module, the terms and conditions of the license of that
30 module.  An independent module is a module which is not derived from
31 or based on this library.  If you modify this library, you may extend
32 this exception to your version of the library, but you are not
33 obligated to do so.  If you do not wish to do so, delete this
34 exception statement from your version.
35  */
36 
37 package net.sourceforge.jnlp.security.policyeditor;
38 
39 import static org.junit.Assert.assertEquals;
40 import static org.junit.Assert.assertNotEquals;
41 import static org.junit.Assert.assertTrue;
42 
43 import org.junit.Test;
44 
45 public class CustomPermissionTest {
46 
47     @Test
assertFieldsPopulateCorrectly()48     public void assertFieldsPopulateCorrectly() throws Exception {
49         final CustomPermission cp = new CustomPermission("type", "target", "some,actions");
50         assertTrue("Permission type should be \"type\"", cp.type.equals("type"));
51         assertTrue("Permission target should be \"target\"", cp.target.equals("target"));
52         assertTrue("Permission actions should be \"some,actions\"", cp.actions.equals("some,actions"));
53     }
54 
55     @Test
testFromStringWithoutActions()56     public void testFromStringWithoutActions() throws Exception {
57         final CustomPermission cp = CustomPermission.fromString("permission java.lang.RuntimePermission \"queuePrintJob\";");
58         assertTrue("Permission type should be \"java.lang.RuntimePermission\"", cp.type.equals("java.lang.RuntimePermission"));
59         assertTrue("Permission target should be \"queuePrintJob\"", cp.target.equals("queuePrintJob"));
60         assertTrue("Permission actions should be empty", cp.actions.isEmpty());
61     }
62 
63     @Test
testFromStringWithActions()64     public void testFromStringWithActions() throws Exception {
65         final CustomPermission cp = CustomPermission.fromString("permission java.io.FilePermission \"*\", \"read,write\";");
66         assertTrue("Permission type should be \"java.io.FilePermission\"", cp.type.equals("java.io.FilePermission"));
67         assertTrue("Permission target should be \"*\"", cp.target.equals("*"));
68         assertTrue("Permission actions should be \"read,write\"", cp.actions.equals("read,write"));
69     }
70 
71     @Test
testMissingQuotationMarks()72     public void testMissingQuotationMarks() throws Exception {
73         final CustomPermission cp = CustomPermission.fromString("permission java.io.FilePermission *, read,write;");
74         assertTrue("Custom permission should be null", cp == null);
75     }
76 
77     @Test
testActionsMissingComma()78     public void testActionsMissingComma() throws Exception {
79         final String missingComma = "permission java.io.FilePermission \"*\" \"read,write\";";
80         final CustomPermission cp1 = CustomPermission.fromString(missingComma);
81         assertTrue("Custom permission for " + missingComma + " should be null", cp1 == null);
82     }
83 
84     @Test
testActionsMissingFirstQuote()85     public void testActionsMissingFirstQuote() throws Exception {
86         final String missingFirstQuote = "permission java.io.FilePermission \"*\", read,write\";";
87         final CustomPermission cp2 = CustomPermission.fromString(missingFirstQuote);
88         assertTrue("Custom permission for " + missingFirstQuote + " should be null", cp2 == null);
89     }
90 
91     @Test
testActionsMissingSecondQuote()92     public void testActionsMissingSecondQuote() throws Exception {
93         final String missingSecondQuote = "permission java.io.FilePermission \"*\", \"read,write;";
94         final CustomPermission cp3 = CustomPermission.fromString(missingSecondQuote);
95         assertTrue("Custom permission for " + missingSecondQuote + " should be null", cp3 == null);
96     }
97 
98     @Test
testActionsMissingBothQuotes()99     public void testActionsMissingBothQuotes() throws Exception {
100         final String missingBothQuotes = "permission java.io.FilePermission \"*\", read,write;";
101         final CustomPermission cp4 = CustomPermission.fromString(missingBothQuotes);
102         assertTrue("Custom permission for " + missingBothQuotes + " should be null", cp4 == null);
103     }
104 
105     @Test
testActionsMissingAllPunctuation()106     public void testActionsMissingAllPunctuation() throws Exception {
107         final String missingAll = "permission java.io.FilePermission \"*\" read,write;";
108         final CustomPermission cp5 = CustomPermission.fromString(missingAll);
109         assertTrue("Custom permission for " + missingAll + " should be null", cp5 == null);
110     }
111 
112     @Test
testToString()113     public void testToString() throws Exception {
114         final CustomPermission cp = new CustomPermission("java.io.FilePermission", "*", "read");
115         final String expected = "permission java.io.FilePermission \"*\", \"read\";";
116         assertTrue("Permissions string should have equalled " + expected, cp.toString().equals(expected));
117     }
118 
119     @Test
testToStringWithNoActions()120     public void testToStringWithNoActions() throws Exception {
121         final CustomPermission cp = new CustomPermission("java.lang.RuntimePermission", "createClassLoader");
122         final String expected = "permission java.lang.RuntimePermission \"createClassLoader\";";
123         assertEquals(expected, cp.toString());
124     }
125 
126     @Test
testToStringWithEmptyActions()127     public void testToStringWithEmptyActions() throws Exception {
128         final CustomPermission cp = new CustomPermission("java.lang.RuntimePermission", "createClassLoader", "");
129         final String expected = "permission java.lang.RuntimePermission \"createClassLoader\";";
130         assertEquals(expected, cp.toString());
131     }
132 
133     @Test
testCompareToWithNoActions()134     public void testCompareToWithNoActions() throws Exception {
135         final CustomPermission cp1 = new CustomPermission("java.io.FilePermission", "*", "read");
136         final CustomPermission cp2 = new CustomPermission("java.io.FilePermission", "${user.home}${/}*", "read");
137         final CustomPermission cp3 = new CustomPermission("java.lang.RuntimePermission", "queuePrintJob");
138         assertTrue("cp1.compareTo(cp2) should be > 0", cp1.compareTo(cp2) > 0);
139         assertTrue("cp1.compareTo(cp1) should be 0", cp1.compareTo(cp1) == 0);
140         assertTrue("cp2.compareTo(cp3) should be < 0", cp2.compareTo(cp3) < 0);
141     }
142 
143     @Test
testCompareToWithEmptyActions()144     public void testCompareToWithEmptyActions() throws Exception {
145         final CustomPermission cp1 = new CustomPermission("java.io.FilePermission", "*", "read");
146         final CustomPermission cp2 = new CustomPermission("java.io.FilePermission", "${user.home}${/}*", "read");
147         final CustomPermission cp3 = new CustomPermission("java.lang.RuntimePermission", "queuePrintJob", "");
148         assertTrue("cp1.compareTo(cp2) should be > 0", cp1.compareTo(cp2) > 0);
149         assertTrue("cp1.compareTo(cp1) should be 0", cp1.compareTo(cp1) == 0);
150         assertTrue("cp2.compareTo(cp3) should be < 0", cp2.compareTo(cp3) < 0);
151     }
152 
153     @Test
testConstructFromEnumsToString()154     public void testConstructFromEnumsToString() throws Exception {
155         final CustomPermission cp = new CustomPermission(PermissionType.FILE_PERMISSION, PermissionTarget.USER_HOME, PermissionActions.READ);
156         final String permissionString = "permission java.io.FilePermission \"${user.home}\", \"read\";";
157         assertEquals(permissionString, cp.toString());
158     }
159 
160     @Test
testEquals()161     public void testEquals() throws Exception {
162         final CustomPermission cp1 = new CustomPermission(PermissionType.RUNTIME_PERMISSION, PermissionTarget.CLASSLOADER);
163         final CustomPermission cp2 = new CustomPermission(PermissionType.RUNTIME_PERMISSION, PermissionTarget.CLASSLOADER);
164         final CustomPermission cp3 = new CustomPermission(PermissionType.RUNTIME_PERMISSION, PermissionTarget.ACCESS_THREADS);
165         assertEquals(cp1, cp2);
166         assertNotEquals(cp1, cp3);
167     }
168 
169     @Test
testHashcode()170     public void testHashcode() throws Exception {
171         final CustomPermission cp1 = new CustomPermission(PermissionType.RUNTIME_PERMISSION, PermissionTarget.CLASSLOADER);
172         final CustomPermission cp2 = new CustomPermission(PermissionType.RUNTIME_PERMISSION, PermissionTarget.CLASSLOADER);
173         final CustomPermission cp3 = new CustomPermission(PermissionType.RUNTIME_PERMISSION, PermissionTarget.ACCESS_THREADS);
174         assertEquals(cp1.hashCode(), cp2.hashCode());
175         assertNotEquals(cp1.hashCode(), cp3.hashCode());
176     }
177 }
178