xref: /PHP-8.1/ext/zip/tests/bug80833.phpt (revision a6ffd8d1)
1--TEST--
2Bug #80833 (ZipArchive::getStream doesn't use setPassword)
3--SKIPIF--
4<?php
5if (!extension_loaded('zip')) die("skip zip extension not available");
6if (!method_exists('ZipArchive', 'setEncryptionName')) die('skip encryption not supported');
7?>
8--FILE--
9<?php
10$create_zip = new ZipArchive();
11$create_zip->open(__DIR__ . "/80833.zip", ZipArchive::CREATE);
12$create_zip->setPassword("default_password");
13$create_zip->addFromString("test.txt", "This is a test string.");
14$create_zip->setEncryptionName("test.txt", ZipArchive::EM_AES_256, "first_password");
15$create_zip->addFromString("test2.txt", "This is another test string.");
16$create_zip->setEncryptionName("test2.txt", ZipArchive::EM_AES_256, "second_password");
17$create_zip->close();
18
19// Stream API
20$o = [
21	'zip' => [
22		'password' => "first_password",
23	],
24];
25$c = stream_context_create($o);
26var_dump(file_get_contents("zip://" . __DIR__ . "/80833.zip#test.txt", false, $c));
27
28// getStream method
29$extract_zip = new ZipArchive();
30$extract_zip->open(__DIR__ . "/80833.zip", ZipArchive::RDONLY);
31$extract_zip->setPassword("first_password");
32$file_stream = $extract_zip->getStream("test.txt");
33var_dump(stream_get_contents($file_stream));
34fclose($file_stream);
35$extract_zip->setPassword("second_password");
36$file_stream = $extract_zip->getStream("test2.txt");
37var_dump(stream_get_contents($file_stream));
38fclose($file_stream);
39
40// Archive close before the stream
41$extract_zip->setPassword("first_password");
42$file_stream = $extract_zip->getStream("test.txt");
43$extract_zip->close();
44var_dump(stream_get_contents($file_stream));
45fclose($file_stream);
46?>
47--CLEAN--
48<?php
49@unlink(__DIR__ . "/80833.zip");
50?>
51--EXPECTF--
52string(22) "This is a test string."
53string(22) "This is a test string."
54string(28) "This is another test string."
55
56Warning: stream_get_contents(): Zip stream error: Containing zip archive was closed in %s
57string(0) ""
58