message_test.go 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package dnsmessage
  5. import (
  6. "bytes"
  7. "fmt"
  8. "reflect"
  9. "strings"
  10. "testing"
  11. )
  12. func TestPrintPaddedUint8(t *testing.T) {
  13. tests := []struct {
  14. num uint8
  15. want string
  16. }{
  17. {0, "000"},
  18. {1, "001"},
  19. {9, "009"},
  20. {10, "010"},
  21. {99, "099"},
  22. {100, "100"},
  23. {124, "124"},
  24. {104, "104"},
  25. {120, "120"},
  26. {255, "255"},
  27. }
  28. for _, test := range tests {
  29. if got := printPaddedUint8(test.num); got != test.want {
  30. t.Errorf("got printPaddedUint8(%d) = %s, want = %s", test.num, got, test.want)
  31. }
  32. }
  33. }
  34. func TestPrintUint8Bytes(t *testing.T) {
  35. tests := []uint8{
  36. 0,
  37. 1,
  38. 9,
  39. 10,
  40. 99,
  41. 100,
  42. 124,
  43. 104,
  44. 120,
  45. 255,
  46. }
  47. for _, test := range tests {
  48. if got, want := string(printUint8Bytes(nil, test)), fmt.Sprint(test); got != want {
  49. t.Errorf("got printUint8Bytes(%d) = %s, want = %s", test, got, want)
  50. }
  51. }
  52. }
  53. func TestPrintUint16(t *testing.T) {
  54. tests := []uint16{
  55. 65535,
  56. 0,
  57. 1,
  58. 10,
  59. 100,
  60. 1000,
  61. 10000,
  62. 324,
  63. 304,
  64. 320,
  65. }
  66. for _, test := range tests {
  67. if got, want := printUint16(test), fmt.Sprint(test); got != want {
  68. t.Errorf("got printUint16(%d) = %s, want = %s", test, got, want)
  69. }
  70. }
  71. }
  72. func TestPrintUint32(t *testing.T) {
  73. tests := []uint32{
  74. 4294967295,
  75. 65535,
  76. 0,
  77. 1,
  78. 10,
  79. 100,
  80. 1000,
  81. 10000,
  82. 100000,
  83. 1000000,
  84. 10000000,
  85. 100000000,
  86. 1000000000,
  87. 324,
  88. 304,
  89. 320,
  90. }
  91. for _, test := range tests {
  92. if got, want := printUint32(test), fmt.Sprint(test); got != want {
  93. t.Errorf("got printUint32(%d) = %s, want = %s", test, got, want)
  94. }
  95. }
  96. }
  97. func mustEDNS0ResourceHeader(l int, extrc RCode, do bool) ResourceHeader {
  98. h := ResourceHeader{Class: ClassINET}
  99. if err := h.SetEDNS0(l, extrc, do); err != nil {
  100. panic(err)
  101. }
  102. return h
  103. }
  104. func (m *Message) String() string {
  105. s := fmt.Sprintf("Message: %#v\n", &m.Header)
  106. if len(m.Questions) > 0 {
  107. s += "-- Questions\n"
  108. for _, q := range m.Questions {
  109. s += fmt.Sprintf("%#v\n", q)
  110. }
  111. }
  112. if len(m.Answers) > 0 {
  113. s += "-- Answers\n"
  114. for _, a := range m.Answers {
  115. s += fmt.Sprintf("%#v\n", a)
  116. }
  117. }
  118. if len(m.Authorities) > 0 {
  119. s += "-- Authorities\n"
  120. for _, ns := range m.Authorities {
  121. s += fmt.Sprintf("%#v\n", ns)
  122. }
  123. }
  124. if len(m.Additionals) > 0 {
  125. s += "-- Additionals\n"
  126. for _, e := range m.Additionals {
  127. s += fmt.Sprintf("%#v\n", e)
  128. }
  129. }
  130. return s
  131. }
  132. func TestNameString(t *testing.T) {
  133. want := "foo"
  134. name := MustNewName(want)
  135. if got := fmt.Sprint(name); got != want {
  136. t.Errorf("got fmt.Sprint(%#v) = %s, want = %s", name, got, want)
  137. }
  138. }
  139. func TestQuestionPackUnpack(t *testing.T) {
  140. want := Question{
  141. Name: MustNewName("."),
  142. Type: TypeA,
  143. Class: ClassINET,
  144. }
  145. buf, err := want.pack(make([]byte, 1, 50), map[string]int{}, 1)
  146. if err != nil {
  147. t.Fatal("Question.pack() =", err)
  148. }
  149. var p Parser
  150. p.msg = buf
  151. p.header.questions = 1
  152. p.section = sectionQuestions
  153. p.off = 1
  154. got, err := p.Question()
  155. if err != nil {
  156. t.Fatalf("Parser{%q}.Question() = %v", string(buf[1:]), err)
  157. }
  158. if p.off != len(buf) {
  159. t.Errorf("unpacked different amount than packed: got = %d, want = %d", p.off, len(buf))
  160. }
  161. if !reflect.DeepEqual(got, want) {
  162. t.Errorf("got from Parser.Question() = %+v, want = %+v", got, want)
  163. }
  164. }
  165. func TestName(t *testing.T) {
  166. tests := []string{
  167. "",
  168. ".",
  169. "google..com",
  170. "google.com",
  171. "google..com.",
  172. "google.com.",
  173. ".google.com.",
  174. "www..google.com.",
  175. "www.google.com.",
  176. }
  177. for _, test := range tests {
  178. n, err := NewName(test)
  179. if err != nil {
  180. t.Errorf("NewName(%q) = %v", test, err)
  181. continue
  182. }
  183. if ns := n.String(); ns != test {
  184. t.Errorf("got %#v.String() = %q, want = %q", n, ns, test)
  185. continue
  186. }
  187. }
  188. }
  189. func TestNamePackUnpack(t *testing.T) {
  190. tests := []struct {
  191. in string
  192. want string
  193. err error
  194. }{
  195. {"", "", errNonCanonicalName},
  196. {".", ".", nil},
  197. {"google..com", "", errNonCanonicalName},
  198. {"google.com", "", errNonCanonicalName},
  199. {"google..com.", "", errZeroSegLen},
  200. {"google.com.", "google.com.", nil},
  201. {".google.com.", "", errZeroSegLen},
  202. {"www..google.com.", "", errZeroSegLen},
  203. {"www.google.com.", "www.google.com.", nil},
  204. }
  205. for _, test := range tests {
  206. in := MustNewName(test.in)
  207. want := MustNewName(test.want)
  208. buf, err := in.pack(make([]byte, 0, 30), map[string]int{}, 0)
  209. if err != test.err {
  210. t.Errorf("got %q.pack() = %v, want = %v", test.in, err, test.err)
  211. continue
  212. }
  213. if test.err != nil {
  214. continue
  215. }
  216. var got Name
  217. n, err := got.unpack(buf, 0)
  218. if err != nil {
  219. t.Errorf("%q.unpack() = %v", test.in, err)
  220. continue
  221. }
  222. if n != len(buf) {
  223. t.Errorf(
  224. "unpacked different amount than packed for %q: got = %d, want = %d",
  225. test.in,
  226. n,
  227. len(buf),
  228. )
  229. }
  230. if got != want {
  231. t.Errorf("unpacking packing of %q: got = %#v, want = %#v", test.in, got, want)
  232. }
  233. }
  234. }
  235. func TestIncompressibleName(t *testing.T) {
  236. name := MustNewName("example.com.")
  237. compression := map[string]int{}
  238. buf, err := name.pack(make([]byte, 0, 100), compression, 0)
  239. if err != nil {
  240. t.Fatal("first Name.pack() =", err)
  241. }
  242. buf, err = name.pack(buf, compression, 0)
  243. if err != nil {
  244. t.Fatal("second Name.pack() =", err)
  245. }
  246. var n1 Name
  247. off, err := n1.unpackCompressed(buf, 0, false /* allowCompression */)
  248. if err != nil {
  249. t.Fatal("unpacking incompressible name without pointers failed:", err)
  250. }
  251. var n2 Name
  252. if _, err := n2.unpackCompressed(buf, off, false /* allowCompression */); err != errCompressedSRV {
  253. t.Errorf("unpacking compressed incompressible name with pointers: got %v, want = %v", err, errCompressedSRV)
  254. }
  255. }
  256. func checkErrorPrefix(err error, prefix string) bool {
  257. e, ok := err.(*nestedError)
  258. return ok && e.s == prefix
  259. }
  260. func TestHeaderUnpackError(t *testing.T) {
  261. wants := []string{
  262. "id",
  263. "bits",
  264. "questions",
  265. "answers",
  266. "authorities",
  267. "additionals",
  268. }
  269. var buf []byte
  270. var h header
  271. for _, want := range wants {
  272. n, err := h.unpack(buf, 0)
  273. if n != 0 || !checkErrorPrefix(err, want) {
  274. t.Errorf("got header.unpack([%d]byte, 0) = %d, %v, want = 0, %s", len(buf), n, err, want)
  275. }
  276. buf = append(buf, 0, 0)
  277. }
  278. }
  279. func TestParserStart(t *testing.T) {
  280. const want = "unpacking header"
  281. var p Parser
  282. for i := 0; i <= 1; i++ {
  283. _, err := p.Start([]byte{})
  284. if !checkErrorPrefix(err, want) {
  285. t.Errorf("got Parser.Start(nil) = _, %v, want = _, %s", err, want)
  286. }
  287. }
  288. }
  289. func TestResourceNotStarted(t *testing.T) {
  290. tests := []struct {
  291. name string
  292. fn func(*Parser) error
  293. }{
  294. {"CNAMEResource", func(p *Parser) error { _, err := p.CNAMEResource(); return err }},
  295. {"MXResource", func(p *Parser) error { _, err := p.MXResource(); return err }},
  296. {"NSResource", func(p *Parser) error { _, err := p.NSResource(); return err }},
  297. {"PTRResource", func(p *Parser) error { _, err := p.PTRResource(); return err }},
  298. {"SOAResource", func(p *Parser) error { _, err := p.SOAResource(); return err }},
  299. {"TXTResource", func(p *Parser) error { _, err := p.TXTResource(); return err }},
  300. {"SRVResource", func(p *Parser) error { _, err := p.SRVResource(); return err }},
  301. {"AResource", func(p *Parser) error { _, err := p.AResource(); return err }},
  302. {"AAAAResource", func(p *Parser) error { _, err := p.AAAAResource(); return err }},
  303. }
  304. for _, test := range tests {
  305. if err := test.fn(&Parser{}); err != ErrNotStarted {
  306. t.Errorf("got Parser.%s() = _ , %v, want = _, %v", test.name, err, ErrNotStarted)
  307. }
  308. }
  309. }
  310. func TestDNSPackUnpack(t *testing.T) {
  311. wants := []Message{
  312. {
  313. Questions: []Question{
  314. {
  315. Name: MustNewName("."),
  316. Type: TypeAAAA,
  317. Class: ClassINET,
  318. },
  319. },
  320. Answers: []Resource{},
  321. Authorities: []Resource{},
  322. Additionals: []Resource{},
  323. },
  324. largeTestMsg(),
  325. }
  326. for i, want := range wants {
  327. b, err := want.Pack()
  328. if err != nil {
  329. t.Fatalf("%d: Message.Pack() = %v", i, err)
  330. }
  331. var got Message
  332. err = got.Unpack(b)
  333. if err != nil {
  334. t.Fatalf("%d: Message.Unapck() = %v", i, err)
  335. }
  336. if !reflect.DeepEqual(got, want) {
  337. t.Errorf("%d: Message.Pack/Unpack() roundtrip: got = %+v, want = %+v", i, &got, &want)
  338. }
  339. }
  340. }
  341. func TestDNSAppendPackUnpack(t *testing.T) {
  342. wants := []Message{
  343. {
  344. Questions: []Question{
  345. {
  346. Name: MustNewName("."),
  347. Type: TypeAAAA,
  348. Class: ClassINET,
  349. },
  350. },
  351. Answers: []Resource{},
  352. Authorities: []Resource{},
  353. Additionals: []Resource{},
  354. },
  355. largeTestMsg(),
  356. }
  357. for i, want := range wants {
  358. b := make([]byte, 2, 514)
  359. b, err := want.AppendPack(b)
  360. if err != nil {
  361. t.Fatalf("%d: Message.AppendPack() = %v", i, err)
  362. }
  363. b = b[2:]
  364. var got Message
  365. err = got.Unpack(b)
  366. if err != nil {
  367. t.Fatalf("%d: Message.Unapck() = %v", i, err)
  368. }
  369. if !reflect.DeepEqual(got, want) {
  370. t.Errorf("%d: Message.AppendPack/Unpack() roundtrip: got = %+v, want = %+v", i, &got, &want)
  371. }
  372. }
  373. }
  374. func TestSkipAll(t *testing.T) {
  375. msg := largeTestMsg()
  376. buf, err := msg.Pack()
  377. if err != nil {
  378. t.Fatal("Message.Pack() =", err)
  379. }
  380. var p Parser
  381. if _, err := p.Start(buf); err != nil {
  382. t.Fatal("Parser.Start(non-nil) =", err)
  383. }
  384. tests := []struct {
  385. name string
  386. f func() error
  387. }{
  388. {"SkipAllQuestions", p.SkipAllQuestions},
  389. {"SkipAllAnswers", p.SkipAllAnswers},
  390. {"SkipAllAuthorities", p.SkipAllAuthorities},
  391. {"SkipAllAdditionals", p.SkipAllAdditionals},
  392. }
  393. for _, test := range tests {
  394. for i := 1; i <= 3; i++ {
  395. if err := test.f(); err != nil {
  396. t.Errorf("%d: Parser.%s() = %v", i, test.name, err)
  397. }
  398. }
  399. }
  400. }
  401. func TestSkipEach(t *testing.T) {
  402. msg := smallTestMsg()
  403. buf, err := msg.Pack()
  404. if err != nil {
  405. t.Fatal("Message.Pack() =", err)
  406. }
  407. var p Parser
  408. if _, err := p.Start(buf); err != nil {
  409. t.Fatal("Parser.Start(non-nil) =", err)
  410. }
  411. tests := []struct {
  412. name string
  413. f func() error
  414. }{
  415. {"SkipQuestion", p.SkipQuestion},
  416. {"SkipAnswer", p.SkipAnswer},
  417. {"SkipAuthority", p.SkipAuthority},
  418. {"SkipAdditional", p.SkipAdditional},
  419. }
  420. for _, test := range tests {
  421. if err := test.f(); err != nil {
  422. t.Errorf("first Parser.%s() = %v, want = nil", test.name, err)
  423. }
  424. if err := test.f(); err != ErrSectionDone {
  425. t.Errorf("second Parser.%s() = %v, want = %v", test.name, err, ErrSectionDone)
  426. }
  427. }
  428. }
  429. func TestSkipAfterRead(t *testing.T) {
  430. msg := smallTestMsg()
  431. buf, err := msg.Pack()
  432. if err != nil {
  433. t.Fatal("Message.Pack() =", err)
  434. }
  435. var p Parser
  436. if _, err := p.Start(buf); err != nil {
  437. t.Fatal("Parser.Srart(non-nil) =", err)
  438. }
  439. tests := []struct {
  440. name string
  441. skip func() error
  442. read func() error
  443. }{
  444. {"Question", p.SkipQuestion, func() error { _, err := p.Question(); return err }},
  445. {"Answer", p.SkipAnswer, func() error { _, err := p.Answer(); return err }},
  446. {"Authority", p.SkipAuthority, func() error { _, err := p.Authority(); return err }},
  447. {"Additional", p.SkipAdditional, func() error { _, err := p.Additional(); return err }},
  448. }
  449. for _, test := range tests {
  450. if err := test.read(); err != nil {
  451. t.Errorf("got Parser.%s() = _, %v, want = _, nil", test.name, err)
  452. }
  453. if err := test.skip(); err != ErrSectionDone {
  454. t.Errorf("got Parser.Skip%s() = %v, want = %v", test.name, err, ErrSectionDone)
  455. }
  456. }
  457. }
  458. func TestSkipNotStarted(t *testing.T) {
  459. var p Parser
  460. tests := []struct {
  461. name string
  462. f func() error
  463. }{
  464. {"SkipAllQuestions", p.SkipAllQuestions},
  465. {"SkipAllAnswers", p.SkipAllAnswers},
  466. {"SkipAllAuthorities", p.SkipAllAuthorities},
  467. {"SkipAllAdditionals", p.SkipAllAdditionals},
  468. }
  469. for _, test := range tests {
  470. if err := test.f(); err != ErrNotStarted {
  471. t.Errorf("got Parser.%s() = %v, want = %v", test.name, err, ErrNotStarted)
  472. }
  473. }
  474. }
  475. func TestTooManyRecords(t *testing.T) {
  476. const recs = int(^uint16(0)) + 1
  477. tests := []struct {
  478. name string
  479. msg Message
  480. want error
  481. }{
  482. {
  483. "Questions",
  484. Message{
  485. Questions: make([]Question, recs),
  486. },
  487. errTooManyQuestions,
  488. },
  489. {
  490. "Answers",
  491. Message{
  492. Answers: make([]Resource, recs),
  493. },
  494. errTooManyAnswers,
  495. },
  496. {
  497. "Authorities",
  498. Message{
  499. Authorities: make([]Resource, recs),
  500. },
  501. errTooManyAuthorities,
  502. },
  503. {
  504. "Additionals",
  505. Message{
  506. Additionals: make([]Resource, recs),
  507. },
  508. errTooManyAdditionals,
  509. },
  510. }
  511. for _, test := range tests {
  512. if _, got := test.msg.Pack(); got != test.want {
  513. t.Errorf("got Message.Pack() for %d %s = %v, want = %v", recs, test.name, got, test.want)
  514. }
  515. }
  516. }
  517. func TestVeryLongTxt(t *testing.T) {
  518. want := Resource{
  519. ResourceHeader{
  520. Name: MustNewName("foo.bar.example.com."),
  521. Type: TypeTXT,
  522. Class: ClassINET,
  523. },
  524. &TXTResource{[]string{
  525. "",
  526. "",
  527. "foo bar",
  528. "",
  529. "www.example.com",
  530. "www.example.com.",
  531. strings.Repeat(".", 255),
  532. }},
  533. }
  534. buf, err := want.pack(make([]byte, 0, 8000), map[string]int{}, 0)
  535. if err != nil {
  536. t.Fatal("Resource.pack() =", err)
  537. }
  538. var got Resource
  539. off, err := got.Header.unpack(buf, 0)
  540. if err != nil {
  541. t.Fatal("ResourceHeader.unpack() =", err)
  542. }
  543. body, n, err := unpackResourceBody(buf, off, got.Header)
  544. if err != nil {
  545. t.Fatal("unpackResourceBody() =", err)
  546. }
  547. got.Body = body
  548. if n != len(buf) {
  549. t.Errorf("unpacked different amount than packed: got = %d, want = %d", n, len(buf))
  550. }
  551. if !reflect.DeepEqual(got, want) {
  552. t.Errorf("Resource.pack/unpack() roundtrip: got = %#v, want = %#v", got, want)
  553. }
  554. }
  555. func TestTooLongTxt(t *testing.T) {
  556. rb := TXTResource{[]string{strings.Repeat(".", 256)}}
  557. if _, err := rb.pack(make([]byte, 0, 8000), map[string]int{}, 0); err != errStringTooLong {
  558. t.Errorf("packing TXTResource with 256 character string: got err = %v, want = %v", err, errStringTooLong)
  559. }
  560. }
  561. func TestStartAppends(t *testing.T) {
  562. buf := make([]byte, 2, 514)
  563. wantBuf := []byte{4, 44}
  564. copy(buf, wantBuf)
  565. b := NewBuilder(buf, Header{})
  566. b.EnableCompression()
  567. buf, err := b.Finish()
  568. if err != nil {
  569. t.Fatal("Builder.Finish() =", err)
  570. }
  571. if got, want := len(buf), headerLen+2; got != want {
  572. t.Errorf("got len(buf) = %d, want = %d", got, want)
  573. }
  574. if string(buf[:2]) != string(wantBuf) {
  575. t.Errorf("original data not preserved, got = %#v, want = %#v", buf[:2], wantBuf)
  576. }
  577. }
  578. func TestStartError(t *testing.T) {
  579. tests := []struct {
  580. name string
  581. fn func(*Builder) error
  582. }{
  583. {"Questions", func(b *Builder) error { return b.StartQuestions() }},
  584. {"Answers", func(b *Builder) error { return b.StartAnswers() }},
  585. {"Authorities", func(b *Builder) error { return b.StartAuthorities() }},
  586. {"Additionals", func(b *Builder) error { return b.StartAdditionals() }},
  587. }
  588. envs := []struct {
  589. name string
  590. fn func() *Builder
  591. want error
  592. }{
  593. {"sectionNotStarted", func() *Builder { return &Builder{section: sectionNotStarted} }, ErrNotStarted},
  594. {"sectionDone", func() *Builder { return &Builder{section: sectionDone} }, ErrSectionDone},
  595. }
  596. for _, env := range envs {
  597. for _, test := range tests {
  598. if got := test.fn(env.fn()); got != env.want {
  599. t.Errorf("got Builder{%s}.Start%s() = %v, want = %v", env.name, test.name, got, env.want)
  600. }
  601. }
  602. }
  603. }
  604. func TestBuilderResourceError(t *testing.T) {
  605. tests := []struct {
  606. name string
  607. fn func(*Builder) error
  608. }{
  609. {"CNAMEResource", func(b *Builder) error { return b.CNAMEResource(ResourceHeader{}, CNAMEResource{}) }},
  610. {"MXResource", func(b *Builder) error { return b.MXResource(ResourceHeader{}, MXResource{}) }},
  611. {"NSResource", func(b *Builder) error { return b.NSResource(ResourceHeader{}, NSResource{}) }},
  612. {"PTRResource", func(b *Builder) error { return b.PTRResource(ResourceHeader{}, PTRResource{}) }},
  613. {"SOAResource", func(b *Builder) error { return b.SOAResource(ResourceHeader{}, SOAResource{}) }},
  614. {"TXTResource", func(b *Builder) error { return b.TXTResource(ResourceHeader{}, TXTResource{}) }},
  615. {"SRVResource", func(b *Builder) error { return b.SRVResource(ResourceHeader{}, SRVResource{}) }},
  616. {"AResource", func(b *Builder) error { return b.AResource(ResourceHeader{}, AResource{}) }},
  617. {"AAAAResource", func(b *Builder) error { return b.AAAAResource(ResourceHeader{}, AAAAResource{}) }},
  618. {"OPTResource", func(b *Builder) error { return b.OPTResource(ResourceHeader{}, OPTResource{}) }},
  619. }
  620. envs := []struct {
  621. name string
  622. fn func() *Builder
  623. want error
  624. }{
  625. {"sectionNotStarted", func() *Builder { return &Builder{section: sectionNotStarted} }, ErrNotStarted},
  626. {"sectionHeader", func() *Builder { return &Builder{section: sectionHeader} }, ErrNotStarted},
  627. {"sectionQuestions", func() *Builder { return &Builder{section: sectionQuestions} }, ErrNotStarted},
  628. {"sectionDone", func() *Builder { return &Builder{section: sectionDone} }, ErrSectionDone},
  629. }
  630. for _, env := range envs {
  631. for _, test := range tests {
  632. if got := test.fn(env.fn()); got != env.want {
  633. t.Errorf("got Builder{%s}.%s() = %v, want = %v", env.name, test.name, got, env.want)
  634. }
  635. }
  636. }
  637. }
  638. func TestFinishError(t *testing.T) {
  639. var b Builder
  640. want := ErrNotStarted
  641. if _, got := b.Finish(); got != want {
  642. t.Errorf("got Builder.Finish() = %v, want = %v", got, want)
  643. }
  644. }
  645. func TestBuilder(t *testing.T) {
  646. msg := largeTestMsg()
  647. want, err := msg.Pack()
  648. if err != nil {
  649. t.Fatal("Message.Pack() =", err)
  650. }
  651. b := NewBuilder(nil, msg.Header)
  652. b.EnableCompression()
  653. if err := b.StartQuestions(); err != nil {
  654. t.Fatal("Builder.StartQuestions() =", err)
  655. }
  656. for _, q := range msg.Questions {
  657. if err := b.Question(q); err != nil {
  658. t.Fatalf("Builder.Question(%#v) = %v", q, err)
  659. }
  660. }
  661. if err := b.StartAnswers(); err != nil {
  662. t.Fatal("Builder.StartAnswers() =", err)
  663. }
  664. for _, a := range msg.Answers {
  665. switch a.Header.Type {
  666. case TypeA:
  667. if err := b.AResource(a.Header, *a.Body.(*AResource)); err != nil {
  668. t.Fatalf("Builder.AResource(%#v) = %v", a, err)
  669. }
  670. case TypeNS:
  671. if err := b.NSResource(a.Header, *a.Body.(*NSResource)); err != nil {
  672. t.Fatalf("Builder.NSResource(%#v) = %v", a, err)
  673. }
  674. case TypeCNAME:
  675. if err := b.CNAMEResource(a.Header, *a.Body.(*CNAMEResource)); err != nil {
  676. t.Fatalf("Builder.CNAMEResource(%#v) = %v", a, err)
  677. }
  678. case TypeSOA:
  679. if err := b.SOAResource(a.Header, *a.Body.(*SOAResource)); err != nil {
  680. t.Fatalf("Builder.SOAResource(%#v) = %v", a, err)
  681. }
  682. case TypePTR:
  683. if err := b.PTRResource(a.Header, *a.Body.(*PTRResource)); err != nil {
  684. t.Fatalf("Builder.PTRResource(%#v) = %v", a, err)
  685. }
  686. case TypeMX:
  687. if err := b.MXResource(a.Header, *a.Body.(*MXResource)); err != nil {
  688. t.Fatalf("Builder.MXResource(%#v) = %v", a, err)
  689. }
  690. case TypeTXT:
  691. if err := b.TXTResource(a.Header, *a.Body.(*TXTResource)); err != nil {
  692. t.Fatalf("Builder.TXTResource(%#v) = %v", a, err)
  693. }
  694. case TypeAAAA:
  695. if err := b.AAAAResource(a.Header, *a.Body.(*AAAAResource)); err != nil {
  696. t.Fatalf("Builder.AAAAResource(%#v) = %v", a, err)
  697. }
  698. case TypeSRV:
  699. if err := b.SRVResource(a.Header, *a.Body.(*SRVResource)); err != nil {
  700. t.Fatalf("Builder.SRVResource(%#v) = %v", a, err)
  701. }
  702. }
  703. }
  704. if err := b.StartAuthorities(); err != nil {
  705. t.Fatal("Builder.StartAuthorities() =", err)
  706. }
  707. for _, a := range msg.Authorities {
  708. if err := b.NSResource(a.Header, *a.Body.(*NSResource)); err != nil {
  709. t.Fatalf("Builder.NSResource(%#v) = %v", a, err)
  710. }
  711. }
  712. if err := b.StartAdditionals(); err != nil {
  713. t.Fatal("Builder.StartAdditionals() =", err)
  714. }
  715. for _, a := range msg.Additionals {
  716. switch a.Body.(type) {
  717. case *TXTResource:
  718. if err := b.TXTResource(a.Header, *a.Body.(*TXTResource)); err != nil {
  719. t.Fatalf("Builder.TXTResource(%#v) = %v", a, err)
  720. }
  721. case *OPTResource:
  722. if err := b.OPTResource(a.Header, *a.Body.(*OPTResource)); err != nil {
  723. t.Fatalf("Builder.OPTResource(%#v) = %v", a, err)
  724. }
  725. }
  726. }
  727. got, err := b.Finish()
  728. if err != nil {
  729. t.Fatal("Builder.Finish() =", err)
  730. }
  731. if !bytes.Equal(got, want) {
  732. t.Fatalf("got from Builder.Finish() = %#v\nwant = %#v", got, want)
  733. }
  734. }
  735. func TestResourcePack(t *testing.T) {
  736. for _, tt := range []struct {
  737. m Message
  738. err error
  739. }{
  740. {
  741. Message{
  742. Questions: []Question{
  743. {
  744. Name: MustNewName("."),
  745. Type: TypeAAAA,
  746. Class: ClassINET,
  747. },
  748. },
  749. Answers: []Resource{{ResourceHeader{}, nil}},
  750. },
  751. &nestedError{"packing Answer", errNilResouceBody},
  752. },
  753. {
  754. Message{
  755. Questions: []Question{
  756. {
  757. Name: MustNewName("."),
  758. Type: TypeAAAA,
  759. Class: ClassINET,
  760. },
  761. },
  762. Authorities: []Resource{{ResourceHeader{}, (*NSResource)(nil)}},
  763. },
  764. &nestedError{"packing Authority",
  765. &nestedError{"ResourceHeader",
  766. &nestedError{"Name", errNonCanonicalName},
  767. },
  768. },
  769. },
  770. {
  771. Message{
  772. Questions: []Question{
  773. {
  774. Name: MustNewName("."),
  775. Type: TypeA,
  776. Class: ClassINET,
  777. },
  778. },
  779. Additionals: []Resource{{ResourceHeader{}, nil}},
  780. },
  781. &nestedError{"packing Additional", errNilResouceBody},
  782. },
  783. } {
  784. _, err := tt.m.Pack()
  785. if !reflect.DeepEqual(err, tt.err) {
  786. t.Errorf("got Message{%v}.Pack() = %v, want %v", tt.m, err, tt.err)
  787. }
  788. }
  789. }
  790. func TestOptionPackUnpack(t *testing.T) {
  791. for _, tt := range []struct {
  792. name string
  793. w []byte // wire format of m.Additionals
  794. m Message
  795. dnssecOK bool
  796. extRCode RCode
  797. }{
  798. {
  799. name: "without EDNS(0) options",
  800. w: []byte{
  801. 0x00, 0x00, 0x29, 0x10, 0x00, 0xfe, 0x00, 0x80,
  802. 0x00, 0x00, 0x00,
  803. },
  804. m: Message{
  805. Header: Header{RCode: RCodeFormatError},
  806. Questions: []Question{
  807. {
  808. Name: MustNewName("."),
  809. Type: TypeA,
  810. Class: ClassINET,
  811. },
  812. },
  813. Additionals: []Resource{
  814. {
  815. mustEDNS0ResourceHeader(4096, 0xfe0|RCodeFormatError, true),
  816. &OPTResource{},
  817. },
  818. },
  819. },
  820. dnssecOK: true,
  821. extRCode: 0xfe0 | RCodeFormatError,
  822. },
  823. {
  824. name: "with EDNS(0) options",
  825. w: []byte{
  826. 0x00, 0x00, 0x29, 0x10, 0x00, 0xff, 0x00, 0x00,
  827. 0x00, 0x00, 0x0c, 0x00, 0x0c, 0x00, 0x02, 0x00,
  828. 0x00, 0x00, 0x0b, 0x00, 0x02, 0x12, 0x34,
  829. },
  830. m: Message{
  831. Header: Header{RCode: RCodeServerFailure},
  832. Questions: []Question{
  833. {
  834. Name: MustNewName("."),
  835. Type: TypeAAAA,
  836. Class: ClassINET,
  837. },
  838. },
  839. Additionals: []Resource{
  840. {
  841. mustEDNS0ResourceHeader(4096, 0xff0|RCodeServerFailure, false),
  842. &OPTResource{
  843. Options: []Option{
  844. {
  845. Code: 12, // see RFC 7828
  846. Data: []byte{0x00, 0x00},
  847. },
  848. {
  849. Code: 11, // see RFC 7830
  850. Data: []byte{0x12, 0x34},
  851. },
  852. },
  853. },
  854. },
  855. },
  856. },
  857. dnssecOK: false,
  858. extRCode: 0xff0 | RCodeServerFailure,
  859. },
  860. {
  861. // Containing multiple OPT resources in a
  862. // message is invalid, but it's necessary for
  863. // protocol conformance testing.
  864. name: "with multiple OPT resources",
  865. w: []byte{
  866. 0x00, 0x00, 0x29, 0x10, 0x00, 0xff, 0x00, 0x00,
  867. 0x00, 0x00, 0x06, 0x00, 0x0b, 0x00, 0x02, 0x12,
  868. 0x34, 0x00, 0x00, 0x29, 0x10, 0x00, 0xff, 0x00,
  869. 0x00, 0x00, 0x00, 0x06, 0x00, 0x0c, 0x00, 0x02,
  870. 0x00, 0x00,
  871. },
  872. m: Message{
  873. Header: Header{RCode: RCodeNameError},
  874. Questions: []Question{
  875. {
  876. Name: MustNewName("."),
  877. Type: TypeAAAA,
  878. Class: ClassINET,
  879. },
  880. },
  881. Additionals: []Resource{
  882. {
  883. mustEDNS0ResourceHeader(4096, 0xff0|RCodeNameError, false),
  884. &OPTResource{
  885. Options: []Option{
  886. {
  887. Code: 11, // see RFC 7830
  888. Data: []byte{0x12, 0x34},
  889. },
  890. },
  891. },
  892. },
  893. {
  894. mustEDNS0ResourceHeader(4096, 0xff0|RCodeNameError, false),
  895. &OPTResource{
  896. Options: []Option{
  897. {
  898. Code: 12, // see RFC 7828
  899. Data: []byte{0x00, 0x00},
  900. },
  901. },
  902. },
  903. },
  904. },
  905. },
  906. },
  907. } {
  908. w, err := tt.m.Pack()
  909. if err != nil {
  910. t.Errorf("Message.Pack() for %s = %v", tt.name, err)
  911. continue
  912. }
  913. if !bytes.Equal(w[len(w)-len(tt.w):], tt.w) {
  914. t.Errorf("got Message.Pack() for %s = %#v, want %#v", tt.name, w[len(w)-len(tt.w):], tt.w)
  915. continue
  916. }
  917. var m Message
  918. if err := m.Unpack(w); err != nil {
  919. t.Errorf("Message.Unpack() for %s = %v", tt.name, err)
  920. continue
  921. }
  922. if !reflect.DeepEqual(m.Additionals, tt.m.Additionals) {
  923. t.Errorf("got Message.Pack/Unpack() roundtrip for %s = %+v, want %+v", tt.name, m, tt.m)
  924. continue
  925. }
  926. }
  927. }
  928. // TestGoString tests that Message.GoString produces Go code that compiles to
  929. // reproduce the Message.
  930. //
  931. // This test was produced as follows:
  932. // 1. Run (*Message).GoString on largeTestMsg().
  933. // 2. Remove "dnsmessage." from the output.
  934. // 3. Paste the result in the test to store it in msg.
  935. // 4. Also put the original output in the test to store in want.
  936. func TestGoString(t *testing.T) {
  937. msg := Message{Header: Header{ID: 0, Response: true, OpCode: 0, Authoritative: true, Truncated: false, RecursionDesired: false, RecursionAvailable: false, RCode: RCodeSuccess}, Questions: []Question{{Name: MustNewName("foo.bar.example.com."), Type: TypeA, Class: ClassINET}}, Answers: []Resource{{Header: ResourceHeader{Name: MustNewName("foo.bar.example.com."), Type: TypeA, Class: ClassINET, TTL: 0, Length: 0}, Body: &AResource{A: [4]byte{127, 0, 0, 1}}}, {Header: ResourceHeader{Name: MustNewName("foo.bar.example.com."), Type: TypeA, Class: ClassINET, TTL: 0, Length: 0}, Body: &AResource{A: [4]byte{127, 0, 0, 2}}}, {Header: ResourceHeader{Name: MustNewName("foo.bar.example.com."), Type: TypeAAAA, Class: ClassINET, TTL: 0, Length: 0}, Body: &AAAAResource{AAAA: [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}}}, {Header: ResourceHeader{Name: MustNewName("foo.bar.example.com."), Type: TypeCNAME, Class: ClassINET, TTL: 0, Length: 0}, Body: &CNAMEResource{CNAME: MustNewName("alias.example.com.")}}, {Header: ResourceHeader{Name: MustNewName("foo.bar.example.com."), Type: TypeSOA, Class: ClassINET, TTL: 0, Length: 0}, Body: &SOAResource{NS: MustNewName("ns1.example.com."), MBox: MustNewName("mb.example.com."), Serial: 1, Refresh: 2, Retry: 3, Expire: 4, MinTTL: 5}}, {Header: ResourceHeader{Name: MustNewName("foo.bar.example.com."), Type: TypePTR, Class: ClassINET, TTL: 0, Length: 0}, Body: &PTRResource{PTR: MustNewName("ptr.example.com.")}}, {Header: ResourceHeader{Name: MustNewName("foo.bar.example.com."), Type: TypeMX, Class: ClassINET, TTL: 0, Length: 0}, Body: &MXResource{Pref: 7, MX: MustNewName("mx.example.com.")}}, {Header: ResourceHeader{Name: MustNewName("foo.bar.example.com."), Type: TypeSRV, Class: ClassINET, TTL: 0, Length: 0}, Body: &SRVResource{Priority: 8, Weight: 9, Port: 11, Target: MustNewName("srv.example.com.")}}}, Authorities: []Resource{{Header: ResourceHeader{Name: MustNewName("foo.bar.example.com."), Type: TypeNS, Class: ClassINET, TTL: 0, Length: 0}, Body: &NSResource{NS: MustNewName("ns1.example.com.")}}, {Header: ResourceHeader{Name: MustNewName("foo.bar.example.com."), Type: TypeNS, Class: ClassINET, TTL: 0, Length: 0}, Body: &NSResource{NS: MustNewName("ns2.example.com.")}}}, Additionals: []Resource{{Header: ResourceHeader{Name: MustNewName("foo.bar.example.com."), Type: TypeTXT, Class: ClassINET, TTL: 0, Length: 0}, Body: &TXTResource{TXT: []string{"So Long\x2c and Thanks for All the Fish"}}}, {Header: ResourceHeader{Name: MustNewName("foo.bar.example.com."), Type: TypeTXT, Class: ClassINET, TTL: 0, Length: 0}, Body: &TXTResource{TXT: []string{"Hamster Huey and the Gooey Kablooie"}}}, {Header: ResourceHeader{Name: MustNewName("."), Type: TypeOPT, Class: 4096, TTL: 4261412864, Length: 0}, Body: &OPTResource{Options: []Option{{Code: 10, Data: []byte{1, 35, 69, 103, 137, 171, 205, 239}}}}}}}
  938. if !reflect.DeepEqual(msg, largeTestMsg()) {
  939. t.Error("Message.GoString lost information or largeTestMsg changed: msg != largeTestMsg()")
  940. }
  941. got := msg.GoString()
  942. want := `dnsmessage.Message{Header: dnsmessage.Header{ID: 0, Response: true, OpCode: 0, Authoritative: true, Truncated: false, RecursionDesired: false, RecursionAvailable: false, RCode: dnsmessage.RCodeSuccess}, Questions: []dnsmessage.Question{dnsmessage.Question{Name: dnsmessage.MustNewName("foo.bar.example.com."), Type: dnsmessage.TypeA, Class: dnsmessage.ClassINET}}, Answers: []dnsmessage.Resource{dnsmessage.Resource{Header: dnsmessage.ResourceHeader{Name: dnsmessage.MustNewName("foo.bar.example.com."), Type: dnsmessage.TypeA, Class: dnsmessage.ClassINET, TTL: 0, Length: 0}, Body: &dnsmessage.AResource{A: [4]byte{127, 0, 0, 1}}}, dnsmessage.Resource{Header: dnsmessage.ResourceHeader{Name: dnsmessage.MustNewName("foo.bar.example.com."), Type: dnsmessage.TypeA, Class: dnsmessage.ClassINET, TTL: 0, Length: 0}, Body: &dnsmessage.AResource{A: [4]byte{127, 0, 0, 2}}}, dnsmessage.Resource{Header: dnsmessage.ResourceHeader{Name: dnsmessage.MustNewName("foo.bar.example.com."), Type: dnsmessage.TypeAAAA, Class: dnsmessage.ClassINET, TTL: 0, Length: 0}, Body: &dnsmessage.AAAAResource{AAAA: [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}}}, dnsmessage.Resource{Header: dnsmessage.ResourceHeader{Name: dnsmessage.MustNewName("foo.bar.example.com."), Type: dnsmessage.TypeCNAME, Class: dnsmessage.ClassINET, TTL: 0, Length: 0}, Body: &dnsmessage.CNAMEResource{CNAME: dnsmessage.MustNewName("alias.example.com.")}}, dnsmessage.Resource{Header: dnsmessage.ResourceHeader{Name: dnsmessage.MustNewName("foo.bar.example.com."), Type: dnsmessage.TypeSOA, Class: dnsmessage.ClassINET, TTL: 0, Length: 0}, Body: &dnsmessage.SOAResource{NS: dnsmessage.MustNewName("ns1.example.com."), MBox: dnsmessage.MustNewName("mb.example.com."), Serial: 1, Refresh: 2, Retry: 3, Expire: 4, MinTTL: 5}}, dnsmessage.Resource{Header: dnsmessage.ResourceHeader{Name: dnsmessage.MustNewName("foo.bar.example.com."), Type: dnsmessage.TypePTR, Class: dnsmessage.ClassINET, TTL: 0, Length: 0}, Body: &dnsmessage.PTRResource{PTR: dnsmessage.MustNewName("ptr.example.com.")}}, dnsmessage.Resource{Header: dnsmessage.ResourceHeader{Name: dnsmessage.MustNewName("foo.bar.example.com."), Type: dnsmessage.TypeMX, Class: dnsmessage.ClassINET, TTL: 0, Length: 0}, Body: &dnsmessage.MXResource{Pref: 7, MX: dnsmessage.MustNewName("mx.example.com.")}}, dnsmessage.Resource{Header: dnsmessage.ResourceHeader{Name: dnsmessage.MustNewName("foo.bar.example.com."), Type: dnsmessage.TypeSRV, Class: dnsmessage.ClassINET, TTL: 0, Length: 0}, Body: &dnsmessage.SRVResource{Priority: 8, Weight: 9, Port: 11, Target: dnsmessage.MustNewName("srv.example.com.")}}}, Authorities: []dnsmessage.Resource{dnsmessage.Resource{Header: dnsmessage.ResourceHeader{Name: dnsmessage.MustNewName("foo.bar.example.com."), Type: dnsmessage.TypeNS, Class: dnsmessage.ClassINET, TTL: 0, Length: 0}, Body: &dnsmessage.NSResource{NS: dnsmessage.MustNewName("ns1.example.com.")}}, dnsmessage.Resource{Header: dnsmessage.ResourceHeader{Name: dnsmessage.MustNewName("foo.bar.example.com."), Type: dnsmessage.TypeNS, Class: dnsmessage.ClassINET, TTL: 0, Length: 0}, Body: &dnsmessage.NSResource{NS: dnsmessage.MustNewName("ns2.example.com.")}}}, Additionals: []dnsmessage.Resource{dnsmessage.Resource{Header: dnsmessage.ResourceHeader{Name: dnsmessage.MustNewName("foo.bar.example.com."), Type: dnsmessage.TypeTXT, Class: dnsmessage.ClassINET, TTL: 0, Length: 0}, Body: &dnsmessage.TXTResource{TXT: []string{"So Long\x2c and Thanks for All the Fish"}}}, dnsmessage.Resource{Header: dnsmessage.ResourceHeader{Name: dnsmessage.MustNewName("foo.bar.example.com."), Type: dnsmessage.TypeTXT, Class: dnsmessage.ClassINET, TTL: 0, Length: 0}, Body: &dnsmessage.TXTResource{TXT: []string{"Hamster Huey and the Gooey Kablooie"}}}, dnsmessage.Resource{Header: dnsmessage.ResourceHeader{Name: dnsmessage.MustNewName("."), Type: dnsmessage.TypeOPT, Class: 4096, TTL: 4261412864, Length: 0}, Body: &dnsmessage.OPTResource{Options: []dnsmessage.Option{dnsmessage.Option{Code: 10, Data: []byte{1, 35, 69, 103, 137, 171, 205, 239}}}}}}}`
  943. if got != want {
  944. t.Errorf("got msg1.GoString() = %s\nwant = %s", got, want)
  945. }
  946. }
  947. func benchmarkParsingSetup() ([]byte, error) {
  948. name := MustNewName("foo.bar.example.com.")
  949. msg := Message{
  950. Header: Header{Response: true, Authoritative: true},
  951. Questions: []Question{
  952. {
  953. Name: name,
  954. Type: TypeA,
  955. Class: ClassINET,
  956. },
  957. },
  958. Answers: []Resource{
  959. {
  960. ResourceHeader{
  961. Name: name,
  962. Class: ClassINET,
  963. },
  964. &AResource{[4]byte{}},
  965. },
  966. {
  967. ResourceHeader{
  968. Name: name,
  969. Class: ClassINET,
  970. },
  971. &AAAAResource{[16]byte{}},
  972. },
  973. {
  974. ResourceHeader{
  975. Name: name,
  976. Class: ClassINET,
  977. },
  978. &CNAMEResource{name},
  979. },
  980. {
  981. ResourceHeader{
  982. Name: name,
  983. Class: ClassINET,
  984. },
  985. &NSResource{name},
  986. },
  987. },
  988. }
  989. buf, err := msg.Pack()
  990. if err != nil {
  991. return nil, fmt.Errorf("Message.Pack() = %v", err)
  992. }
  993. return buf, nil
  994. }
  995. func benchmarkParsing(tb testing.TB, buf []byte) {
  996. var p Parser
  997. if _, err := p.Start(buf); err != nil {
  998. tb.Fatal("Parser.Start(non-nil) =", err)
  999. }
  1000. for {
  1001. _, err := p.Question()
  1002. if err == ErrSectionDone {
  1003. break
  1004. }
  1005. if err != nil {
  1006. tb.Fatal("Parser.Question() =", err)
  1007. }
  1008. }
  1009. for {
  1010. h, err := p.AnswerHeader()
  1011. if err == ErrSectionDone {
  1012. break
  1013. }
  1014. if err != nil {
  1015. tb.Fatal("Parser.AnswerHeader() =", err)
  1016. }
  1017. switch h.Type {
  1018. case TypeA:
  1019. if _, err := p.AResource(); err != nil {
  1020. tb.Fatal("Parser.AResource() =", err)
  1021. }
  1022. case TypeAAAA:
  1023. if _, err := p.AAAAResource(); err != nil {
  1024. tb.Fatal("Parser.AAAAResource() =", err)
  1025. }
  1026. case TypeCNAME:
  1027. if _, err := p.CNAMEResource(); err != nil {
  1028. tb.Fatal("Parser.CNAMEResource() =", err)
  1029. }
  1030. case TypeNS:
  1031. if _, err := p.NSResource(); err != nil {
  1032. tb.Fatal("Parser.NSResource() =", err)
  1033. }
  1034. case TypeOPT:
  1035. if _, err := p.OPTResource(); err != nil {
  1036. tb.Fatal("Parser.OPTResource() =", err)
  1037. }
  1038. default:
  1039. tb.Fatalf("got unknown type: %T", h)
  1040. }
  1041. }
  1042. }
  1043. func BenchmarkParsing(b *testing.B) {
  1044. buf, err := benchmarkParsingSetup()
  1045. if err != nil {
  1046. b.Fatal(err)
  1047. }
  1048. b.ReportAllocs()
  1049. for i := 0; i < b.N; i++ {
  1050. benchmarkParsing(b, buf)
  1051. }
  1052. }
  1053. func TestParsingAllocs(t *testing.T) {
  1054. buf, err := benchmarkParsingSetup()
  1055. if err != nil {
  1056. t.Fatal(err)
  1057. }
  1058. if allocs := testing.AllocsPerRun(100, func() { benchmarkParsing(t, buf) }); allocs > 0.5 {
  1059. t.Errorf("allocations during parsing: got = %f, want ~0", allocs)
  1060. }
  1061. }
  1062. func benchmarkBuildingSetup() (Name, []byte) {
  1063. name := MustNewName("foo.bar.example.com.")
  1064. buf := make([]byte, 0, packStartingCap)
  1065. return name, buf
  1066. }
  1067. func benchmarkBuilding(tb testing.TB, name Name, buf []byte) {
  1068. bld := NewBuilder(buf, Header{Response: true, Authoritative: true})
  1069. if err := bld.StartQuestions(); err != nil {
  1070. tb.Fatal("Builder.StartQuestions() =", err)
  1071. }
  1072. q := Question{
  1073. Name: name,
  1074. Type: TypeA,
  1075. Class: ClassINET,
  1076. }
  1077. if err := bld.Question(q); err != nil {
  1078. tb.Fatalf("Builder.Question(%+v) = %v", q, err)
  1079. }
  1080. hdr := ResourceHeader{
  1081. Name: name,
  1082. Class: ClassINET,
  1083. }
  1084. if err := bld.StartAnswers(); err != nil {
  1085. tb.Fatal("Builder.StartQuestions() =", err)
  1086. }
  1087. ar := AResource{[4]byte{}}
  1088. if err := bld.AResource(hdr, ar); err != nil {
  1089. tb.Fatalf("Builder.AResource(%+v, %+v) = %v", hdr, ar, err)
  1090. }
  1091. aaar := AAAAResource{[16]byte{}}
  1092. if err := bld.AAAAResource(hdr, aaar); err != nil {
  1093. tb.Fatalf("Builder.AAAAResource(%+v, %+v) = %v", hdr, aaar, err)
  1094. }
  1095. cnr := CNAMEResource{name}
  1096. if err := bld.CNAMEResource(hdr, cnr); err != nil {
  1097. tb.Fatalf("Builder.CNAMEResource(%+v, %+v) = %v", hdr, cnr, err)
  1098. }
  1099. nsr := NSResource{name}
  1100. if err := bld.NSResource(hdr, nsr); err != nil {
  1101. tb.Fatalf("Builder.NSResource(%+v, %+v) = %v", hdr, nsr, err)
  1102. }
  1103. extrc := 0xfe0 | RCodeNotImplemented
  1104. if err := (&hdr).SetEDNS0(4096, extrc, true); err != nil {
  1105. tb.Fatalf("ResourceHeader.SetEDNS0(4096, %#x, true) = %v", extrc, err)
  1106. }
  1107. optr := OPTResource{}
  1108. if err := bld.OPTResource(hdr, optr); err != nil {
  1109. tb.Fatalf("Builder.OPTResource(%+v, %+v) = %v", hdr, optr, err)
  1110. }
  1111. if _, err := bld.Finish(); err != nil {
  1112. tb.Fatal("Builder.Finish() =", err)
  1113. }
  1114. }
  1115. func BenchmarkBuilding(b *testing.B) {
  1116. name, buf := benchmarkBuildingSetup()
  1117. b.ReportAllocs()
  1118. for i := 0; i < b.N; i++ {
  1119. benchmarkBuilding(b, name, buf)
  1120. }
  1121. }
  1122. func TestBuildingAllocs(t *testing.T) {
  1123. name, buf := benchmarkBuildingSetup()
  1124. if allocs := testing.AllocsPerRun(100, func() { benchmarkBuilding(t, name, buf) }); allocs > 0.5 {
  1125. t.Errorf("allocations during building: got = %f, want ~0", allocs)
  1126. }
  1127. }
  1128. func smallTestMsg() Message {
  1129. name := MustNewName("example.com.")
  1130. return Message{
  1131. Header: Header{Response: true, Authoritative: true},
  1132. Questions: []Question{
  1133. {
  1134. Name: name,
  1135. Type: TypeA,
  1136. Class: ClassINET,
  1137. },
  1138. },
  1139. Answers: []Resource{
  1140. {
  1141. ResourceHeader{
  1142. Name: name,
  1143. Type: TypeA,
  1144. Class: ClassINET,
  1145. },
  1146. &AResource{[4]byte{127, 0, 0, 1}},
  1147. },
  1148. },
  1149. Authorities: []Resource{
  1150. {
  1151. ResourceHeader{
  1152. Name: name,
  1153. Type: TypeA,
  1154. Class: ClassINET,
  1155. },
  1156. &AResource{[4]byte{127, 0, 0, 1}},
  1157. },
  1158. },
  1159. Additionals: []Resource{
  1160. {
  1161. ResourceHeader{
  1162. Name: name,
  1163. Type: TypeA,
  1164. Class: ClassINET,
  1165. },
  1166. &AResource{[4]byte{127, 0, 0, 1}},
  1167. },
  1168. },
  1169. }
  1170. }
  1171. func BenchmarkPack(b *testing.B) {
  1172. msg := largeTestMsg()
  1173. b.ReportAllocs()
  1174. for i := 0; i < b.N; i++ {
  1175. if _, err := msg.Pack(); err != nil {
  1176. b.Fatal("Message.Pack() =", err)
  1177. }
  1178. }
  1179. }
  1180. func BenchmarkAppendPack(b *testing.B) {
  1181. msg := largeTestMsg()
  1182. buf := make([]byte, 0, packStartingCap)
  1183. b.ReportAllocs()
  1184. for i := 0; i < b.N; i++ {
  1185. if _, err := msg.AppendPack(buf[:0]); err != nil {
  1186. b.Fatal("Message.AppendPack() = ", err)
  1187. }
  1188. }
  1189. }
  1190. func largeTestMsg() Message {
  1191. name := MustNewName("foo.bar.example.com.")
  1192. return Message{
  1193. Header: Header{Response: true, Authoritative: true},
  1194. Questions: []Question{
  1195. {
  1196. Name: name,
  1197. Type: TypeA,
  1198. Class: ClassINET,
  1199. },
  1200. },
  1201. Answers: []Resource{
  1202. {
  1203. ResourceHeader{
  1204. Name: name,
  1205. Type: TypeA,
  1206. Class: ClassINET,
  1207. },
  1208. &AResource{[4]byte{127, 0, 0, 1}},
  1209. },
  1210. {
  1211. ResourceHeader{
  1212. Name: name,
  1213. Type: TypeA,
  1214. Class: ClassINET,
  1215. },
  1216. &AResource{[4]byte{127, 0, 0, 2}},
  1217. },
  1218. {
  1219. ResourceHeader{
  1220. Name: name,
  1221. Type: TypeAAAA,
  1222. Class: ClassINET,
  1223. },
  1224. &AAAAResource{[16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}},
  1225. },
  1226. {
  1227. ResourceHeader{
  1228. Name: name,
  1229. Type: TypeCNAME,
  1230. Class: ClassINET,
  1231. },
  1232. &CNAMEResource{MustNewName("alias.example.com.")},
  1233. },
  1234. {
  1235. ResourceHeader{
  1236. Name: name,
  1237. Type: TypeSOA,
  1238. Class: ClassINET,
  1239. },
  1240. &SOAResource{
  1241. NS: MustNewName("ns1.example.com."),
  1242. MBox: MustNewName("mb.example.com."),
  1243. Serial: 1,
  1244. Refresh: 2,
  1245. Retry: 3,
  1246. Expire: 4,
  1247. MinTTL: 5,
  1248. },
  1249. },
  1250. {
  1251. ResourceHeader{
  1252. Name: name,
  1253. Type: TypePTR,
  1254. Class: ClassINET,
  1255. },
  1256. &PTRResource{MustNewName("ptr.example.com.")},
  1257. },
  1258. {
  1259. ResourceHeader{
  1260. Name: name,
  1261. Type: TypeMX,
  1262. Class: ClassINET,
  1263. },
  1264. &MXResource{
  1265. 7,
  1266. MustNewName("mx.example.com."),
  1267. },
  1268. },
  1269. {
  1270. ResourceHeader{
  1271. Name: name,
  1272. Type: TypeSRV,
  1273. Class: ClassINET,
  1274. },
  1275. &SRVResource{
  1276. 8,
  1277. 9,
  1278. 11,
  1279. MustNewName("srv.example.com."),
  1280. },
  1281. },
  1282. },
  1283. Authorities: []Resource{
  1284. {
  1285. ResourceHeader{
  1286. Name: name,
  1287. Type: TypeNS,
  1288. Class: ClassINET,
  1289. },
  1290. &NSResource{MustNewName("ns1.example.com.")},
  1291. },
  1292. {
  1293. ResourceHeader{
  1294. Name: name,
  1295. Type: TypeNS,
  1296. Class: ClassINET,
  1297. },
  1298. &NSResource{MustNewName("ns2.example.com.")},
  1299. },
  1300. },
  1301. Additionals: []Resource{
  1302. {
  1303. ResourceHeader{
  1304. Name: name,
  1305. Type: TypeTXT,
  1306. Class: ClassINET,
  1307. },
  1308. &TXTResource{[]string{"So Long, and Thanks for All the Fish"}},
  1309. },
  1310. {
  1311. ResourceHeader{
  1312. Name: name,
  1313. Type: TypeTXT,
  1314. Class: ClassINET,
  1315. },
  1316. &TXTResource{[]string{"Hamster Huey and the Gooey Kablooie"}},
  1317. },
  1318. {
  1319. mustEDNS0ResourceHeader(4096, 0xfe0|RCodeSuccess, false),
  1320. &OPTResource{
  1321. Options: []Option{
  1322. {
  1323. Code: 10, // see RFC 7873
  1324. Data: []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef},
  1325. },
  1326. },
  1327. },
  1328. },
  1329. },
  1330. }
  1331. }