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 java.security.Permission;
21
22import dalvik.annotation.TestLevel;
23import dalvik.annotation.TestTargetClass;
24import dalvik.annotation.TestTargetNew;
25
26import junit.framework.TestCase;
27
28import javax.security.auth.PrivateCredentialPermission;
29
30
31/**
32 * Tests for <code>PrivateCredentialPermission</code> class constructors and methods.
33 *
34 */
35@TestTargetClass(PrivateCredentialPermission.class)
36public class PrivateCredentialPermissionTest extends TestCase {
37
38    private final static String cred_class1 = "a.b.Credential";
39    private final static String cred_class2 = "a.b.Credential1";
40    private final static String name1 = cred_class1 + " a.b.Principal \"*\"";
41    private final static String name2 = cred_class1 + " a.c.Principal \"*\"";
42    private final static String name4 = cred_class2 + " a.c.Principal \"*\"";
43    private final static String pc1 = "a.b.Principal";
44    private final static String pn1 = "*";
45    private final static String pc2 = "a.c.Principal";
46    private final static String pn2 = "abc";
47
48    private final static String name3 = cred_class1 + " " + pc1 + " \"" + pn1 + "\" " + pc2 + " \"" + pn2 + "\"";
49
50    /**
51     * @tests javax.security.auth.PrivateCredentialPermission#PrivateCredentialPermission(String name, String actions)
52     */
53    @TestTargetNew(
54        level = TestLevel.COMPLETE,
55        notes = "",
56        method = "PrivateCredentialPermission",
57        args = {String.class, String.class}
58    )
59    public void test_Constructor_01() {
60        PrivateCredentialPermission ap = new PrivateCredentialPermission(name1, "read");
61
62        String actions[] = { "write", "", null };
63
64        for(int i = 0; i < actions.length; i++) {
65            try {
66                ap = new PrivateCredentialPermission(name1, "write");
67                fail("expected IllegalArgumentException if action is not \"read\"");
68            } catch (IllegalArgumentException e) {
69                // expected
70            }
71        }
72
73        String names[] = { null,
74                           "",
75                           "a.b.Credential a.c.Principal *\"",
76                           "a.b.Credential_a.c.Principal_\"*\"",
77                           "a.b.Credential a.c.Principal_\"*\"",
78                           "a.b.Credential * \"a\""
79                          };
80
81        for(int i = 0; i < names.length; i++) {
82            try {
83                ap = new PrivateCredentialPermission(names[i], "read");
84                fail("expected IllegalArgumentException for malformed \"name\" argument (" + names[i] +")");
85            } catch (IllegalArgumentException e) {
86                // expected
87            } catch (NullPointerException npe) {
88                if (names[i] != null)
89                    throw npe;
90                else
91                    ;     // expected if name is null
92            }
93        }
94    }
95
96    /**
97     * @tests javax.security.auth.PrivateCredentialPermission#getActions()
98     */
99    @TestTargetNew(
100        level = TestLevel.COMPLETE,
101        notes = "",
102        method = "getActions",
103        args = {}
104    )
105    public void test_getActions() {
106        PrivateCredentialPermission ap = new PrivateCredentialPermission(name1, "read");
107        assertEquals("getActions() must alway return \"read\"", "read", ap.getActions());
108    }
109
110    /**
111     * @tests javax.security.auth.PrivateCredentialPermission#implies()
112     */
113    @TestTargetNew(
114        level = TestLevel.COMPLETE,
115        notes = "",
116        method = "implies",
117        args = { Permission.class }
118    )
119    public void test_implies() {
120        PrivateCredentialPermission p1 = new PrivateCredentialPermission("* P1 \"abc\"", "read");
121        PrivateCredentialPermission p2 = new PrivateCredentialPermission("a.b.Credential P1 \"abc\"", "read");
122        PrivateCredentialPermission p3 = new PrivateCredentialPermission("C1 P1 \"abc\"", "read");
123        PrivateCredentialPermission p4 = new PrivateCredentialPermission("C1 P1 \"abc\" P2 \"abcd\"", "read");
124        PrivateCredentialPermission p5 = new PrivateCredentialPermission("C1 P1 \"*\"", "read");
125        PrivateCredentialPermission p6 = new PrivateCredentialPermission("a.b.Credential * \"*\"", "read");
126        PrivateCredentialPermission p7 = new PrivateCredentialPermission("a.b.Credential P2 \"abc\"", "read");
127        PrivateCredentialPermission p8 = new PrivateCredentialPermission("a.b.Credential1 P2 \"abc\"", "read");
128        PrivateCredentialPermission p9 = new PrivateCredentialPermission("a.b.Credential1 P2 \"*\"", "read");
129
130        PrivateCredentialPermission[][] arr = { { p1, p2 },
131                                                { p2, p1 },
132                                                { p3, p4 },
133                                                { p5, p3 },
134                                                { p6, p2 },
135                                                { p2, p7 },
136                                                { p7, p8 },
137                                                { p8, p9 }};
138
139        boolean[] r = { true, false, true, true, true, false, false, false };
140        for(int i = 0; i < arr.length; i++)
141            assertEquals("implies() returned wrong result (" + i + ")", r[i], arr[i][0].implies(arr[i][1]));
142    }
143
144    /**
145     * @tests javax.security.auth.PrivateCredentialPermission#getCredentialClass()
146     */
147    @TestTargetNew(
148        level = TestLevel.COMPLETE,
149        notes = "",
150        method = "getCredentialClass",
151        args = {}
152    )
153    public void test_getCredentialClass() {
154        PrivateCredentialPermission ap = new PrivateCredentialPermission(name1, "read");
155        assertEquals("getCredentialClass() returned wrong name", cred_class1, ap.getCredentialClass());
156    }
157
158    /**
159     * @tests javax.security.auth.PrivateCredentialPermission#getPrincipals()
160     */
161    @TestTargetNew(
162        level = TestLevel.COMPLETE,
163        notes = "",
164        method = "getPrincipals",
165        args = {}
166    )
167    public void test_getPrincipals() {
168
169        PrivateCredentialPermission ap = new PrivateCredentialPermission(name3, "read");
170        String[][] p = ap.getPrincipals();
171
172        assertEquals("wrong number of principals", 2, p.length);
173
174        assertEquals("wrong principal class 0", pc1, p[0][0]);
175        assertEquals("wrong principal name 0", pn1, p[0][1]);
176
177        assertEquals("wrong principal class 1", pc2, p[1][0]);
178        assertEquals("wrong principal name 1", pn2, p[1][1]);
179    }
180
181    /**
182     * @tests javax.security.auth.PrivateCredentialPermission#equals()
183     */
184    @TestTargetNew(
185        level = TestLevel.COMPLETE,
186        notes = "",
187        method = "equals",
188        args = { Object.class }
189    )
190    public void test_equals() {
191        PrivateCredentialPermission p1 = new PrivateCredentialPermission(name3, "read");
192        PrivateCredentialPermission p2 = new PrivateCredentialPermission(name3, "read");
193        PrivateCredentialPermission p3 = new PrivateCredentialPermission(name1, "read");
194        PrivateCredentialPermission p4 = new PrivateCredentialPermission(name1, "read");
195        PrivateCredentialPermission p5 = new PrivateCredentialPermission(name2, "read");
196        PrivateCredentialPermission p6 = new PrivateCredentialPermission(name4, "read");
197
198        PrivateCredentialPermission arr[][] = { { p1, p2 },
199                                                { p3, p4 },
200                                                { p4, p5 },
201                                                { p1, p3 },
202                                                { p4, p6 } };
203        boolean r[] = { true, true, false, false, false };
204
205        for(int i = 0; i < arr.length; i++) {
206            assertEquals("equals() returned wrong result", r[i], arr[i][0].equals(arr[i][1]));
207        }
208
209        try {
210            assertFalse(p1.equals(null));
211        } catch(NullPointerException npe) {
212
213        }
214    }
215
216    /**
217     * @tests javax.security.auth.PrivateCredentialPermission#hashCode()
218     */
219    @TestTargetNew(
220        level = TestLevel.COMPLETE,
221        notes = "",
222        method = "hashCode",
223        args = {}
224    )
225    public void test_hashCode() {
226        PrivateCredentialPermission p1 = new PrivateCredentialPermission(name1, "read");
227        PrivateCredentialPermission p2 = new PrivateCredentialPermission(name1, "read");
228        int arr[][] = new int[10][];
229        for(int i = 0; i < 10; i++) {
230            int h1 = p1.hashCode();
231
232            System.gc();
233
234            // force some memory allocations
235            arr[i] = new int[50000];
236
237            assertEquals("hashCode() must consistently return the same integer", h1, p1.hashCode());
238            assertEquals("hashCode() must be the same for equal PrivateCredentialPermission objects", p1.hashCode(), p2.hashCode());
239        }
240
241
242        PrivateCredentialPermission p3 = new PrivateCredentialPermission(name4, "read");
243        assertFalse("hashCode() must not be the same for non-equal PrivateCredentialPermission objects", p1.hashCode() == p3.hashCode());
244    }
245
246    /**
247     * @tests javax.security.auth.PrivateCredentialPermission#newPermissionCollection()
248     */
249    @TestTargetNew(
250        level = TestLevel.COMPLETE,
251        notes = "",
252        method = "newPermissionCollection",
253        args = {}
254    )
255    public void test_newPermissionCollection() {
256        PrivateCredentialPermission ap = new PrivateCredentialPermission(name1, "read");
257        assertNull("newPermissionCollection must always return null", ap.newPermissionCollection());
258    }
259
260}
261
262