xref: /PHP-5.5/ext/standard/tests/network/inet.phpt (revision 1bef7308)
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((binary)$packed));
13
14$packed = chr(255) . chr(255) . chr(255) . chr(0);
15var_dump(inet_ntop((binary)$packed));
16
17var_dump(inet_ntop());
18var_dump(inet_ntop(-1));
19var_dump(inet_ntop(b""));
20var_dump(inet_ntop(b"blah-blah"));
21
22var_dump(inet_pton());
23var_dump(inet_pton(b""));
24var_dump(inet_pton(-1));
25var_dump(inet_pton(b"abra"));
26
27$array = array(
28	b"127.0.0.1",
29	b"66.163.161.116",
30	b"255.255.255.255",
31	b"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--
41%unicode|string%(9) "127.0.0.1"
42%unicode|string%(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)
67%unicode|string%(%d) "7f000001"
68%unicode|string%(9) "127.0.0.1"
69%unicode|string%(%d) "42a3a174"
70%unicode|string%(14) "66.163.161.116"
71%unicode|string%(%d) "ffffffff"
72%unicode|string%(15) "255.255.255.255"
73%unicode|string%(%d) "00000000"
74%unicode|string%(7) "0.0.0.0"
75Done
76