Skip to content
Snippets Groups Projects
ip_test.go 2.33 KiB
Newer Older
  • Learn to ignore specific revisions
  • package net
    
    import (
    	"testing"
    
    	"github.com/stretchr/testify/assert"
    )
    
    func TestToUint32(t *testing.T) {
    	tests := []struct {
    		name     string
    		val      uint64
    		expected uint32
    	}{
    		{
    			name:     "IP: 172.24.5.1",
    			val:      2887255297,
    			expected: 2887255297,
    		},
    		{
    			name:     "bigger than IPv4 address",
    			val:      2887255295 + 17179869184,
    			expected: 2887255295,
    		},
    	}
    
    	for _, test := range tests {
    		t.Run(test.name, func(t *testing.T) {
    			ip := IP{
    				lower: test.val,
    			}
    			assert.Equal(t, test.expected, ip.ToUint32())
    		})
    	}
    }
    
    func TestCompare(t *testing.T) {
    	tests := []struct {
    		name     string
    
    Daniel Czerwonk's avatar
    Daniel Czerwonk committed
    		ip       IP
    		other    IP
    
    		expected int
    	}{
    		{
    			name: "equal",
    
    Daniel Czerwonk's avatar
    Daniel Czerwonk committed
    			ip: IP{
    
    				lower:  100,
    				higher: 200,
    			},
    
    Daniel Czerwonk's avatar
    Daniel Czerwonk committed
    			other: IP{
    
    				lower:  100,
    				higher: 200,
    			},
    			expected: 0,
    		},
    		{
    			name: "greater higher word",
    
    Daniel Czerwonk's avatar
    Daniel Czerwonk committed
    			ip: IP{
    
    				lower:  123,
    				higher: 200,
    			},
    
    Daniel Czerwonk's avatar
    Daniel Czerwonk committed
    			other: IP{
    
    				lower:  456,
    				higher: 100,
    			},
    			expected: 1,
    		},
    		{
    			name: "lesser higher word",
    
    Daniel Czerwonk's avatar
    Daniel Czerwonk committed
    			ip: IP{
    
    				lower:  123,
    				higher: 100,
    			},
    
    Daniel Czerwonk's avatar
    Daniel Czerwonk committed
    			other: IP{
    
    				lower:  456,
    				higher: 200,
    			},
    			expected: -1,
    		},
    		{
    			name: "equal higher word but lesser lower word",
    
    Daniel Czerwonk's avatar
    Daniel Czerwonk committed
    			ip: IP{
    
    				lower:  456,
    				higher: 100,
    			},
    
    Daniel Czerwonk's avatar
    Daniel Czerwonk committed
    			other: IP{
    
    				lower:  123,
    				higher: 100,
    			},
    			expected: 1,
    		},
    		{
    			name: "equal higher word but lesser lower word",
    
    Daniel Czerwonk's avatar
    Daniel Czerwonk committed
    			ip: IP{
    
    				lower:  123,
    				higher: 100,
    			},
    
    Daniel Czerwonk's avatar
    Daniel Czerwonk committed
    			other: IP{
    
    				lower:  456,
    				higher: 100,
    			},
    			expected: -1,
    		},
    	}
    
    	for _, test := range tests {
    		t.Run(test.name, func(t *testing.T) {
    			assert.Equal(t, test.expected, test.ip.Compare(test.other))
    		})
    	}
    }
    
    Daniel Czerwonk's avatar
    Daniel Czerwonk committed
    
    func TestIPString(t *testing.T) {
    	tests := []struct {
    		ip       IP
    		expected string
    	}{
    		{
    			ip:       IPv4(strAddr("192.168.0.1")),
    			expected: "192.168.0.1",
    		},
    		{
    			ip:       IPv4(strAddr("0.0.0.0")),
    			expected: "0.0.0.0",
    		},
    		{
    			ip:       IPv4(strAddr("255.255.255.255")),
    			expected: "255.255.255.255",
    		},
    		{
    			ip:       IPv6(0, 0),
    			expected: "0:0:0:0:0:0:0:0",
    		},
    		{
    			ip:       IPv6(2306131596687708724, 6230974922281175806),
    			expected: "2001:678:1E0:1234:5678:DEAD:BEEF:CAFE",
    		},
    		{
    			ip:       IPv6(^uint64(0), ^uint64(0)),
    			expected: "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF",
    		},
    	}
    
    	for _, test := range tests {
    		assert.Equal(t, test.expected, test.ip.String())
    	}
    }