<?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 . "/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;
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, $good_sid);
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 origin ' . escapeshellarg($repo['clone_url_http'])
. ' && git remote add origin2 ' . escapeshellarg($repo2['clone_url_http'])
. ' && git remote add origin2_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 master (without user/pass)...');
putenv('GIT_ASKPASS=/bin/true');
$r = rg_exec('cd .by_http && git push origin master', '', FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log_ml('out: ' . print_r($r, TRUE));
rg_log('Seems I cannot push master 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 master (with user/pass)...');
putenv('GIT_ASKPASS=' . dirname(__FILE__) . '/ask_pass');
$r = rg_exec('cd .by_http && strace -s2000 -f -tt -o x.strace git push --verbose origin master', '', FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log_ml('out: ' . print_r($r, TRUE));
rg_log('Seems I cannot push master with authentication!');
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('Trying to fetch master (with user/pass)...');
putenv('GIT_ASKPASS=' . dirname(__FILE__) . '/ask_pass');
$r = rg_exec('cd .by_http && strace -s2000 -f -tt -o x.strace git fetch --verbose origin master', '', FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log_ml('out: ' . print_r($r, TRUE));
rg_log('Seems I cannot fetch master with authentication!');
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('Trying to fetch master (private repo; by git://)...');
$r = rg_exec('cd .by_http && strace -s2000 -f -tt -o fetch_by_git.strace'
. ' git fetch --verbose origin2_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 master (without user/pass; private repo)...');
putenv('GIT_ASKPASS=/bin/true');
$r = rg_exec('cd .by_http && strace -s2000 -f -tt -o push_no_user.strace'
. ' git push --verbose origin2 master', '', FALSE, FALSE);
if ($r['ok'] == 1) {
rg_log_ml('out: ' . print_r($r, TRUE));
rg_log('Seems I can push master 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 master (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 origin2 master', '', FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log_ml('out: ' . print_r($r, TRUE));
rg_log('Seems I cannot push master with authentication!');
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('Trying to fetch master (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 origin2', '', FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log_ml('out: ' . print_r($r, TRUE));
rg_log('Seems I cannot fetch master with authentication!');
exit(1);
}
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("Cookie: sid=" . $good_sid);
$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!');
?>