Home | History | Annotate | Download | only in kerberos
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 package org.apache.harmony.auth.tests.jgss.kerberos;
     19 
     20 import java.util.Date;
     21 import java.security.PrivilegedAction;
     22 
     23 import javax.security.auth.Subject;
     24 import javax.security.auth.kerberos.KerberosPrincipal;
     25 import javax.security.auth.kerberos.KerberosTicket;
     26 
     27 import org.apache.harmony.auth.jgss.kerberos.KerberosUtils;
     28 import org.apache.harmony.auth.jgss.kerberos.toolbox.KerberosToolboxImpl;
     29 import org.apache.harmony.auth.jgss.kerberos.toolbox.KerberosToolboxSpi;
     30 
     31 import junit.framework.TestCase;
     32 
     33 public class KerberosUtilsTest extends TestCase {
     34 
     35     public void testGetKerberosToolBox() throws Exception {
     36         KerberosToolboxSpi kerberosToolBoxSpi = KerberosUtils
     37                 .getKerberosToolbox("TESTKDCNAME");
     38         assertTrue(kerberosToolBoxSpi instanceof KerberosToolboxImpl);
     39     }
     40 
     41     public void testGetTGT_fromContext() throws Exception {
     42         final KerberosPrincipal clientPrincipal = new KerberosPrincipal(
     43                 "leo (at) EXAMPLE.COM");
     44         final KerberosPrincipal serverPrincipal = new KerberosPrincipal(
     45                 "krbtgt/EXAMPLE.COM@EXAMPLE.COM");
     46         KerberosTicket tgt = new KerberosTicket(new byte[0], clientPrincipal,
     47                 serverPrincipal, new byte[0], 1, new boolean[0],
     48                 new Date(1000), null, new Date(new Date().getTime() + 1000), null, null);
     49         Subject subject = new Subject();
     50         subject.getPrivateCredentials().add(tgt);
     51         KerberosTicket tgtFromContext = (KerberosTicket) Subject.doAs(subject, new PrivilegedAction<KerberosTicket>(){
     52             public KerberosTicket run(){
     53                 return KerberosUtils.getTGT(clientPrincipal);
     54             }
     55         });
     56         assertNotNull(tgtFromContext);
     57         assertEquals(tgt, tgtFromContext);
     58     }
     59 
     60     public void testGetTGT_fromLoginContext() throws Exception {
     61         final KerberosPrincipal clientPrincipal = new KerberosPrincipal(
     62                 "leo (at) EXAMPLE.COM");
     63         KerberosTicket tgt = KerberosUtils.getTGT(clientPrincipal);
     64         assertNull(tgt);
     65     }
     66 }
     67