Skip to content

Commit

Permalink
Better Util test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-scott committed Dec 30, 2015
1 parent 151f58c commit 4531fd0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
2 changes: 2 additions & 0 deletions test/phpunit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ if [ $? -eq 0 ]; then
# Run the testing suite
php phpunit.phar --bootstrap "$parentdir/autoload.php" "$parentdir/test/unit"
EXITCODE=$?
# Test with mbstring.func_overload = 7
php -dmbstring.func_overload=7 phpunit.phar --bootstrap "$parentdir/autoload.php" "$parentdir/test/unit"
# Cleanup
if [ "$clean" -eq 1 ]; then
echo -e "\033[32mCleaning Up!\033[0m"
Expand Down
66 changes: 63 additions & 3 deletions test/unit/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,77 @@
class UtilTest extends PHPUnit_Framework_TestCase
{
/**
* test safeStrLen() with illegal parameter. We expect to see an exception
* Test our HKDF-esque construct built atop BLAKE2b
*
* @covers Util::hkdfBlake2b()
*/
public function testBlake2bKDF()
{
$ikm = 'YELLOW SUBMARINE';
$len = 32;
$info = 'TESTING HKDF-BLAKE2B';
$salt = str_repeat("\x80", 32);

$test = Util::hkdfBlake2b($ikm, $len, $info, $salt);
$this->assertEquals(
$test,
"\x7b\xaf\xb1\x11\x1c\xda\xce\x81\xd1\xb0\x73\xff\x6e\x68\x8f\xc3".
"\x6f\xb5\xa2\xc7\xbd\x53\xf6\xf1\xb4\x2f\x80\x71\x29\x4b\xb7\xf7"
);
// Let's change the IKM
$ikmB = 'YELLOW SUBMARINF';
$testIkm = Util::hkdfBlake2b($ikmB, $len, $info, $salt);
$this->assertNotEquals($test, $testIkm);

// Let's change the info
$infoB = 'TESTING HKDF-BLAKE2C';
$testInfo = Util::hkdfBlake2b($ikm, $len, $infoB, $salt);
$this->assertNotEquals($test, $testInfo);

// Let's change the salt
$saltB = str_repeat("\x80", 31) . "\x81";
$testSalt = Util::hkdfBlake2b($ikm, $len, $info, $saltB);
$this->assertNotEquals($test, $testSalt);
}

/**
* @covers Util::safeStrlen()
*/
public function testSafeStrlen()
{
$valid = "\xF0\x9D\x92\xB3"; // One 4-byte UTF-8 character
$this->assertEquals(Util::safeStrlen($valid), 4);
}

/**
* test safeStrlen() with illegal parameter. We expect to see an exception
* @return void
* @throws CannotPerformOperation
*
* @covers Util::safeStrlen()
*/
public function testSafeStrlen()
public function testSafeStrlenFail()
{
$this->setExpectedException('\ParagonIE\Halite\Alerts\HaliteAlert');

$teststring = []; // is not a string, will provoke a warning

//suppress php warning
Util::safeStrlen($teststring);
}

/**
* Verify that safeSubstr() operates over binary data.
*
* @covers Util::safeSubstr()
*/
public function testSafeSubstr()
{
$string = \str_repeat("\xF0\x9D\x92\xB3", 4);
$this->assertEquals(Util::safeSubstr($string, 0, 1), "\xF0");
$this->assertEquals(Util::safeSubstr($string, 1, 1), "\x9D");
$this->assertEquals(Util::safeSubstr($string, 2, 1), "\x92");
$this->assertEquals(Util::safeSubstr($string, 3, 1), "\xB3");
$this->assertEquals(Util::safeSubstr($string, 0, 2), "\xF0\x9D");
$this->assertEquals(Util::safeSubstr($string, 2, 2), "\x92\xB3");
}
}

0 comments on commit 4531fd0

Please sign in to comment.