CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/spec/lib/rex/proto/dns/static_hostnames_spec.rb
Views: 1904
1
# -*- coding:binary -*-
2
require 'dnsruby'
3
require 'spec_helper'
4
5
RSpec.describe Rex::Proto::DNS::StaticHostnames do
6
describe '#parse_hosts_file' do
7
context 'when parsing a file' do
8
let(:subject) { described_class.new }
9
let(:hosts_file) {
10
<<~CONTENT
11
# this is a comment
12
13
127.0.0.1 localhost localhost4
14
::1 localhost localhost6
15
127.1.1.1 localhost
16
thisIsInvalid
17
CONTENT
18
}
19
20
before(:each) do
21
expect(File).to receive(:file?).and_return(true)
22
expect(File).to receive(:readable?).and_return(true)
23
expect(::IO).to receive(:foreach) do |_, &block|
24
hosts_file.split("\n").each do |line|
25
block.call(line)
26
end
27
end
28
subject.parse_hosts_file
29
end
30
31
it 'is not empty' do
32
expect(subject.empty?).to be_falsey
33
end
34
35
context 'when no type is specified' do
36
it 'returns an IPv4 address' do
37
expect(subject.get('localhost')).to eq ['127.0.0.1', '127.1.1.1']
38
end
39
end
40
41
it 'defines an IPv4 address for localhost' do
42
expect(subject.get('localhost', Dnsruby::Types::A)).to eq ['127.0.0.1', '127.1.1.1']
43
end
44
45
it 'defines an IPv6 address for localhost' do
46
expect(subject.get('localhost', Dnsruby::Types::AAAA)).to eq ['::1']
47
end
48
end
49
end
50
51
context 'when no hosts are defined' do
52
let(:subject) { described_class.new }
53
54
describe '#empty?' do
55
it 'is true' do
56
expect(subject.empty?).to be_truthy
57
end
58
end
59
60
describe '#get' do
61
it 'returns an empty array' do
62
expect(subject.get('localhost')).to eq []
63
end
64
end
65
66
describe '#get1' do
67
it 'returns nil' do
68
expect(subject.get1('localhost')).to be_nil
69
end
70
end
71
72
describe '#length' do
73
it 'is zero' do
74
expect(subject.length).to eq 0
75
end
76
end
77
end
78
end
79
80