GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
TestSecIOUtil01.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
29package com.jogamp.junit.sec;
30
31import java.net.URISyntaxException;
32import java.security.AccessControlException;
33import java.io.File;
34import java.io.IOException;
35
36import org.junit.Assert;
37import org.junit.BeforeClass;
38import org.junit.Test;
39
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;
45
46import org.junit.FixMethodOrder;
47import org.junit.runners.MethodSorters;
48
49@FixMethodOrder(MethodSorters.NAME_ASCENDING)
50public class TestSecIOUtil01 extends SingletonJunitCase {
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;
55
56 static {
57 if( null == System.getSecurityManager() ) {
58 usesSecurityManager = false;
59 System.err.println("No SecurityManager Installed");
60 } else {
61 usesSecurityManager = true;
62 System.err.println("SecurityManager Already Installed");
63 }
64 }
65
66 @BeforeClass
67 public static void setup() throws IOException {
69 }
70
71 static void testPropImpl01(final String propKey, boolean isSecure) {
72 isSecure |= !usesSecurityManager;
73
74 Exception se0 = null;
75 try {
76 final String p0 = System.getProperty(propKey);
77 System.err.println(propKey+": "+p0);
78 } catch (final AccessControlException e) {
79 se0 = e;
80 if( !isSecure ) {
81 System.err.println("Expected exception for insecure property <"+propKey+">");
82 System.err.println("Message: "+se0.getMessage());
83 } else {
84 System.err.println("Unexpected exception for secure property <"+propKey+">");
85 se0.printStackTrace();
86 }
87 }
88 if( isSecure ) {
89 Assert.assertNull("AccessControlException thrown on secure property <"+propKey+">", se0);
90 } else {
91 Assert.assertNotNull("AccessControlException not thrown on insecure property <"+propKey+">", se0);
92 }
93 }
94
95 @Test
96 public void testProp00_Temp() {
97 testPropImpl01(os_name_propkey, true);
98 }
99
100 @Test
101 public void testProp01_Temp() {
102 testPropImpl01(java_home_propkey, false);
103 }
104
105 @Test
106 public void testProp02_Temp() {
107 testPropImpl01(java_io_tmpdir_propkey, false);
108 }
109
110 static void testTempDirImpl(boolean isSecure) {
111 isSecure |= !usesSecurityManager;
112
113 Exception se0 = null;
114 try {
115 final File tmp = IOUtil.getTempDir(true);
116 System.err.println("Temp: "+tmp);
117 } catch (final AccessControlException e) {
118 se0 = e;
119 if( !isSecure ) {
120 System.err.println("Expected exception for insecure temp dir");
121 System.err.println("Message: "+se0.getMessage());
122 } else {
123 System.err.println("Unexpected exception for secure temp dir");
124 se0.printStackTrace();
125 }
126 } catch (final SecurityException e) {
127 se0 = e;
128 if( !isSecure ) {
129 System.err.println("Expected exception for insecure temp dir (2)");
130 System.err.println("Message: "+se0.getMessage());
131 } else {
132 System.err.println("Unexpected exception for secure temp dir (2)");
133 se0.printStackTrace();
134 }
135 } catch (final IOException e) {
136 throw new RuntimeException(e); // oops
137 }
138 if( isSecure ) {
139 Assert.assertNull("AccessControlException thrown on secure temp dir", se0);
140 } else {
141 Assert.assertNotNull("AccessControlException not thrown on insecure temp dir", se0);
142 }
143 }
144
145 @Test
146 public void testTempDir00() {
147 testTempDirImpl(false);
148 }
149
150 private NativeLibrary openLibraryImpl(final boolean global) throws URISyntaxException {
151 final ClassLoader cl = getClass().getClassLoader();
152 System.err.println("CL "+cl);
153
154 String libBaseName = null;
155 final Class<?> clazz = this.getClass();
156 Uri libUri = null;
157 try {
158 libUri = Uri.valueOf(clazz.getResource("/libtest1.so"));
159 } catch (final URISyntaxException e2) {
160 // not found .. OK
161 }
162 if( null != libUri ) {
163 libBaseName = "libtest1.so";
164 } else {
165 try {
166 libUri = Uri.valueOf(clazz.getResource("/test1.dll"));
167 if( null != libUri ) {
168 libBaseName = "test1.dll";
169 }
170 } catch (final URISyntaxException e) {
171 // not found
172 }
173 }
174 System.err.println("Untrusted Library (URL): "+libUri);
175
176 if( null != libUri ) {
177 Uri libDir1 = libUri.getContainedUri();
178 System.err.println("libDir1.1: "+libDir1);
179 libDir1= libDir1.getParent();
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;
185 try {
186 nlib = NativeLibrary.open(absLib.toFile().getPath(), true, true, cl, true);
187 System.err.println("NativeLibrary: "+nlib);
188 } catch (final SecurityException e) {
189 se0 = e;
190 if( usesSecurityManager ) {
191 System.err.println("Expected exception for loading native library");
192 System.err.println("Message: "+se0.getMessage());
193 } else {
194 System.err.println("Unexpected exception for loading native library");
195 se0.printStackTrace();
196 }
197 }
198 if( !usesSecurityManager ) {
199 Assert.assertNull("SecurityException thrown on loading native library", se0);
200 } else {
201 Assert.assertNotNull("SecurityException not thrown on loading native library", se0);
202 }
203 return nlib;
204 } else {
205 System.err.println("No library found");
206 return null;
207 }
208
209 }
210
211 public void testOpenLibrary() throws URISyntaxException {
212 final NativeLibrary nlib = openLibraryImpl(true);
213 if( null != nlib ) {
214 nlib.close();
215 }
216 }
217
218 public static void main(final String args[]) throws IOException, URISyntaxException {
220
221 final TestSecIOUtil01 aa = new TestSecIOUtil01();
222 aa.testProp00_Temp();
223 aa.testProp01_Temp();
224 aa.testProp02_Temp();
225 aa.testTempDir00();
226 aa.testOpenLibrary();
227 }
228
229}
This class implements an immutable Uri as defined by RFC 2396.
Definition: Uri.java:160
static Uri valueOf(final File file)
Creates a new Uri instance using the given File instance.
Definition: Uri.java:1121
final Uri getContainedUri()
If this instance's schemeSpecificPart contains a Uri itself, a sub-Uri, return schemeSpecificPart + #...
Definition: Uri.java:1437
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
final Uri getParent()
Returns this Uri's parent directory Uri.
Definition: Uri.java:1664
Provides low-level, relatively platform-independent access to shared ("native") libraries.
final void close()
Closes this native library.
Utility class for querying platform specific properties.
Definition: Platform.java:58
static void initSingleton()
kick off static initialization of platform property information and native gluegen_rt lib loading
Definition: Platform.java:359
static File getTempDir(final boolean executable)
Returns a platform independent writable directory for temporary files consisting of the platform's te...
Definition: IOUtil.java:1246
static void main(final String args[])