28package com.jogamp.junit.sec;
30import java.applet.Applet;
31import java.io.BufferedOutputStream;
33import java.io.FileOutputStream;
34import java.io.IOException;
35import java.io.OutputStream;
36import java.net.URISyntaxException;
37import java.security.AccessControlException;
39import com.jogamp.common.net.Uri;
40import com.jogamp.common.os.MachineDataInfo;
41import com.jogamp.common.os.NativeLibrary;
42import com.jogamp.common.os.Platform;
43import com.jogamp.common.util.IOUtil;
48@SuppressWarnings(
"serial")
50 static final String java_io_tmpdir_propkey =
"java.io.tmpdir";
51 static final String java_home_propkey =
"java.home";
52 static final String os_name_propkey =
"os.name";
54 static final String tfilename =
"test.bin" ;
56 static final int tsz = machine.pageSizeInBytes();
58 static final boolean usesSecurityManager;
61 if(
null == System.getSecurityManager() ) {
62 usesSecurityManager =
false;
63 System.err.println(
"No SecurityManager Installed");
65 usesSecurityManager =
true;
66 System.err.println(
"SecurityManager Already Installed");
70 static void testPropImpl(
final String propKey,
boolean isSecure) {
71 isSecure |= !usesSecurityManager;
75 final String p0 = System.getProperty(propKey);
76 System.err.println(propKey+
": "+p0);
77 }
catch (
final AccessControlException e) {
80 System.err.println(
"Expected exception for insecure property <"+propKey+
">");
81 System.err.println(
"Message: "+se0.getMessage());
83 System.err.println(
"Unexpected exception for secure property <"+propKey+
">");
84 se0.printStackTrace();
89 throw new Error(
"AccessControlException thrown on secure property <"+propKey+
">", se0);
93 throw new Error(
"AccessControlException not thrown on secure property <"+propKey+
">");
98 static void testTempDirImpl(
boolean isSecure) {
99 isSecure |= !usesSecurityManager;
101 Exception se0 =
null;
104 System.err.println(
"Temp: "+tmp);
105 }
catch (
final AccessControlException e) {
108 System.err.println(
"Expected exception for insecure temp dir");
109 System.err.println(
"Message: "+se0.getMessage());
111 System.err.println(
"Unexpected exception for secure temp dir");
112 se0.printStackTrace();
114 }
catch (
final SecurityException e) {
117 System.err.println(
"Expected exception for insecure temp dir (2)");
118 System.err.println(
"Message: "+se0.getMessage());
120 System.err.println(
"Unexpected exception for secure temp dir (2)");
121 se0.printStackTrace();
123 }
catch (
final IOException e) {
124 throw new RuntimeException(e);
128 throw new Error(
"AccessControlException thrown on secure temp dir", se0);
132 throw new Error(
"AccessControlException not thrown on secure temp dir");
137 private void testWriteFile() {
138 AccessControlException sec01 =
null;
141 System.err.println(
"Temp: "+tmp);
142 final byte[] orig =
new byte[tsz];
143 final File tfile =
new File(tmp, tfilename);
144 final OutputStream tout =
new BufferedOutputStream(
new FileOutputStream(tfile));
145 for(
int i=0; i<tsz; i++) {
146 final byte b = (byte) (i%256);
151 }
catch (
final IOException ioe) {
152 ioe.printStackTrace();
153 }
catch (
final AccessControlException ace) {
156 System.err.println(
"Expected:"+ace.getMessage());
158 if( !usesSecurityManager ) {
159 if(
null != sec01 ) {
160 throw new Error(
"SecurityException thrown on writing to temp", sec01);
163 if(
null == sec01 ) {
164 throw new Error(
"SecurityException not thrown on writing to temp");
169 private void testOpenLibrary(
final boolean global)
throws URISyntaxException {
170 final ClassLoader cl = getClass().getClassLoader();
171 System.err.println(
"CL "+cl);
173 String libBaseName =
null;
174 final Class<?> clazz = this.getClass();
177 libUri =
Uri.
valueOf(clazz.getResource(
"/libtest1.so"));
178 }
catch (
final URISyntaxException e2) {
181 if(
null != libUri ) {
182 libBaseName =
"libtest1.so";
185 libUri =
Uri.
valueOf(clazz.getResource(
"/test1.dll"));
186 if(
null != libUri ) {
187 libBaseName =
"test1.dll";
189 }
catch (
final URISyntaxException e) {
193 System.err.println(
"Untrusted Library (URL): "+libUri);
195 if(
null != libUri ) {
197 System.err.println(
"libDir1.1: "+libDir1);
199 System.err.println(
"libDir1.2: "+libDir1);
200 System.err.println(
"Untrusted Library Dir1 (abs): "+libDir1);
202 Exception sec01 =
null;
205 System.err.println(
"NativeLibrary: "+nlib);
206 }
catch (
final SecurityException e) {
208 if( usesSecurityManager ) {
209 System.err.println(
"Expected exception for loading native library");
210 System.err.println(
"Message: "+sec01.getMessage());
212 System.err.println(
"Unexpected exception for loading native library");
213 sec01.printStackTrace();
216 if( !usesSecurityManager ) {
217 if(
null != sec01 ) {
218 throw new Error(
"SecurityException thrown on loading native library", sec01);
221 if(
null == sec01 ) {
222 throw new Error(
"SecurityException not thrown on loading native library");
226 System.err.println(
"No library found");
238 testPropImpl(os_name_propkey,
true);
240 System.err.println(
"p0: OK");
242 testPropImpl(java_home_propkey,
false);
244 System.err.println(
"p1: OK");
246 testPropImpl(java_io_tmpdir_propkey,
false);
248 System.err.println(
"p2: OK");
250 testTempDirImpl(
false);
252 System.err.println(
"temp0: OK");
255 System.err.println(
"writeFile: OK");
258 testOpenLibrary(
true);
259 }
catch (
final URISyntaxException e) {
262 System.err.println(
"lib0: OK");
Immutable RFC3986 encoded string.
static Encoded cast(final String encoded)
Casts the given encoded String by creating a new Encoded instance.
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 File toFile()
If this instance is a file scheme, implementation decodes [ "//"+authority ] + path,...
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.
Machine data description for alignment and size onle, see com.jogamp.gluegen.
Provides low-level, relatively platform-independent access to shared ("native") libraries.
static final NativeLibrary open(final String libName, final boolean searchOSSystemPath, final boolean searchSystemPathFirst, final ClassLoader loader, final boolean global)
Opens the given native library, assuming it has the same base name on all platforms.
static File getTempDir(final boolean executable)
Returns a platform independent writable directory for temporary files consisting of the platform's te...
Applet: Provoke AccessControlException while writing to file!