1#!/bin/ksh -p
2# Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
3# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4#
5# This code is free software; you can redistribute it and/or modify it
6# under the terms of the GNU General Public License version 2 only, as
7# published by the Free Software Foundation.
8#
9# This code is distributed in the hope that it will be useful, but WITHOUT
10# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12# version 2 for more details (a copy is included in the LICENSE file that
13# accompanied this code).
14#
15# You should have received a copy of the GNU General Public License version
16# 2 along with this work; if not, write to the Free Software Foundation,
17# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18#
19# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20# or visit www.oracle.com if you need additional information or have any
21# questions.
22#
23#   @test
24#
25#   @bug        6342404 7078379 8167503 8183351
26#
27#   @summary    Test verifies that incorrectly configured ImageIO plugin spi
28#               does not affect registration of other ImageIO plugin in the
29#               applet context.
30#
31#
32#   @compile    IIOPluginTest.java
33#   @compile    DummyReaderPluginSpi.java
34#   @run shell  BadPluginConfigurationTest.sh
35
36# There are several resources which need to be present before many
37#  shell scripts can run.  Following are examples of how to check for
38#  many common ones.
39#
40# Note that the shell used is the Korn Shell, KSH
41#
42# Also note, it is recommended that make files NOT be used.  Rather,
43#  put the individual commands directly into this file.  That way,
44#  it is possible to use command line arguments and other shell tech-
45#  niques to find the compiler, etc on different systems.  For example,
46#  a different path could be used depending on whether this were a
47#  Solaris or Win32 machine, which is more difficult (if even possible)
48#  in a make file.
49
50
51# Beginning of subroutines:
52status=1
53
54#Call this from anywhere to fail the test with an error message
55# usage: fail "reason why the test failed"
56fail()
57 { echo "The test failed :-("
58   echo "$*" 1>&2
59   echo "exit status was $status"
60   clean
61   exit $status
62 } #end of fail()
63
64#Call this from anywhere to pass the test with a message
65# usage: pass "reason why the test passed if applicable"
66pass()
67 { echo "The test passed!!!"
68   echo "$*" 1>&2
69   clean
70   exit 0
71 } #end of pass()
72
73#Clean up the test_ext directory (PLUGINDST_DIR) before leaving
74clean()
75 {
76 echo "Removing PLUGINDST_DIR ${PLUGINDST_DIR}"
77 if [ -n "${PLUGINDST_DIR}" -a -d "${PLUGINDST_DIR}" ] ; then
78 rm -rf "${PLUGINDST_DIR}"
79 fi
80 }
81
82# end of subroutines
83
84
85# The beginning of the script proper
86
87# Checking for proper OS
88OS=`uname -s`
89MKTEMP="mktemp"
90case "$OS" in
91   AIX )
92      FILESEP="/"
93      PATHSEP=":"
94      TMP=`cd /tmp; pwd -P`
95
96      type ${MKTEMP} > /dev/null 2>&1
97
98      if ! [ $? -ne 0 ] ; then
99        MKTEMP="/opt/freeware/bin/mktemp"
100      fi
101      if ! [ -e ${MKTEMP} ] ; then
102        pass "Test skipped because no mktemp found on this machine"
103      fi
104      ;;
105
106   Darwin | Linux | SunOS | *BSD )
107      FILESEP="/"
108      PATHSEP=":"
109      TMP=`cd /tmp; pwd -P`
110      ;;
111
112   Windows* )
113      FILESEP="\\"
114      PATHSEP=";"
115      TMP=`cd "${SystemRoot}/Temp"; echo ${PWD}`
116      ;;
117
118   CYGWIN* )
119      FILESEP="/"
120      PATHSEP=";"
121      TMP="/tmp"
122      ;;
123
124   # catch all other OSs
125   * )
126      echo "Unrecognized system!  $OS"
127      fail "Unrecognized system!  $OS"
128      ;;
129esac
130
131# Want this test to run standalone as well as in the harness, so do the
132#  following to copy the test's directory into the harness's scratch directory
133#  and set all appropriate variables:
134
135if [ -z "${TESTJAVA}" ] ; then
136   # TESTJAVA is not set, so the test is running stand-alone.
137   # TESTJAVA holds the path to the root directory of the build of the JDK
138   # to be tested.  That is, any java files run explicitly in this shell
139   # should use TESTJAVA in the path to the java interpreter.
140   # So, we'll set this to the JDK spec'd on the command line.  If none
141   # is given on the command line, tell the user that and use a cheesy
142   # default.
143   # THIS IS THE JDK BEING TESTED.
144   if [ -n "$1" ] ;
145      then TESTJAVA=$1
146      else fail "no JDK specified on command line!"
147   fi
148   TESTSRC=.
149   TESTCLASSES=.
150   STANDALONE=1;
151fi
152echo "JDK under test is: $TESTJAVA"
153
154#Deal with .class files:
155if [ -n "${STANDALONE}" ] ;
156   then
157   #if standalone, remind user to cd to dir. containing test before running it
158   echo "Just a reminder: cd to the dir containing this test when running it"
159   # then compile all .java files (if there are any) into .class files
160   if [ -a *.java ] ;
161      then echo "Reminder, this test should be in its own directory with all"
162      echo "supporting files it needs in the directory with it."
163      ${COMPILEJAVA}/bin/javac ./*.java ;
164   fi
165   # else in harness so copy all the class files from where jtreg put them
166   # over to the scratch directory this test is running in.
167   else cp ${TESTCLASSES}/*.class . ;
168fi
169
170#if in test harness, then copy the entire directory that the test is in over
171# to the scratch directory.  This catches any support files needed by the test.
172if [ -z "${STANDALONE}" ] ;
173   then cp ${TESTSRC}/*.java .
174fi
175
176#Just before executing anything, make sure it has executable permission!
177chmod 777 ./*
178
179###############  YOUR TEST CODE HERE!!!!!!!  #############
180
181#All files required for the test should be in the same directory with
182# this file.  If converting a standalone test to run with the harness,
183# as long as all files are in the same directory and it returns 0 for
184# pass, you should be able to cut and paste it into here and it will
185# run with the test harness.
186
187# This is an example of running something -- test
188# The stuff below catches the exit status of test then passes or fails
189# this shell test as appropriate ( 0 status is considered a pass here )
190
191echo
192echo ------ PREPARE TEST PLUGIN ---------
193
194# note that we can not use some subdirectory of the
195# scratch dir as the plugin dst dir because the test
196# app have file read permission for all subdirs of the
197# scratch dir
198
199PLUGINDST_DIR=$(${MKTEMP} -d ${TMP}/iio_test.XXXXXXXX)
200echo "Created PLUGINDST_DIR as ${PLUGINDST_DIR}"
201
202TEST_PLUGIN=dummy.jar
203
204# remove old service declaration
205if [ -d META-INF ] ; then
206    rm -rf META-INF
207fi
208
209# generate the service declaration
210if [ ! -d META_INF ] ; then
211     mkdir META-INF
212     mkdir META-INF/services
213fi
214
215# add wrong record to the service configuration
216echo "BadReaderPluginSpi" >  META-INF/services/javax.imageio.spi.ImageReaderSpi
217
218echo "DummyReaderPluginSpi" >> META-INF/services/javax.imageio.spi.ImageReaderSpi
219
220
221${TESTJAVA}/bin/jar -cvf ${TEST_PLUGIN} DummyReaderPluginSpi*.class META-INF/services/javax.imageio.spi.ImageReaderSpi
222
223echo ----- TEST PLUGIN IS READY --------
224echo
225echo ----- INSTALL PLUGIN --------
226echo "Install test plugin to ${PLUGINDST_DIR}"
227if [ -f ${PLUGINDST_DIR}/${TEST_PLUGIN} ] ; then
228    echo "Remove old plugin..."
229    rm -f ${PLUGINDST_DIR}/${TEST_PLUGIN}
230fi
231mv -f ${TEST_PLUGIN} ${PLUGINDST_DIR}
232if [ -f ${PLUGINDST_DIR}/${TEST_PLUGIN} ] ; then
233    echo Test plugin is installed.
234else
235    fail "Unable to install test plugin to $PLUGINDST_DIR"
236fi
237echo ----- PLUGIN IS INSTALLED ------
238echo
239echo ----- CLEAN PLUGIN TEMPORARY FILES -----
240rm -rf DummyReaderPluginSpi*.class META-INF
241echo ----- CLEANING IS COMPLETE -------
242echo
243
244
245case "$OS" in
246   CYGWIN* )
247      TEST_CODEBASE=$(cygpath -m ${PWD})
248      TEST_PLUGIN_JAR=$(cygpath -m ${PLUGINDST_DIR}${FILESEP}${TEST_PLUGIN})
249      ;;
250
251   # catch all other OSs
252   * )
253      TEST_CODEBASE=${PWD}
254      TEST_PLUGIN_JAR=${PLUGINDST_DIR}${FILESEP}${TEST_PLUGIN}
255      ;;
256esac
257
258
259# Update policy file to grant read permission
260echo "grant codeBase \"file:${TEST_CODEBASE}\" {" > classpath.policy
261echo " permission java.io.FilePermission \"${TEST_PLUGIN_JAR}\", \"read\";" >> classpath.policy
262echo " permission java.util.PropertyPermission \"test.5076692.property\", \"read\";" >> classpath.policy
263echo "};" >> classpath.policy
264echo "grant codeBase \"file:${TEST_PLUGIN_JAR}\" {" >> classpath.policy
265echo " permission java.util.PropertyPermission \"test.5076692.property\", \"read\";" >> classpath.policy
266echo "};" >> classpath.policy
267
268echo ---------------------
269echo --- Applet policy ---
270echo ---------------------
271cat classpath.policy
272echo ---------------------
273echo
274
275echo -------------------------------
276echo ---  Applet Classpath Test  ---
277echo -------------------------------
278#
279# please note that we need to use "==" in setup of the java.security.policy
280# property in order to overwrite policies defined in the user policy file
281# For more details see:
282#  http://java.sun.com/j2se/1.5.0/docs/guide/security/PolicyFiles.html)
283#
284
285${TESTJAVA}/bin/java ${TESTVMOPTS} -cp ".${PATHSEP}${TEST_PLUGIN_JAR}" \
286    -Djava.security.policy==classpath.policy \
287    -Djava.security.manager IIOPluginTest
288
289status=$?
290
291if [ $status -eq "0" ] ; then
292    pass ""
293else
294    fail "Test failed due to test plugin was not found."
295fi
296
297