github.com/prebid/prebid-server/v2@v2.18.0/util/httputil/httputil_test.go (about) 1 package httputil 2 3 import ( 4 "net" 5 "net/http" 6 "testing" 7 8 "github.com/prebid/prebid-server/v2/util/iputil" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestFindIP(t *testing.T) { 13 alwaysTrue := hardcodedResponseIPValidator{response: true} 14 alwaysFalse := hardcodedResponseIPValidator{response: false} 15 16 testCases := []struct { 17 description string 18 trueClientIP string 19 xForwardedFor string 20 xRealIP string 21 remoteAddr string 22 validator iputil.IPValidator 23 expectedIP net.IP 24 expectedVer iputil.IPVersion 25 }{ 26 { 27 description: "No Address", 28 expectedIP: nil, 29 expectedVer: iputil.IPvUnknown, 30 }, 31 { 32 description: "False Validator - IPv4", 33 trueClientIP: "1.1.1.1", 34 xForwardedFor: "2.2.2.2, 3.3.3.3", 35 xRealIP: "4.4.4.4", 36 remoteAddr: "5.5.5.5:5", 37 validator: alwaysFalse, 38 expectedIP: nil, 39 expectedVer: iputil.IPvUnknown, 40 }, 41 { 42 description: "False Validator - IPv6", 43 trueClientIP: "1111::", 44 xForwardedFor: "2222::, 3333::", 45 xRealIP: "4444::", 46 remoteAddr: "[5555::]:5]", 47 validator: alwaysFalse, 48 expectedIP: nil, 49 expectedVer: iputil.IPvUnknown, 50 }, 51 { 52 description: "True Validator - IPv4 - True Client IP", 53 trueClientIP: "1.1.1.1", 54 xForwardedFor: "2.2.2.2, 3.3.3.3", 55 xRealIP: "4.4.4.4", 56 remoteAddr: "5.5.5.5:5", 57 validator: alwaysTrue, 58 expectedIP: net.ParseIP("1.1.1.1"), 59 expectedVer: iputil.IPv4, 60 }, 61 { 62 description: "True Validator - IPv4 - True Client IP - Ignore Whitespace", 63 trueClientIP: " 1.1.1.1 ", 64 xForwardedFor: "2.2.2.2, 3.3.3.3", 65 xRealIP: "4.4.4.4", 66 remoteAddr: "5.5.5.5:5", 67 validator: alwaysTrue, 68 expectedIP: net.ParseIP("1.1.1.1"), 69 expectedVer: iputil.IPv4, 70 }, 71 { 72 description: "True Validator - IPv4 - X Forwarded For", 73 trueClientIP: "", 74 xForwardedFor: "2.2.2.2, 3.3.3.3", 75 xRealIP: "4.4.4.4", 76 remoteAddr: "5.5.5.5:5", 77 validator: alwaysTrue, 78 expectedIP: net.ParseIP("2.2.2.2"), 79 expectedVer: iputil.IPv4, 80 }, 81 { 82 description: "True Validator - IPv4 - X Forwarded For - Ignore Whitespace", 83 trueClientIP: "", 84 xForwardedFor: " 2.2.2.2, 3.3.3.3 ", 85 xRealIP: "4.4.4.4", 86 remoteAddr: "5.5.5.5:5", 87 validator: alwaysTrue, 88 expectedIP: net.ParseIP("2.2.2.2"), 89 expectedVer: iputil.IPv4, 90 }, 91 { 92 description: "True Validator - IPv4 - X Real IP", 93 trueClientIP: "", 94 xForwardedFor: "", 95 xRealIP: "4.4.4.4", 96 remoteAddr: "5.5.5.5:5", 97 validator: alwaysTrue, 98 expectedIP: net.ParseIP("4.4.4.4"), 99 expectedVer: iputil.IPv4, 100 }, 101 { 102 description: "True Validator - IPv4 - X Real IP - Ignore Whitespace", 103 trueClientIP: "", 104 xForwardedFor: "", 105 xRealIP: " 4.4.4.4 ", 106 remoteAddr: "5.5.5.5:5", 107 validator: alwaysTrue, 108 expectedIP: net.ParseIP("4.4.4.4"), 109 expectedVer: iputil.IPv4, 110 }, 111 { 112 description: "True Validator - IPv4 - Remote Address", 113 trueClientIP: "", 114 xForwardedFor: "", 115 xRealIP: "", 116 remoteAddr: "5.5.5.5:80", 117 validator: alwaysTrue, 118 expectedIP: net.ParseIP("5.5.5.5"), 119 expectedVer: iputil.IPv4, 120 }, 121 { 122 description: "True Validator - IPv6 - True Client IP", 123 trueClientIP: "1111::", 124 xForwardedFor: "2222::, 3333::", 125 xRealIP: "4444::", 126 remoteAddr: "[5555::]:5", 127 validator: alwaysTrue, 128 expectedIP: net.ParseIP("1111::"), 129 expectedVer: iputil.IPv6, 130 }, 131 { 132 description: "True Validator - IPv6 - True Client IP - Ignore Whitespace", 133 trueClientIP: " 1111:: ", 134 xForwardedFor: "2222::, 3333::", 135 xRealIP: "4444::", 136 remoteAddr: "[5555::]:5", 137 validator: alwaysTrue, 138 expectedIP: net.ParseIP("1111::"), 139 expectedVer: iputil.IPv6, 140 }, 141 { 142 description: "True Validator - IPv6 - X Forwarded For", 143 trueClientIP: "", 144 xForwardedFor: "2222::, 3333::", 145 xRealIP: "4444::", 146 remoteAddr: "[5555::]:5", 147 validator: alwaysTrue, 148 expectedIP: net.ParseIP("2222::"), 149 expectedVer: iputil.IPv6, 150 }, 151 { 152 description: "True Validator - IPv6 - X Forwarded For - Ignore Whitespace", 153 trueClientIP: "", 154 xForwardedFor: " 2222::, 3333:: ", 155 xRealIP: "4444::", 156 remoteAddr: "[5555::]:5", 157 validator: alwaysTrue, 158 expectedIP: net.ParseIP("2222::"), 159 expectedVer: iputil.IPv6, 160 }, 161 { 162 description: "True Validator - IPv6 - X Real IP", 163 trueClientIP: "", 164 xForwardedFor: "", 165 xRealIP: "4444::", 166 remoteAddr: "[5555::]:5", 167 validator: alwaysTrue, 168 expectedIP: net.ParseIP("4444::"), 169 expectedVer: iputil.IPv6, 170 }, 171 { 172 description: "True Validator - IPv6 - X Real IP - Ignore Whitespace", 173 trueClientIP: "", 174 xForwardedFor: "", 175 xRealIP: " 4444:: ", 176 remoteAddr: "[5555::]:5", 177 validator: alwaysTrue, 178 expectedIP: net.ParseIP("4444::"), 179 expectedVer: iputil.IPv6, 180 }, 181 { 182 description: "True Validator - IPv6 - Remote Address", 183 trueClientIP: "", 184 xForwardedFor: "", 185 xRealIP: "", 186 remoteAddr: "[5555::]:5", 187 validator: alwaysTrue, 188 expectedIP: net.ParseIP("5555::"), 189 expectedVer: iputil.IPv6, 190 }, 191 { 192 description: "True Validator - Malformed - All", 193 trueClientIP: "malformed", 194 xForwardedFor: "malformed", 195 xRealIP: "malformed", 196 remoteAddr: "malformed", 197 validator: alwaysTrue, 198 expectedIP: nil, 199 expectedVer: iputil.IPvUnknown, 200 }, 201 { 202 description: "True Validator - Malformed - Some", 203 trueClientIP: "malformed", 204 xForwardedFor: "malformed", 205 xRealIP: "4.4.4.4", 206 remoteAddr: "malformed", 207 validator: alwaysTrue, 208 expectedIP: net.ParseIP("4.4.4.4"), 209 expectedVer: iputil.IPv4, 210 }, 211 { 212 description: "True Validator - Malformed - X Forwarded For - IPv4", 213 trueClientIP: "malformed", 214 xForwardedFor: "malformed, 4.4.4.4, 3333::, malformed", 215 xRealIP: "malformed", 216 remoteAddr: "malformed", 217 validator: alwaysTrue, 218 expectedIP: net.ParseIP("4.4.4.4"), 219 expectedVer: iputil.IPv4, 220 }, 221 { 222 description: "True Validator - Malformed - X Forwarded For - IPv6", 223 trueClientIP: "malformed", 224 xForwardedFor: "malformed, 3333::, 4.4.4.4, malformed", 225 xRealIP: "malformed", 226 remoteAddr: "malformed", 227 validator: alwaysTrue, 228 expectedIP: net.ParseIP("3333::"), 229 expectedVer: iputil.IPv6, 230 }, 231 } 232 233 for _, test := range testCases { 234 // Build Request 235 request, err := http.NewRequest("GET", "http://anyurl.com", nil) 236 if err != nil { 237 t.Fatalf("Unable to create test http request. Err: %v", err) 238 } 239 if test.trueClientIP != "" { 240 request.Header.Add("True-Client-IP", test.trueClientIP) 241 } 242 if test.xForwardedFor != "" { 243 request.Header.Add("X-Forwarded-For", test.xForwardedFor) 244 } 245 if test.xRealIP != "" { 246 request.Header.Add("X-Real-IP", test.xRealIP) 247 } 248 request.RemoteAddr = test.remoteAddr 249 250 // Run Test 251 ip, ver := FindIP(request, test.validator) 252 253 // Assertions 254 assert.Equal(t, test.expectedIP, ip, test.description+":ip") 255 assert.Equal(t, test.expectedVer, ver, test.description+":ver") 256 } 257 } 258 259 type hardcodedResponseIPValidator struct { 260 response bool 261 } 262 263 func (v hardcodedResponseIPValidator) IsValid(net.IP, iputil.IPVersion) bool { 264 return v.response 265 }