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 / repo.php (e7760dc24888e7052ec4c7217795f360178cf0f8) (8,149B) (mode 100644) [raw]
<?php
error_reporting(E_ALL | E_STRICT);
ini_set("track_errors", "On");

$rg_cache_socket = "rg_cache_socket";

$INC = dirname(__FILE__) . "/../inc";
require_once(dirname(__FILE__) . "/config.php");
require_once($INC . "/init.inc.php");
require_once($INC . "/repo.inc.php");
require_once($INC . "/sql.inc.php");
require_once($INC . "/struct.inc.php");
require_once($INC . "/fixes.inc.php");
require_once("helpers.inc.php");

$old_allow = $rg_repo_allow;

rg_log_set_file("repo.log");

rg_log("Starting");

require_once("common.php");

$rg_sql_debug = 1;
$rg_event_socket = "/var/lib/rocketgit/sockets/event.sock";

$sql = "DELETE FROM repos";
$res = rg_sql_query($db, $sql);
rg_sql_free_result($res);

$sql = "DELETE FROM users WHERE uid = 12";
$res = rg_sql_query($db, $sql);
rg_sql_free_result($res);

$sql = "DELETE FROM rights WHERE uid = 12";
$res = rg_sql_query($db, $sql);
rg_sql_free_result($res);


rg_log('');
rg_log_enter('rg_repo_compare_refs');
$patterns = array(
	'' => 'any',
	'a' => 'a',
	'.*' => 'aaaa',
	'v.*' => 'v3.3',
	'v[0-9]*' => 'v3333',
	'prod|.*' => 'production' // because | is removed
);
foreach ($patterns as $pattern => $data) {
	$r = rg_repo_compare_refs($pattern, $data);
	if ($r !== TRUE) {
		rg_log('Ref [' . $data . '] should match pattern [' . $pattern . ']');
		exit(1);
	}
}
rg_log_exit();


rg_log('');
rg_log_enter("rg_repo_path 1");
$e = $rg_repos . "/by_id/11/22/33/44/11223344/repos/by_id/55.git";
$c = rg_repo_path_by_id(0x11223344, 55);
if (strcmp($c, $e) != 0) {
	rg_log("rg_repo_path 1 is not working correctly: c=$c e=$e.");
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('rg_repo_path 2');
$e = $rg_repos . "/by_id/00/00/00/02/00000002/repos/by_id/55.git";
$c = rg_repo_path_by_id(2, 55);
if (strcmp($c, $e) != 0) {
	rg_log("rg_repo_path 2 is not working correctly: c=$c e=$e.");
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('test if repo_allow works correctly (0)');
$rg_repo_allow = 'A-Za-z0-9';
$v = "xx";
$c = rg_repo_ok($v);
if ($c !== TRUE) {
	rg_log("repo_allow problem for valid repo [$v] (c=$c).");
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('test if repo_allow works correctly (1)');
$rg_repo_allow = 'A-Za-z0-9';
$v = "eyhtmcmet_";
$c = rg_repo_ok($v);
if ($c !== FALSE) {
	rg_log("repo_allow problem for '_' in [$v] (c=$c).");
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('test if repo_allow works correctly (2)');
$rg_repo_allow = 'A-Za-z0-9_.-';
$v = ".e&y.h-tmcmet&_.-";
$c = rg_repo_ok($v);
if ($c !== FALSE) {
	rg_log("repo_allow problem for '&'.");
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('check if we allow \'..\'');
$rg_repo_allow = 'A-Za-z0-9_.-';
$v = "a..b";
$c = rg_repo_ok($v);
if ($c !== FALSE) {
	rg_log("repo_allow problem for '..'.");
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('check if we allow \':\'');
$rg_repo_allow = $old_allow;
$v = "a:b";
$c = rg_repo_ok($v);
if ($c !== FALSE) {
	rg_log("repo_allow problem for ':'.");
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('check len test');
$v = "0123456789A";
$rg_repo_allow = 'A-Za-z0-9';
$rg_repo_max_len = 10;
$c = rg_repo_ok($v);
if ($c !== FALSE) {
	rg_log("repo_ok: max length is not enforced!");
	exit(1);
}
rg_log_exit();


rg_log('');
$uid = time();
rg_log_enter('Inserting a fake user uid=' . $uid);
$sql = "INSERT INTO users (uid, username, realname, salt, pass, email, itime, suspended"
	. ", rights)"
	. " VALUES ($uid, 'user-$uid', 'realname-$uid', '', '', '', $uid, 0"
	. ", '')";
$res = rg_sql_query($db, $sql);
if ($res === FALSE) {
	rg_log("Cannot insert a user uid $uid (" . rg_sql_error() . ")!");
	exit(1);
}
rg_sql_free_result($res);
$rg_ui = rg_user_info($db, $uid, "", "");
if ($rg_ui['exists'] != 1) {
	rg_log("Cannot load user info!");
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('creating fake user 12');
$_uid = 12;
rg_log("Inserting a fake user uid=$_uid");
$sql = "INSERT INTO users (uid, username, realname, salt, pass, email, itime, suspended"
	. ", rights)"
	. " VALUES ($_uid, 'user-$_uid', 'realname-$_uid', '', '', '', $_uid, 0"
	. ", '')";
$res = rg_sql_query($db, $sql);
if ($res === FALSE) {
	rg_log("Cannot insert user uid $_uid (" . rg_sql_error() . ")!");
	exit(1);
}
rg_sql_free_result($res);
rg_log_exit();


$rg_repo_allow = $old_allow;
$rg_repo_max_len = 50;

rg_test_create_repo_no_dir($db, $rg_ui, $new);
$repo_id = $new['repo_id'];

$ri = rg_repo_info($db, $repo_id, 0, "");
if ($ri['exists'] != 1) {
	rg_log_ml("ri: " . print_r($ri, TRUE));
	rg_log("Cannot lookup repo_id $repo_id!");
	exit(1);
}


rg_log('');
rg_log_enter('Cleaning repos folder...');
$r = rg_exec("rm -rf base/*", '', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
	rg_log("Failed to clean base folder.");
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('Creating git folder.');
$e = array();
$e['ui'] = array();
$e['ui']['uid'] = $rg_ui['uid'];
$e = rg_array_merge($e, 'ri', $ri);
$r = rg_repo_event_storage_create($db, $e);
if ($r === FALSE) {
	rg_log("Cannot create storage dir (" . rg_repo_error() . ")!");
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('test giving rights');
$a = array();
$a['right_id'] = 0;
$a['obj_id'] = $ri['repo_id'];
$a['who'] = 400;
$a['uid'] = 10;
$a['rights'] = "P";
$a['prio'] = 13;
$a['ip'] = "1.1.1.1";
$a['misc'] = "";
$a['description'] = "desc <>";
$v = rg_rights_set($db, "repo", $a);
if ($v === FALSE) {
	rg_log("Cannot give rights (1): " . rg_rights_error() . "!");
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter("non-owner gets correct rights: 'A' is from injected rights.");
$a = array();
$a['right_id'] = 0;
$a['obj_id'] = $ri['repo_id'];
$a['who'] = 400;
$a['uid'] = 12;
$a['rights'] = "aB"; // Access bug tracker + Add bugs
$a['prio'] = 50;
$a['ip'] = "";
$a['misc'] = "";
$a['description'] = "rights for uid 12";
$r = rg_rights_set($db, "repo", $a);
if ($r !== TRUE) {
	rg_log("Cannot set rights (" . rg_rights_error() . ")!");
	exit(1);
}
$e = 'Aat'; // will not match the above right but the one injected
$r = rg_rights_get($db, $ri['repo_id'], "repo", $uid /*owner */,
	$a['uid'] /*user*/, 0 /*right_id*/);
$copy = $r['list'];
$first = array_shift($copy);
$c = isset($first['rights']) ? $first['rights'] : "_BAD_";
if (strcmp($c, $e) != 0) {
	rg_log_ml("rights_get: r=" . print_r($r, TRUE));
	rg_log("Non-owner (uid " . $a['uid']. ") did not get correct rights: c=$c e=$e.");
	exit(1);
}
rg_log_exit();

rg_log('');
rg_log_enter("owner can set separate rights for him");
$a = array();
$a['right_id'] = 0;
$a['obj_id'] = $ri['repo_id'];
$a['who'] = 400;
$a['uid'] = 500;
$a['rights'] = "E";
$a['prio'] = 100;
$a['ip'] = "";
$a['misc'] = "";
$a['description'] = "rights for uid 500";
$v = rg_rights_set($db, "repo", $a);
if ($v === FALSE) {
	rg_log("Owner cannot set separate rights for him!");
	exit(1);
}
rg_log_exit();

rg_log('');
rg_log_enter('list1');
$r = rg_rights_load($db, $ri['repo_id'], "repo");
if ($r === FALSE) {
	rg_log("Cannot list rights (" . rg_repo_error() . ")");
	exit(1);
}
if (count($r) != 3) {
	rg_log_ml("r=" . print_r($r, TRUE));
	rg_log("Cannot load all rights (should be 3, have " . count($r) . ")!");
	exit(1);
}
// TODO: we should test if expected fields are returned!
rg_log_exit();


rg_log('');
rg_log_enter('Testing the rename of the repos');
$repo_name = "renameA";
$rg_repos = "repos";
$_path = rg_repo_path_by_id($uid, $repo_id);
if (!file_exists($_path)) {
	$r = @mkdir($_path, 0755, TRUE);
	if ($r !== TRUE) {
		rg_log("Cannot create fake dir ($_path)!");
		exit(1);
	}
}
$_path = dirname(rg_repo_path_by_name($uid, $repo_name));
if (!file_exists($_path)) {
	$r = @mkdir($_path, 0755, TRUE);
	if ($r !== TRUE) {
		rg_log("Cannot create fake dir 2 ($_path)!");
		exit(1);
	}
}
$new = $ri;
$new['rights'] = "F";
// Do first rename
$new['name'] = $repo_name . "b";
$r = rg_repo_edit($db, $rg_ui, $new);
if ($r === FALSE) {
	rg_log("Cannot rename repository (" . rg_repo_error() . ")!");
	exit(1);
}
// Do a second rename
$new['name'] = $repo_name . "c";
$r = rg_repo_edit($db, $rg_ui, $new);
if ($r === FALSE) {
	rg_log("Cannot rename repository (" . rg_repo_error() . ")!");
	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