From 36492e4bd8d1521945466c39515eb9a3a961a72d Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Thu, 22 Jan 2015 08:32:09 -0800 Subject: catch and raise invalid string error better --- lib/ffi_yajl/encoder.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/ffi_yajl/encoder.rb b/lib/ffi_yajl/encoder.rb index 58b817a..004c006 100644 --- a/lib/ffi_yajl/encoder.rb +++ b/lib/ffi_yajl/encoder.rb @@ -46,6 +46,8 @@ module FFI_Yajl raise FFI_Yajl::EncodeError, "Invalid number: cannot encode Infinity, -Infinity, or NaN" when 6 # yajl_gen_no_buf raise FFI_Yajl::EncodeError, "YAJL internal error: yajl_gen_get_buf was called, but a print callback was specified, so no internal buffer is available" + when 7 # yajl_gen_invalid_string + raise FFI_Yajl::EncodeError, "Invalid UTF-8 string: cannot encode to UTF-8" else raise FFI_Yajl::EncodeError, "Unknown YAJL Error (#{status}), please report this as a bug" end -- cgit v1.2.1 From 07e5fac2052a62be3a273b45e2c3f764254c6bb4 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Thu, 22 Jan 2015 08:50:51 -0800 Subject: support turning off validate_utf8 --- lib/ffi_yajl/encoder.rb | 1 + spec/ffi_yajl/encoder_spec.rb | 28 +++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/ffi_yajl/encoder.rb b/lib/ffi_yajl/encoder.rb index 004c006..18b606e 100644 --- a/lib/ffi_yajl/encoder.rb +++ b/lib/ffi_yajl/encoder.rb @@ -9,6 +9,7 @@ module FFI_Yajl yajl_gen_opts = {} yajl_gen_opts[:yajl_gen_validate_utf8] = true + yajl_gen_opts[:yajl_gen_validate_utf8] = @opts[:validate_utf8] == false ? false : true yajl_gen_opts[:yajl_gen_beautify] = false yajl_gen_opts[:yajl_gen_indent_string] = " " diff --git a/spec/ffi_yajl/encoder_spec.rb b/spec/ffi_yajl/encoder_spec.rb index 5c9e6b7..c697411 100644 --- a/spec/ffi_yajl/encoder_spec.rb +++ b/spec/ffi_yajl/encoder_spec.rb @@ -5,7 +5,9 @@ require 'date' describe "FFI_Yajl::Encoder" do - let(:encoder) { FFI_Yajl::Encoder.new } + let(:options) { {} } + + let(:encoder) { FFI_Yajl::Encoder.new(options) } it "encodes hashes in keys as strings", :ruby_gte_193 => true do ruby = { {'a' => 'b'} => 2 } @@ -146,4 +148,28 @@ describe "FFI_Yajl::Encoder" do end end + context "when encoding invalid utf-8" do + ruby = { + "automatic"=>{ + "etc"=>{ + "passwd"=>{ + "root"=>{"dir"=>"/root", "gid"=>0, "uid"=>0, "shell"=>"/bin/sh", "gecos"=>"Elan Ruusam\xc3\xa4e"}, + "glen"=>{"dir"=>"/home/glen", "gid"=>500, "uid"=>500, "shell"=>"/bin/bash", "gecos"=>"Elan Ruusam\xE4e"}, + } + }, + }, + } + + it "raises and error on invalid json" do + expect{ encoder.encode(ruby) }.to raise_error(FFI_Yajl::EncodeError, "Invalid UTF-8 string: cannot encode to UTF-8") + end + + context "when validate_utf8 is off" do + let(:options) { { :validate_utf8 => false } } + + it "does not raise an error" do + expect{ encoder.encode(ruby) }.not_to raise_error + end + end + end end -- cgit v1.2.1 From 3417ff6ec256f65e8000b99145039a948df0805b Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Thu, 22 Jan 2015 08:52:20 -0800 Subject: fix typo --- spec/ffi_yajl/encoder_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/ffi_yajl/encoder_spec.rb b/spec/ffi_yajl/encoder_spec.rb index c697411..34a4d04 100644 --- a/spec/ffi_yajl/encoder_spec.rb +++ b/spec/ffi_yajl/encoder_spec.rb @@ -160,7 +160,7 @@ describe "FFI_Yajl::Encoder" do }, } - it "raises and error on invalid json" do + it "raises an error on invalid json" do expect{ encoder.encode(ruby) }.to raise_error(FFI_Yajl::EncodeError, "Invalid UTF-8 string: cannot encode to UTF-8") end -- cgit v1.2.1 From 0a2244d16fe73e18f0b9453da9424db11a7d5f1d Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Thu, 22 Jan 2015 08:56:55 -0800 Subject: remove unnecessary line --- lib/ffi_yajl/encoder.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/ffi_yajl/encoder.rb b/lib/ffi_yajl/encoder.rb index 18b606e..18ef701 100644 --- a/lib/ffi_yajl/encoder.rb +++ b/lib/ffi_yajl/encoder.rb @@ -8,7 +8,6 @@ module FFI_Yajl # initialization that we can do in pure ruby yajl_gen_opts = {} - yajl_gen_opts[:yajl_gen_validate_utf8] = true yajl_gen_opts[:yajl_gen_validate_utf8] = @opts[:validate_utf8] == false ? false : true yajl_gen_opts[:yajl_gen_beautify] = false yajl_gen_opts[:yajl_gen_indent_string] = " " -- cgit v1.2.1