1 /*
   2  * Copyright (c) 2016, 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 /**
  25  * JDK-8144113: Nashorn: enable jjs testing.
  26  * @subtest
  27  * @summary test used by all other jjs-option* test cases
  28  */
  29 
  30 load(__DIR__ + "JDK-util.js")
  31 
  32 var javaHome = System.getenv("JAVA_HOME"),
  33     homeJjs = "${javaHome}" + "/bin/jjs",
  34     altJjs = which('jjs'),
  35     homejavac = "${javaHome}" + "/bin/javac",
  36     altjavac = which('javac')
  37 
  38 if (windows) {
  39     if (winCyg) {
  40         //Files.exists() expects proper extension as it talks to windows filesystem even on cygwin
  41         //make paths work on on underlying shells cygwin/cmd/linux.
  42         homeJjs = toShellPath("${javaHome}" + "/bin/jjs.exe")
  43         homejavac = toShellPath("${javaHome}" + "/bin/javac.exe")
  44     }
  45     else {
  46         homeJjs = toShellPath("${javaHome}" + "\\bin\\jjs.exe")
  47         homejavac = toShellPath("${javaHome}" + "\\bin\\javac.exe")
  48     }
  49     altJjs = which('jjs.exe')
  50     altjavac = which('javac.exe')
  51 }
  52 
  53 // Initialize default values for variables used in different functions
  54 var func_cond_p = <<EOD
  55 $EXIT == 0
  56 EOD
  57 
  58 var func_cond_n = <<EOD
  59 $EXIT != 0
  60 EOD
  61 
  62 var flag_cond_p = <<EOD
  63 out == e_outp
  64 EOD
  65 
  66 var flag_cond_n = <<EOD
  67 out == e_outn
  68 EOD
  69 
  70 var e_outp = "true"
  71 var e_outn = "false"
  72 
  73 // special cases in which arguments used for negative testing also
  74 var args_p = "-scripting"
  75 var args_n = "-scripting"
  76 
  77 // create file to check -flag passing
  78 var path_f = Paths.get("temp-flag.js")
  79 var testflag_file = toShellPath(path_f.toAbsolutePath().toString())
  80 
  81 // create file to check -flag functionality
  82 var path_func = Paths.get("temp-func.js")
  83 var testfunc_file = toShellPath(path_func.toAbsolutePath().toString())
  84 
  85 function exists(f) {
  86     return Files.exists(Paths.get(f))
  87 }
  88 
  89 var jjs = exists(homeJjs) ? homeJjs : altJjs
  90 var javac = exists(homejavac) ? homejavac : altjavac
  91 
  92 if (!exists(jjs)) {
  93     throw "no jjs executable found; tried ${homeJjs} and ${altJjs}"
  94 }
  95 
  96 // write code to testflag_file
  97 function write_testflag_file() {
  98     Files.write(Paths.get(testflag_file), msg_flag.getBytes())
  99 }
 100 
 101 // write code to testfunc_file
 102 function write_testfunc_file() {
 103     Files.write(Paths.get(testfunc_file), msg_func.getBytes())
 104 }
 105 
 106 function flag_test_pass() {
 107     print("flag test PASSED")
 108 }
 109 
 110 function flag_test_fail(e_out, out) {
 111     print("flag test FAILED expected out:${e_out} found:${out}")
 112 }
 113 
 114 // check functionality of flag,cover both positive and negative cases
 115 function testjjs_opt_func(args, positive) {
 116     $EXEC("${jjs} ${args}")
 117     var out = $OUT.trim(),
 118         err = $ERR.trim()
 119     if (positive) {
 120         if (eval(func_cond_p))
 121             print("functionality test PASSED")
 122         else 
 123             print("functionality test FAILED. stdout: ${out} -- stderr: ${err}")
 124     }
 125     else {
 126         if (eval(func_cond_n))
 127             print("functionality test PASSED")
 128         else
 129             print("functionality test FAILED. stdout: ${out} -- stderr: ${err}")
 130     }
 131 
 132 }
 133 
 134 // check if corresponding $OPTIONS._XXX is set for given flag
 135 function testjjs_opt(args, type, func) {
 136     $EXEC("${jjs} ${args}")
 137     var out = $OUT.trim(),
 138         err = $ERR.trim()
 139     if (type) {
 140         if (eval(flag_cond_p)) {
 141             flag_test_pass()
 142             if (func)
 143                 testjjs_opt_func(arg_p, type)
 144         }
 145         else {
 146             flag_test_fail(e_outp, out)
 147         }
 148     }
 149     else {
 150         if (eval(flag_cond_n)) {
 151             flag_test_pass()
 152             if (func)
 153                 testjjs_opt_func(arg_n, type)
 154         }
 155         else {
 156             flag_test_fail(e_outn, out)
 157         }
 158     }
 159 }
 160 
 161 // Main entry point to test both flag and its functionality
 162 function testjjs_flag_and_func(flag, param) {
 163     try {
 164         var args = "${flag}" + "${param}"
 165         write_testflag_file()
 166         write_testfunc_file()
 167         print("${flag} flag positive test:")
 168         testjjs_opt("${args_p} ${args} ${testflag_file}", true, true) // positive test
 169         print("${flag} flag negative test:")
 170         testjjs_opt("${args_n} ${testflag_file}", false, true)        // negative test
 171     } finally {
 172         rm("${testflag_file}")
 173         rm("${testfunc_file}")
 174     }
 175 }
 176 
 177 // Main entry point to check only functionality of given -flag
 178 function testjjs_functionality(flag, param) {
 179     try {
 180         var args = "${flag}" + "${param}"
 181         write_testfunc_file()
 182         print("${flag} flag positive test:")
 183         testjjs_opt_func("${args_p} ${args} ${testfunc_file}", true) // positive test
 184         print("${flag} flag negative test:")
 185         testjjs_opt_func("${args_n} ${testfunc_file}", false)        // negative test
 186     } finally {
 187         rm("${testfunc_file}")
 188     }
 189 }
 190 
 191 // Main entry point to check only -flag settings for given flag
 192 function testjjs_flag(flag, param) {
 193     try {
 194         var args = "${flag}" + "${param}"
 195         write_testflag_file()
 196         print("${flag} flag positive test:")
 197         testjjs_opt("${args_p} ${args} ${testflag_file}", true, false) // positive test
 198         print("${flag} flag negative test:")
 199         testjjs_opt("${args_n} ${testflag_file}", false, false)        // negative test
 200     } finally {
 201         rm("${testflag_file}")
 202     }
 203 }