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