<?php
error_reporting(E_ALL | E_STRICT);
ini_set("track_errors", "On");
$rg_util_debug = TRUE;
$INC = dirname(__FILE__) . "/../inc";
require_once(dirname(__FILE__) . "/config.php");
require_once($INC . "/init.inc.php");
require_once($INC . "/util.inc.php");
require_once($INC . "/log.inc.php");
rg_log_set_file("util.log");
$rg_no_db = TRUE;
require_once("common.php");
rg_log('');
rg_log_enter('Testing rg_template_eval_cond');
$a = array('a' => 100, 'b' => 200);
$cond = '1 >= 100';
$r = rg_template_eval_cond($cond, $a);
if (($r === FALSE) || ($r === 1)) {
rg_log('Error for ' . $cond . '!');
exit(1);
}
$cond = '@@a@@ == 100';
$r = rg_template_eval_cond($cond, $a);
if (($r === FALSE) || ($r === 0)) {
rg_log('Error for ' . $cond . '!');
exit(1);
}
$cond = '-100 != -100';
$r = rg_template_eval_cond($cond, $a);
if (($r === FALSE) || ($r === 1)) {
rg_log('Error for ' . $cond . '!');
exit(1);
}
rg_log_exit();
rg_log("Testing template_tree_lookup1");
$var = 'a::b';
$e = '<';
$data = array('a' => array('HTML:b' => '<'));
$r = rg_template_tree_lookup($var, $data, TRUE /*xss_protection*/);
if (strcmp($r, $e) != 0) {
rg_log("rg_template_string [template_tree_lookup1] is not working as expected [$r] != [$e]!");
exit(1);
}
// Test: rg_template_tree_lookup
$data = array('a' => array('a2' => array('a3' => 'X')));
$e = 'X';
$r = rg_template_tree_lookup('a::a2::a3', $data, FALSE /*xss*/);
if (strcmp($r, $e) != 0) {
rg_log("tree_lookup is not working as expected!");
exit(1);
}
rg_log("Testing ::+nesting");
$s = '@@a::b@@';
$e = '<';
$data = array('a' => array('b' => '<'));
$r = rg_template_string($s, 0, $data, TRUE /*xss*/);
if (strcmp($r, $e) != 0) {
rg_log("rg_template_string [::+nesting] is not working as expected [$r] != [$e]!");
exit(1);
}
rg_log("Testing ::+nesting-html0");
$s = '@@a@@';
$e = 'Y';
$data = array('HTML:a' => 'Y');
$r = rg_template_string($s, 0, $data, TRUE /*xss*/);
if (strcmp($r, $e) != 0) {
rg_log("rg_template_string [::+nesting-html0] is not working as expected [$r] != [$e]!");
exit(1);
}
rg_log("Testing ::+nesting-html");
$s = '@@a::b@@';
$e = '<';
$data = array('a' => array('HTML:b' => '<'));
$r = rg_template_string($s, 0, $data, TRUE /*xss*/);
if (strcmp($r, $e) != 0) {
rg_log("rg_template_string [::+nesting-html] is not working as expected [$r] != [$e]!");
exit(1);
}
rg_log("Testing nesting");
$s = '@@if(@@x@@ == 0){{@@if(@@x@@ == 0){{@@if(@@x@@ == 0){{3}}{{!3}}}}{{!2}}}}{{!1}}';
$e = '3';
$data = array('x' => 0);
$r = rg_template_string($s, 0, $data, TRUE /*xss*/);
if (strcmp($r, $e) != 0) {
rg_log("rg_template_string [3 nested] is not working as expected [$r] != [$e]!");
exit(1);
}
rg_log("Testing nesting with false");
$s = '@@if(@@x@@ == 0){{@@if(@@x@@ != 0){{}}{{@@if(@@x@@ == 0){{3}}{{!3}}}}}}{{!1}}';
$e = '3';
$data = array('x' => 0);
$r = rg_template_string($s, 0, $data, TRUE /*xss*/);
if (strcmp($r, $e) != 0) {
rg_log("rg_template_string [3 nested false] is not working as expected [$r] != [$e]!");
exit(1);
}
rg_log("Testing string compared with int");
$s = '@@if(@@x@@ == 0){{A}}';
$e = 'A';
$data = array('x' => 0);
$r = rg_template_string($s, 0, $data, TRUE /*xss*/);
if (strcmp($r, $e) != 0) {
rg_log("rg_template_string [string int] is not working as expected [$r] != [$e]!");
exit(1);
}
rg_log("Testing !=");
$s = '@@if(x != 0)123456789{{A}}';
rg_log("s=$s");
$e = 'A';
$data = array();
$r = rg_template_string($s, 0, $data, TRUE /*xss*/);
if (strcmp($r, $e) != 0) {
rg_log("rg_template_string [!=] is not working as expected [$r] != [$e]!");
exit(1);
}
// Test: empty if
$s = '@@if(x == x)123456789{{}}';
rg_log("s=$s");
$e = '';
$data = array();
$r = rg_template_string($s, 0, $data, TRUE /*xss*/);
if (strcmp($r, $e) != 0) {
rg_log("rg_template_string [empty if] is not working as expected [$r] != [$e]!");
exit(1);
}
// Test: empty if
$s = '@@if(x == x)123456789{{}}{{}}';
rg_log("s=$s");
$e = '';
$data = array();
$r = rg_template_string($s, 0, $data, TRUE /*xss*/);
if (strcmp($r, $e) != 0) {
rg_log("rg_template_string [empty if with false] is not working as expected [$r] != [$e]!");
exit(1);
}
// Test: rg_template_string with if (negate)
$s = 'Start@@if(@@v@@ != x)123456789{{@@a@@}} x {{alien}}End';
rg_log("s=$s");
$data = array('a' => '<>', 'v' => 'x2');
$e = 'Start<> x {{alien}}End';
$r = rg_template_string($s, 0, $data, TRUE /*xss*/);
if (strcmp($r, $e) != 0) {
rg_log("rg_template_string [simple if true xss neg] is not working as expected [$r] != [$e]!");
exit(1);
}
// Test: rg_template_string with if
$s = 'Start@@if(@@v@@ == x)123456789{{@@a@@}} x {{alien}}End';
rg_log("s=$s");
$data = array('a' => '<>', 'v' => 'x');
$e = 'Start<> x {{alien}}End';
$r = rg_template_string($s, 0, $data, TRUE /*xss*/);
if (strcmp($r, $e) != 0) {
rg_log("rg_template_string [simple if true xss] is not working as expected [$r] != [$e]!");
exit(1);
}
// Test: rg_template_string with if
$s = '@@if("" == "")123456789{{A}}';
rg_log("s=$s");
$data = array();
$e = 'A';
$r = rg_template_string($s, 0, $data, FALSE /*xss*/);
if (strcmp($r, $e) != 0) {
rg_log("rg_template_string [simple if true] is not working as expected [$r] != [$e]!");
exit(1);
}
// Test: rg_template_string with if
$s = '@@if("a" != "")123456789{{A}}';
rg_log("s=$s");
$data = array();
$e = 'A';
$r = rg_template_string($s, 0, $data, FALSE /*xss*/);
if (strcmp($r, $e) != 0) {
rg_log("rg_template_string [simple if false] is not working as expected [$r] != [$e]!");
exit(1);
}
// Test: rg_template_string with if
$s = '@@if("" != "")123456789{{A}}{{B}}';
rg_log("s=$s");
$data = array();
$e = 'B';
$r = rg_template_string($s, 0, $data, FALSE /*xss*/);
if (strcmp($r, $e) != 0) {
rg_log("rg_template_string [if without gap] is not working as expected [$r] != [$e]!");
exit(1);
}
// Test: rg_template_string with if (with gap)
$s = "@@if(\"\" != \"\")123456789{{A}} \n\t {{B}}";
rg_log("s=$s");
$data = array();
$e = 'B';
$r = rg_template_string($s, 0, $data, FALSE /*xss*/);
if (strcmp($r, $e) != 0) {
rg_log("rg_template_string [if with gap] is not working as expected [$r] != [$e]!");
exit(1);
}
// Test: rg_template_string
$data = array();
$s = '@@a::a2@@ @@b@@ @@b@@';
$e = $s;
$r = rg_template_string($s, 0, $data, FALSE /*xss*/);
if (strcmp($r, $e) != 0) {
rg_log("rg_template_string [empty data] is not working as expected [$r] != [$e]!");
exit(1);
}
$s = '@@a::a2@@ @@b@@ @@b@@ @@c@@';
$data = array(
'a' => array('a2' => 'a3'),
'b' => 'X'
);
$e = 'a3 X X @@c@@';
$r = rg_template_string($s, 0, $data, FALSE /*xss*/);
if (strcmp($r, $e) != 0) {
rg_log("rg_template_string [second level] is not working as expected [$r] != [$e]!");
exit(1);
}
// Test template functions
function inc($v) { rg_log("DEBUG: inc called with v=$v"); return $v + 1; }
function dec($v) { rg_log("DEBUG: dec called with v=$v"); return $v - 1; }
rg_template_func("inc", "inc");
rg_template_func("dec", "dec");
$_rg = array("uid" => 5);
$r = trim(rg_template("func.txt", $_rg, TRUE /*xss*/));
$e = "5 + 1 = 6 | 5 - 1 = 4 | 4";
if (strcmp($r, $e) != 0) {
rg_log("template func1 test failed [$r] != [$e]");
exit(1);
}
$id = rg_id(16);
if (strlen($id) != 16) {
rg_log("Cannot generate an id!");
exit(1);
}
// rg_array2string
$a = 5;
$e = "5";
$r = rg_array2string($a);
if ($r !== $e) {
rg_log("array2string is not working for integers ($r != $a)!");
exit(1);
}
$a = "6";
$e = "6";
$r = rg_array2string($a);
if ($r !== $e) {
rg_log("array2string is not working for strings ($r != $a)!");
exit(1);
}
@mkdir("util.tmp", 0700, TRUE);
file_put_contents("util.tmp/file1", "aaa");
file_put_contents("util.tmp/file2", "bbb");
$r = rg_rmdir("util.tmp");
if ($r !== TRUE) {
rg_log("Cannot delete dir (" . rg_util_error() . ")!");
exit(1);
}
$r = rg_exec("/xxxx", '', FALSE, FALSE);
if ($r['ok'] == 1) {
rg_log("util.php: running non existing command does not return 0!");
print_r($r);
exit(1);
}
$r = rg_exec("ls", '', FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log("util.php: cannot run a command!");
print_r($r);
exit(1);
}
$r = rg_exec("./util_exit_code.sh 5", '', FALSE, FALSE);
if ($r['code'] != 5) {
rg_log("util.php: error code seems to not be propageted!");
print_r($r);
exit(1);
}
$t = "test rg_template_table(dir, data, more) with no data";
rg_log($t);
$data = array();
$r = rg_template_table("t1", $data, array("a" => "A"));
$e = "XAX";
if (strcmp($r, $e) != 0) {
rg_log("util.php: $t: not working (r=$r e=$e)!");
exit(1);
}
$t = "test rg_template_table(dir, data, more) with data";
rg_log($t);
$data = array(array("a" => "A", "b" => "B"), array("a" => "A2", "b" => "B2"));
$r = rg_template_table("t2", $data, array("c" => "C"));
$e = "HEADCABCA2B2CFOOTC";
if (strcmp($r, $e) != 0) {
rg_log("util.php: $t: not working (r=$r e=$e)!");
exit(1);
}
$t = "test rg_template with conditional formating (false)";
rg_log($t);
$data = array("X" => "0", "A" => "Avalue", "B" => "Bvalue");
$r = rg_template("t3/c1", $data, TRUE /*xss*/);
$e = "XXBvalueYY";
if (strcmp($r, $e) != 0) {
rg_log("util.php: $t: not working (r=$r e=$e)!");
exit(1);
}
$t = "test rg_template with conditional formating (true)";
rg_log($t);
$data = array("X" => "1", "A" => "Avalue", "B" => "Bvalue");
$r = rg_template("t3/c1", $data, TRUE /*xss*/);
$e = "XXAvalueYY";
if (strcmp($r, $e) != 0) {
rg_log("util.php: $t: not working (r=$r e=$e)!");
exit(1);
}
$t = "test rg_template with conditional formating (multiline)";
rg_log($t);
$data = array("X" => "1", "A" => "Avalue", "B" => "Bvalue");
$r = rg_template("t3/c1", $data, TRUE /*xss*/);
$e = "XXAvalueYY";
if (strcmp($r, $e) != 0) {
rg_log("util.php: $t: not working (r=$r e=$e)!");
exit(1);
}
$t = "test rg_template with conditional formating (nested 1)";
rg_log($t);
$data = array("X" => "1", "Y" => "1", "A" => "Avalue", "B" => "Bvalue",
"R" => "Rvalue", "T" => "Tvalue");
$r = rg_template("t3/c3", $data, TRUE /*xss*/);
$e = "XXRvalueZZYY";
if (strcmp($r, $e) != 0) {
rg_log("util.php: $t: not working (r=$r e=$e)!");
exit(1);
}
$t = "test rg_template with conditional formating (nested 2)";
rg_log($t);
$data = array("X" => "1", "Y" => "0", "A" => "Avalue", "B" => "Bvalue",
"R" => "Rvalue", "T" => "Tvalue");
$r = rg_template("t3/c3", $data, TRUE /*xss*/);
$e = "XXTvalueZZYY";
if (strcmp($r, $e) != 0) {
rg_log("util.php: $t: not working (r=$r e=$e)!");
exit(1);
}
$t = "test rg_template with conditional formating (nested 3)";
rg_log($t);
$data = array("X" => "0", "Y" => "1", "A" => "Avalue", "B" => "Bvalue",
"R" => "Rvalue", "T" => "Tvalue");
$r = rg_template("t3/c3", $data, TRUE /*xss*/);
$e = "XXBvalueYY";
if (strcmp($r, $e) != 0) {
rg_log("util.php: $t: not working (r=$r e=$e)!");
exit(1);
}
$t = "test rg_template with conditional formating (nested nested 1)";
rg_log($t);
$data = array("X" => "1", "Y" => "1", "Z" => "1");
$r = rg_template("t3/c4", $data, TRUE /*xss*/);
$r = preg_replace('/\s/', '', $r);
$e = "XXTRUE_LEVEL_2YY";
if (strcmp($r, $e) != 0) {
rg_log("util.php: $t: not working (r=$r e=$e)!");
exit(1);
}
$t = "test rg_template with conditional formating (nested nested 2)";
rg_log($t);
$data = array("X" => "1", "Y" => "0", "Z" => "1");
$r = rg_template("t3/c4", $data, TRUE /*xss*/);
$r = preg_replace('/\s/', '', $r);
$e = "XXFALSE_LEVEL_1YY";
if (strcmp($r, $e) != 0) {
rg_log("util.php: $t: not working (r=$r e=$e)!");
exit(1);
}
$t = "test rg_template with conditional formating (nested nested 2)";
rg_log($t);
$data = array("X" => "0", "Y" => "1", "Z" => "1");
$r = rg_template("t3/c4", $data, TRUE /*xss*/);
$r = preg_replace('/\s/', '', $r);
$e = "XXFALSE_LEVEL_0YY";
if (strcmp($r, $e) != 0) {
rg_log("util.php: $t: not working (r=$r e=$e)!");
exit(1);
}
$t = "test rg_template with conditional formating (nested nested 3)";
rg_log($t);
$data = array("X" => "0", "Y" => "0", "Z" => "0");
$r = rg_template("t3/c5", $data, TRUE /*xss*/);
$r = preg_replace('/\s/', '', $r);
$e = "XX-X0Y0Z0-YY";
if (strcmp($r, $e) != 0) {
rg_log("util.php: $t: not working (r=$r e=$e)!");
exit(1);
}
$t = "test rg_template with conditional formating (nested nested 4)";
rg_log($t);
$data = array("X" => "0", "Y" => "1", "Z" => "0");
$r = rg_template("t3/c5", $data, TRUE /*xss*/);
$r = preg_replace('/\s/', '', $r);
$e = "XX-X0Y1Z0-YY";
if (strcmp($r, $e) != 0) {
rg_log("util.php: $t: not working (r=$r e=$e)!");
exit(1);
}
$t = "test rg_template with conditional formating (empty)";
rg_log($t);
$data = array();
$r = rg_template("t3/c6", $data, TRUE /*xss*/);
$r = preg_replace('/\s/', '', $r);
$e = "A";
if (strcmp($r, $e) != 0) {
rg_log("util.php: $t: not working (r=$r e=$e)!");
exit(1);
}
$t = "test rg_template with conditional formating (quotes)";
rg_log($t);
$data = array("a" => "abc");
$r = rg_template("t3/c6b", $data, TRUE /*xss*/);
$r = preg_replace('/\s/', '', $r);
$e = "AY";
if (strcmp($r, $e) != 0) {
rg_log("util.php: $t: not working (r=$r e=$e)!");
exit(1);
}
$t = "test rg_template with conditional formating (!empty)";
rg_log($t);
$data = array("AAA" => "");
$r = rg_template("t3/c7", $data, TRUE /*xss*/);
$r = preg_replace('/\s/', '', $r);
$e = "B";
if (strcmp($r, $e) != 0) {
rg_log("util.php: $t: not working (r=$r e=$e)!");
exit(1);
}
$t = "test rg_template with conditional formating (a variable contains '{{')";
rg_log($t);
$data = array("AAA" => "1", "BBB" => "}}", "CCC" => "{{");
$r = rg_template("t3/c8", $data, TRUE /*xss*/);
$r = preg_replace('/\s/', '', $r);
$e = "}}";
if (strcmp($r, $e) != 0) {
rg_log("util.php: $t: not working (r=[$r] e=[$e])!");
exit(1);
}
$t = "test rg_template with conditional formating: false branch is empty)";
rg_log($t);
$data = array("X" => "abc");
$r = rg_template("t3/c9", $data, TRUE /*xss*/);
$r = preg_replace('/\s/', '', $r);
$e = "XXBLABLABLAabcYYabc";
if (strcmp($r, $e) != 0) {
rg_log("util.php: $t: not working (r=[$r] e=[$e])!");
exit(1);
}
$t = "test rg_template with conditional formating: false branch is empty, no space stripping)";
rg_log($t);
$data = array("X" => "abc");
$r = rg_template("t3/c9", $data, TRUE /*xss protection*/);
$e = "XX\n\tBLA\n\tBLA\n\tBLA\n\tabc\nYY\nabc\n";
if (strcmp($r, $e) != 0) {
rg_log("util.php: $t: not working (r=[$r] e=[$e])!");
exit(1);
}
rg_log('');
rg_log_enter('Testing rg_template with file template');
$a = array('aaa' => 'ccc');
$r = trim(rg_template('template1.html', $a, TRUE /*xss*/));
$e = 'T1S T2S ccc T2E T1E';
if (strcmp($r, $e) != 0) {
rg_log('TEMPLATE: is not working correctly'
. ' [' . $r . '] != [' . $e . ']');
exit(1);
}
rg_log_exit();
$t = "test rg_copy_tree";
rg_log($t);
$r = rg_copy_tree("tree1", "tree1.copy", 0755);
if ($r !== TRUE) {
rg_log("util.php: $t: not working!");
exit(1);
}
if (!file_exists("tree1.copy/a/f2")) {
rg_log("util.php: $t(2): not working!");
exit(1);
}
$t = "rg_dir_load";
rg_log($t);
$x = rg_dir_load("/non_existing_dir");
if ($x !== FALSE) {
rg_log("$t(non_existing) is not working right!");
exit(1);
}
$t = "rg_dir_load_pattern";
rg_log($t);
$x = rg_dir_load_pattern("util/dir_pattern", "err-.*");
$x2 = implode("", $x);
$e = "err-asasasas";
if (strcmp($x2, $e) != 0) {
rg_log("$t() is not working right ($r != $e)!");
exit(1);
}
$src = array();
$a = array("u" => "uval", "HTML:A" => "Aval");
$x = rg_array_merge($src, 'X', $a);
if (strcmp($x['X']['u'], "uval") != 0) {
rg_log("array_merge is not working correctly (2)!");
print_r($x);
exit(1);
}
if (strcmp($x['X']['HTML:A'], "Aval") != 0) {
rg_log("array_merge is not working correctly (1)!");
print_r($x);
exit(1);
}
// rg_re_repo_git($organization, $user, $repo)
$r = rg_re_repo_git(1, 'u1<', 'repo2ș');
$e = 'git://localhost/u1%3C/repo2%C8%99';
if (strcmp($r, $e) != 0) {
rg_log("re_repo_git is wrong [$r] != [$e]!");
exit(1);
}
rg_log('');
rg_log_enter('rg_exec2');
// Define helpers
function cb_input($index, &$a, $stream)
{
rg_log('cb_input[' . $index . '] stream=' . $stream);
switch ($stream) {
case 1: rg_log(' stdout: ' . $a['in_buf']); break;
case 1: rg_log(' stderr: ' . $a['err_buf']); break;
}
$a['out_buf'] .= ' send_something_from_cb_input_' . $index . "\n";
}
function cb_output($index, &$a)
{
rg_log('cb_output[' . $index . ']');
$a['out_buf'] .= ' generated_output_' . $index . "\n";
// we do not want anymore to be called
rg_log('Unsetting cb_output');
unset($a['cb_output']);
}
function cb_error($index, &$a, $msg)
{
rg_log('cb_error[' . $index . ']: ' . $msg);
// If we need to restart the command, use this:
//$a['needs_restart'] = TRUE;
}
function cb_idle($index, &$a)
{
rg_log('cb_idle[' . $index . ']');
$a['done'] = TRUE;
}
function cb_finish($index, &$a, $exitcode)
{
rg_log('cb_finish[' . $index . ']: exitcode=' . $exitcode);
}
function cb_tick(&$a)
{
rg_log('cb_tick');
$a['my'] = 'tick_was_here';
}
$a = array(
'cmds' => array(
'cmd1' => array(
'cmd' => 'echo first1; read a; sleep 2; echo last1; echo "err1-[${a}]" 1>&2',
'cb_input' => 'cb_input',
'cb_output' => 'cb_output',
'cb_error' => 'cb_error',
'cb_idle' => 'cb_idle',
'cb_finish' => 'cb_finish',
'out_buf' => 'aaa1'
),
'cmd2' => array(
'cmd' => 'echo first2; read a; sleep 2; echo last2; echo "err2-[${a}]" 1>&2',
'cb_input' => 'cb_input',
'cb_output' => 'cb_output',
'cb_error' => 'cb_error',
'cb_idle' => 'cb_idle',
'cb_finish' => 'cb_finish',
'out_buf' => 'aaa2'
)
),
'cb_tick' => 'cb_tick'
);
$r = rg_exec2($a);
if ($r['ok'] != 1) {
rg_log('rg_exec2 failed: ' . $r['errmsg'] . '!');
exit(1);
}
$e = "first1\nlast1\n";
if (strcmp($a['cmds']['cmd1']['in_buf'], $e) != 0) {
rg_log('cmd1 in_buf is not ok: ['
. $a['cmds']['cmd1']['in_buf'] . '] != [' . $e . ']!');
exit(1);
}
$e = "first2\nlast2\n";
if (strcmp($a['cmds']['cmd2']['in_buf'], $e) != 0) {
rg_log('cmd2 in_buf is not ok: ['
. $a['cmds']['cmd2']['in_buf'] . '] != [' . $e . ']!');
exit(1);
}
$e = "err2-[aaa2 generated_output_cmd2]\n";
if (strcmp($a['cmds']['cmd2']['err_buf'], $e) != 0) {
rg_log('cmd2 err_buf is not ok: ['
. $a['cmds']['cmd2']['err_buf'] . '] != [' . $e . ']!');
exit(1);
}
$e = 'tick_was_here';
if (!isset($a['my']) || (strcmp($a['my'], $e) != 0)) {
rg_log('my is not ok (cb_tick was not called?): ['
. (isset($a['my']) ? $a['my'] : '') . '] != [' . $e . ']!');
exit(1);
}
rg_log_exit();
rg_log("OK!");
?>