xref: /PHP-7.3/ext/standard/tests/network/inet.phpt (revision 782352c5)
1--TEST--
2inet_ntop() & inet_pton() tests
3--SKIPIF--
4<?php
5if (!function_exists("inet_ntop")) die("skip no inet_ntop()");
6if (!function_exists("inet_pton")) die("skip no inet_pton()");
7?>
8--FILE--
9<?php
10
11$packed = chr(127) . chr(0) . chr(0) . chr(1);
12var_dump(inet_ntop($packed));
13
14$packed = chr(255) . chr(255) . chr(255) . chr(0);
15var_dump(inet_ntop($packed));
16
17var_dump(inet_ntop());
18var_dump(inet_ntop(-1));
19var_dump(inet_ntop(""));
20var_dump(inet_ntop("blah-blah"));
21
22var_dump(inet_pton());
23var_dump(inet_pton(""));
24var_dump(inet_pton(-1));
25var_dump(inet_pton("abra"));
26
27$array = array(
28	"127.0.0.1",
29	"66.163.161.116",
30	"255.255.255.255",
31	"0.0.0.0",
32	);
33foreach ($array as $val) {
34	var_dump(bin2hex($packed = inet_pton($val)));
35	var_dump(inet_ntop($packed));
36}
37
38echo "Done\n";
39?>
40--EXPECTF--
41string(9) "127.0.0.1"
42string(13) "255.255.255.0"
43
44Warning: inet_ntop() expects exactly 1 parameter, 0 given in %s on line %d
45bool(false)
46bool(false)
47bool(false)
48bool(false)
49
50Warning: inet_pton() expects exactly 1 parameter, 0 given in %s on line %d
51bool(false)
52bool(false)
53bool(false)
54bool(false)
55string(8) "7f000001"
56string(9) "127.0.0.1"
57string(8) "42a3a174"
58string(14) "66.163.161.116"
59string(8) "ffffffff"
60string(15) "255.255.255.255"
61string(8) "00000000"
62string(7) "0.0.0.0"
63Done
64