summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2015-01-22 12:44:50 -0800
committerLamont Granquist <lamont@scriptkiddie.org>2015-01-22 12:44:50 -0800
commit8915463e6b17553215645d22b008f5d176261c10 (patch)
treee0f002318ee04ede490045d0971d243caca080b8
parent8bc806d4fc4f22ebb9c38c20838fa0824b5e6c21 (diff)
parent0a2244d16fe73e18f0b9453da9424db11a7d5f1d (diff)
downloadffi-yajl-8915463e6b17553215645d22b008f5d176261c10.tar.gz
Merge pull request #38 from chef/lcg/invalid-utf8
Lcg/invalid utf8
-rw-r--r--lib/ffi_yajl/encoder.rb4
-rw-r--r--spec/ffi_yajl/encoder_spec.rb28
2 files changed, 30 insertions, 2 deletions
diff --git a/lib/ffi_yajl/encoder.rb b/lib/ffi_yajl/encoder.rb
index 58b817a..18ef701 100644
--- a/lib/ffi_yajl/encoder.rb
+++ b/lib/ffi_yajl/encoder.rb
@@ -8,7 +8,7 @@ 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] = " "
@@ -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
diff --git a/spec/ffi_yajl/encoder_spec.rb b/spec/ffi_yajl/encoder_spec.rb
index 5c9e6b7..34a4d04 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 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
+
+ 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