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 (89643246a973c895401c3835c2b599bba9bc7ddf) (2,725B) (mode 100644) [raw]
<?php
error_reporting(E_ALL | E_STRICT);
ini_set("track_errors", "On");

$INC = "../inc";
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");

rg_log_set_file("keys.log");

$rg_sql_debug = 1;

$db = rg_sql_open("dbname=trg");
if ($db === FALSE) {
	echo "Cannot create a database (" . rg_sql_error() . ")!\n";
	exit(1);
}

$r = rg_sql_struct_update($db, RG_DROP_TABLES);
if ($r !== TRUE) {
	echo "Cannot create structure (" . rg_sql_error() . ")!\n";
	exit(1);
}

// clean all old keys
$sql = "DELETE FROM keys";
$res = rg_sql_query($db, $sql);
rg_sql_free_result($res);

// insert a key 1
$rg_ui = array("uid" => 1, "is_admin" => 0);
$key = "aaa 'bbb' first_key";
$key_id1 = rg_keys_add($db, $rg_ui, $key);
if ($key_id1 === FALSE) {
	echo "Cannot add key 1 (" . rg_keys_error() . ")!\n";
	exit(1);
}

// insert a key 2
$rg_ui = array("uid" => 2, "is_admin" => 0);
$key = "aaa 'bbb' second_key";
$key_id2 = rg_keys_add($db, $rg_ui, $key);
if ($key_id2 === FALSE) {
	echo "Cannot add key 2 (" . rg_keys_error() . ")!\n";
	exit(1);
}


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

@unlink("afile.txt");


// delete a key
$rg_ui = array("uid" => 1, "is_admin" => 0);
$flags = array();
$r = rg_keys_remove($db, $rg_ui, $key_id1, $flags);
if ($r === FALSE) {
	echo "Cannot remove key (" . rg_keys_error() . ")!\n";
	exit(1);
}


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



rg_sql_close($db);

echo "keys: OK\n";
?>
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