github.com/iDigitalFlame/xmt@v0.5.4/device/address_test.go (about)

     1  // Copyright (C) 2020 - 2023 iDigitalFlame
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU General Public License as published by
     5  // the Free Software Foundation, either version 3 of the License, or
     6  // any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU General Public License
    14  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    15  //
    16  
    17  package device
    18  
    19  import (
    20  	"net"
    21  	"testing"
    22  )
    23  
    24  func TestAddress(t *testing.T) {
    25  	v := [...]net.IP{
    26  		net.ParseIP("127.0.0.1"),
    27  		net.ParseIP("::1"),
    28  		net.ParseIP("fe80::1"),
    29  		net.ParseIP("192.168.1.1"),
    30  		net.ParseIP("2006:a0:beef:dead::47a"),
    31  	}
    32  	for i := range v {
    33  		if len(v[i]) == 0 {
    34  			t.Fatalf(`TestAddress(): ParseIP index "%d" returned an invalid net.IP!`, i)
    35  		}
    36  		var a Address
    37  		if a.Set(v[i]); a.IsUnspecified() {
    38  			t.Fatalf(`TestAddress(): Address "%d" should not be zero!`, i)
    39  		}
    40  		if v[i][0] == 127 && a.IsLoopback() {
    41  			t.Fatalf(`TestAddress(): Address "%s" IsLoopback() should return true!`, a.String())
    42  		}
    43  		if x := v[i].To4(); x != nil {
    44  			if a.Len() != 32 {
    45  				t.Fatalf(`TestAddress(): IPv4 Address "%s" Len() should return "32"!`, a.String())
    46  			}
    47  		} else {
    48  			if a.Len() != 128 {
    49  				t.Fatalf(`TestAddress(): IPv6 Address "%s" Len() should return "128"!`, a.String())
    50  			}
    51  		}
    52  	}
    53  }