From 46250f0cd892120af2f5f9d6041ca014f30bc885 Mon Sep 17 00:00:00 2001 From: belliash Date: Wed, 10 Apr 2019 11:28:57 +0200 Subject: [PATCH] IP address encode/decode test. --- tests/ip_addr_enc_dec.aer | 22 ++++++++++++++++++++++ tests/ip_addr_enc_dec.exp | 4 ++++ 2 files changed, 26 insertions(+) create mode 100644 tests/ip_addr_enc_dec.aer create mode 100644 tests/ip_addr_enc_dec.exp diff --git a/tests/ip_addr_enc_dec.aer b/tests/ip_addr_enc_dec.aer new file mode 100644 index 0000000..7ef807e --- /dev/null +++ b/tests/ip_addr_enc_dec.aer @@ -0,0 +1,22 @@ +class Program { + + string encode_ip(string $dotquad_ip) { + string[] $ip_sep = explode('.', $dotquad_ip); + return sprintf('%02x%02x%02x%02x', $ip_sep[0], $ip_sep[1], $ip_sep[2], $ip_sep[3]); + } + + string decode_ip(string $int_ip) { + string[] $hexipbang = explode('.', chunk_split($int_ip, 2, '.')); + return hexdec($hexipbang[0]) + '.' + hexdec($hexipbang[1]) + '.' + hexdec($hexipbang[2]) + '.' + hexdec($hexipbang[3]); + } + + void main() { + string $localhost = $this->encode_ip('127.0.0.1'); + print("127.0.0.1 ==> $localhost\n"); + string $router = $this->encode_ip('192.168.2.1'); + print("192.168.2.1 ==> $router\n"); + print("$localhost ==> ", $this->decode_ip($localhost) + PHP_EOL); + print("$router ==> ", $this->decode_ip($router) + PHP_EOL); + } + +} diff --git a/tests/ip_addr_enc_dec.exp b/tests/ip_addr_enc_dec.exp new file mode 100644 index 0000000..91c2428 --- /dev/null +++ b/tests/ip_addr_enc_dec.exp @@ -0,0 +1,4 @@ +127.0.0.1 ==> 7f000001 +192.168.2.1 ==> c0a80201 +7f000001 ==> 127.0.0.1 +c0a80201 ==> 192.168.2.1