summaryrefslogtreecommitdiff
path: root/lib/custodian/testfactory.rb
blob: 179b0509ce31b2712c597678ed646356506cba24 (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
#
#
# Base class for custodian protocol-tests
#
# Each subclass will register themselves, via the call
# to 'register_test_type'.
#
# This class is a factory that will return the correct
# derived class for a given line from our configuration
# file.
#
#
module Custodian

  class TestFactory


    #
    # The subclasses we have.
    #
    @@subclasses = { }


    #
    # Create a test-type object given a line of text from our parser.
    #
    # The line will be like "target must run tcp|ssh|ftp|smtp .."
    #
    #
    def self.create( line )

      #
      # If this is an obvious protocol test.
      #
      if ( line =~ /must\s+(not\s+)?run\s+(\S+)(\s+|\.|$)/ )
        test_type = $2.dup
        test_type.chomp!( "." )
        c = @@subclasses[test_type]
        if c
          c.new( line )
        else
          raise ArgumentError, "Bad test type: '#{test_type}'"
        end
      else
        raise "Unknown line given - Failed to instantiate a suitable protocol-test: '#{line}'"
      end
    end


    #
    # Register a new test type - this must be called by our derived classes
    #
    def self.register_test_type name
      @@subclasses[name] = self
    end


    #
    # Return an array of test-types we know about
    #
    # i.e. Derived classes that have registered themselves.
    #
    #
    def self.known_tests
      @@subclasses
    end




    #
    # Return the target of this test.
    #
    def target
      @host
    end



    #
    #  Is this test inverted?
    #
    def inverted
      @inverted
    end


    #
    #  Return the port of this test.
    #
    def port
      @port
    end




  end

end