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