From a8edfbbac238fcf5a4aa4a9f004910856d874fed Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Wed, 10 Apr 2019 18:34:49 +0200 Subject: [PATCH 1/4] Add test for #read_bytes and #write_bytes * Nothing was testing #read_bytes before. --- spec/ffi/buffer_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/ffi/buffer_spec.rb b/spec/ffi/buffer_spec.rb index 65737b26e..b2ea6ee2d 100644 --- a/spec/ffi/buffer_spec.rb +++ b/spec/ffi/buffer_spec.rb @@ -138,6 +138,14 @@ end describe "Reading/Writing binary strings" do + it "Buffer#write_bytes and read_bytes" do + str = "hello\0world" + buf = FFI::Buffer.new 11 + buf.write_bytes(str) + s2 = buf.read_bytes(11) + expect(s2).to eq(str) + end + it "Buffer#put_bytes" do str = "hello\0world" buf = FFI::Buffer.new 1024 From 4d1c93cfeae2173f7aa38fbc4b2f2076e1642ac1 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Wed, 3 Apr 2019 17:42:22 +0200 Subject: [PATCH 2/4] Test using FFI::Function and unsigned values in callback_spec.rb --- spec/ffi/callback_spec.rb | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/spec/ffi/callback_spec.rb b/spec/ffi/callback_spec.rb index 9d66e2aa7..d94b7396d 100644 --- a/spec/ffi/callback_spec.rb +++ b/spec/ffi/callback_spec.rb @@ -500,28 +500,32 @@ class S8F32S32 < FFI::Struct expect(v).to eq(-1) end + def testCallbackU8rV(value) + v1 = 0xdeadbeef + LibTest.testCallbackU8rV(value) { |i| v1 = i } + expect(v1).to eq(value) + + # Using a FFI::Function (v2) should be consistent with the direct callback (v1) + v2 = 0xdeadbeef + fun = FFI::Function.new(:void, [:uchar]) { |i| v2 = i } + LibTest.testCallbackU8rV(fun, value) + expect(v2).to eq(value) + end + it ":uchar (0) argument" do - v = 0xdeadbeef - LibTest.testCallbackU8rV(0) { |i| v = i } - expect(v).to eq(0) + testCallbackU8rV(0) end it ":uchar (127) argument" do - v = 0xdeadbeef - LibTest.testCallbackU8rV(127) { |i| v = i } - expect(v).to eq(127) + testCallbackU8rV(127) end it ":uchar (128) argument" do - v = 0xdeadbeef - LibTest.testCallbackU8rV(128) { |i| v = i } - expect(v).to eq(128) + testCallbackU8rV(128) end it ":uchar (255) argument" do - v = 0xdeadbeef - LibTest.testCallbackU8rV(255) { |i| v = i } - expect(v).to eq(255) + testCallbackU8rV(255) end it ":short (0) argument" do From 6de9feb005073a98803f5f2812ba438bf7305979 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Thu, 11 Apr 2019 12:20:36 +0200 Subject: [PATCH 3/4] Add spec for passing a String to a :pointer argument --- spec/ffi/string_spec.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/spec/ffi/string_spec.rb b/spec/ffi/string_spec.rb index dfd368458..7f87d6d5f 100644 --- a/spec/ffi/string_spec.rb +++ b/spec/ffi/string_spec.rb @@ -11,6 +11,7 @@ module StrLibTest ffi_lib TestLibrary::PATH attach_function :ptr_ret_pointer, [ :pointer, :int], :string attach_function :string_equals, [ :string, :string ], :int + attach_function :pointer_string_equals, :string_equals, [ :pointer, :string ], :int attach_function :string_dummy, [ :string ], :void attach_function :string_null, [ ], :string end @@ -32,6 +33,12 @@ module StrLibTest expect(str).to be_tainted end + it "A String can be passed to a :pointer argument" do + str = "string buffer" + expect(StrLibTest.pointer_string_equals(str, str)).to eq(1) + expect(StrLibTest.pointer_string_equals(str + "a", str)).to eq(0) + end + it "Poison null byte raises error" do s = "123\0abc" expect { StrLibTest.string_equals(s, s) }.to raise_error(ArgumentError) From 5b81727746525c93ef34be42130f32eafb10840b Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Thu, 16 May 2019 16:26:52 +0200 Subject: [PATCH 4/4] Add spec for a function returning a function * Used by the sassc gem. --- spec/ffi/callback_spec.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/spec/ffi/callback_spec.rb b/spec/ffi/callback_spec.rb index d94b7396d..400364397 100644 --- a/spec/ffi/callback_spec.rb +++ b/spec/ffi/callback_spec.rb @@ -75,6 +75,7 @@ class S8F32S32 < FFI::Struct attach_function :testCallbackVrS64, :testClosureVrLL, [ :cbVrS64 ], :long_long attach_function :testCallbackVrU64, :testClosureVrLL, [ :cbVrU64 ], :ulong_long attach_function :testCallbackVrP, :testClosureVrP, [ :cbVrP ], :pointer + attach_function :testCallbackReturningFunction, :testClosureVrP, [ :cbVrP ], :cbVrP attach_function :testCallbackVrY, :testClosureVrP, [ :cbVrY ], S8F32S32.ptr attach_function :testCallbackVrT, :testClosureVrT, [ :cbVrT ], S8F32S32.by_value attach_function :testCallbackTrV, :testClosureTrV, [ :cbTrV, S8F32S32.ptr ], :void @@ -266,6 +267,12 @@ class S8F32S32 < FFI::Struct expect(LibTest.testCallbackVrP { p }).to eq(p) end + it "returning a callback function" do + ret = LibTest.testCallbackReturningFunction { FFI::Pointer.new(42) } + expect(ret).to be_kind_of(FFI::Function) + expect(ret.address).to eq(42) + end + it "returning struct by value" do skip "Segfault on 32 bit MINGW" if RUBY_PLATFORM == 'i386-mingw32' s = LibTest::S8F32S32.new