xaizek / rocketgit (License: AGPLv3+) (since 2018-12-09)
Light and fast Git hosting solution suitable to serve both as a hub or as a personal code storage with its tickets, pull requests, API and much more.
<root> / tests / wh_http.php (76214d556bd0eae7ecfe48c7b51a6d94baac28d6) (9,584B) (mode 100644) [raw]
<?php
error_reporting(E_ALL | E_STRICT);
ini_set("track_errors", "On");

$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");

$rg_sql = "host=localhost user=rocketgit dbname=rocketgit connect_timeout=10";
$rg_no_db = TRUE;
require_once("common.php");

$_testns = 'wh_http';
$rg_cache_enable = TRUE;
$rg_cache_debug = TRUE;
$rg_event_socket = "/var/lib/rocketgit/sockets/event.sock";

$port1 = 64000 + (rand(0, 100000) + time()) % 1000;
$port2 = $port1 + 1;
$port3 = $port2 + 1;
rg_log('port1=' . $port1 . ' port2=' . $port2 . ' port3=' . $port3);

function clean()
{
	system('fuser -k -9 wh-stunnel-1.log 1>/dev/null 2>&1');
	system('fuser -k -9 wh-stunnel-2.log 1>/dev/null 2>&1');
	system('fuser -k -9 wh-stunnel-3.log 1>/dev/null 2>&1');

	@unlink('wh-stunnel.conf-1.tmp');
	@unlink('wh-stunnel.conf-2.tmp');
	@unlink('wh-stunnel.conf-3.tmp');
}
register_shutdown_function('clean');


rg_log('');
rg_log('Generating certificates...');
$r = shell_exec('./ca.sh wh');
if (!strstr($r, 'CA_SH_OK')) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	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);
}

$y = str_replace('@@port@@', $port1, $x);
$y = str_replace('@@verify@@', '2', $y);
$y = str_replace('@@id@@', '1', $y);
file_put_contents('wh-stunnel.conf-1.tmp', $y);

$y = str_replace('@@port@@', $port2, $x);
$y = str_replace('@@verify@@', '2', $y);
$y = str_replace('@@id@@', '2', $y);
file_put_contents('wh-stunnel.conf-2.tmp', $y);

$y = str_replace('@@port@@', $port3, $x);
$y = str_replace('@@verify@@', '0', $y);
$y = str_replace('@@id@@', '3', $y);
file_put_contents('wh-stunnel.conf-3.tmp', $y);


rg_log('');
rg_log('Starting stunnel1...');
$pid = pcntl_fork();
if ($pid == -1) {
	rg_log('Cannot fork');
	exit(1);
}
if ($pid == 0) { //child
	$r = exec('stunnel wh-stunnel.conf-1.tmp 2>/dev/null');
	exit(0);
}
rg_log('Started stunnel with pid ' . $pid);


rg_log('');
rg_log('Starting stunnel2...');
$pid = pcntl_fork();
if ($pid == -1) {
	rg_log('Cannot fork');
	exit(1);
}
if ($pid == 0) { //child
	$r = exec('stunnel wh-stunnel.conf-2.tmp 2>/dev/null');
	exit(0);
}
rg_log('Started stunnel with pid ' . $pid);


rg_log('');
rg_log('Starting stunnel3...');
$pid = pcntl_fork();
if ($pid == -1) {
	rg_log('Cannot fork');
	exit(1);
}
if ($pid == 0) { //child
	$r = exec('stunnel wh-stunnel.conf-3.tmp 2>/dev/null');
	exit(0);
}
rg_log('Started stunnel with pid ' . $pid);


rg_log('');
rg_log("Creating a user...");
rg_test_create_user($db, $rg_ui);
$key1 = 'DEBUG::' . $rg_ui['uid'] . '::webhooks::' . $port1;
$key2 = 'DEBUG::' . $rg_ui['uid'] . '::webhooks::' . $port2;
$key3 = 'DEBUG::' . $rg_ui['uid'] . '::webhooks::' . $port3;


rg_log('');
rg_log('Login...');
$r = test_login($test_url, $rg_ui, $good_sid);
if ($r === FALSE) {
	rg_log("Cannot login!");
	exit(1);
}


rg_log('');
rg_log('Registering webhook1...');
$extra = array(
	'wh::htype' => 'http',
	'wh::url' => 'https://localhost:' . $port1 . '/wh.html',
	'wh::events[C]' => 'on',
	'wh::events[P]' => 'on',
	'wh::events[B]' => 'on',
	'wh::description' => 'description1 <xss>',
	'wh::key' => 'key1 <xss>',
	'wh::opaque' => $port1,
	'wh::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, $good_sid, 'http', $extra);


rg_log('');
rg_log('Registering webhook2...');
$extra = array(
	'wh::htype' => 'http',
	'wh::url' => 'https://localhost:' . $port2 . '/wh.html',
	'wh::events[C]' => 'on',
	'wh::events[P]' => 'on',
	'wh::events[B]' => 'on',
	'wh::description' => 'description1 <xss>',
	'wh::key' => 'key2 <xss>',
	'wh::opaque' => $port2,
	'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, $good_sid, 'http', $extra);


rg_log('');
rg_log('Registering webhook3...');
$extra = array(
	'wh::htype' => 'http',
	'wh::url' => 'https://localhost:' . $port3 . '/wh.html',
	'wh::events[C]' => 'on',
	'wh::events[P]' => 'on',
	'wh::events[B]' => 'on',
	'wh::description' => 'description1 <xss>',
	'wh::key' => 'key2 <xss>',
	'wh::opaque' => $port3,
	'wh::idata::itype' => 1,
	'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, $good_sid, 'http', $extra);


rg_log('Finding out the ids...');
for ($i = 0; $i < 10; $i++) {
	$r = rg_cache_get('wh' . '::' . $rg_ui['uid']);
	if ($r !== FALSE)
		break;
	sleep(1);
}
if ($r === FALSE) {
	rg_log('Cannot get id from cache');
	exit(1);
}
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('Testing if the curl posted with success (wh1)');
for ($i = 0; $i < 10; $i++) {
	$r = rg_cache_get($key1 . '::' . $wh_id1);
	rg_log_ml('cache: ' . $r);
	if ($r !== FALSE)
		break;
	sleep(1);
}
if ($r === FALSE) {
	rg_log('Seems the event does not set the cache!');
	exit(1);
}
if (strcmp($r, "BAD") != 0) {
	rg_log('Seems wh1 executed correctly without client'
		. ' cert (r=' . $r . ')!');
	exit(1);
}


rg_log('');
rg_log('Testing if the curl posted with success (wh2)');
for ($i = 0; $i < 10; $i++) {
	$r = rg_cache_get($key2 . '::' . $wh_id2);
	rg_log_ml('cache: ' . print_r($r, TRUE));
	if ($r !== FALSE)
		break;
	sleep(1);
}
if (strcmp($r, "OK") != 0) {
	rg_log('Seems wh2 did not returned success'
		. ' (r=' . $r . ')!');
	exit(1);
}


rg_log('');
rg_log('Testing if the curl posted with success (wh3)');
for ($i = 0; $i < 10; $i++) {
	$r = rg_cache_get($key3 . '::' . $wh_id3);
	rg_log_ml('cache: ' . print_r($r, TRUE));
	if ($r !== FALSE)
		break;
	sleep(1);
}
if (strcmp($r, "OK") != 0) {
	rg_log('Seems wh3 did not returned success'
		. ' (r=' . $r . ')!');
	exit(1);
}


rg_log('');
rg_log('Testing the edit of webhook1...');
$extra = array(
	'wh::htype' => 'http',
	'wh::url' => 'https://localhost:' . $port1 . '/wh.html',
	'wh::id' => $wh_id1,
	'wh::events[C]' => 'on',
	'wh::events[B]' => 'on',
	'wh::description' => 'desc2 <xss>',
	'wh::key' => 'another key <xss>',
	'wh::opaque' => 'xxx',
	'wh::idata::itype' => 1,
	'wh::idata::flags[I]' => 'on',
	'wh::idata::client_cert' => 'abc <xss>',
	'wh::idata::client_ca_cert' => 'zzz <xss>'
	);
rg_test_wh_add_edit($db, $rg_ui, $good_sid, 'http', $extra);
$sql = "SELECT * FROM webhooks WHERE uid = " . $rg_ui['uid']
	. " AND id = " . $wh_id1;
$res = rg_sql_query($db, $sql);
$row = rg_sql_fetch_array($res);
rg_sql_free_result($res);
$row['idata'] = unserialize($row['idata']);
$key = 'wh' . '::' . $rg_ui['uid'] . '::' . 'list' . '::' . $wh_id1;
rg_cache_core_unset($key); // else we will get previous copy!
$c = rg_cache_get($key);
$list = array('htype' => 'http', 'events' => 'CB',
	'description' => 'desc2 <xss>', 'key' => 'another key <xss>',
	'opaque' => 'xxx', 'itype' => '1', 'flags' => 'I',
	'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('');
rg_log_enter('Testing the delete - loading form...');
$data = array();
$headers = array("Cookie: sid=" . $good_sid);
$r = do_req($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($test_url . "/op/settings/wh/list", $data, $headers);
if (!strstr($r['body'], 'deleted with 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);
$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!");
?>
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/rocketgit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/rocketgit

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master