summaryrefslogtreecommitdiff
path: root/t/test-alerter.rb
blob: f82620533eebcc7f27ac1a2ea07856a979d99235 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/ruby1.8 -I./lib/ -I../lib/



require 'test/unit'
require 'custodian/alerter'



#
# Unit test for our alerting class
#
# This doesn't actually test the alerts, but it will
# test that we can successfully determine whether a
# destination is inside or outside the Bytemark network.
#
#
class TestAlerter < Test::Unit::TestCase

  #
  # Create the test suite environment: NOP.
  #
  def setup
  end

  #
  # Destroy the test suite environment: NOP.
  #
  def teardown
  end


  #
  # Ensure we can instantiate the object
  #
  def test_init

    assert_nothing_raised do
      obj = Alerter.new( {} )
      assert( obj )
    end
  end


  #
  #  Test location-detection.
  #
  def test_location_detection

    #
    #  Hash of hostnames and version of address.
    #
    to_test = {

      #
      # Hosts inside the Bytemark network
      #
      "www.steve.org.uk"               => true,
      "ipv6.steve.org.uk"              => true,
      "http://www.steve.org.uk/"       => true,
      "http://ipv6.steve.org.uk"       => true,
      "canalrivertrust.org.uk"         => true,
      "http://canalrivertrust.org.uk/" => true,
      "http://canalrivertrust.org.uk"  => true,

      #
      # Hosts outside the Bytemark network
      #
      "https://google.com/"     => false,
      "http://google.com/"      => false,
      "http://ipv6.google.com/" => false,
      "http://192.168.0.333/"   => false,
    }



    to_test.each do |name,inside|

      obj = Alerter.new( nil )

      text = obj.expand_inside_bytemark( name )

      if ( text =~ /is inside/ )
        assert( inside == true )
      end
      if ( text =~ /OUTSIDE/ )
        assert( inside == false )
      end
    end

    #
    # OK now look for the text returned
    #
    obj = Alerter.new( nil )
    details = obj.expand_inside_bytemark( "46.43.50.217" )
    assert( details.match( /46.43.50.217 is inside the Bytemark network/ ) )

    details = obj.expand_inside_bytemark( "www.linnrecords.com" )
    assert( details.match( /resolves to 46.43.50.217 which is inside the Bytemark network/ ) )

  end



  #
  #  Test documentation-detection.
  #
  def test_locations_inside_outside

    obj = Alerter.new( nil )

    assert_raise ArgumentError do
      obj.document_address( nil )
    end

    #
    # IPv6 lookup
    #
    details = obj.document_address( "2001:41c8:125:46::22" )
    assert( details =~ /ssh.steve.org.uk/i )

    #
    # IPv4 lookup
    #
    details = obj.document_address( "80.68.85.48" )
    assert( details =~ /ssh.steve.org.uk/i )

    #
    # Bogus lookup - should return nil.
    #
    details = obj.document_address( "800.683.853.348" )
    assert( details.nil? )

  end
end