| 
									
										
										
										
											2019-11-01 12:02:43 -04:00
										 |  |  | package transfer
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"testing"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-24 18:14:41 +02:00
										 |  |  | 	"github.com/coredns/caddy"
 | 
					
						
							| 
									
										
										
										
											2019-11-01 12:02:43 -04:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestParse(t *testing.T) {
 | 
					
						
							|  |  |  | 	tests := []struct {
 | 
					
						
							|  |  |  | 		input     string
 | 
					
						
							| 
									
										
										
										
											2020-06-30 03:02:56 +08:00
										 |  |  | 		zones     []string
 | 
					
						
							| 
									
										
										
										
											2019-11-01 12:02:43 -04:00
										 |  |  | 		shouldErr bool
 | 
					
						
							|  |  |  | 		exp       *Transfer
 | 
					
						
							|  |  |  | 	}{
 | 
					
						
							|  |  |  | 		{`transfer example.net example.org {
 | 
					
						
							|  |  |  | 			to 1.2.3.4 5.6.7.8:1053 [1::2]:34
 | 
					
						
							|  |  |  | 		 }
 | 
					
						
							|  |  |  |          transfer example.com example.edu {
 | 
					
						
							|  |  |  |             to * 1.2.3.4
 | 
					
						
							|  |  |  |          }`,
 | 
					
						
							| 
									
										
										
										
											2020-06-30 03:02:56 +08:00
										 |  |  | 			nil,
 | 
					
						
							| 
									
										
										
										
											2019-11-01 12:02:43 -04:00
										 |  |  | 			false,
 | 
					
						
							|  |  |  | 			&Transfer{
 | 
					
						
							|  |  |  | 				xfrs: []*xfr{{
 | 
					
						
							|  |  |  | 					Zones: []string{"example.net.", "example.org."},
 | 
					
						
							|  |  |  | 					to:    []string{"1.2.3.4:53", "5.6.7.8:1053", "[1::2]:34"},
 | 
					
						
							|  |  |  | 				}, {
 | 
					
						
							|  |  |  | 					Zones: []string{"example.com.", "example.edu."},
 | 
					
						
							|  |  |  | 					to:    []string{"*", "1.2.3.4:53"},
 | 
					
						
							|  |  |  | 				}},
 | 
					
						
							|  |  |  | 			},
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		// errors
 | 
					
						
							|  |  |  | 		{`transfer example.net example.org {
 | 
					
						
							|  |  |  | 		 }`,
 | 
					
						
							| 
									
										
										
										
											2020-06-30 03:02:56 +08:00
										 |  |  | 			nil,
 | 
					
						
							| 
									
										
										
										
											2019-11-01 12:02:43 -04:00
										 |  |  | 			true,
 | 
					
						
							|  |  |  | 			nil,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{`transfer example.net example.org {
 | 
					
						
							|  |  |  |            invalid option
 | 
					
						
							|  |  |  | 		 }`,
 | 
					
						
							| 
									
										
										
										
											2020-06-30 03:02:56 +08:00
										 |  |  | 			nil,
 | 
					
						
							| 
									
										
										
										
											2019-11-01 12:02:43 -04:00
										 |  |  | 			true,
 | 
					
						
							|  |  |  | 			nil,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2020-06-30 03:02:56 +08:00
										 |  |  | 		{
 | 
					
						
							|  |  |  | 			`
 | 
					
						
							|  |  |  |          transfer example.com example.edu {
 | 
					
						
							|  |  |  |             to example.com 1.2.3.4
 | 
					
						
							|  |  |  |          }`,
 | 
					
						
							|  |  |  | 			nil,
 | 
					
						
							|  |  |  | 			true,
 | 
					
						
							|  |  |  | 			nil,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			`transfer {
 | 
					
						
							|  |  |  | 			to 1.2.3.4 5.6.7.8:1053 [1::2]:34
 | 
					
						
							|  |  |  | 		 }`,
 | 
					
						
							|  |  |  | 			[]string{"."},
 | 
					
						
							|  |  |  | 			false,
 | 
					
						
							|  |  |  | 			&Transfer{
 | 
					
						
							|  |  |  | 				xfrs: []*xfr{{
 | 
					
						
							|  |  |  | 					Zones: []string{"."},
 | 
					
						
							|  |  |  | 					to:    []string{"1.2.3.4:53", "5.6.7.8:1053", "[1::2]:34"},
 | 
					
						
							|  |  |  | 				}},
 | 
					
						
							|  |  |  | 			},
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2019-11-01 12:02:43 -04:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	for i, tc := range tests {
 | 
					
						
							| 
									
										
										
										
											2020-09-24 11:30:39 -07:00
										 |  |  | 		c := caddy.NewTestController("dns", tc.input)
 | 
					
						
							|  |  |  | 		c.ServerBlockKeys = append(c.ServerBlockKeys, tc.zones...)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		transfer, err := parseTransfer(c)
 | 
					
						
							| 
									
										
										
										
											2019-11-01 12:02:43 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if err == nil && tc.shouldErr {
 | 
					
						
							|  |  |  | 			t.Fatalf("Test %d expected errors, but got no error", i)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		if err != nil && !tc.shouldErr {
 | 
					
						
							|  |  |  | 			t.Fatalf("Test %d expected no errors, but got '%v'", i, err)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2020-06-30 03:02:56 +08:00
										 |  |  | 		if tc.exp == nil && transfer != nil {
 | 
					
						
							|  |  |  | 			t.Fatalf("Test %d expected %v xfrs, got %#v", i, tc.exp, transfer)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2019-11-01 12:02:43 -04:00
										 |  |  | 		if tc.shouldErr {
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if len(tc.exp.xfrs) != len(transfer.xfrs) {
 | 
					
						
							|  |  |  | 			t.Fatalf("Test %d expected %d xfrs, got %d", i, len(tc.exp.xfrs), len(transfer.xfrs))
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		for j, x := range transfer.xfrs {
 | 
					
						
							|  |  |  | 			// Check Zones
 | 
					
						
							|  |  |  | 			if len(tc.exp.xfrs[j].Zones) != len(x.Zones) {
 | 
					
						
							|  |  |  | 				t.Fatalf("Test %d expected %d zones, got %d", i, len(tc.exp.xfrs[i].Zones), len(x.Zones))
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			for k, zone := range x.Zones {
 | 
					
						
							|  |  |  | 				if tc.exp.xfrs[j].Zones[k] != zone {
 | 
					
						
							|  |  |  | 					t.Errorf("Test %d expected zone %v, got %v", i, tc.exp.xfrs[j].Zones[k], zone)
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			// Check to
 | 
					
						
							|  |  |  | 			if len(tc.exp.xfrs[j].to) != len(x.to) {
 | 
					
						
							|  |  |  | 				t.Fatalf("Test %d expected %d 'to' values, got %d", i, len(tc.exp.xfrs[i].to), len(x.to))
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			for k, to := range x.to {
 | 
					
						
							|  |  |  | 				if tc.exp.xfrs[j].to[k] != to {
 | 
					
						
							|  |  |  | 					t.Errorf("Test %d expected %v in 'to', got %v", i, tc.exp.xfrs[j].to[k], to)
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2020-06-30 03:02:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestSetup(t *testing.T) {
 | 
					
						
							|  |  |  | 	c := caddy.NewTestController("dns", "transfer")
 | 
					
						
							|  |  |  | 	if err := setup(c); err == nil {
 | 
					
						
							|  |  |  | 		t.Fatal("Expected errors, but got nil")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	c = caddy.NewTestController("dns", `transfer example.net example.org {
 | 
					
						
							|  |  |  | 			to 1.2.3.4 5.6.7.8:1053 [1::2]:34
 | 
					
						
							|  |  |  | 		 }
 | 
					
						
							|  |  |  |          transfer example.com example.edu {
 | 
					
						
							|  |  |  |             to * 1.2.3.4
 | 
					
						
							|  |  |  |          }`)
 | 
					
						
							|  |  |  | 	if err := setup(c); err != nil {
 | 
					
						
							|  |  |  | 		t.Fatalf("Expected no errors, but got %v", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 |