提交 3a4c5a9a 编写于 作者: N Nikita Akilov

dbeaver/dbeaver-ee#521 add escaping to cron entry params

上级 aa88ac71
......@@ -801,4 +801,15 @@ public class CommonUtils {
cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR));
}
public static String escapeBourneShellString(@NotNull String s) {
return "'" + s.replace("'", "'\\''") + "'";
}
public static String unescapeBourneShellString(@NotNull String s) {
if (!s.startsWith("'") || !s.endsWith("'") || s.length() < 2) { //not an escaped bourne shell string
return s;
}
return s.substring(1, s.length() - 1).replace("'\\''", "'");
}
}
......@@ -522,4 +522,29 @@ public class CommonUtilsTest {
public void testgetSingleLineString() {
Assert.assertEquals("a¶bc d ", CommonUtils.getSingleLineString("a\nb\rc\td\0"));
}
@Test
public void testEscapeStringForBourneShell() {
Assert.assertEquals("''", CommonUtils.escapeBourneShellString(""));
Assert.assertEquals("'string'", CommonUtils.escapeBourneShellString("string"));
Assert.assertEquals("'string with '\\''one single quote symbol'", CommonUtils.escapeBourneShellString("string with 'one single quote symbol"));
Assert.assertEquals("'string with '\\''two '\\''single quote symbols'", CommonUtils.escapeBourneShellString("string with 'two 'single quote symbols"));
Assert.assertEquals("'string with '\\''three '\\''single '\\''quote symbols'", CommonUtils.escapeBourneShellString("string with 'three 'single 'quote symbols"));
Assert.assertEquals("''\\'''", CommonUtils.escapeBourneShellString("'"));
Assert.assertEquals("'unit'\\'''\\''test'", CommonUtils.escapeBourneShellString("unit''test"));
Assert.assertEquals("'unit'\\'''\\'''\\''test'", CommonUtils.escapeBourneShellString("unit'''test"));
}
@Test
public void testUnescapeStringForBourneShell() {
Assert.assertEquals("", CommonUtils.unescapeBourneShellString("''"));
Assert.assertEquals("string", CommonUtils.unescapeBourneShellString("'string'"));
Assert.assertEquals("string with 'one single quote symbol", CommonUtils.unescapeBourneShellString("'string with '\\''one single quote symbol'"));
Assert.assertEquals("string with 'two 'single quote symbols", CommonUtils.unescapeBourneShellString("'string with '\\''two '\\''single quote symbols'"));
Assert.assertEquals("string with 'three 'single 'quote symbols", CommonUtils.unescapeBourneShellString("'string with '\\''three '\\''single '\\''quote symbols'"));
Assert.assertEquals("'", CommonUtils.unescapeBourneShellString("''\\'''"));
Assert.assertEquals("unit''test", CommonUtils.unescapeBourneShellString("'unit'\\'''\\''test'"));
Assert.assertEquals("unit'''test", CommonUtils.unescapeBourneShellString("'unit'\\'''\\'''\\''test'"));
Assert.assertEquals("'''unit'''test'''", CommonUtils.unescapeBourneShellString("''\\'''\\'''\\''unit'\\'''\\'''\\''test'\\'''\\'''\\'''"));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册