xref: /PHP-8.0/ext/zip/tests/oo_encryption.phpt (revision d1764ca3)
1--TEST--
2ZipArchive::setEncryption*() functions
3--SKIPIF--
4<?php
5if (!extension_loaded('zip')) die('skip');
6if (!method_exists('ZipArchive', 'setEncryptionName')) die('skip encryption not supported');
7?>
8--FILE--
9<?php
10
11$name = __DIR__ . '/encrypted.zip';
12$pass = 'secret';
13
14echo "== Write\n";
15$zip  = new ZipArchive;
16$r = $zip->open($name, ZIPARCHIVE::CREATE);
17// Clear
18$zip->addFromString('foo.txt', 'foo');
19// Encrypted
20$zip->addFromString('bar.txt', 'bar');
21var_dump($zip->setEncryptionName('bar.txt', 9999, $pass)); // Fails
22var_dump($zip->setEncryptionName('bar.txt', ZipArchive::EM_AES_256, $pass));
23$zip->close();
24
25echo "== Read\n";
26$r = $zip->open($name);
27$s = $zip->statName('foo.txt');
28var_dump($s['encryption_method'] === ZipArchive::EM_NONE);
29$s = $zip->statName('bar.txt');
30var_dump($s['encryption_method'] === ZipArchive::EM_AES_256);
31var_dump($zip->getFromName('foo.txt')); // Clear, ok
32var_dump($zip->getFromName('bar.txt')); // Encrypted, fails
33$zip->setPassword($pass);
34var_dump($zip->getFromName('bar.txt')); // Ecnrypted, ok
35$zip->close();
36
37echo "== Stream\n";
38var_dump(file_get_contents("zip://$name#foo.txt")); // Clear, ok
39var_dump(file_get_contents("zip://$name#bar.txt")); // Encrypted, fails
40$ctx = stream_context_create(array('zip' => array('password' => $pass)));
41var_dump(file_get_contents("zip://$name#bar.txt", false, $ctx)); // Ecnrypted, ok
42?>
43== Done
44--CLEAN--
45<?php
46$name = __DIR__ . '/encrypted.zip';
47@unlink($name);
48?>
49--EXPECTF--
50== Write
51bool(false)
52bool(true)
53== Read
54bool(true)
55bool(true)
56string(3) "foo"
57bool(false)
58string(3) "bar"
59== Stream
60string(3) "foo"
61
62Warning: file_get_contents(%s): Failed to open stream: operation failed in %s on line %d
63bool(false)
64string(3) "bar"
65== Done
66