1 require "#{File.expand_path(File.dirname(__FILE__))}/../test_helper"
4 class TestEmitter < Test::Unit::TestCase
6 assert_instance_of Module, LWES, "LWES is not a module"
7 assert_instance_of Class, LWES::Emitter, "LWES::Emitter is not a class"
11 @options = LISTENER_DEFAULTS.dup
15 @options.delete(:iface)
16 assert_instance_of LWES::Emitter, LWES::Emitter.new(@options)
20 assert_instance_of LWES::Emitter, LWES::Emitter.new(@options)
24 orig = LWES::Emitter.new(@options)
26 assert_instance_of LWES::Emitter, duped
27 assert duped.object_id != orig.object_id
31 orig = LWES::Emitter.new(@options)
33 assert_instance_of LWES::Emitter, cloned
34 assert cloned.object_id != orig.object_id
37 def test_initialize_with_heartbeat
38 l = LWES::Listener.new(@options)
39 heartbeat = @options.merge(:heartbeat => 30)
40 e = LWES::Emitter.new(heartbeat)
41 assert_instance_of LWES::Emitter, e
43 assert_instance_of LWES::Event, l.recv
47 def test_initialize_no_ttl
50 assert_instance_of LWES::Emitter, LWES::Emitter.new(no_ttl)
53 def test_initialize_invalid
54 assert_raises(TypeError) {
55 LWES::Emitter.new(@options.merge(:address => nil))
59 def test_initialize_empty_options
60 assert_raises(TypeError) { LWES::Emitter.new({}) }
64 emitter = LWES::Emitter.new(@options)
65 assert_raises(TypeError) { emitter.emit "Invalid", nil }
66 assert_raises(ArgumentError) { emitter.emit nil, { :hello => "world" }}
69 def test_emit_empty_hash
70 emitter = LWES::Emitter.new(@options)
71 assert_nothing_raised { emitter.emit("Valid", Hash.new) }
74 def test_emit_non_empty_hashes
75 emitter = LWES::Emitter.new(@options)
76 out = lwes_listener do
77 assert_nothing_raised {
78 emitter.emit("ASDF", { :foo => "FOO", :nr => [ :int16, 50 ] })
82 assert_match %r{\AASDF\b}, lines.first
83 assert ! lines.grep(/foo = FOO;/).empty?
84 assert ! lines.grep(/nr = 50;/).empty?
87 def test_emit_ip_addr_string
88 emitter = LWES::Emitter.new(@options)
89 event = { :string_ip => [ :ip_addr, "192.168.1.1" ] }
90 out = lwes_listener do
91 assert_nothing_raised { emitter.emit("STRING_IP", event) }
94 assert_equal 1, lines.grep(/string_ip = 192.168.1.1/).size
97 def test_emit_ip_addr_int
98 emitter = LWES::Emitter.new(@options)
99 event = { :int_ip => [ :ip_addr, IPAddr.new("192.168.1.1").to_i ] }
100 out = lwes_listener do
101 assert_nothing_raised { emitter.emit("INT_IP", event) }
103 lines = out.readlines
104 assert_equal 1, lines.grep(/int_ip = 192.168.1.1/).size
108 emitter = LWES::Emitter.new(@options)
109 assert_raises(ArgumentError) { emitter.emit("JUNK", :junk => %r{junk}) }
112 def test_emit_booleans
113 emitter = LWES::Emitter.new(@options)
114 event = { :true => true, :false => false }
115 out = lwes_listener do
116 assert_nothing_raised { emitter.emit("BOOLS", event)
119 lines = out.readlines
120 assert_equal 1, lines.grep(/true = true;/).size
121 assert_equal 1, lines.grep(/false = false;/).size
124 def test_emit_numeric_ranges
125 check_min_max(:int16, -0x7fff - 1, 0x7fff)
126 check_min_max(:int32, -0x7fffffff - 1, 0x7fffffff)
127 check_min_max(:int64, -0x7fffffffffffffff - 1, 0x7fffffffffffffff)
128 check_min_max(:uint16, 0, 0xffff)
129 check_min_max(:uint32, 0, 0xffffffff)
130 check_min_max(:uint64, 0, 0xffffffffffffffff)
131 huge = 0xffffffffffffffffffffffffffffffff
132 tiny = -0xffffffffffffffffffffffffffffffff
133 [ :int16, :int32, :int64, :uint16, :uint32, :uint64 ].each do |type|
134 emitter = LWES::Emitter.new(@options)
135 assert_raises(RangeError) {
136 emitter.emit("way over", { type => [ type, huge ] })
138 assert_raises(RangeError) {
139 emitter.emit("way under", { type => [ type, tiny ] })
144 def check_min_max(type, min, max)
145 emitter = LWES::Emitter.new(@options)
146 out = lwes_listener do
147 assert_raises(RangeError) {
148 emitter.emit("over", { type => [ type, max + 1] })
150 assert_raises(RangeError, "type=#{type} min=#{min}") {
151 emitter.emit("under", { type => [ type, min - 1 ] })
153 assert_nothing_raised {
154 emitter.emit("zero", { type => [ type, 0 ] })
156 assert_nothing_raised {
157 emitter.emit("min", { type => [ type, min ] })
159 assert_nothing_raised {
160 emitter.emit("max", { type => [ type, max ] })
163 lines = out.readlines
164 assert lines.grep(/\Aover/).empty?
165 assert lines.grep(/\Aunder/).empty?
166 assert_equal 1, lines.grep(/\Amax/).size
167 assert_equal 1, lines.grep(/\Amin/).size
168 assert_equal 1, lines.grep(/\Azero/).size
172 emitter = LWES::Emitter.new(@options)
173 assert_nothing_raised {
174 emitter.emit("Foo", { :uint64 => [ :uint64, 10_000_000_000 ] })
178 def test_emit_enc_sym
179 emitter = LWES::Emitter.new(@options)
185 out = lwes_listener do
186 assert_nothing_raised { emitter.emit("EncTest", tmp) }
188 lines = out.readlines
189 assert_equal 1, lines.grep(/\A\tenc = 1;\n\z/).size
190 assert_equal 1, lines.grep(/\A\ta = b;\n\z/).size
191 assert_equal 1, lines.grep(/\A\tc = d;\n\z/).size
194 def test_emit_enc_str
195 emitter = LWES::Emitter.new(@options)
201 out = lwes_listener do
202 assert_nothing_raised { emitter.emit("EncTest", tmp) }
204 lines = out.readlines
205 assert_equal 1, lines.grep(/\A\tenc = 1;\n\z/).size
206 assert_equal 1, lines.grep(/\A\ta = b;\n\z/).size
207 assert_equal 1, lines.grep(/\A\tc = d;\n\z/).size
210 def test_emit_huge_event
211 emitter = LWES::Emitter.new(@options)
213 :a => ("b" * (1024 * 1024)),
215 assert_raises(ArgumentError) do
216 emitter.emit("BigTest", tmp)
221 emitter = LWES::Emitter.new(@options)
222 assert_nil emitter.close