aboutsummaryrefslogtreecommitdiff
path: root/test/tc_mauve_source_list.rb
blob: 6241a2f60c50e850792f9acc33fad0dc42ad3c26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
$:.unshift "../lib/"

require 'th_mauve'
require 'th_mauve_resolv'
require 'mauve/source_list'
require 'webmock'

class TcMauveSourceList < Mauve::UnitTest

  include Mauve
  include WebMock::API

  def setup
    super
    setup_database
    WebMock.disable_net_connect!
  end

  def teardown
    WebMock.reset!
    WebMock.allow_net_connect!
    teardown_database
    super
  end

  def test_hostname_match
    sl = SourceList.new("test")
    assert_equal("test", sl.label)

    list = %w(a.example.com b.example.com c.example.com)
    assert_nothing_raised{ sl += list }

    assert_equal(list, sl.list)

    assert( sl.includes?("a.example.com") )
    assert( !sl.includes?("d.example.com") )
  end

  def test_regex_match
    sl = SourceList.new("test")

    assert_nothing_raised{ sl += %w([a-c].example.com *.[d-f].example.com g.example.com) }

    %w(a.example.com www.f.example.com www.a.example.com g.example.com www.other.a.example.com).each do |h|
      assert( sl.includes?(h), "#{h} did not match")
    end

    %w(d.example.com a.example.com.other d.example.com).each do |h|
      assert( !sl.includes?(h), "#{h} matched when it shouldn't have")
    end
  end

  def test_ip_match
    sl = SourceList.new("test")

    assert_nothing_raised{ sl += %w(test-1.example.com 1.2.3.5 2001:1:2:3::5 1.2.4.0/24 2001:1:2:4::/64) }

    %w(1.2.3.4 2001:1:2:3::4 1.2.3.5 2001:1:2:3::5 test-2.example.com 1.2.4.23 2001:1:2:4::23 ).each do |h|
      assert( sl.includes?(h), "#{h} did not match")
    end

    %w(1.2.3.6 2001:1:2:3::6 test-3.example.com 1.2.5.23 2001:1:2:5::23 ).each do |h|
      assert( !sl.includes?(h), "#{h} matched when it shouldn't have")
    end
 
  end

  def test_uri_match
    sl = SourceList.new("test")

    assert_nothing_raised { sl += "test-1.example.com" }

    %w(https://www.example.com ftp://test-1.example.com http://1.2.3.4 https://[2001:1:2:3::4]).each do |uri|
      assert( sl.includes?(uri), "#{uri} did not match") 
    end

    %w(http://www.google.com ftp://www2.example.com).each do |uri|
      assert( !sl.includes?(uri), "#{uri} matched when it shouldn't have" )
    end
  end

  def test_ip_crossmatch
    sl = SourceList.new("test")
    assert_nothing_raised { sl += "test-1.example.com" }
    assert( sl.includes?("www.example.com"), "www.example.com not found in #{sl.list}" )

    sl = SourceList.new("test")
    assert_nothing_raised { sl += "2001::/3" }
    assert( sl.includes?("www2.example.com"), "www2.example.com not found in #{sl.list}" )
  end

  def test_ip_crossmatch_fail_when_minimal_dns_is_available
    Configuration.current.minimal_dns_lookups = true

    sl = SourceList.new("test")
    assert_nothing_raised { sl += "test-1.example.com" }
    assert( !sl.includes?("www.example.com"), "www.example.com not found in #{sl.list}" )

    sl = SourceList.new("test")
    assert_nothing_raised { sl += "2001::/3" }
    assert( !sl.includes?("www2.example.com"), "www2.example.com not found in #{sl.list}" )
  end

  def test_remote_source_list
    stub_request(:get, "http://localhost/network/monitor_ip/by_tag/Managed").
      to_return(:status => 200, :body => %w(1.2.3.4 1.2.3.5).join("\n"))

    sl = SourceList.new("test","http://localhost/network/monitor_ip/by_tag/Managed")

    assert( sl.includes?("1.2.3.4"), "1.2.3.4 not found in #{sl.list}" )
    assert( sl.includes?("test-1.example.com"), "test-1.example.com not found in #{sl.list}" )
  end

end