1#!/bin/ksh -p
2# Copyright (c) 2005, 2017, 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
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   exit $status
61 } #end of fail()
62
63#Call this from anywhere to pass the test with a message
64# usage: pass "reason why the test passed if applicable"
65pass()
66 { echo "The test passed!!!"
67   echo "$*" 1>&2
68   exit 0
69 } #end of pass()
70
71# end of subroutines
72
73
74# The beginning of the script proper
75
76# Checking for proper OS
77OS=`uname -s`
78case "$OS" in
79   SunOS | Linux | Darwin )
80      FILESEP="/"
81      PATHSEP=":"
82      TMP=`cd /tmp; pwd -P`
83      ;;
84
85   Windows* )
86      FILESEP="\\"
87      PATHSEP=";"
88      TMP=`cd "${SystemRoot}/Temp"; echo ${PWD}`
89      ;;
90
91   CYGWIN* )
92      FILESEP="/"
93      PATHSEP=";"
94      TMP="/tmp"
95      ;;
96
97   # catch all other OSs
98   * )
99      echo "Unrecognized system!  $OS"
100      fail "Unrecognized system!  $OS"
101      ;;
102esac
103
104# Want this test to run standalone as well as in the harness, so do the
105#  following to copy the test's directory into the harness's scratch directory
106#  and set all appropriate variables:
107
108if [ -z "${TESTJAVA}" ] ; then
109   # TESTJAVA is not set, so the test is running stand-alone.
110   # TESTJAVA holds the path to the root directory of the build of the JDK
111   # to be tested.  That is, any java files run explicitly in this shell
112   # should use TESTJAVA in the path to the java interpreter.
113   # So, we'll set this to the JDK spec'd on the command line.  If none
114   # is given on the command line, tell the user that and use a cheesy
115   # default.
116   # THIS IS THE JDK BEING TESTED.
117   if [ -n "$1" ] ;
118      then TESTJAVA=$1
119      else fail "no JDK specified on command line!"
120   fi
121   TESTSRC=.
122   TESTCLASSES=.
123   STANDALONE=1;
124fi
125echo "JDK under test is: $TESTJAVA"
126
127#Deal with .class files:
128if [ -n "${STANDALONE}" ] ;
129   then
130   #if standalone, remind user to cd to dir. containing test before running it
131   echo "Just a reminder: cd to the dir containing this test when running it"
132   # then compile all .java files (if there are any) into .class files
133   if [ -a *.java ] ;
134      then echo "Reminder, this test should be in its own directory with all"
135      echo "supporting files it needs in the directory with it."
136      ${COMPILEJAVA}/bin/javac ./*.java ;
137   fi
138   # else in harness so copy all the class files from where jtreg put them
139   # over to the scratch directory this test is running in.
140   else cp ${TESTCLASSES}/*.class . ;
141fi
142
143#if in test harness, then copy the entire directory that the test is in over
144# to the scratch directory.  This catches any support files needed by the test.
145if [ -z "${STANDALONE}" ] ;
146   then cp ${TESTSRC}/*.java .
147fi
148
149#Just before executing anything, make sure it has executable permission!
150chmod 777 ./*
151
152###############  YOUR TEST CODE HERE!!!!!!!  #############
153
154#All files required for the test should be in the same directory with
155# this file.  If converting a standalone test to run with the harness,
156# as long as all files are in the same directory and it returns 0 for
157# pass, you should be able to cut and paste it into here and it will
158# run with the test harness.
159
160# This is an example of running something -- test
161# The stuff below catches the exit status of test then passes or fails
162# this shell test as appropriate ( 0 status is considered a pass here )
163
164echo
165echo ------ PREPARE TEST PLUGIN ---------
166
167# note that we can not use some subdirectory of the
168# scratch dir as the plugin dst dir because the test
169# app have file read permission for all subdirs of the
170# scratch dir
171
172PLUGINDST_DIR=${TMP}/test_ext
173#PLUGINDST_DIR=${TESTJAVA}/lib/ext
174TEST_PLUGIN=dummy.jar
175
176if [ ! -d ${PLUGINDST_DIR} ] ; then
177    mkdir ${PLUGINDST_DIR}
178fi
179
180# remove old service declaration
181if [ -d META-INF ] ; then
182    rm -rf META-INF
183fi
184
185# generate the service declaration
186if [ ! -d META_INF ] ; then
187     mkdir META-INF
188     mkdir META-INF/services
189fi
190
191# add wrong record to the service configuration
192echo "BadReaderPluginSpi" >  META-INF/services/javax.imageio.spi.ImageReaderSpi
193
194echo "DummyReaderPluginSpi" >> META-INF/services/javax.imageio.spi.ImageReaderSpi
195
196
197${TESTJAVA}/bin/jar -cvf ${TEST_PLUGIN} DummyReaderPluginSpi*.class META-INF/services/javax.imageio.spi.ImageReaderSpi
198
199echo ----- TEST PLUGIN IS READY --------
200echo
201echo ----- INSTALL PLUGIN --------
202echo "Install test plugin to ${PLUGINDST_DIR}"
203if [ -f ${PLUGINDST_DIR}/${TEST_PLUGIN} ] ; then
204    echo "Remove old plugin..."
205    rm -f ${PLUGINDST_DIR}/${TEST_PLUGIN}
206fi
207mv -f ${TEST_PLUGIN} ${PLUGINDST_DIR}
208if [ -f ${PLUGINDST_DIR}/${TEST_PLUGIN} ] ; then
209    echo Test plugin is installed.
210else
211    fail "Unable to install test plugin to $PLUGINDST_DIR"
212fi
213echo ----- PLUGIN IS INSTALLED ------
214echo
215echo ----- CLEAN PLUGIN TEMPORARY FILES -----
216rm -rf DummyReaderPluginSpi*.class META-INF
217echo ----- CLEANING IS COMPLETE -------
218echo
219
220
221case "$OS" in
222   CYGWIN* )
223      TEST_CODEBASE=$(cygpath -m ${PWD})
224      TEST_PLUGIN_JAR=$(cygpath -m ${PLUGINDST_DIR}${FILESEP}${TEST_PLUGIN})
225      ;;
226
227   # catch all other OSs
228   * )
229      TEST_CODEBASE=${PWD}
230      TEST_PLUGIN_JAR=${PLUGINDST_DIR}${FILESEP}${TEST_PLUGIN}
231      ;;
232esac
233
234
235# Update policy file to grant read permission
236echo "grant codeBase \"file:${TEST_CODEBASE}\" {" > classpath.policy
237echo " permission java.io.FilePermission \"${TEST_PLUGIN_JAR}\", \"read\";" >> classpath.policy
238echo " permission java.util.PropertyPermission \"test.5076692.property\", \"read\";" >> classpath.policy
239echo "};" >> classpath.policy
240echo "grant codeBase \"file:${TEST_PLUGIN_JAR}\" {" >> classpath.policy
241echo " permission java.util.PropertyPermission \"test.5076692.property\", \"read\";" >> classpath.policy
242echo "};" >> classpath.policy
243
244echo ---------------------
245echo --- Applet policy ---
246echo ---------------------
247cat classpath.policy
248echo ---------------------
249echo
250
251echo -------------------------------
252echo ---  Applet Classpath Test  ---
253echo -------------------------------
254#
255# please note that we need to use "==" in setup of the java.security.policy
256# property in order to overwrite policies defined in the user policy file
257# For more details see:
258#  http://java.sun.com/j2se/1.5.0/docs/guide/security/PolicyFiles.html)
259#
260
261${TESTJAVA}/bin/java ${TESTVMOPTS} -cp ".${PATHSEP}${TEST_PLUGIN_JAR}" \
262    -Djava.security.policy==classpath.policy \
263    -Djava.security.manager IIOPluginTest
264
265status=$?
266
267if [ $status -eq "0" ] ; then
268    pass ""
269else
270    fail "Test failed due to test plugin was not found."
271fi
272
273