2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2006-05-01 05:05:08 -04:00
|
|
|
begin
|
|
|
|
require 'win32ole'
|
2006-05-06 19:40:08 -04:00
|
|
|
rescue LoadError
|
2006-05-01 05:05:08 -04:00
|
|
|
end
|
|
|
|
require 'test/unit'
|
2006-05-06 19:40:08 -04:00
|
|
|
|
2008-11-02 08:22:42 -05:00
|
|
|
def ado_installed?
|
|
|
|
installed = false
|
|
|
|
if defined?(WIN32OLE)
|
|
|
|
db = nil
|
|
|
|
begin
|
|
|
|
db = WIN32OLE.new('ADODB.Connection')
|
|
|
|
db.connectionString = "Driver={Microsoft Text Driver (*.txt; *.csv)};DefaultDir=.;"
|
|
|
|
db.open
|
|
|
|
db.close
|
|
|
|
db = nil
|
|
|
|
installed = true
|
|
|
|
rescue
|
2007-02-08 08:00:06 -05:00
|
|
|
end
|
2008-11-02 08:22:42 -05:00
|
|
|
end
|
|
|
|
installed
|
|
|
|
end
|
2007-02-08 08:00:06 -05:00
|
|
|
|
2015-09-21 07:53:41 -04:00
|
|
|
def swbemsink_available?
|
2014-12-20 20:56:04 -05:00
|
|
|
available = false
|
|
|
|
if defined?(WIN32OLE)
|
|
|
|
wmi = nil
|
|
|
|
begin
|
|
|
|
wmi = WIN32OLE.new('WbemScripting.SWbemSink')
|
|
|
|
available = true
|
|
|
|
rescue
|
|
|
|
end
|
|
|
|
end
|
|
|
|
available
|
|
|
|
end
|
|
|
|
|
2008-11-02 08:22:42 -05:00
|
|
|
if defined?(WIN32OLE_EVENT)
|
2012-07-17 20:36:01 -04:00
|
|
|
class TestWIN32OLE_EVENT < Test::Unit::TestCase
|
2014-12-20 20:56:04 -05:00
|
|
|
def test_s_new_exception
|
|
|
|
assert_raise(TypeError) {
|
|
|
|
WIN32OLE_EVENT.new("A")
|
|
|
|
}
|
|
|
|
end
|
2014-12-22 07:31:45 -05:00
|
|
|
def test_s_new_non_exist_event
|
|
|
|
dict = WIN32OLE.new('Scripting.Dictionary')
|
|
|
|
assert_raise(RuntimeError) {
|
|
|
|
WIN32OLE_EVENT.new(dict)
|
|
|
|
}
|
|
|
|
end
|
2014-12-20 20:56:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class TestWIN32OLE_EVENT_SWbemSink < Test::Unit::TestCase
|
2015-09-21 07:53:41 -04:00
|
|
|
unless swbemsink_available?
|
2014-12-20 20:56:04 -05:00
|
|
|
def test_dummy_for_skip_message
|
|
|
|
skip "'WbemScripting.SWbemSink' is not available"
|
|
|
|
end
|
|
|
|
else
|
2014-12-22 07:31:45 -05:00
|
|
|
def setup
|
|
|
|
@wmi = WIN32OLE.connect('winmgmts://localhost/root/cimv2')
|
|
|
|
@sws = WIN32OLE.new('WbemScripting.SWbemSink')
|
|
|
|
@event = @event1 = @event2 = ""
|
|
|
|
@sql = "SELECT * FROM __InstanceModificationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_LocalTime'"
|
|
|
|
end
|
|
|
|
|
|
|
|
def message_loop
|
2014-12-22 07:31:59 -05:00
|
|
|
2.times do
|
2014-12-22 07:31:45 -05:00
|
|
|
WIN32OLE_EVENT.message_loop
|
|
|
|
sleep 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_handler(event, *args)
|
|
|
|
@event += event
|
|
|
|
end
|
|
|
|
|
|
|
|
def handler1
|
|
|
|
@event1 = "handler1"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_s_new_non_exist_event
|
|
|
|
assert_raise(RuntimeError) {
|
|
|
|
WIN32OLE_EVENT.new(@sws, 'XXXXX')
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_s_new
|
|
|
|
obj = WIN32OLE_EVENT.new(@sws, 'ISWbemSinkEvents')
|
|
|
|
assert_instance_of(WIN32OLE_EVENT, obj)
|
|
|
|
obj = WIN32OLE_EVENT.new(@sws)
|
|
|
|
assert_instance_of(WIN32OLE_EVENT, obj)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_s_new_loop
|
2015-08-30 02:46:16 -04:00
|
|
|
exec_notification_query_async
|
2014-12-22 07:31:45 -05:00
|
|
|
ev = WIN32OLE_EVENT.new(@sws)
|
|
|
|
ev.on_event {|*args| default_handler(*args)}
|
|
|
|
message_loop
|
|
|
|
10.times do |i|
|
|
|
|
WIN32OLE_EVENT.new(@sws)
|
|
|
|
message_loop
|
|
|
|
GC.start
|
|
|
|
end
|
|
|
|
assert_match(/OnObjectReady/, @event)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_on_event
|
2015-08-30 02:46:16 -04:00
|
|
|
exec_notification_query_async
|
2014-12-22 07:31:45 -05:00
|
|
|
ev = WIN32OLE_EVENT.new(@sws, 'ISWbemSinkEvents')
|
|
|
|
ev.on_event {|*args| default_handler(*args)}
|
|
|
|
message_loop
|
|
|
|
assert_match(/OnObjectReady/, @event)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_on_event_symbol
|
2015-08-30 02:46:16 -04:00
|
|
|
exec_notification_query_async
|
2014-12-22 07:31:45 -05:00
|
|
|
ev = WIN32OLE_EVENT.new(@sws)
|
|
|
|
ev.on_event(:OnObjectReady) {|*args|
|
|
|
|
handler1
|
|
|
|
}
|
|
|
|
message_loop
|
|
|
|
assert_equal("handler1", @event1)
|
|
|
|
end
|
|
|
|
|
2015-08-30 02:46:16 -04:00
|
|
|
private
|
|
|
|
def exec_notification_query_async
|
|
|
|
@wmi.ExecNotificationQueryAsync(@sws, @sql)
|
|
|
|
rescue => e
|
2015-08-30 03:15:52 -04:00
|
|
|
if /OLE error code:80041008 in SWbemServicesEx/ =~ e.message
|
|
|
|
skip "No administrator privilege?"
|
|
|
|
end
|
2015-08-30 02:46:16 -04:00
|
|
|
raise
|
|
|
|
end
|
2014-12-20 20:56:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestWIN32OLE_EVENT_ADO < Test::Unit::TestCase
|
2012-07-17 20:36:01 -04:00
|
|
|
unless ado_installed?
|
|
|
|
def test_dummy_for_skip_message
|
|
|
|
skip "ActiveX Data Object Library not found"
|
|
|
|
end
|
|
|
|
else
|
2008-11-02 08:22:42 -05:00
|
|
|
CONNSTR="Driver={Microsoft Text Driver (*.txt; *.csv)};DefaultDir=.;"
|
|
|
|
module ADO
|
|
|
|
end
|
|
|
|
def message_loop
|
|
|
|
WIN32OLE_EVENT.message_loop
|
|
|
|
end
|
2008-07-25 07:49:28 -04:00
|
|
|
|
2008-11-02 08:22:42 -05:00
|
|
|
def default_handler(event, *args)
|
|
|
|
@event += event
|
2008-07-26 08:27:57 -04:00
|
|
|
end
|
|
|
|
|
2008-11-02 08:22:42 -05:00
|
|
|
def setup
|
|
|
|
@db = WIN32OLE.new('ADODB.Connection')
|
|
|
|
if !defined?(ADO::AdStateOpen)
|
|
|
|
WIN32OLE.const_load(@db, ADO)
|
|
|
|
end
|
|
|
|
@db.connectionString = CONNSTR
|
|
|
|
@event = ""
|
|
|
|
@event2 = ""
|
|
|
|
@event3 = ""
|
|
|
|
end
|
2006-05-01 05:05:08 -04:00
|
|
|
|
2008-11-02 08:22:42 -05:00
|
|
|
def test_on_event2
|
|
|
|
ev = WIN32OLE_EVENT.new(@db, 'ConnectionEvents')
|
|
|
|
ev.on_event('WillConnect') {|*args| handler1}
|
|
|
|
ev.on_event('WillConnect') {|*args| handler2}
|
|
|
|
@db.open
|
|
|
|
message_loop
|
|
|
|
assert_equal("handler2", @event2)
|
|
|
|
end
|
2006-05-01 05:05:08 -04:00
|
|
|
|
2008-11-02 08:22:42 -05:00
|
|
|
def test_on_event4
|
|
|
|
ev = WIN32OLE_EVENT.new(@db, 'ConnectionEvents')
|
|
|
|
ev.on_event{|*args| handler1}
|
|
|
|
ev.on_event{|*args| handler2}
|
|
|
|
ev.on_event('WillConnect'){|*args| handler3(*args)}
|
|
|
|
@db.open
|
|
|
|
message_loop
|
|
|
|
assert_equal(CONNSTR, @event3)
|
|
|
|
assert("handler2", @event2)
|
|
|
|
end
|
2006-05-01 05:05:08 -04:00
|
|
|
|
2008-11-02 08:22:42 -05:00
|
|
|
def test_on_event5
|
|
|
|
ev = WIN32OLE_EVENT.new(@db, 'ConnectionEvents')
|
2007-09-04 07:48:53 -04:00
|
|
|
ev.on_event {|*args| default_handler(*args)}
|
2008-11-02 08:22:42 -05:00
|
|
|
ev.on_event('WillConnect'){|*args| handler3(*args)}
|
|
|
|
@db.open
|
|
|
|
message_loop
|
|
|
|
assert_match(/ConnectComplete/, @event)
|
|
|
|
assert(/WillConnect/ !~ @event)
|
|
|
|
assert_equal(CONNSTR, @event3)
|
|
|
|
end
|
2008-07-08 10:05:38 -04:00
|
|
|
|
2008-11-02 08:22:42 -05:00
|
|
|
def test_unadvise
|
|
|
|
ev = WIN32OLE_EVENT.new(@db, 'ConnectionEvents')
|
|
|
|
ev.on_event {|*args| default_handler(*args)}
|
|
|
|
@db.open
|
|
|
|
message_loop
|
|
|
|
assert_match(/WillConnect/, @event)
|
|
|
|
ev.unadvise
|
|
|
|
@event = ""
|
|
|
|
@db.close
|
|
|
|
@db.open
|
|
|
|
message_loop
|
|
|
|
assert_equal("", @event);
|
|
|
|
assert_raise(WIN32OLERuntimeError) {
|
|
|
|
ev.on_event {|*args| default_handler(*args)}
|
|
|
|
}
|
|
|
|
end
|
2008-07-11 20:31:32 -04:00
|
|
|
|
2008-07-13 08:36:26 -04:00
|
|
|
|
2008-11-02 08:22:42 -05:00
|
|
|
def test_on_event_with_outargs
|
|
|
|
ev = WIN32OLE_EVENT.new(@db)
|
|
|
|
@db.connectionString = 'XXX' # set illegal connection string
|
|
|
|
assert_raise(WIN32OLERuntimeError) {
|
|
|
|
@db.open
|
|
|
|
}
|
|
|
|
ev.on_event_with_outargs('WillConnect'){|*args|
|
|
|
|
args.last[0] = CONNSTR # ConnectionString = CONNSTR
|
|
|
|
}
|
|
|
|
@db.open
|
|
|
|
message_loop
|
|
|
|
assert(true)
|
|
|
|
end
|
2008-07-13 08:36:26 -04:00
|
|
|
|
2008-11-02 08:22:42 -05:00
|
|
|
def test_on_event_hash_return
|
|
|
|
ev = WIN32OLE_EVENT.new(@db)
|
|
|
|
ev.on_event('WillConnect'){|*args|
|
|
|
|
{:return => 1, :ConnectionString => CONNSTR}
|
|
|
|
}
|
2009-03-05 22:56:38 -05:00
|
|
|
@db.connectionString = 'XXX'
|
2008-11-02 08:22:42 -05:00
|
|
|
@db.open
|
|
|
|
assert(true)
|
|
|
|
end
|
2008-07-13 08:36:26 -04:00
|
|
|
|
2008-11-02 08:22:42 -05:00
|
|
|
def test_on_event_hash_return2
|
|
|
|
ev = WIN32OLE_EVENT.new(@db)
|
|
|
|
ev.on_event('WillConnect'){|*args|
|
|
|
|
{:ConnectionString => CONNSTR}
|
|
|
|
}
|
2009-03-05 22:56:38 -05:00
|
|
|
@db.connectionString = 'XXX'
|
2008-11-02 08:22:42 -05:00
|
|
|
@db.open
|
|
|
|
assert(true)
|
|
|
|
end
|
2008-07-13 08:36:26 -04:00
|
|
|
|
2008-11-02 08:22:42 -05:00
|
|
|
def test_on_event_hash_return3
|
|
|
|
ev = WIN32OLE_EVENT.new(@db)
|
|
|
|
ev.on_event('WillConnect'){|*args|
|
|
|
|
{'ConnectionString' => CONNSTR}
|
|
|
|
}
|
2009-03-05 22:56:38 -05:00
|
|
|
@db.connectionString = 'XXX'
|
2008-11-02 08:22:42 -05:00
|
|
|
@db.open
|
|
|
|
assert(true)
|
|
|
|
end
|
2008-07-25 07:49:28 -04:00
|
|
|
|
2008-11-02 08:22:42 -05:00
|
|
|
def test_on_event_hash_return4
|
|
|
|
ev = WIN32OLE_EVENT.new(@db)
|
|
|
|
ev.on_event('WillConnect'){|*args|
|
|
|
|
{'return' => 1, 'ConnectionString' => CONNSTR}
|
|
|
|
}
|
2009-03-05 22:56:38 -05:00
|
|
|
@db.connectionString = 'XXX'
|
2008-11-02 08:22:42 -05:00
|
|
|
@db.open
|
|
|
|
assert(true)
|
|
|
|
end
|
2008-07-25 07:49:28 -04:00
|
|
|
|
2008-11-02 08:22:42 -05:00
|
|
|
def test_on_event_hash_return5
|
|
|
|
ev = WIN32OLE_EVENT.new(@db)
|
|
|
|
ev.on_event('WillConnect'){|*args|
|
|
|
|
{0 => CONNSTR}
|
|
|
|
}
|
2009-03-05 22:56:38 -05:00
|
|
|
@db.connectionString = 'XXX'
|
2008-11-02 08:22:42 -05:00
|
|
|
@db.open
|
|
|
|
assert(true)
|
|
|
|
end
|
2008-08-03 06:43:44 -04:00
|
|
|
|
2008-11-02 08:22:42 -05:00
|
|
|
def test_off_event
|
|
|
|
ev = WIN32OLE_EVENT.new(@db)
|
|
|
|
ev.on_event{handler1}
|
|
|
|
ev.off_event
|
|
|
|
@db.open
|
|
|
|
message_loop
|
|
|
|
assert_equal("", @event2)
|
|
|
|
end
|
2006-05-01 05:05:08 -04:00
|
|
|
|
2008-11-02 08:22:42 -05:00
|
|
|
def test_off_event_arg
|
|
|
|
ev = WIN32OLE_EVENT.new(@db)
|
|
|
|
ev.on_event('WillConnect'){handler1}
|
|
|
|
ev.off_event('WillConnect')
|
|
|
|
@db.open
|
|
|
|
message_loop
|
|
|
|
assert_equal("", @event2)
|
|
|
|
end
|
2006-05-01 05:05:08 -04:00
|
|
|
|
2008-11-02 08:22:42 -05:00
|
|
|
def test_off_event_arg2
|
|
|
|
ev = WIN32OLE_EVENT.new(@db)
|
|
|
|
ev.on_event('WillConnect'){handler1}
|
|
|
|
ev.on_event('ConnectComplete'){handler1}
|
|
|
|
ev.off_event('WillConnect')
|
|
|
|
@db.open
|
|
|
|
message_loop
|
|
|
|
assert_equal("handler1", @event2)
|
|
|
|
end
|
2006-05-01 05:05:08 -04:00
|
|
|
|
2008-11-02 08:22:42 -05:00
|
|
|
def test_off_event_sym_arg
|
|
|
|
ev = WIN32OLE_EVENT.new(@db)
|
|
|
|
ev.on_event('WillConnect'){handler1}
|
|
|
|
ev.off_event(:WillConnect)
|
|
|
|
@db.open
|
2008-07-25 07:49:28 -04:00
|
|
|
message_loop
|
2008-11-02 08:22:42 -05:00
|
|
|
assert_equal("", @event2)
|
2008-07-13 08:36:26 -04:00
|
|
|
end
|
2008-07-26 08:27:57 -04:00
|
|
|
|
2008-11-02 08:22:42 -05:00
|
|
|
def handler1
|
|
|
|
@event2 = "handler1"
|
2008-07-26 08:27:57 -04:00
|
|
|
end
|
2008-11-02 08:22:42 -05:00
|
|
|
|
|
|
|
def handler2
|
|
|
|
@event2 = "handler2"
|
2008-07-26 08:27:57 -04:00
|
|
|
end
|
2008-11-02 08:22:42 -05:00
|
|
|
|
|
|
|
def handler3(*arg)
|
|
|
|
@event3 += arg[0]
|
2008-07-26 08:27:57 -04:00
|
|
|
end
|
2008-11-02 08:22:42 -05:00
|
|
|
|
|
|
|
def teardown
|
|
|
|
if @db && @db.state == ADO::AdStateOpen
|
|
|
|
@db.close
|
|
|
|
end
|
|
|
|
message_loop
|
|
|
|
@db = nil
|
2008-07-26 08:27:57 -04:00
|
|
|
end
|
|
|
|
|
2008-11-02 08:22:42 -05:00
|
|
|
class Handler1
|
|
|
|
attr_reader :val1, :val2, :val3, :val4
|
|
|
|
def initialize
|
|
|
|
@val1 = nil
|
|
|
|
@val2 = nil
|
|
|
|
@val3 = nil
|
|
|
|
@val4 = nil
|
|
|
|
end
|
|
|
|
def onWillConnect(conn, uid, pwd, opts, stat, pconn)
|
|
|
|
@val1 = conn
|
|
|
|
end
|
|
|
|
def onConnectComplete(err, stat, pconn)
|
|
|
|
@val2 = err
|
|
|
|
@val3 = stat
|
|
|
|
end
|
|
|
|
def onInfoMessage(err, stat, pconn)
|
|
|
|
@val4 = stat
|
|
|
|
end
|
2008-07-26 08:27:57 -04:00
|
|
|
end
|
2008-11-02 08:22:42 -05:00
|
|
|
|
|
|
|
class Handler2
|
|
|
|
attr_reader :ev
|
|
|
|
def initialize
|
|
|
|
@ev = ""
|
|
|
|
end
|
|
|
|
def method_missing(ev, *arg)
|
|
|
|
@ev += ev
|
|
|
|
end
|
2008-07-26 08:27:57 -04:00
|
|
|
end
|
|
|
|
|
2008-11-02 08:22:42 -05:00
|
|
|
def test_handler1
|
|
|
|
ev = WIN32OLE_EVENT.new(@db)
|
|
|
|
h1 = Handler1.new
|
|
|
|
ev.handler = h1
|
|
|
|
@db.open
|
|
|
|
message_loop
|
|
|
|
assert_equal(CONNSTR, h1.val1)
|
|
|
|
assert_equal(h1.val1, ev.handler.val1)
|
|
|
|
assert_equal(nil, h1.val2)
|
|
|
|
assert_equal(ADO::AdStateOpen, h1.val3)
|
|
|
|
assert_equal(ADO::AdStateOpen, h1.val4)
|
|
|
|
end
|
2008-07-26 08:27:57 -04:00
|
|
|
|
2008-11-02 08:22:42 -05:00
|
|
|
def test_handler2
|
|
|
|
ev = WIN32OLE_EVENT.new(@db)
|
|
|
|
h2 = Handler2.new
|
|
|
|
ev.handler = h2
|
|
|
|
@db.open
|
|
|
|
message_loop
|
|
|
|
assert(h2.ev != "")
|
|
|
|
end
|
2014-08-25 07:18:17 -04:00
|
|
|
|
|
|
|
def test_s_new_exc_tainted
|
|
|
|
th = Thread.new {
|
|
|
|
$SAFE=1
|
|
|
|
str = 'ConnectionEvents'
|
|
|
|
str.taint
|
|
|
|
ev = WIN32OLE_EVENT.new(@db, str)
|
|
|
|
}
|
|
|
|
exc = assert_raise(SecurityError) {
|
|
|
|
th.join
|
|
|
|
}
|
|
|
|
assert_match(/insecure event creation - `ConnectionEvents'/, exc.message)
|
2017-12-28 15:09:24 -05:00
|
|
|
ensure
|
|
|
|
$SAFE = 0
|
2014-08-25 07:18:17 -04:00
|
|
|
end
|
2008-07-26 08:27:57 -04:00
|
|
|
end
|
2006-05-01 05:05:08 -04:00
|
|
|
end
|
|
|
|
end
|