<?php
error_reporting(E_ALL | E_STRICT);
ini_set("track_errors", "On");
$rg_cache_debug = FALSE;
$rg_util_debug = FALSE;
$rg_sql_debug = 100;
$test_normal = TRUE;
$INC = dirname(__FILE__) . "/../inc";
require_once(dirname(__FILE__) . "/config.php");
require_once($INC . "/init.inc.php");
require_once($INC . "/git.inc.php");
require_once("helpers.inc.php");
require_once("http.inc.php");
rg_log_set_file("by_http.log");
require_once("common.php");
$_testns = 'by_http';
$e_testns = escapeshellarg($_testns);
//TODO $git_push = 'git push --verbose --push-option _testns=' . $e_testns;
$git_push = 'git push --verbose';
rg_test_create_user($db, $rg_ui);
$info = array('id' => $rg_ui['username']);
prepare_http($info);
putenv('git_username=' . $rg_ui['username']);
putenv('git_password=' . $rg_ui['pass']);
rg_test_create_repo($db, $rg_ui, $repo);
rg_log('Created repo ' . $repo['repo_id']);
$repo2 = array('public' => 0);
rg_test_create_repo($db, $rg_ui, $repo2);
rg_log('Created repo2 ' . $repo2['repo_id']);
$r = test_login($test_url, $rg_ui);
if ($r === FALSE) {
rg_log("Cannot login!");
exit(1);
}
$commit_body = rg_id(32);
$a = rg_exec('rm -rf .by_http'
. ' && mkdir .by_http'
. ' && cd .by_http'
. ' && git init'
. ' && git remote add public_repo ' . escapeshellarg($repo['clone_url_http'])
. ' && git remote add private_repo ' . escapeshellarg($repo2['clone_url_http'])
. ' && git remote add private_repo_git ' . escapeshellarg($repo2['clone_url_git'])
. ' && echo "a signature" > a'
. ' && git add a'
. ' && git commit -a -m "' . $commit_body . '"',
'', FALSE, FALSE, FALSE);
if ($a['ok'] != 1) {
rg_log("Something wrong generating the git repo: " . $a['stderr']);
exit(1);
}
rg_log('');
rg_log_enter('Trying to push to public (empty user)...');
putenv('GIT_ASKPASS=' . dirname(__FILE__) . '/ask_pass_empty');
$r = rg_exec('cd .by_http'
. ' && ' . $git_push . ' public_repo master',
'', FALSE, FALSE, FALSE);
if ($r['ok'] == 1) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('Seems I can push with empty user!');
exit(1);
}
if (!strstr($r['stderr'], 'Authentication failed')) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('The stderr does not contain \'Authentication failed\'!');
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('Trying to push to public (with guest)...');
putenv('GIT_ASKPASS=' . dirname(__FILE__) . '/ask_pass_guest');
$r = rg_exec('cd .by_http && ' . $git_push . ' public_repo master',
'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('Seems I cannot push without authentication (anonymous push)!');
exit(1);
}
if (!strstr($r['stderr'], 'transformed into a merge request')) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('The stderr does not contain something about anonymous push!');
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('Trying to push to public (with user/pass)...');
putenv('GIT_ASKPASS=' . dirname(__FILE__) . '/ask_pass');
$r = rg_exec('cd .by_http && ' . $git_push . ' public_repo master',
'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('Seems I cannot push with authentication!');
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('Trying to fetch from public (with user/pass)...');
putenv('GIT_ASKPASS=' . dirname(__FILE__) . '/ask_pass');
$r = rg_exec('cd .by_http && git fetch --verbose public_repo master',
'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('Seems I cannot fetch with authentication!');
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('Trying to fetch from private (by git://)...');
$r = rg_exec('cd .by_http && git fetch --verbose private_repo_git master',
'', FALSE, FALSE, FALSE);
if ($r['ok'] == 1) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('Seems I can fetch by git://!');
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('Trying to push to private (guest user)...');
putenv('GIT_ASKPASS=' . dirname(__FILE__) . '/ask_pass_guest');
$r = rg_exec('cd .by_http && ' . $git_push . ' private_repo master',
'', FALSE, FALSE, FALSE);
if ($r['ok'] == 1) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('Seems I can push without authentication (anonymous push)!');
exit(1);
}
if (!strstr($r['stderr'], 'Authentication failed')) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('The error is not authentication failure (but ['
. $r['stderr'] . '])!');
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('Trying to push to private (with user/pass)...');
putenv('GIT_ASKPASS=' . dirname(__FILE__) . '/ask_pass');
$r = rg_exec('cd .by_http && ' . $git_push . ' private_repo master',
'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('Seems I cannot push with authentication!');
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('Trying to fetch from private (with user/pass)...');
putenv('GIT_ASKPASS=' . dirname(__FILE__) . '/ask_pass');
$r = rg_exec('cd .by_http && git fetch --verbose private_repo',
'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('Seems I cannot fetch with authentication!');
exit(1);
}
rg_log_exit();
$r = totp_enroll($db, $info);
if ($r['ok'] !== 1)
exit(1);
$key = $r['key'];
rg_log('key: ' . $key);
rg_log('');
rg_log_enter('Trying to push to public (with user/pass/login_token_missing)...');
putenv('GIT_ASKPASS=' . dirname(__FILE__) . '/ask_pass');
$r = rg_exec('cd .by_http && ' . $git_push . ' public_repo master',
'', FALSE, FALSE, FALSE);
if ($r['ok'] !== 0) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('Seems I can push without passing login_token!');
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('Trying to fetch from public (with user/pass/login_token_missing)...');
putenv('GIT_ASKPASS=' . dirname(__FILE__) . '/ask_pass');
$r = rg_exec('cd .by_http && git fetch --verbose public_repo master',
'', FALSE, FALSE, FALSE);
if ($r['ok'] !== 1) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('Seems I cannot fetch without passing login_token!');
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('Trying to push to public (with user/pass/login_token_ok)...');
putenv('RG_LOGIN_TOKEN=' . rg_totp_compute($key, (time() - 30) / 30, 6));
putenv('GIT_ASKPASS=' . dirname(__FILE__) . '/ask_pass');
$r = rg_exec('cd .by_http && ' . $git_push . ' public_repo master',
'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('Seems I cannot push with authentication!');
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('Trying to fetch from public (with user/pass/login_token_ok)...');
putenv('RG_LOGIN_TOKEN=' . rg_totp_compute($key, (time() - 0) / 30, 6));
putenv('GIT_ASKPASS=' . dirname(__FILE__) . '/ask_pass');
$r = rg_exec('cd .by_http && git fetch --verbose public_repo master',
'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('Seems I cannot fetch with authentication!');
exit(1);
}
putenv('RG_LOGIN_TOKEN=');
rg_log_exit();
rg_log('');
$commit = trim(file_get_contents('.by_http/.git/refs/heads/master'));
rg_log('master from .git: ' . $commit);
rg_log_enter('Checking on web that everything is OK...');
$data = array();
$headers = array();
$r = do_req($info, $test_url . '/user/' . rawurlencode($rg_ui['username'])
. '/' . rawurlencode($repo['name']) . '/source/log/commit/'
. $commit, $data, $headers);
if ($r === FALSE) {
rg_log('Cannot load master commit!');
exit(1);
}
if (!strstr($r['body'], $commit_body)) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('I cannot find [' . $commit_body . '] inside the body!');
exit(1);
}
rg_log_exit();
rg_log('OK!');