github.com/prebid/prebid-server/v2@v2.18.0/config/requestvalidation_test.go (about)

     1  package config
     2  
     3  import (
     4  	"net"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestParse(t *testing.T) {
    11  	ipv4Mask16 := net.CIDRMask(16, 32)
    12  	ipv4Mask24 := net.CIDRMask(24, 32)
    13  
    14  	ipv6Mask16 := net.CIDRMask(16, 128)
    15  	ipv6Mask32 := net.CIDRMask(32, 128)
    16  
    17  	testCases := []struct {
    18  		description  string
    19  		ipv4         []string
    20  		ipv4Expected []net.IPNet
    21  		ipv6         []string
    22  		ipv6Expected []net.IPNet
    23  		expectedErr  string
    24  	}{
    25  		{
    26  			description:  "Empty",
    27  			ipv4:         []string{},
    28  			ipv4Expected: []net.IPNet{},
    29  			ipv6:         []string{},
    30  			ipv6Expected: []net.IPNet{},
    31  		},
    32  		{
    33  			description:  "One",
    34  			ipv4:         []string{"1.1.1.1/24"},
    35  			ipv4Expected: []net.IPNet{{IP: net.IP{1, 1, 1, 0}, Mask: ipv4Mask24}},
    36  			ipv6:         []string{"1111:2222::/16"},
    37  			ipv6Expected: []net.IPNet{{IP: net.ParseIP("1111::"), Mask: ipv6Mask16}},
    38  		},
    39  		{
    40  			description:  "One - Ignore Whitespace",
    41  			ipv4:         []string{"   1.1.1.1/24 "},
    42  			ipv4Expected: []net.IPNet{{IP: net.IP{1, 1, 1, 0}, Mask: ipv4Mask24}},
    43  			ipv6:         []string{"   1111:2222::/16 "},
    44  			ipv6Expected: []net.IPNet{{IP: net.ParseIP("1111::"), Mask: ipv6Mask16}},
    45  		},
    46  		{
    47  			description:  "Many",
    48  			ipv4:         []string{"1.1.1.1/24", "2.2.2.2/16"},
    49  			ipv4Expected: []net.IPNet{{IP: net.IP{1, 1, 1, 0}, Mask: ipv4Mask24}, {IP: net.IP{2, 2, 0, 0}, Mask: ipv4Mask16}},
    50  			ipv6:         []string{"1111:2222::/16", "1111:2222:3333::/32"},
    51  			ipv6Expected: []net.IPNet{{IP: net.ParseIP("1111::"), Mask: ipv6Mask16}, {IP: net.ParseIP("1111:2222::"), Mask: ipv6Mask32}},
    52  		},
    53  		{
    54  			description: "Malformed - IPv4 - One",
    55  			ipv4:        []string{"malformed1"},
    56  			ipv6:        []string{},
    57  			expectedErr: "Invalid private IPv4 network: 'malformed1'",
    58  		},
    59  		{
    60  			description: "Malformed - IPv4 - Many",
    61  			ipv4:        []string{"malformed1", "malformed2"},
    62  			ipv6:        []string{},
    63  			expectedErr: "Invalid private IPv4 network: 'malformed1','malformed2'",
    64  		},
    65  		{
    66  			description: "Malformed - IPv6 - One",
    67  			ipv4:        []string{},
    68  			ipv6:        []string{"malformed2"},
    69  			expectedErr: "Invalid private IPv6 network: 'malformed2'",
    70  		},
    71  		{
    72  			description: "Malformed - IPv6 - Many",
    73  			ipv4:        []string{},
    74  			ipv6:        []string{"malformed1", "malformed2"},
    75  			expectedErr: "Invalid private IPv6 network: 'malformed1','malformed2'",
    76  		},
    77  		{
    78  			description: "Malformed - Mixed",
    79  			ipv4:        []string{"malformed1"},
    80  			ipv6:        []string{"malformed2"},
    81  			expectedErr: "Invalid private IPv4 network: 'malformed1'",
    82  		},
    83  		{
    84  			description: "Malformed - IPv4 - Ignore Whitespace",
    85  			ipv4:        []string{"   malformed1 "},
    86  			ipv6:        []string{},
    87  			expectedErr: "Invalid private IPv4 network: 'malformed1'",
    88  		},
    89  		{
    90  			description: "Malformed - IPv6 - Ignore Whitespace",
    91  			ipv4:        []string{},
    92  			ipv6:        []string{"   malformed2 "},
    93  			expectedErr: "Invalid private IPv6 network: 'malformed2'",
    94  		},
    95  		{
    96  			description: "Malformed - IPv4 - Missing Network Mask",
    97  			ipv4:        []string{"1.1.1.1"},
    98  			ipv6:        []string{},
    99  			expectedErr: "Invalid private IPv4 network: '1.1.1.1'",
   100  		},
   101  		{
   102  			description: "Malformed - IPv6 - Missing Network Mask",
   103  			ipv4:        []string{},
   104  			ipv6:        []string{"1111::"},
   105  			expectedErr: "Invalid private IPv6 network: '1111::'",
   106  		},
   107  		{
   108  			description: "Malformed - IPv4 - Wrong IP Version",
   109  			ipv4:        []string{"1111::/16"},
   110  			ipv6:        []string{},
   111  			expectedErr: "Invalid private IPv4 network: '1111::/16'",
   112  		},
   113  		{
   114  			description: "Malformed - IPv6 - Wrong IP Version",
   115  			ipv4:        []string{},
   116  			ipv6:        []string{"1.1.1.1/16"},
   117  			expectedErr: "Invalid private IPv6 network: '1.1.1.1/16'",
   118  		},
   119  		{
   120  			description: "Malformed - IPv6 Mapped IPv4",
   121  			ipv4:        []string{"::FFFF:1.1.1.1"},
   122  			ipv6:        []string{},
   123  			expectedErr: "Invalid private IPv4 network: '::FFFF:1.1.1.1'",
   124  		},
   125  	}
   126  
   127  	for _, test := range testCases {
   128  		requestValidation := &RequestValidation{
   129  			IPv4PrivateNetworks: test.ipv4,
   130  			IPv6PrivateNetworks: test.ipv6,
   131  		}
   132  
   133  		err := requestValidation.Parse()
   134  
   135  		if test.expectedErr == "" {
   136  			assert.NoError(t, err, test.description+":err")
   137  		} else {
   138  			assert.Error(t, err, test.description+":err")
   139  			assert.Equal(t, test.expectedErr, err.Error(), test.description+":err_msg")
   140  		}
   141  
   142  		assert.ElementsMatch(t, requestValidation.IPv4PrivateNetworksParsed, test.ipv4Expected, test.description+":ipv4")
   143  		assert.ElementsMatch(t, requestValidation.IPv6PrivateNetworksParsed, test.ipv6Expected, test.description+":ipv6")
   144  	}
   145  }