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 / rights.php (349438cf05edde314e87d6e5c6f2773b20eb6672) (4,522B) (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 . "/rights.inc.php");

rg_log_set_file("rights.log");

require_once("common.php");

$rg_sql_debug = 1;

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


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

rg_log("test if combine works correctly (1)");
$a = "AF"; $b = "AD"; $e = "AFD";
$r = rg_rights_combine($a, $b);
if (strcmp($r, $e) != 0) {
	echo "Combine rights error1 ([$r] vs [$e])\n";
	exit(1);
}

rg_log("test if combine works correctly (2)");
$a = ""; $b = ""; $e = "";
$r = rg_rights_combine($a, $b);
if (strcmp($r, $e) != 0) {
	echo "Combine rights error1 ([$r] vs [$e])\n";
	exit(1);
}

rg_log("test if combine works correctly (3)");
$a = "AXUJUNFUUFU"; $b = ""; $e = $a;
$r = rg_rights_combine($a, $b);
if (strcmp($r, $e) != 0) {
	echo "Combine rights error1 ([$r] vs [$e])\n";
	exit(1);
}

rg_log("testing mask...");
$a = "ABCDE"; $mask = "AEZ"; $e = "AE";
$r = rg_rights_mask($a, $mask);
if (strcmp($e, $e) != 0) {
	echo "mask is not working right [$r] != [$e]!\n";
	exit(1);
}

rg_log("rights: testing 'test'...");
$rights = array(array("rights" => "ABC", "ip" => ""));
$needed_rights = "BCD";
$ip = "1.2.3.4";
$misc = FALSE;
$r = rg_rights_test($rights, $needed_rights, $ip, $misc);
if ($r !== FALSE) {
	echo "allow is not working right!\n";
	exit(1);
}

rg_log("rights: testing rg_rights_set...");
$a = array();
$a['right_id'] = 0;
$a['who'] = 90;
$a['obj_id'] = 333;
$a['uid'] = 200;
$a['rights'] = "abc";
$a['misc'] = "misc1";
$a['ip'] = "1.1.1.1 2.2.2.2 10.0.0.0/8";
$a['prio'] = 13;
$a['description'] = "desc1";
$r = rg_rights_set($db, "type1", $a);
if ($r !== TRUE) {
	echo "Seems I cannot set rights 1 (" . rg_rights_error() . ")\n";
	exit(1);
}
$a['rights'] = "d"; $a['misc'] = "misc2"; $a['prio'] = 14;
$r = rg_rights_set($db, "type1", $a);
if ($r !== TRUE) {
	echo "Seems I cannot set rights 2 (" . rg_rights_error() . ")\n";
	exit(1);
}
$a['rights'] = "E"; $a['misc'] = "misc3"; $a['prio'] = 14;
$r = rg_rights_set($db, "type2", $a);
if ($r !== TRUE) {
	echo "Seems I cannot set rights 2 (" . rg_rights_error() . ")\n";
	exit(1);
}

rg_log("Testing rg_rights_get...");
$right_id = 0;
$r = rg_rights_get($db, $a['obj_id'], "type1", $a['who'], $a['uid'], $right_id);
if (($r['ok'] !== 1) || (strcmp($r['list'][1]['rights'], "d") != 0)) {
	echo "Seems I cannot get rights (" . rg_rights_error() . ")\n";
	print_r($r);
	exit (1);
}
// 'get' again, to see if cache works
$r = rg_rights_get($db, $a['obj_id'], "type1", $a['who'], $a['uid'], $right_id);
if (($r['ok'] !== 1) || (strcmp($r['list'][1]['rights'], "d") != 0)) {
	echo "Seems I cannot get rights (" . rg_rights_error() . ")\n";
	print_r($r);
	exit (1);
}

rg_log("Testing delete_list...");
$list = array();
foreach ($r['list'] as $junk => $i)
	$list[] = $i['right_id'];
$r = rg_rights_delete_list($db, "type1", $a['obj_id'], $list);
if ($r !== TRUE) {
	echo "We should be able to delete rights!\n";
	exit(1);
}
$right_id = 0;
$r = rg_rights_get($db, $a['obj_id'], "type1", $a['who'], $a['uid'], $right_id);
if (($r['ok'] !== 1) || (count($r['list']) > 0)) {
	print_r($r);
	echo "We should not have anymore type1 objects, after a delete.\n";
	exit (1);
}

rg_log("Testing IP match part - test1");
$list = "1.2.3.4/24 10.0.0.0/8 fd00::/64"
	. " 1234:5678:aaaa:bbbb:cccc:dddd:eeee::/120"
	. " 2222::/24";
$r = rg_rights_test_ip($list, "1.2.3.5");
if ($r !== TRUE) {
	echo "ip test 1 failed\n";
	exit(1);
}
rg_log("Testing IP match part - test2");
$r = rg_rights_test_ip($list, "10.2.3.4");
if ($r !== TRUE) {
	echo "ip test 2 failed\n";
	exit(1);
}
rg_log("Testing IP match part - test3");
$r = rg_rights_test_ip($list, "fd00::3030:aaaa");
if ($r !== TRUE) {
	echo "ip test 3 failed\n";
	exit(1);
}
rg_log("Testing IP match part - test4a");
$r = rg_rights_test_ip($list, "1234:5678:aaaa:bbbb:cccc:dddd:eeee:44");
if ($r !== TRUE) {
	echo "ip test 4a failed\n";
	exit(1);
}
rg_log("Testing IP match part - test4b");
$r = rg_rights_test_ip($list, "1234:5678:aaaa:bbbb:cccc:dddd:eeee:3344");
if ($r === TRUE) {
	echo "ip test 4b failed\n";
	exit(1);
}
rg_log("Testing IP match part - test5");
$r = rg_rights_test_ip($list, "2222::5533");
if ($r !== TRUE) {
	echo "ip test 5 failed\n";
	exit(1);
}

rg_log("Finish");

// TODO: test if a user can read other rights

echo "rights: 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