github.com/philhug/dnscontrol@v0.2.4-0.20180625181521-921fa9849001/pkg/transform/ptr_test.go (about) 1 package transform 2 3 import ( 4 "fmt" 5 "testing" 6 ) 7 8 // ptrmagic(name, domain string, al int) (string, error) 9 10 func TestPtrMagic(t *testing.T) { 11 tests := []struct { 12 name string 13 domain string 14 output string 15 fail bool 16 }{ 17 // Magic IPv4: 18 {"1.2.3.4", "3.2.1.in-addr.arpa", "4", false}, 19 {"1.2.3.4", "2.1.in-addr.arpa", "4.3", false}, 20 {"1.2.3.4", "1.in-addr.arpa", "4.3.2", false}, 21 22 // No magic IPv4: 23 {"1", "2.3.4.in-addr.arpa", "1", false}, 24 {"1.2", "3.4.in-addr.arpa", "1.2", false}, 25 {"1.2.3", "4.in-addr.arpa", "1.2.3", false}, 26 {"1.2.3.4", "in-addr.arpa", "1.2.3.4", false}, // Not supported, but it works. 27 28 // Magic IPv6: 29 {"1", "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "1", false}, 30 {"1.0", "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "1.0", false}, 31 {"1.0.0", "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "1.0.0", false}, 32 {"1.0.0.0", "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "1.0.0.0", false}, 33 {"1.0.0.0.0", "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "1.0.0.0.0", false}, 34 {"1.0.0.0.0.0", "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "1.0.0.0.0.0", false}, 35 {"1.0.0.0.0.0.0", "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "1.0.0.0.0.0.0", false}, 36 {"1.0.0.0.0.0.0.0", "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "1.0.0.0.0.0.0.0", false}, 37 {"1.0.0.0.0.0.0.0.0", "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "1.0.0.0.0.0.0.0.0", false}, 38 {"1.0.0.0.0.0.0.0.0.0", "0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "1.0.0.0.0.0.0.0.0.0", false}, 39 {"1.0.0.0.0.0.0.0.0.0.0", "0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "1.0.0.0.0.0.0.0.0.0.0", false}, 40 {"1.0.0.0.0.0.0.0.0.0.0.0", "0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "1.0.0.0.0.0.0.0.0.0.0.0", false}, 41 {"1.0.0.0.0.0.0.0.0.0.0.0.0", "0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "1.0.0.0.0.0.0.0.0.0.0.0.0", false}, 42 {"1.0.0.0.0.0.0.0.0.0.0.0.0.0", "0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "1.0.0.0.0.0.0.0.0.0.0.0.0.0", false}, 43 44 // RFC2317 (Classless) 45 // 172.20.18.160/27 is .160 - .191: 46 {"172.20.18.159", "160/27.18.20.172.in-addr.arpa", "", true}, 47 {"172.20.18.160", "160/27.18.20.172.in-addr.arpa", "160", false}, 48 {"172.20.18.191", "160/27.18.20.172.in-addr.arpa", "191", false}, 49 {"172.20.18.192", "160/27.18.20.172.in-addr.arpa", "", true}, 50 51 // If it doesn't end in .arpa, the magic is disabled: 52 {"1.2.3.4", "example.com", "1.2.3.4", false}, 53 {"1", "example.com", "1", false}, 54 {"1.0.0.0", "example.com", "1.0.0.0", false}, 55 {"1.0.0.0.0.0.0.0", "example.com", "1.0.0.0.0.0.0.0", false}, 56 57 // User manually reversed addresses: 58 {"1.1.1.1.in-addr.arpa.", "1.1.in-addr.arpa", "1.1", false}, 59 {"1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa.", 60 "0.2.ip6.arpa", "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0", false}, 61 62 // Error cases: 63 {"1.1.1.1.in-addr.arpa.", "2.2.in-addr.arpa", "", true}, 64 {"1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa.", "9.9.ip6.arpa", "", true}, 65 {"3.3.3.3", "4.4.in-addr.arpa", "", true}, 66 {"2001:db8::1", "9.9.ip6.arpa", "", true}, 67 68 // These should be errors but we don't check for them at this time: 69 // {"blurg", "3.4.in-addr.arpa", "blurg", true}, 70 // {"1", "3.4.in-addr.arpa", "1", true}, 71 } 72 for _, tst := range tests { 73 t.Run(fmt.Sprintf("%s %s", tst.name, tst.domain), func(t *testing.T) { 74 o, errs := PtrNameMagic(tst.name, tst.domain) 75 if errs != nil && !tst.fail { 76 t.Errorf("Got error but expected none (%v)", errs) 77 } else if errs == nil && tst.fail { 78 t.Errorf("Expected error but got none (%v)", o) 79 } else if errs == nil && o != tst.output { 80 t.Errorf("Got (%v) expected (%v)", o, tst.output) 81 } 82 }) 83 } 84 }