1#
2# Copyright (c) 2008, 2015, 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
24# @test
25# @bug 6274276
26# @summary JLI JAR manifest processing should ignore leading and trailing white space.
27# @author Daniel D. Daugherty
28#
29# @key intermittent
30# @run build ManifestTestApp ExampleForBootClassPath
31# @run shell/timeout=900 ManifestTest.sh
32#
33
34make_a_JAR() {
35    # version_line and premain_line are required
36    version_line="Manifest-Version: 1.0"
37    premain_line="Premain-Class: ${AGENT}"
38    boot_cp_line=""
39    expect_boot_cp_line="ExampleForBootClassPath was not loaded."
40    can_redef_line=""
41    expect_redef_line="isRedefineClassesSupported()=false"
42    can_retrans_line=""
43    expect_retrans_line="isRetransformClassesSupported()=false"
44    can_set_nmp_line=""
45    expect_set_nmp_line="isNativeMethodPrefixSupported()=false"
46    # some tests create directories with spaces in their name,
47    # explicitly delete these.
48    to_be_deleted=""
49
50    while [ $# != 0 ] ; do
51        case "$1" in
52        defaults)
53            # just use the defaults for the test
54            ;;
55
56        boot_cp_line1)
57            boot_cp_line="Boot-Class-Path: no_white_space"
58            expect_boot_cp_line="ExampleForBootClassPath was loaded."
59            mkdir -p no_white_space
60            cp -p $OUT_OF_THE_WAY/ExampleForBootClassPath.class no_white_space
61            ;;
62
63        boot_cp_line2)
64            boot_cp_line="Boot-Class-Path:  has_leading_blank"
65            expect_boot_cp_line="ExampleForBootClassPath was loaded."
66            to_be_deleted=" has_leading_blank"
67            mkdir -p has_leading_blank " has_leading_blank"
68            # the good class is in the directory without the blank
69            cp -p $OUT_OF_THE_WAY/ExampleForBootClassPath.class \
70                has_leading_blank
71            # the bad class is in the directory with the blank
72            cp -p $OUT_OF_THE_WAY/ExampleForBootClassPath.class.bad \
73                " has_leading_blank/ExampleForBootClassPath.class"
74            ;;
75
76        boot_cp_line3)
77            boot_cp_line="Boot-Class-Path: has_trailing_blank "
78            expect_boot_cp_line="ExampleForBootClassPath was loaded."
79            to_be_deleted="has_trailing_blank "
80            mkdir -p has_trailing_blank "has_trailing_blank "
81            # the good class is in the directory without the blank
82            cp -p $OUT_OF_THE_WAY/ExampleForBootClassPath.class \
83                has_trailing_blank
84            # the bad class is in the directory with the blank
85            cp -p $OUT_OF_THE_WAY/ExampleForBootClassPath.class.bad \
86                "has_trailing_blank /ExampleForBootClassPath.class"
87            ;;
88
89        boot_cp_line4)
90            boot_cp_line="Boot-Class-Path:  has_leading_and_trailing_blank "
91            expect_boot_cp_line="ExampleForBootClassPath was loaded."
92            to_be_deleted=" has_leading_and_trailing_blank "
93            mkdir -p has_leading_and_trailing_blank \
94                " has_leading_and_trailing_blank "
95            # the good class is in the directory without the blanks
96            cp -p $OUT_OF_THE_WAY/ExampleForBootClassPath.class \
97                has_leading_and_trailing_blank
98            # the bad class is in the directory with the blanks
99            cp -p $OUT_OF_THE_WAY/ExampleForBootClassPath.class.bad \
100                " has_leading_and_trailing_blank /ExampleForBootClassPath.class"
101            ;;
102
103        boot_cp_line5)
104            boot_cp_line="Boot-Class-Path: has_embedded blank"
105            expect_boot_cp_line="ExampleForBootClassPath was loaded."
106            to_be_deleted="has_embedded blank"
107            mkdir -p has_embedded "has_embedded blank"
108            # the good class is in the first blank separated word
109            cp -p $OUT_OF_THE_WAY/ExampleForBootClassPath.class has_embedded
110            # the bad class is in the directory with the blank
111            cp -p $OUT_OF_THE_WAY/ExampleForBootClassPath.class.bad \
112                "has_embedded blank/ExampleForBootClassPath.class"
113            ;;
114
115        can_redef_line1)
116            can_redef_line="Can-Redefine-Classes: true"
117            expect_redef_line="isRedefineClassesSupported()=true"
118            ;;
119
120        can_redef_line2)
121            can_redef_line="Can-Redefine-Classes:  true"
122            expect_redef_line="isRedefineClassesSupported()=true"
123            ;;
124
125        can_redef_line3)
126            can_redef_line="Can-Redefine-Classes: true "
127            expect_redef_line="isRedefineClassesSupported()=true"
128            ;;
129
130        can_redef_line4)
131            can_redef_line="Can-Redefine-Classes:  true "
132            expect_redef_line="isRedefineClassesSupported()=true"
133            ;;
134
135        can_redef_line5)
136            can_redef_line="Can-Redefine-Classes: false"
137            ;;
138
139        can_redef_line6)
140            can_redef_line="Can-Redefine-Classes:  false"
141            ;;
142
143        can_redef_line7)
144            can_redef_line="Can-Redefine-Classes: false "
145            ;;
146
147        can_redef_line8)
148            can_redef_line="Can-Redefine-Classes:  false "
149            ;;
150
151        can_redef_line9)
152            # this line makes the jar command unhappy and that's
153            # not what we're testing so skip this case
154            can_redef_line="Can-Redefine-Classes:"
155            ;;
156
157        can_redef_line10)
158            can_redef_line="Can-Redefine-Classes: "
159            ;;
160
161        can_redef_line11)
162            can_redef_line="Can-Redefine-Classes:  "
163            ;;
164
165        can_retrans_line1)
166            can_retrans_line="Can-Retransform-Classes: true"
167            expect_retrans_line="isRetransformClassesSupported()=true"
168            ;;
169
170        can_retrans_line2)
171            can_retrans_line="Can-Retransform-Classes:  true"
172            expect_retrans_line="isRetransformClassesSupported()=true"
173            ;;
174
175        can_retrans_line3)
176            can_retrans_line="Can-Retransform-Classes: true "
177            expect_retrans_line="isRetransformClassesSupported()=true"
178            ;;
179
180        can_retrans_line4)
181            can_retrans_line="Can-Retransform-Classes:  true "
182            expect_retrans_line="isRetransformClassesSupported()=true"
183            ;;
184
185        can_retrans_line5)
186            can_retrans_line="Can-Retransform-Classes: false"
187            ;;
188
189        can_retrans_line6)
190            can_retrans_line="Can-Retransform-Classes:  false"
191            ;;
192
193        can_retrans_line7)
194            can_retrans_line="Can-Retransform-Classes: false "
195            ;;
196
197        can_retrans_line8)
198            can_retrans_line="Can-Retransform-Classes:  false "
199            ;;
200
201        can_retrans_line9)
202            # this line makes the jar command unhappy and that's
203            # not what we're testing so skip this case
204            can_retrans_line="Can-Retransform-Classes:"
205            ;;
206
207        can_retrans_line10)
208            can_retrans_line="Can-Retransform-Classes: "
209            ;;
210
211        can_retrans_line11)
212            can_retrans_line="Can-Retransform-Classes:  "
213            ;;
214
215        can_set_nmp_line1)
216            can_set_nmp_line="Can-Set-Native-Method-Prefix: true"
217            expect_set_nmp_line="isNativeMethodPrefixSupported()=true"
218            ;;
219
220        can_set_nmp_line2)
221            can_set_nmp_line="Can-Set-Native-Method-Prefix:  true"
222            expect_set_nmp_line="isNativeMethodPrefixSupported()=true"
223            ;;
224
225        can_set_nmp_line3)
226            can_set_nmp_line="Can-Set-Native-Method-Prefix: true "
227            expect_set_nmp_line="isNativeMethodPrefixSupported()=true"
228            ;;
229
230        can_set_nmp_line4)
231            can_set_nmp_line="Can-Set-Native-Method-Prefix:  true "
232            expect_set_nmp_line="isNativeMethodPrefixSupported()=true"
233            ;;
234
235        can_set_nmp_line5)
236            can_set_nmp_line="Can-Set-Native-Method-Prefix: false"
237            ;;
238
239        can_set_nmp_line6)
240            can_set_nmp_line="Can-Set-Native-Method-Prefix:  false"
241            ;;
242
243        can_set_nmp_line7)
244            can_set_nmp_line="Can-Set-Native-Method-Prefix: false "
245            ;;
246
247        can_set_nmp_line8)
248            can_set_nmp_line="Can-Set-Native-Method-Prefix:  false "
249            ;;
250
251        can_set_nmp_line9)
252            # this line makes the jar command unhappy and that's
253            # not what we're testing so skip this case
254            can_set_nmp_line="Can-Set-Native-Method-Prefix:"
255            ;;
256
257        can_set_nmp_line10)
258            can_set_nmp_line="Can-Set-Native-Method-Prefix: "
259            ;;
260
261        can_set_nmp_line11)
262            can_set_nmp_line="Can-Set-Native-Method-Prefix:  "
263            ;;
264
265        premain_line1)
266            premain_line="Premain-Class:  ${AGENT}"
267            ;;
268
269        premain_line2)
270            premain_line="Premain-Class: ${AGENT} "
271            ;;
272
273        premain_line3)
274            premain_line="Premain-Class:  ${AGENT} "
275            ;;
276
277        version_line1)
278            version_line="Manifest-Version:  1.0"
279            ;;
280
281        version_line2)
282            version_line="Manifest-Version: 1.0 "
283            ;;
284
285        version_line3)
286            version_line="Manifest-Version:  1.0 "
287            ;;
288
289        *)
290            echo "ERROR: invalid test token"
291            exit 1
292        esac
293        shift
294    done
295
296    echo "${version_line}" >  ${AGENT}.mf
297    echo "${premain_line}" >> ${AGENT}.mf
298
299    if [ -n "$boot_cp_line" ]; then
300        echo "${boot_cp_line}" >> ${AGENT}.mf
301    fi
302
303    if [ -n "$can_redef_line" ]; then
304        echo "${can_redef_line}" >> ${AGENT}.mf
305    fi
306
307    if [ -n "$can_retrans_line" ]; then
308        echo "${can_retrans_line}" >> ${AGENT}.mf
309    fi
310
311    if [ -n "$can_set_nmp_line" ]; then
312        echo "${can_set_nmp_line}" >> ${AGENT}.mf
313    fi
314
315    rm -f ${AGENT}.jar
316    ${JAR} ${TESTTOOLVMOPTS} cvfm ${AGENT}.jar ${AGENT}.mf ${AGENT}.class
317
318    echo "$expect_boot_cp_line" > expect_boot_cp_line
319    echo "$expect_redef_line"   > expect_redef_line
320    echo "$expect_retrans_line" > expect_retrans_line
321    echo "$expect_set_nmp_line" > expect_set_nmp_line
322}
323
324if [ "${TESTJAVA}" = "" ]
325then
326  echo "TESTJAVA not set.  Test cannot execute.  Failed."
327  exit 1
328fi
329
330if [ "${COMPILEJAVA}" = "" ]
331then
332  COMPILEJAVA="${TESTJAVA}"
333fi
334echo "COMPILEJAVA=${COMPILEJAVA}"
335
336if [ "${TESTSRC}" = "" ]
337then
338  echo "TESTSRC not set.  Test cannot execute.  Failed."
339  exit 1
340fi
341
342if [ "${TESTCLASSES}" = "" ]
343then
344  echo "TESTCLASSES not set.  Test cannot execute.  Failed."
345  exit 1
346fi
347
348JAR="${COMPILEJAVA}/bin/jar"
349JAVAC="${COMPILEJAVA}"/bin/javac
350JAVA="${TESTJAVA}"/bin/java
351
352# Now that ManifestTestApp.class is built, we move
353# ExampleForBootClassPath.class so that it cannot be found
354# by default
355OUT_OF_THE_WAY=out_of_the_way
356mkdir $OUT_OF_THE_WAY
357mv "${TESTCLASSES}/ExampleForBootClassPath.class" $OUT_OF_THE_WAY
358
359# create a bad version of ExampleForBootClassPath.class
360# so we can tell when the wrong version is run
361sed 's/return 15/return 42/' "${TESTSRC}"/ExampleForBootClassPath.java \
362    > ExampleForBootClassPath.java
363"$JAVAC" ${TESTJAVACOPTS} ${TESTTOOLVMOPTS} ExampleForBootClassPath.java
364mv ExampleForBootClassPath.class \
365    $OUT_OF_THE_WAY/ExampleForBootClassPath.class.bad
366mv ExampleForBootClassPath.java \
367    $OUT_OF_THE_WAY/ExampleForBootClassPath.java.bad
368
369AGENT=ManifestTestAgent
370# We compile the agent in the working directory instead of with
371# a build task because we construct a different agent JAR file
372# for each test case.
373${JAVAC} ${TESTJAVACOPTS} ${TESTTOOLVMOPTS} -d . ${TESTSRC}/${AGENT}.java
374
375FAIL_MARKER=fail_marker
376rm -f $FAIL_MARKER
377
378while read token; do
379    echo
380    echo "===== begin test case: $token ====="
381    make_a_JAR "$token"
382
383    "${JAVA}" ${TESTVMOPTS} -javaagent:${AGENT}.jar \
384        -classpath "${TESTCLASSES}" ManifestTestApp > output.log 2>&1
385    result=$?
386
387    cat output.log
388
389    if [ "$result" = 0 ]; then
390        echo "PASS: ManifestTestApp exited with status of 0."
391    else
392        echo "FAIL: ManifestTestApp exited with status of $result"
393        touch $FAIL_MARKER
394    fi
395
396    MESG="Hello from ${AGENT}!"
397    grep -s "$MESG" output.log > /dev/null
398    result=$?
399    if [ "$result" = 0 ]; then
400        echo "PASS: found '$MESG' in the test output"
401    else
402        echo "FAIL: did NOT find '$MESG' in the test output"
403        touch $FAIL_MARKER
404    fi
405
406    MESG=`cat expect_boot_cp_line | tr -d '\n\r'`
407    grep -s "$MESG" output.log > /dev/null
408    result=$?
409    if [ "$result" = 0 ]; then
410        echo "PASS: found '$MESG' in the test output"
411    else
412        echo "FAIL: did NOT find '$MESG' in the test output"
413        touch $FAIL_MARKER
414    fi
415
416    MESG=`cat expect_redef_line | tr -d '\n\r'`
417    grep -s "$MESG" output.log > /dev/null
418    result=$?
419    if [ "$result" = 0 ]; then
420        echo "PASS: found '$MESG' in the test output"
421    else
422        echo "FAIL: did NOT find '$MESG' in the test output"
423        touch $FAIL_MARKER
424    fi
425
426    MESG=`cat expect_retrans_line | tr -d '\n\r'`
427    grep -s "$MESG" output.log > /dev/null
428    result=$?
429    if [ "$result" = 0 ]; then
430        echo "PASS: found '$MESG' in the test output"
431    else
432        echo "FAIL: did NOT find '$MESG' in the test output"
433        touch $FAIL_MARKER
434    fi
435
436    MESG=`cat expect_set_nmp_line | tr -d '\n\r'`
437    grep -s "$MESG" output.log > /dev/null
438    result=$?
439    if [ "$result" = 0 ]; then
440        echo "PASS: found '$MESG' in the test output"
441    else
442        echo "FAIL: did NOT find '$MESG' in the test output"
443        touch $FAIL_MARKER
444    fi
445
446    #clean up any problematic directories
447    if [ -n "$to_be_deleted" ]; then
448        echo "Test removing [$to_be_deleted]"
449        rm -rf "$to_be_deleted"
450    fi
451
452    echo "===== end test case: $token ====="
453    echo
454done << EOF
455defaults
456version_line1
457version_line2
458version_line3
459premain_line1
460premain_line2
461premain_line3
462boot_cp_line1
463boot_cp_line2
464boot_cp_line3
465boot_cp_line4
466boot_cp_line5
467can_redef_line1
468can_redef_line2
469can_redef_line3
470can_redef_line4
471can_redef_line5
472can_redef_line6
473can_redef_line7
474can_redef_line8
475can_redef_line10
476can_redef_line11
477can_retrans_line1
478can_retrans_line2
479can_retrans_line3
480can_retrans_line4
481can_retrans_line5
482can_retrans_line6
483can_retrans_line7
484can_retrans_line8
485can_retrans_line10
486can_retrans_line11
487can_set_nmp_line1
488can_set_nmp_line2
489can_set_nmp_line3
490can_set_nmp_line4
491can_set_nmp_line5
492can_set_nmp_line6
493can_set_nmp_line7
494can_set_nmp_line8
495can_set_nmp_line10
496can_set_nmp_line11
497EOF
498
499if [ -f $FAIL_MARKER ]; then
500    exit 1
501else
502    exit 0
503fi
504