summaryrefslogtreecommitdiff
path: root/ext/zip/tests
diff options
context:
space:
mode:
authorRemi Collet <remi@php.net>2017-03-02 13:36:40 +0100
committerRemi Collet <remi@php.net>2017-03-02 13:36:40 +0100
commit402eeb85988be91d90a92eff94635b6e9ef6160e (patch)
treedaa4427a300d8560f35dd262e8986acead3d3ccf /ext/zip/tests
parent859a6505d3bdb017f68ea1beedb118e3758ccb6f (diff)
downloadphp-git-402eeb85988be91d90a92eff94635b6e9ef6160e.tar.gz
Zip: add support for encrypted archive
Diffstat (limited to 'ext/zip/tests')
-rw-r--r--ext/zip/tests/oo_encryption.phpt66
1 files changed, 66 insertions, 0 deletions
diff --git a/ext/zip/tests/oo_encryption.phpt b/ext/zip/tests/oo_encryption.phpt
new file mode 100644
index 0000000000..b703611667
--- /dev/null
+++ b/ext/zip/tests/oo_encryption.phpt
@@ -0,0 +1,66 @@
+--TEST--
+ZipArchive::setEncryption*() functions
+--SKIPIF--
+<?php
+/* $Id$ */
+if (!extension_loaded('zip')) die('skip');
+if (!method_exists('ZipArchive', 'setEncryptionName')) die('skip encrytion not supported');
+?>
+--FILE--
+<?php
+
+$name = __DIR__ . '/encrypted.zip';
+$pass = 'secret';
+
+echo "== Write\n";
+$zip = new ZipArchive;
+$r = $zip->open($name, ZIPARCHIVE::CREATE);
+// Clear
+$zip->addFromString('foo.txt', 'foo');
+// Encrypted
+$zip->addFromString('bar.txt', 'bar');
+var_dump($zip->setEncryptionName('bar.txt', 9999, $pass)); // Fails
+var_dump($zip->setEncryptionName('bar.txt', ZipArchive::EM_AES_256, $pass));
+$zip->close();
+
+echo "== Read\n";
+$r = $zip->open($name);
+$s = $zip->statName('foo.txt');
+var_dump($s['encryption_method'] === ZipArchive::EM_NONE);
+$s = $zip->statName('bar.txt');
+var_dump($s['encryption_method'] === ZipArchive::EM_AES_256);
+var_dump($zip->getFromName('foo.txt')); // Clear, ok
+var_dump($zip->getFromName('bar.txt')); // Encrypted, fails
+$zip->setPassword($pass);
+var_dump($zip->getFromName('bar.txt')); // Ecnrypted, ok
+$zip->close();
+
+echo "== Stream\n";
+var_dump(file_get_contents("zip://$name#foo.txt")); // Clear, ok
+var_dump(file_get_contents("zip://$name#bar.txt")); // Encrypted, fails
+$ctx = stream_context_create(array('zip' => array('password' => $pass)));
+var_dump(file_get_contents("zip://$name#bar.txt", false, $ctx)); // Ecnrypted, ok
+?>
+== Done
+--CLEAN--
+<?php
+$name = __DIR__ . '/encrypted.zip';
+@unlink($name);
+?>
+--EXPECTF--
+== Write
+bool(false)
+bool(true)
+== Read
+bool(true)
+bool(true)
+string(3) "foo"
+bool(false)
+string(3) "bar"
+== Stream
+string(3) "foo"
+
+Warning: file_get_contents(%s): failed to open stream: operation failed in %s on line %d
+bool(false)
+string(3) "bar"
+== Done