| 1 | package dnsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ |
| 2 | |
| 3 | import ( |
| 4 | "encoding/binaryscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/" |
| 5 | "netscip-go gomod github.com/golang/go/src go1.25.0 net/" |
| 6 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 7 | ) |
| 8 | |
| 9 | func TestAcceptNotifyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TestAcceptNotify().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 10 | HandleFuncscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/HandleFunc().("example.org.", handleNotifyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/handleNotify().) |
| 11 | s, addrstr, _, err := RunLocalUDPServerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RunLocalUDPServer().(":0") |
| 12 | if err != nil { |
| 13 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("unable to run test server: %v", err) |
| 14 | } |
| 15 | defer s.Shutdownscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Server#Shutdown().() |
| 16 | |
| 17 | m := new(Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) |
| 18 | m.SetNotifyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#SetNotify().("example.org.") |
| 19 | // Set a SOA hint in the answer section, this is allowed according to RFC 1996. |
| 20 | soa, _ := NewRRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/NewRR().("example.org. IN SOA sns.dns.icann.org. noc.dns.icann.org. 2018112827 7200 3600 1209600 3600") |
| 21 | m.Answerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Answer. = []RRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#{soa} |
| 22 | |
| 23 | c := new(Clientscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Client#) |
| 24 | resp, _, err := c.Exchangescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Client#Exchange().(m, addrstr) |
| 25 | if err != nil { |
| 26 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("failed to exchange: %v", err) |
| 27 | } |
| 28 | if resp.Rcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Rcode. != RcodeSuccessscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeSuccess. { |
| 29 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("expected %s, got %s", RcodeToStringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeToString.[RcodeSuccessscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeSuccess.], RcodeToStringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeToString.[resp.Rcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Rcode.]) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func handleNotifyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/handleNotify().(w ResponseWriterscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ResponseWriter#, req *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) { |
| 34 | m := new(Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) |
| 35 | m.SetReplyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#SetReply().(req) |
| 36 | w.WriteMsgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ResponseWriter#WriteMsg.(m) |
| 37 | } |
| 38 | |
| 39 | func TestInvalidMsgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TestInvalidMsg().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 40 | HandleFuncscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/HandleFunc().("example.org.", func(ResponseWriterscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ResponseWriter#, *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) { |
| 41 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().("the handler must not be called in any of these tests") |
| 42 | }) |
| 43 | s, addrstr, _, err := RunLocalTCPServerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RunLocalTCPServer().(":0") |
| 44 | if err != nil { |
| 45 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("unable to run test server: %v", err) |
| 46 | } |
| 47 | defer s.Shutdownscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Server#Shutdown().() |
| 48 | |
| 49 | s.MsgAcceptFuncscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Server#MsgAcceptFunc. = func(dh Headerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#) MsgAcceptActionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgAcceptAction# { |
| 50 | switch dh.Idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Id. { |
| 51 | case 0x0001: |
| 52 | return MsgAcceptscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgAccept. |
| 53 | case 0x0002: |
| 54 | return MsgRejectscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgReject. |
| 55 | case 0x0003: |
| 56 | return MsgIgnorescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgIgnore. |
| 57 | case 0x0004: |
| 58 | return MsgRejectNotImplementedscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgRejectNotImplemented. |
| 59 | default: |
| 60 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("unexpected ID %x", dh.Idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Id.) |
| 61 | return -1 |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | invalidErrors := make(chan error) |
| 66 | s.MsgInvalidFuncscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Server#MsgInvalidFunc. = func(m []byte, err error) { |
| 67 | invalidErrors <- err |
| 68 | } |
| 69 | |
| 70 | c, err := netscip-go gomod github.com/golang/go/src go1.25.0 net/.Dialscip-go gomod github.com/golang/go/src go1.25.0 net/Dial().("tcp", addrstr) |
| 71 | if err != nil { |
| 72 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("cannot connect to test server: %v", err) |
| 73 | } |
| 74 | |
| 75 | write := func(m []byte) { |
| 76 | var length [2]byte |
| 77 | binaryscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/BigEndian..PutUint16scip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/bigEndian#PutUint16().(length[:], uint16(len(m))) |
| 78 | _, err := c.Writescip-go gomod github.com/golang/go/src go1.25.0 net/Conn#Write().(length[:]) |
| 79 | if err != nil { |
| 80 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("length write failed: %v", err) |
| 81 | } |
| 82 | _, err = c.Writescip-go gomod github.com/golang/go/src go1.25.0 net/Conn#Write().(m) |
| 83 | if err != nil { |
| 84 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("content write failed: %v", err) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /* Message is too short, so there is no header to accept or reject. */ |
| 89 | |
| 90 | tooShortMessage := make([]byte, 11) |
| 91 | tooShortMessage[1] = 0x3 // ID = 3, would be ignored if it were parsable. |
| 92 | |
| 93 | write(tooShortMessage) |
| 94 | // Expect an error to be reported. |
| 95 | <-invalidErrors |
| 96 | |
| 97 | /* Message is accepted but is actually invalid. */ |
| 98 | |
| 99 | badMessage := make([]byte, 13) |
| 100 | badMessage[1] = 0x1 // ID = 1, Accept. |
| 101 | badMessage[5] = 1 // QDCOUNT = 1 |
| 102 | badMessage[12] = 99 // Bad question section. Invalid! |
| 103 | |
| 104 | write(badMessage) |
| 105 | // Expect an error to be reported. |
| 106 | <-invalidErrors |
| 107 | |
| 108 | /* Message is rejected before it can be determined to be invalid. */ |
| 109 | |
| 110 | close(invalidErrors) // A call to InvalidMsgFunc would panic due to the closed chan. |
| 111 | |
| 112 | badMessage[1] = 0x2 // ID = 2, Reject |
| 113 | write(badMessage) |
| 114 | |
| 115 | badMessage[1] = 0x3 // ID = 3, Ignore |
| 116 | write(badMessage) |
| 117 | |
| 118 | badMessage[1] = 0x4 // ID = 4, RejectNotImplemented |
| 119 | write(badMessage) |
| 120 | } |