Simple function to clear the DO bit from an OPT resource record. (#407)
* Function to clear the DO bit from an OPT RR. * Tests for ClearDo() function. * Changed from ClearDo() to SetDo() with an optional argument. * Update doc string for SetDo(). Make tests for SetDo() comprehensive.
This commit is contained in:
parent
fce7af874c
commit
b96e5025a3
14
edns.go
14
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.
|
||||
|
|
36
edns_test.go
36
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")
|
||||
|
|
Loading…
Reference in New Issue