decred.org/dcrdex@v1.0.5/dex/ip_test.go (about)

     1  // This code is available on the terms of the project LICENSE.md file,
     2  // also available online at https://blueoakcouncil.org/license/1.0.0.
     3  
     4  package dex
     5  
     6  import (
     7  	"testing"
     8  )
     9  
    10  func TestIPKey(t *testing.T) {
    11  	if NewIPKey("127.0.0.1") == NewIPKey("127.0.0.2") {
    12  		t.Fatalf("IPKey v4 failed basic comparison test, %x = %x", NewIPKey("127.0.0.1"), NewIPKey("127.0.0.2"))
    13  	}
    14  	if NewIPKey("[a:b:c:d::]:1234") == NewIPKey("[a:b:c:e::]:1234") {
    15  		t.Fatalf("IPKey v6 failed basic comparison test, %x = %x", NewIPKey("[a:b:c:d]"), NewIPKey("[a:b:c:d]"))
    16  	}
    17  
    18  	tests := []struct {
    19  		name, addr1, addr2 string
    20  		wantEqual          bool
    21  		wantString1        string
    22  	}{
    23  		{
    24  			name:        "bad",
    25  			addr1:       "",
    26  			addr2:       "xxx",
    27  			wantEqual:   true,
    28  			wantString1: "::",
    29  		},
    30  		{
    31  			name:        "ipv4 unequal",
    32  			addr1:       "127.0.0.1",
    33  			addr2:       "127.0.0.2",
    34  			wantEqual:   false,
    35  			wantString1: "127.0.0.1",
    36  		},
    37  		{
    38  			name:        "ipv4 equal",
    39  			addr1:       "127.0.0.1",
    40  			addr2:       "127.0.0.1",
    41  			wantEqual:   true,
    42  			wantString1: "127.0.0.1",
    43  		},
    44  		{
    45  			name:        "ipv4 port unequal",
    46  			addr1:       "127.0.0.1:1234",
    47  			addr2:       "127.0.0.2:1234",
    48  			wantEqual:   false,
    49  			wantString1: "127.0.0.1",
    50  		},
    51  		{
    52  			name:        "ipv4 port equal",
    53  			addr1:       "127.0.0.1:1234",
    54  			addr2:       "127.0.0.1:1234",
    55  			wantEqual:   true,
    56  			wantString1: "127.0.0.1",
    57  		},
    58  		{
    59  			name:        "ipv4 port equal noport",
    60  			addr1:       "127.0.0.1",
    61  			addr2:       "127.0.0.1:1234",
    62  			wantEqual:   true,
    63  			wantString1: "127.0.0.1",
    64  		},
    65  		{
    66  			name:        "ipv6 loopback with and without port",
    67  			addr1:       "[::1]", // brackets to strip
    68  			addr2:       "[::1]:13245",
    69  			wantEqual:   true,
    70  			wantString1: "::1", // mask exception for ipv6 loopback
    71  		},
    72  		{
    73  			name:        "ipv6 port unequal",
    74  			addr1:       "[a:b:c:d::]:1234",
    75  			addr2:       "[a:b:c:e::]:1234",
    76  			wantEqual:   false,
    77  			wantString1: "a:b:c:d::",
    78  		},
    79  		{
    80  			name:        "ipv6 port equal",
    81  			addr1:       "[a:b:c:d::]:1234",
    82  			addr2:       "[a:b:c:d::]:1234",
    83  			wantEqual:   true,
    84  			wantString1: "a:b:c:d::",
    85  		},
    86  		{
    87  			name:        "ipv6 port equal noport",
    88  			addr1:       "a:b:c:d::",
    89  			addr2:       "[a:b:c:d::]:1234",
    90  			wantEqual:   true,
    91  			wantString1: "a:b:c:d::",
    92  		},
    93  		{
    94  			name:        "ipv6 port equal noport with brackets",
    95  			addr1:       "[a:b:c:d::]", // brackets to strip
    96  			addr2:       "[a:b:c:d::]:1234",
    97  			wantEqual:   true,
    98  			wantString1: "a:b:c:d::",
    99  		},
   100  		{
   101  			name:        "ipv6 mask equal",
   102  			addr1:       "[a:b:c:d:e:f:a:b]:1234",
   103  			addr2:       "[a:b:c:d:f:d:c:f]:1234",
   104  			wantEqual:   true,
   105  			wantString1: "a:b:c:d::",
   106  		},
   107  	}
   108  
   109  	for _, tt := range tests {
   110  		ipKey1 := NewIPKey(tt.addr1)
   111  		ipKey2 := NewIPKey(tt.addr2)
   112  
   113  		if (ipKey1 == ipKey2) != tt.wantEqual {
   114  			t.Fatalf("%s: wantEqual = %t, addr1 = %s, addr2 = %s, ipKey1 = %x, ipKey2 = %x",
   115  				tt.name, tt.wantEqual, tt.addr1, tt.addr2, ipKey1[:], ipKey2[:])
   116  		}
   117  		if tt.wantString1 != ipKey1.String() {
   118  			t.Errorf("expected ipKey1 string %s, got %s", tt.wantString1, ipKey1.String())
   119  		}
   120  	}
   121  }