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: }
|