xref: /PHP-7.2/ext/standard/tests/network/inet.phpt (revision 17ccbeec)
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)
46
47Warning: inet_ntop(): Invalid in_addr value in %s on line %d
48bool(false)
49
50Warning: inet_ntop(): Invalid in_addr value in %s on line %d
51bool(false)
52
53Warning: inet_ntop(): Invalid in_addr value in %s on line %d
54bool(false)
55
56Warning: inet_pton() expects exactly 1 parameter, 0 given in %s on line %d
57bool(false)
58
59Warning: inet_pton(): Unrecognized address  in %s on line %d
60bool(false)
61
62Warning: inet_pton(): Unrecognized address -1 in %s on line %d
63bool(false)
64
65Warning: inet_pton(): Unrecognized address abra in %s on line %d
66bool(false)
67string(%d) "7f000001"
68string(9) "127.0.0.1"
69string(%d) "42a3a174"
70string(14) "66.163.161.116"
71string(%d) "ffffffff"
72string(15) "255.255.255.255"
73string(%d) "00000000"
74string(7) "0.0.0.0"
75Done
76