1561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes/*
2561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  Licensed to the Apache Software Foundation (ASF) under one or more
3561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  contributor license agreements.  See the NOTICE file distributed with
4561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  this work for additional information regarding copyright ownership.
5561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  The ASF licenses this file to You under the Apache License, Version 2.0
6561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  (the "License"); you may not use this file except in compliance with
7561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  the License.  You may obtain a copy of the License at
8561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *
9561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *     http://www.apache.org/licenses/LICENSE-2.0
10561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *
11561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  Unless required by applicable law or agreed to in writing, software
12561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  distributed under the License is distributed on an "AS IS" BASIS,
13561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  See the License for the specific language governing permissions and
15561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  limitations under the License.
16561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes */
17561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
18561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughespackage org.apache.harmony.auth.tests.module;
19561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
20561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.io.IOException;
21561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.security.Principal;
22561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.util.HashMap;
23561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.util.Set;
24561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
25561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport javax.security.auth.Subject;
26561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport javax.security.auth.callback.Callback;
27561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport javax.security.auth.callback.CallbackHandler;
28561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport javax.security.auth.callback.NameCallback;
29561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport javax.security.auth.callback.PasswordCallback;
30561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport javax.security.auth.callback.UnsupportedCallbackException;
31561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport javax.security.auth.login.LoginException;
32561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
33561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport junit.framework.TestCase;
34561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
35561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport org.apache.harmony.auth.module.LdapLoginModule;
36561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport org.apache.harmony.auth.UserPrincipal;
37561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
38561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
39561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughespublic class LdapLoginModuleTest extends TestCase {
40561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
41561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    //  module options
42561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    private HashMap<String, String> options = new HashMap<String, String>();
43561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
44561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    private final String USER_PROVIDER_URL = "ldap://9.181.106.121:389/ou=People,o=JNDITutorial,dc=my-domain,dc=com";
45561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
46561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    protected void setUp() throws Exception {
47561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        options.put("userProvider", USER_PROVIDER_URL);
48561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        options.put("useSSL", "false");
49561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
50561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
51561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    @Override
52561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    protected void tearDown() throws Exception {
53561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        options.clear();
54561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
55561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
56561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    /**
57561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * Test method for {@link org.apache.harmony.auth.module.LdapLoginModule#abort()}.
58561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     */
59561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void test_abort() throws LoginException{
60561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        LdapLoginModule jlm = new LdapLoginModule();
61561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
62561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertFalse("Should return false if login failed or no login", jlm
63561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    .abort());
64561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (LoginException e) {
65561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("Abort failed");
66561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
67561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        Subject subject = new Subject();
68561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        subject.setReadOnly();
69561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        jlm.initialize(subject, null, null, options);
70561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
71561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertFalse("Should return false if login failed or no login", jlm
72561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    .abort());
73561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (Exception e) {
74561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("Not any exception here");
75561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
76561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        subject = new Subject();
77561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        jlm.initialize(subject, new FaultCallbackHandler(), null, options);
78561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
79561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            jlm.login();
80561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("login should fail");
81561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (LoginException e) {
82561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertFalse("Should return false because of login failure", jlm
83561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    .abort());
84561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
85561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        subject = new Subject();
86561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        options.put("authIdentity","cn=Manager,dc=my-domain,dc=com");
87561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        jlm.initialize(subject, new MockCallbackHandler(), null, options);
88561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        jlm.login();
89561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        assertTrue("Should return true if login was successful", jlm
90561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                .abort());
91561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
92561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
93561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    /**
94561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * Test method for {@link org.apache.harmony.auth.module.LdapLoginModule#commit()}.
95561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     */
96561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void test_commit() {
97561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        LdapLoginModule module = new LdapLoginModule();
98561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        Subject subject = new Subject();
99561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        options.put("authIdentity","cn=Manager,dc=my-domain,dc=com");
100561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        module.initialize(subject, new MockCallbackHandler(), null, options);
101561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
102561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertTrue("Login should be successful", module.login());
103561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            module.commit();
104561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (LoginException e) {
105561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("Login shouldn't fail");
106561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
107561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        Set<Principal> principals = subject.getPrincipals();
108561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        assertFalse("Should get at least one principal", principals.isEmpty());
109561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        subject = new Subject();
110561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        subject.setReadOnly();
111561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        module.initialize(subject, new MockCallbackHandler(), null, options);
112561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
113561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertFalse("Commit shouldn't be successful", module.commit());
114561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("Should throw LoginException here because of trying to clear read-only subject");
115561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (LoginException e) {
116561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            // expected LoginException here
117561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
118561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
119561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
120561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    /**
121561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * Test method for {@link org.apache.harmony.auth.module.LdapLoginModule#initialize(javax.security.auth.Subject, javax.security.auth.callback.CallbackHandler, java.util.Map, java.util.Map)}.
122561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     */
123561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void test_initialize() {
124561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        LdapLoginModule module = new LdapLoginModule();
125561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
126561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            module.initialize(null, null, null, null);
127561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("Should throw NullPointerException here.");
128561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (NullPointerException e) {
129561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            // expected NullPointerException
130561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
131561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
132561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
133561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    /**
134561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * Test method for {@link org.apache.harmony.auth.module.LdapLoginModule#login()}.
135561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     */
136561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void test_login() {
137561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        LdapLoginModule module = new LdapLoginModule();
138561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        HashMap<String, String> emptyOptions = new HashMap<String, String>();
139561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        module.initialize(null, new MockCallbackHandler(), null, emptyOptions);
140561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
141561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            module.login();
142561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("Should throw LoginException here.");
143561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (LoginException e) {
144561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            // expected LoginException
145561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
146561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
147561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        options.put("authIdentity","cn=Manager,dc=my-domain,dc=com");
148561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        Subject subject = new Subject();
149561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        module.initialize(subject, new MockCallbackHandler(), null, options);
150561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
151561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertTrue("Login should be successful", module.login());
152561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (LoginException e) {
153561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("Login shouldn't fail");
154561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
155561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        module.initialize(subject, new FaultCallbackHandler(), null, options);
156561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
157561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertFalse("Login shouldn't be successful", module.login());
158561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("Login should fail");
159561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (LoginException e) {
160561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            // expected Loginexception here
161561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
162561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
163561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
164561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    /**
165561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * Test method for {@link org.apache.harmony.auth.module.LdapLoginModule#logout()}.
166561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     */
167561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void test_logout() {
168561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        LdapLoginModule module = new LdapLoginModule();
169561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        Subject subject = new Subject();
170561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        options.put("authIdentity","cn=Manager,dc=my-domain,dc=com");
171561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        module.initialize(subject, new MockCallbackHandler(), null, options);
172561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
173561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertTrue("Login should be successful", module.login());
174561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            module.commit();
175561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (LoginException e) {
176561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("Login shouldn't fail");
177561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
178561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        Set<Principal> principals = subject.getPrincipals();
179561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        assertFalse("Should get at least one principal", principals.isEmpty());
180561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
181561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertTrue("Should be true", module.logout());
182561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (LoginException e) {
183561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("Logout failed");
184561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
185561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        principals = subject.getPrincipals();
186561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        assertTrue("Principals should be cleared", principals.isEmpty());
187561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
188561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
189561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void test_optionsAndSharedStatus() throws LoginException{
190561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        options.put("authIdentity","cn=Manager,dc=my-domain,dc=com");
191561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        options.put("authzIdentity","testAuthzIdentityOption");
192561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        LdapLoginModule module = new LdapLoginModule();
193561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        Subject subject = new Subject();
194561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        module.initialize(subject, new MockCallbackHandler(), null, options);
195561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
196561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            module.login();
197561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            module.commit();
198561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertTrue("Should get a principal from authzIdentity option",subject.getPrincipals().contains(new UserPrincipal("testAuthzIdentityOption")));
199561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
200561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        catch(LoginException e){
201561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("Login failed");
202561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
203561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        finally{
204561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            module.logout();
205561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
206561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
207561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        options.put("debug", "true");
208561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        options.put("useFirstPass", "true");
209561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        HashMap<String, Object> status = new HashMap<String,Object>();
210561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        status.put("javax.security.auth.login.name", "leo");
211561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        status.put("javax.security.auth.login.password", "faultPass".toCharArray());
212561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        subject = new Subject();
213561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        module.initialize(subject, new MockCallbackHandler(), status, options);
214561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
215561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            module.login();
216561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("Should be failed for using password from shared state");
217561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
218561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        catch(LoginException e){
219561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            //expected LoginException here
220561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
221561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
222561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        options.remove("useFirstPass");
223561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        options.put("tryFirstPass", "true");
224561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        module.initialize(subject, new MockCallbackHandler(), status, options);
225561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
226561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            module.login();
227561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            module.commit();
228561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
229561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        catch(LoginException e){
230561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("Login should be failed");
231561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
232561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        finally{
233561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            module.logout();
234561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
235561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
236561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        options.remove("tryFirstPass");
237561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        options.put("clearPass", "true");
238561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        status.put("javax.security.auth.login.name", "leo");
239561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        status.put("javax.security.auth.login.password", "passw0rd".toCharArray());
240561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        module.initialize(subject, new MockCallbackHandler(), status, options);
241561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
242561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            module.login();
243561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            module.commit();
244561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertNull("javax.security.auth.login.name in shared state should be null when clearPass switch on",status.get("javax.security.auth.login.name"));
245561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertNull("javax.security.auth.login.password in shared state should be null when clearPass switch on",status.get("javax.security.auth.login.password"));
246561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (LoginException e) {
247561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("Login shouldn't fail");
248561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
249561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        finally{
250561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            module.logout();
251561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
252561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
253561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        status = new HashMap<String,Object>();
254561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        options.remove("clearPass");
255561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        options.put("storePass", "true");
256561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        module.initialize(subject, new FaultCallbackHandler(), status, options);
257561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
258561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            module.login();
259561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            module.commit();
260561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (LoginException e) {
261561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertNull("javax.security.auth.login.name in shared state should be null when login failed",status.get("javax.security.auth.login.name"));
262561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertNull("javax.security.auth.login.password in shared state should be null when login failed",status.get("javax.security.auth.login.password"));
263561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
264561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        finally{
265561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            module.logout();
266561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
267561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
268561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        module.initialize(subject, new MockCallbackHandler(), status, options);
269561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
270561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            module.login();
271561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            module.commit();
272561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (LoginException e) {
273561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("Login failed");
274561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
275561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        finally{
276561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            module.logout();
277561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
278561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        assertNotNull("javax.security.auth.login.name should be stored in shared state when storePass switch on",status.get("javax.security.auth.login.name"));
279561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        assertNotNull("javax.security.auth.login.password should be stored in shared state when storePass switch on",status.get("javax.security.auth.login.password"));
280561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
281561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        status.put("javax.security.auth.login.name", "tester");
282561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        status.put("javax.security.auth.login.password", "testerPass");
283561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        module.initialize(subject, new MockCallbackHandler(), status, options);
284561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
285561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            module.login();
286561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            module.commit();
287561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (LoginException e) {
288561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("Login failed");
289561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
290561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        finally{
291561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            module.logout();
292561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
293561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        assertEquals("Should't override the username value in sharedState",status.get("javax.security.auth.login.name"),"tester");
294561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        assertEquals("Should't override the password value in sharedState",status.get("javax.security.auth.login.password"),"testerPass");
295561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
296561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
297561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    static private class MockCallbackHandler implements CallbackHandler{
298561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
299561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
300561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            for(int i=0;i<callbacks.length;i++){
301561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                if(callbacks[i] instanceof NameCallback){
302561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    NameCallback nc = (NameCallback)callbacks[i];
303561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    nc.setName("leo");
304561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                }
305561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                else if(callbacks[i] instanceof PasswordCallback){
306561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    PasswordCallback pc = (PasswordCallback)callbacks[i];
307561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    pc.setPassword("secret".toCharArray());
308561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                }
309561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                else
310561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                {
311561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    throw new Error(callbacks[i].getClass().toString());
312561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                }
313561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            }
314561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
315561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
316561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
317561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    static private class FaultCallbackHandler implements CallbackHandler{
318561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
319561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
320561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            for(int i=0;i<callbacks.length;i++){
321561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                if(callbacks[i] instanceof NameCallback){
322561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    NameCallback nc = (NameCallback)callbacks[i];
323561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    nc.setName("leo");
324561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                }
325561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                else if(callbacks[i] instanceof PasswordCallback){
326561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    PasswordCallback pc = (PasswordCallback)callbacks[i];
327561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    pc.setPassword("password".toCharArray());
328561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                }
329561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                else
330561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                {
331561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    throw new Error(callbacks[i].getClass().toString());
332561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                }
333561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            }
334561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
335561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
336561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes}
337