summaryrefslogtreecommitdiff
path: root/ext/zip/tests/oo_encryption.phpt
blob: 7567111dbbd3293b98a6b7590f1d6d38e8abe340 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
--TEST--
ZipArchive::setEncryption*() functions
--SKIPIF--
<?php
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