PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/host_spec.rb

http://github.com/sophsec/ruby-nmap
Ruby | 224 lines | 169 code | 55 blank | 0 comment | 6 complexity | f1d500cffa0c61428a4dd9b6db5d7e5b MD5 | raw file
  1. require 'spec_helper'
  2. require 'scripts_examples'
  3. require 'nmap/xml'
  4. require 'nmap/host'
  5. describe Host do
  6. subject { @xml.hosts.first }
  7. describe "#start_time" do
  8. it "should parse the start_time" do
  9. expect(subject.start_time).to be > Time.at(0)
  10. end
  11. end
  12. describe "#end_time" do
  13. it "should parse the end_time" do
  14. expect(subject.end_time).to be > Time.at(0)
  15. expect(subject.end_time).to be > subject.start_time
  16. end
  17. end
  18. describe "#uptime" do
  19. subject { super().uptime }
  20. it "should return an Uptime object" do
  21. expect(subject).to be_kind_of(Uptime)
  22. end
  23. it "should parse the seconds attribute" do
  24. expect(subject.seconds).to be > 0
  25. end
  26. it "should parse the lastboot attribute" do
  27. expect(subject.last_boot).to be_kind_of(Time)
  28. end
  29. end
  30. describe "#tcp_sequence" do
  31. subject { super().tcp_sequence }
  32. it { is_expected.to be_kind_of(TcpSequence) }
  33. end
  34. describe "#ip_ip_sequence" do
  35. subject { super().ip_id_sequence }
  36. it { is_expected.to be_kind_of(IpIdSequence) }
  37. end
  38. describe "#tcp_ts_sequence" do
  39. subject { super().tcp_ts_sequence }
  40. it { is_expected.to be_kind_of(TcpTsSequence) }
  41. end
  42. describe "#status" do
  43. subject { super().status }
  44. it "should parse the state" do
  45. expect(subject.state).to eq(:up).or eq(:down)
  46. end
  47. it "should parse the reason" do
  48. expect(subject.reason).to eq('syn-ack').or \
  49. eq('timestamp-reply').or \
  50. eq('echo-reply').or \
  51. eq('reset')
  52. end
  53. it "should parse the reason_ttl" do
  54. expect(subject.reason_ttl).to be_kind_of(Integer)
  55. end
  56. end
  57. let(:ipv4) { '45.33.32.156' }
  58. describe "#addresses" do
  59. subject { super().addresses.first }
  60. it "should parse the type" do
  61. expect(subject.type).to eq(:ipv4)
  62. end
  63. it "should parser the addr" do
  64. expect(subject.addr).to eq(ipv4)
  65. end
  66. end
  67. describe "#mac" do
  68. it "should parse the first MAC address" do
  69. skip "need a host with address[@addrtype='mac']"
  70. end
  71. end
  72. describe "#ipv6" do
  73. it "should parse the first IPv6 address" do
  74. skip "need a host with address[@addrtype='ipv6']"
  75. end
  76. end
  77. describe "#ipv4" do
  78. it "should parse the IPv4 address" do
  79. expect(subject.ipv4).to eq(ipv4)
  80. end
  81. end
  82. describe "#ip" do
  83. it "should have an IP" do
  84. expect(subject.ip).to eq(ipv4)
  85. end
  86. end
  87. describe "#address" do
  88. it "should have an address" do
  89. expect(subject.address).to eq(ipv4)
  90. end
  91. end
  92. describe "#hostnames" do
  93. subject { super().hostnames }
  94. it { is_expected.not_to be_empty }
  95. it "should parse the type" do
  96. expect(subject.all? { |hostname| hostname.type }).to be_truthy
  97. end
  98. it "should parse the name" do
  99. expect(subject.all? { |hostname| hostname.name }).to be_truthy
  100. end
  101. it "should include a user hostname" do
  102. expect(subject.any? { |hostname| hostname.type == 'user' }).to be_truthy
  103. end
  104. it "should include a PTR hostname" do
  105. expect(subject.any? { |hostname| hostname.type == 'PTR' }).to be_truthy
  106. end
  107. end
  108. describe "#hostname" do
  109. it "should return the first hostname" do
  110. expect(subject.hostname).to be == subject.hostnames.first
  111. end
  112. end
  113. describe "#os" do
  114. subject { super().os }
  115. it { is_expected.not_to be_nil }
  116. it { is_expected.to be_kind_of(OS) }
  117. end
  118. describe "#ports" do
  119. subject { super().ports }
  120. it { is_expected.not_to be_empty }
  121. end
  122. describe "#open_ports" do
  123. subject { super().open_ports }
  124. it { is_expected.not_to be_empty }
  125. it "should list the open ports" do
  126. expect(subject.all? { |port| port.state == :open }).to be_truthy
  127. end
  128. end
  129. describe "#tcp_ports" do
  130. subject { super().tcp_ports }
  131. it { is_expected.not_to be_empty }
  132. it "should contain TCP ports" do
  133. expect(subject.all? { |port| port.protocol == :tcp }).to be_truthy
  134. end
  135. end
  136. describe "#udp_ports" do
  137. subject { super().udp_ports }
  138. it { is_expected.not_to be_empty }
  139. it "should contain UDP ports" do
  140. expect(subject.all? { |port| port.protocol == :udp }).to be_truthy
  141. end
  142. end
  143. describe "#to_s" do
  144. it "should return the first hostname" do
  145. expect(subject.to_s).to eq('scanme.nmap.org')
  146. end
  147. context "when #hostname returns nil" do
  148. before { expect(subject).to receive(:hostname).and_return(nil) }
  149. it "should return the first address" do
  150. expect(subject.to_s).to eq(ipv4)
  151. end
  152. end
  153. end
  154. describe "#inspect" do
  155. it "should include the String representation of the host" do
  156. expect(subject.inspect).to include(subject.to_s)
  157. end
  158. end
  159. describe "#host_script" do
  160. subject { super().host_script }
  161. pending "scan.xml does not currently include any hostscript elements"
  162. end
  163. describe "#vendor" do
  164. subject { @local_xml.host }
  165. it "should parse the vendor name" do
  166. expect(subject.vendor).to eq('LG Electronics')
  167. end
  168. end
  169. end