test/script/nosecurity/JDK-8144221.js

changeset 1823
e6bd60e65f33
child 1825
8673bde5227d
equal deleted inserted replaced
1822:adcf08eb1715 1823:e6bd60e65f33
1 /*
2 * Copyright (c) 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 /**
25 * Test that shebang handling works properly.
26 *
27 * @test
28 * @runif os.not.windows
29 * @option -scripting
30 * @run
31 */
32
33 // The test generates three different JavaScript source files. The first two
34 // are generated at the beginning of the test and do not change.
35 // * a.js
36 // print("A: " + arguments)
37 // * b.js
38 // #!<path_to_jjs> -lalelu -- ignore
39 // print("B: " + arguments)
40 //
41 // The third file, shebang.js, is generated differently for each particular
42 // test case, containing different shebang lines and one statement:
43 // * shebang.js
44 // #!<path_to_jjs> <shebang_line>
45 // print("S: " + arguments)
46 //
47 // The path_to_jjs is extracted from the environment based on JAVA_HOME, so the
48 // latter must be set properly.
49 //
50 // Each shebang.js is run four times, in all possible combinations of values
51 // from the following two axes:
52 // * without passing any arguments, and passing the arguments 'a.js' and
53 // '"hello world"' (the latter being a quoted string);
54 // * run via jjs, and via direct shell execution (using shebang).
55
56 var pseudosheb = "#!${jjs} -lalelu -- ignore",
57 System = Java.type('java.lang.System'),
58 Paths = Java.type('java.nio.file.Paths'),
59 Files = Java.type('java.nio.file.Files'),
60 Opt = Java.type('java.nio.file.StandardOpenOption'),
61 Arrays = Java.type('java.util.Arrays')
62
63 var sep = Java.type('java.io.File').separator,
64 win = System.getProperty("os.name").startsWith("Windows"),
65 jjsName = "jjs" + (win ? ".exe" : ""),
66 javaHome = System.getProperty("java.home")
67
68 var jjs = javaHome + "/../bin/".replace(/\//g, sep) + jjsName
69 if (!Files.exists(Paths.get(jjs))) {
70 jjs = javaHome + "/bin/".replace(/\//g, sep) + jjsName
71 }
72
73 // Create and cwd to a temporary directory.
74
75 var tmpdir = Files.createTempDirectory(null),
76 tmp = tmpdir.toAbsolutePath().toString(),
77 curpwd = $ENV.PWD
78
79 $ENV.PWD = tmp
80
81 // Test cases. Each case is documented with the expected output for the four
82 // different executions.
83
84 var shebs = [
85 // No arguments on the shebang line.
86 // noargs jjs/shebang -> no output but "S" prefix
87 // args jjs/shebang -> output the arguments with "S" prefix
88 "",
89 // One interpreter argument.
90 // noargs jjs/shebang -> no output but "S" prefix
91 // args jjs/shebang -> output the arguments with "S" prefix
92 "--language=es6",
93 // Two interpreter arguments.
94 // noargs jjs/shebang -> no output but "S" prefix
95 // args jjs/shebang -> output the arguments with "S" prefix
96 "--language=es6 -scripting",
97 // One interpreter argument and a JavaScript file without shebang.
98 // (For shebang execution, this is a pathological example, as the
99 // JavaScript file passed as a shebang argument will be analyzed and
100 // shebang mode will not be entered.)
101 // noargs jjs -> no output but "S" prefix
102 // args jjs -> output the arguments with "S" prefix
103 // noargs shebang -> no output but "A" and "S" prefixes
104 // args shebang -> output "A", "S", and "A" prefixes, then the error
105 // message:
106 // "java.io.IOException: hello world is not a file"
107 "-scripting a.js",
108 // One interpreter argument and a JavaScript file with shebang. (This
109 // is another pathological example, as it will force shebang mode,
110 // leading to all subsequent arguments, including shebang.js, being
111 // treated as arguments to the script b.js.)
112 // noargs jjs -> no output but the "S" prefix
113 // args jjs -> output the arguments with "S" prefix
114 // noargs shebang -> output shebang.js with "B" prefix
115 // args shebang -> output shebang.js and the arguments with "B"
116 // prefix
117 "-scripting b.js"
118 ]
119
120 function write(file, lines) {
121 Files.write(Paths.get(tmp, file), Arrays.asList(lines), Opt.CREATE, Opt.WRITE)
122 }
123
124 function insn(name) {
125 return "print('${name}:' + arguments)"
126 }
127
128 function run(viajjs, name, arg1, arg2) {
129 var prefix = viajjs ? "${jjs} -scripting " : ''
130 $EXEC("${prefix}./shebang.js ${arg1} ${arg2}")
131 print("* ${name} via ${viajjs ? 'jjs' : 'shebang'}")
132 print($OUT.trim())
133 print($ERR.trim())
134 }
135
136 write('a.js', insn('A'))
137 write('b.js', [pseudosheb, insn('B')])
138
139 shebs.forEach(function(sheb) {
140 var shebang = "#!${jjs} ${sheb}"
141 print("<<< ${sheb} >>>")
142 write('shebang.js', [shebang, insn('S')])
143 $EXEC('chmod +x shebang.js')
144 run(false, 'noargs', '', '')
145 run(true, 'noargs', '', '')
146 run(false, 'withargs', 'a.js', '"hello world"')
147 run(true, 'withargs', 'a.js', '"hello world"')
148 $EXEC('rm shebang.js')
149 })
150
151 // Cleanup.
152
153 $EXEC('rm a.js b.js')
154 $ENV.PWD = curpwd
155 Files.delete(tmpdir)

mercurial