github.com/prebid/prebid-server/v2@v2.18.0/util/iputil/validator_test.go (about)

     1  package iputil
     2  
     3  import (
     4  	"net"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestPublicNetworkIPValidator(t *testing.T) {
    11  	ipv4Network1 := net.IPNet{IP: net.ParseIP("1.0.0.0"), Mask: net.CIDRMask(8, 32)}
    12  	ipv4Network2 := net.IPNet{IP: net.ParseIP("2.0.0.0"), Mask: net.CIDRMask(8, 32)}
    13  
    14  	ipv6Network1 := net.IPNet{IP: net.ParseIP("3300::"), Mask: net.CIDRMask(8, 128)}
    15  	ipv6Network2 := net.IPNet{IP: net.ParseIP("4400::"), Mask: net.CIDRMask(8, 128)}
    16  
    17  	testCases := []struct {
    18  		description         string
    19  		ip                  net.IP
    20  		ver                 IPVersion
    21  		ipv4PrivateNetworks []net.IPNet
    22  		ipv6PrivateNetworks []net.IPNet
    23  		expected            bool
    24  	}{
    25  		{
    26  			description:         "IPv4 - Public - None",
    27  			ip:                  net.ParseIP("1.1.1.1"),
    28  			ver:                 IPv4,
    29  			ipv4PrivateNetworks: []net.IPNet{},
    30  			ipv6PrivateNetworks: []net.IPNet{},
    31  			expected:            true,
    32  		},
    33  		{
    34  			description:         "IPv4 - Public - One",
    35  			ip:                  net.ParseIP("2.2.2.2"),
    36  			ver:                 IPv4,
    37  			ipv4PrivateNetworks: []net.IPNet{ipv4Network1},
    38  			ipv6PrivateNetworks: []net.IPNet{},
    39  			expected:            true,
    40  		},
    41  		{
    42  			description:         "IPv4 - Public - Many",
    43  			ip:                  net.ParseIP("3.3.3.3"),
    44  			ver:                 IPv4,
    45  			ipv4PrivateNetworks: []net.IPNet{ipv4Network1, ipv4Network2},
    46  			ipv6PrivateNetworks: []net.IPNet{},
    47  			expected:            true,
    48  		},
    49  		{
    50  			description:         "IPv4 - Private - One",
    51  			ip:                  net.ParseIP("1.1.1.1"),
    52  			ver:                 IPv4,
    53  			ipv4PrivateNetworks: []net.IPNet{ipv4Network1},
    54  			ipv6PrivateNetworks: []net.IPNet{},
    55  			expected:            false,
    56  		},
    57  		{
    58  			description:         "IPv4 - Private - Many",
    59  			ip:                  net.ParseIP("2.2.2.2"),
    60  			ver:                 IPv4,
    61  			ipv4PrivateNetworks: []net.IPNet{ipv4Network1, ipv4Network2},
    62  			ipv6PrivateNetworks: []net.IPNet{},
    63  			expected:            false,
    64  		},
    65  		{
    66  			description:         "IPv6 - Public - None",
    67  			ip:                  net.ParseIP("3333::"),
    68  			ver:                 IPv6,
    69  			ipv4PrivateNetworks: []net.IPNet{},
    70  			ipv6PrivateNetworks: []net.IPNet{},
    71  			expected:            true,
    72  		},
    73  		{
    74  			description:         "IPv6 - Public - One",
    75  			ip:                  net.ParseIP("4444::"),
    76  			ver:                 IPv6,
    77  			ipv4PrivateNetworks: []net.IPNet{},
    78  			ipv6PrivateNetworks: []net.IPNet{ipv6Network1},
    79  			expected:            true,
    80  		},
    81  		{
    82  			description:         "IPv6 - Public - Many",
    83  			ip:                  net.ParseIP("5555::"),
    84  			ver:                 IPv6,
    85  			ipv4PrivateNetworks: []net.IPNet{},
    86  			ipv6PrivateNetworks: []net.IPNet{ipv6Network1, ipv6Network2},
    87  			expected:            true,
    88  		},
    89  		{
    90  			description:         "IPv6 - Private - One",
    91  			ip:                  net.ParseIP("3333::"),
    92  			ver:                 IPv6,
    93  			ipv4PrivateNetworks: []net.IPNet{},
    94  			ipv6PrivateNetworks: []net.IPNet{ipv6Network1},
    95  			expected:            false,
    96  		},
    97  		{
    98  			description:         "IPv6 - Private - Many",
    99  			ip:                  net.ParseIP("4444::"),
   100  			ver:                 IPv6,
   101  			ipv4PrivateNetworks: []net.IPNet{},
   102  			ipv6PrivateNetworks: []net.IPNet{ipv6Network1, ipv6Network2},
   103  			expected:            false,
   104  		},
   105  		{
   106  			description:         "Mixed - Unknown",
   107  			ip:                  net.ParseIP("3.3.3.3"),
   108  			ver:                 IPvUnknown,
   109  			ipv4PrivateNetworks: []net.IPNet{ipv4Network1, ipv4Network1},
   110  			ipv6PrivateNetworks: []net.IPNet{ipv6Network1, ipv6Network2},
   111  			expected:            false,
   112  		},
   113  		{
   114  			description:         "Mixed - Public - IPv4",
   115  			ip:                  net.ParseIP("3.3.3.3"),
   116  			ver:                 IPv4,
   117  			ipv4PrivateNetworks: []net.IPNet{ipv4Network1, ipv4Network1},
   118  			ipv6PrivateNetworks: []net.IPNet{ipv6Network1, ipv6Network2},
   119  			expected:            true,
   120  		},
   121  		{
   122  			description:         "Mixed - Public - IPv6",
   123  			ip:                  net.ParseIP("5555::"),
   124  			ver:                 IPv6,
   125  			ipv4PrivateNetworks: []net.IPNet{ipv4Network1, ipv4Network1},
   126  			ipv6PrivateNetworks: []net.IPNet{ipv6Network1, ipv6Network2},
   127  			expected:            true,
   128  		},
   129  		{
   130  			description:         "Mixed - Private - IPv4",
   131  			ip:                  net.ParseIP("1.1.1.1"),
   132  			ver:                 IPv4,
   133  			ipv4PrivateNetworks: []net.IPNet{ipv4Network1, ipv4Network1},
   134  			ipv6PrivateNetworks: []net.IPNet{ipv6Network1, ipv6Network2},
   135  			expected:            false,
   136  		},
   137  		{
   138  			description:         "Mixed - Private - IPv6",
   139  			ip:                  net.ParseIP("3333::"),
   140  			ver:                 IPv6,
   141  			ipv4PrivateNetworks: []net.IPNet{ipv4Network1, ipv4Network1},
   142  			ipv6PrivateNetworks: []net.IPNet{ipv6Network1, ipv6Network2},
   143  			expected:            false,
   144  		},
   145  		{
   146  			description:         "Mixed - Public - IPv6 Encoded IPv4",
   147  			ip:                  net.ParseIP("::FFFF:1.1.1.1"),
   148  			ver:                 IPv6,
   149  			ipv4PrivateNetworks: []net.IPNet{{IP: net.ParseIP("1.0.0.0"), Mask: net.CIDRMask(8, 32)}},
   150  			ipv6PrivateNetworks: []net.IPNet{{IP: net.ParseIP("::FFFF:2.0.0.0"), Mask: net.CIDRMask(108, 128)}},
   151  			expected:            true,
   152  		},
   153  		{
   154  			description:         "Mixed - Private - IPv6 Encoded IPv4",
   155  			ip:                  net.ParseIP("::FFFF:2.2.2.2"),
   156  			ver:                 IPv6,
   157  			ipv4PrivateNetworks: []net.IPNet{{IP: net.ParseIP("1.0.0.0"), Mask: net.CIDRMask(8, 32)}},
   158  			ipv6PrivateNetworks: []net.IPNet{{IP: net.ParseIP("::FFFF:2.0.0.0"), Mask: net.CIDRMask(108, 128)}},
   159  			expected:            false,
   160  		},
   161  	}
   162  
   163  	for _, test := range testCases {
   164  		requestValidation := PublicNetworkIPValidator{
   165  			IPv4PrivateNetworks: test.ipv4PrivateNetworks,
   166  			IPv6PrivateNetworks: test.ipv6PrivateNetworks,
   167  		}
   168  
   169  		result := requestValidation.IsValid(test.ip, test.ver)
   170  
   171  		assert.Equal(t, test.expected, result, test.description)
   172  	}
   173  }
   174  
   175  func TestVersionIPValidator(t *testing.T) {
   176  	testCases := []struct {
   177  		description      string
   178  		validatorVersion IPVersion
   179  		ip               net.IP
   180  		ipVer            IPVersion
   181  		expected         bool
   182  	}{
   183  		{
   184  			description:      "IPv4",
   185  			validatorVersion: IPv4,
   186  			ip:               net.ParseIP("1.1.1.1"),
   187  			ipVer:            IPv4,
   188  			expected:         true,
   189  		},
   190  		{
   191  			description:      "IPv4 - Given Unknown",
   192  			validatorVersion: IPv4,
   193  			ip:               nil,
   194  			ipVer:            IPvUnknown,
   195  			expected:         false,
   196  		},
   197  		{
   198  			description:      "IPv6",
   199  			validatorVersion: IPv6,
   200  			ip:               net.ParseIP("1111::"),
   201  			ipVer:            IPv6,
   202  			expected:         true,
   203  		},
   204  		{
   205  			description:      "IPv6 - Given Unknown",
   206  			validatorVersion: IPv6,
   207  			ip:               nil,
   208  			ipVer:            IPvUnknown,
   209  			expected:         false,
   210  		},
   211  	}
   212  
   213  	for _, test := range testCases {
   214  		m := VersionIPValidator{
   215  			Version: test.validatorVersion,
   216  		}
   217  
   218  		result := m.IsValid(test.ip, test.ipVer)
   219  
   220  		assert.Equal(t, test.expected, result)
   221  	}
   222  }