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.jgss;
019:
020: import java.util.Arrays;
021:
022: import org.apache.harmony.auth.jgss.kerberos.KerberosUtils;
023: import org.ietf.jgss.GSSManager;
024: import org.ietf.jgss.GSSName;
025: import org.ietf.jgss.Oid;
026:
027: import junit.framework.TestCase;
028:
029: public class GSSManagerImplTest extends TestCase {
030:
031: private GSSManager gssManager;
032:
033: public void testGetMechs() throws Exception {
034: Oid[] mechs = gssManager.getMechs();
035: Oid kerberosMech = new Oid("1.2.840.113554.1.2.2");
036: Oid[] expectedMechs = new Oid[] { kerberosMech };
037: assertTrue(Arrays.equals(expectedMechs, mechs));
038: }
039:
040: public void testGetMechsForName() throws Exception {
041: Oid nameType = GSSName.NT_ANONYMOUS;
042: Oid[] mechs = gssManager.getMechsForName(nameType);
043: assertEquals(0, mechs.length);
044:
045: nameType = GSSName.NT_MACHINE_UID_NAME;
046: mechs = gssManager.getMechsForName(nameType);
047: assertEquals(0, mechs.length);
048:
049: nameType = GSSName.NT_STRING_UID_NAME;
050: mechs = gssManager.getMechsForName(nameType);
051: assertEquals(0, mechs.length);
052:
053: nameType = GSSName.NT_USER_NAME;
054: mechs = gssManager.getMechsForName(nameType);
055: Oid kerberosMech = new Oid("1.2.840.113554.1.2.2");
056: Oid[] expectedMechs = new Oid[] { kerberosMech };
057: assertTrue(Arrays.equals(expectedMechs, mechs));
058:
059: nameType = GSSName.NT_HOSTBASED_SERVICE;
060: mechs = gssManager.getMechsForName(nameType);
061: assertTrue(Arrays.equals(expectedMechs, mechs));
062:
063: nameType = GSSName.NT_EXPORT_NAME;
064: mechs = gssManager.getMechsForName(nameType);
065: assertTrue(Arrays.equals(expectedMechs, mechs));
066:
067: nameType = KerberosUtils.KRB5_PRINCIPAL_NAMETYPE;
068: mechs = gssManager.getMechsForName(nameType);
069: assertTrue(Arrays.equals(expectedMechs, mechs));
070: }
071:
072: public void testGetNamesForMech() throws Exception {
073: Oid kerberosMech = new Oid("1.2.840.113554.1.2.2");
074: Oid[] nameTypes = gssManager.getNamesForMech(kerberosMech);
075: Oid[] expectedNameTypes = new Oid[] { GSSName.NT_USER_NAME,
076: GSSName.NT_HOSTBASED_SERVICE, GSSName.NT_EXPORT_NAME,
077: KerberosUtils.KRB5_PRINCIPAL_NAMETYPE };
078: assertEquals(expectedNameTypes.length, nameTypes.length);
079: for (Oid expectedNameType : expectedNameTypes) {
080: boolean got = false;
081: for (Oid nameType : nameTypes) {
082: if (nameType.equals(expectedNameType)) {
083: got = true;
084: break;
085: }
086: }
087: if (!got) {
088: fail("Missing expected NameType " + expectedNameType);
089: }
090: }
091: }
092:
093: public void testCreateName() throws Exception {
094:
095: GSSName gssName = gssManager.createName("username",
096: GSSName.NT_USER_NAME);
097: assertEquals(GSSName.NT_USER_NAME, gssName.getStringNameType());
098:
099: gssName = gssManager.createName("service@host",
100: GSSName.NT_HOSTBASED_SERVICE);
101: assertEquals(GSSName.NT_HOSTBASED_SERVICE, gssName
102: .getStringNameType());
103:
104: final Oid kerberosPrincipalOid = new Oid(
105: "1.2.840.113554.1.2.2.1");
106: gssName = gssManager.createName("kerberosPrincipal",
107: kerberosPrincipalOid);
108: assertEquals(kerberosPrincipalOid, gssName.getStringNameType());
109:
110: byte[] encoded = new byte[] { 4, 1, 0, 11, 6, 9, 42, -122, 72,
111: -122, -9, 18, 1, 2, 2, 0, 0, 0, 17, 115, 101, 114, 118,
112: 105, 99, 101, 47, 108, 111, 99, 97, 108, 104, 111, 115,
113: 116 };
114: gssName = gssManager
115: .createName(encoded, GSSName.NT_EXPORT_NAME);
116: assertEquals(kerberosPrincipalOid, gssName.getStringNameType());
117: GSSName expectedGSSName = gssManager.createName(
118: "service/localhost", kerberosPrincipalOid);
119: assertEquals(expectedGSSName, gssName);
120: }
121:
122: public void setUp() throws Exception {
123: gssManager = GSSManager.getInstance();
124: }
125:
126: }
|