Skip to content

Commit 670a8f7

Browse files
committed
ext/phar: Improve signature tests
1 parent e5d0e62 commit 670a8f7

File tree

3 files changed

+122
-62
lines changed

3 files changed

+122
-62
lines changed

ext/phar/tests/phar_setsignaturealgo2.phpt

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Phar::setSupportedSignatures() with hash
33
--EXTENSIONS--
44
phar
5+
openssl
56
--SKIPIF--
67
<?php
78
$arr = Phar::getSupportedSignatures();
@@ -15,70 +16,89 @@ phar.readonly=0
1516
$fname = __DIR__ . '/' . basename(__FILE__, '.php') . '.phar';
1617
$p = new Phar($fname);
1718
$p['file1.txt'] = 'hi';
19+
20+
echo "Default:\n";
1821
var_dump($p->getSignature());
22+
23+
echo "Set MD5:\n";
1924
$p->setSignatureAlgorithm(Phar::MD5);
2025
var_dump($p->getSignature());
26+
27+
echo "Set SHA1:\n";
2128
$p->setSignatureAlgorithm(Phar::SHA1);
2229
var_dump($p->getSignature());
30+
31+
echo "Set SHA256:\n";
2332
try {
24-
$p->setSignatureAlgorithm(Phar::SHA256);
25-
var_dump($p->getSignature());
26-
} catch (Exception $e) {
27-
echo $e->getMessage();
33+
$p->setSignatureAlgorithm(Phar::SHA256);
34+
var_dump($p->getSignature());
35+
} catch (Throwable $e) {
36+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
2837
}
38+
39+
echo "Set SHA512:\n";
2940
try {
30-
$p->setSignatureAlgorithm(Phar::SHA512);
31-
var_dump($p->getSignature());
32-
} catch (Exception $e) {
33-
echo $e->getMessage();
41+
$p->setSignatureAlgorithm(Phar::SHA512);
42+
var_dump($p->getSignature());
43+
} catch (Throwable $e) {
44+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
3445
}
46+
47+
echo "Set OPENSSL:\n";
3548
try {
36-
$config = __DIR__ . '/files/openssl.cnf';
37-
$config_arg = array('config' => $config);
38-
$private = openssl_get_privatekey(file_get_contents(__DIR__ . '/files/private.pem'));
39-
$pkey = '';
40-
openssl_pkey_export($private, $pkey, NULL, $config_arg);
41-
$p->setSignatureAlgorithm(Phar::OPENSSL, $pkey);
42-
var_dump($p->getSignature());
43-
} catch (Exception $e) {
44-
echo $e->getMessage();
49+
$config = __DIR__ . '/files/openssl.cnf';
50+
$config_arg = array('config' => $config);
51+
$private = openssl_get_privatekey(file_get_contents(__DIR__ . '/files/private.pem'));
52+
$pkey = '';
53+
openssl_pkey_export($private, $pkey, NULL, $config_arg);
54+
$p->setSignatureAlgorithm(Phar::OPENSSL, $pkey);
55+
var_dump($p->getSignature());
56+
} catch (Throwable $e) {
57+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
4558
}
59+
4660
?>
4761
--CLEAN--
4862
<?php
4963
unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.phar');
5064
?>
5165
--EXPECTF--
66+
Default:
5267
array(2) {
5368
["hash"]=>
5469
string(%d) "%s"
5570
["hash_type"]=>
5671
string(7) "SHA-256"
5772
}
73+
Set MD5:
5874
array(2) {
5975
["hash"]=>
6076
string(%d) "%s"
6177
["hash_type"]=>
6278
string(3) "MD5"
6379
}
80+
Set SHA1:
6481
array(2) {
6582
["hash"]=>
6683
string(%d) "%s"
6784
["hash_type"]=>
6885
string(5) "SHA-1"
6986
}
87+
Set SHA256:
7088
array(2) {
7189
["hash"]=>
7290
string(%d) "%s"
7391
["hash_type"]=>
7492
string(7) "SHA-256"
7593
}
94+
Set SHA512:
7695
array(2) {
7796
["hash"]=>
7897
string(%d) "%s"
7998
["hash_type"]=>
8099
string(7) "SHA-512"
81100
}
101+
Set OPENSSL:
82102
array(2) {
83103
["hash"]=>
84104
string(%d) "%s"

ext/phar/tests/tar/phar_setsignaturealgo2.phpt

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Phar::setSupportedSignatures() with hash, tar-based
33
--EXTENSIONS--
44
phar
5+
openssl
56
--SKIPIF--
67
<?php
78
$arr = Phar::getSupportedSignatures();
@@ -15,86 +16,109 @@ phar.readonly=0
1516
$fname = __DIR__ . '/' . basename(__FILE__, '.php') . '.phar.tar';
1617
$p = new Phar($fname);
1718
$p['file1.txt'] = 'hi';
19+
20+
echo "Default:\n";
1821
var_dump($p->getSignature());
22+
23+
echo "Set MD5:\n";
1924
$p->setSignatureAlgorithm(Phar::MD5);
2025
var_dump($p->getSignature());
26+
27+
echo "Set SHA1:\n";
2128
$p->setSignatureAlgorithm(Phar::SHA1);
2229
var_dump($p->getSignature());
30+
31+
echo "Set SHA256:\n";
2332
try {
24-
$p->setSignatureAlgorithm(Phar::SHA256);
25-
var_dump($p->getSignature());
26-
} catch (Exception $e) {
27-
echo $e->getMessage();
33+
$p->setSignatureAlgorithm(Phar::SHA256);
34+
var_dump($p->getSignature());
35+
} catch (Throwable $e) {
36+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
2837
}
38+
39+
echo "Set SHA512:\n";
2940
try {
30-
$p->setSignatureAlgorithm(Phar::SHA512);
31-
var_dump($p->getSignature());
32-
} catch (Exception $e) {
33-
echo $e->getMessage();
41+
$p->setSignatureAlgorithm(Phar::SHA512);
42+
var_dump($p->getSignature());
43+
} catch (Throwable $e) {
44+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
3445
}
46+
47+
echo "Set OPENSSL:\n";
3548
try {
36-
$config = __DIR__ . '/../files/openssl.cnf';
37-
$config_arg = array('config' => $config);
38-
$private = openssl_get_privatekey(file_get_contents(dirname(__DIR__) . '/files/private.pem'));
39-
$pkey = '';
40-
openssl_pkey_export($private, $pkey, NULL, $config_arg);
41-
$p->setSignatureAlgorithm(Phar::OPENSSL, $pkey);
42-
var_dump($p->getSignature());
43-
$p->setSignatureAlgorithm(Phar::OPENSSL_SHA512, $pkey);
44-
var_dump($p->getSignature());
45-
$p->setSignatureAlgorithm(Phar::OPENSSL_SHA256, $pkey);
46-
var_dump($p->getSignature());
47-
} catch (Exception $e) {
48-
echo $e->getMessage();
49+
$config = __DIR__ . '/../files/openssl.cnf';
50+
$config_arg = array('config' => $config);
51+
$private = openssl_get_privatekey(file_get_contents(dirname(__DIR__) . '/files/private.pem'));
52+
$pkey = '';
53+
openssl_pkey_export($private, $pkey, NULL, $config_arg);
54+
$p->setSignatureAlgorithm(Phar::OPENSSL, $pkey);
55+
var_dump($p->getSignature());
56+
echo "Set OPENSSL_SHA512:\n";
57+
$p->setSignatureAlgorithm(Phar::OPENSSL_SHA512, $pkey);
58+
var_dump($p->getSignature());
59+
echo "Set OPENSSL_SHA256:\n";
60+
$p->setSignatureAlgorithm(Phar::OPENSSL_SHA256, $pkey);
61+
var_dump($p->getSignature());
62+
} catch (Throwable $e) {
63+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
4964
}
65+
5066
?>
5167
--CLEAN--
5268
<?php
5369
unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.phar.tar');
5470
?>
5571
--EXPECTF--
72+
Default:
5673
array(2) {
5774
["hash"]=>
5875
string(%d) "%s"
5976
["hash_type"]=>
6077
string(7) "SHA-256"
6178
}
79+
Set MD5:
6280
array(2) {
6381
["hash"]=>
6482
string(%d) "%s"
6583
["hash_type"]=>
6684
string(3) "MD5"
6785
}
86+
Set SHA1:
6887
array(2) {
6988
["hash"]=>
7089
string(%d) "%s"
7190
["hash_type"]=>
7291
string(5) "SHA-1"
7392
}
93+
Set SHA256:
7494
array(2) {
7595
["hash"]=>
7696
string(%d) "%s"
7797
["hash_type"]=>
7898
string(7) "SHA-256"
7999
}
100+
Set SHA512:
80101
array(2) {
81102
["hash"]=>
82103
string(%d) "%s"
83104
["hash_type"]=>
84105
string(7) "SHA-512"
85106
}
107+
Set OPENSSL:
86108
array(2) {
87109
["hash"]=>
88110
string(%d) "%s"
89111
["hash_type"]=>
90112
string(7) "OpenSSL"
91113
}
114+
Set OPENSSL_SHA512:
92115
array(2) {
93116
["hash"]=>
94117
string(%d) "%s"
95118
["hash_type"]=>
96119
string(14) "OpenSSL_SHA512"
97120
}
121+
Set OPENSSL_SHA256:
98122
array(2) {
99123
["hash"]=>
100124
string(%d) "%s"

ext/phar/tests/zip/phar_setsignaturealgo2.phpt

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,49 +20,59 @@ $fname5 = __DIR__ . '/' . basename(__FILE__, '.php') . '.5.phar.zip';
2020
$fname6 = __DIR__ . '/' . basename(__FILE__, '.php') . '.6.phar.zip';
2121
$p = new Phar($fname);
2222
$p['file1.txt'] = 'hi';
23+
24+
echo "Default:\n";
2325
var_dump($p->getSignature());
26+
27+
echo "Set MD5:\n";
2428
$p->setSignatureAlgorithm(Phar::MD5);
2529

2630
copy($fname, $fname2);
2731
$p = new Phar($fname2);
2832
var_dump($p->getSignature());
2933

34+
echo "Set SHA1:\n";
3035
$p->setSignatureAlgorithm(Phar::SHA1);
3136

3237
copy($fname2, $fname3);
3338
$p = new Phar($fname3);
3439
var_dump($p->getSignature());
3540

41+
echo "Set SHA256:\n";
3642
try {
37-
$p->setSignatureAlgorithm(Phar::SHA256);
38-
copy($fname3, $fname4);
39-
$p = new Phar($fname4);
40-
var_dump($p->getSignature());
41-
} catch (Exception $e) {
42-
echo $e->getMessage();
43+
$p->setSignatureAlgorithm(Phar::SHA256);
44+
copy($fname3, $fname4);
45+
$p = new Phar($fname4);
46+
var_dump($p->getSignature());
47+
} catch (Throwable $e) {
48+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
4349
}
50+
51+
echo "Set SHA512:\n";
4452
try {
45-
$p->setSignatureAlgorithm(Phar::SHA512);
46-
copy($fname4, $fname5);
47-
$p = new Phar($fname5);
48-
var_dump($p->getSignature());
49-
} catch (Exception $e) {
50-
echo $e->getMessage();
53+
$p->setSignatureAlgorithm(Phar::SHA512);
54+
copy($fname4, $fname5);
55+
$p = new Phar($fname5);
56+
var_dump($p->getSignature());
57+
} catch (Throwable $e) {
58+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
5159
}
60+
61+
echo "Set OPENSSL:\n";
5262
try {
53-
$config = __DIR__ . '/../files/openssl.cnf';
54-
$config_arg = array('config' => $config);
55-
$keys=openssl_pkey_new($config_arg);
56-
openssl_pkey_export($keys, $privkey, NULL, $config_arg);
57-
$pubkey=openssl_pkey_get_details($keys);
58-
$p->setSignatureAlgorithm(Phar::OPENSSL, $privkey);
63+
$config = __DIR__ . '/../files/openssl.cnf';
64+
$config_arg = array('config' => $config);
65+
$keys=openssl_pkey_new($config_arg);
66+
openssl_pkey_export($keys, $privkey, NULL, $config_arg);
67+
$pubkey=openssl_pkey_get_details($keys);
68+
$p->setSignatureAlgorithm(Phar::OPENSSL, $privkey);
5969

60-
copy($fname5, $fname6);
61-
file_put_contents($fname6 . '.pubkey', $pubkey['key']);
62-
$p = new Phar($fname6);
63-
var_dump($p->getSignature());
64-
} catch (Exception $e) {
65-
echo $e->getMessage();
70+
copy($fname5, $fname6);
71+
file_put_contents($fname6 . '.pubkey', $pubkey['key']);
72+
$p = new Phar($fname6);
73+
var_dump($p->getSignature());
74+
} catch (Throwable $e) {
75+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
6676
}
6777
?>
6878
--CLEAN--
@@ -76,36 +86,42 @@ unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.6.phar.zip');
7686
unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.6.phar.zip.pubkey');
7787
?>
7888
--EXPECTF--
89+
Default:
7990
array(2) {
8091
["hash"]=>
8192
string(%d) "%s"
8293
["hash_type"]=>
8394
string(7) "SHA-256"
8495
}
96+
Set MD5:
8597
array(2) {
8698
["hash"]=>
8799
string(%d) "%s"
88100
["hash_type"]=>
89101
string(3) "MD5"
90102
}
103+
Set SHA1:
91104
array(2) {
92105
["hash"]=>
93106
string(%d) "%s"
94107
["hash_type"]=>
95108
string(5) "SHA-1"
96109
}
110+
Set SHA256:
97111
array(2) {
98112
["hash"]=>
99113
string(%d) "%s"
100114
["hash_type"]=>
101115
string(7) "SHA-256"
102116
}
117+
Set SHA512:
103118
array(2) {
104119
["hash"]=>
105120
string(%d) "%s"
106121
["hash_type"]=>
107122
string(7) "SHA-512"
108123
}
124+
Set OPENSSL:
109125
array(2) {
110126
["hash"]=>
111127
string(%d) "%s"

0 commit comments

Comments
 (0)