feat: publish NIRO Gen5W research overview and reviewed source examples
This commit is contained in:
commit
ccad0ed9f3
23 files changed
+1279
No files matched your search
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/../../backend/php/NiroMqttKeepalive.php';
|
||||
|
||||
function assertMqttKeepalive(bool $condition, string $message): void
|
||||
{
|
||||
if (!$condition) {
|
||||
fwrite(STDERR, "NiroMqttKeepaliveTest failed: {$message}\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
$keepalive = new NiroMqttKeepalive(100, 15);
|
||||
for ($now = 101; $now < 115; $now++) {
|
||||
assertMqttKeepalive(!$keepalive->isDue($now), 'PINGREQ became due too early');
|
||||
}
|
||||
assertMqttKeepalive($keepalive->isDue(115),
|
||||
'continuous inbound packets postponed the client PINGREQ');
|
||||
|
||||
$keepalive->markSent(115);
|
||||
assertMqttKeepalive(!$keepalive->isDue(129), 'PINGREQ repeated before its interval');
|
||||
assertMqttKeepalive($keepalive->isDue(130), 'next PINGREQ was not scheduled');
|
||||
|
||||
$invalidIntervalRejected = false;
|
||||
try {
|
||||
new NiroMqttKeepalive(100, 0);
|
||||
} catch (InvalidArgumentException) {
|
||||
$invalidIntervalRejected = true;
|
||||
}
|
||||
assertMqttKeepalive($invalidIntervalRejected, 'invalid keepalive interval was accepted');
|
||||
|
||||
echo "NiroMqttKeepaliveTest passed\n";
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/../../backend/php/NiroRawSnappy.php';
|
||||
|
||||
function checkSnappy(bool $condition, string $message): void
|
||||
{
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
}
|
||||
|
||||
/** Independent literal-only reader for synthetic encoder tests. */
|
||||
function readLiteralSnappy(string $encoded): string
|
||||
{
|
||||
$position = 0;
|
||||
$length = 0;
|
||||
$shift = 0;
|
||||
do {
|
||||
checkSnappy($position < strlen($encoded) && $shift <= 28, 'invalid varint');
|
||||
$byte = ord($encoded[$position++]);
|
||||
$length |= ($byte & 0x7f) << $shift;
|
||||
$shift += 7;
|
||||
} while (($byte & 0x80) !== 0);
|
||||
|
||||
$output = '';
|
||||
while ($position < strlen($encoded)) {
|
||||
$tag = ord($encoded[$position++]);
|
||||
checkSnappy(($tag & 3) === 0, 'non-literal tag');
|
||||
$size = $tag >> 2;
|
||||
if ($size >= 60) {
|
||||
$count = $size - 59;
|
||||
checkSnappy($position + $count <= strlen($encoded), 'truncated size');
|
||||
$size = 0;
|
||||
for ($i = 0; $i < $count; ++$i) {
|
||||
$size |= ord($encoded[$position++]) << ($i * 8);
|
||||
}
|
||||
}
|
||||
++$size;
|
||||
checkSnappy($position + $size <= strlen($encoded), 'truncated literal');
|
||||
$output .= substr($encoded, $position, $size);
|
||||
$position += $size;
|
||||
}
|
||||
checkSnappy(strlen($output) === $length, 'uncompressed length mismatch');
|
||||
return $output;
|
||||
}
|
||||
|
||||
$cases = ['', 'a', "synthetic\0payload"];
|
||||
foreach ([60, 61, 255, 256, 65535, 65536, 65537, 131073] as $length) {
|
||||
$cases[] = str_repeat('x', $length);
|
||||
}
|
||||
foreach ($cases as $plain) {
|
||||
$encoded = NiroRawSnappy::encodeLiteral($plain);
|
||||
checkSnappy(readLiteralSnappy($encoded) === $plain, 'round trip failed');
|
||||
checkSnappy($encoded === NiroRawSnappy::encodeLiteral($plain), 'not deterministic');
|
||||
}
|
||||
$rejected = false;
|
||||
try {
|
||||
NiroRawSnappy::encodeLiteral(str_repeat('x', 8 * 1024 * 1024 + 1));
|
||||
} catch (InvalidArgumentException $expected) {
|
||||
$rejected = true;
|
||||
}
|
||||
checkSnappy($rejected, 'oversized input was accepted');
|
||||
echo 'NiroRawSnappyTest passed cases=' . count($cases) . PHP_EOL;
|
||||
Reference in new issue
Block a user