| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
package org.apache.felix.gogo.runtime; |
| 20 |
|
| 21 |
import static org.mockito.Matchers.eq; |
| 22 |
import static org.mockito.Mockito.mock; |
| 23 |
import static org.mockito.Mockito.when; |
| 24 |
|
| 25 |
import java.io.ByteArrayInputStream; |
| 26 |
import java.io.File; |
| 27 |
import java.net.URI; |
| 28 |
import java.util.HashMap; |
| 29 |
import java.util.Map; |
| 30 |
|
| 31 |
import junit.framework.TestCase; |
| 32 |
|
| 33 |
import org.apache.felix.gogo.runtime.Tokenizer.Type; |
| 34 |
import org.apache.felix.gogo.runtime.threadio.ThreadIOImpl; |
| 35 |
import org.junit.Test; |
| 36 |
import org.osgi.framework.Bundle; |
| 37 |
import org.osgi.framework.BundleContext; |
| 38 |
|
| 39 |
public class TestTokenizer extends TestCase |
| 40 |
{ |
| 41 |
private final Map<String, Object> vars = new HashMap<String, Object>(); |
| 42 |
private final Evaluate evaluate; |
| 43 |
|
| 44 |
public TestTokenizer() |
| 45 |
{ |
| 46 |
evaluate = new Evaluate() |
| 47 |
{ |
| 48 |
public Object eval(Token t) throws Exception |
| 49 |
{ |
| 50 |
throw new UnsupportedOperationException("eval not implemented."); |
| 51 |
} |
| 52 |
|
| 53 |
public Object get(String key) |
| 54 |
{ |
| 55 |
return vars.get(key); |
| 56 |
} |
| 57 |
|
| 58 |
public Object put(String key, Object value) |
| 59 |
{ |
| 60 |
return vars.put(key, value); |
| 61 |
} |
| 62 |
}; |
| 63 |
} |
| 64 |
|
| 65 |
@Test |
| 66 |
public void testHello() throws Exception |
| 67 |
{ |
| 68 |
testHello("hello world\n"); |
| 69 |
testHello("hello world\n\n"); // multiple \n reduced to single Token.NL |
| 70 |
testHello("hello world\r\n"); // \r\n -> \n |
| 71 |
|
| 72 |
// escapes |
| 73 |
|
| 74 |
testHello("hello \\\nworld\n"); |
| 75 |
try |
| 76 |
{ |
| 77 |
testHello("hello\\u20world\n"); |
| 78 |
fail("bad unicode accepted"); |
| 79 |
} |
| 80 |
catch (SyntaxError e) |
| 81 |
{ |
| 82 |
// expected |
| 83 |
} |
| 84 |
|
| 85 |
// whitespace and comments |
| 86 |
|
| 87 |
testHello(" hello world \n "); |
| 88 |
testHello("hello world // comment\n\n"); |
| 89 |
testHello("hello world #\\ comment\n\n"); |
| 90 |
testHello("// comment\nhello world\n"); |
| 91 |
testHello("// comment ?\\ \nhello world\n"); |
| 92 |
testHello("hello /*\n * comment\n */ world\n"); |
| 93 |
} |
| 94 |
|
| 95 |
// hello world |
| 96 |
private void testHello(CharSequence text) throws Exception |
| 97 |
{ |
| 98 |
Tokenizer t = new Tokenizer(text); |
| 99 |
assertEquals(Type.WORD, t.next()); |
| 100 |
assertEquals("hello", t.value().toString()); |
| 101 |
assertEquals(Type.WORD, t.next()); |
| 102 |
assertEquals("world", t.value().toString()); |
| 103 |
assertEquals(Type.NEWLINE, t.next()); |
| 104 |
assertEquals(Type.EOT, t.next()); |
| 105 |
} |
| 106 |
|
| 107 |
@Test |
| 108 |
public void testString() throws Exception |
| 109 |
{ |
| 110 |
testString("'single $quote' \"double $quote\"\n"); |
| 111 |
} |
| 112 |
|
| 113 |
// 'single quote' "double quote" |
| 114 |
private void testString(CharSequence text) throws Exception |
| 115 |
{ |
| 116 |
Tokenizer t = new Tokenizer(text); |
| 117 |
assertEquals(Type.WORD, t.next()); |
| 118 |
assertEquals("'single $quote'", t.value().toString()); |
| 119 |
assertEquals(Type.WORD, t.next()); |
| 120 |
assertEquals("\"double $quote\"", t.value().toString()); |
| 121 |
assertEquals(Type.NEWLINE, t.next()); |
| 122 |
assertEquals(Type.EOT, t.next()); |
| 123 |
} |
| 124 |
|
| 125 |
@Test |
| 126 |
public void testClosure() throws Exception |
| 127 |
{ |
| 128 |
testClosure2("x = { echo '}' $args //comment's\n}\n"); |
| 129 |
testClosure2("x={ echo '}' $args //comment's\n}\n"); |
| 130 |
assertEquals(Type.CLOSURE, token1("{ echo \\{ $args \n}")); |
| 131 |
assertEquals(Type.CLOSURE, token1("{ echo \\} $args \n}")); |
| 132 |
} |
| 133 |
|
| 134 |
/* |
| 135 |
* x = {echo $args}; |
| 136 |
*/ |
| 137 |
@Test |
| 138 |
private void testClosure2(CharSequence text) throws Exception |
| 139 |
{ |
| 140 |
Tokenizer t = new Tokenizer(text); |
| 141 |
assertEquals(Type.WORD, t.next()); |
| 142 |
assertEquals("x", t.value().toString()); |
| 143 |
assertEquals(Type.ASSIGN, t.next()); |
| 144 |
assertEquals(Type.CLOSURE, t.next()); |
| 145 |
assertEquals(" echo '}' $args //comment's\n", t.value().toString()); |
| 146 |
assertEquals(Type.NEWLINE, t.next()); |
| 147 |
assertEquals(Type.EOT, t.next()); |
| 148 |
} |
| 149 |
|
| 150 |
private Type token1(CharSequence text) throws Exception |
| 151 |
{ |
| 152 |
Tokenizer t = new Tokenizer(text); |
| 153 |
Type type = t.next(); |
| 154 |
assertEquals(Type.EOT, t.next()); |
| 155 |
return type; |
| 156 |
} |
| 157 |
|
| 158 |
@Test |
| 159 |
public void testExpand() throws Exception |
| 160 |
{ |
| 161 |
final URI home = new URI("/home/derek"); |
| 162 |
final File pwd = new File("/tmp"); |
| 163 |
final String user = "derek"; |
| 164 |
|
| 165 |
vars.clear(); |
| 166 |
vars.put("HOME", home); |
| 167 |
vars.put("PWD", pwd); |
| 168 |
vars.put("USER", user); |
| 169 |
vars.put(user, "Derek Baum"); |
| 170 |
|
| 171 |
// quote removal |
| 172 |
assertEquals("hello", expand("hello")); |
| 173 |
assertEquals("hello", expand("'hello'")); |
| 174 |
assertEquals("\"hello\"", expand("'\"hello\"'")); |
| 175 |
assertEquals("hello", expand("\"hello\"")); |
| 176 |
assertEquals("'hello'", expand("\"'hello'\"")); |
| 177 |
|
| 178 |
// escapes |
| 179 |
assertEquals("hello\\w", expand("hello\\\\w")); |
| 180 |
assertEquals("hellow", expand("hello\\w")); |
| 181 |
assertEquals("hello\\w", expand("\"hello\\\\w\"")); |
| 182 |
assertEquals("hello\\w", expand("\"hello\\w\"")); |
| 183 |
assertEquals("hello\\\\w", expand("'hello\\\\w'")); |
| 184 |
assertEquals("hello", expand("he\\\nllo")); |
| 185 |
assertEquals("he\\llo", expand("'he\\llo'")); |
| 186 |
assertEquals("he'llo", expand("'he'\\''llo'")); |
| 187 |
assertEquals("he\"llo", expand("\"he\\\"llo\"")); |
| 188 |
assertEquals("he'llo", expand("he\\'llo")); |
| 189 |
assertEquals("he$llo", expand("\"he\\$llo\"")); |
| 190 |
assertEquals("he\\'llo", expand("\"he\\'llo\"")); |
| 191 |
assertEquals("hello\\w", expand("\"hello\\w\"")); |
| 192 |
|
| 193 |
// unicode |
| 194 |
|
| 195 |
// Note: we could use literal Unicode pound '£' instead of \u00a3 in next test. |
| 196 |
// if above is not UK currency symbol, then your locale is not configured for UTF-8. |
| 197 |
// Java on Macs cannot handle UTF-8 unless you explicitly set '-Dfile.encoding=UTF-8'. |
| 198 |
assertEquals("pound\u00a3cent\u00a2", expand("pound\\u00a3cent\\u00a2")); |
| 199 |
assertEquals("euro\\u20ac", expand("'euro\\u20ac'")); |
| 200 |
try |
| 201 |
{ |
| 202 |
expand("eot\\u20a"); |
| 203 |
fail("EOT in unicode"); |
| 204 |
} |
| 205 |
catch (SyntaxError e) |
| 206 |
{ |
| 207 |
// expected |
| 208 |
} |
| 209 |
try |
| 210 |
{ |
| 211 |
expand("bad\\u20ag"); |
| 212 |
fail("bad unicode"); |
| 213 |
} |
| 214 |
catch (SyntaxError e) |
| 215 |
{ |
| 216 |
// expected |
| 217 |
} |
| 218 |
|
| 219 |
// simple variable expansion - quoting or concatenation converts result to String |
| 220 |
assertEquals(user, expand("$USER")); |
| 221 |
assertEquals(home, expand("$HOME")); |
| 222 |
assertEquals(home.toString(), expand("$HOME$W")); |
| 223 |
assertEquals(pwd, expand("$PWD")); |
| 224 |
assertEquals("$PWD", expand("'$PWD'")); |
| 225 |
assertEquals("$PWD", expand("\\$PWD")); |
| 226 |
assertEquals(pwd.toString(), expand("\"$PWD\"")); |
| 227 |
assertEquals("W" + pwd, expand("W$PWD")); |
| 228 |
assertEquals(pwd + user, expand("$PWD$USER")); |
| 229 |
|
| 230 |
// variable substitution ${NAME:-WORD} etc |
| 231 |
assertNull(expand("$JAVA_HOME")); |
| 232 |
assertEquals(user, expand("${USER}")); |
| 233 |
assertEquals(user + "W", expand("${USER}W")); |
| 234 |
assertEquals("java_home", expand("${JAVA_HOME:-java_home}")); |
| 235 |
assertEquals(pwd, expand("${NOTSET:-$PWD}")); |
| 236 |
assertNull(vars.get("JAVA_HOME")); |
| 237 |
assertEquals("java_home", expand("${JAVA_HOME:=java_home}")); |
| 238 |
assertEquals("java_home", vars.get("JAVA_HOME")); |
| 239 |
assertEquals("java_home", expand("$JAVA_HOME")); |
| 240 |
assertEquals("yes", expand("${JAVA_HOME:+yes}")); |
| 241 |
assertNull(expand("${NOTSET:+yes}")); |
| 242 |
assertEquals("", expand("\"${NOTSET:+yes}\"")); |
| 243 |
try |
| 244 |
{ |
| 245 |
expand("${NOTSET:?}"); |
| 246 |
fail("expected 'not set' exception"); |
| 247 |
} |
| 248 |
catch (IllegalArgumentException e) |
| 249 |
{ |
| 250 |
// expected |
| 251 |
} |
| 252 |
|
| 253 |
// bad variable names |
| 254 |
assertEquals("$ W", expand("$ W")); |
| 255 |
assertEquals("$ {W}", expand("$ {W}")); |
| 256 |
try |
| 257 |
{ |
| 258 |
expand("${W }"); |
| 259 |
fail("expected syntax error"); |
| 260 |
} |
| 261 |
catch (SyntaxError e) |
| 262 |
{ |
| 263 |
// expected |
| 264 |
} |
| 265 |
|
| 266 |
assertEquals(user, expand("${USER\\\n:?}")); |
| 267 |
assertEquals(user, expand("${US\\u0045R:?}")); |
| 268 |
|
| 269 |
// bash doesn't supported nested expansions |
| 270 |
// gogo only supports them in the ${} syntax |
| 271 |
assertEquals("Derek Baum", expand("${$USER}")); |
| 272 |
assertEquals("x", expand("${$USR:-x}")); |
| 273 |
assertEquals("$" + user, expand("$$USER")); |
| 274 |
} |
| 275 |
|
| 276 |
private Object expand(CharSequence word) throws Exception |
| 277 |
{ |
| 278 |
return Tokenizer.expand(word, evaluate); |
| 279 |
} |
| 280 |
|
| 281 |
@Test |
| 282 |
public void testParser() throws Exception |
| 283 |
{ |
| 284 |
new Parser("// comment\n" + "a=\"who's there?\"; ps -ef;\n" + "ls | \n grep y\n").program(); |
| 285 |
String p1 = "a=1 \\$b=2 c={closure}\n"; |
| 286 |
new Parser(p1).program(); |
| 287 |
new Parser(new Token(Type.ARRAY, p1, (short) 0, (short) 0)).program(); |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* FELIX-4679 / FELIX-4671. |
| 292 |
*/ |
| 293 |
@Test |
| 294 |
public void testScriptFelix4679() throws Exception |
| 295 |
{ |
| 296 |
String script = "addcommand system (((${.context} bundles) 0) loadclass java.lang.System)"; |
| 297 |
|
| 298 |
ThreadIOImpl tio = new ThreadIOImpl(); |
| 299 |
tio.start(); |
| 300 |
|
| 301 |
try |
| 302 |
{ |
| 303 |
BundleContext bc = createMockContext(); |
| 304 |
|
| 305 |
CommandProcessorImpl processor = new CommandProcessorImpl(tio); |
| 306 |
processor.addCommand("gogo", processor, "addcommand"); |
| 307 |
processor.addConstant(".context", bc); |
| 308 |
|
| 309 |
CommandSessionImpl session = new CommandSessionImpl(processor, new ByteArrayInputStream(script.getBytes()), System.out, System.err); |
| 310 |
|
| 311 |
Closure c = new Closure(session, null, script); |
| 312 |
assertNull(c.execute(session, null)); |
| 313 |
} |
| 314 |
finally |
| 315 |
{ |
| 316 |
tio.stop(); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
private BundleContext createMockContext() throws ClassNotFoundException |
| 321 |
{ |
| 322 |
Bundle systemBundle = mock(Bundle.class); |
| 323 |
when(systemBundle.loadClass(eq("java.lang.System"))).thenReturn(System.class); |
| 324 |
|
| 325 |
BundleContext bc = mock(BundleContext.class); |
| 326 |
when(bc.getBundles()).thenReturn(new Bundle[] { systemBundle }); |
| 327 |
return bc; |
| 328 |
} |
| 329 |
} |