Source Code Cross Referenced for ProtectionDomainTest.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » security » tests » java » security » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » org package » org.apache.harmony.security.tests.java.security 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        /**
019:         * @author Alexander V. Astapchuk
020:         * @version $Revision$
021:         */package org.apache.harmony.security.tests.java.security;
022:
023:        import java.security.*;
024:        import java.net.URL;
025:        import java.net.MalformedURLException;
026:        import java.net.URLClassLoader;
027:
028:        import junit.framework.TestCase;
029:
030:        /**
031:         * Unit tests for java.security.ProtectionDomain.
032:         * 
033:         */
034:
035:        public class ProtectionDomainTest extends TestCase {
036:
037:            /**
038:             * Entry point for standalone runs.
039:             * @param args command line arguments
040:             */
041:            public static void main(String[] args) {
042:                junit.textui.TestRunner.run(ProtectionDomainTest.class);
043:            }
044:
045:            private final AllPermission allperm = new AllPermission();
046:
047:            private URL url = null;
048:
049:            private CodeSource cs = null;
050:
051:            private PermissionCollection perms = null;
052:
053:            private ClassLoader classldr = null;
054:
055:            private Principal[] principals = null; // changed in setUp()
056:
057:            /*
058:             * @see TestCase#setUp()
059:             */
060:            protected void setUp() throws Exception {
061:                super .setUp();
062:                try {
063:                    url = new URL("http://localhost");
064:                } catch (MalformedURLException ex) {
065:                    throw new Error(ex);
066:                }
067:                cs = new CodeSource(url,
068:                        (java.security.cert.Certificate[]) null);
069:                perms = allperm.newPermissionCollection();
070:                perms.add(allperm);
071:                classldr = URLClassLoader.newInstance(new URL[] { url });
072:                principals = new Principal[] { new TestPrincipal("0"),
073:                        new TestPrincipal("1"), new TestPrincipal("2"),
074:                        new TestPrincipal("3"), new TestPrincipal("4"), };
075:            }
076:
077:            /**
078:             * Class under test for void ProtectionDomain(CodeSource,
079:             * PermissionCollection)
080:             */
081:            public void testProtectionDomainCodeSourcePermissionCollection_00() {
082:                new ProtectionDomain(null, null);
083:                new ProtectionDomain(cs, null);
084:
085:                new ProtectionDomain(cs, perms);
086:            }
087:
088:            /**
089:             * the ctor must set the PermissionCollection read-only
090:             */
091:            public void testProtectionDomainCodeSourcePermissionCollection_01() {
092:                assertFalse(perms.isReadOnly());
093:                new ProtectionDomain(null, perms);
094:                assertTrue(perms.isReadOnly());
095:            }
096:
097:            /**
098:             * Test for ProtectionDomain(CodeSource, PermissionCollection, ClassLoader, Principal[])
099:             */
100:            public void testProtectionDomainCodeSourcePermissionCollectionClassLoaderPrincipalArray() {
101:                new ProtectionDomain(null, null, null, null);
102:
103:                new ProtectionDomain(cs, null, null, null);
104:                new ProtectionDomain(null, perms, null, null);
105:                new ProtectionDomain(null, null, classldr, null);
106:                new ProtectionDomain(null, null, null, principals);
107:
108:                new ProtectionDomain(cs, perms, classldr, principals);
109:            }
110:
111:            /**
112:             * Tests for ProtectionDomain.getClassLoader()
113:             */
114:            public void testGetClassLoader() {
115:                assertNull(new ProtectionDomain(null, null).getClassLoader());
116:                assertSame(new ProtectionDomain(null, null, classldr, null)
117:                        .getClassLoader(), classldr);
118:            }
119:
120:            /**
121:             * Tests for ProtectionDomain.getCodeSource()
122:             */
123:            public void testGetCodeSource() {
124:                assertNull(new ProtectionDomain(null, null).getCodeSource());
125:                assertSame(new ProtectionDomain(cs, null).getCodeSource(), cs);
126:            }
127:
128:            /**
129:             * Tests for ProtectionDomain.getPermissions()
130:             */
131:            public void testGetPermissions() {
132:                assertNull(new ProtectionDomain(null, null).getPermissions());
133:                assertSame(new ProtectionDomain(null, perms).getPermissions(),
134:                        perms);
135:            }
136:
137:            /**
138:             * getPrincipals() always returns non null array
139:             */
140:            public void testGetPrincipals_00() {
141:                assertNotNull(new ProtectionDomain(null, null).getPrincipals());
142:            }
143:
144:            /**
145:             * getPrincipals() returns new array each time it's called
146:             */
147:            public void testGetPrincipals_01() {
148:                ProtectionDomain pd = new ProtectionDomain(null, null, null,
149:                        principals);
150:                Principal[] got = pd.getPrincipals();
151:                assertNotNull(got);
152:                assertNotSame(got, principals);
153:                assertNotSame(got, pd.getPrincipals());
154:                assertTrue(got.length == principals.length);
155:            }
156:
157:            /**
158:             * ProtectionDomain with null Permissions must not imply() permissions.
159:             */
160:            public void testImplies_00() {
161:                assertFalse(new ProtectionDomain(null, null).implies(allperm));
162:            }
163:
164:            /**
165:             * ProtectionDomain with PermissionCollection which contains AllPermission
166:             * must imply() AllPermission.
167:             */
168:            public void testImplies_01() {
169:                assertTrue(new ProtectionDomain(null, perms).implies(allperm));
170:            }
171:
172:            /**
173:             * ProtectionDomain created with a static set of permissions must not query 
174:             * policy. 
175:             */
176:            public void testImplies_02() {
177:                TestPolicy policy = new TestPolicy();
178:                // null set of permissions [must] force the PD to use Policy - for 
179:                // dynamic permissions
180:                ProtectionDomain pd = new ProtectionDomain(cs, null);
181:                policy.setTrackPD(pd);
182:                try {
183:                    Policy.setPolicy(policy);
184:                    pd.implies(allperm);
185:                } finally {
186:                    Policy.setPolicy(null);
187:                }
188:                assertFalse(policy.getPdTracked());
189:            }
190:
191:            /**
192:             * ProtectionDomain created with dynamic set of permissions must query 
193:             * policy. 
194:             */
195:            public void testImplies_03() {
196:                TestPolicy policy = new TestPolicy();
197:                ProtectionDomain pd = new ProtectionDomain(cs, null,
198:                        ClassLoader.getSystemClassLoader(), principals);
199:                policy.setTrackPD(pd);
200:                try {
201:                    Policy.setPolicy(policy);
202:                    pd.implies(allperm);
203:                } finally {
204:                    Policy.setPolicy(null);
205:                }
206:                assertTrue(policy.getPdTracked());
207:            }
208:
209:            /**
210:             * Simply checks that it's working somehow
211:             */
212:            public void testToString() {
213:                new ProtectionDomain(null, null).toString();
214:                new ProtectionDomain(cs, perms).toString();
215:                new ProtectionDomain(null, null, null, null).toString();
216:                new ProtectionDomain(cs, perms, classldr, principals)
217:                        .toString();
218:            }
219:
220:            /**
221:             * Test principal used during the testing. Does nothing.
222:             */
223:
224:            private static class TestPrincipal implements  Principal {
225:                private String name;
226:
227:                TestPrincipal(String name) {
228:                    this .name = name;
229:                }
230:
231:                public String getName() {
232:                    return "TestPrincipal: " + name;
233:                }
234:            }
235:
236:            private static class TestPolicy extends Policy {
237:                ProtectionDomain trackPD = null;
238:
239:                boolean pdTracked = false;
240:
241:                ProtectionDomain setTrackPD(ProtectionDomain pd) {
242:                    ProtectionDomain tmp = trackPD;
243:                    trackPD = pd;
244:                    pdTracked = false;
245:                    return tmp;
246:                }
247:
248:                boolean getPdTracked() {
249:                    return pdTracked;
250:                }
251:
252:                public PermissionCollection getPermissions(CodeSource cs) {
253:                    return new Permissions();
254:                }
255:
256:                //        public PermissionCollection getPermissions(ProtectionDomain domain) {
257:                //            return super.getPermissions(domain);
258:                //        }
259:                public boolean implies(ProtectionDomain domain,
260:                        Permission permission) {
261:                    if (trackPD != null && trackPD == domain) {
262:                        pdTracked = true;
263:                    }
264:                    return super .implies(domain, permission);
265:                }
266:
267:                public void refresh() {
268:                    // do nothing
269:                }
270:            }
271:
272:        }
w__w___w__._j__a___v_a__2__s___.__c___o___m__ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.