Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,3 @@
set -e
./test/phpunit.sh
echo ""
ORIGDIR=`pwd`
cd test/stream
php keygen.php
php encrypt.php
php decrypt.php
cd $ORIGDIR
75 changes: 0 additions & 75 deletions test/stream/decrypt.php

This file was deleted.

74 changes: 0 additions & 74 deletions test/stream/encrypt.php

This file was deleted.

37 changes: 0 additions & 37 deletions test/stream/error.php

This file was deleted.

6 changes: 0 additions & 6 deletions test/stream/keygen.php

This file was deleted.

File renamed without changes.
File renamed without changes
154 changes: 154 additions & 0 deletions test/unit/FileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php
/**
* Created by PhpStorm.
* User: seth
* Date: 12/12/15
* Time: 1:37 AM
*/
namespace Defuse\Crypto;

class FileTest extends \PHPUnit_Framework_TestCase
{
private $key;
private static $FILE_DIR;
private static $TEMP_DIR;

public function setUp()
{
self::$FILE_DIR = __DIR__ . '/File';
self::$TEMP_DIR = self::$FILE_DIR . '/tmp';
if (!is_dir(self::$TEMP_DIR)){
mkdir(self::$TEMP_DIR);
}

// See test 'testFileCreateKey'
$this->key = Key::CreateNewRandomKey();
}

public function tearDown()
{
array_map('unlink', glob(self::$TEMP_DIR . '/*'));
rmdir(self::$TEMP_DIR);
}

/**
* Test encryption from one file name to a destination file name
* @dataProvider fileToFileProvider
* @param string $srcName source file name
*/
public function testFileToFile($srcName)
{
$src = self::$FILE_DIR . '/' . $srcName;

$dest1 = self::$TEMP_DIR . '/ff1';
$result = File::encryptFile($src, $dest1, $this->key);
$this->assertTrue($result,
sprintf('File "%s" did not encrypt successfully.', $src));
$this->assertFileExists($dest1, 'destination file not created.');

$reverse1 = self::$TEMP_DIR . '/rv1';
$result = File::decryptFile($dest1, $reverse1, $this->key);
$this->assertTrue($result,
sprintf('File "%s" did not decrypt successfully.', $dest1));
$this->assertFileExists($reverse1);
$this->assertEquals(md5_file($src), md5_file($reverse1),
'File and encrypted-decrypted file do not match.');

$dest2 = self::$TEMP_DIR . '/ff2';
$result = File::encryptFile($reverse1, $dest2, $this->key);
$this->assertFileExists($dest2);
$this->assertTrue($result,
sprintf('File "%s" did not re-encrypt successfully.', $reverse1));

$this->assertNotEquals(md5_file($dest1), md5_file($dest2),
'First and second encryption produced identical files.');

$reverse2 = self::$TEMP_DIR . '/rv2';
$result = File::decryptFile($dest2, $reverse2, $this->key);
$this->assertTrue($result,
sprintf('File "%s" did not re-decrypt successfully.', $dest1));
$this->assertEquals(md5_file($src), md5_file($reverse2),
'File and encrypted-decrypted file do not match.');

}

/**
* @dataProvider fileToFileProvider
* @param string $src source handle
*/
public function testResourceToResource($srcFile)
{
$srcName = self::$FILE_DIR . '/' . $srcFile;
$destName = self::$TEMP_DIR . "/$srcFile.dest";
$src = fopen($srcName, 'r');
$dest = fopen($destName, 'w');

$success = File::encryptResource($src, $dest, $this->key);
$this->assertTrue($success, "File did not encrypt successfully.");

fclose($src);
fclose($dest);

$src2 = fopen($destName, 'r');
$dest2 = fopen(self::$TEMP_DIR . '/dest2', 'w');

$success = File::decryptResource($src2, $dest2, $this->key);
$this->assertTrue($success, "File did not decrypt successfully.");
fclose($src2);
fclose($dest2);

$this->assertEquals(md5_file($srcName), md5_file(self::$TEMP_DIR . '/dest2'),
'Original file mismatches the result of encrypt and decrypt');

}

/**
* @expectedException \Defuse\Crypto\Exception\InvalidCiphertextException
* @excpectedExceptionMessage Ciphertext file has a bad magic number.
*/
public function testGarbage()
{
$junk = self::$TEMP_DIR . '/junk';
file_put_contents($junk,
str_repeat("this is not anything that can be decrypted.", 100));

$success = File::decryptFile($junk, self::$TEMP_DIR . '/unjunked', $this->key);
}

/**
* @expectedException \Defuse\Crypto\Exception\InvalidCiphertextException
* @excpectedExceptionMessage Message Authentication failure; tampering detected.
*/
public function testExtraData()
{
$src = self::$FILE_DIR . '/wat-gigantic-duck.jpg';
$dest = self::$TEMP_DIR . '/err';

File::encryptFile($src, $dest, $this->key);

file_put_contents($dest, str_repeat('A', 2048), FILE_APPEND);

File::decryptFile($dest, $dest . '.jpg', $this->key);
}

public function testFileCreateRandomKey()
{
$result = File::createNewRandomKey();
$this->assertInstanceOf('\Defuse\Crypto\Key', $result);
}

public function fileToFileProvider()
{
$data = [];

$data['wat-giagantic-duck'] = ['wat-gigantic-duck.jpg'];
$data['large'] = ['large.jpg'];

if (file_exists(__DIR__ . '/File/In_the_Conservatory.jpg')){
// see File/get_large.sh
$data['extra-large'] = ['In_the_Conservatory.jpg'];
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why that file isn't checked in. I'm not even sure where to get it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_large.sh
On Dec 12, 2015 2:13 PM, "Taylor Hornby" notifications@github.com wrote:

In test/unit/FileTest.php
#147 (comment):

  • {
  •    $result = File::createNewRandomKey();
    
  •    $this->assertInstanceOf('\Defuse\Crypto\Key', $result);
    
  • }
  • public function fileToFileProvider()
  • {
  •    $data = [];
    
  •    $data['wat-giagantic-duck'] = ['wat-gigantic-duck.jpg'];
    
  •    $data['large'] = ['large.jpg'];
    
  •    if (file_exists(**DIR** . '/File/In_the_Conservatory.jpg')){
    
  •        // see File/get_large.sh
    
  •        $data['extra-large'] = ['In_the_Conservatory.jpg'];
    
  •    }
    

I wonder why that file isn't checked in. I'm not even sure where to get it.


Reply to this email directly or view it on GitHub
https://github.com/defuse/php-encryption/pull/147/files#r47435458.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I'm an idiot... there's a comment right there telling me to look in get_large.sh.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's too large for github. The file tests were originally intended for
benchmarking memory usage BTW. I welcome making these unit tests :)
On Dec 12, 2015 2:17 PM, "Taylor Hornby" notifications@github.com wrote:

In test/unit/FileTest.php
#147 (comment):

  • {
  •    $result = File::createNewRandomKey();
    
  •    $this->assertInstanceOf('\Defuse\Crypto\Key', $result);
    
  • }
  • public function fileToFileProvider()
  • {
  •    $data = [];
    
  •    $data['wat-giagantic-duck'] = ['wat-gigantic-duck.jpg'];
    
  •    $data['large'] = ['large.jpg'];
    
  •    if (file_exists(**DIR** . '/File/In_the_Conservatory.jpg')){
    
  •        // see File/get_large.sh
    
  •        $data['extra-large'] = ['In_the_Conservatory.jpg'];
    
  •    }
    

Oh I'm an idiot... there's a comment right there telling me to look in
get_large.sh.


Reply to this email directly or view it on GitHub
https://github.com/defuse/php-encryption/pull/147/files#r47435499.


return $data;
}
}