<?php
error_reporting(E_ALL | E_STRICT);
ini_set("track_errors", "On");
$rg_cache_debug = FALSE;
$rg_util_debug = FALSE;
$rg_sql_debug = 100;
$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");
$rg_sql = "host=localhost user=rocketgit dbname=rocketgit connect_timeout=10";
$rg_no_db = TRUE;
require_once("common.php");
$_testns = 'by_http';
$rg_cache_enable = TRUE;
prepare_http();
rg_test_create_user($db, $rg_ui);
rg_log('Created user ' . $rg_ui['uid']);
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);
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 (without user/pass)...');
putenv('GIT_ASKPASS=' . dirname(__FILE__) . '/ask_pass_guest');
$r = rg_exec('cd .by_http && git push public_repo master', '', FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log_ml('out: ' . print_r($r, TRUE));
rg_log('Seems I cannot push without authentication (anonymous push)!');
exit(1);
}
if (!strstr($r['stderr'], 'transformed into a pull request')) {
rg_log_ml('out: ' . 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 && strace -s2000 -f -tt -o by_http.strace git push --verbose public_repo master', '', FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log_ml('out: ' . 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 && strace -s2000 -f -tt -o by_http.strace git fetch --verbose public_repo master', '', FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log_ml('out: ' . 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 (private repo; by git://)...');
$r = rg_exec('cd .by_http && strace -s2000 -f -tt -o fetch_by_git.strace'
. ' git fetch --verbose private_repo_git master', '', FALSE, FALSE);
if ($r['ok'] == 1) {
rg_log_ml('out: ' . print_r($r, TRUE));
rg_log('Seems I can fetch by git:// from a private repo!');
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('Trying to push to private (without user/pass; private repo)...');
putenv('GIT_ASKPASS=' . dirname(__FILE__) . '/ask_pass_guest');
$r = rg_exec('cd .by_http && strace -s2000 -f -tt -o push_no_user.strace'
. ' git push --verbose private_repo master', '', FALSE, FALSE);
if ($r['ok'] == 1) {
rg_log_ml('out: ' . 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('out: ' . print_r($r, TRUE));
rg_log('The error is not authentication failure!');
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('Trying to push to private (with user/pass; private repo)...');
putenv('GIT_ASKPASS=' . dirname(__FILE__) . '/ask_pass');
$r = rg_exec('cd .by_http && strace -s2000 -f -tt -o push_with_user.strace'
. ' git push --verbose private_repo master', '', FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log_ml('out: ' . 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; private repo)...');
putenv('GIT_ASKPASS=' . dirname(__FILE__) . '/ask_pass');
$r = rg_exec('cd .by_http && strace -s2000 -f -tt -o x2.strace git fetch --verbose private_repo', '', FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log_ml('out: ' . print_r($r, TRUE));
rg_log('Seems I cannot fetch with authentication!');
exit(1);
}
rg_log_exit();
$r = totp_enroll($db);
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 && strace -s2000 -f -tt -o by_http.strace git push --verbose public_repo master', '', FALSE, FALSE);
if ($r['ok'] !== 0) {
rg_log_ml('out: ' . 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 && strace -s2000 -f -tt -o by_http.strace git fetch --verbose public_repo master', '', FALSE, FALSE);
if ($r['ok'] !== 1) {
rg_log_ml('out: ' . 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 && strace -s2000 -f -tt -o by_http.strace git push --verbose public_repo master', '', FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log_ml('out: ' . 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 && strace -s2000 -f -tt -o by_http.strace git fetch --verbose public_repo master', '', FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log_ml('out: ' . 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($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!');
?>