File: contexts.java

package info (click to toggle)
mauve 20161030-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 44,628 kB
  • ctags: 35,425
  • sloc: java: 336,555; sh: 2,834; xml: 208; makefile: 72
file content (348 lines) | stat: -rw-r--r-- 11,125 bytes parent folder | download | duplicates (4)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright (C) 2006, 2007 Red Hat, Inc.
// Written by Gary Benson <gbenson@redhat.com>

// This file is part of Mauve.

// Mauve is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.

// Mauve is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Mauve; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.

// Tags: JDK1.2

package gnu.testlet.java.security.AccessController;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.FilePermission;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.AccessController;
import java.security.AccessControlContext;
import java.security.Permission;
import java.security.PermissionCollection;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;

import gnu.testlet.Testlet;
import gnu.testlet.TestHarness;

// In this test we load three different instances of ourself from
// three different jarfiles with three different classloaders.  Each
// classloader has a different protection domain in which that
// classloader's jarfile is readable.  All kinds of context-hopping is
// performed, and we infer which protection domains are in our stack
// by seeing which jarfile read permissions we can see.

public class contexts implements Testlet
{
  public void test(TestHarness harness)
  {
    // The bits where we access protection domains is Classpath-specific.
    if (System.getProperty("gnu.classpath.version") == null)
      return;

    File jars[] = new File[] {null, null, null};
    try {
      harness.checkPoint("setup");

      // Make jarfiles containing this class and its dependencies
      String base =
          new File(harness.getTempDirectory(), "ac").getCanonicalPath();

      jars[0] = new File(base + "1.jar");
      JarOutputStream jos = new JarOutputStream(new FileOutputStream(jars[0]));
      copyClass(harness.getBuildDirectory(), jos, getClass());
      copyClass(harness.getBuildDirectory(), jos, TestHarness.class);
      jos.close();

      for (int i = 1; i < jars.length; i++) {
	jars[i] = new File(base + (i + 1) + ".jar");
	copyFile(jars[0], jars[i]);
      }

      // Create instances of ourself loaded from different loaders
      TestObject testObjects[] = new TestObject[jars.length];
      for (int i = 0; i < jars.length; i++) {
	Class testClass = new URLClassLoader(new URL[] {
	  jars[i].toURL()}, null).loadClass(getClass().getName());
	harness.check(
	  getClass().getClassLoader() != testClass.getClassLoader());
	Constructor c = testClass.getConstructor(new Class[] {String.class});
	testObjects[i] = new TestObject(c.newInstance(new Object[] {base}));
      }

      // Run the tests
      test(harness, testObjects);
    }
    catch (Exception ex) {
      harness.debug(ex);
      harness.check(false, "Unexpected exception");
    }
    finally {
      for (int i = 0; i < jars.length; i++) {
	if (jars[i].exists())
	  jars[i].delete();
      }
    }
  }

  // Copy a classfile and its dependencies into a jarfile
  private static void copyClass(String srcdir, JarOutputStream jos, Class cls)
    throws Exception
  {
    File root = new File(srcdir, cls.getName().replace(".", File.separator));
    final String rootpath = root.getPath();
    int chop = srcdir.length() + File.separator.length();

    File dir = root.getParentFile();
    if (dir.isDirectory()) {
      File[] files = dir.listFiles(new FileFilter() {
	public boolean accept(File file) {
	  String path = file.getPath();
	  if (path.endsWith(".class")) {
	    path = path.substring(0, path.length() - 6);
	    if (path.equals(rootpath))
	      return true;
	    if (path.startsWith(rootpath + "$"))
	      return true;
	  }
	  return false;
	}
      });
      for (int i = 0; i < files.length; i++) {
	byte[] bytes = new byte[(int) files[i].length()];
	FileInputStream fis = new FileInputStream(files[i]);
	fis.read(bytes);
	fis.close();

	jos.putNextEntry(new JarEntry(files[i].getPath().substring(chop)));
	jos.write(bytes, 0, bytes.length);
      }
    }

    Class superclass = cls.getSuperclass();
    if (superclass != null)
      copyClass(srcdir, jos, superclass);
    Class[] interfaces = cls.getInterfaces();
    for (int i = 0; i < interfaces.length; i++)
      copyClass(srcdir, jos, interfaces[i]);
  }

  // Make a copy of a file
  private static void copyFile(File src, File dst) throws Exception
  {
    byte[] bytes = new byte[(int) src.length()];
    FileInputStream fis = new FileInputStream(src);
    fis.read(bytes);
    fis.close();

    FileOutputStream fos = new FileOutputStream(dst);
    fos.write(bytes);
    fos.close();
  }

  // Constructor for the main object that Mauve creates
  public contexts()
  {
  }
  
  // Constructor for the sub-objects that the main object creates
  private String base = null;
  public contexts(String base)
  {
    this.base = base;
  }
  
  // Wrapper to hide the pain of reflection
  private static class TestObject
  {
    private Object object;

    public TestObject(Object object)
    {
      this.object = object;
    }

    public String[] listJarsOf(TestObject other) throws Exception
    {
      Method method = object.getClass().getMethod(
	"listJarsOf", new Class[] {Object.class});
      return (String[]) method.invoke(object, new Object[] {other.object});
    }

    public String[] callListJarsOf(TestObject caller, TestObject callee)
      throws Exception
    {
      Method method = object.getClass().getMethod(
	"callListJarsOf", new Class[] {Object.class, Object.class});
      return (String[]) method.invoke(
	object, new Object[] {caller.object, callee.object});
    }

    public String[] callPrivilegedListJarsOf(
      TestObject caller, TestObject callee) throws Exception
    {
      Method method = object.getClass().getMethod(
	"callPrivilegedListJarsOf", new Class[] {Object.class, Object.class});
      return (String[]) method.invoke(
	object, new Object[] {caller.object, callee.object});
    }
  }

  public String[] listJarsOf(Object object) throws Exception
  {
    Method method = object.getClass().getMethod("listJars", new Class[0]);
    return (String[]) method.invoke(object, new Object[0]);
  }

  public String[] callListJarsOf(Object caller, Object callee)
    throws Exception
  {
    Method method = caller.getClass().getMethod(
      "listJarsOf", new Class[] {Object.class});
    return (String[]) method.invoke(caller, new Object[] {callee});
  }

  public String[] callPrivilegedListJarsOf(Object caller, Object callee)
    throws Exception
  {
    Method method = caller.getClass().getMethod(
      "privilegedListJarsOf", new Class[] {Object.class});
    return (String[]) method.invoke(caller, new Object[] {callee});
  }

  public String[] privilegedListJarsOf(final Object object) throws Exception
  {
    final Method method = object.getClass().getMethod(
      "listJars", new Class[0]);
    return (String[]) AccessController.doPrivileged(new PrivilegedAction() {
      public Object run() {
	try {
	  return method.invoke(object, new Object[0]);
	}
	catch (Exception e) {
	  return e;
	}
      }
    });
  }

  public String[] listJars() throws Exception
  {
    AccessControlContext ctx = AccessController.getContext();
    // XXX start of classpath-specific code
    Field field = ctx.getClass().getDeclaredField("protectionDomains");
    field.setAccessible(true);
    ProtectionDomain[] domains = (ProtectionDomain[]) field.get(ctx);
    // XXX end of classpath-specific code

    LinkedList jars = new LinkedList();
    for (int i = 0; i < domains.length; i++) {
      PermissionCollection perms = domains[i].getPermissions();
      for (Enumeration e = perms.elements(); e.hasMoreElements() ;) {
	Permission p = (Permission) e.nextElement();
	if (!(p instanceof FilePermission))
	  continue;
	String path = p.getName();
	if (path.length() == base.length() + 5
	    && path.startsWith(base)
	    && Character.isDigit(path.charAt(base.length()))
	    && path.endsWith(".jar"))
	  jars.add(path);
      }
    }
    return (String[]) jars.toArray(new String[jars.size()]);
  }
  
  // Perform the tests
  private static void test(TestHarness harness, TestObject[] objects)
    throws Exception
  {
    // Each object should see only its own protection domain
    harness.checkPoint("self-listing");

    String[] jars = new String[objects.length];
    for (int i = 0; i < objects.length; i++) {
      String[] result = objects[i].listJarsOf(objects[i]);
      harness.check(result.length == 1);
      jars[i] = result[0];
    }
    for (int i = 0; i < objects.length; i++) {
      for (int j = i + 1; j < objects.length; j++)
	harness.check(!jars[i].equals(jars[j]));
    }
    
    // When one object calls another both objects' protection domains
    // should be present.
    harness.checkPoint("straight other-listing");

    boolean[] seen = new boolean[jars.length];
    String[] result = objects[0].listJarsOf(objects[1]);
    harness.check(result.length == 2);
    for (int i = 0; i < seen.length; i++) {
      seen[i] = false;
      for (int j = 0; j < result.length; j++) {
	if (result[j].equals(jars[i])) {
	  harness.check(!seen[i]);
	  seen[i] = true;
	}
      }
    }
    harness.check(seen[0] && seen[1] && !seen[2]);

    // When one object calls another that calls another all three
    // objects' protection domains should be present.
    harness.checkPoint("straight other-other-listing");

    result = objects[0].callListJarsOf(objects[1], objects[2]);
    harness.check(result.length == 3);
    for (int i = 0; i < seen.length; i++) {
      seen[i] = false;
      for (int j = 0; j < result.length; j++) {
	if (result[j].equals(jars[i])) {
	  harness.check(!seen[i]);
	  seen[i] = true;
	}
      }
    }
    harness.check(seen[0] && seen[1] && seen[2]);

    // When one object calls another that uses doPrivileged to call
    // a third then the first object's protection domain should not
    // be present.
    harness.checkPoint("privileged other-other-listing");

    result = objects[0].callPrivilegedListJarsOf(objects[1], objects[2]);
    harness.check(result.length == 2);
    for (int i = 0; i < seen.length; i++) {
      seen[i] = false;
      for (int j = 0; j < result.length; j++) {
	if (result[j].equals(jars[i])) {
	  harness.check(!seen[i]);
	  seen[i] = true;
	}
      }
    }
    harness.check(!seen[0] && seen[1] && seen[2]);
  }
}