2024-06-04 01:13:55 -04:00
|
|
|
<?php declare(strict_types=1);
|
2019-05-03 17:03:57 -04:00
|
|
|
|
2020-10-10 06:08:58 -04:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2019-05-03 17:03:57 -04:00
|
|
|
use PrivateBin\FormatV2;
|
|
|
|
|
2020-10-10 06:08:58 -04:00
|
|
|
class FormatV2Test extends TestCase
|
2019-05-03 17:03:57 -04:00
|
|
|
{
|
|
|
|
public function testFormatV2ValidatorValidatesCorrectly()
|
|
|
|
{
|
2019-05-10 01:55:39 -04:00
|
|
|
$this->assertTrue(FormatV2::isValid(Helper::getPastePost()), 'valid format');
|
|
|
|
$this->assertTrue(FormatV2::isValid(Helper::getCommentPost(), true), 'valid format');
|
2019-05-08 16:11:21 -04:00
|
|
|
|
2019-05-10 15:45:34 -04:00
|
|
|
$paste = Helper::getPastePost();
|
2019-05-03 17:03:57 -04:00
|
|
|
$paste['adata'][0][0] = '$';
|
|
|
|
$this->assertFalse(FormatV2::isValid($paste), 'invalid base64 encoding of iv');
|
|
|
|
|
2019-05-10 15:45:34 -04:00
|
|
|
$paste = Helper::getPastePost();
|
2019-05-03 17:03:57 -04:00
|
|
|
$paste['adata'][0][1] = '$';
|
|
|
|
$this->assertFalse(FormatV2::isValid($paste), 'invalid base64 encoding of salt');
|
|
|
|
|
2019-05-10 15:45:34 -04:00
|
|
|
$paste = Helper::getPastePost();
|
2019-05-03 17:03:57 -04:00
|
|
|
$paste['ct'] = '$';
|
|
|
|
$this->assertFalse(FormatV2::isValid($paste), 'invalid base64 encoding of ct');
|
|
|
|
|
2019-05-10 15:45:34 -04:00
|
|
|
$paste = Helper::getPastePost();
|
2019-05-03 17:03:57 -04:00
|
|
|
$paste['ct'] = 'bm9kYXRhbm9kYXRhbm9kYXRhbm9kYXRhbm9kYXRhCg==';
|
|
|
|
$this->assertFalse(FormatV2::isValid($paste), 'low ct entropy');
|
|
|
|
|
2019-05-10 15:45:34 -04:00
|
|
|
$paste = Helper::getPastePost();
|
2019-05-03 17:03:57 -04:00
|
|
|
$paste['adata'][0][0] = 'MTIzNDU2Nzg5MDEyMzQ1Njc4OTA=';
|
|
|
|
$this->assertFalse(FormatV2::isValid($paste), 'iv too long');
|
|
|
|
|
2019-05-10 15:45:34 -04:00
|
|
|
$paste = Helper::getPastePost();
|
2019-05-03 17:03:57 -04:00
|
|
|
$paste['adata'][0][1] = 'MTIzNDU2Nzg5MDEyMzQ1Njc4OTA=';
|
|
|
|
$this->assertFalse(FormatV2::isValid($paste), 'salt too long');
|
|
|
|
|
2019-05-10 15:45:34 -04:00
|
|
|
$paste = Helper::getPastePost();
|
2019-05-03 17:03:57 -04:00
|
|
|
$paste['foo'] = 'bar';
|
|
|
|
$this->assertFalse(FormatV2::isValid($paste), 'invalid additional key');
|
|
|
|
unset($paste['meta']);
|
|
|
|
$this->assertFalse(FormatV2::isValid($paste), 'invalid missing key');
|
|
|
|
|
2019-05-10 15:45:34 -04:00
|
|
|
$paste = Helper::getPastePost();
|
2019-05-03 17:03:57 -04:00
|
|
|
$paste['v'] = 0.9;
|
|
|
|
$this->assertFalse(FormatV2::isValid($paste), 'unsupported version');
|
|
|
|
|
2019-05-10 15:45:34 -04:00
|
|
|
$paste = Helper::getPastePost();
|
2019-05-03 17:03:57 -04:00
|
|
|
$paste['adata'][0][2] = 1000;
|
|
|
|
$this->assertFalse(FormatV2::isValid($paste), 'not enough iterations');
|
|
|
|
|
2019-05-10 15:45:34 -04:00
|
|
|
$paste = Helper::getPastePost();
|
2019-05-03 17:03:57 -04:00
|
|
|
$paste['adata'][0][3] = 127;
|
|
|
|
$this->assertFalse(FormatV2::isValid($paste), 'invalid key size');
|
|
|
|
|
2019-05-10 15:45:34 -04:00
|
|
|
$paste = Helper::getPastePost();
|
2019-05-03 17:03:57 -04:00
|
|
|
$paste['adata'][0][4] = 63;
|
|
|
|
$this->assertFalse(FormatV2::isValid($paste), 'invalid tag length');
|
|
|
|
|
2019-05-10 15:45:34 -04:00
|
|
|
$paste = Helper::getPastePost();
|
2019-05-03 17:03:57 -04:00
|
|
|
$paste['adata'][0][5] = '!#@';
|
|
|
|
$this->assertFalse(FormatV2::isValid($paste), 'invalid algorithm');
|
|
|
|
|
2019-05-10 15:45:34 -04:00
|
|
|
$paste = Helper::getPastePost();
|
2019-05-03 17:03:57 -04:00
|
|
|
$paste['adata'][0][6] = '!#@';
|
|
|
|
$this->assertFalse(FormatV2::isValid($paste), 'invalid mode');
|
|
|
|
|
2019-05-10 15:45:34 -04:00
|
|
|
$paste = Helper::getPastePost();
|
2019-05-03 17:03:57 -04:00
|
|
|
$paste['adata'][0][7] = '!#@';
|
|
|
|
$this->assertFalse(FormatV2::isValid($paste), 'invalid compression');
|
|
|
|
|
2019-05-10 01:55:39 -04:00
|
|
|
$this->assertFalse(FormatV2::isValid(Helper::getPaste()), 'invalid meta key');
|
2019-05-03 17:03:57 -04:00
|
|
|
}
|
|
|
|
}
|