Skip to content

Commit

Permalink
Tweak tests
Browse files Browse the repository at this point in the history
  • Loading branch information
psiniemi committed Nov 5, 2015
1 parent b0f80aa commit 51b435b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ protected MergeableProperties(Properties defaults, LinkedHashMap<String, String>
}

public MergeableProperties(boolean allowScripts) {
super();
defaults = new Properties();
prefixes = new String[] { "classpath:" };
this.allowScripts = allowScripts;
this(new Properties(), new LinkedHashMap<>(), allowScripts, "classpath:");
}

public MergeableProperties() {
Expand Down Expand Up @@ -102,8 +99,19 @@ public MergeableProperties merge(String name) {
private boolean pathEndsWith(String name, String suffix) {
try {
URI uri = new URI(name);
return uri.getPath().endsWith(suffix);
} catch (URISyntaxException e) {
String path = uri.getPath();
if (path != null) {
return path.endsWith(suffix);
} else {
URL url = new URL(name);
path = url.getPath();
if (path != null) {
return path.endsWith(suffix);
} else {
return false;
}
}
} catch (URISyntaxException | MalformedURLException e) {
return false;
}
}
Expand Down Expand Up @@ -144,18 +152,15 @@ private void postMerge() {
}

private String evaluate(String replace, boolean allowEval) {
if (!allowEval || !this.allowScripts) return replace;
Matcher m = SCRIPT_REGEX.matcher(replace);
StringBuffer ret = new StringBuffer();
int end = 0;
engine.put("self", this);
while (m.find()) {
ret.append(m.group(1));
try {
if (allowEval) {
ret.append(engine.eval(m.group(3)));
} else {
ret.append(m.group(3));
}
} catch (ScriptException e) {
ret.append(m.group(2));
log.log(Level.INFO, "Failed to execute javascript", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.nitorcreations.willow.utils;

import static org.junit.Assert.*;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;

import org.junit.Test;

public class TestObfuscatorTool {
@Test
public void testTool() throws Exception {
ObfuscatorTool.main(new String[] {"target/test-classes/root.properties"});
File enc = new File("target/test-classes/root.properties.encrypted");
File key = new File("target/test-classes/root.properties.key");
assertTrue(enc.exists());
assertTrue(key.exists());
System.setProperty("decrypt", "");
ByteArrayOutputStream output = new ByteArrayOutputStream();
PrintStream oldOut = System.out;
System.setOut(new PrintStream(output));
ObfuscatorTool.main(new String[] {"target/test-classes/root.properties.encrypted", "target/test-classes/root.properties.key" });
System.setOut(oldOut);
byte[] outputData = output.toByteArray();
ByteArrayInputStream in = new ByteArrayInputStream(outputData);
MergeableProperties props = new MergeableProperties();
props.load(in);
assertEquals("foo/bar", props.getProperty("included.file[1].extraprops"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,11 @@ public void testPutAll() {
assertEquals("ptql", r.getProperty("deployer.statistics[5]"));
assertEquals("State.Name.eq=java,Args.*.eq=com.nitorcreations.willow.deployer.Main", r.getProperty("deployer.statistics[5].query"));
}
@Test
public void testDisallowEval() {
MergeableProperties p = new MergeableProperties(false);
p.merge("file:./target/test-classes/root.properties?target.id=env_test&node-group.id=appservers&node.id=appserver&component.id=webfront");
assertEquals("<script>(self.get('test.scripting.value')-2).toPrecision(1)</script><script>(10+21).toPrecision(2)</script>", p.getProperty("test.scripting"));
}

}

0 comments on commit 51b435b

Please sign in to comment.