You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
4.9 KiB

4 years ago
  1. 'use strict'
  2. var os = require('os')
  3. var dgram = require('dgram')
  4. var tape = require('tape')
  5. var afterAll = require('after-all')
  6. var Service = require('../lib/service')
  7. var Bonjour = require('../')
  8. var getAddresses = function () {
  9. var addresses = []
  10. var itrs = os.networkInterfaces()
  11. for (var i in itrs) {
  12. var addrs = itrs[i]
  13. for (var j in addrs) {
  14. if (addrs[j].internal === false) {
  15. addresses.push(addrs[j].address)
  16. }
  17. }
  18. }
  19. return addresses
  20. }
  21. var port = function (cb) {
  22. var s = dgram.createSocket('udp4')
  23. s.bind(0, function () {
  24. var port = s.address().port
  25. s.on('close', function () {
  26. cb(port)
  27. })
  28. s.close()
  29. })
  30. }
  31. var test = function (name, fn) {
  32. tape(name, function (t) {
  33. port(function (p) {
  34. fn(Bonjour({ ip: '127.0.0.1', port: p, multicast: false }), t)
  35. })
  36. })
  37. }
  38. test('bonjour.publish', function (bonjour, t) {
  39. var service = bonjour.publish({ name: 'foo', type: 'bar', port: 3000 })
  40. t.ok(service instanceof Service)
  41. t.equal(service.published, false)
  42. service.on('up', function () {
  43. t.equal(service.published, true)
  44. bonjour.destroy()
  45. t.end()
  46. })
  47. })
  48. test('bonjour.unpublishAll', function (bonjour, t) {
  49. t.test('published services', function (t) {
  50. var service = bonjour.publish({ name: 'foo', type: 'bar', port: 3000 })
  51. service.on('up', function () {
  52. bonjour.unpublishAll(function (err) {
  53. t.error(err)
  54. t.equal(service.published, false)
  55. bonjour.destroy()
  56. t.end()
  57. })
  58. })
  59. })
  60. t.test('no published services', function (t) {
  61. bonjour.unpublishAll(function (err) {
  62. t.error(err)
  63. t.end()
  64. })
  65. })
  66. })
  67. test('bonjour.find', function (bonjour, t) {
  68. var next = afterAll(function () {
  69. var browser = bonjour.find({ type: 'test' })
  70. var ups = 0
  71. browser.on('up', function (s) {
  72. if (s.name === 'Foo Bar') {
  73. t.equal(s.name, 'Foo Bar')
  74. t.equal(s.fqdn, 'Foo Bar._test._tcp.local')
  75. t.deepEqual(s.txt, {})
  76. t.deepEqual(s.rawTxt, new Buffer('00', 'hex'))
  77. } else {
  78. t.equal(s.name, 'Baz')
  79. t.equal(s.fqdn, 'Baz._test._tcp.local')
  80. t.deepEqual(s.txt, { foo: 'bar' })
  81. t.deepEqual(s.rawTxt, new Buffer('07666f6f3d626172', 'hex'))
  82. }
  83. t.equal(s.host, os.hostname())
  84. t.equal(s.port, 3000)
  85. t.equal(s.type, 'test')
  86. t.equal(s.protocol, 'tcp')
  87. t.equal(s.referer.address, '127.0.0.1')
  88. t.equal(s.referer.family, 'IPv4')
  89. t.ok(Number.isFinite(s.referer.port))
  90. t.ok(Number.isFinite(s.referer.size))
  91. t.deepEqual(s.subtypes, [])
  92. t.deepEqual(s.addresses.sort(), getAddresses().sort())
  93. if (++ups === 2) {
  94. // use timeout in an attempt to make sure the invalid record doesn't
  95. // bubble up
  96. setTimeout(function () {
  97. bonjour.destroy()
  98. t.end()
  99. }, 50)
  100. }
  101. })
  102. })
  103. bonjour.publish({ name: 'Foo Bar', type: 'test', port: 3000 }).on('up', next())
  104. bonjour.publish({ name: 'Invalid', type: 'test2', port: 3000 }).on('up', next())
  105. bonjour.publish({ name: 'Baz', type: 'test', port: 3000, txt: { foo: 'bar' } }).on('up', next())
  106. })
  107. test('bonjour.find - binary txt', function (bonjour, t) {
  108. var next = afterAll(function () {
  109. var browser = bonjour.find({ type: 'test', txt: { binary: true } })
  110. browser.on('up', function (s) {
  111. t.equal(s.name, 'Foo')
  112. t.deepEqual(s.txt, { bar: new Buffer('buz') })
  113. t.deepEqual(s.rawTxt, new Buffer('076261723d62757a', 'hex'))
  114. bonjour.destroy()
  115. t.end()
  116. })
  117. })
  118. bonjour.publish({ name: 'Foo', type: 'test', port: 3000, txt: { bar: new Buffer('buz') } }).on('up', next())
  119. })
  120. test('bonjour.find - down event', function (bonjour, t) {
  121. var service = bonjour.publish({ name: 'Foo Bar', type: 'test', port: 3000 })
  122. service.on('up', function () {
  123. var browser = bonjour.find({ type: 'test' })
  124. browser.on('up', function (s) {
  125. t.equal(s.name, 'Foo Bar')
  126. service.stop()
  127. })
  128. browser.on('down', function (s) {
  129. t.equal(s.name, 'Foo Bar')
  130. bonjour.destroy()
  131. t.end()
  132. })
  133. })
  134. })
  135. test('bonjour.findOne - callback', function (bonjour, t) {
  136. var next = afterAll(function () {
  137. bonjour.findOne({ type: 'test' }, function (s) {
  138. t.equal(s.name, 'Callback')
  139. bonjour.destroy()
  140. t.end()
  141. })
  142. })
  143. bonjour.publish({ name: 'Invalid', type: 'test2', port: 3000 }).on('up', next())
  144. bonjour.publish({ name: 'Callback', type: 'test', port: 3000 }).on('up', next())
  145. })
  146. test('bonjour.findOne - emitter', function (bonjour, t) {
  147. var next = afterAll(function () {
  148. var browser = bonjour.findOne({ type: 'test' })
  149. browser.on('up', function (s) {
  150. t.equal(s.name, 'Emitter')
  151. bonjour.destroy()
  152. t.end()
  153. })
  154. })
  155. bonjour.publish({ name: 'Emitter', type: 'test', port: 3000 }).on('up', next())
  156. bonjour.publish({ name: 'Invalid', type: 'test2', port: 3000 }).on('up', next())
  157. })