PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/test/test_dnssd_record.rb

http://github.com/tenderlove/dnssd
Ruby | 96 lines | 73 code | 23 blank | 0 comment | 0 complexity | 9120d0f9b418bf2281b187b2019890c7 MD5 | raw file
  1. require 'helper'
  2. class TestDNSSDRecord < DNSSD::Test
  3. def setup
  4. @fullname = 'blackjack._blackjack._tcp.test.'
  5. @IN = DNSSD::Record::IN
  6. @ipv4 = "\300\000\002\001".force_encoding("ascii-8bit")
  7. @ipv6 = " \001\r\270\000\000\000\000\000\000\000\000\000\000\000\001".force_encoding("ascii-8bit")
  8. @nowhere = "\007nowhere\007example\000".force_encoding("ascii-8bit")
  9. @R = DNSSD::Record
  10. end
  11. def test_class_to_data_invalid
  12. assert_raises ArgumentError do
  13. @R.to_data(-1)
  14. end
  15. end
  16. def test_class_to_data_A
  17. assert_equal @ipv4, @R.to_data(DNSSD::Record::A, '192.0.2.1')
  18. assert_equal @ipv4, @R.to_data(DNSSD::Record::A, IPAddr.new('192.0.2.1'))
  19. assert_raises ArgumentError do
  20. @R.to_data DNSSD::Record::A, '2001:db8::1'
  21. end
  22. end
  23. def test_idk
  24. a_variable = IPAddr.new '2001:db8::1'
  25. assert_equal @ipv6, a_variable.hton
  26. end
  27. def test_class_to_data_AAAA
  28. assert_equal @ipv6, @R.to_data(DNSSD::Record::AAAA, '2001:db8::1')
  29. assert_equal @ipv6,
  30. @R.to_data(DNSSD::Record::AAAA, IPAddr.new('2001:db8::1'))
  31. assert_raises ArgumentError do
  32. @R.to_data DNSSD::Record::AAAA, '192.0.2.1'
  33. end
  34. end
  35. def test_class_to_data_CNAME
  36. assert_equal @nowhere, @R.to_data(DNSSD::Record::CNAME, 'nowhere.example.')
  37. end
  38. def test_class_to_data_MX
  39. assert_equal "\000\010#{@nowhere}",
  40. @R.to_data(DNSSD::Record::MX, 8, 'nowhere.example.')
  41. end
  42. def test_class_to_data_NS
  43. assert_equal @nowhere, @R.to_data(DNSSD::Record::NS, 'nowhere.example.')
  44. end
  45. def test_class_to_data_PTR
  46. assert_equal @nowhere, @R.to_data(DNSSD::Record::PTR, 'nowhere.example.')
  47. end
  48. def test_class_to_data_SOA
  49. serial = 1
  50. refresh = 86400
  51. rtry = 3600
  52. expire = 86400 * 2
  53. minimum = 3600 * 12
  54. expected = "#{@nowhere}\002me#{@nowhere}#{[serial, refresh, rtry, expire, minimum].pack 'NNNNN'}"
  55. data = @R.to_data(DNSSD::Record::SOA, 'nowhere.example.',
  56. 'me.nowhere.example.', serial, refresh, rtry, expire,
  57. minimum)
  58. assert_equal expected, data
  59. end
  60. def test_class_to_data_SRV
  61. priority = 1
  62. weight = 5
  63. port = 1025
  64. expected = "#{[priority, weight, port].pack 'nnn'}#{@nowhere}"
  65. assert_equal expected,
  66. @R.to_data(DNSSD::Record::SRV, priority, weight, port,
  67. 'nowhere.example.')
  68. end
  69. def test_class_to_data_TXT
  70. assert_equal "\005Hello\006World!",
  71. @R.to_data(DNSSD::Record::TXT, 'Hello', 'World!')
  72. end
  73. end