KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > test > NamespacePermissionsUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.security.test;
23
24 import java.io.FilePermission JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.security.CodeSource JavaDoc;
27 import java.security.Permission JavaDoc;
28 import java.security.PermissionCollection JavaDoc;
29 import java.security.Policy JavaDoc;
30
31 /**
32  * A JUnit TestCase for the NamespacePermissions and NamespacePermission
33  * classes.
34  *
35 @author Scott.Stark@jboss.org
36 @version $Revision: 37406 $
37 */

38 public class NamespacePermissionsUnitTestCase
39    extends junit.framework.TestCase
40 {
41    PermissionCollection JavaDoc pc;
42
43    public NamespacePermissionsUnitTestCase(String JavaDoc name)
44    {
45       super(name);
46    }
47
48    protected void setUp() throws Exception JavaDoc
49    {
50       pc = new NamespacePermissionCollection();
51       NamespacePermission p = new NamespacePermission("starksm/Project1", "r---");
52       pc.add(p);
53       p = new NamespacePermission("starksm/Project1/Documents/readme.html", "rw--");
54       pc.add(p);
55       p = new NamespacePermission("starksm/Project1/Documents/Public", "rw--");
56       pc.add(p);
57       p = new NamespacePermission("starksm/Project1/Documents/Public/Private", "----");
58       pc.add(p);
59       p = new NamespacePermission("Project1/Documents/Public", "r---");
60       pc.add(p);
61       p = new NamespacePermission("Project1/Documents/Public/starksm", "----");
62       pc.add(p);
63    }
64    protected void tearDown()
65    {
66       pc = null;
67    }
68
69    /**
70     * This method caueses errors, because newer junit this is a static.
71     * and thus can not be overridden.
72     *
73    protected void assertEquals(String msg, boolean expected, boolean actual)
74    {
75       assert(msg, (expected == actual));
76    }
77    */

78    
79    /**
80     * Test the NamespacePermissionCollection implies method for various
81     * permission that should be implied by the setup PermissionCollection.
82     */

83    public void testImplied()
84    {
85       NamespacePermission p = new NamespacePermission("Project1/Documents/Public/view1.jpg", "r---");
86       boolean implied = pc.implies(p);
87       assertEquals(p.toString(), true, implied);
88         
89       p = new NamespacePermission("starksm/Project1", "r---");
90       implied = pc.implies(p);
91       assertEquals(p.toString(), true, implied);
92       p = new NamespacePermission("starksm/Project1/Documents/Folder1", "r---");
93       implied = pc.implies(p);
94       assertEquals(p.toString(), true, implied);
95       p = new NamespacePermission("starksm/Project1/Documents/readme.html", "r---");
96       implied = pc.implies(p);
97       assertEquals(p.toString(), true, implied);
98       p = new NamespacePermission("starksm/Project1/Documents/readme.html", "rw--");
99       implied = pc.implies(p);
100       assertEquals(p.toString(), true, implied);
101       p = new NamespacePermission("starksm/Project1/Documents/readme.html", "-w--");
102       implied = pc.implies(p);
103       assertEquals(p.toString(), true, implied);
104       p = new NamespacePermission("starksm/Project1/Documents/Public/readme.html", "r---");
105       implied = pc.implies(p);
106       assertEquals(p.toString(), true, implied);
107       p = new NamespacePermission("starksm/Project1/Documents/Public/readme.html", "rw--");
108       implied = pc.implies(p);
109       //assertEquals(p.toString(), true, implied);
110
}
111
112    /**
113     * Test the NamespacePermissionCollection implies method for various
114     * permission that should NOT be implied by the setup PermissionCollection.
115     */

116    public void testNotImplied()
117    {
118       NamespacePermission p = new NamespacePermission("Project1/Drawings/view1.jpg", "r---");
119       boolean implied = pc.implies(p);
120       assertEquals(p.toString(), false, implied);
121       p = new NamespacePermission("Project1/Documents/view1.jpg", "r---");
122       implied = pc.implies(p);
123       assertEquals(p.toString(), false, implied);
124       p = new NamespacePermission("starksm/Project1/Documents/readme.html", "rw-d");
125       implied = pc.implies(p);
126       assertEquals(p.toString(), false, implied);
127       p = new NamespacePermission("starksm/Project1/Documents", "rw--");
128       implied = pc.implies(p);
129       assertEquals(p.toString(), false, implied);
130       p = new NamespacePermission("starksm/Project1/Documents/Public/Private/readme.html", "r---");
131       implied = pc.implies(p);
132       assertEquals(p.toString(), false, implied);
133       p = new NamespacePermission("starksm/Project1/Documents/Public/Private/readme.html", "rw--");
134       implied = pc.implies(p);
135       assertEquals(p.toString(), false, implied);
136       p = new NamespacePermission("starksm/Project1/Documents/Folder1/readme.html", "rw--");
137       implied = pc.implies(p);
138       assertEquals(p.toString(), false, implied);
139       p = new NamespacePermission("Project1/Documents/Public/starksm/.bashrc", "r---");
140       implied = pc.implies(p);
141       assertEquals(p.toString(), false, implied);
142    }
143
144    public static void main(String JavaDoc[] args) throws Exception JavaDoc
145    {
146       NamespacePermissionsUnitTestCase tst = new NamespacePermissionsUnitTestCase("main");
147       tst.setUp();
148       tst.testImplied();
149       tst.testNotImplied();
150    }
151 }
152
Popular Tags