Source Code Cross Referenced for KerberosPrincipalTest.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » auth » tests » javax » security » auth » kerberos » 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.auth.tests.javax.security.auth.kerberos 
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:        package org.apache.harmony.auth.tests.javax.security.auth.kerberos;
019:
020:        import java.io.File;
021:        import java.io.FileOutputStream;
022:
023:        import javax.security.auth.kerberos.KerberosPrincipal;
024:
025:        import junit.framework.TestCase;
026:
027:        import org.apache.harmony.auth.tests.support.TestUtils;
028:
029:        import tests.support.Support_Exec;
030:
031:        /**
032:         * Tests KerberosPrincipal class implementation.
033:         */
034:        public class KerberosPrincipalTest extends TestCase {
035:
036:            // system property for specifying the default realm
037:            private static final String KRB5_REALM_SYS_PROP = "java.security.krb5.realm";
038:
039:            // system property for specifying the default kdc
040:            private static final String KRB5_KDC_SYS_PROP = "java.security.krb5.kdc";
041:
042:            // system property for specifying the default config file
043:            private static final String KRB5_CONF_SYS_PROP = "java.security.krb5.conf";
044:
045:            /**
046:             * @tests javax.security.auth.kerberos.KerberosPrincipal#KerberosPrincipal(
047:             *        java.lang.String)
048:             */
049:            public void test_Ctor1() {
050:
051:                // null value is invalid
052:                try {
053:                    new KerberosPrincipal(null);
054:                    fail("No expected IllegalArgumentException for null");
055:                } catch (IllegalArgumentException e) {
056:                }
057:
058:                // testing illegal kerberos principal names
059:                String[] illegalNames = new String[] { "bbb@a:a.com", // ':' char
060:                        "bbb@a/a.com", // '/' char
061:                        "bbb@a\0a.com",// null char
062:                        "@/" // Regression for HARMONY-770
063:                };
064:                for (String illegalName : illegalNames) {
065:                    try {
066:                        new KerberosPrincipal(illegalName);
067:
068:                        fail("No expected IllegalArgumentException for: "
069:                                + illegalName);
070:                    } catch (IllegalArgumentException e) {
071:                    }
072:                }
073:
074:                // valid values
075:                KerberosPrincipal principal = new KerberosPrincipal(
076:                        "name@apache.org");
077:
078:                assertEquals("name@apache.org", principal.getName());
079:                assertEquals("apache.org", principal.getRealm());
080:                assertEquals(KerberosPrincipal.KRB_NT_PRINCIPAL, principal
081:                        .getNameType());
082:
083:                // Regression for HARMONY-1182
084:                principal = new KerberosPrincipal("file:C://@apache.org");
085:                assertEquals("file:C:@apache.org", principal.getName());
086:            }
087:
088:            /**
089:             * @tests javax.security.auth.kerberos.KerberosPrincipal#KerberosPrincipal(
090:             *        java.lang.String, int)
091:             */
092:            public void test_Ctor2() {
093:
094:                // null value is invalid
095:                try {
096:                    new KerberosPrincipal(null,
097:                            KerberosPrincipal.KRB_NT_UNKNOWN);
098:                    fail("No expected IllegalArgumentException for null");
099:                } catch (IllegalArgumentException e) {
100:                }
101:
102:                // '-1' nameType value is invalid
103:                try {
104:                    new KerberosPrincipal("name@apache.org", -1);
105:                    fail("No expected IllegalArgumentException for -1 nameType value");
106:                } catch (IllegalArgumentException e) {
107:                }
108:
109:                // '6' nameType value is invalid
110:                try {
111:                    new KerberosPrincipal("name@apache.org", 6);
112:                    fail("No expected IllegalArgumentException 6 nameType value");
113:                } catch (IllegalArgumentException e) {
114:                }
115:
116:                // valid values
117:                KerberosPrincipal principal = new KerberosPrincipal(
118:                        "name@apache.org", KerberosPrincipal.KRB_NT_UNKNOWN);
119:
120:                assertEquals("name@apache.org", principal.getName());
121:                assertEquals("apache.org", principal.getRealm());
122:                assertEquals(KerberosPrincipal.KRB_NT_UNKNOWN, principal
123:                        .getNameType());
124:            }
125:
126:            /**
127:             * @tests javax.security.auth.kerberos.KerberosPrincipal#KerberosPrincipal(
128:             *        java.lang.String)
129:             */
130:            public void test_Ctor_defaultRealm() throws Exception {
131:
132:                // test: if the input name has no realm part then default realm is used.
133:                //
134:                // the test forks a separate VM for each case because   
135:                // default realm value is cashed
136:
137:                // test: default realm is unset (has null value)
138:                Support_Exec
139:                        .execJava(new String[] { DefaultRealm_NullValue.class
140:                                .getName() }, null, true);
141:
142:                // test: default realm name is specified via system property
143:                Support_Exec.execJava(
144:                        new String[] { DefaultRealm_SystemProperty.class
145:                                .getName() }, null, true);
146:
147:                // test: default realm name is specified in config file
148:                Support_Exec
149:                        .execJava(new String[] { DefaultRealm_ConfigFile.class
150:                                .getName() }, null, true);
151:            }
152:
153:            /**
154:             * Test: default realm is unset
155:             */
156:            public static class DefaultRealm_NullValue {
157:
158:                // Regression for HARMONY-1090
159:                public static void main(String[] av) throws Exception {
160:
161:                    // clear system properties
162:                    TestUtils.setSystemProperty(KRB5_REALM_SYS_PROP, null);
163:                    TestUtils.setSystemProperty(KRB5_KDC_SYS_PROP, null);
164:
165:                    // point to empty config file
166:                    File f = File.createTempFile("krb5", "conf");
167:                    f.deleteOnExit();
168:                    TestUtils.setSystemProperty(KRB5_CONF_SYS_PROP, f
169:                            .getCanonicalPath());
170:
171:                    // test: default realm value is 'null'
172:                    KerberosPrincipal principal = new KerberosPrincipal("name");
173:                    assertEquals("name", principal.getName());
174:                    assertNull(principal.getRealm());
175:                }
176:            }
177:
178:            /**
179:             * Tests: default realm name is specified via system property
180:             */
181:            public static class DefaultRealm_SystemProperty {
182:                public static void main(String[] av) {
183:
184:                    // case 1: unset 'kdc' and set 'realm'
185:                    TestUtils.setSystemProperty(KRB5_REALM_SYS_PROP,
186:                            "some_value");
187:                    TestUtils.setSystemProperty(KRB5_KDC_SYS_PROP, null);
188:                    try {
189:                        new KerberosPrincipal("name");
190:                        fail("No expected IllegalArgumentException");
191:                    } catch (IllegalArgumentException e) {
192:                    } finally {
193:                        TestUtils.setSystemProperty(KRB5_REALM_SYS_PROP, null);
194:                    }
195:
196:                    // case 2: set 'kdc' and unset 'realm' sys.props
197:                    TestUtils
198:                            .setSystemProperty(KRB5_KDC_SYS_PROP, "some_value");
199:                    try {
200:                        new KerberosPrincipal("name");
201:                        fail("No expected IllegalArgumentException");
202:                    } catch (IllegalArgumentException e) {
203:                    } finally {
204:                        TestUtils.setSystemProperty(KRB5_KDC_SYS_PROP, null);
205:                    }
206:
207:                    String testRealm = "This_is_test_realm";
208:
209:                    System.setProperty(KRB5_REALM_SYS_PROP, testRealm);
210:                    System.setProperty(KRB5_KDC_SYS_PROP, "some_value");
211:
212:                    // test
213:                    KerberosPrincipal principal = new KerberosPrincipal("name");
214:                    assertEquals("name@" + testRealm, principal.getName());
215:                    assertEquals(testRealm, principal.getRealm());
216:
217:                    // test: default realm value is cashed 
218:                    // change system property value 
219:                    System.setProperty(KRB5_REALM_SYS_PROP,
220:                            "Another_test_realm");
221:                    principal = new KerberosPrincipal("name");
222:                    assertEquals("name@" + testRealm, principal.getName());
223:                    assertEquals(testRealm, principal.getRealm());
224:                }
225:            }
226:
227:            /**
228:             * Tests: default realm name is specified in config file
229:             */
230:            public static class DefaultRealm_ConfigFile {
231:                public static void main(String[] av) throws Exception {
232:
233:                    // clear system properties
234:                    TestUtils.setSystemProperty(KRB5_REALM_SYS_PROP, null);
235:                    TestUtils.setSystemProperty(KRB5_KDC_SYS_PROP, null);
236:
237:                    // point to config file
238:                    File f = File.createTempFile("krb5", "conf");
239:                    f.deleteOnExit();
240:                    FileOutputStream out = new FileOutputStream(f);
241:                    out.write("[libdefaults]\n".getBytes());
242:                    out.write("         default_realm = MY.TEST_REALM"
243:                            .getBytes());
244:                    out.close();
245:
246:                    TestUtils.setSystemProperty(KRB5_CONF_SYS_PROP, f
247:                            .getCanonicalPath());
248:
249:                    // test
250:                    KerberosPrincipal principal = new KerberosPrincipal("name");
251:                    assertEquals("name@MY.TEST_REALM", principal.getName());
252:                    assertEquals("MY.TEST_REALM", principal.getRealm());
253:                }
254:            }
255:
256:            /**
257:             * @tests javax.security.auth.kerberos.KerberosPrincipal#hashCode()
258:             */
259:            public void test_hashCode() {
260:                KerberosPrincipal principal = new KerberosPrincipal(
261:                        "name@apache.org");
262:
263:                assertEquals(principal.getName().hashCode(), principal
264:                        .hashCode());
265:            }
266:
267:            /**
268:             * @tests javax.security.auth.kerberos.KerberosPrincipal#toString()
269:             */
270:            public void test_toString() {
271:
272:                String name = "name@apache.org";
273:
274:                KerberosPrincipal principal = new KerberosPrincipal(name);
275:
276:                assertEquals(name, principal.toString());
277:            }
278:
279:            /**
280:             * @tests javax.security.auth.kerberos.KerberosPrincipal#equals(Object)
281:             */
282:            public void test_equals() {
283:
284:                KerberosPrincipal p = new KerberosPrincipal("A@B");
285:
286:                assertTrue(p.equals(new KerberosPrincipal("A@B")));
287:                assertFalse(p.equals(new KerberosPrincipal("A@B.org")));
288:                assertFalse(p.equals(new KerberosPrincipal("aaa@B")));
289:                assertFalse(p.equals(new KerberosPrincipal("A@B",
290:                        KerberosPrincipal.KRB_NT_UID)));
291:
292:                // Regression for HARMONY-744
293:                assertFalse(p.equals(null));
294:                assertFalse(p.equals(new Object()));
295:            }
296:        }
w__w__w___.___j___av_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.