Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e85267e27eeb4f1a664c327cf1542f111e68e486 (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
/*******************************************************************************
 * 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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Permission;
import java.security.PermissionCollection;
import java.util.Enumeration;
import java.util.NoSuchElementException;
import org.osgi.framework.PackagePermission;
import org.osgi.framework.ServicePermission;

public class PermissionTests {

	protected void badServicePermission(String name, String actions) {
		try {
			ServicePermission p = new ServicePermission(name, actions);
			fail(p + " created with invalid actions"); //$NON-NLS-1$
		} catch (IllegalArgumentException e) {
			// expected
		}
	}

	protected void badPackagePermission(String name, String actions) {
		try {
			PackagePermission p = new PackagePermission(name, actions);
			fail(p + " created with invalid actions"); //$NON-NLS-1$
		} catch (IllegalArgumentException e) {
			// expected
		}
	}

	protected void checkEnumeration(Enumeration en, boolean isEmpty) {
		assertEquals(en + " empty state is invalid", !isEmpty, en.hasMoreElements()); //$NON-NLS-1$
		try {
			while (en.hasMoreElements()) {
				en.nextElement();
			}
		} catch (NoSuchElementException e) {
			fail(en + " threw NoSuchElementException"); //$NON-NLS-1$
		}

		try {
			en.nextElement();
			fail(en + " is empty but didn't throw NoSuchElementException"); //$NON-NLS-1$
		} catch (NoSuchElementException e) {
			// expected
		}
	}

	protected void shouldImply(Permission p1, Permission p2) {
		assertTrue(p1 + " does not imply " + p2, p1.implies(p2)); //$NON-NLS-1$
	}

	protected void shouldNotImply(Permission p1, Permission p2) {
		assertFalse(p1 + " does imply " + p2, p1.implies(p2)); //$NON-NLS-1$
	}

	protected void shouldImply(PermissionCollection p1, Permission p2) {
		assertTrue(p1 + " does not imply " + p2, p1.implies(p2)); //$NON-NLS-1$
	}

	protected void shouldNotImply(PermissionCollection p1, Permission p2) {
		assertFalse(p1 + " does imply " + p2, p1.implies(p2)); //$NON-NLS-1$
	}

	protected void shouldEqual(Permission p1, Permission p2) {
		assertTrue(p1 + " does not equal " + p2, p1.equals(p2)); //$NON-NLS-1$
		assertTrue(p2 + " does not equal " + p1, p2.equals(p1)); //$NON-NLS-1$
	}

	protected void shouldNotEqual(Permission p1, Permission p2) {
		assertFalse(p1 + " does equal " + p2, p1.equals(p2)); //$NON-NLS-1$
	}

	protected void shouldAdd(PermissionCollection p1, Permission p2) {
		try {
			p1.add(p2);
		} catch (Exception e) {
			fail(p1 + " will not add " + p2); //$NON-NLS-1$
		}
	}

	protected void shouldNotAdd(PermissionCollection p1, Permission p2) {
		try {
			p1.add(p2);
			fail(p1 + " will add " + p2); //$NON-NLS-1$
		} catch (Exception e) {
			// expected
		}
	}

	protected void testSerialization(Permission p1) {
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ObjectOutputStream out = new ObjectOutputStream(baos);

			out.writeObject(p1);
			out.flush();
			out.close();

			ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
			ObjectInputStream in = new ObjectInputStream(bais);

			Permission p2 = (Permission) in.readObject();

			shouldEqual(p1, p2);
			shouldImply(p1, p2);
			shouldImply(p2, p1);
		} catch (Exception e) {
			fail(e.toString());
		}
	}

}

Back to the top