-
Notifications
You must be signed in to change notification settings - Fork 311
Issue/144 convert file tests #147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
0
test/stream/get_large.sh → test/unit/File/get_large.sh
100644 → 100755
File renamed without changes.
File renamed without changes
File renamed without changes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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']; | ||
| } | ||
|
|
||
| return $data; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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: