test.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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: 'IN'})
  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: 'IN'})
  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: 'IN', 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: 'IN'})
  74. t.same(packet.questions[1], {name: 'hej.verden', type: 'A', class: 'IN'})
  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: 'IN', flush: false})
  85. t.same(packet.answers[1], {type: 'A', name: 'hej.verden', ttl: 120, data: '127.0.0.2', class: 'IN', 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: 'IN'})
  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: 'IN',
  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: 'IN'})
  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: 'IN',
  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: 'IN'})
  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: 'IN', flush: false})
  151. dns.destroy(function () {
  152. t.end()
  153. })
  154. })
  155. dns.query('hello-world', 'TXT')
  156. })
  157. test('TXT array record', function (dns, t) {
  158. var data = ['black', 'box']
  159. dns.once('query', function (packet) {
  160. t.same(packet.questions.length, 1, 'one question')
  161. t.same(packet.questions[0], {name: 'hello-world', type: 'TXT', class: 'IN'})
  162. dns.respond([{type: 'TXT', name: 'hello-world', ttl: 120, data: data}])
  163. })
  164. dns.once('response', function (packet) {
  165. t.same(packet.answers.length, 1, 'one answer')
  166. t.same(packet.answers[0], {type: 'TXT', name: 'hello-world', ttl: 120, data: data, class: 'IN', flush: false})
  167. dns.destroy(function () {
  168. t.end()
  169. })
  170. })
  171. dns.query('hello-world', 'TXT')
  172. })
  173. test('QU question bit', function (dns, t) {
  174. dns.once('query', function (packet) {
  175. t.same(packet.questions, [
  176. {type: 'A', name: 'foo', class: 'IN'},
  177. {type: 'A', name: 'bar', class: 'IN'}
  178. ])
  179. dns.destroy(function () {
  180. t.end()
  181. })
  182. })
  183. dns.query([
  184. {type: 'A', name: 'foo', class: 'IN'},
  185. {type: 'A', name: 'bar', class: 'IN'}
  186. ])
  187. })
  188. test('cache flush bit', function (dns, t) {
  189. dns.once('query', function (packet) {
  190. dns.respond({
  191. answers: [
  192. {type: 'A', name: 'foo', ttl: 120, data: '127.0.0.1', class: 'IN', flush: true},
  193. {type: 'A', name: 'foo', ttl: 120, data: '127.0.0.2', class: 'IN', flush: false}
  194. ],
  195. additionals: [
  196. {type: 'A', name: 'foo', ttl: 120, data: '127.0.0.3', class: 'IN', flush: true}
  197. ]
  198. })
  199. })
  200. dns.once('response', function (packet) {
  201. t.same(packet.answers, [
  202. {type: 'A', name: 'foo', ttl: 120, data: '127.0.0.1', class: 'IN', flush: true},
  203. {type: 'A', name: 'foo', ttl: 120, data: '127.0.0.2', class: 'IN', flush: false}
  204. ])
  205. t.same(packet.additionals[0], {type: 'A', name: 'foo', ttl: 120, data: '127.0.0.3', class: 'IN', flush: true})
  206. dns.destroy(function () {
  207. t.end()
  208. })
  209. })
  210. dns.query('foo', 'A')
  211. })
  212. test('Authoritive Answer bit', function (dns, t) {
  213. dns.once('query', function (packet) {
  214. dns.respond([])
  215. })
  216. dns.once('response', function (packet) {
  217. t.ok(packet.flag_aa, 'should be set')
  218. dns.destroy(function () {
  219. t.end()
  220. })
  221. })
  222. dns.query('foo', 'A')
  223. })
  224. })