message_test.go 41 KB

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