Skip to content

fix base32 encode/decode#9225

Open
live627 wants to merge 1 commit intoSimpleMachines:release-3.0from
live627:uuid
Open

fix base32 encode/decode#9225
live627 wants to merge 1 commit intoSimpleMachines:release-3.0from
live627:uuid

Conversation

@live627
Copy link
Copy Markdown
Contributor

@live627 live627 commented May 9, 2026

test script

<?php

declare(strict_types=1);

use SMF\Uuid;

require_once __DIR__ . '/Uuid.php';

/**
 * Verify equality.
 */
function assertEqual(
	mixed $expected,
	mixed $actual,
	string $message
): void {
	if ($expected !== $actual) {
		throw new RuntimeException(
			$message .
			PHP_EOL .
			'Expected: ' . var_export($expected, true) .
			PHP_EOL .
			'Actual:   ' . var_export($actual, true)
		);
	}
}

/******************************************************************************
 * Base32 encode/decode correctness tests
 *****************************************************************************/

echo PHP_EOL;
echo "Running Base32 correctness tests..." . PHP_EOL;

/**
 * Test vectors.
 */
$vectors = [

	/******************************************************************************
	 * Nil UUID
	 *****************************************************************************/

	'00000000-0000-0000-0000-000000000000',

	/******************************************************************************
	 * Max UUID
	 *****************************************************************************/

	'ffffffff-ffff-ffff-ffff-ffffffffffff',

	/******************************************************************************
	 * RFC example UUIDs
	 *****************************************************************************/

	'6ba7b810-9dad-11d1-80b4-00c04fd430c8',
	'6ba7b811-9dad-11d1-80b4-00c04fd430c8',
	'6ba7b812-9dad-11d1-80b4-00c04fd430c8',

	/******************************************************************************
	 * Random fixed UUIDs
	 *****************************************************************************/

	'019e0e2e-4a3a-7fb1-8754-29d817add942',
	'12345678-1234-5678-9abc-def012345678',
	'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee',
	'deadbeef-cafe-babe-face-0123456789ab',

	/******************************************************************************
	 * Leading zero stress
	 *****************************************************************************/

	'00000001-0000-0000-0000-000000000000',
	'00000000-0000-0000-0000-000000000001',
	'00000000-0000-0000-0000-ffffffffffff',

	/******************************************************************************
	 * Trailing bit stress
	 *****************************************************************************/

	'ffffffff-ffff-ffff-ffff-fffffffffffe',
	'ffffffff-ffff-ffff-ffff-ffffffffffff',
];

/******************************************************************************
 * Test both implementations
 *****************************************************************************/

echo PHP_EOL;
echo "Testing {Uuid}" . PHP_EOL;

foreach ($vectors as $uuid) {

	/******************************************************************************
	 * Encode
	 *****************************************************************************/

	$b32 = Uuid::compress(
		$uuid,
		Uuid::COMPRESS_BASE32
	);

	/******************************************************************************
	 * Length check
	 *****************************************************************************/

	assertEqual(
		26,
		strlen($b32),
		"Base32 length mismatch for {$uuid}"
	);

	/******************************************************************************
	 * Character set check
	 *****************************************************************************/

	if (
		strspn(
			$b32,
			Uuid::BASE32_ALT
		) !== 26
	) {
		throw new RuntimeException(
			"Invalid Base32 chars for {$uuid}: {$b32}"
		);
	}

	/******************************************************************************
	 * Decode
	 *****************************************************************************/

	$expanded = Uuid::expand($b32);

	/******************************************************************************
	 * Roundtrip check
	 *****************************************************************************/

	assertEqual(
		strtolower($uuid),
		strtolower($expanded),
		"Base32 roundtrip failed"
	);

	/******************************************************************************
	 * Stability check
	 *****************************************************************************/

	$b32_2 = Uuid::compress(
		$expanded,
		Uuid::COMPRESS_BASE32
	);

	assertEqual(
		$b32,
		$b32_2,
		"Base32 stability failed"
	);
}

echo "  OK" . PHP_EOL;

/******************************************************************************
 * Random fuzz tests
 *****************************************************************************/

echo PHP_EOL;
echo "Running Base32 fuzz tests..." . PHP_EOL;

for ($i = 0; $i < 10000; $i++) {

	/******************************************************************************
	 * Generate UUID
	 *****************************************************************************/

	$uuid = (string) new Uuid(7);

	/******************************************************************************
	 * Encode/decode
	 *****************************************************************************/

	$b32 = Uuid::compress(
		$uuid,
		Uuid::COMPRESS_BASE32
	);

	$expanded = Uuid::expand($b32);

	/******************************************************************************
	 * Verify exact roundtrip
	 *****************************************************************************/

	assertEqual(
		strtolower($uuid),
		strtolower($expanded),
		"Fuzz roundtrip failed at iteration {$i}"
	);
}

echo "Fuzz tests passed." . PHP_EOL;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant