dns_name_roundtrip.mx raw

   1  // The dnsmessage Name round trip, over the public API only.
   2  //
   3  // Name.Data used to be an array field, which is why the port's first version of
   4  // unpack wrote `name := n.Data[:0]` and pushed into a COPY: every parsed name
   5  // came back empty while Length looked right, and the resolver rejected every
   6  // reply. Data is a slice now; this fixture pins the behavior that has to survive
   7  // that change, including the malformed shapes that must fail rather than panic.
   8  package main
   9  
  10  import (
  11  	"fmt"
  12  	"os"
  13  
  14  	"golang.org/x/net/dns/dnsmessage"
  15  )
  16  
  17  func fail(msg string) {
  18  	fmt.Printf("FAIL %s\n", msg)
  19  	os.Exit(1)
  20  }
  21  
  22  func name(s string) (n dnsmessage.Name) {
  23  	n, err := dnsmessage.NewName(s)
  24  	if err != nil {
  25  		fail("NewName(" | s | ")")
  26  	}
  27  	return n
  28  }
  29  
  30  // roundTrip packs one question and unpacks the message again.
  31  func roundTrip(want string) (got string) {
  32  	q := dnsmessage.Question{
  33  		Name:  name(want),
  34  		Type:  dnsmessage.TypeA,
  35  		Class: dnsmessage.ClassINET,
  36  	}
  37  	m := dnsmessage.Message{
  38  		Header:    dnsmessage.Header{ID: 1, RecursionDesired: true},
  39  		Questions: []dnsmessage.Question{q},
  40  	}
  41  	buf, err := m.Pack()
  42  	if err != nil {
  43  		fail("Pack(" | want | ")")
  44  	}
  45  	var out dnsmessage.Message
  46  	if uerr := out.Unpack(buf); uerr != nil {
  47  		fail("Unpack(" | want | ")")
  48  	}
  49  	if len(out.Questions) != 1 {
  50  		fail("question count for " | want)
  51  	}
  52  	return out.Questions[0].Name.String()
  53  }
  54  
  55  func main() {
  56  	// Termination: a batch program must still be stoppable, so the entry
  57  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  58  	select {
  59  	case <-shutdown:
  60  		return
  61  	default:
  62  	}
  63  
  64  	// A plain name, the root, and a 63-byte label (the maximum a label may be).
  65  	if got := roundTrip("example.com."); got != "example.com." {
  66  		fail("example.com. round trip: [" | got | "]")
  67  	}
  68  	if got := roundTrip("."); got != "." {
  69  		fail("root round trip: [" | got | "]")
  70  	}
  71  	label63 := ""
  72  	for i := int32(0); i < 63; i++ {
  73  		label63 = label63 | "a"
  74  	}
  75  	if got := roundTrip(label63 | ".example."); got != label63 | ".example." {
  76  		fail("63-byte label round trip")
  77  	}
  78  
  79  	// The longest name that fits: 63+1+63+1+63+1+61+1 = 254 bytes.
  80  	label61 := ""
  81  	for i := int32(0); i < 61; i++ {
  82  		label61 = label61 | "d"
  83  	}
  84  	longest := label63 | "." | label63 | "." | label63 | "." | label61 | "."
  85  	if len(longest) != 254 {
  86  		fail("fixture: longest name should be 254 bytes")
  87  	}
  88  	if got := roundTrip(longest); got != longest {
  89  		fail("maximum-length round trip")
  90  	}
  91  
  92  	// A 64-byte label is not encodable: the two top bits of the length byte are
  93  	// reserved, so pack must reject it.
  94  	label64 := label63 | "a"
  95  	segTooLong := dnsmessage.Message{
  96  		Header:    dnsmessage.Header{ID: 2},
  97  		Questions: []dnsmessage.Question{{Name: name(label64 | "."), Type: dnsmessage.TypeA, Class: dnsmessage.ClassINET}},
  98  	}
  99  	if _, e1 := segTooLong.Pack(); e1 == nil {
 100  		fail("64-byte label must not pack")
 101  	}
 102  
 103  	// A non-canonical name (no trailing dot) must not pack.
 104  	nonCanonical := dnsmessage.Message{
 105  		Header:    dnsmessage.Header{ID: 3},
 106  		Questions: []dnsmessage.Question{{Name: name("example.com"), Type: dnsmessage.TypeA, Class: dnsmessage.ClassINET}},
 107  	}
 108  	if _, e2 := nonCanonical.Pack(); e2 == nil {
 109  		fail("non-canonical name must not pack")
 110  	}
 111  
 112  	// A name whose Length runs past its storage is malformed: pack fails, and
 113  	// String returns empty rather than indexing past the slice.
 114  	broken := dnsmessage.Name{Length: 5}
 115  	brokenMsg := dnsmessage.Message{
 116  		Header:    dnsmessage.Header{ID: 4},
 117  		Questions: []dnsmessage.Question{{Name: broken, Type: dnsmessage.TypeA, Class: dnsmessage.ClassINET}},
 118  	}
 119  	if _, e3 := brokenMsg.Pack(); e3 == nil {
 120  		fail("Length beyond storage must not pack")
 121  	}
 122  	if got := broken.String(); got != "" {
 123  		fail("Length beyond storage String")
 124  	}
 125  
 126  	// Two questions with the same name: the packer compresses the second as a
 127  	// pointer, so unpack has to follow one. This is the compression path the
 128  	// resolver depends on.
 129  	twoNames := dnsmessage.Message{
 130  		Header: dnsmessage.Header{ID: 5},
 131  		Questions: []dnsmessage.Question{
 132  			{Name: name("compressed.example."), Type: dnsmessage.TypeA, Class: dnsmessage.ClassINET},
 133  			{Name: name("compressed.example."), Type: dnsmessage.TypeA, Class: dnsmessage.ClassINET},
 134  		},
 135  	}
 136  	cbuf, cerr := twoNames.Pack()
 137  	if cerr != nil {
 138  		fail("compressed Pack")
 139  	}
 140  	var cout dnsmessage.Message
 141  	if uerr := cout.Unpack(cbuf); uerr != nil {
 142  		fail("compressed Unpack")
 143  	}
 144  	if len(cout.Questions) != 2 {
 145  		fail("compressed question count")
 146  	}
 147  	if cout.Questions[0].Name.String() != "compressed.example." || cout.Questions[1].Name.String() != "compressed.example." {
 148  		fail("compressed names")
 149  	}
 150  
 151  	// A question name with no terminating zero: header + QDCOUNT=1 + a length
 152  	// byte and label bytes that run out. Unpack must fail cleanly.
 153  	truncated := []byte{
 154  		0, 0, // ID
 155  		0, 0, // flags
 156  		0, 1, // QDCOUNT
 157  		0, 0, 0, 0, 0, 0, // NSCOUNT, ARCOUNT, and the last two of the header
 158  		5, 'e', 'x', 'a', 'm', 'p',
 159  	}
 160  	var tmsg dnsmessage.Message
 161  	if e4 := tmsg.Unpack(truncated); e4 == nil {
 162  		fail("truncated name must not unpack")
 163  	}
 164  
 165  	// The Builder path: NewBuilder reserves the header slot, Finish fills it in,
 166  	// and each resource's RDLENGTH is patched after its body is packed. Both
 167  	// patches write into a slice that a Go implementation would alias; the port
 168  	// must keep them landing in the message, because a zero header or a zero
 169  	// RDLENGTH is a query no server answers and an answer no resolver parses.
 170  	bb := dnsmessage.NewBuilder(nil, dnsmessage.Header{ID: 0x4321, Response: true})
 171  	if serr := bb.StartQuestions(); serr != nil {
 172  		fail("builder StartQuestions")
 173  	}
 174  	if qerr := bb.Question(dnsmessage.Question{Name: name("build.example."), Type: dnsmessage.TypeA, Class: dnsmessage.ClassINET}); qerr != nil {
 175  		fail("builder Question")
 176  	}
 177  	if aerr := bb.StartAnswers(); aerr != nil {
 178  		fail("builder StartAnswers")
 179  	}
 180  	rh := dnsmessage.ResourceHeader{Name: name("build.example."), Type: dnsmessage.TypeA, Class: dnsmessage.ClassINET, TTL: 60}
 181  	var ares dnsmessage.AResource
 182  	ares.A = []byte{192, 0, 2, 1}
 183  	if rerr := bb.AResource(rh, ares); rerr != nil {
 184  		fail("builder AResource")
 185  	}
 186  	built, berr := bb.Finish()
 187  	if berr != nil {
 188  		fail("builder Finish")
 189  	}
 190  	var bout dnsmessage.Message
 191  	if uerr := bout.Unpack(built); uerr != nil {
 192  		fail("builder Unpack")
 193  	}
 194  	if bout.Header.ID != 0x4321 {
 195  		fail("builder header id")
 196  	}
 197  	if !bout.Header.Response {
 198  		fail("builder header Response flag")
 199  	}
 200  	if len(bout.Questions) != 1 || len(bout.Answers) != 1 {
 201  		fail("builder section counts")
 202  	}
 203  	if bout.Answers[0].Header.Name.String() != "build.example." {
 204  		fail("builder answer name")
 205  	}
 206  	if ip, ok := bout.Answers[0].Body.(*dnsmessage.AResource); ok {
 207  		if ip.A[0] != 192 || ip.A[3] != 1 {
 208  			fail("builder answer address")
 209  		}
 210  	} else {
 211  		fail("builder answer body type")
 212  	}
 213  
 214  	fmt.Printf("ok   dns name round trip: plain, root, 63-byte label, 254-byte name\n")
 215  	fmt.Printf("ok   dns name rejection: 64-byte label, non-canonical, short storage, truncation\n")
 216  	fmt.Printf("ok   dns compression pointer: second question round trips\n")
 217  	fmt.Printf("ok   dns builder: header id, flags, counts, RDLENGTH and A body\n")
 218  	fmt.Printf("dns_name_roundtrip ok\n")
 219  }
 220