Source Code Cross Referenced for IdentityTest.java in  » Apache-Harmony-Java-SE » java-package » 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 » java package » 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 Aleksei Y. Semenov
020:         * @version $Revision$
021:         */package java.security;
022:
023:        import org.apache.harmony.security.tests.support.CertificateStub;
024:        import org.apache.harmony.security.tests.support.IdentityStub;
025:        import org.apache.harmony.security.tests.support.PublicKeyStub;
026:
027:        import junit.framework.TestCase;
028:
029:        /**
030:         * Tests for class Identity
031:         * 
032:         */
033:
034:        public class IdentityTest extends TestCase {
035:
036:            public static class MySecurityManager extends SecurityManager {
037:                public Permissions denied = new Permissions();
038:
039:                public void checkPermission(Permission permission) {
040:                    if (denied != null && denied.implies(permission))
041:                        throw new SecurityException();
042:                }
043:            }
044:
045:            public static void main(String[] args) {
046:                junit.textui.TestRunner.run(IdentityTest.class);
047:            }
048:
049:            /**
050:             * Constructor for IdentityTest.
051:             * @param name
052:             */
053:            public IdentityTest(String name) {
054:                super (name);
055:            }
056:
057:            public void testHashCode() {
058:                new IdentityStub("testHashCode").hashCode();
059:            }
060:
061:            public void testEquals() throws Exception {
062:                Identity i1 = new IdentityStub("testEquals");
063:                Object value[] = { null, Boolean.FALSE, new Object(),
064:                        Boolean.FALSE, i1, Boolean.TRUE,
065:                        new IdentityStub(i1.getName()), Boolean.TRUE };
066:
067:                for (int k = 0; k < value.length; k += 2) {
068:                    assertEquals(value[k + 1], new Boolean(i1.equals(value[k])));
069:                    if (Boolean.TRUE.equals(value[k + 1]))
070:                        assertEquals(i1.hashCode(), value[k].hashCode());
071:                }
072:                // check other cases
073:                Identity i2 = new IdentityStub("testEquals", IdentityScope
074:                        .getSystemScope());
075:                assertEquals(i1.identityEquals(i2), i1.equals(i2));
076:                Identity i3 = new IdentityStub("testEquals3");
077:                assertEquals(i1.identityEquals(i3), i1.equals(i3));
078:
079:            }
080:
081:            /**
082:             * verify Identity.toString() throws Exception is permission is denied
083:             */
084:            public void testToString1() {
085:                MySecurityManager sm = new MySecurityManager();
086:                sm.denied.add(new SecurityPermission("printIdentity"));
087:                System.setSecurityManager(sm);
088:                try {
089:                    new IdentityStub("testToString").toString();
090:                    fail("SecurityException should be thrown");
091:                } catch (SecurityException ok) {
092:                } finally {
093:                    System.setSecurityManager(null);
094:                }
095:            }
096:
097:            /**
098:             * verify Identity.toString() 
099:             */
100:            public void testToString2() {
101:                assertNotNull(new IdentityStub("testToString2").toString());
102:            }
103:
104:            /**
105:             * verify Identity() creates instance
106:             */
107:            public void testIdentity() {
108:                assertNotNull(new IdentityStub());
109:            }
110:
111:            /*
112:             * verify Identity(String) creates instance with given name
113:             */
114:            public void testIdentityString() {
115:                Identity i = new IdentityStub("iii");
116:                assertNotNull(i);
117:                assertEquals("iii", i.getName());
118:                i = new IdentityStub(null);
119:                assertNotNull(i);
120:                assertNull(i.getName());
121:            }
122:
123:            /**
124:             * verify Identity(String, IdentityScope) creates instance with given name and in give scope
125:             */
126:            public void testIdentityStringIdentityScope() throws Exception {
127:                IdentityScope s = IdentityScope.getSystemScope();
128:                Identity i = new IdentityStub("iii2", s);
129:                assertNotNull(i);
130:                assertEquals("iii2", i.getName());
131:                assertSame(s, i.getScope());
132:                assertSame(i, s.getIdentity(i.getName()));
133:            }
134:
135:            /**
136:             * verify addCertificate(Certificate certificate) adds a certificate for this identity.
137:             * If the identity has a public key, the public key in the certificate must be the same
138:             *  
139:             */
140:            public void testAddCertificate1() throws Exception {
141:                Identity i = new IdentityStub("iii");
142:                PublicKeyStub pk1 = new PublicKeyStub("kkk", "fff", new byte[] {
143:                        1, 2, 3, 4, 5 });
144:                i.setPublicKey(pk1);
145:                // try with the same key
146:                CertificateStub c1 = new CertificateStub("fff", null, null, pk1);
147:                i.addCertificate(c1);
148:                assertSame(c1, i.certificates()[0]);
149:                // try Certificate with different key
150:                try {
151:                    i.addCertificate(new CertificateStub("ccc", null, null,
152:                            new PublicKeyStub("k2", "fff", new byte[] { 6, 7,
153:                                    8, 9, 0 })));
154:                    fail("KeyManagementException should be thrown");
155:                } catch (KeyManagementException ok) {
156:                }
157:            }
158:
159:            /**
160:             * verify addCertificate(Certificate certificate) adds a certificate for this identity.
161:             * if the identity does not have a public key, the identity's public key is set to be that specified in the certificate.
162:             */
163:            public void testAddCertificate2() throws Exception {
164:                Identity i = new IdentityStub("iii");
165:                PublicKeyStub pk1 = new PublicKeyStub("kkk", "fff", null);
166:                CertificateStub c1 = new CertificateStub("fff", null, null, pk1);
167:                i.addCertificate(c1);
168:                assertSame(c1, i.certificates()[0]);
169:                assertSame(pk1, i.getPublicKey());
170:
171:            }
172:
173:            /**
174:             * verify addCertificate(Certificate certificate) throws SecurityException is permission is denied
175:             */
176:            public void testAddCertificate3() throws Exception {
177:                MySecurityManager sm = new MySecurityManager();
178:                sm.denied.add(new SecurityPermission("addIdentityCertificate"));
179:                System.setSecurityManager(sm);
180:                try {
181:                    new IdentityStub("iii").addCertificate(new CertificateStub(
182:                            "ccc", null, null, null));
183:                    fail("SecurityException should be thrown");
184:                } catch (SecurityException ok) {
185:                } finally {
186:                    System.setSecurityManager(null);
187:                }
188:            }
189:
190:            /**
191:             * verify addCertificate(Certificate certificate) throws KeyManagementException if certificate is null
192:             */
193:            public void testAddCertificate4() throws Exception {
194:                try {
195:                    new IdentityStub("aaa").addCertificate(null);
196:                    fail("KeyManagementException should be thrown");
197:                } catch (KeyManagementException ok) {
198:                } catch (NullPointerException ok) {
199:                }
200:
201:            }
202:
203:            //
204:            //  Commented out since there will no be fix for the test failure    
205:            //    /**
206:            //     * verify removeCertificate(Certificate certificate) removes certificate
207:            //     */
208:            //    public void testRemoveCertificate1() throws Exception{
209:            //        Identity i = new IdentityStub("iii");
210:            //        PublicKeyStub pk1 = new PublicKeyStub("kkk", "fff", null);        
211:            //        CertificateStub c1 = new CertificateStub("fff", null, null, pk1);
212:            //        i.addCertificate(c1);
213:            //        assertSame(c1, i.certificates()[0]);
214:            //        i.removeCertificate(c1);
215:            //        assertEquals(0, i.certificates().length);
216:            //        // throw KeyManagementException if certificate not found
217:            //        try {
218:            //            i.removeCertificate(c1);
219:            //            fail("KeyManagementException should be thrown");
220:            //        } catch (KeyManagementException ok) {     
221:            //        }
222:            //        try {
223:            //            i.removeCertificate(null);
224:            //            fail("KeyManagementException should be thrown");
225:            //        } catch (KeyManagementException ok) {
226:            //            
227:            //        }
228:            //    }
229:            /**
230:             * verify removeCertificate(Certificate certificate) throws SecurityException if permission is denied
231:             */
232:            public void testRemoveCertificate2() throws Exception {
233:                MySecurityManager sm = new MySecurityManager();
234:                sm.denied.add(new SecurityPermission(
235:                        "removeIdentityCertificate"));
236:                Identity i = new IdentityStub("iii");
237:                i.addCertificate(new CertificateStub("ccc", null, null, null));
238:                System.setSecurityManager(sm);
239:                try {
240:                    i.removeCertificate(i.certificates()[0]);
241:                    fail("SecurityException should be thrown");
242:                } catch (SecurityException ok) {
243:                } finally {
244:                    System.setSecurityManager(null);
245:                }
246:
247:            }
248:
249:            /**
250:             * verify certificates() returns a copy of all certificates for this identity
251:             */
252:            public void testCertificates() throws Exception {
253:                Identity i = new IdentityStub("iii");
254:                PublicKeyStub pk1 = new PublicKeyStub("kkk", "fff", null);
255:                CertificateStub c1 = new CertificateStub("fff", null, null, pk1);
256:                CertificateStub c2 = new CertificateStub("zzz", null, null, pk1);
257:                i.addCertificate(c1);
258:                i.addCertificate(c2);
259:                Certificate[] s = i.certificates();
260:                assertEquals(2, s.length);
261:                assertTrue(c1.equals(s[0]) || c2.equals(s[0]));
262:                assertTrue(c1.equals(s[1]) || c2.equals(s[1]));
263:                s[0] = null;
264:                s[1] = null;
265:                // check that the copy was modified
266:                s = i.certificates();
267:                assertEquals(2, s.length);
268:                assertTrue(c1.equals(s[0]) || c2.equals(s[0]));
269:                assertTrue(c1.equals(s[1]) || c2.equals(s[1]));
270:            }
271:
272:            /**
273:             * verify Identity.identityEquals(Identity) return true, only if names and public keys are equal 
274:             */
275:
276:            public void testIdentityEquals() throws Exception {
277:                String name = "nnn";
278:                PublicKey pk = new PublicKeyStub("aaa", "fff", new byte[] { 1,
279:                        2, 3, 4, 5 });
280:                Identity i = new IdentityStub(name);
281:                i.setPublicKey(pk);
282:                Object[] value = {
283:                        //null, Boolean.FALSE,
284:                        //new Object(), Boolean.FALSE,
285:                        new IdentityStub("111"), Boolean.FALSE,
286:                        new IdentityStub(name), Boolean.FALSE,
287:                        new IdentityStub(name, IdentityScope.getSystemScope()),
288:                        Boolean.FALSE, i, Boolean.TRUE,
289:                        new IdentityStub(name, pk), Boolean.TRUE };
290:                for (int k = 0; k < value.length; k += 2) {
291:                    assertEquals(value[k + 1], new Boolean(i
292:                            .identityEquals((Identity) value[k])));
293:                    if (Boolean.TRUE.equals(value[k + 1]))
294:                        assertEquals(i.hashCode(), value[k].hashCode());
295:                }
296:                Identity i2 = IdentityScope.getSystemScope().getIdentity(name);
297:                i2.setPublicKey(pk);
298:                assertTrue(i.identityEquals(i2));
299:            }
300:
301:            /**
302:             * verify Identity.toString(boolean) return string representation of identity
303:             */
304:            public void testToStringboolean() throws Exception {
305:                new IdentityStub("aaa").toString(false);
306:                new IdentityStub("aaa2", IdentityScope.getSystemScope())
307:                        .toString(false);
308:                new IdentityStub("bbb").toString(true);
309:                new IdentityStub("bbb2", IdentityScope.getSystemScope())
310:                        .toString(true);
311:            }
312:
313:            /**
314:             * verify Identity.getScope() returns identity's scope
315:             */
316:            public void testGetScope() throws Exception {
317:                Identity i = new IdentityStub("testGetScope");
318:                assertNull(i.getScope());
319:                IdentityScope s = IdentityScope.getSystemScope();
320:
321:                Identity i2 = new IdentityStub("testGetScope2", s);
322:                assertSame(s, i2.getScope());
323:
324:            }
325:
326:            /**
327:             * 
328:             * verify Identity.setPublicKey() throws SecurityException if permission is denied
329:             *
330:             */
331:            public void testSetPublicKey1() throws Exception {
332:                MySecurityManager sm = new MySecurityManager();
333:                sm.denied.add(new SecurityPermission("setIdentityPublicKey"));
334:                System.setSecurityManager(sm);
335:                try {
336:                    new IdentityStub("testSetPublicKey1")
337:                            .setPublicKey(new PublicKeyStub("kkk",
338:                                    "testSetPublicKey1", null));
339:                    fail("SecurityException should be thrown");
340:                } catch (SecurityException ok) {
341:                } finally {
342:                    System.setSecurityManager(null);
343:                }
344:
345:            }
346:
347:            /**
348:             * 
349:             * verify Identity.setPublicKey() throws KeyManagementException if key is invalid 
350:             *
351:             */
352:            public void testSetPublicKey2() throws Exception {
353:                Identity i2 = new IdentityStub("testSetPublicKey2_2",
354:                        IdentityScope.getSystemScope());
355:                new PublicKeyStub("kkk", "testSetPublicKey2", new byte[] { 1,
356:                        2, 3, 4, 5 });
357:                try {
358:                    i2.setPublicKey(null);
359:                    //fail("KeyManagementException should be thrown - key is null");            
360:                } catch (KeyManagementException ok) {
361:                }
362:            }
363:
364:            //
365:            //  Commented out since there will no be fix for the test failure       
366:            //    /**
367:            //     * 
368:            //     * verify Identity.setPublicKey() throws KeyManagementException if key is already used 
369:            //     *
370:            //     */
371:            //    public void testSetPublicKey3() throws Exception {
372:            //        Identity i1 = new IdentityStub("testSetPublicKey3_1", IdentityScope.getSystemScope());
373:            //        Identity i2 = new IdentityStub("testSetPublicKey3_2", IdentityScope.getSystemScope());
374:            //        PublicKey pk = new PublicKeyStub("kkk", "fff", new byte[]{1,2,3,4,5});
375:            //        i1.setPublicKey(pk);
376:            //        try {
377:            //            i2.setPublicKey(pk);
378:            //            fail("KeyManagementException should be thrown - key already used");            
379:            //        } catch (KeyManagementException ok) {};
380:            //    }
381:            /**
382:             * 
383:             * verify Identity.setPublicKey()  removes old key and all identity's certificates
384:             *
385:             */
386:            public void testSetPublicKey4() throws Exception {
387:                Identity i = new IdentityStub("testSetPublicKey4");
388:                PublicKeyStub pk1 = new PublicKeyStub("kkk",
389:                        "Identity.testSetPublicKey4", null);
390:                CertificateStub c1 = new CertificateStub("fff", null, null, pk1);
391:                CertificateStub c2 = new CertificateStub("zzz", null, null, pk1);
392:                i.addCertificate(c1);
393:                i.addCertificate(c2);
394:                assertEquals(2, i.certificates().length);
395:                assertSame(pk1, i.getPublicKey());
396:
397:                PublicKeyStub pk2 = new PublicKeyStub("zzz",
398:                        "Identity.testSetPublicKey4", null);
399:                i.setPublicKey(pk2);
400:                assertSame(pk2, i.getPublicKey());
401:                assertEquals(0, i.certificates().length);
402:            }
403:
404:            /**
405:             * verify Identity.getPublicKey() returns public key
406:             */
407:
408:            public void testGetPublicKey() throws Exception {
409:                Identity i = new IdentityStub("testGetPublicKey");
410:                assertNull(i.getPublicKey());
411:                PublicKey pk = new PublicKeyStub("kkk",
412:                        "Identity.testGetPublicKey", null);
413:                i.setPublicKey(pk);
414:                assertSame(pk, i.getPublicKey());
415:            }
416:
417:            /**
418:             * 
419:             * verify Identity.setInfo() throws SecurityException if permission is denied
420:             *
421:             *
422:             */
423:            public void testSetInfo() throws Exception {
424:                MySecurityManager sm = new MySecurityManager();
425:                sm.denied.add(new SecurityPermission("setIdentityInfo"));
426:                System.setSecurityManager(sm);
427:                try {
428:                    new IdentityStub("testSetInfo").setInfo("some info");
429:                    fail("SecurityException should be thrown");
430:                } catch (SecurityException ok) {
431:                } finally {
432:                    System.setSecurityManager(null);
433:                }
434:            }
435:
436:            public void testGetInfo() {
437:
438:                Identity i = new IdentityStub("testGetInfo");
439:                i.setInfo("some info");
440:                assertEquals("some info", i.getInfo());
441:            }
442:
443:            public void testGetName() {
444:                Identity i = new IdentityStub("testGetName");
445:                assertEquals("testGetName", i.getName());
446:            }
447:
448:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.