<?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.log"); $rg_sql = "host=localhost user=rocketgit dbname=rocketgit connect_timeout=10"; $rg_no_db = TRUE; require_once("common.php"); $_testns = 'wh'; $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; rg_log('port1=' . $port1 . ' port2=' . $port2); function clean() { system('fuser -k wh-stunnel-1.log 1>/dev/null 2>&1'); system('fuser -k wh-stunnel-2.log 1>/dev/null 2>&1'); @unlink('wh-stunnel.conf-1.tmp'); @unlink('wh-stunnel.conf-2.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); file_put_contents('wh-stunnel.conf-1.tmp', $y); $y = str_replace('@@port@@', $port2, $x); file_put_contents('wh-stunnel.conf-2.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("Creating a user..."); rg_test_create_user($db, $rg_ui); $key1 = 'DEBUG::' . $rg_ui['uid'] . '::webhooks::' . $port1; $key2 = 'DEBUG::' . $rg_ui['uid'] . '::webhooks::' . $port2; 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::url' => 'https://localhost:' . $port1 . '/wh.html', 'wh::type' => 0, '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::client_ca_cert' => file_get_contents('ca/wh/certs/cacert.pem') ); rg_test_wh_add_edit($db, $rg_ui, $good_sid, $extra); rg_log(''); rg_log('Registering webhook2...'); $extra = array( 'wh::url' => 'https://localhost:' . $port2 . '/wh.html', 'wh::type' => 1, 'wh::events[C]' => 'on', 'wh::events[P]' => 'on', 'wh::events[B]' => 'on', 'wh::description' => 'description1 <xss>', 'wh::key' => 'key2 <xss>', 'wh::opaque' => $port2, 'wh::client_cert' => file_get_contents('ca/wh/certs/client.pem') . file_get_contents('ca/wh/private/client.key'), 'wh::client_ca_cert' => file_get_contents('ca/wh/certs/cacert.pem') ); rg_test_wh_add_edit($db, $rg_ui, $good_sid, $extra); rg_log('Finding out the ids...'); for ($i = 0; $i < 10; $i++) { $r = rg_cache_get('user::' . $rg_ui['uid'] . '::wh'); 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); $wh_id1 = $t[0]; $wh_id2 = $t[1]; rg_log('wh_id1=' . $wh_id1); rg_log('wh_id2=' . $wh_id2); 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 the edit of webhook1...'); $extra = array( 'wh::url' => 'https://localhost:' . $port1 . '/wh.html', 'wh::id' => $wh_id1, 'wh::type' => 1, 'wh::flags[I]' => 'on', 'wh::events[C]' => 'on', 'wh::events[B]' => 'on', 'wh::description' => 'desc2 <xss>', 'wh::key' => 'another key <xss>', 'wh::opaque' => 'xxx', 'wh::client_cert' => 'abc <xss>', 'wh::client_ca_cert' => 'zzz <xss>' ); rg_test_wh_add_edit($db, $rg_ui, $good_sid, $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); $key = 'user' . '::' . $rg_ui['uid'] . '::' . 'wh' . '::' . $wh_id1; rg_cache_core_unset($key); // else we will get previous copy! $c = rg_cache_get($key); $list = array('flags' => 'I', 'events' => 'CB', 'type' => '1', 'description' => 'desc2 <xss>', 'key' => 'another key <xss>', 'opaque' => 'xxx', 'client_cert' => 'abc <xss>', 'client_ca_cert' => 'zzz <xss>'); foreach ($list as $k => $v) { if (strcmp($row[$k], $v) != 0) { rg_log_ml('row: ' . print_r($row, TRUE)); rg_log("db: Seems that [$k] has not been updated [" . $row[$k] . "] != " . $v); exit(1); } if (strcmp($c[$k], $v) != 0) { rg_log_ml('c: ' . print_r($c, TRUE)); rg_log("cache: Seems that [$k] has not been updated [" . $c[$k] . "] != " . $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!"); ?>