Home | History | Annotate | Download | only in module
      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 /**
     19  * @author Alexander V. Astapchuk
     20  */
     21 package org.apache.harmony.auth.module;
     22 
     23 import java.util.HashMap;
     24 import java.util.Map;
     25 
     26 import javax.security.auth.Subject;
     27 import javax.security.auth.callback.CallbackHandler;
     28 import javax.security.auth.callback.Callback;
     29 import javax.security.auth.login.LoginException;
     30 
     31 import junit.framework.TestCase;
     32 import org.apache.harmony.auth.module.UnixLoginModule;
     33 
     34 
     35 /**
     36  * Unit test for UnixLoginModule
     37  */
     38 @SuppressWarnings("unchecked")
     39 public class UnixLoginModuleTest extends TestCase {
     40 
     41     UnixLoginModule lm = new UnixLoginModule();
     42 
     43     @Override
     44     protected void setUp() throws Exception {
     45         Subject subj = new Subject();
     46         CallbackHandler cbh = new TestCallbackHandler();
     47         Map sharedState = new HashMap();
     48         Map options = new HashMap();
     49         lm.initialize(subj, cbh, sharedState, options);
     50     }
     51 
     52     private static class TestCallbackHandler implements CallbackHandler {
     53         public void handle(Callback[] cbs) {
     54             // does nothing
     55         }
     56     }
     57 
     58     /**
     59      * Test for UnixLoginModule.initialize()
     60      */
     61     public void testInitialize() {
     62         // Need new, non initialized instance of LoginModule
     63         lm = new UnixLoginModule();
     64 
     65         Map shared = new HashMap();
     66         Map options = new HashMap();
     67         CallbackHandler cbh = new TestCallbackHandler();
     68         // must not accept null for subject
     69         try {
     70             lm.initialize(null, cbh, shared, options);
     71             fail("must not pass here");
     72         } catch (NullPointerException _) {
     73             // gut
     74         }
     75         Subject subj = new Subject();
     76         // must accept null for handler
     77         lm.initialize(subj, null, shared, options);
     78         // must accept null for sharedState
     79         lm.initialize(subj, cbh, null, options);
     80         // must not accept null for options
     81         try {
     82             lm.initialize(subj, cbh, shared, null);
     83             fail("must not pass here");
     84         } catch (NullPointerException _) {
     85             // gut
     86         }
     87     }
     88 
     89     public void testAbort() throws  LoginException {
     90         lm.login();
     91         lm.abort();
     92     }
     93 
     94     public void testCommit() throws  LoginException {
     95         lm.login();
     96         lm.commit();
     97         lm.logout();
     98     }
     99 
    100     public void testLogin() throws  LoginException {
    101         lm.login();
    102         lm.abort();
    103     }
    104 
    105     public void testLogout() throws LoginException {
    106         lm.logout();
    107     }
    108 
    109 }
    110