xref: /PHP-8.0/ext/sodium/tests/utils.phpt (revision f8d79582)
1--TEST--
2Check for libsodium utils
3--SKIPIF--
4<?php if (!extension_loaded("sodium")) print "skip"; ?>
5--FILE--
6<?php
7$a = 'test';
8sodium_memzero($a);
9if ($a !== 'test') {
10  echo strlen($a);
11} else {
12  echo $a;
13}
14echo "\n";
15$b = 'string';
16$c = 'string';
17var_dump(!sodium_memcmp($b, $c));
18var_dump(!sodium_memcmp($b, 'String'));
19$v = "\xFF\xFF\x80\x01\x02\x03\x04\x05\x06\x07";
20$v .= "\x08";
21sodium_increment($v);
22var_dump(bin2hex($v));
23
24$w = "\x01\x02\x03\x04\x05\x06\x07\x08\xFA\xFB";
25$w .= "\xFC";
26sodium_add($v, $w);
27var_dump(bin2hex($v));
28
29if (SODIUM_LIBRARY_MAJOR_VERSION > 7 ||
30    (SODIUM_LIBRARY_MAJOR_VERSION == 7 &&
31     SODIUM_LIBRARY_MINOR_VERSION >= 6)) {
32    $v_1 = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F";
33    $v_2 = ""."\x02\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F";
34    $v_1 .= '';
35    var_dump(sodium_compare($v_1, $v_2));
36    sodium_increment($v_1);
37    var_dump(sodium_compare($v_1, $v_2));
38    sodium_increment($v_1);
39    var_dump(sodium_compare($v_1, $v_2));
40} else {
41    // Dummy test results for libsodium < 1.0.4
42    var_dump(-1, 0, 1);
43}
44
45$str = 'stdClass';
46sodium_memzero($str);
47$obj = (object)array('foo' => 'bar');
48var_dump($obj);
49
50$str = 'xyz';
51$str_padded = sodium_pad($str, 16);
52var_dump(bin2hex($str_padded));
53
54$str_unpadded = sodium_unpad($str_padded, 16);
55var_dump($str_unpadded == $str);
56
57if (defined('SODIUM_BASE64_VARIANT_ORIGINAL')) {
58    for ($i = 0; $i < 100; $i++) {
59        $bin = $i == 0 ? '' : random_bytes($i);
60        $b64 = base64_encode($bin);
61        $b64_ = sodium_bin2base64($bin, SODIUM_BASE64_VARIANT_ORIGINAL);
62        if ($b64 !== $b64_) {
63            echo "frombin[$b64] != frombin_[$b64_]\n";
64        }
65        $bin_ = sodium_base642bin($b64, SODIUM_BASE64_VARIANT_ORIGINAL);
66        if ($bin !== $bin_) {
67            echo "frombase64([$b64]) != frombase64_[$b64]\n";
68        }
69        $bin_ = sodium_base642bin(" $b64\n", SODIUM_BASE64_VARIANT_ORIGINAL, " \n");
70        if ($bin !== $bin_) {
71            echo "frombase64([$b64]) != frombase64_([ $b64\\n])\n";
72        }
73    }
74    try {
75        var_dump(sodium_base642bin('O1R', SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING));
76    } catch (Exception $e) {
77        var_dump('base64("O1R") case passed');
78    }
79    try {
80        var_dump(sodium_base642bin('O1', SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING));
81    } catch (Exception $e) {
82        var_dump('base64("O1") case passed');
83    }
84    try {
85        var_dump(sodium_base642bin('O', SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING));
86    } catch (Exception $e) {
87        var_dump('base64("O") case passed');
88    }
89    var_dump(sodium_base642bin('YWJjZA', SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING));
90} else {
91    var_dump('base64("O1R") case passed');
92    var_dump('base64("O1") case passed');
93    var_dump('base64("O") case passed');
94    var_dump('abcd');
95}
96
97function sodium_foo()
98{
99    throw new SodiumException('test');
100}
101
102try {
103    sodium_foo();
104} catch (SodiumException $ex) {
105    var_dump($ex->getMessage());
106}
107
108?>
109--EXPECT--
1100
111bool(true)
112bool(false)
113string(22) "0000810102030405060708"
114string(22) "0102840507090b0d000305"
115int(-1)
116int(0)
117int(1)
118object(stdClass)#1 (1) {
119  ["foo"]=>
120  string(3) "bar"
121}
122string(32) "78797a80000000000000000000000000"
123bool(true)
124string(25) "base64("O1R") case passed"
125string(24) "base64("O1") case passed"
126string(23) "base64("O") case passed"
127string(4) "abcd"
128string(4) "test"
129