PasswordCallbackTest.java revision f33eae7e84eb6d3b0f4e86b59605bb3de73009f3
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
18package tests.api.javax.security.auth;
19
20import dalvik.annotation.TestLevel;
21import dalvik.annotation.TestTargetClass;
22import dalvik.annotation.TestTargetNew;
23import dalvik.annotation.TestTargets;
24
25import junit.framework.TestCase;
26
27import javax.security.auth.callback.PasswordCallback;
28
29/**
30 * Tests for <code>PasswordCallback</code> class constructors and methods.
31 *
32 */
33@TestTargetClass(PasswordCallback.class)
34public class PasswordCallbackTest extends TestCase {
35
36    /**
37     * @tests javax.security.auth.callback.PasswordCallback#PasswordCallback(String prompt, boolean echoOn)
38     * @tests javax.security.auth.callback.PasswordCallback#getPrompt()
39     * @tests javax.security.auth.callback.PasswordCallback#isEchoOn()
40     */
41    @TestTargets({
42        @TestTargetNew(
43            level = TestLevel.COMPLETE,
44            notes = "",
45            method = "PasswordCallback",
46            args = {String.class, boolean.class}
47        ),
48        @TestTargetNew(
49            level = TestLevel.COMPLETE,
50            notes = "",
51            method = "getPrompt",
52            args = {}
53        ),
54        @TestTargetNew(
55            level = TestLevel.COMPLETE,
56            notes = "",
57            method = "isEchoOn",
58            args = {}
59        )
60    })
61    public void test_PasswordCallback() {
62        String prompt = "promptTest";
63
64        try {
65            PasswordCallback pc = new PasswordCallback(prompt, true);
66            assertNotNull("Null object returned", pc);
67            assertEquals(prompt, pc.getPrompt());
68            assertEquals(true, pc.isEchoOn());
69        } catch (Exception e) {
70            fail("Unexpected exception: " + e);
71        }
72
73        try {
74            PasswordCallback pc = new PasswordCallback(prompt, false);
75            assertNotNull("Null object returned", pc);
76            assertEquals(prompt, pc.getPrompt());
77            assertEquals(false, pc.isEchoOn());
78        } catch (Exception e) {
79            fail("Unexpected exception: " + e);
80        }
81
82        try {
83            PasswordCallback pc = new PasswordCallback(null, true);
84            fail("IllegalArgumentException wasn't thrown");
85        } catch (IllegalArgumentException npe) {
86        }
87
88        try {
89            PasswordCallback pc = new PasswordCallback("", true);
90            fail("IllegalArgumentException wasn't thrown");
91        } catch (IllegalArgumentException npe) {
92        }
93    }
94
95    /**
96     * @tests javax.security.auth.callback.PasswordCallback#getPassword()
97     * @tests javax.security.auth.callback.PasswordCallback#setPassword(char[] password)
98     * @tests javax.security.auth.callback.PasswordCallback#clearPassword()
99     */
100    @TestTargets({
101        @TestTargetNew(
102            level = TestLevel.COMPLETE,
103            notes = "",
104            method = "getPassword",
105            args = {}
106        ),
107        @TestTargetNew(
108            level = TestLevel.COMPLETE,
109            notes = "",
110            method = "setPassword",
111            args = {char[].class}
112        ),
113        @TestTargetNew(
114            level = TestLevel.COMPLETE,
115            notes = "",
116            method = "clearPassword",
117            args = {}
118        )
119    })
120    public void test_Password() {
121        String prompt = "promptTest";
122        char[] psw1 = "testPassword".toCharArray();
123        char[] psw2 = "newPassword".toCharArray();
124        PasswordCallback pc = new PasswordCallback(prompt, true);
125
126        try {
127            assertNull(pc.getPassword());
128            pc.setPassword(psw1);
129            assertEquals(psw1.length, pc.getPassword().length);
130            pc.setPassword(null);
131            assertNull(pc.getPassword());
132            pc.setPassword(psw2);
133            char[] res = pc.getPassword();
134            assertEquals(psw2.length, res.length);
135            for (int i = 0; i < res.length; i++) {
136                assertEquals("Incorrect password was returned", psw2[i], res[i]);
137            }
138            pc.clearPassword();
139            res = pc.getPassword();
140            if (res.equals(psw2)) {
141                fail("Incorrect password was returned after clear");
142            }
143            pc.setPassword(psw1);
144            res = pc.getPassword();
145            assertEquals(psw1.length, res.length);
146            for (int i = 0; i < res.length; i++) {
147                assertEquals("Incorrect result", psw1[i], res[i]);
148            }
149        } catch (Exception e) {
150            fail("Unexpected exception: " + e);
151        }
152    }
153}
154