<?php
error_reporting(E_ALL | E_STRICT);
ini_set("track_errors", "On");
$test_normal = TRUE;
$INC = dirname(__FILE__) . "/../inc";
require_once(dirname(__FILE__) . "/config.php");
require_once($INC . "/init.inc.php");
require_once($INC . "/user.inc.php");
require_once("helpers.inc.php");
require_once("http.inc.php");
rg_log_set_file("wh_http.log");
require_once("common.php");
$_testns = 'wh_http';
$ports = array();
$ports[1] = 64000 + (rand(0, 100000) + time()) % 1000;
$ports[2] = $ports[1] + 1;
$ports[3] = $ports[2] + 1;
rg_log('ports=' . rg_array2string($ports));
function clean()
{
rg_log_set_file("wh_http_clean.log");
for ($id = 1; $id <= 3; $id++)
rg_exec('fuser -k -9 wh-stunnel-' . $id . '.log', '', FALSE, FALSE, FALSE);
}
rg_log('');
rg_log('Generating certificates...');
$r = rg_exec('./ca.sh wh', '', FALSE, FALSE, FALSE);
if (!strstr($r['data'], 'CA_SH_OK')) {
rg_log_ml('data: ' . $r['data']);
rg_log('Cannot generate certificates!');
exit(1);
}
rg_log('');
rg_log('Preparing stunnel conf file...');
$x = file_get_contents('wh-stunnel.conf');
if ($x === FALSE) {
rg_log('Cannot load conf file');
exit(1);
}
rg_log('');
rg_log_enter('starting stunnels...');
for ($i = 1; $i <= 3; $i++) {
rg_log('Starting stunnel' . $i . '..');
// Only stunnel 3 verify the cert
$verify = ($i == 3) ? 0 : 2;
$y = str_replace('@@port@@', $ports[$i], $x);
$y = str_replace('@@verify@@', $verify, $y);
$y = str_replace('@@id@@', $i, $y);
$y = str_replace('@@cwd@@', __DIR__, $y);
file_put_contents('wh-stunnel.conf-' . $i . '.tmp', $y);
$pid = pcntl_fork();
if ($pid == -1) {
rg_log('Cannot fork');
exit(1);
}
if ($pid == 0) { //child
rg_log_set_file('wh-stunnel-' . $i . '-rg.log');
rg_exec('stunnel wh-stunnel.conf-' . $i . '.tmp'
. ' 1>wh-stunnel-' . $i . '-1.log'
. ' 2>wh-stunnel-' . $i . '-2.log',
'', FALSE, FALSE, FALSE);
exit(0);
}
rg_log('Started stunnel with pid ' . $pid);
}
rg_log_exit();
register_shutdown_function('clean');
rg_log('');
$rg_ui = array('confirmed' => 20);
rg_test_create_user($db, $rg_ui);
$info = array('id' => $rg_ui['username']);
prepare_http($info);
$key1 = 'DEBUG::' . $rg_ui['uid'] . '::webhooks::' . $ports[1];
$key2 = 'DEBUG::' . $rg_ui['uid'] . '::webhooks::' . $ports[2];
$key3 = 'DEBUG::' . $rg_ui['uid'] . '::webhooks::' . $ports[3];
rg_log('');
rg_log('Login...');
$r = test_login($test_url, $rg_ui);
if ($r === FALSE) {
rg_log("Cannot login!");
exit(1);
}
rg_log('');
rg_log('Registering webhook1...');
$extra = array(
'wh::description' => 'description1 <xss>',
'wh::repo' => '',
'wh::refname' => '',
'wh::idata::url' => 'https://localhost:' . $ports[1] . '/wh.html',
'wh::idata::events[C]' => 'on',
'wh::idata::events[P]' => 'on',
'wh::idata::events[B]' => 'on',
'wh::idata::key' => 'key1 <xss>',
'wh::idata::opaque' => $ports[1],
'wh::idata::client_cert' => '',
'wh::idata::itype' => 0,
'wh::idata::client_ca_cert' => file_get_contents('ca/wh/certs/cacert.pem')
);
rg_test_wh_add_edit($db, $rg_ui, 'http', 'generic', $extra);
rg_log('');
rg_log('Registering webhook2...');
$extra = array(
'wh::htype' => 'http',
'wh::hsubtype' => 'generic',
'wh::description' => 'description1 <xss>',
'wh::repo' => '',
'wh::refname' => '',
'wh::idata::url' => 'https://localhost:' . $ports[2] . '/wh.html',
'wh::idata::events[C]' => 'on',
'wh::idata::events[P]' => 'on',
'wh::idata::events[B]' => 'on',
'wh::idata::key' => 'key2 <xss>',
'wh::idata::opaque' => $ports[2],
'wh::idata::itype' => 1,
'wh::idata::client_cert' => file_get_contents('ca/wh/certs/client.pem')
. file_get_contents('ca/wh/private/client.key'),
'wh::idata::client_ca_cert' => file_get_contents('ca/wh/certs/cacert.pem')
);
rg_test_wh_add_edit($db, $rg_ui, 'http', 'generic', $extra);
rg_log('');
rg_log('Registering webhook3...');
$extra = array(
'wh::description' => 'description1 <xss>',
'wh::repo' => '',
'wh::refname' => '',
'wh::idata::url' => 'https://localhost:' . $ports[3] . '/wh.html',
'wh::idata::events[C]' => 'on',
'wh::idata::events[P]' => 'on',
'wh::idata::events[B]' => 'on',
'wh::idata::key' => 'key2 <xss>',
'wh::idata::opaque' => $ports[3],
'wh::idata::itype' => 2,
'wh::idata::custom_body' => 'This is the custom_body commit=##commit## ip=##ip##',
'wh::idata::client_cert' => '',
'wh::idata::client_ca_cert' => file_get_contents('ca/wh/certs/cacert.pem')
);
rg_test_wh_add_edit($db, $rg_ui, 'http', 'generic', $extra);
rg_log('Finding out the ids...');
$r = test_wait_cache('wh' . '::' . $rg_ui['uid'], 50);
rg_log_ml('r=' . print_r($r, TRUE));
$t = array_keys($r['list']);
if (count($t) != 3) {
rg_log('We do not have 3 ids!');
exit(1);
}
$wh_id1 = $t[0];
$wh_id2 = $t[1];
$wh_id3 = $t[2];
rg_log('wh_id1=' . $wh_id1);
rg_log('wh_id2=' . $wh_id2);
rg_log('wh_id3=' . $wh_id3);
rg_log('');
rg_log('Creating a repo and waiting for trigger');
$repo = array();
rg_test_create_repo($db, $rg_ui, $repo);
rg_log('');
rg_log_enter('Testing if the curl posted with success (wh1) (should not work)');
$r = test_wait_cache($key1 . '::' . $wh_id1, 50);
if (strcmp($r, "BAD") != 0) {
$_k = 'wh::' . $rg_ui['uid'] . '::list::' . $wh_id1 . '::last_output';
$_e = test_wait_cache($_k, 50);
rg_log_ml('_e:' . $_e);
rg_log('Seems wh1 executed correctly without client'
. ' cert (r=' . $r . ')!');
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('Testing if the curl posted with success (wh2) (should work)');
$r = test_wait_cache($key2 . '::' . $wh_id2, 50);
if (strcmp($r, "OK") != 0) {
$_k = 'wh::' . $rg_ui['uid'] . '::list::' . $wh_id2 . '::last_output';
$_e = test_wait_cache($_k, 50);
rg_log_ml('_e:' . $_e);
rg_log('Seems wh2 did not returned success'
. ' (r=' . $r . ')!');
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('Testing if the curl posted with success (wh3) (should work)');
$r = test_wait_cache($key3 . '::' . $wh_id3, 50);
if (strcmp($r, "OK") != 0) {
$_k = 'wh::' . $rg_ui['uid'] . '::list::' . $wh_id3 . '::last_output';
$_e = test_wait_cache($_k, 50);
rg_log_ml('_e:' . $_e);
rg_log('Seems wh3 did not returned success'
. ' (r=' . $r . ')!');
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('Testing the edit of webhook1...');
$extra = array(
'wh::id' => $wh_id1,
'wh::description' => 'desc2 <xss>',
'wh::repo' => '.*',
'wh::refname' => '..*',
'wh::flags[D]' => 'on',
'wh::idata::url' => 'https://localhost:' . $ports[1] . '/wh.html',
'wh::idata::events[C]' => 'on',
'wh::idata::events[B]' => 'on',
'wh::idata::key' => 'another key <xss>',
'wh::idata::opaque' => 'xxx',
'wh::idata::itype' => 1,
'wh::idata::client_cert' => 'abc <xss>',
'wh::idata::client_ca_cert' => 'zzz <xss>'
);
rg_test_wh_add_edit($db, $rg_ui, 'http', 'generic', $extra);
$sql = "SELECT * FROM webhooks WHERE uid = " . $rg_ui['uid']
. " AND id = " . $wh_id1;
$res = rg_sql_query($db, $sql);
if ($res === FALSE) {
rg_log('Cannot do query!');
exit(1);
}
$row = rg_sql_fetch_array($res);
rg_sql_free_result($res);
$row['idata'] = rg_unserialize($row['idata']);
$key = 'wh' . '::' . $rg_ui['uid'] . '::' . 'list' . '::' . $wh_id1;
$c = test_wait_cache($key, 50);
$list = array('htype' => 'http', 'events' => 'CB', 'repo' => '.*',
'refname' => '..*',
'description' => 'desc2 <xss>', 'key' => 'another key <xss>',
'opaque' => 'xxx', 'itype' => '1', 'flags' => 'D',
'client_cert' => 'abc <xss>', 'client_ca_cert' => 'zzz <xss>');
foreach ($list as $k => $v) {
if (isset($row[$k]))
$a = $row[$k];
else if (isset($row['idata'][$k]))
$a = $row['idata'][$k];
else {
rg_log('Key [' . $k . '] not found! Bad!');
exit(1);
}
if (strcmp($a, $v) != 0) {
rg_log_ml('row: ' . print_r($row, TRUE));
rg_log("db: Seems that [$k] has not been updated"
. " [" . $a . "] != " . $v);
exit(1);
}
if (isset($c[$k]))
$a = $c[$k];
else
$a = $c['idata'][$k];
if (strcmp($a, $v) != 0) {
rg_log_ml('c: ' . print_r($c, TRUE));
rg_log("cache: Seems that [$k] has not been updated"
. " [" . $a . "] != " . $v);
exit(1);
}
}
rg_log_exit();
rg_log('');
rg_log_enter('Testing the delete - loading form...');
$data = array();
$headers = array();
$r = do_req($info, $test_url . "/op/settings/wh/list", $data, $headers);
if ($r === FALSE) {
rg_log("Cannot load list form.");
exit(1);
}
if (!isset($r['tokens']['wh_list'])) {
rg_log_ml('tokens: ' . print_r($r['tokens'], TRUE));
rg_log('Cannot find wh_list token!');
exit(1);
}
$good_token = $r['tokens']['wh_list'];
$data = array( 'delete' => 1,
'token' => $good_token,
'delete_list[' . $wh_id1 . ']' => 'on');
$r = do_req($info, $test_url . "/op/settings/wh/list", $data, $headers);
if (!strstr($r['body'], 'success')) {
rg_log_ml('r[body]: ' . print_r($r['body'], TRUE));
rg_log("Cannot delete webhook!");
exit(1);
}
$sql = "SELECT id FROM webhooks WHERE id = " . $wh_id1;
$res = rg_sql_query($db, $sql);
if ($res === FALSE) {
rg_log('Cannot do query!');
exit(1);
}
$rows = rg_sql_num_rows($res);
rg_sql_free_result($res);
if ($rows != 0) {
rg_log("Cannot delete webhook - sql still returns data!");
exit(1);
}
$key = 'user::' . $rg_ui['uid'] . '::wh::' . $wh_id1;
rg_cache_core_unset($key); // else we will get data from local mem!
$r = rg_cache_get($key);
if (strcmp($r, '') != 0) {
rg_log_ml($key . ': ' . print_r($r, TRUE));
rg_log('Deleted webhooks are still in cache!');
exit(1);
}
rg_log_exit();
rg_log('OK!');