GlueGen: src/junit/com/jogamp/junit/sec/Applet01.java Source File

GlueGen v2.6.0 GlueGen, Native Binding Generator for Java™ (public API).

Applet01.java
Go to the documentation of this file.
1/**
2 * Copyright 2013 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28 package com.jogamp.junit.sec;
29
30import java.applet.Applet;
31import java.io.BufferedOutputStream;
32import java.io.File;
33import java.io.FileOutputStream;
34import java.io.IOException;
35import java.io.OutputStream;
36import java.net.URISyntaxException;
37import java.security.AccessControlException;
38
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;
44
45/**
46 * Applet: Provoke AccessControlException while writing to file!
47 */
48@SuppressWarnings("serial")
49public class Applet01 extends Applet {
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";
53
54 static final String tfilename = "test.bin" ;
55 static final MachineDataInfo machine = Platform.getMachineDataInfo();
56 static final int tsz = machine.pageSizeInBytes();
57
58 static final boolean usesSecurityManager;
59
60 static {
61 if( null == System.getSecurityManager() ) {
62 usesSecurityManager = false;
63 System.err.println("No SecurityManager Installed");
64 } else {
65 usesSecurityManager = true;
66 System.err.println("SecurityManager Already Installed");
67 }
68 }
69
70 static void testPropImpl(final String propKey, boolean isSecure) {
71 isSecure |= !usesSecurityManager;
72
73 Exception se0 = null;
74 try {
75 final String p0 = System.getProperty(propKey);
76 System.err.println(propKey+": "+p0);
77 } catch (final AccessControlException e) {
78 se0 = e;
79 if( !isSecure ) {
80 System.err.println("Expected exception for insecure property <"+propKey+">");
81 System.err.println("Message: "+se0.getMessage());
82 } else {
83 System.err.println("Unexpected exception for secure property <"+propKey+">");
84 se0.printStackTrace();
85 }
86 }
87 if( isSecure ) {
88 if( null != se0 ) {
89 throw new Error("AccessControlException thrown on secure property <"+propKey+">", se0);
90 }
91 } else {
92 if( null == se0 ) {
93 throw new Error("AccessControlException not thrown on secure property <"+propKey+">");
94 }
95 }
96 }
97
98 static void testTempDirImpl(boolean isSecure) {
99 isSecure |= !usesSecurityManager;
100
101 Exception se0 = null;
102 try {
103 final File tmp = IOUtil.getTempDir(true);
104 System.err.println("Temp: "+tmp);
105 } catch (final AccessControlException e) {
106 se0 = e;
107 if( !isSecure ) {
108 System.err.println("Expected exception for insecure temp dir");
109 System.err.println("Message: "+se0.getMessage());
110 } else {
111 System.err.println("Unexpected exception for secure temp dir");
112 se0.printStackTrace();
113 }
114 } catch (final SecurityException e) {
115 se0 = e;
116 if( !isSecure ) {
117 System.err.println("Expected exception for insecure temp dir (2)");
118 System.err.println("Message: "+se0.getMessage());
119 } else {
120 System.err.println("Unexpected exception for secure temp dir (2)");
121 se0.printStackTrace();
122 }
123 } catch (final IOException e) {
124 throw new RuntimeException(e); // oops
125 }
126 if( isSecure ) {
127 if( null != se0 ) {
128 throw new Error("AccessControlException thrown on secure temp dir", se0);
129 }
130 } else {
131 if( null == se0 ) {
132 throw new Error("AccessControlException not thrown on secure temp dir");
133 }
134 }
135 }
136
137 private void testWriteFile() {
138 AccessControlException sec01 = null;
139 try {
140 final File tmp = IOUtil.getTempDir(true);
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);
147 orig[i] = b;
148 tout.write(b);
149 }
150 tout.close();
151 } catch (final IOException ioe) {
152 ioe.printStackTrace();
153 } catch (final AccessControlException ace) {
154 // GOOD!
155 sec01 = ace;
156 System.err.println("Expected:"+ace.getMessage());
157 }
158 if( !usesSecurityManager ) {
159 if( null != sec01 ) {
160 throw new Error("SecurityException thrown on writing to temp", sec01);
161 }
162 } else {
163 if( null == sec01 ) {
164 throw new Error("SecurityException not thrown on writing to temp");
165 }
166 }
167 }
168
169 private void testOpenLibrary(final boolean global) throws URISyntaxException {
170 final ClassLoader cl = getClass().getClassLoader();
171 System.err.println("CL "+cl);
172
173 String libBaseName = null;
174 final Class<?> clazz = this.getClass();
175 Uri libUri = null;
176 try {
177 libUri = Uri.valueOf(clazz.getResource("/libtest1.so"));
178 } catch (final URISyntaxException e2) {
179 // not found .. OK
180 }
181 if( null != libUri ) {
182 libBaseName = "libtest1.so";
183 } else {
184 try {
185 libUri = Uri.valueOf(clazz.getResource("/test1.dll"));
186 if( null != libUri ) {
187 libBaseName = "test1.dll";
188 }
189 } catch (final URISyntaxException e) {
190 // not found
191 }
192 }
193 System.err.println("Untrusted Library (URL): "+libUri);
194
195 if( null != libUri ) {
196 Uri libDir1 = libUri.getContainedUri();
197 System.err.println("libDir1.1: "+libDir1);
198 libDir1= libDir1.getParent();
199 System.err.println("libDir1.2: "+libDir1);
200 System.err.println("Untrusted Library Dir1 (abs): "+libDir1);
201 final Uri absLib = libDir1.concat(Uri.Encoded.cast("natives/" + libBaseName));
202 Exception sec01 = null;
203 try {
204 final NativeLibrary nlib = NativeLibrary.open(absLib.toFile().getPath(), true, true, cl, true);
205 System.err.println("NativeLibrary: "+nlib);
206 } catch (final SecurityException e) {
207 sec01 = e;
208 if( usesSecurityManager ) {
209 System.err.println("Expected exception for loading native library");
210 System.err.println("Message: "+sec01.getMessage());
211 } else {
212 System.err.println("Unexpected exception for loading native library");
213 sec01.printStackTrace();
214 }
215 }
216 if( !usesSecurityManager ) {
217 if( null != sec01 ) {
218 throw new Error("SecurityException thrown on loading native library", sec01);
219 }
220 } else {
221 if( null == sec01 ) {
222 throw new Error("SecurityException not thrown on loading native library");
223 }
224 }
225 } else {
226 System.err.println("No library found");
227 }
228 }
229
230 public void init() {
231
232 }
233
234 public void start() {
235 Platform.initSingleton();
236
237 {
238 testPropImpl(os_name_propkey, true);
239 }
240 System.err.println("p0: OK");
241 {
242 testPropImpl(java_home_propkey, false);
243 }
244 System.err.println("p1: OK");
245 {
246 testPropImpl(java_io_tmpdir_propkey, false);
247 }
248 System.err.println("p2: OK");
249 {
250 testTempDirImpl(false);
251 }
252 System.err.println("temp0: OK");
253
254 testWriteFile();
255 System.err.println("writeFile: OK");
256
257 try {
258 testOpenLibrary(true);
259 } catch (final URISyntaxException e) {
260 e.printStackTrace();
261 }
262 System.err.println("lib0: OK");
263 }
264
265 public void stop() {
266
267 }
268}
com.jogamp.common.net.Uri.Encoded
Immutable RFC3986 encoded string.
Definition: Uri.java:296
com.jogamp.common.net.Uri.Encoded.cast
static Encoded cast(final String encoded)
Casts the given encoded String by creating a new Encoded instance.
Definition: Uri.java:305
com.jogamp.common.net.Uri
This class implements an immutable Uri as defined by RFC 2396.
Definition: Uri.java:160
com.jogamp.common.net.Uri.valueOf
static Uri valueOf(final File file)
Creates a new Uri instance using the given File instance.
Definition: Uri.java:1121
com.jogamp.common.net.Uri.toFile
final File toFile()
If this instance is a file scheme, implementation decodes [ "//"+authority ] + path,...
Definition: Uri.java:1390
com.jogamp.common.net.Uri.getContainedUri
final Uri getContainedUri()
If this instance's schemeSpecificPart contains a Uri itself, a sub-Uri, return schemeSpecificPart + #...
Definition: Uri.java:1437
com.jogamp.common.net.Uri.concat
final Uri concat(final Encoded suffix)
Concatenates the given encoded string to the encoded uri of this instance and returns a new Uri insta...
Definition: Uri.java:1725
com.jogamp.common.net.Uri.getParent
final Uri getParent()
Returns this Uri's parent directory Uri.
Definition: Uri.java:1664
com.jogamp.common.os.MachineDataInfo
Machine data description for alignment and size onle, see com.jogamp.gluegen.
Definition: MachineDataInfo.java:57
com.jogamp.common.os.NativeLibrary
Provides low-level, relatively platform-independent access to shared ("native") libraries.
Definition: NativeLibrary.java:112
com.jogamp.common.os.NativeLibrary.open
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.
Definition: NativeLibrary.java:355
com.jogamp.common.os.Platform
Utility class for querying platform specific properties.
Definition: Platform.java:58
com.jogamp.common.os.Platform.initSingleton
static void initSingleton()
kick off static initialization of platform property information and native gluegen_rt lib loading
Definition: Platform.java:359
com.jogamp.common.os.Platform.getMachineDataInfo
static MachineDataInfo getMachineDataInfo()
Returns the MachineDataInfo of the running machine.
Definition: Platform.java:510
com.jogamp.common.util.IOUtil
Definition: IOUtil.java:69
com.jogamp.common.util.IOUtil.getTempDir
static File getTempDir(final boolean executable)
Returns a platform independent writable directory for temporary files consisting of the platform's te...
Definition: IOUtil.java:1275
com.jogamp.junit.sec.Applet01
Applet: Provoke AccessControlException while writing to file!
Definition: Applet01.java:49
com.jogamp.junit.sec.Applet01.stop
void stop()
Definition: Applet01.java:265
com.jogamp.junit.sec.Applet01.start
void start()
Definition: Applet01.java:234
com.jogamp.junit.sec.Applet01.init
void init()
Definition: Applet01.java:230