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 org.apache.harmony.security.tests.java.security;
19
20import dalvik.annotation.TestTargetClass;
21import dalvik.annotation.TestTargets;
22import dalvik.annotation.TestLevel;
23import dalvik.annotation.TestTargetNew;
24
25import java.security.Permission;
26import java.security.PermissionCollection;
27import java.security.SecurityPermission;
28
29@TestTargetClass(Permission.class)
30public class Permission2Test extends junit.framework.TestCase {
31    static class ConcretePermission extends Permission {
32        public ConcretePermission() {
33            super("noname");
34        }
35
36        public boolean equals(Object obj) {
37            return true;
38        }
39
40        public String getActions() {
41            return "none";
42        }
43
44        public int hashCode() {
45            return 1;
46        }
47
48        public boolean implies(Permission p) {
49            return true;
50        }
51    }
52
53    /**
54     * @tests java.security.Permission#Permission(java.lang.String)
55     */
56    @TestTargetNew(
57        level = TestLevel.COMPLETE,
58        notes = "",
59        method = "Permission",
60        args = {java.lang.String.class}
61    )
62    public void test_ConstructorLjava_lang_String() {
63        // test method java.security.permission.Permission(string)
64        try {
65        SecurityPermission permi = new SecurityPermission(
66                "Testing the permission abstract class");
67        String name = permi.getName();
68        assertEquals("Permission Constructor failed",
69                "Testing the permission abstract class", name);
70        } catch (Exception e) {
71            fail("Unexpected excpetion");
72        }
73
74        try {
75            SecurityPermission permi = new SecurityPermission(null);
76            fail("NullPointerException was not thrown for NULL parameter");
77        } catch (NullPointerException e) {
78            //expected
79        }
80
81        try {
82            SecurityPermission permi = new SecurityPermission("");
83            fail("IllegalArgumentException was not thrown for empty parameter");
84        } catch (IllegalArgumentException e) {
85            //expected
86        }
87    }
88
89    /**
90     * @tests java.security.Permission#checkGuard(java.lang.Object)
91     */
92    @TestTargetNew(
93        level = TestLevel.PARTIAL_COMPLETE,
94        notes = "",
95        method = "checkGuard",
96        args = {java.lang.Object.class}
97    )
98    public void test_checkGuardLjava_lang_Object() {
99        // test method java.security.permission.checkGuard(object)
100        SecurityPermission permi = new SecurityPermission(
101                "Testing the permission abstract class");
102        String name = permi.getName();
103        try {
104            permi.checkGuard(name);
105        } catch (SecurityException e) {
106            fail("security not granted when it is suppose to be : " + e);
107        }
108    }
109
110    /**
111     * @tests java.security.Permission#getName()
112     */
113    @TestTargetNew(
114        level = TestLevel.COMPLETE,
115        notes = "",
116        method = "getName",
117        args = {}
118    )
119    public void test_getName() {
120        // test method java.security.permission.getName()
121        SecurityPermission permi = new SecurityPermission("testing getName()");
122        String name = permi.getName();
123        assertEquals("getName failed to obtain the correct name",
124                "testing getName()", name);
125
126        SecurityPermission permi2 = new SecurityPermission("93048Helloworld");
127        assertEquals("getName failed to obtain correct name",
128                "93048Helloworld", permi2.getName());
129    }
130
131    /**
132     * @tests java.security.Permission#newPermissionCollection()
133     */
134    @TestTargetNew(
135        level = TestLevel.COMPLETE,
136        notes = "",
137        method = "newPermissionCollection",
138        args = {}
139    )
140    public void test_newPermissionCollection() {
141        // test method java.security.permission.newPermissionCollection
142        Permission permi = new ConcretePermission();
143        PermissionCollection permiCollect = permi.newPermissionCollection();
144        assertNull("newPermissionCollector of the abstract class "
145                + "permission did not return a null instance "
146                + "of permissionCollection", permiCollect);
147    }
148
149    /**
150     * @tests java.security.Permission#toString()
151     */
152    @TestTargetNew(
153        level = TestLevel.COMPLETE,
154        notes = "",
155        method = "toString",
156        args = {}
157    )
158    public void test_toString() {
159        // test method java.security.permission.toString
160        // test for permission with no action
161        SecurityPermission permi = new SecurityPermission("testing toString");
162        String toString = permi.toString();
163        assertNotNull("toString should have returned a string of elements",
164                toString);
165    }
166}