<?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 html escape - attr value');
$s = '<a href="@@a@@">'; // TODO: we may do a @@ATTR_ESCAPE:a@@ to escape only ' and "
$data = array('a' => '\'ș/ț"<>');
$e = '<a href="'ș/ț"<>">';
$r = rg_template_string($s, 0, $data, TRUE /*xss*/);
if (strcmp($r, $e) != 0) {
rg_log(' fail: [' . $r . '] != [' . $e . ']!');
exit(1);
}
rg_log('Testing html escape - body');
$s = '<b>@@a@@</b>';
$data = array('a' => '\'ș/ț"<>&');
$e = '<b>'ș/ț"<>&</b>';
$r = rg_template_string($s, 0, $data, TRUE /*xss*/);
if (strcmp($r, $e) != 0) {
rg_log(' fail: [' . $r . '] != [' . $e . ']!');
exit(1);
}
rg_log('Testing html escape - para');
$s = '<a href="/a?para1=@@URL_ESCAPE:a@@">';
$data = array('a' => 'ș/ț&');
$e = '<a href="/a?para1=%C8%99%2F%C8%9B%26">';
$r = rg_template_string($s, 0, $data, TRUE /*xss*/);
if (strcmp($r, $e) != 0) {
rg_log(' fail: [' . $r . '] != [' . $e . ']!');
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) { global $_rg; rg_log("DEBUG: inc called with v=$v"); return array('value' => $_rg[$v] + 1, 'html' => 0); }
function dec($v) { global $_rg; rg_log("DEBUG: dec called with v=$v"); return array('value' => $_rg[$v] - 1, 'html' => 0); }
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);
}
rg_log('');
rg_log_enter('exec a non existing command');
$r = rg_exec("/xxxx", '', FALSE, FALSE, FALSE);
if ($r['ok'] == 1) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log("running non existing command does not return 0!");
exit(1);
}
$e = 'No such file or directory' . "\n";
if (!strstr($r['stderr'], $e) != 0) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('stderr is not correct; not [' . trim($e) . '\n]!');
exit(1);
}
if ($r['code'] != 127) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('error code must be 127!');
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('exec a normal \'ls\' command');
$r = rg_exec("ls", '', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log("cannot run a command!");
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('force exit code 5');
$r = rg_exec("./util_exit_code.sh 5", '', FALSE, FALSE, FALSE);
if (($r['ok'] == 1) || ($r['code'] != 5)) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log("error code seems to not be propageted!");
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('stdout closes first');
$r = rg_exec("./util_stdout_closes_first.sh", '', FALSE, FALSE, FALSE);
if (($r['ok'] != 1) || ($r['code'] != 0)) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log("stdout_closes_first should return ok/code0!");
exit(1);
}
if (strcmp($r['data'], "stdout\n") != 0) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log("stdout returned value is not correct!");
exit(1);
}
if (strcmp($r['stderr'], "stderr1\nstderr2\n") != 0) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log("stderr returned value is not correct!");
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('sleep after closing fds');
$r = rg_exec("./util_sleep_after_closing_fds.sh", '', FALSE, FALSE, FALSE);
if (($r['ok'] != 1) || ($r['code'] != 0)) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log("We need code 0 here!");
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('exec2 - complex');
// Define helpers
function cb_input($index, &$a, $stream)
{
if (isset($a['cb_input_called']))
return;
rg_log('cb_input[' . $index . '] stream=' . $stream);
switch ($stream) {
case 1: rg_log(' stdout: ' . $a['in_buf']); break;
case 2: rg_log(' stderr: ' . $a['err_buf']); break;
}
$a['out_buf'] .= ' send_something_from_cb_input_' . $index . "\n";
$a['cb_input_called'] = 1;
}
function cb_output($index, &$a)
{
if (isset($a['cb_output_called']))
return;
rg_log('cb_output[' . $index . ']');
$a['out_buf'] .= ' generated_output_' . $index . "\n";
$a['cb_output_called'] = 1;
}
function cb_error($index, &$a, $msg)
{
rg_log('cb_error[' . $index . ']: ' . $msg);
// If we need to restart the command, use this:
//$a['restart_delay'] = 0; // in seconds
}
function cb_idle($index, &$a)
{
rg_log('cb_idle[' . $index . ']');
}
function cb_finish($index, &$a, $exitcode)
{
rg_log('cb_finish[' . $index . ']: exitcode=' . $exitcode);
}
function cb_tick($index, &$a)
{
rg_log('cb_tick[' . $index . ']');
$a['my'] = 'tick_was_here';
if (isset($a['cb_output_called']) && isset($a['cb_input_called'])) {
rg_log('cb_tick[' . $index . ']: set out_buf_done to 1');
$a['out_buf_done'] = 1;
}
}
$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',
'cb_tick' => 'cb_tick',
'out_buf' => 'aaa1',
'out_buf_done' => 0
),
'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',
'cb_tick' => 'cb_tick',
'out_buf' => 'aaa2',
'out_buf_done' => 0
)
)
);
$r = rg_exec2($a);
if ($r['ok'] != 1) {
rg_log('rg_exec2 failed: ' . $r['errmsg'] . '!');
exit(1);
}
$e = "first1\nlast1\n";
if (strcmp($r['cmds']['cmd1']['in_buf'], $e) != 0) {
rg_log('cmd1 in_buf is not ok: ['
. $r['cmds']['cmd1']['in_buf'] . '] != [' . $e . ']!');
exit(1);
}
$e = "first2\nlast2\n";
if (strcmp($r['cmds']['cmd2']['in_buf'], $e) != 0) {
rg_log('cmd2 in_buf is not ok: ['
. $r['cmds']['cmd2']['in_buf'] . '] != [' . $e . ']!');
exit(1);
}
$e = "err2-[aaa2 generated_output_cmd2]\n";
if (strcmp($r['cmds']['cmd2']['err_buf'], $e) != 0) {
rg_log('cmd2 err_buf is not ok: ['
. $r['cmds']['cmd2']['err_buf'] . '] != [' . $e . ']!');
exit(1);
}
$e = 'tick_was_here';
if (!isset($r['cmds']['cmd2']['my']) || (strcmp($r['cmds']['cmd2']['my'], $e) != 0)) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('my is not ok (cb_tick was not called?)!');
exit(1);
}
rg_log_exit();
rg_log('');
$t = 'test input decompression';
rg_log_enter($t);
$in = 'this is a test string to be decompressed';
$input = gzencode($in);
$a = array(
'cmds' => array(
'cmd1' => array(
'cmd' => 'cat',
'out_buf' => $input,
'out_buf_helper' => 'rg_exec2_helper_gzip_in',
'out_buf_done' => 0
)
)
);
$r = rg_exec2($a);
if ($r['ok'] != 1) {
rg_log('rg_exec2 failed: ' . $r['errmsg'] . '!');
exit(1);
}
rg_log_ml('r: ' . print_r($r, TRUE));
if (strcmp($r['cmds']['cmd1']['in_buf'], $in) != 0) {
rg_log('cmd1 in_buf is not ok: ['
. $r['cmds']['cmd1']['in_buf'] . '] != [' . $in . ']!');
exit(1);
}
rg_log_exit();
rg_log('');
$t = "test rg_template_table(dir, data, more) with no data";
rg_log_enter($t);
$data = array();
$r = rg_template_table("t1", $data, array("a" => "A"));
$e = "XAX";
if (strcmp($r, $e) != 0) {
rg_log("$t: not working (r=$r e=$e)!");
exit(1);
}
rg_log_exit();
$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("$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("$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("$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("$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("$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("$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("$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("$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("$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("$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("$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("$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("$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("$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("$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("$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("$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("$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("$t: not working!");
exit(1);
}
if (!file_exists("tree1.copy/a/f2")) {
rg_log("$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('');
$s = 'șțaaș';
rg_log_enter('visible_string [' . $s . ']');
$e = $s;
$r = rg_visible_string($s);
if (strcmp($r, $e) != 0) {
rg_log($r . ' != ' . $e . '!');
exit(1);
}
rg_log_exit();
rg_log('');
$s = "ș\xffțaaș\xff";
rg_log_enter('visible_string [' . $s . ']');
$e = 'ș\377țaaș\377';
$r = rg_visible_string($s);
if (strcmp($r, $e) != 0) {
rg_log($r . ' != ' . $e . '!');
exit(1);
}
rg_log_exit();
rg_log('');
$s = "ș\xc8\x7f\\x\a";
rg_log_enter('visible_string [' . $s . '] - missing last byte');
$e = 'ș\310\177\\x\a';
$r = rg_visible_string($s);
if (strcmp($r, $e) != 0) {
rg_log($r . ' != ' . $e . '!');
exit(1);
}
rg_log_exit();
rg_log('');
$s = "ș\xc8a";
rg_log_enter('visible_string [' . $s . '] - invalid UTF-8 body');
$e = 'ș\310a';
$r = rg_visible_string($s);
if (strcmp($r, $e) != 0) {
rg_log($r . ' != ' . $e . '!');
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('rg_str_replace');
$keys = array('a', 'v'); $values = array('A', 'V');
$e = 'a:2:{s:2:"a1";a:1:{s:2:"k1";s:2:"V1";}s:2:"a2";s:2:"V2";}';
$a = array('a1' => array('k1' => 'v1'), 'a2' => 'v2');
$x = rg_str_replace($keys, $values, $a, 0);
$y = serialize($x);
if (strcmp($y, $e) != 0) {
rg_log('\'' . $y . '\'' . ' != \'' . $e . '\'!');
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('rg_php_err');
$e = 'mkdir(): File exists';
$old_log_errors = ini_get('log_errors');
ini_set('log_errors', 0);
$old_error_reporting = ini_get('error_reporting');
ini_set('error_reporting', 0);
mkdir('/tmp');
$y = rg_php_err();
if (strcmp($y, $e) != 0) {
rg_log('\'' . $y . '\'' . ' != \'' . $e . '\'!');
exit(1);
}
ini_set('log_errors', $old_log_errors);
ini_set('error_reporting', $old_error_reporting);
rg_log_exit();
$s = '/a b/c/d/';
$e = '/a%20b/c/d/';
$r = rg_path2url($s);
if (strcmp($r, $e) != 0) {
rg_log('path2url error [' . $r . '] != [' . $e . ']!');
exit(1);
}
rg_log('');
rg_log_enter('rg_dir_size - non existing');
$r = rg_dir_size('temp_repos/non-existing');
if (($r['blocks'] != 0) || ($r['size'] != 0)) {
rg_log('Non existing dir returns non-zero sizes!');
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('rg_dir_size1');
$r = rg_exec('mkdir -p temp_repos && cd temp_repos'
. ' && mkdir -p dir1 && cd dir1'
. ' && echo "aaa" > a'
. ' && echo "bbbbb" > b'
. ' && mkdir -p dir2 && echo "a" > dir2/z'
. ' && ln -f dir2/z zclone'
. ' && mkdir -p empty',
'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('Cannot run command!');
exit(1);
}
$r = rg_dir_size('temp_repos/dir1');
if ($r === FALSE) {
rg_log('Got an error: ' . rg_util_error());
exit(1);
}
if ($r['size'] != 12) {
rg_log('Size is not 12: ' . $r['size']);
exit(1);
}
if ($r['blocks'] != 24) {
rg_log('Size is not 24: ' . $r['size']);
exit(1);
}
rg_log_exit();
rg_prof_log();
rg_log('OK!');