Skip to content

Commit 02ee5ad

Browse files
author
Scott Arciszewski
committedMar 4, 2015
Set up unit tests
1 parent 8a441ca commit 02ee5ad

File tree

5 files changed

+107
-5
lines changed

5 files changed

+107
-5
lines changed
 

Diff for: ‎autoload.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
*/
4141
function hash_equals($known_string, $user_string)
4242
{
43-
return Future\Security::hash_equals(
43+
return Future\Security::hashEquals(
4444
$known_string,
4545
$user_string
4646
);

Diff for: ‎run-tests.bat

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@echo off
2+
REM todo: interface with gpg4win to verify the signature
3+
php vendor\phpunit\phpunit\phpunit --bootstrap autoload.php tests

Diff for: ‎run-tests.sh

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/bin/bash
2+
3+
clean=1 # Clean up?
4+
gpg --fingerprint D8406D0D82947747293778314AA394086372C20A
5+
if [ $? -ne 0 ]; then
6+
echo -e "\033[33mDownloading PGP Public Key...\033[0m"
7+
gpg --recv-keys D8406D0D82947747293778314AA394086372C20A
8+
# Sebastian Bergmann <sb@sebastian-bergmann.de>
9+
gpg --fingerprint D8406D0D82947747293778314AA394086372C20A
10+
if [ $? -ne 0 ]; then
11+
echo -e "\033[31mCould not download PGP public key for verification\033[0m"
12+
exit
13+
fi
14+
fi
15+
if [ "$clean" -eq 1 ]; then
16+
# Let's clean them up, if they exist
17+
if [ -f phpunit.phar ]; then
18+
rm -f phpunit.phar
19+
fi
20+
if [ -f phpunit.phar.asc ]; then
21+
rm -f phpunit.phar.asc
22+
fi
23+
fi
24+
25+
# Let's grab the latest release and its signature
26+
if [ ! -f phpunit.phar ]; then
27+
wget https://phar.phpunit.de/phpunit.phar
28+
fi
29+
if [ ! -f phpunit.phar.asc ]; then
30+
wget https://phar.phpunit.de/phpunit.phar.asc
31+
fi
32+
33+
# Verify before running
34+
gpg --verify phpunit.phar.asc phpunit.phar
35+
if [ $? -eq 0 ]; then
36+
echo
37+
echo -e "\033[33mBegin Unit Testing\033[0m"
38+
# Run the testing suite
39+
php phpunit.phar --bootstrap autoload.php tests
40+
# Cleanup
41+
if [ "$clean" -eq 1 ]; then
42+
echo -e "\033[32mCleaning Up!\033[0m"
43+
rm -f phpunit.phar
44+
rm -f phpunit.phar.asc
45+
fi
46+
else
47+
echo
48+
chmod -x phpunit.phar
49+
mv phpunit.phar /tmp/bad-phpunit.phar
50+
mv phpunit.phar.asc /tmp/bad-phpunit.phar.asc
51+
echo -e "\033[31mSignature did not match! Check /tmp/bad-phpunit.phar for trojans\033[0m"
52+
exit 1
53+
fi
54+
55+
# The MIT License (MIT)
56+
#
57+
# Copyright (c) 2015 Resonant Core, LLC
58+
#
59+
# Permission is hereby granted, free of charge, to any person obtaining a copy
60+
# of this software and associated documentation files (the "Software"), to deal
61+
# in the Software without restriction, including without limitation the rights
62+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
63+
# copies of the Software, and to permit persons to whom the Software is
64+
# furnished to do so, subject to the following conditions:
65+
#
66+
# The above copyright notice and this permission notice shall be included in all
67+
# copies or substantial portions of the Software.
68+
#
69+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
70+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
71+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
72+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
73+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
74+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
75+
# SOFTWARE.

Diff for: ‎src/Security.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ class Security
3333
* @param string $userString
3434
* @return bool
3535
*/
36-
public static function hash_equals($knownString, $userString)
36+
public static function hashEquals($knownString, $userString)
3737
{
3838
// We have to roll our own
39-
$kLen = self::_strlen($knownString);
40-
$uLen = self::_strlen($userString);
39+
$kLen = self::ourStrlen($knownString);
40+
$uLen = self::ourStrlen($userString);
4141
if ($kLen !== $uLen) {
4242
return false;
4343
}
@@ -55,7 +55,7 @@ public static function hash_equals($knownString, $userString)
5555
* @param string $str
5656
* @return int
5757
*/
58-
private static function _strlen($str)
58+
private static function ourStrlen($str)
5959
{
6060
// Premature optimization: cache the function_exists() result
6161
static $exists = null;

Diff for: ‎tests/Security.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
use \ResonantCore\PHPFuture as Future;
3+
4+
class TestSecurity extends PHPUnit_Framework_TestCase
5+
{
6+
/**
7+
* @covers \ResonantCore\PHPFuture\Security::hashEquals()
8+
*/
9+
public function testHashEquals()
10+
{
11+
$a = 'd4ef98335dc5512500ea74a70a26dbd2759acc0ac116f0d747d05848a8365da9';
12+
$b = 'd4ef98335dc5512500ea74a70a26dbd2759acc0ac116f0d747d05848a8365da9';
13+
14+
// Obvious test case:
15+
$this->assertTrue(\hash_equals($a, $b));
16+
17+
$a = 'd4ef98335dc5512500ea74a70a26dbd2759acc0ac116f0d747d05848a8365da9';
18+
$b = 'd4ef98335dc5512500ea74a70a26dbd2759acc0ac116f0d747d05848a8365da1';
19+
20+
$this->assertFalse(\hash_equals($a, $b));
21+
22+
23+
}
24+
}

0 commit comments

Comments
 (0)
Please sign in to comment.