Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8b74fd5fc83b78472e29d6f2d9848ccfb879a5b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*******************************************************************************
 * Copyright (c) 2008 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.osgi.tests.permissions;

import java.security.Permission;
import java.security.PermissionCollection;
import java.util.PropertyPermission;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.osgi.framework.ServicePermission;

public class ServicePermissionTests extends PermissionTests {

	public static Test suite() {
		return new TestSuite(ServicePermissionTests.class);
	}

	public void testServicePermission() {
		badServicePermission("a.b.c", "x"); //$NON-NLS-1$ //$NON-NLS-2$
		badServicePermission("a.b.c", "   get  ,  x   "); //$NON-NLS-1$ //$NON-NLS-2$
		badServicePermission("a.b.c", ""); //$NON-NLS-1$ //$NON-NLS-2$
		badServicePermission("a.b.c", "      "); //$NON-NLS-1$ //$NON-NLS-2$
		badServicePermission("a.b.c", null); //$NON-NLS-1$
		badServicePermission("a.b.c", ","); //$NON-NLS-1$ //$NON-NLS-2$
		badServicePermission("a.b.c", ",xxx"); //$NON-NLS-1$ //$NON-NLS-2$
		badServicePermission("a.b.c", "xxx,"); //$NON-NLS-1$ //$NON-NLS-2$
		badServicePermission("a.b.c", "get,"); //$NON-NLS-1$ //$NON-NLS-2$
		badServicePermission("a.b.c", "register,   "); //$NON-NLS-1$ //$NON-NLS-2$
		badServicePermission("a.b.c", "getme,"); //$NON-NLS-1$ //$NON-NLS-2$
		badServicePermission("a.b.c", "registerme,"); //$NON-NLS-1$ //$NON-NLS-2$
		badServicePermission("a.b.c", ",get"); //$NON-NLS-1$ //$NON-NLS-2$
		badServicePermission("a.b.c", ",register"); //$NON-NLS-1$ //$NON-NLS-2$
		badServicePermission("a.b.c", "   getme   "); //$NON-NLS-1$ //$NON-NLS-2$
		badServicePermission("a.b.c", "   registerme     "); //$NON-NLS-1$ //$NON-NLS-2$
		badServicePermission("a.b.c", "   ge"); //$NON-NLS-1$ //$NON-NLS-2$
		badServicePermission("a.b.c", "   registe"); //$NON-NLS-1$ //$NON-NLS-2$

		Permission op = new PropertyPermission("java.home", "read"); //$NON-NLS-1$ //$NON-NLS-2$

		ServicePermission p11 = new ServicePermission("com.foo.service1", "    GET,register   "); //$NON-NLS-1$ //$NON-NLS-2$
		ServicePermission p12 = new ServicePermission("com.foo.service1", "REGISTER  ,   get"); //$NON-NLS-1$ //$NON-NLS-2$
		ServicePermission p13 = new ServicePermission("com.foo.service1", "regisTER   "); //$NON-NLS-1$ //$NON-NLS-2$
		ServicePermission p14 = new ServicePermission("com.foo.service1", "    Get    "); //$NON-NLS-1$ //$NON-NLS-2$

		shouldImply(p11, p11);
		shouldImply(p11, p12);
		shouldImply(p11, p13);
		shouldImply(p11, p14);

		shouldImply(p12, p11);
		shouldImply(p12, p12);
		shouldImply(p12, p13);
		shouldImply(p12, p14);

		shouldImply(p13, p13);
		shouldImply(p14, p14);

		shouldNotImply(p13, p11);
		shouldNotImply(p13, p12);

		shouldNotImply(p14, p11);
		shouldNotImply(p14, p12);

		shouldNotImply(p13, p14);
		shouldNotImply(p14, p13);

		shouldNotImply(p11, op);

		shouldEqual(p11, p11);
		shouldEqual(p11, p12);
		shouldEqual(p12, p11);
		shouldEqual(p12, p12);
		shouldEqual(p13, p13);
		shouldEqual(p14, p14);

		shouldNotEqual(p11, p13);
		shouldNotEqual(p11, p14);
		shouldNotEqual(p12, p13);
		shouldNotEqual(p12, p14);
		shouldNotEqual(p13, p11);
		shouldNotEqual(p13, p12);
		shouldNotEqual(p13, p14);
		shouldNotEqual(p14, p11);
		shouldNotEqual(p14, p12);
		shouldNotEqual(p14, p13);

		PermissionCollection pc = p13.newPermissionCollection();

		checkEnumeration(pc.elements(), true);

		shouldNotImply(pc, p11);

		shouldAdd(pc, p14);
		shouldImply(pc, p14);
		shouldNotImply(pc, p11);
		shouldNotImply(pc, p12);
		shouldNotImply(pc, p13);

		shouldAdd(pc, p13);
		shouldImply(pc, p11);
		shouldImply(pc, p12);
		shouldImply(pc, p13);
		shouldImply(pc, p14);

		shouldNotAdd(pc, op);

		pc = p13.newPermissionCollection();

		shouldAdd(pc, p13);
		shouldImply(pc, p13);
		shouldNotImply(pc, p11);
		shouldNotImply(pc, p12);
		shouldNotImply(pc, p14);

		shouldAdd(pc, p14);
		shouldImply(pc, p11);
		shouldImply(pc, p12);
		shouldImply(pc, p13);
		shouldImply(pc, p14);

		pc = p11.newPermissionCollection();

		shouldAdd(pc, p11);
		shouldImply(pc, p11);
		shouldImply(pc, p12);
		shouldImply(pc, p13);
		shouldImply(pc, p14);

		pc.setReadOnly();

		shouldNotAdd(pc, p12);

		checkEnumeration(pc.elements(), false);

		ServicePermission p21 = new ServicePermission("com.foo.service2", "get"); //$NON-NLS-1$ //$NON-NLS-2$
		ServicePermission p22 = new ServicePermission("com.foo.*", "get"); //$NON-NLS-1$ //$NON-NLS-2$
		ServicePermission p23 = new ServicePermission("com.*", "get"); //$NON-NLS-1$ //$NON-NLS-2$
		ServicePermission p24 = new ServicePermission("*", "get"); //$NON-NLS-1$ //$NON-NLS-2$

		shouldImply(p21, p21);
		shouldImply(p22, p21);
		shouldImply(p23, p21);
		shouldImply(p24, p21);

		shouldImply(p22, p22);
		shouldImply(p23, p22);
		shouldImply(p24, p22);

		shouldImply(p23, p23);
		shouldImply(p24, p23);

		shouldImply(p24, p24);

		shouldNotImply(p21, p22);
		shouldNotImply(p21, p23);
		shouldNotImply(p21, p24);

		shouldNotImply(p22, p23);
		shouldNotImply(p22, p24);

		shouldNotImply(p23, p24);

		pc = p21.newPermissionCollection();

		shouldAdd(pc, p21);
		shouldImply(pc, p21);
		shouldNotImply(pc, p22);
		shouldNotImply(pc, p23);
		shouldNotImply(pc, p24);

		shouldAdd(pc, p22);
		shouldImply(pc, p21);
		shouldImply(pc, p22);
		shouldNotImply(pc, p23);
		shouldNotImply(pc, p24);

		shouldAdd(pc, p23);
		shouldImply(pc, p21);
		shouldImply(pc, p22);
		shouldImply(pc, p23);
		shouldNotImply(pc, p24);

		shouldAdd(pc, p24);
		shouldImply(pc, p21);
		shouldImply(pc, p22);
		shouldImply(pc, p23);
		shouldImply(pc, p24);

		pc = p22.newPermissionCollection();

		shouldAdd(pc, p22);
		shouldImply(pc, p21);
		shouldImply(pc, p22);
		shouldNotImply(pc, p23);
		shouldNotImply(pc, p24);

		pc = p23.newPermissionCollection();

		shouldAdd(pc, p23);
		shouldImply(pc, p21);
		shouldImply(pc, p22);
		shouldImply(pc, p23);
		shouldNotImply(pc, p24);

		pc = p24.newPermissionCollection();

		shouldAdd(pc, p24);
		shouldImply(pc, p21);
		shouldImply(pc, p22);
		shouldImply(pc, p23);
		shouldImply(pc, p24);

		testSerialization(p11);
		testSerialization(p12);
		testSerialization(p13);
		testSerialization(p14);
		testSerialization(p21);
		testSerialization(p22);
		testSerialization(p23);
		testSerialization(p24);
	}

}

Back to the top