github.com/karsthammer/dnscontrol@v0.2.8/models/dns_test.go (about)

     1  package models
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestHasRecordTypeName(t *testing.T) {
     8  	x := &RecordConfig{
     9  		Type: "A",
    10  		Name: "@",
    11  	}
    12  	dc := DomainConfig{}
    13  	if dc.HasRecordTypeName("A", "@") {
    14  		t.Errorf("%v: expected (%v) got (%v)\n", dc.Records, false, true)
    15  	}
    16  	dc.Records = append(dc.Records, x)
    17  	if !dc.HasRecordTypeName("A", "@") {
    18  		t.Errorf("%v: expected (%v) got (%v)\n", dc.Records, true, false)
    19  	}
    20  	if dc.HasRecordTypeName("AAAA", "@") {
    21  		t.Errorf("%v: expected (%v) got (%v)\n", dc.Records, false, true)
    22  	}
    23  }
    24  
    25  func TestRR(t *testing.T) {
    26  	experiment := RecordConfig{
    27  		Type:         "A",
    28  		Name:         "foo",
    29  		NameFQDN:     "foo.example.com",
    30  		Target:       "1.2.3.4",
    31  		TTL:          0,
    32  		MxPreference: 0,
    33  	}
    34  	expected := "foo.example.com.\t300\tIN\tA\t1.2.3.4"
    35  	found := experiment.ToRR().String()
    36  	if found != expected {
    37  		t.Errorf("RR expected (%#v) got (%#v)\n", expected, found)
    38  	}
    39  
    40  	experiment = RecordConfig{
    41  		Type:     "CAA",
    42  		Name:     "@",
    43  		NameFQDN: "example.com",
    44  		Target:   "mailto:test@example.com",
    45  		TTL:      300,
    46  		CaaTag:   "iodef",
    47  		CaaFlag:  1,
    48  	}
    49  	expected = "example.com.\t300\tIN\tCAA\t1 iodef \"mailto:test@example.com\""
    50  	found = experiment.ToRR().String()
    51  	if found != expected {
    52  		t.Errorf("RR expected (%#v) got (%#v)\n", expected, found)
    53  	}
    54  
    55  	experiment = RecordConfig{
    56  		Type:             "TLSA",
    57  		Name:             "@",
    58  		NameFQDN:         "_443._tcp.example.com",
    59  		Target:           "abcdef0123456789",
    60  		TTL:              300,
    61  		TlsaUsage:        0,
    62  		TlsaSelector:     0,
    63  		TlsaMatchingType: 1,
    64  	}
    65  	expected = "_443._tcp.example.com.\t300\tIN\tTLSA\t0 0 1 abcdef0123456789"
    66  	found = experiment.ToRR().String()
    67  	if found != expected {
    68  		t.Errorf("RR expected (%#v) got (%#v)\n", expected, found)
    69  	}
    70  }
    71  
    72  func TestDowncase(t *testing.T) {
    73  	dc := DomainConfig{Records: Records{
    74  		&RecordConfig{Type: "MX", Name: "lower", Target: "targetmx"},
    75  		&RecordConfig{Type: "MX", Name: "UPPER", Target: "TARGETMX"},
    76  	}}
    77  	downcase(dc.Records)
    78  	if !dc.HasRecordTypeName("MX", "lower") {
    79  		t.Errorf("%v: expected (%v) got (%v)\n", dc.Records, false, true)
    80  	}
    81  	if !dc.HasRecordTypeName("MX", "upper") {
    82  		t.Errorf("%v: expected (%v) got (%v)\n", dc.Records, false, true)
    83  	}
    84  	if dc.Records[0].GetTargetField() != "targetmx" {
    85  		t.Errorf("%v: target0 expected (%v) got (%v)\n", dc.Records, "targetmx", dc.Records[0].GetTargetField())
    86  	}
    87  	if dc.Records[1].GetTargetField() != "targetmx" {
    88  		t.Errorf("%v: target1 expected (%v) got (%v)\n", dc.Records, "targetmx", dc.Records[1].GetTargetField())
    89  	}
    90  }