29package com.jogamp.junit.sec;
31import java.net.URISyntaxException;
32import java.security.AccessControlException;
34import java.io.IOException;
36import org.junit.Assert;
37import org.junit.BeforeClass;
40import com.jogamp.common.net.Uri;
41import com.jogamp.common.os.NativeLibrary;
42import com.jogamp.common.os.Platform;
43import com.jogamp.common.util.IOUtil;
44import com.jogamp.junit.util.SingletonJunitCase;
46import org.junit.FixMethodOrder;
47import org.junit.runners.MethodSorters;
49@FixMethodOrder(MethodSorters.NAME_ASCENDING)
51 static final String java_io_tmpdir_propkey =
"java.io.tmpdir";
52 static final String java_home_propkey =
"java.home";
53 static final String os_name_propkey =
"os.name";
54 static final boolean usesSecurityManager;
57 if(
null == System.getSecurityManager() ) {
58 usesSecurityManager =
false;
59 System.err.println(
"No SecurityManager Installed");
61 usesSecurityManager =
true;
62 System.err.println(
"SecurityManager Already Installed");
67 public static void setup() throws IOException {
71 static void testPropImpl01(
final String propKey,
boolean isSecure) {
72 isSecure |= !usesSecurityManager;
76 final String p0 = System.getProperty(propKey);
77 System.err.println(propKey+
": "+p0);
78 }
catch (
final AccessControlException e) {
81 System.err.println(
"Expected exception for insecure property <"+propKey+
">");
82 System.err.println(
"Message: "+se0.getMessage());
84 System.err.println(
"Unexpected exception for secure property <"+propKey+
">");
85 se0.printStackTrace();
89 Assert.assertNull(
"AccessControlException thrown on secure property <"+propKey+
">", se0);
91 Assert.assertNotNull(
"AccessControlException not thrown on insecure property <"+propKey+
">", se0);
97 testPropImpl01(os_name_propkey,
true);
102 testPropImpl01(java_home_propkey,
false);
107 testPropImpl01(java_io_tmpdir_propkey,
false);
110 static void testTempDirImpl(
boolean isSecure) {
111 isSecure |= !usesSecurityManager;
113 Exception se0 =
null;
116 System.err.println(
"Temp: "+tmp);
117 }
catch (
final AccessControlException e) {
120 System.err.println(
"Expected exception for insecure temp dir");
121 System.err.println(
"Message: "+se0.getMessage());
123 System.err.println(
"Unexpected exception for secure temp dir");
124 se0.printStackTrace();
126 }
catch (
final SecurityException e) {
129 System.err.println(
"Expected exception for insecure temp dir (2)");
130 System.err.println(
"Message: "+se0.getMessage());
132 System.err.println(
"Unexpected exception for secure temp dir (2)");
133 se0.printStackTrace();
135 }
catch (
final IOException e) {
136 throw new RuntimeException(e);
139 Assert.assertNull(
"AccessControlException thrown on secure temp dir", se0);
141 Assert.assertNotNull(
"AccessControlException not thrown on insecure temp dir", se0);
147 testTempDirImpl(
false);
150 private NativeLibrary openLibraryImpl(
final boolean global)
throws URISyntaxException {
151 final ClassLoader cl = getClass().getClassLoader();
152 System.err.println(
"CL "+cl);
154 String libBaseName =
null;
155 final Class<?> clazz = this.getClass();
158 libUri =
Uri.
valueOf(clazz.getResource(
"/libtest1.so"));
159 }
catch (
final URISyntaxException e2) {
162 if(
null != libUri ) {
163 libBaseName =
"libtest1.so";
166 libUri = Uri.
valueOf(clazz.getResource(
"/test1.dll"));
167 if(
null != libUri ) {
168 libBaseName =
"test1.dll";
170 }
catch (
final URISyntaxException e) {
174 System.err.println(
"Untrusted Library (URL): "+libUri);
176 if(
null != libUri ) {
178 System.err.println(
"libDir1.1: "+libDir1);
180 System.err.println(
"libDir1.2: "+libDir1);
181 System.err.println(
"Untrusted Library Dir1 (abs): "+libDir1);
182 final Uri absLib = libDir1.
concat(Uri.Encoded.cast(
"natives/" + libBaseName));
183 Exception se0 =
null;
184 NativeLibrary nlib =
null;
186 nlib = NativeLibrary.open(absLib.toFile().getPath(),
true,
true, cl,
true);
187 System.err.println(
"NativeLibrary: "+nlib);
188 }
catch (
final SecurityException e) {
190 if( usesSecurityManager ) {
191 System.err.println(
"Expected exception for loading native library");
192 System.err.println(
"Message: "+se0.getMessage());
194 System.err.println(
"Unexpected exception for loading native library");
195 se0.printStackTrace();
198 if( !usesSecurityManager ) {
199 Assert.assertNull(
"SecurityException thrown on loading native library", se0);
201 Assert.assertNotNull(
"SecurityException not thrown on loading native library", se0);
205 System.err.println(
"No library found");
218 public static void main(
final String args[])
throws IOException, URISyntaxException {
This class implements an immutable Uri as defined by RFC 2396.
static Uri valueOf(final File file)
Creates a new Uri instance using the given File instance.
final Uri getContainedUri()
If this instance's schemeSpecificPart contains a Uri itself, a sub-Uri, return schemeSpecificPart + #...
final Uri concat(final Encoded suffix)
Concatenates the given encoded string to the encoded uri of this instance and returns a new Uri insta...
final Uri getParent()
Returns this Uri's parent directory Uri.
Provides low-level, relatively platform-independent access to shared ("native") libraries.
final void close()
Closes this native library.
static File getTempDir(final boolean executable)
Returns a platform independent writable directory for temporary files consisting of the platform's te...
static void main(final String args[])