1 require "#{File.dirname(__FILE__)}/../test_helper"
5 class TestEmitter < Test::Unit::TestCase
6 BEFORE_DELAY = ENV['BEFORE_DELAY'] ? ENV['BEFORE_DELAY'].to_f : 0.5
7 AFTER_DELAY = ENV['AFTER_DELAY'] ? ENV['AFTER_DELAY'].to_f : 0.5
10 assert_instance_of Module, LWES, "LWES is not a module"
11 assert_instance_of Class, LWES::Emitter, "LWES::Emitter is not a class"
14 def lwes_listener(&block)
15 cmd = "lwes-event-printing-listener" \
16 " -m #{@options[:address]}" \
17 " -i #{@options[:iface]}" \
18 " -p #{@options[:port]}"
19 out = Tempfile.new("out")
20 err = Tempfile.new("err")
24 $stdout.reopen(out.path)
25 $stderr.reopen(err.path)
29 # since everything executes asynchronously and our messaging,
30 # we need to ensure our listener is ready, then ensure our
31 # listener has printed something...
37 Process.kill(:TERM, pid)
39 assert_equal 0, err.size
46 :address => ENV["LWES_TEST_ADDRESS"] || "127.0.0.1",
47 :iface => ENV["LWES_TEST_IFACE"] || "0.0.0.0",
48 :port => ENV["LWES_TEST_PORT"] ? ENV["LWES_TEST_PORT"].to_i : 12345,
49 :ttl => 60, # nil for no ttl)
54 assert_instance_of LWES::Emitter, LWES::Emitter.new(@options)
57 def test_initialize_with_heartbeat
58 heartbeat = @options.merge(:heartbeat => 30)
59 assert_instance_of LWES::Emitter, LWES::Emitter.new(heartbeat)
62 def test_initialize_no_ttl
65 assert_instance_of LWES::Emitter, LWES::Emitter.new(no_ttl)
68 def test_initialize_invalid
69 assert_raises(TypeError) {
70 LWES::Emitter.new(@options.merge(:address => nil))
74 def test_initialize_empty_options
75 assert_raises(TypeError) { LWES::Emitter.new({}) }
79 emitter = LWES::Emitter.new(@options)
80 assert_raises(TypeError) { emitter.emit "Invalid", nil }
81 assert_raises(ArgumentError) { emitter.emit nil, { :hello => "world" }}
84 def test_emit_empty_hash
85 emitter = LWES::Emitter.new(@options)
86 assert_nothing_raised { emitter.emit("Valid", Hash.new) }
89 def test_emit_non_empty_hashes
90 emitter = LWES::Emitter.new(@options)
91 out = lwes_listener do
92 assert_nothing_raised {
93 emitter.emit("ASDF", { :foo => "FOO", :nr => [ :int16, 50 ] })
97 assert_match %r{\AASDF\b}, lines.first
98 assert ! lines.grep(/foo = FOO;/).empty?
99 assert ! lines.grep(/nr = 50;/).empty?
102 def test_emit_ip_addr_string
103 emitter = LWES::Emitter.new(@options)
104 event = { :string_ip => [ :ip_addr, "192.168.1.1" ] }
105 out = lwes_listener do
106 assert_nothing_raised { emitter.emit("STRING_IP", event) }
108 lines = out.readlines
109 assert_equal 1, lines.grep(/string_ip = 192.168.1.1/).size
112 def test_emit_ip_addr_int
113 emitter = LWES::Emitter.new(@options)
114 event = { :int_ip => [ :ip_addr, IPAddr.new("192.168.1.1").to_i ] }
115 out = lwes_listener do
116 assert_nothing_raised { emitter.emit("INT_IP", event) }
118 lines = out.readlines
119 assert_equal 1, lines.grep(/int_ip = 192.168.1.1/).size
122 def TODO_emit_ip_addr_object
123 emitter = LWES::Emitter.new(@options)
124 event = { :ip => IPAddr.new("192.168.1.1") }
125 out = lwes_listener do
126 assert_nothing_raised { emitter.emit("IP", event) }
128 lines = out.readlines
129 assert_equal 1, lines.grep(/\bip = 192.168.1.1/).size
132 def test_emit_invalid
133 emitter = LWES::Emitter.new(@options)
134 assert_raises(ArgumentError) { emitter.emit("JUNK", :junk => %r{junk}) }
137 def test_emit_booleans
138 emitter = LWES::Emitter.new(@options)
139 event = { :true => true, :false => false }
140 out = lwes_listener do
141 assert_nothing_raised { emitter.emit("BOOLS", event)
144 lines = out.readlines
145 assert_equal 1, lines.grep(/true = true;/).size
146 assert_equal 1, lines.grep(/false = false;/).size
149 def test_emit_numeric_ranges
150 check_min_max(:int16, -0x7fff - 1, 0x7fff)
151 check_min_max(:int32, -0x7fffffff - 1, 0x7fffffff)
152 check_min_max(:int64, -0x7fffffffffffffff - 1, 0x7fffffffffffffff)
153 check_min_max(:uint16, 0, 0xffff)
154 check_min_max(:uint32, 0, 0xffffffff)
155 check_min_max(:uint64, 0, 0xffffffffffffffff)
156 huge = 0xffffffffffffffffffffffffffffffff
157 tiny = -0xffffffffffffffffffffffffffffffff
158 [ :int16, :int32, :int64, :uint16, :uint32, :uint64 ].each do |type|
159 emitter = LWES::Emitter.new(@options)
160 assert_raises(RangeError) {
161 emitter.emit("way over", { type => [ type, huge ] })
163 assert_raises(RangeError) {
164 emitter.emit("way under", { type => [ type, tiny ] })
169 def check_min_max(type, min, max)
170 emitter = LWES::Emitter.new(@options)
171 out = lwes_listener do
172 assert_raises(RangeError) {
173 emitter.emit("over", { type => [ type, max + 1] })
175 assert_raises(RangeError, "type=#{type} min=#{min}") {
176 emitter.emit("under", { type => [ type, min - 1 ] })
178 assert_nothing_raised {
179 emitter.emit("zero", { type => [ type, 0 ] })
181 assert_nothing_raised {
182 emitter.emit("min", { type => [ type, min ] })
184 assert_nothing_raised {
185 emitter.emit("max", { type => [ type, max ] })
188 lines = out.readlines
189 assert lines.grep(/\Aover/).empty?
190 assert lines.grep(/\Aunder/).empty?
191 assert_equal 1, lines.grep(/\Amax/).size
192 assert_equal 1, lines.grep(/\Amin/).size
193 assert_equal 1, lines.grep(/\Azero/).size
197 emitter = LWES::Emitter.new(@options)
198 assert_nil emitter.close