<?php
error_reporting(E_ALL | E_STRICT);
ini_set("track_errors", "On");
$test_normal = TRUE;
$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("source.log");
require_once("common.php");
$_testns = 'source';
rg_test_create_user($db, $rg_ui);
$info = array('id' => $rg_ui['username']);
prepare_http($info);
rg_test_create_repo($db, $rg_ui, $repo);
$r = test_login($test_url, $rg_ui);
if ($r === FALSE) {
rg_log("Cannot login!");
exit(1);
}
rg_test_upload_ssh_key($db, $rg_ui, 'source', $kn);
rg_log('');
rg_log_enter('Trying to load source/log for an empty repo...');
$data = array();
$headers = array();
$r = do_req($info, $test_url . '/user/' . rawurlencode($rg_ui['username'])
. '/' . rawurlencode($repo['name']) . '/source/log',
$data, $headers);
if (!strstr($r['body'], 'This repo contains no commits.')) {
rg_log_ml('r[body]: ' . print_r($r['body'], TRUE));
rg_log('I cannot find \'no commits\' inside the body!');
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('Trying to push master with an empty file...');
$cmsg = time() . ' commit message';
$remote = 'ssh://rocketgit@' . $rg_ssh_host . ':' .$rg_ssh_port
. '/user/' . escapeshellarg($rg_ui['username'])
. '/' . escapeshellarg($repo['name']);
$r = rg_exec('mkdir -p temp_repos && cd temp_repos'
. ' && rm -rf .source && mkdir .source && cd .source'
. ' && git init && git commit --allow-empty -m "' . $cmsg . '"'
. ' && git remote add origin ' . $remote
. ' && git push origin master',
'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log_ml('out: ' . $r['errmsg']);
rg_log('Seems I cannot push master!');
exit(1);
}
$data = array();
$headers = array();
rg_log('Check "log" menu...');
$r = do_req($info, $test_url . '/user/' . rawurlencode($rg_ui['username'])
. '/' . rawurlencode($repo['name']) . '/source/log',
$data, $headers);
// TODO: can func ever return FALSE? Change everywhere!
if ($r === FALSE) {
rg_log('Cannot load source/log page!');
exit(1);
}
if (!strstr($r['body'], $cmsg)) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('I cannot find [' . $cmsg . '] inside the body!');
exit(1);
}
rg_log('Check "Tree" menu...');
$r = do_req($info, $test_url . '/user/' . rawurlencode($rg_ui['username'])
. '/' . rawurlencode($repo['name']) . '/source/tree',
$data, $headers);
if ($r === FALSE) {
rg_log('Cannot load source/tree page!');
exit(1);
}
if (!strstr($r['body'], 'Tree is empty.')) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('I cannot find [Tree is empty.] inside the body!');
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('Trying to push master with strange file names...');
$d = 'ă'; $ed = escapeshellarg($d);
$f = 'ș ț"\\'; $ef = escapeshellarg($f);
$cmsg = time() . ' commit message';
$r = rg_exec('cd temp_repos/.source'
. ' && touch "a b c" && mkdir ' . $ed . ' && f=' . $ef . ' && echo "aș" > ' . $ed . '/' . $ef
. ' && git add "a b c" ' . $ed . '/' . $ef
. ' && git commit -m "' . $cmsg . '"'
. ' && git branch "Î/branch_î"'
. ' && git push origin master'
. ' && git push origin "Î/branch_î"',
'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log_ml('out: ' . $r['errmsg']);
rg_log('Seems I cannot push master!');
exit(1);
}
$data = array();
$headers = array();
rg_log('Check "Tree" menu...');
$r = do_req($info, $test_url . '/user/' . rawurlencode($rg_ui['username'])
. '/' . rawurlencode($repo['name']) . '/source/tree',
$data, $headers);
if ($r === FALSE) {
rg_log('Cannot load source/tree page!');
exit(1);
}
if (!strstr($r['body'], '>a b c<')) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('I cannot find [a b c] inside the body!');
exit(1);
}
if (!strstr($r['body'], 'log/branch/%C3%8E%2Cbranch_%C3%AE">Î/branch_î</a>')) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('I cannot find [Î/branch_î] inside the body!');
exit(1);
}
$_x = explode('>' . $d . '<', $r['body']);
if (!isset($_x[1])) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('I cannot find [' . $d . '] inside the body!');
exit(1);
}
$p = strrpos($_x[0], '<a href="');
$url = substr($_x[0], $p + 9, -1);
$data = array();
$headers = array();
rg_log('Check "Tree" menu (dir ' . $d . ')...');
$r = do_req($info, $test_url . $url, $data, $headers);
if ($r === FALSE) {
rg_log('Cannot load source/tree page!');
exit(1);
}
if (!strstr($r['body'], '>' . htmlspecialchars($f) . '<')) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('I cannot find [' . $f . '] inside the body!');
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('Loading blob page...');
$data = array();
$headers = array();
$r = do_req($info, $test_url . '/user/' . rawurlencode($rg_ui['username'])
. '/' . rawurlencode($repo['name']) . '/source/tree/branch/master'
. '/blob/' . rawurlencode($d) . '/' . rawurlencode($f),
$data, $headers);
if ($r === FALSE) {
rg_log('Cannot load blob page!');
exit(1);
}
if (!strstr($r['body'], 'aș')) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('I cannot find the content of the blob [aș] inside the body!');
exit(1);
}
rg_log_enter('Downloading blob...');
$data = array();
$headers = array();
$r = do_req($info, $test_url . '/user/' . rawurlencode($rg_ui['username'])
. '/' . rawurlencode($repo['name']) . '/source/tree/branch/master'
. '/blob_download/' . rawurlencode($d) . '/' . rawurlencode($f) . '?format=raw',
$data, $headers);
if ($r === FALSE) {
rg_log('Cannot download!');
exit(1);
}
if (strcmp($r['body'], "aș\n") != 0) {
rg_log('body: ' . $r['body']);
rg_log('I cannot find the content of the blob [aș] inside the body!');
exit(1);
}
if (!stristr($r['header'], 'Content-Type: application/octet-stream')) {
rg_log('Content-Type header is wrong!');
exit(1);
}
if (!stristr($r['header'], 'Content-Disposition: attachment; filename="' . addcslashes($f, '"\\') . '"')) {
rg_log('Content-Disposition header is wrong!');
exit(1);
}
if (!stristr($r['header'], 'X-Content-Type-Options: nosniff')) {
rg_log('X-Content-Type-Options header is missing/incorrect!');
exit(1);
}
if (!stristr($r['header'], 'Content-Length: 4')) {
rg_log('Content-Length header is missing/incorrect!');
exit(1);
}
rg_log_exit();
rg_log('OK!');