<?php
$json = <<<EOT
[
{
"file": "/jquery/2.1.1/jquery.min.js",
"md5": "d021c983bd6e7291b43a5cc1fb2ebe99"
},
{
"file": "/g/jquery@2.1.0,angularjs@1.2.14(angular.min.js+angular-resource.min.js+angular-animate.min.js+angular-cookies.min.js+angular-route.min.js+angular-sanitize.min.js)",
"md5": "57e021c8c8b5e5b262ad8e072180fc22"
},
{
"file": "/g/jquery@2.1.1,d3js@3.4.10,typeahead.js@0.10.2,underscorejs@1.6.0,spinjs@1.3.0,handlebarsjs@1.3.0(handlebars.min.js)",
"md5": "e723f9074b6ceab9f74f5d191c497245"
},
{
"file": "/bootstrap/3.2.0/css/bootstrap.min.css",
"md5": "385b964b68acb68d23cb43a5218fade9"
},
{
"file": "/jquery/1.4/jquery.min.js",
"md5": "73a9c334c5ca71d70d092b42064f6476"
},
{
"file": "/fancybox/2.1.5/helpers/fancybox_buttons.png",
"md5": "b448080f8615e664b7788c7003803b59"
},
{
"file": "/foundation/5.3.0/foundation.zip",
"md5": "2687f8c45789171eae115649a52ec464"
},
{
"file": "/normalize/1/normalize.css",
"md5": "f1780870594b8948c02270c3c5408437"
},
{
"file": "/jquery/latest/jquery.min.js",
"dynamic": {
"name": "jquery",
"pathFormat": "/jquery/{$version}/jquery.min.js"
}
},
{
"file": "/angularjs/latest/angular.min.js",
"dynamic": {
"name": "angularjs",
"pathFormat": "/angularjs/{$version}/angular.min.js"
}
},
{
"file": "/g/jquery.fileupload@7(jquery/js/jquery.fileupload.js)",
"md5": "5114585e67c495043e7f72e47bf9cefb"
},
{
"file": "/g/chosen",
"dynamic": {
"name": "chosen",
"pathFormat": "/chosen/{$version}/chosen.jquery.min.js",
"trimWhitespace": true
}
}
]
EOT;
$config = json_decode($json);
function curl($url){
if (function_exists('curl_version')){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close ($ch);
return $result;
} else {
return file_get_contents($url);
}
}
$apiServer = 'jsdelivr-api.dak.netdna-cdn.com';
$originServer = 'origin.jsdelivr.net';
// Host defaults to http://cdn.jsdelivr.net
if (empty($_GET['host'])) {
$host = 'http://cdn.jsdelivr.net';
} else {
$host = 'http://' . $_GET['host'];
}
$passed = 0;
$failed = 0;
foreach ($config as $test) {
if (!empty($test->dynamic)) {
// Ask the api for the latest version of the project
$apiResult = json_decode(curl("http://{$apiServer}/v1/jsdelivr/libraries?fields=lastversion&name=" . $test->dynamic->name));
$staticPath = strtr($test->dynamic->pathFormat, array('{$version}' => $apiResult[0]->lastversion));
if (isset($test->dynamic->trimWhitespace) && ($test->dynamic->trimWhitespace === true)) {
$fileMD5 = md5(trim(curl($host . $test->file)));
} else {
$fileMD5 = md5(curl($host . $test->file));
}
$staticMD5 = md5(curl($host . $staticPath));
// Test if it matches MD5 of static file
if ($fileMD5 == $staticMD5) {
// Test if it matches origin
$originMD5 = md5(curl("http://{$originServer}" . $staticPath));
if($fileMD5 == $originMD5) {
$passed += 1;
echo '[PASS] ', $test->file, "\n";
} else {
$failed += 1;
echo '[FAIL] ', $test->file, "\n";
echo '-> Origin mismatch', "\n";
}
} else {
$failed += 1;
echo '[FAIL] ', $test->file, "\n";
echo '-> Checksum mismatch', "\n";
}
} else {
$fileMD5 = md5(curl($host . $test->file));
// Test if it matches MD5 in config
if ($fileMD5 == $test->md5) {
// Test if it matches origin
$originMD5 = md5(curl("http://{$originServer}" . $test->file));
if($fileMD5 == $originMD5) {
$passed += 1;
echo '[PASS] ', $test->file, "\n";
} else {
$failed += 1;
echo '[FAIL] ', $test->file, "\n";
echo '-> Origin mismatch', "\n";
}
} else {
$failed += 1;
echo '[FAIL] ', $test->file, "\n";
echo '-> Checksum mismatch', "\n";
}
}
// Don't trigger DDoS firewalls, etc
sleep(0.75);
}
// Let's spin the asset wheel ten times, twice!
function array_mt_rand($array) {
// PHP should use mt_rand for array_rand...
return $array[mt_rand(0, count($array) - 1)];
}
// This section randomly tests origin-cdn matching
$allProjects = json_decode(curl("http://{$apiServer}/v1/jsdelivr/libraries/?fields=name,assets"));
$pickedFiles = array();
$i = 0;
while ($i++ < 10) {
// Much files. Such random. Wow.
$randomProject = array_mt_rand($allProjects);
$randomName = $randomProject->name;
$randomAssetGroup = array_mt_rand($randomProject->assets);
$randomVersion = $randomAssetGroup->version;
$randomFile = array_mt_rand($randomAssetGroup->files);
$pickedFiles[] = "/{$randomName}/{$randomVersion}/{$randomFile}";
}
foreach ($pickedFiles as $filePath) {
$fileMD5 = md5(curl($host . $filePath));
// Test if it matches origin
$originMD5 = md5(curl("http://{$originServer}" . $filePath));
if($fileMD5 == $originMD5) {
$passed += 1;
echo '[PASS] ', $filePath, "\n";
} else {
$failed += 1;
echo '[FAIL] ', $filePath, "\n";
echo '-> Origin mismatch', "\n";
}
// Don't trigger DDoS firewalls, etc
sleep(0.75);
}
// This section randomly tests latest version matching for mainfiles
$allProjects = json_decode(curl("http://{$apiServer}/v1/jsdelivr/libraries/?fields=name,mainfile,lastversion"));
$pickedFiles = array();
$i = 0;
while ($i++ < 10) {
$randomProject = array_mt_rand($allProjects);
$randomName = $randomProject->name;
$randomMainfile = $randomProject->mainfile;
$randomLastVersion = $randomProject->lastversion;
if (empty($randomMainfile)) {
$i--;
} else {
$pickedFiles[] = array(
'latestPath' => "/{$randomName}/latest/{$randomMainfile}",
'staticPath' => "/{$randomName}/{$randomLastVersion}/{$randomMainfile}"
);
}
}
foreach ($pickedFiles as $fileInfo) {
// Test if it matches MD5 of static file
$fileMD5 = md5(curl($host . $fileInfo['latestPath']));
$staticMD5 = md5(curl($host . $fileInfo['staticPath']));
if ($fileMD5 == $staticMD5) {
// Test if it matches origin
$originMD5 = md5(curl("http://{$originServer}" . $fileInfo['staticPath']));
if($fileMD5 == $originMD5) {
$passed += 1;
echo '[PASS] ', $fileInfo['latestPath'], "\n";
} else {
$failed += 1;
echo '[FAIL] ', $fileInfo['latestPath'], "\n";
echo '-> Origin mismatch', "\n";
}
} else {
$failed += 1;
echo '[FAIL] ', $fileInfo['latestPath'], "\n";
echo '-> Checksum mismatch', "\n";
}
}
echo $passed, ' tests passed, ', $failed, ' tests failed', "\n";
preferences:
47.64 ms | 402 KiB | 5 Q