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 / keys.php (5f236c1ebb0a7df11f98e9d279e3b4a59af08573) (2,520B) (mode 100644) [raw]
<?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 . "/util.inc.php");
require_once($INC . "/keys.inc.php");
require_once($INC . "/sql.inc.php");
require_once($INC . "/struct.inc.php");
require_once($INC . "/fixes.inc.php");

rg_log_set_file("keys.log");

require_once("common.php");

$rg_sql_debug = 1;

// Defaults
$rg_admin_email = "rg@embedromix.ro";

$rg_ui = array("uid" => 1, "is_admin" => 0, "email" => "test@embedromix.ro",
	"confirmed" => 1);

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

// insert a key 1
$key = "aaa 'bbb' first_key";
$key_id1 = rg_keys_add($db, $rg_ui, $key);
if ($key_id1 === FALSE) {
	rg_log("Cannot add key 1 (" . rg_keys_error() . ")!");
	exit(1);
}

// insert a key 2
$rg_ui['uid'] = 2;
$key = "aaa 'bbb' second_key";
$key_id2 = rg_keys_add($db, $rg_ui, $key);
if ($key_id2 === FALSE) {
	rg_log("Cannot add key 2 (" . rg_keys_error() . ")!");
	exit(1);
}


// test key file generation
$rg_scripts = "/a";
$rg_ssh_paras = "ssh1,ssh2,ssh3";
$r = rg_keys_regen($db);
if ($r === FALSE) {
	rg_log("Cannot regenerate keys (" . rg_keys_error() . ")!");
	exit(1);
}
$c = @file_get_contents("afile.txt");
if ($c === FALSE) {
	rg_log("Cannot regenerate file (afile.txt not found)!");
	exit(1);
}
$e = "command=\""
	. $rg_scripts . "/scripts/remote.sh 1 $key_id1\","
	. $rg_ssh_paras . " aaa 'bbb' first_key\n"
	. "command=\""
	. $rg_scripts . "/scripts/remote.sh 2 $key_id2\","
	. $rg_ssh_paras . " aaa 'bbb' second_key\n";
if (strcmp($c, $e) != 0) {
	rg_log("Generated file does not seems OK!");
	rg_log("e: $e");
	rg_log("c: $c");
	exit(1);
}

@unlink("afile.txt");


// delete a key
$rg_ui['uid'] = 1;
$list = array($key_id1 => "junk");
$r = rg_keys_remove($db, $rg_ui, $list);
if ($r === FALSE) {
	rg_log("Cannot remove key (" . rg_keys_error() . ")!");
	exit(1);
}


// test rg_max_ssh_keys
$rg_max_ssh_keys = 1;
// insert a key - should succeed
$rg_ui['uid'] = 10;
$key = "aaa 'bbb' first_key";
$key_id1 = rg_keys_add($db, $rg_ui, $key);
if ($key_id1 === FALSE) {
	rg_log("Cannot add key 1 (" . rg_keys_error() . ")!");
	exit(1);
}
// insert a key - must fail because overlimit
$key = "aaa 'bbb' second_key";
$key_id2 = rg_keys_add($db, $rg_ui, $key);
if ($key_id2 !== FALSE) {
	rg_log("Seems we can add more keys than allowed!");
	exit(1);
}



rg_sql_close($db);

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