From 3d232f6f82e23b1fda6679fabdf648b504cd43a7 Mon Sep 17 00:00:00 2001 From: Seth Battin Date: Sat, 12 Dec 2015 04:23:02 -0600 Subject: [PATCH 1/3] add phpunit tests for File --- test/{stream => unit/File}/get_large.sh | 0 test/{stream => unit/File}/large.jpg | Bin .../File}/wat-gigantic-duck.jpg | Bin test/unit/FileTest.php | 125 ++++++++++++++++++ 4 files changed, 125 insertions(+) rename test/{stream => unit/File}/get_large.sh (100%) mode change 100644 => 100755 rename test/{stream => unit/File}/large.jpg (100%) rename test/{stream => unit/File}/wat-gigantic-duck.jpg (100%) create mode 100644 test/unit/FileTest.php diff --git a/test/stream/get_large.sh b/test/unit/File/get_large.sh old mode 100644 new mode 100755 similarity index 100% rename from test/stream/get_large.sh rename to test/unit/File/get_large.sh diff --git a/test/stream/large.jpg b/test/unit/File/large.jpg similarity index 100% rename from test/stream/large.jpg rename to test/unit/File/large.jpg diff --git a/test/stream/wat-gigantic-duck.jpg b/test/unit/File/wat-gigantic-duck.jpg similarity index 100% rename from test/stream/wat-gigantic-duck.jpg rename to test/unit/File/wat-gigantic-duck.jpg diff --git a/test/unit/FileTest.php b/test/unit/FileTest.php new file mode 100644 index 0000000..e2b53c4 --- /dev/null +++ b/test/unit/FileTest.php @@ -0,0 +1,125 @@ +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'); + + } + + 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; + } +} From 898f623666f7c7763c33905a26628d87cb88bbbd Mon Sep 17 00:00:00 2001 From: Seth Battin Date: Sat, 12 Dec 2015 04:24:02 -0600 Subject: [PATCH 2/3] remove previous scripts for File tests --- test.sh | 6 ---- test/stream/decrypt.php | 75 ----------------------------------------- test/stream/encrypt.php | 74 ---------------------------------------- test/stream/error.php | 37 -------------------- test/stream/keygen.php | 6 ---- 5 files changed, 198 deletions(-) delete mode 100644 test/stream/decrypt.php delete mode 100644 test/stream/encrypt.php delete mode 100644 test/stream/error.php delete mode 100644 test/stream/keygen.php diff --git a/test.sh b/test.sh index 1a55aa7..84ca6d7 100755 --- a/test.sh +++ b/test.sh @@ -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 diff --git a/test/stream/decrypt.php b/test/stream/decrypt.php deleted file mode 100644 index e6710aa..0000000 --- a/test/stream/decrypt.php +++ /dev/null @@ -1,75 +0,0 @@ -saveToAsciiSafeString()); From 5a257d898390b65a9a7459f347b7ba8d520b80e0 Mon Sep 17 00:00:00 2001 From: Seth Battin Date: Sat, 12 Dec 2015 04:41:52 -0600 Subject: [PATCH 3/3] insert error cases from preivous test/stream/error.php --- test/unit/FileTest.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/unit/FileTest.php b/test/unit/FileTest.php index e2b53c4..885b961 100644 --- a/test/unit/FileTest.php +++ b/test/unit/FileTest.php @@ -101,6 +101,35 @@ public function testResourceToResource($srcFile) '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() {