| 
									
										
										
										
											2021-07-14 08:25:30 +01:00
										 |  |  | package geoip
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"context"
 | 
					
						
							|  |  |  | 	"fmt"
 | 
					
						
							| 
									
										
										
										
											2022-05-02 19:25:02 +02:00
										 |  |  | 	"net"
 | 
					
						
							| 
									
										
										
										
											2021-07-14 08:25:30 +01:00
										 |  |  | 	"testing"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/metadata"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/test"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/request"
 | 
					
						
							| 
									
										
										
										
											2022-05-02 19:25:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							| 
									
										
										
										
											2021-07-14 08:25:30 +01:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestMetadata(t *testing.T) {
 | 
					
						
							|  |  |  | 	tests := []struct {
 | 
					
						
							|  |  |  | 		label         string
 | 
					
						
							|  |  |  | 		expectedValue string
 | 
					
						
							|  |  |  | 	}{
 | 
					
						
							| 
									
										
										
										
											2022-05-02 19:25:02 +02:00
										 |  |  | 		{"geoip/city/name", "Cambridge"},
 | 
					
						
							| 
									
										
										
										
											2021-07-14 08:25:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-02 19:25:02 +02:00
										 |  |  | 		{"geoip/country/code", "GB"},
 | 
					
						
							|  |  |  | 		{"geoip/country/name", "United Kingdom"},
 | 
					
						
							| 
									
										
										
										
											2021-07-14 08:25:30 +01:00
										 |  |  | 		// is_in_european_union is set to true only to work around bool zero value, and test is really being set.
 | 
					
						
							| 
									
										
										
										
											2022-05-02 19:25:02 +02:00
										 |  |  | 		{"geoip/country/is_in_european_union", "true"},
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		{"geoip/continent/code", "EU"},
 | 
					
						
							|  |  |  | 		{"geoip/continent/name", "Europe"},
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		{"geoip/latitude", "52.2242"},
 | 
					
						
							|  |  |  | 		{"geoip/longitude", "0.1315"},
 | 
					
						
							|  |  |  | 		{"geoip/timezone", "Europe/London"},
 | 
					
						
							|  |  |  | 		{"geoip/postalcode", "CB4"},
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-23 17:57:33 +08:00
										 |  |  | 	knownIPAddr := "81.2.69.142" // This IP should be part of the CDIR address range used to create the database fixtures.
 | 
					
						
							| 
									
										
										
										
											2022-05-02 19:25:02 +02:00
										 |  |  | 	for _, tc := range tests {
 | 
					
						
							|  |  |  | 		t.Run(fmt.Sprintf("%s/%s", tc.label, "direct"), func(t *testing.T) {
 | 
					
						
							|  |  |  | 			geoIP, err := newGeoIP(cityDBPath, false)
 | 
					
						
							|  |  |  | 			if err != nil {
 | 
					
						
							|  |  |  | 				t.Fatalf("unable to create geoIP plugin: %v", err)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			state := request.Request{
 | 
					
						
							|  |  |  | 				Req: new(dns.Msg),
 | 
					
						
							|  |  |  | 				W:   &test.ResponseWriter{RemoteIP: knownIPAddr},
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			testMetadata(t, state, geoIP, tc.label, tc.expectedValue)
 | 
					
						
							|  |  |  | 		})
 | 
					
						
							| 
									
										
										
										
											2021-07-14 08:25:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-02 19:25:02 +02:00
										 |  |  | 		t.Run(fmt.Sprintf("%s/%s", tc.label, "subnet"), func(t *testing.T) {
 | 
					
						
							|  |  |  | 			geoIP, err := newGeoIP(cityDBPath, true)
 | 
					
						
							|  |  |  | 			if err != nil {
 | 
					
						
							|  |  |  | 				t.Fatalf("unable to create geoIP plugin: %v", err)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			state := request.Request{
 | 
					
						
							|  |  |  | 				Req: new(dns.Msg),
 | 
					
						
							|  |  |  | 				W:   &test.ResponseWriter{RemoteIP: "127.0.0.1"},
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			state.Req.SetEdns0(4096, false)
 | 
					
						
							|  |  |  | 			if o := state.Req.IsEdns0(); o != nil {
 | 
					
						
							|  |  |  | 				addr := net.ParseIP(knownIPAddr)
 | 
					
						
							|  |  |  | 				o.Option = append(o.Option, (&dns.EDNS0_SUBNET{
 | 
					
						
							|  |  |  | 					SourceNetmask: 32,
 | 
					
						
							|  |  |  | 					Address:       addr,
 | 
					
						
							|  |  |  | 				}))
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			testMetadata(t, state, geoIP, tc.label, tc.expectedValue)
 | 
					
						
							|  |  |  | 		})
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func testMetadata(t *testing.T, state request.Request, geoIP *GeoIP, label, expectedValue string) {
 | 
					
						
							| 
									
										
										
										
											2025-06-05 00:36:04 +03:00
										 |  |  | 	t.Helper()
 | 
					
						
							| 
									
										
										
										
											2022-05-02 19:25:02 +02:00
										 |  |  | 	ctx := metadata.ContextWithMetadata(context.Background())
 | 
					
						
							|  |  |  | 	rCtx := geoIP.Metadata(ctx, state)
 | 
					
						
							|  |  |  | 	if fmt.Sprintf("%p", ctx) != fmt.Sprintf("%p", rCtx) {
 | 
					
						
							|  |  |  | 		t.Errorf("returned context is expected to be the same one passed in the Metadata function")
 | 
					
						
							| 
									
										
										
										
											2021-07-14 08:25:30 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-02 19:25:02 +02:00
										 |  |  | 	fn := metadata.ValueFunc(ctx, label)
 | 
					
						
							|  |  |  | 	if fn == nil {
 | 
					
						
							|  |  |  | 		t.Errorf("label %q not set in metadata plugin context", label)
 | 
					
						
							|  |  |  | 		return
 | 
					
						
							| 
									
										
										
										
											2021-07-14 08:25:30 +01:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2022-05-02 19:25:02 +02:00
										 |  |  | 	value := fn()
 | 
					
						
							|  |  |  | 	if value != expectedValue {
 | 
					
						
							|  |  |  | 		t.Errorf("expected value for label %q should be %q, got %q instead",
 | 
					
						
							|  |  |  | 			label, expectedValue, value)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2021-07-14 08:25:30 +01:00
										 |  |  | }
 |