2017-09-12 07:52:23 -04:00
|
|
|
# frozen_string_literal: true
|
2013-05-25 10:29:02 -04:00
|
|
|
begin
|
|
|
|
require_relative 'helper'
|
|
|
|
rescue LoadError
|
|
|
|
end
|
2012-11-27 14:54:50 -05:00
|
|
|
|
|
|
|
module Fiddle
|
|
|
|
class TestHandle < TestCase
|
|
|
|
include Fiddle
|
|
|
|
|
2015-12-16 07:08:49 -05:00
|
|
|
def test_safe_handle_open
|
2017-12-12 13:44:49 -05:00
|
|
|
Thread.new do
|
2015-12-16 07:08:49 -05:00
|
|
|
$SAFE = 1
|
2017-12-12 13:44:49 -05:00
|
|
|
assert_raise(SecurityError) {
|
|
|
|
Fiddle::Handle.new(LIBC_SO.dup.taint)
|
|
|
|
}
|
|
|
|
end.join
|
2017-12-28 15:09:24 -05:00
|
|
|
ensure
|
|
|
|
$SAFE = 0
|
2015-12-16 07:08:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_safe_function_lookup
|
2017-12-12 13:44:49 -05:00
|
|
|
Thread.new do
|
2015-12-16 07:08:49 -05:00
|
|
|
h = Fiddle::Handle.new(LIBC_SO)
|
|
|
|
$SAFE = 1
|
2017-12-12 13:44:49 -05:00
|
|
|
assert_raise(SecurityError) {
|
|
|
|
h["qsort".dup.taint]
|
|
|
|
}
|
|
|
|
end.join
|
2017-12-28 15:09:24 -05:00
|
|
|
ensure
|
|
|
|
$SAFE = 0
|
2015-12-16 07:08:49 -05:00
|
|
|
end
|
|
|
|
|
2012-11-27 14:54:50 -05:00
|
|
|
def test_to_i
|
|
|
|
handle = Fiddle::Handle.new(LIBC_SO)
|
|
|
|
assert_kind_of Integer, handle.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_static_sym_unknown
|
2015-10-30 00:37:13 -04:00
|
|
|
assert_raise(DLError) { Fiddle::Handle.sym('fooo') }
|
|
|
|
assert_raise(DLError) { Fiddle::Handle['fooo'] }
|
2012-11-27 14:54:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_static_sym
|
|
|
|
begin
|
|
|
|
# Linux / Darwin / FreeBSD
|
2013-05-25 10:29:05 -04:00
|
|
|
refute_nil Fiddle::Handle.sym('dlopen')
|
|
|
|
assert_equal Fiddle::Handle.sym('dlopen'), Fiddle::Handle['dlopen']
|
2015-01-05 10:15:48 -05:00
|
|
|
return
|
2012-11-27 14:54:50 -05:00
|
|
|
rescue
|
2015-01-05 10:15:48 -05:00
|
|
|
end
|
2015-01-05 13:11:32 -05:00
|
|
|
|
|
|
|
begin
|
2012-11-27 14:54:50 -05:00
|
|
|
# NetBSD
|
2015-01-05 10:16:13 -05:00
|
|
|
require '-test-/dln/empty'
|
|
|
|
refute_nil Fiddle::Handle.sym('Init_empty')
|
|
|
|
assert_equal Fiddle::Handle.sym('Init_empty'), Fiddle::Handle['Init_empty']
|
2015-01-05 10:15:48 -05:00
|
|
|
return
|
|
|
|
rescue
|
2012-11-27 14:54:50 -05:00
|
|
|
end
|
2015-01-05 10:15:41 -05:00
|
|
|
end unless /mswin|mingw/ =~ RUBY_PLATFORM
|
2012-11-27 14:54:50 -05:00
|
|
|
|
|
|
|
def test_sym_closed_handle
|
|
|
|
handle = Fiddle::Handle.new(LIBC_SO)
|
|
|
|
handle.close
|
2015-10-30 00:37:13 -04:00
|
|
|
assert_raise(DLError) { handle.sym("calloc") }
|
|
|
|
assert_raise(DLError) { handle["calloc"] }
|
2012-11-27 14:54:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sym_unknown
|
|
|
|
handle = Fiddle::Handle.new(LIBC_SO)
|
2015-10-30 00:37:13 -04:00
|
|
|
assert_raise(DLError) { handle.sym('fooo') }
|
|
|
|
assert_raise(DLError) { handle['fooo'] }
|
2012-11-27 14:54:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sym_with_bad_args
|
|
|
|
handle = Handle.new(LIBC_SO)
|
2015-10-30 00:37:13 -04:00
|
|
|
assert_raise(TypeError) { handle.sym(nil) }
|
|
|
|
assert_raise(TypeError) { handle[nil] }
|
2012-11-27 14:54:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sym
|
|
|
|
handle = Handle.new(LIBC_SO)
|
|
|
|
refute_nil handle.sym('calloc')
|
|
|
|
refute_nil handle['calloc']
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_handle_close
|
|
|
|
handle = Handle.new(LIBC_SO)
|
|
|
|
assert_equal 0, handle.close
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_handle_close_twice
|
|
|
|
handle = Handle.new(LIBC_SO)
|
|
|
|
handle.close
|
2015-10-30 00:37:13 -04:00
|
|
|
assert_raise(DLError) do
|
2012-11-27 14:54:50 -05:00
|
|
|
handle.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_dlopen_returns_handle
|
|
|
|
assert_instance_of Handle, dlopen(LIBC_SO)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_initialize_noargs
|
|
|
|
handle = Handle.new
|
|
|
|
refute_nil handle['rb_str_new']
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_initialize_flags
|
|
|
|
handle = Handle.new(LIBC_SO, RTLD_LAZY | RTLD_GLOBAL)
|
|
|
|
refute_nil handle['calloc']
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_enable_close
|
|
|
|
handle = Handle.new(LIBC_SO)
|
|
|
|
assert !handle.close_enabled?, 'close is enabled'
|
|
|
|
|
|
|
|
handle.enable_close
|
|
|
|
assert handle.close_enabled?, 'close is not enabled'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_disable_close
|
|
|
|
handle = Handle.new(LIBC_SO)
|
|
|
|
|
|
|
|
handle.enable_close
|
|
|
|
assert handle.close_enabled?, 'close is enabled'
|
|
|
|
handle.disable_close
|
|
|
|
assert !handle.close_enabled?, 'close is enabled'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_NEXT
|
|
|
|
begin
|
|
|
|
# Linux / Darwin
|
|
|
|
#
|
|
|
|
# There are two special pseudo-handles, RTLD_DEFAULT and RTLD_NEXT. The former will find
|
|
|
|
# the first occurrence of the desired symbol using the default library search order. The
|
|
|
|
# latter will find the next occurrence of a function in the search order after the current
|
|
|
|
# library. This allows one to provide a wrapper around a function in another shared
|
|
|
|
# library.
|
|
|
|
# --- Ubuntu Linux 8.04 dlsym(3)
|
|
|
|
handle = Handle::NEXT
|
|
|
|
refute_nil handle['malloc']
|
2015-01-05 10:15:48 -05:00
|
|
|
return
|
2012-11-27 14:54:50 -05:00
|
|
|
rescue
|
2015-01-05 10:15:48 -05:00
|
|
|
end
|
2015-01-05 13:11:32 -05:00
|
|
|
|
2015-01-05 10:15:48 -05:00
|
|
|
begin
|
2012-11-27 14:54:50 -05:00
|
|
|
# BSD
|
|
|
|
#
|
|
|
|
# If dlsym() is called with the special handle RTLD_NEXT, then the search
|
|
|
|
# for the symbol is limited to the shared objects which were loaded after
|
|
|
|
# the one issuing the call to dlsym(). Thus, if the function is called
|
|
|
|
# from the main program, all the shared libraries are searched. If it is
|
|
|
|
# called from a shared library, all subsequent shared libraries are
|
|
|
|
# searched. RTLD_NEXT is useful for implementing wrappers around library
|
|
|
|
# functions. For example, a wrapper function getpid() could access the
|
|
|
|
# "real" getpid() with dlsym(RTLD_NEXT, "getpid"). (Actually, the dlfunc()
|
|
|
|
# interface, below, should be used, since getpid() is a function and not a
|
|
|
|
# data object.)
|
|
|
|
# --- FreeBSD 8.0 dlsym(3)
|
2015-01-05 10:16:13 -05:00
|
|
|
require '-test-/dln/empty'
|
2012-11-27 14:54:50 -05:00
|
|
|
handle = Handle::NEXT
|
2015-01-05 10:16:13 -05:00
|
|
|
refute_nil handle['Init_empty']
|
2015-01-05 10:15:48 -05:00
|
|
|
return
|
|
|
|
rescue
|
2012-11-27 14:54:50 -05:00
|
|
|
end
|
|
|
|
end unless /mswin|mingw/ =~ RUBY_PLATFORM
|
|
|
|
|
|
|
|
def test_DEFAULT
|
|
|
|
handle = Handle::DEFAULT
|
|
|
|
refute_nil handle['malloc']
|
|
|
|
end unless /mswin|mingw/ =~ RUBY_PLATFORM
|
|
|
|
|
|
|
|
def test_dlerror
|
|
|
|
# FreeBSD (at least 7.2 to 7.2) calls nsdispatch(3) when it calls
|
|
|
|
# getaddrinfo(3). And nsdispatch(3) doesn't call dlerror(3) even if
|
|
|
|
# it calls _nss_cache_cycle_prevention_function with dlsym(3).
|
|
|
|
# So our Fiddle::Handle#sym must call dlerror(3) before call dlsym.
|
|
|
|
# In general uses of dlerror(3) should call it before use it.
|
|
|
|
require 'socket'
|
|
|
|
Socket.gethostbyname("localhost")
|
2014-01-14 20:46:03 -05:00
|
|
|
Fiddle.dlopen("/lib/libc.so.7").sym('strcpy')
|
2012-11-27 14:54:50 -05:00
|
|
|
end if /freebsd/=~ RUBY_PLATFORM
|
2014-03-08 23:19:10 -05:00
|
|
|
|
|
|
|
def test_no_memory_leak
|
2014-03-13 12:18:30 -04:00
|
|
|
assert_no_memory_leak(%w[-W0 -rfiddle.so], '', '100_000.times {Fiddle::Handle.allocate}; GC.start', rss: true)
|
2014-03-08 23:19:10 -05:00
|
|
|
end
|
2016-05-16 00:29:01 -04:00
|
|
|
|
|
|
|
if /cygwin|mingw|mswin/ =~ RUBY_PLATFORM
|
|
|
|
def test_fallback_to_ansi
|
|
|
|
k = Fiddle::Handle.new("kernel32.dll")
|
2016-05-16 02:26:56 -04:00
|
|
|
ansi = k["GetFileAttributesA"]
|
|
|
|
assert_equal(ansi, k["GetFileAttributes"], "should fallback to ANSI version")
|
2016-05-16 00:29:01 -04:00
|
|
|
end
|
|
|
|
end
|
2012-11-27 14:54:50 -05:00
|
|
|
end
|
2013-05-25 10:29:02 -04:00
|
|
|
end if defined?(Fiddle)
|