Skip to content

Instantly share code, notes, and snippets.

@mjordan
Created July 24, 2017 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjordan/f34bb81286d79d9ca66417475fffb5f5 to your computer and use it in GitHub Desktop.
Save mjordan/f34bb81286d79d9ca66417475fffb5f5 to your computer and use it in GitHub Desktop.
Script to test Fedora Repository PIDs
<?php
$pids = array(
'foo:%0F',
'foo-:123',
'.foo.:123',
'foo.:-.~_',
'foo.:-___',
);
print "PID validity tests:\n";
foreach ($pids as &$pid) {
$pid = trim($pid);
if (is_valid_pid($pid)) {
print "PASS: $pid is a valid PID\n";
}
else {
print "FAIL: $pid is not a valid PID\n";
}
}
print "\n";
print "URL encoding round trippability tests:\n";
foreach ($pids as &$pid) {
$pid = trim($pid);
$url_encoded = urlencode($pid);
$url_decoded = urldecode($url_encoded);
if ($url_decoded == $pid) {
print "PASS: $pid was round tripped\n";
}
else {
print "FAIL: $pid was not round tripped\n";
}
}
print "\n";
print "URL encoded filename round trippability tests:\n";
$tmp_dir = __DIR__;
$seed = '6e3e54ad-0628-4e36-9ea1-c6b9ea14e136';
foreach ($pids as &$pid) {
$pid = trim($pid);
$url_encoded = urlencode($pid);
$contents = str_shuffle($seed);
$path = $tmp_dir . DIRECTORY_SEPARATOR . $url_encoded;
file_put_contents($path, $contents);
$read_contents = file_get_contents($path);
if ($read_contents == $contents) {
print "PASS: File with $pid URL-encoded name round tripped\n";
}
else {
print "FAIL: File with $pid URL-encoded name was not round tripped\n";
}
unlink($path);
}
/**
* Checks if the given pid is valid.
*
* @param string $pid
* The object id to check.
*
* @return bool
* TRUE if valid, FALSE otherwise.
*/
function is_valid_pid($pid) {
return strlen(trim($pid)) <= 64 && preg_match('/^([A-Za-z0-9]|-|\.)+:(([A-Za-z0-9])|-|\.|~|_|(%[0-9A-F]{2}))+$/', trim($pid));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment