diff options
Diffstat (limited to 'test/fiddle/test_handle.rb')
-rw-r--r-- | test/fiddle/test_handle.rb | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/test/fiddle/test_handle.rb b/test/fiddle/test_handle.rb index 17f9c92a11..412c10e09d 100644 --- a/test/fiddle/test_handle.rb +++ b/test/fiddle/test_handle.rb @@ -13,15 +13,23 @@ module Fiddle assert_kind_of Integer, handle.to_i end + def test_to_ptr + handle = Fiddle::Handle.new(LIBC_SO) + ptr = handle.to_ptr + assert_equal ptr.to_i, handle.to_i + end + def test_static_sym_unknown assert_raise(DLError) { Fiddle::Handle.sym('fooo') } assert_raise(DLError) { Fiddle::Handle['fooo'] } + refute Fiddle::Handle.sym_defined?('fooo') end def test_static_sym begin # Linux / Darwin / FreeBSD refute_nil Fiddle::Handle.sym('dlopen') + assert Fiddle::Handle.sym_defined?('dlopen') assert_equal Fiddle::Handle.sym('dlopen'), Fiddle::Handle['dlopen'] return rescue @@ -48,6 +56,7 @@ module Fiddle handle = Fiddle::Handle.new(LIBC_SO) assert_raise(DLError) { handle.sym('fooo') } assert_raise(DLError) { handle['fooo'] } + refute handle.sym_defined?('fooo') end def test_sym_with_bad_args @@ -60,6 +69,7 @@ module Fiddle handle = Handle.new(LIBC_SO) refute_nil handle.sym('calloc') refute_nil handle['calloc'] + assert handle.sym_defined?('calloc') end def test_handle_close @@ -106,6 +116,24 @@ module Fiddle assert !handle.close_enabled?, 'close is enabled' end + def test_file_name + file_name = Handle.new(LIBC_SO).file_name + if file_name + assert_kind_of String, file_name + expected = [File.basename(LIBC_SO)] + begin + expected << File.basename(File.realpath(LIBC_SO, File.dirname(file_name))) + rescue Errno::ENOENT + end + basename = File.basename(file_name) + unless File::FNM_SYSCASE.zero? + basename.downcase! + expected.each(&:downcase!) + end + assert_include expected, basename + end + end + def test_NEXT begin # Linux / Darwin @@ -155,13 +183,28 @@ module Fiddle # 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. + verbose, $VERBOSE = $VERBOSE, nil require 'socket' Socket.gethostbyname("localhost") Fiddle.dlopen("/lib/libc.so.7").sym('strcpy') + ensure + $VERBOSE = verbose end if /freebsd/=~ RUBY_PLATFORM def test_no_memory_leak - assert_no_memory_leak(%w[-W0 -rfiddle.so], '', '100_000.times {Fiddle::Handle.allocate}; GC.start', rss: true) + # https://github.com/ruby/fiddle/actions/runs/3202406059/jobs/5231356410 + omit if RUBY_VERSION >= '3.2' + + if respond_to?(:assert_nothing_leaked_memory) + n_tries = 100_000 + assert_nothing_leaked_memory(SIZEOF_VOIDP * (n_tries / 100)) do + n_tries.times do + Fiddle::Handle.allocate + end + end + else + assert_no_memory_leak(%w[-W0 -rfiddle.so], '', '100_000.times {Fiddle::Handle.allocate}; GC.start', rss: true) + end end if /cygwin|mingw|mswin/ =~ RUBY_PLATFORM |