rob-revisited/tools/embed/tests/check.php

62 lines
4.4 KiB
PHP

<?php
// test.sh: what a browser and a partner page see through the kit
require __DIR__ . '/../jumphp-embed.php';
$fails = 0;
function ok(bool $c, string $what): void { global $fails; echo ($c ? 'ok ' : 'FAIL ') . $what . "\n"; $fails += $c ? 0 : 1; }
function get(string $path, array $headers = [], string $method = 'GET', string $body = ''): array {
$ch = curl_init('http://127.0.0.1:9002' . $path);
$h = [];
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => $headers, CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_HEADERFUNCTION => function ($c, $l) use (&$h) { if (strpos($l, ':')) { [$n, $v] = explode(':', $l, 2); $h[strtolower(trim($n))] = trim($v); } return strlen($l); }]);
if ($body !== '') curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$b = (string)curl_exec($ch);
return [(int)curl_getinfo($ch, CURLINFO_RESPONSE_CODE), $h, $b];
}
$hits = fn () => substr_count((string)@file_get_contents(sys_get_temp_dir() . '/fake-hits.log'), "\n");
// the documented signature (the same test vector as the playground's tests/EmbedTest.php)
ok(JumphpEmbed::signature('s3cret', 'es_x', 'GET', '/api/embed/a/block/login', '', '', 1000)['X-JumPHP-Signature'] === hash_hmac('sha256', "GET\n/api/embed/a/block/login\n\n1000\n" . hash('sha256', ''), 's3cret'), 'signature: METHOD, path, query, time, body hash');
[$s, $h, $b] = get('/elsewhere');
ok($s === 404 && $b === 'site page', 'outside the base path: the site goes on');
[$s, $h] = get('/chat?nickname=ada');
ok($s === 301 && $h['location'] === '/chat/?nickname=ada', '/chat -> /chat/ with its query');
[$s, $h, $b] = get('/chat/?nickname=ada&site=x');
ok($s === 200 && strpos($b, 'GET query=nickname=ada&site=x') !== false, 'the page, its query passed as it came (signed)');
ok(!isset($h['set-cookie']) && !isset($h['x-robots-tag']), 'no cookie nor robots header of the playground');
ok(($h['x-content-type-options'] ?? '') === 'nosniff' && ($h['x-frame-options'] ?? '') === 'SAMEORIGIN', 'security headers');
[$s, , $b] = get('/chat/', ['Content-Type: application/x-www-form-urlencoded'], 'POST', 'nickname=bob&age=30');
ok($s === 200 && strpos($b, 'POST query= body=nickname=bob&age=30') !== false, 'a form sent with POST: its fields go along');
[$s] = get('/chat/../etc/passwd');
ok($s === 400 || $s === 404, 'no way out of the base path');
$before = $hits();
[$s, $h, $b] = get('/chat/engine/1.0.0/big.bin');
ok($s === 200 && strlen($b) === 262144, 'a big file streamed');
[$s, $h, $b] = get('/chat/engine/1.0.0/big.bin', ['Range: bytes=16-31']);
ok($s === 206 && $b === '0123456789abcdef' && $h['content-range'] === 'bytes 16-31/262144', 'Range, from the disk');
[$s, $h, $b] = get('/chat/engine/1.0.0/big.bin', ['Range: bytes=-4']);
ok($s === 206 && $b === 'cdef', 'suffix Range');
[$s] = get('/chat/engine/1.0.0/big.bin', ['If-None-Match: "big"']);
ok($s === 304, 'If-None-Match');
ok($hits() - $before === 1, 'the immutable file was asked once to the playground');
[$s, , $b] = get('/chat/api/tcp/ticket', ['Content-Type: application/json'], 'POST', '{"host":"irc.example.net","port":6667}');
$t = json_decode($b, true);
ok($s === 200 && $t['ip'] === '127.0.0.1' && $t['body']['port'] === 6667, 'the ticket relayed with the visitor\'s address');
[$s] = get('/chat/api/tcp/ticket');
ok($s === 405, 'a ticket is a POST');
$kit = JumphpEmbed::fromConfig(['server' => 'http://127.0.0.1:9001', 'app' => 'demo', 'site' => 'es_00000000000000000000', 'secret' => 'jes_test-secret',
'base' => '/chat', 'cache' => sys_get_temp_dir() . '/kit-cache', 'ttl' => 0]);
$before = $hits();
ok($kit->block('login') === '<form action="https://partner.test/chat/">login</form>', 'a block');
ok($kit->block('login') === '<form action="https://partner.test/chat/">login</form>', 'the block again (revalidated: 304)');
ok($hits() - $before === 2, 'revalidated with its ETag');
$down = JumphpEmbed::fromConfig(['server' => 'http://127.0.0.1:9', 'app' => 'demo', 'site' => 'es_00000000000000000000', 'secret' => 'x', 'base' => '/chat',
'cache' => sys_get_temp_dir() . '/kit-cache', 'ttl' => 0]);
ok($down->block('login') === '<form action="https://partner.test/chat/">login</form>', 'the playground down: the last copy');
$bad = JumphpEmbed::fromConfig(['server' => 'http://127.0.0.1:9001', 'app' => 'demo', 'site' => 'es_00000000000000000000', 'secret' => 'wrong', 'base' => '/chat',
'cache' => sys_get_temp_dir() . '/kit-cache-2']);
ok($bad->block('login') === '', 'a wrong secret: nothing (and a line in the error log)');
exit($fails ? 1 : 0);