1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/test/fiddle/test_func.rb
ko1 3fe69a0da4 Use mutable strings for mutation tests.
* test/fiddle/test_func.rb (test_string): this test break String buffer
  by `strcpy` ("000" -> "123"). However, the string literal "000" with
  `frozen_string_literal: true` returns a string object from frozen
  string pool. So that after this test "000" from fstring pool becomes
  "123" (modified string). 'test/date/' uses "000" (as fstring) and tests
  are fails (we could check with `make test-all TESTS='fiddle date'`).

* test/fiddle/test_function.rb: ditto.

* test/fiddle/test_import.rb: ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59861 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-09-12 23:15:34 +00:00

93 lines
2.4 KiB
Ruby

# frozen_string_literal: true
begin
require_relative 'helper'
rescue LoadError
end
module Fiddle
class TestFunc < TestCase
def test_random
f = Function.new(@libc['srand'], [-TYPE_LONG], TYPE_VOID)
assert_nil f.call(10)
end
def test_syscall_with_tainted_string
f = Function.new(@libc['system'], [TYPE_VOIDP], TYPE_INT)
assert_raise(SecurityError) do
Thread.new {
$SAFE = 1
f.call("uname -rs".dup.taint)
}.join
end
end
def test_sinf
begin
f = Function.new(@libm['sinf'], [TYPE_FLOAT], TYPE_FLOAT)
rescue Fiddle::DLError
skip "libm may not have sinf()"
end
assert_in_delta 1.0, f.call(90 * Math::PI / 180), 0.0001
end
def test_sin
f = Function.new(@libm['sin'], [TYPE_DOUBLE], TYPE_DOUBLE)
assert_in_delta 1.0, f.call(90 * Math::PI / 180), 0.0001
end
def test_string
stress, GC.stress = GC.stress, true
f = Function.new(@libc['strcpy'], [TYPE_VOIDP, TYPE_VOIDP], TYPE_VOIDP)
buff = +"000"
str = f.call(buff, "123")
assert_equal("123", buff)
assert_equal("123", str.to_s)
ensure
GC.stress = stress
end
def test_isdigit
f = Function.new(@libc['isdigit'], [TYPE_INT], TYPE_INT)
r1 = f.call(?1.ord)
r2 = f.call(?2.ord)
rr = f.call(?r.ord)
assert_operator r1, :>, 0
assert_operator r2, :>, 0
assert_equal 0, rr
end
def test_atof
f = Function.new(@libc['atof'], [TYPE_VOIDP], TYPE_DOUBLE)
r = f.call("12.34")
assert_includes(12.00..13.00, r)
end
def test_strtod
f = Function.new(@libc['strtod'], [TYPE_VOIDP, TYPE_VOIDP], TYPE_DOUBLE)
buff1 = Pointer["12.34"]
buff2 = buff1 + 4
r = f.call(buff1, - buff2)
assert_in_delta(12.34, r, 0.001)
end
def test_qsort1
cb = Class.new(Closure) {
def call(x, y)
Pointer.new(x)[0] <=> Pointer.new(y)[0]
end
}.new(TYPE_INT, [TYPE_VOIDP, TYPE_VOIDP])
qsort = Function.new(@libc['qsort'],
[TYPE_VOIDP, TYPE_SIZE_T, TYPE_SIZE_T, TYPE_VOIDP],
TYPE_VOID)
buff = "9341"
qsort.call(buff, buff.size, 1, cb)
assert_equal("1349", buff)
bug4929 = '[ruby-core:37395]'
buff = "9341"
EnvUtil.under_gc_stress {qsort.call(buff, buff.size, 1, cb)}
assert_equal("1349", buff, bug4929)
end
end
end if defined?(Fiddle)