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