2001-11-26 07:00:40 -05:00
|
|
|
# Please only run this test on machines reasonable for testing.
|
|
|
|
# If in doubt, ask your admin.
|
|
|
|
|
2002-11-27 02:14:00 -05:00
|
|
|
require 'test/unit'
|
2011-01-22 10:46:11 -05:00
|
|
|
|
|
|
|
begin
|
|
|
|
require 'syslog'
|
|
|
|
rescue LoadError
|
|
|
|
# suppress error messages.
|
|
|
|
end
|
2001-11-26 07:00:40 -05:00
|
|
|
|
2002-11-27 02:14:00 -05:00
|
|
|
class TestSyslog < Test::Unit::TestCase
|
2002-02-25 07:13:30 -05:00
|
|
|
def test_new
|
2002-11-27 02:14:00 -05:00
|
|
|
assert_raises(NoMethodError) {
|
2001-11-26 07:00:40 -05:00
|
|
|
Syslog.new
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2002-02-25 07:13:30 -05:00
|
|
|
def test_instance
|
2001-11-26 07:00:40 -05:00
|
|
|
sl1 = Syslog.instance
|
|
|
|
sl2 = Syslog.open
|
|
|
|
sl3 = Syslog.instance
|
|
|
|
|
2002-02-25 07:13:30 -05:00
|
|
|
assert_equal(Syslog, sl1)
|
|
|
|
assert_equal(Syslog, sl2)
|
|
|
|
assert_equal(Syslog, sl3)
|
2001-11-26 07:00:40 -05:00
|
|
|
ensure
|
2002-11-27 03:36:22 -05:00
|
|
|
Syslog.close if Syslog.opened?
|
2001-11-26 07:00:40 -05:00
|
|
|
end
|
|
|
|
|
2002-02-25 07:13:30 -05:00
|
|
|
def test_open
|
2001-11-26 07:00:40 -05:00
|
|
|
# default parameters
|
2002-02-25 07:13:30 -05:00
|
|
|
Syslog.open
|
2001-11-26 07:00:40 -05:00
|
|
|
|
2002-02-25 07:13:30 -05:00
|
|
|
assert_equal($0, Syslog.ident)
|
|
|
|
assert_equal(Syslog::LOG_PID | Syslog::LOG_CONS, Syslog.options)
|
|
|
|
assert_equal(Syslog::LOG_USER, Syslog.facility)
|
2001-11-26 07:00:40 -05:00
|
|
|
|
|
|
|
# open without close
|
2002-11-27 02:14:00 -05:00
|
|
|
assert_raises(RuntimeError) {
|
2002-02-25 07:13:30 -05:00
|
|
|
Syslog.open
|
2001-11-26 07:00:40 -05:00
|
|
|
}
|
|
|
|
|
2002-02-25 07:13:30 -05:00
|
|
|
Syslog.close
|
2001-11-26 07:00:40 -05:00
|
|
|
|
|
|
|
# given parameters
|
2011-03-25 07:06:56 -04:00
|
|
|
options = Syslog::LOG_NDELAY | Syslog::LOG_PID
|
|
|
|
Syslog.open("foo", options, Syslog::LOG_DAEMON)
|
2001-11-26 07:00:40 -05:00
|
|
|
|
2002-02-25 07:13:30 -05:00
|
|
|
assert_equal('foo', Syslog.ident)
|
2011-03-25 07:06:56 -04:00
|
|
|
assert_equal(options, Syslog.options)
|
2002-02-25 07:13:30 -05:00
|
|
|
assert_equal(Syslog::LOG_DAEMON, Syslog.facility)
|
2001-11-26 07:00:40 -05:00
|
|
|
|
2002-02-25 07:13:30 -05:00
|
|
|
Syslog.close
|
2001-11-26 07:00:40 -05:00
|
|
|
|
|
|
|
# default parameters again (after close)
|
2002-02-25 07:13:30 -05:00
|
|
|
Syslog.open
|
|
|
|
Syslog.close
|
2001-11-26 07:00:40 -05:00
|
|
|
|
2002-11-27 03:36:22 -05:00
|
|
|
assert_equal(nil, Syslog.ident)
|
|
|
|
assert_equal(nil, Syslog.options)
|
|
|
|
assert_equal(nil, Syslog.facility)
|
2001-11-26 07:00:40 -05:00
|
|
|
|
|
|
|
# block
|
2009-03-18 04:01:48 -04:00
|
|
|
param = nil
|
|
|
|
Syslog.open { |syslog|
|
|
|
|
param = syslog
|
2009-03-18 03:40:26 -04:00
|
|
|
}
|
2009-03-18 04:01:48 -04:00
|
|
|
assert_equal(Syslog, param)
|
2001-11-26 07:00:40 -05:00
|
|
|
ensure
|
2002-11-27 03:36:22 -05:00
|
|
|
Syslog.close if Syslog.opened?
|
2001-11-26 07:00:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_opened?
|
2002-02-25 07:13:30 -05:00
|
|
|
assert_equal(false, Syslog.opened?)
|
2001-11-26 07:00:40 -05:00
|
|
|
|
2002-02-25 07:13:30 -05:00
|
|
|
Syslog.open
|
|
|
|
assert_equal(true, Syslog.opened?)
|
2001-11-26 07:00:40 -05:00
|
|
|
|
2002-02-25 07:13:30 -05:00
|
|
|
Syslog.close
|
|
|
|
assert_equal(false, Syslog.opened?)
|
2001-11-26 07:00:40 -05:00
|
|
|
|
2002-02-25 07:13:30 -05:00
|
|
|
Syslog.open {
|
|
|
|
assert_equal(true, Syslog.opened?)
|
2001-11-26 07:00:40 -05:00
|
|
|
}
|
|
|
|
|
2002-02-25 07:13:30 -05:00
|
|
|
assert_equal(false, Syslog.opened?)
|
2001-11-26 07:00:40 -05:00
|
|
|
end
|
|
|
|
|
2002-11-27 03:36:22 -05:00
|
|
|
def test_close
|
|
|
|
assert_raises(RuntimeError) {
|
|
|
|
Syslog.close
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2001-11-26 07:00:40 -05:00
|
|
|
def test_mask
|
2002-11-27 03:36:22 -05:00
|
|
|
assert_equal(nil, Syslog.mask)
|
|
|
|
|
2002-02-25 07:13:30 -05:00
|
|
|
Syslog.open
|
2001-11-26 07:00:40 -05:00
|
|
|
|
2002-02-25 07:13:30 -05:00
|
|
|
orig = Syslog.mask
|
2001-11-26 07:00:40 -05:00
|
|
|
|
2002-02-25 07:13:30 -05:00
|
|
|
Syslog.mask = Syslog.LOG_UPTO(Syslog::LOG_ERR)
|
|
|
|
assert_equal(Syslog.LOG_UPTO(Syslog::LOG_ERR), Syslog.mask)
|
2001-11-26 07:00:40 -05:00
|
|
|
|
2002-02-25 07:13:30 -05:00
|
|
|
Syslog.mask = Syslog.LOG_MASK(Syslog::LOG_CRIT)
|
|
|
|
assert_equal(Syslog.LOG_MASK(Syslog::LOG_CRIT), Syslog.mask)
|
2001-11-26 07:00:40 -05:00
|
|
|
|
2002-02-25 07:13:30 -05:00
|
|
|
Syslog.mask = orig
|
2001-11-26 07:00:40 -05:00
|
|
|
ensure
|
2002-11-27 03:36:22 -05:00
|
|
|
Syslog.close if Syslog.opened?
|
2001-11-26 07:00:40 -05:00
|
|
|
end
|
|
|
|
|
2011-08-05 04:26:42 -04:00
|
|
|
def syslog_line_regex(ident, message)
|
|
|
|
/(?:^| )#{Regexp.quote(ident)}(?:\[([1-9][0-9]*)\])?(?: |[: ].* )#{Regexp.quote(message)}$/
|
|
|
|
end
|
|
|
|
|
2001-11-26 07:00:40 -05:00
|
|
|
def test_log
|
2014-05-29 10:59:24 -04:00
|
|
|
IO.pipe {|stderr|
|
|
|
|
pid = fork {
|
|
|
|
stderr[0].close
|
|
|
|
STDERR.reopen(stderr[1])
|
|
|
|
stderr[1].close
|
|
|
|
|
|
|
|
options = Syslog::LOG_PERROR | Syslog::LOG_NDELAY
|
|
|
|
|
|
|
|
Syslog.open("syslog_test", options) { |sl|
|
|
|
|
sl.log(Syslog::LOG_NOTICE, "test1 - hello, %s!", "world")
|
|
|
|
sl.notice("test1 - hello, %s!", "world")
|
|
|
|
}
|
|
|
|
|
|
|
|
Syslog.open("syslog_test", options | Syslog::LOG_PID) { |sl|
|
|
|
|
sl.log(Syslog::LOG_CRIT, "test2 - pid")
|
|
|
|
sl.crit("test2 - pid")
|
|
|
|
}
|
|
|
|
exit!
|
2001-11-26 07:00:40 -05:00
|
|
|
}
|
|
|
|
|
2014-05-29 10:59:24 -04:00
|
|
|
stderr[1].close
|
|
|
|
Process.waitpid(pid)
|
|
|
|
|
|
|
|
# LOG_PERROR is not implemented on Cygwin or Solaris. Only test
|
|
|
|
# these on systems that define it.
|
|
|
|
return unless Syslog.const_defined?(:LOG_PERROR)
|
|
|
|
|
|
|
|
2.times {
|
|
|
|
re = syslog_line_regex("syslog_test", "test1 - hello, world!")
|
|
|
|
line = stderr[0].gets
|
2014-10-04 03:35:43 -04:00
|
|
|
# In AIX, each LOG_PERROR output line has an appended empty line.
|
|
|
|
if /aix/ =~ RUBY_PLATFORM && line =~ /^$/
|
|
|
|
line = stderr[0].gets
|
|
|
|
end
|
2014-05-29 10:59:24 -04:00
|
|
|
m = re.match(line)
|
|
|
|
assert_not_nil(m)
|
|
|
|
if m[1]
|
|
|
|
# pid is written regardless of LOG_PID on OS X 10.7+
|
|
|
|
assert_equal(pid, m[1].to_i)
|
|
|
|
end
|
2001-11-26 07:00:40 -05:00
|
|
|
}
|
|
|
|
|
2014-05-29 10:59:24 -04:00
|
|
|
2.times {
|
|
|
|
re = syslog_line_regex("syslog_test", "test2 - pid")
|
|
|
|
line = stderr[0].gets
|
2014-10-04 03:35:43 -04:00
|
|
|
# In AIX, each LOG_PERROR output line has an appended empty line.
|
|
|
|
if /aix/ =~ RUBY_PLATFORM && line =~ /^$/
|
|
|
|
line = stderr[0].gets
|
|
|
|
end
|
2014-05-29 10:59:24 -04:00
|
|
|
m = re.match(line)
|
|
|
|
assert_not_nil(m)
|
|
|
|
assert_not_nil(m[1])
|
2011-08-05 04:26:42 -04:00
|
|
|
assert_equal(pid, m[1].to_i)
|
2014-05-29 10:59:24 -04:00
|
|
|
}
|
2001-11-26 07:00:40 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_inspect
|
|
|
|
Syslog.open { |sl|
|
2002-11-27 03:36:22 -05:00
|
|
|
assert_equal(format('<#%s: opened=true, ident="%s", options=%d, facility=%d, mask=%d>',
|
2014-02-27 07:52:16 -05:00
|
|
|
Syslog,
|
|
|
|
sl.ident,
|
|
|
|
sl.options,
|
|
|
|
sl.facility,
|
|
|
|
sl.mask),
|
|
|
|
sl.inspect)
|
2001-11-26 07:00:40 -05:00
|
|
|
}
|
2002-11-27 03:36:22 -05:00
|
|
|
|
|
|
|
assert_equal(format('<#%s: opened=false>', Syslog), Syslog.inspect)
|
2001-11-26 07:00:40 -05:00
|
|
|
end
|
2011-01-22 10:46:11 -05:00
|
|
|
end if defined?(Syslog)
|