test.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. var mdns = require('./')
  2. var tape = require('tape')
  3. var dgram = require('dgram')
  4. var port = function (cb) {
  5. var s = dgram.createSocket('udp4')
  6. s.bind(0, function () {
  7. var port = s.address().port
  8. s.on('close', function () {
  9. cb(port)
  10. })
  11. s.close()
  12. })
  13. }
  14. var configs = [
  15. {ip: '127.0.0.1', multicast: false}
  16. // {'interface': '127.0.0.1', multicast: true}
  17. ]
  18. var tests = configs.map(function (config) {
  19. return function (name, fn) {
  20. tape(name, function (t) {
  21. port(function (p) {
  22. config.port = p
  23. var dns = mdns(config)
  24. dns.on('warning', function (e) {
  25. t.error(e)
  26. })
  27. fn(dns, t)
  28. })
  29. })
  30. }
  31. })
  32. tests.forEach(function (test) {
  33. test('works', function (dns, t) {
  34. t.plan(3)
  35. dns.once('query', function (packet) {
  36. t.same(packet.type, 'query')
  37. dns.destroy(function () {
  38. t.ok(true, 'destroys')
  39. })
  40. })
  41. dns.query('hello-world', function () {
  42. t.ok(true, 'flushed')
  43. })
  44. })
  45. test('ANY query', function (dns, t) {
  46. dns.once('query', function (packet) {
  47. t.same(packet.questions.length, 1, 'one question')
  48. t.same(packet.questions[0], {name: 'hello-world', type: 'ANY', class: 1})
  49. dns.destroy(function () {
  50. t.end()
  51. })
  52. })
  53. dns.query('hello-world', 'ANY')
  54. })
  55. test('A record', function (dns, t) {
  56. dns.once('query', function (packet) {
  57. t.same(packet.questions.length, 1, 'one question')
  58. t.same(packet.questions[0], {name: 'hello-world', type: 'A', class: 1})
  59. dns.respond([{type: 'A', name: 'hello-world', ttl: 120, data: '127.0.0.1'}])
  60. })
  61. dns.once('response', function (packet) {
  62. t.same(packet.answers.length, 1, 'one answer')
  63. t.same(packet.answers[0], {type: 'A', name: 'hello-world', ttl: 120, data: '127.0.0.1', class: 1, flush: false})
  64. dns.destroy(function () {
  65. t.end()
  66. })
  67. })
  68. dns.query('hello-world', 'A')
  69. })
  70. test('A record (two questions)', function (dns, t) {
  71. dns.once('query', function (packet) {
  72. t.same(packet.questions.length, 2, 'two questions')
  73. t.same(packet.questions[0], {name: 'hello-world', type: 'A', class: 1})
  74. t.same(packet.questions[1], {name: 'hej.verden', type: 'A', class: 1})
  75. dns.respond([{type: 'A', name: 'hello-world', ttl: 120, data: '127.0.0.1'}, {
  76. type: 'A',
  77. name: 'hej.verden',
  78. ttl: 120,
  79. data: '127.0.0.2'
  80. }])
  81. })
  82. dns.once('response', function (packet) {
  83. t.same(packet.answers.length, 2, 'one answers')
  84. t.same(packet.answers[0], {type: 'A', name: 'hello-world', ttl: 120, data: '127.0.0.1', class: 1, flush: false})
  85. t.same(packet.answers[1], {type: 'A', name: 'hej.verden', ttl: 120, data: '127.0.0.2', class: 1, flush: false})
  86. dns.destroy(function () {
  87. t.end()
  88. })
  89. })
  90. dns.query([{name: 'hello-world', type: 'A'}, {name: 'hej.verden', type: 'A'}])
  91. })
  92. test('AAAA record', function (dns, t) {
  93. dns.once('query', function (packet) {
  94. t.same(packet.questions.length, 1, 'one question')
  95. t.same(packet.questions[0], {name: 'hello-world', type: 'AAAA', class: 1})
  96. dns.respond([{type: 'AAAA', name: 'hello-world', ttl: 120, data: 'fe80::5ef9:38ff:fe8c:ceaa'}])
  97. })
  98. dns.once('response', function (packet) {
  99. t.same(packet.answers.length, 1, 'one answer')
  100. t.same(packet.answers[0], {
  101. type: 'AAAA',
  102. name: 'hello-world',
  103. ttl: 120,
  104. data: 'fe80::5ef9:38ff:fe8c:ceaa',
  105. class: 1,
  106. flush: false
  107. })
  108. dns.destroy(function () {
  109. t.end()
  110. })
  111. })
  112. dns.query('hello-world', 'AAAA')
  113. })
  114. test('SRV record', function (dns, t) {
  115. dns.once('query', function (packet) {
  116. t.same(packet.questions.length, 1, 'one question')
  117. t.same(packet.questions[0], {name: 'hello-world', type: 'SRV', class: 1})
  118. dns.respond([{
  119. type: 'SRV',
  120. name: 'hello-world',
  121. ttl: 120,
  122. data: {port: 11111, target: 'hello.world.com', priority: 10, weight: 12}
  123. }])
  124. })
  125. dns.once('response', function (packet) {
  126. t.same(packet.answers.length, 1, 'one answer')
  127. t.same(packet.answers[0], {
  128. type: 'SRV',
  129. name: 'hello-world',
  130. ttl: 120,
  131. data: {port: 11111, target: 'hello.world.com', priority: 10, weight: 12},
  132. class: 1,
  133. flush: false
  134. })
  135. dns.destroy(function () {
  136. t.end()
  137. })
  138. })
  139. dns.query('hello-world', 'SRV')
  140. })
  141. test('TXT record', function (dns, t) {
  142. var data = Buffer.from('black box')
  143. dns.once('query', function (packet) {
  144. t.same(packet.questions.length, 1, 'one question')
  145. t.same(packet.questions[0], {name: 'hello-world', type: 'TXT', class: 1})
  146. dns.respond([{type: 'TXT', name: 'hello-world', ttl: 120, data: data}])
  147. })
  148. dns.once('response', function (packet) {
  149. t.same(packet.answers.length, 1, 'one answer')
  150. t.same(packet.answers[0], {type: 'TXT', name: 'hello-world', ttl: 120, data: data, class: 1, flush: false})
  151. dns.destroy(function () {
  152. t.end()
  153. })
  154. })
  155. dns.query('hello-world', 'TXT')
  156. })
  157. test('QU question bit', function (dns, t) {
  158. dns.once('query', function (packet) {
  159. t.same(packet.questions, [
  160. {type: 'A', name: 'foo', class: 1},
  161. {type: 'A', name: 'bar', class: 1}
  162. ])
  163. dns.destroy(function () {
  164. t.end()
  165. })
  166. })
  167. dns.query([
  168. {type: 'A', name: 'foo', class: 32769},
  169. {type: 'A', name: 'bar', class: 1}
  170. ])
  171. })
  172. test('cache flush bit', function (dns, t) {
  173. dns.once('query', function (packet) {
  174. dns.respond({
  175. answers: [
  176. {type: 'A', name: 'foo', ttl: 120, data: '127.0.0.1', class: 1, flush: true},
  177. {type: 'A', name: 'foo', ttl: 120, data: '127.0.0.2', class: 1, flush: false}
  178. ],
  179. additionals: [
  180. {type: 'A', name: 'foo', ttl: 120, data: '127.0.0.3', class: 1, flush: true}
  181. ]
  182. })
  183. })
  184. dns.once('response', function (packet) {
  185. t.same(packet.answers, [
  186. {type: 'A', name: 'foo', ttl: 120, data: '127.0.0.1', class: 1, flush: true},
  187. {type: 'A', name: 'foo', ttl: 120, data: '127.0.0.2', class: 1, flush: false}
  188. ])
  189. t.same(packet.additionals[0], {type: 'A', name: 'foo', ttl: 120, data: '127.0.0.3', class: 1, flush: true})
  190. dns.destroy(function () {
  191. t.end()
  192. })
  193. })
  194. dns.query('foo', 'A')
  195. })
  196. test('Authoritive Answer bit', function (dns, t) {
  197. dns.once('query', function (packet) {
  198. dns.respond([])
  199. })
  200. dns.once('response', function (packet) {
  201. t.ok(packet.flag_auth, 'should be set')
  202. dns.destroy(function () {
  203. t.end()
  204. })
  205. })
  206. dns.query('foo', 'A')
  207. })
  208. })