# ip.test - Copyright (C) 2002 Pat Thoyts # # Tests for the Tcllib ip package # # ------------------------------------------------------------------------- # See the file "license.terms" for information on usage and redistribution # of this file, and for a DISCLAIMER OF ALL WARRANTIES. # ------------------------------------------------------------------------- # RCS: @(#) $Id: ip.test,v 1.8 2009/04/13 20:33:17 andreas_kupries Exp $ # ------------------------------------------------------------------------- source [file join \ [file dirname [file dirname [file join [pwd] [info script]]]] \ devtools testutilities.tcl] testsNeedTcl 8.2 testsNeedTcltest 1.0 testing { useLocal ip.tcl ip } # ------------------------------------------------------------------------- # Tests # ------------------------------------------------------------------------- # version set Data { 127.0.0.1 4 0.0.0.0 4 192.168.0.4 4 255.255.255.255 4 127/8 4 192/16 4 :: 6 ::1 6 fec0::1 6 ::192.168.0.4 6 fec0:0:0:0:0:0:0:1 6 fffe:0:0::2 6 2002:192.168.0.4:: 6 2001:192.168.0.4:: -1 2002:127.0.0.1::1 6 hello -1 -1 -1 1.2.3.4.example.com -1 bogus.1.2.3.4.example.com -1 } set n 0 foreach {addr result} $Data { test ip-1.[incr n] [list ip version $addr] { list [catch {ip::version $addr} msg] $addr $msg } [list 0 $addr $result] } # is set n 0 foreach {addr result} $Data { if {$result != 4} {set result 0} test ip-2.[incr n] [list ip::is ipv4 $addr] { list [catch {expr {[ip::is ipv4 $addr] ? 4 : 0}} msg] $addr $msg } [list 0 $addr $result] } set n 0 foreach {addr result} $Data { if {$result != 6} {set result 0} test ip-3.[incr n] [list ip::is ipv6 $addr] { list [catch {expr {[ip::is ipv6 $addr] ? 6 : 0}} msg] $addr $msg } [list 0 $addr $result] } # normalize set Data { 192.168.0.4/32 192.168.0.4 192.168.0.4/24 192.168.0.4/24 192.168 192.168.0.0 192.168/24 192.168.0.0/24 192.168/255.255.0.0 192.168.0.0/255.255.0.0 :: 0000:0000:0000:0000:0000:0000:0000:0000 ::1 0000:0000:0000:0000:0000:0000:0000:0001 fec0::1 fec0:0000:0000:0000:0000:0000:0000:0001 fec0:0:0::1 fec0:0000:0000:0000:0000:0000:0000:0001 fec0:0::8:0:1 fec0:0000:0000:0000:0000:0008:0000:0001 ::192.168.0.4 0000:0000:0000:0000:0000:0000:c0a8:0004 ::ffff:192.168.0.4 0000:0000:0000:0000:0000:ffff:c0a8:0004 fec0::1/16 fec0:0000:0000:0000:0000:0000:0000:0001/16 fec0::1/128 fec0:0000:0000:0000:0000:0000:0000:0001 2002:127.0.0.1::1 2002:7f00:0001:0000:0000:0000:0000:0001 } set n 0 foreach {addr result} $Data { test ip-4.[incr n] [list ip::normalize $addr] { list [catch {ip::normalize $addr} msg] $msg } [list 0 $result] } set Data { 192.168.1.4 8 192.0.0.0 192.168.1.4 1 128.0.0.0 192.168.1.4 16 192.168.0.0 192.169.1.4 15 192.168.0.0 192.168.1.4 24 192.168.1.0 192.168.1.4 32 192.168.1.4 fec0:fafa::1 64 fec0:fafa:0000:0000:0000:0000:0000:0000 fec0:fafa::1 8 fe00:0000:0000:0000:0000:0000:0000:0000 fec0:fafa::1 10 fec0:0000:0000:0000:0000:0000:0000:0000 fec0:fafa::1 128 fec0:fafa:0000:0000:0000:0000:0000:0001 } # prefix set n 0 foreach {addr mask prefix} $Data { test ip-5.[incr n] [list ip::prefix $addr/$mask] { list [catch {ip::prefix $addr/$mask} msg] $msg } [list 0 $prefix] } # mask set n 0 foreach {addr mask prefix} $Data { test ip-6.[incr n] [list ip::mask $addr/$mask] { list [catch {ip::mask $addr/$mask} msg] $msg } [list 0 $mask] } # equal set Data { 192.168.0.4 ::1 1 "type mismatch: cannot compare different address types" 192.168.1.4/16 192.168.0.0/16 0 1 192.169.1.4/16 192.168.0.0/16 0 0 192.169.1.4/15 192.168.0.0/15 0 1 192.168.1.4/24 192.168.1.0/24 0 1 127/8 192/8 0 0 192.168.1.4/255.255.0.0 192.168.1.4/16 0 1 192.169.1.4/255.255.0.0 192.168.1.4/16 0 0 192.169.1.4/255.254.0.0 192.168.1.4/16 0 1 fec0::1/10 fec0::2/10 0 1 ::1/64 ::2/64 0 1 ::1/128 ::2/128 0 0 ::1/127 ::2/127 0 0 ::1/126 ::2/126 0 1 fec0:ffff::1/16 fec0:aaaa::2/16 0 1 fec0:ffff::1/17 fec0:aaaa::2/17 0 1 fec0:ffff::1/18 fec0:aaaa::2/18 0 0 } set n 0 foreach {lhs rhs err result} $Data { test ip-7.[incr n] [list ip::equal $lhs $rhs] { list [catch {ip::equal $lhs $rhs} msg] $msg } [list $err $result] } # contract set Data { 127.0.0.1 127.0.0.1 127.0.0.0 127 0000:0000:0000:0000:0000:0000:0000:0000 :: 0000:0000:0000:0000:0000:0000:0000:0001 ::1 fec0:0000:0000:0000:0000:0000:0000:0000 fec0:: fec0:0000:0000:0000:0000:0000:0000:0001 fec0::1 fec0:0000:0001:0000:0000:0000:0000:0001 fec0:0:1::1 fec0:0001:0002:0003:0004:0005:0006:0001 fec0:1:2:3:4:5:6:1 fec0:0001:2002:0003:0004:0005:0006:0001 fec0:1:2002:3:4:5:6:1 } set n 0 foreach {addr result} $Data { test ip-8.[incr n] [list ip::contract $addr] { list [catch {ip::contract $addr} msg] $msg } [list 0 $result] } # ------------------------------------------------------------------------- testsuiteCleanup # Local Variables: # mode: tcl # indent-tabs-mode: nil # End: