diff --git a/edns.go b/edns.go index 7a58aa9b..0550aaa3 100644 --- a/edns.go +++ b/edns.go @@ -128,8 +128,18 @@ func (rr *OPT) Do() bool { } // SetDo sets the DO (DNSSEC OK) bit. -func (rr *OPT) SetDo() { - rr.Hdr.Ttl |= _DO +// If we pass an argument, set the DO bit to that value. +// It is possible to pass 2 or more arguments. Any arguments after the 1st is silently ignored. +func (rr *OPT) SetDo(do ...bool) { + if len(do) == 1 { + if do[0] { + rr.Hdr.Ttl |= _DO + } else { + rr.Hdr.Ttl &^= _DO + } + } else { + rr.Hdr.Ttl |= _DO + } } // EDNS0 defines an EDNS0 Option. An OPT RR can have multiple options appended to it. diff --git a/edns_test.go b/edns_test.go index 5fd75abb..c290b0c8 100644 --- a/edns_test.go +++ b/edns_test.go @@ -7,10 +7,46 @@ func TestOPTTtl(t *testing.T) { e.Hdr.Name = "." e.Hdr.Rrtype = TypeOPT + // verify the default setting of DO=0 if e.Do() { t.Errorf("DO bit should be zero") } + // There are 6 possible invocations of SetDo(): + // + // 1. Starting with DO=0, using SetDo() + // 2. Starting with DO=0, using SetDo(true) + // 3. Starting with DO=0, using SetDo(false) + // 4. Starting with DO=1, using SetDo() + // 5. Starting with DO=1, using SetDo(true) + // 6. Starting with DO=1, using SetDo(false) + + // verify that invoking SetDo() sets DO=1 (TEST #1) + e.SetDo() + if !e.Do() { + t.Errorf("DO bit should be non-zero") + } + // verify that using SetDo(true) works when DO=1 (TEST #5) + e.SetDo(true) + if !e.Do() { + t.Errorf("DO bit should still be non-zero") + } + // verify that we can use SetDo(false) to set DO=0 (TEST #6) + e.SetDo(false) + if e.Do() { + t.Errorf("DO bit should be zero") + } + // verify that if we call SetDo(false) when DO=0 that it is unchanged (TEST #3) + e.SetDo(false) + if e.Do() { + t.Errorf("DO bit should still be zero") + } + // verify that using SetDo(true) works for DO=0 (TEST #2) + e.SetDo(true) + if !e.Do() { + t.Errorf("DO bit should be non-zero") + } + // verify that using SetDo() works for DO=1 (TEST #4) e.SetDo() if !e.Do() { t.Errorf("DO bit should be non-zero")