diff options
Diffstat (limited to 'test/json')
24 files changed, 2393 insertions, 383 deletions
diff --git a/test/json/fixtures/pass15.json b/test/json/fixtures/fail15.json index fc8376b605..fc8376b605 100644 --- a/test/json/fixtures/pass15.json +++ b/test/json/fixtures/fail15.json diff --git a/test/json/fixtures/pass16.json b/test/json/fixtures/fail16.json index c43ae3c286..c43ae3c286 100644 --- a/test/json/fixtures/pass16.json +++ b/test/json/fixtures/fail16.json diff --git a/test/json/fixtures/pass17.json b/test/json/fixtures/fail17.json index 62b9214aed..62b9214aed 100644 --- a/test/json/fixtures/pass17.json +++ b/test/json/fixtures/fail17.json diff --git a/test/json/fixtures/pass26.json b/test/json/fixtures/fail26.json index 845d26a6a5..845d26a6a5 100644 --- a/test/json/fixtures/pass26.json +++ b/test/json/fixtures/fail26.json diff --git a/test/json/fixtures/fail29.json b/test/json/fixtures/fail29.json new file mode 100644 index 0000000000..98232c64fc --- /dev/null +++ b/test/json/fixtures/fail29.json @@ -0,0 +1 @@ +{ diff --git a/test/json/fixtures/fail30.json b/test/json/fixtures/fail30.json new file mode 100644 index 0000000000..558ed37d93 --- /dev/null +++ b/test/json/fixtures/fail30.json @@ -0,0 +1 @@ +[ diff --git a/test/json/fixtures/fail31.json b/test/json/fixtures/fail31.json new file mode 100644 index 0000000000..70773e47f7 --- /dev/null +++ b/test/json/fixtures/fail31.json @@ -0,0 +1 @@ +[1, 2, 3, diff --git a/test/json/fixtures/fail32.json b/test/json/fixtures/fail32.json new file mode 100644 index 0000000000..b18d550ca5 --- /dev/null +++ b/test/json/fixtures/fail32.json @@ -0,0 +1 @@ +{"foo": "bar" diff --git a/test/json/fixtures/fail4.json b/test/json/fixtures/fail4.json deleted file mode 100644 index 9de168bf34..0000000000 --- a/test/json/fixtures/fail4.json +++ /dev/null @@ -1 +0,0 @@ -["extra comma",]
\ No newline at end of file diff --git a/test/json/fixtures/fail9.json b/test/json/fixtures/fail9.json deleted file mode 100644 index 5815574f36..0000000000 --- a/test/json/fixtures/fail9.json +++ /dev/null @@ -1 +0,0 @@ -{"Extra comma": true,}
\ No newline at end of file diff --git a/test/json/fixtures/pass1.json b/test/json/fixtures/pass1.json index 7828fcc137..fa9058b136 100644 --- a/test/json/fixtures/pass1.json +++ b/test/json/fixtures/pass1.json @@ -12,7 +12,7 @@ "real": -9876.543210, "e": 0.123456789e-12, "E": 1.234567890E+34, - "": 23456789012E666, + "": 23456789012E66, "zero": 0, "one": 1, "space": " ", diff --git a/test/json/json_addition_test.rb b/test/json/json_addition_test.rb index 61625f89e2..4d8d186873 100644 --- a/test/json/json_addition_test.rb +++ b/test/json/json_addition_test.rb @@ -1,5 +1,5 @@ -#frozen_string_literal: false -require 'test_helper' +# frozen_string_literal: true +require_relative 'test_helper' require 'json/add/core' require 'json/add/complex' require 'json/add/rational' @@ -44,10 +44,6 @@ class JSONAdditionTest < Test::Unit::TestCase end class B - def self.json_creatable? - false - end - def to_json(*args) { 'json_class' => self.class.name, @@ -56,10 +52,6 @@ class JSONAdditionTest < Test::Unit::TestCase end class C - def self.json_creatable? - false - end - def to_json(*args) { 'json_class' => 'JSONAdditionTest::Nix', @@ -69,7 +61,6 @@ class JSONAdditionTest < Test::Unit::TestCase def test_extended_json a = A.new(666) - assert A.json_creatable? json = generate(a) a_again = parse(json, :create_additions => true) assert_kind_of a.class, a_again @@ -78,7 +69,7 @@ class JSONAdditionTest < Test::Unit::TestCase def test_extended_json_default a = A.new(666) - assert A.json_creatable? + assert A.respond_to?(:json_create) json = generate(a) a_hash = parse(json) assert_kind_of Hash, a_hash @@ -86,7 +77,6 @@ class JSONAdditionTest < Test::Unit::TestCase def test_extended_json_disabled a = A.new(666) - assert A.json_creatable? json = generate(a) a_again = parse(json, :create_additions => true) assert_kind_of a.class, a_again @@ -101,21 +91,18 @@ class JSONAdditionTest < Test::Unit::TestCase def test_extended_json_fail1 b = B.new - assert !B.json_creatable? json = generate(b) assert_equal({ "json_class"=>"JSONAdditionTest::B" }, parse(json)) end def test_extended_json_fail2 c = C.new - assert !C.json_creatable? json = generate(c) assert_raise(ArgumentError, NameError) { parse(json, :create_additions => true) } end def test_raw_strings - raw = '' - raw.respond_to?(:encode!) and raw.encode!(Encoding::ASCII_8BIT) + raw = ''.b raw_array = [] for i in 0..255 raw << i @@ -163,9 +150,15 @@ class JSONAdditionTest < Test::Unit::TestCase assert_equal(/foo/i, JSON(JSON(/foo/i), :create_additions => true)) end + def test_deprecated_load_create_additions + assert_deprecated_warning(/use JSON\.unsafe_load/) do + JSON.load(JSON.dump(Time.now)) + end + end + def test_utc_datetime now = Time.now - d = DateTime.parse(now.to_s, :create_additions => true) # usual case + d = DateTime.parse(now.to_s) # usual case assert_equal d, parse(d.to_json, :create_additions => true) d = DateTime.parse(now.utc.to_s) # of = 0 assert_equal d, parse(d.to_json, :create_additions => true) @@ -183,21 +176,17 @@ class JSONAdditionTest < Test::Unit::TestCase def test_bigdecimal assert_equal BigDecimal('3.141', 23), JSON(JSON(BigDecimal('3.141', 23)), :create_additions => true) assert_equal BigDecimal('3.141', 666), JSON(JSON(BigDecimal('3.141', 666)), :create_additions => true) - end + end if defined?(::BigDecimal) def test_ostruct o = OpenStruct.new # XXX this won't work; o.foo = { :bar => true } o.foo = { 'bar' => true } assert_equal o, parse(JSON(o), :create_additions => true) - end + end if defined?(::OpenStruct) def test_set s = Set.new([:a, :b, :c, :a]) assert_equal s, JSON.parse(JSON(s), :create_additions => true) - ss = SortedSet.new([:d, :b, :a, :c]) - ss_again = JSON.parse(JSON(ss), :create_additions => true) - assert_kind_of ss.class, ss_again - assert_equal ss, ss_again end end diff --git a/test/json/json_coder_test.rb b/test/json/json_coder_test.rb new file mode 100755 index 0000000000..47e12ff919 --- /dev/null +++ b/test/json/json_coder_test.rb @@ -0,0 +1,149 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require_relative 'test_helper' + +class JSONCoderTest < Test::Unit::TestCase + def test_json_coder_with_proc + coder = JSON::Coder.new do |object| + "[Object object]" + end + assert_equal %(["[Object object]"]), coder.dump([Object.new]) + end + + def test_json_coder_with_proc_with_unsupported_value + coder = JSON::Coder.new do |object, is_key| + assert_equal false, is_key + Object.new + end + assert_raise(JSON::GeneratorError) { coder.dump([Object.new]) } + end + + def test_json_coder_hash_key + obj = Object.new + coder = JSON::Coder.new do |obj, is_key| + assert_equal true, is_key + obj.to_s + end + assert_equal %({#{obj.to_s.inspect}:1}), coder.dump({ obj => 1 }) + + coder = JSON::Coder.new { 42 } + error = assert_raise JSON::GeneratorError do + coder.dump({ obj => 1 }) + end + assert_equal "Integer not allowed as object key in JSON", error.message + end + + def test_json_coder_options + coder = JSON::Coder.new(array_nl: "\n") do |object| + 42 + end + + assert_equal "[\n42\n]", coder.dump([Object.new]) + end + + def test_json_coder_load + coder = JSON::Coder.new + assert_equal [1,2,3], coder.load("[1,2,3]") + end + + def test_json_coder_load_options + coder = JSON::Coder.new(symbolize_names: true) + assert_equal({a: 1}, coder.load('{"a":1}')) + end + + def test_json_coder_dump_NaN_or_Infinity + coder = JSON::Coder.new { |o| o.inspect } + assert_equal "NaN", coder.load(coder.dump(Float::NAN)) + assert_equal "Infinity", coder.load(coder.dump(Float::INFINITY)) + assert_equal "-Infinity", coder.load(coder.dump(-Float::INFINITY)) + end + + def test_json_coder_dump_NaN_or_Infinity_loop + coder = JSON::Coder.new { |o| o.itself } + error = assert_raise JSON::GeneratorError do + coder.dump(Float::NAN) + end + assert_include error.message, "NaN not allowed in JSON" + end + + def test_json_coder_string_invalid_encoding + calls = 0 + coder = JSON::Coder.new do |object, is_key| + calls += 1 + object + end + + error = assert_raise JSON::GeneratorError do + coder.dump("\xFF") + end + assert_equal "source sequence is illegal/malformed utf-8", error.message + assert_equal 1, calls + + error = assert_raise JSON::GeneratorError do + coder.dump({ "\xFF" => 1 }) + end + assert_equal "source sequence is illegal/malformed utf-8", error.message + assert_equal 2, calls + + calls = 0 + coder = JSON::Coder.new do |object, is_key| + calls += 1 + object.dup + end + + error = assert_raise JSON::GeneratorError do + coder.dump("\xFF") + end + assert_equal "source sequence is illegal/malformed utf-8", error.message + assert_equal 1, calls + + error = assert_raise JSON::GeneratorError do + coder.dump({ "\xFF" => 1 }) + end + assert_equal "source sequence is illegal/malformed utf-8", error.message + assert_equal 2, calls + + calls = 0 + coder = JSON::Coder.new do |object, is_key| + calls += 1 + object.bytes + end + + assert_equal "[255]", coder.dump("\xFF") + assert_equal 1, calls + + error = assert_raise JSON::GeneratorError do + coder.dump({ "\xFF" => 1 }) + end + assert_equal "Array not allowed as object key in JSON", error.message + assert_equal 2, calls + + calls = 0 + coder = JSON::Coder.new do |object, is_key| + calls += 1 + [object].pack("m") + end + + assert_equal '"/w==\\n"', coder.dump("\xFF") + assert_equal 1, calls + + assert_equal '{"/w==\\n":1}', coder.dump({ "\xFF" => 1 }) + assert_equal 2, calls + end + + def test_depth + coder = JSON::Coder.new(object_nl: "\n", array_nl: "\n", space: " ", indent: " ", depth: 1) + assert_equal %({\n "foo": 42\n }), coder.dump(foo: 42) + end + + def test_nesting_recovery + coder = JSON::Coder.new + ary = [] + ary << ary + assert_raise JSON::NestingError do + coder.dump(ary) + end + assert_equal '{"a":1}', coder.dump({ a: 1 }) + end +end diff --git a/test/json/json_common_interface_test.rb b/test/json/json_common_interface_test.rb index 6f32247a05..3dfd0623cd 100644 --- a/test/json/json_common_interface_test.rb +++ b/test/json/json_common_interface_test.rb @@ -1,11 +1,19 @@ -#frozen_string_literal: false -require 'test_helper' +# frozen_string_literal: true + +require_relative 'test_helper' require 'stringio' require 'tempfile' class JSONCommonInterfaceTest < Test::Unit::TestCase include JSON + module MethodMissing + def method_missing(name, *args); end + def respond_to_missing?(name, include_private) + true + end + end + def setup @hash = { 'a' => 2, @@ -17,12 +25,26 @@ class JSONCommonInterfaceTest < Test::Unit::TestCase 'h' => 1000.0, 'i' => 0.001 } + + @hash_with_method_missing = { + 'a' => 2, + 'b' => 3.141, + 'c' => 'c', + 'd' => [ 1, "b", 3.14 ], + 'e' => { 'foo' => 'bar' }, + 'g' => "\"\0\037", + 'h' => 1000.0, + 'i' => 0.001 + } + @hash_with_method_missing.extend MethodMissing + @json = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},'\ '"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}' end def test_index assert_equal @json, JSON[@hash] + assert_equal @json, JSON[@hash_with_method_missing] assert_equal @hash, JSON[@json] end @@ -31,11 +53,11 @@ class JSONCommonInterfaceTest < Test::Unit::TestCase end def test_generator - assert_match(/::Generator\z/, JSON.generator.name) + assert_match(/::(TruffleRuby)?Generator\z/, JSON.generator.name) end def test_state - assert_match(/::Generator::State\z/, JSON.state.name) + assert_match(/::(TruffleRuby)?Generator::State\z/, JSON.state.name) end def test_create_id @@ -46,17 +68,12 @@ class JSONCommonInterfaceTest < Test::Unit::TestCase JSON.create_id = 'json_class' end - def test_deep_const_get - assert_raise(ArgumentError) { JSON.deep_const_get('Nix::Da') } - assert_equal File::SEPARATOR, JSON.deep_const_get('File::SEPARATOR') - end - def test_parse assert_equal [ 1, 2, 3, ], JSON.parse('[ 1, 2, 3 ]') end def test_parse_bang - assert_equal [ 1, NaN, 3, ], JSON.parse!('[ 1, NaN, 3 ]') + assert_equal [ 1, Infinity, 3, ], JSON.parse!('[ 1, Infinity, 3 ]') end def test_generate @@ -69,6 +86,30 @@ class JSONCommonInterfaceTest < Test::Unit::TestCase def test_pretty_generate assert_equal "[\n 1,\n 2,\n 3\n]", JSON.pretty_generate([ 1, 2, 3 ]) + assert_equal <<~JSON.strip, JSON.pretty_generate({ a: { b: "f"}, c: "d"}) + { + "a": { + "b": "f" + }, + "c": "d" + } + JSON + + # Cause the state to be spilled on the heap. + o = Object.new + def o.to_s + "Object" + end + actual = JSON.pretty_generate({ a: { b: o}, c: "d", e: "f"}) + assert_equal <<~JSON.strip, actual + { + "a": { + "b": "Object" + }, + "c": "d", + "e": "f" + } + JSON end def test_load @@ -86,9 +127,29 @@ class JSONCommonInterfaceTest < Test::Unit::TestCase tempfile.close! end + def test_load_with_proc + visited = [] + JSON.load('{"foo": [1, 2, 3], "bar": {"baz": "plop"}}', proc { |o| visited << JSON.dump(o); o }) + + expected = [ + '"foo"', + '1', + '2', + '3', + '[1,2,3]', + '"bar"', + '"baz"', + '"plop"', + '{"baz":"plop"}', + '{"foo":[1,2,3],"bar":{"baz":"plop"}}', + ] + assert_equal expected, visited + end + def test_load_with_options json = '{ "foo": NaN }' assert JSON.load(json, nil, :allow_nan => true)['foo'].nan? + assert JSON.load(json, :allow_nan => true)['foo'].nan? end def test_load_null @@ -97,30 +158,202 @@ class JSONCommonInterfaceTest < Test::Unit::TestCase assert_raise(JSON::ParserError) { JSON.load('', nil, :allow_blank => false) } end + def test_unsafe_load + string_able_klass = Class.new do + def initialize(str) + @str = str + end + + def to_str + @str + end + end + + io_able_klass = Class.new do + def initialize(str) + @str = str + end + + def to_io + StringIO.new(@str) + end + end + + assert_equal @hash, JSON.unsafe_load(@json) + tempfile = Tempfile.open('@json') + tempfile.write @json + tempfile.rewind + assert_equal @hash, JSON.unsafe_load(tempfile) + stringio = StringIO.new(@json) + stringio.rewind + assert_equal @hash, JSON.unsafe_load(stringio) + string_able = string_able_klass.new(@json) + assert_equal @hash, JSON.unsafe_load(string_able) + io_able = io_able_klass.new(@json) + assert_equal @hash, JSON.unsafe_load(io_able) + assert_equal nil, JSON.unsafe_load(nil) + assert_equal nil, JSON.unsafe_load('') + ensure + tempfile.close! + end + + def test_unsafe_load_with_proc + visited = [] + JSON.unsafe_load('{"foo": [1, 2, 3], "bar": {"baz": "plop"}}', proc { |o| visited << JSON.dump(o); o }) + + expected = [ + '"foo"', + '1', + '2', + '3', + '[1,2,3]', + '"bar"', + '"baz"', + '"plop"', + '{"baz":"plop"}', + '{"foo":[1,2,3],"bar":{"baz":"plop"}}', + ] + assert_equal expected, visited + end + + def test_unsafe_load_default_options + too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]' + assert JSON.unsafe_load(too_deep, nil).is_a?(Array) + nan_json = '{ "foo": NaN }' + assert JSON.unsafe_load(nan_json, nil)['foo'].nan? + assert_equal nil, JSON.unsafe_load(nil, nil) + t = Time.new(2025, 9, 3, 14, 50, 0) + assert_equal t.to_s, JSON.unsafe_load(JSON(t)).to_s + end + + def test_unsafe_load_with_options + nan_json = '{ "foo": NaN }' + assert_raise(JSON::ParserError) { JSON.unsafe_load(nan_json, nil, :allow_nan => false)['foo'].nan? } + # make sure it still uses the defaults when something is provided + assert JSON.unsafe_load(nan_json, nil, :allow_blank => true)['foo'].nan? + assert JSON.unsafe_load(nan_json, :allow_nan => true)['foo'].nan? + end + + def test_unsafe_load_null + assert_equal nil, JSON.unsafe_load(nil, nil, :allow_blank => true) + assert_raise(TypeError) { JSON.unsafe_load(nil, nil, :allow_blank => false) } + assert_raise(JSON::ParserError) { JSON.unsafe_load('', nil, :allow_blank => false) } + end + def test_dump too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]' - assert_equal too_deep, dump(eval(too_deep)) - assert_kind_of String, Marshal.dump(eval(too_deep)) - assert_raise(ArgumentError) { dump(eval(too_deep), 100) } - assert_raise(ArgumentError) { Marshal.dump(eval(too_deep), 100) } - assert_equal too_deep, dump(eval(too_deep), 101) - assert_kind_of String, Marshal.dump(eval(too_deep), 101) - output = StringIO.new - dump(eval(too_deep), output) - assert_equal too_deep, output.string - output = StringIO.new - dump(eval(too_deep), output, 101) - assert_equal too_deep, output.string + obj = eval(too_deep) + assert_equal too_deep, dump(obj) + assert_kind_of String, Marshal.dump(obj) + assert_raise(ArgumentError) { dump(obj, 100) } + assert_raise(ArgumentError) { Marshal.dump(obj, 100) } + assert_equal too_deep, dump(obj, 101) + assert_kind_of String, Marshal.dump(obj, 101) + + assert_equal too_deep, JSON.dump(obj, StringIO.new, 101, strict: false).string + assert_equal too_deep, dump(obj, StringIO.new, 101, strict: false).string + assert_raise(JSON::GeneratorError) { JSON.dump(Object.new, StringIO.new, 101, strict: true).string } + assert_raise(JSON::GeneratorError) { dump(Object.new, StringIO.new, 101, strict: true).string } + + assert_equal too_deep, dump(obj, nil, nil, strict: false) + assert_equal too_deep, dump(obj, nil, 101, strict: false) + assert_equal too_deep, dump(obj, StringIO.new, nil, strict: false).string + assert_equal too_deep, dump(obj, nil, strict: false) + assert_equal too_deep, dump(obj, 101, strict: false) + assert_equal too_deep, dump(obj, StringIO.new, strict: false).string + assert_equal too_deep, dump(obj, strict: false) + end + + def test_dump_in_io + io = StringIO.new + assert_same io, JSON.dump([1], io) + assert_equal "[1]", io.string + + big_object = ["a" * 10, "b" * 40, { foo: 1.23 }] * 5000 + io.rewind + assert_same io, JSON.dump(big_object, io) + assert_equal JSON.dump(big_object), io.string end def test_dump_should_modify_defaults - max_nesting = JSON.dump_default_options[:max_nesting] + max_nesting = JSON._dump_default_options[:max_nesting] dump([], StringIO.new, 10) - assert_equal max_nesting, JSON.dump_default_options[:max_nesting] + assert_equal max_nesting, JSON._dump_default_options[:max_nesting] end def test_JSON assert_equal @json, JSON(@hash) + assert_equal @json, JSON(@hash_with_method_missing) assert_equal @hash, JSON(@json) end + + def test_load_file + test_load_shared(:load_file) + end + + def test_load_file! + test_load_shared(:load_file!) + end + + def test_load_file_with_option + test_load_file_with_option_shared(:load_file) + end + + def test_load_file_with_option! + test_load_file_with_option_shared(:load_file!) + end + + def test_load_file_with_bad_default_external_encoding + data = { "key" => "€" } + temp_file_containing(JSON.dump(data)) do |path| + loaded_data = with_external_encoding(Encoding::US_ASCII) do + JSON.load_file(path) + end + assert_equal data, loaded_data + end + end + + def test_deprecated_dump_default_options + assert_deprecated_warning(/dump_default_options/) do + JSON.dump_default_options + end + end + + private + + def with_external_encoding(encoding) + verbose = $VERBOSE + $VERBOSE = nil + previous_encoding = Encoding.default_external + Encoding.default_external = encoding + yield + ensure + Encoding.default_external = previous_encoding + $VERBOSE = verbose + end + + def test_load_shared(method_name) + temp_file_containing(@json) do |filespec| + assert_equal JSON.public_send(method_name, filespec), @hash + end + end + + def test_load_file_with_option_shared(method_name) + temp_file_containing(@json) do |filespec| + parsed_object = JSON.public_send(method_name, filespec, symbolize_names: true) + key_classes = parsed_object.keys.map(&:class) + assert_include(key_classes, Symbol) + assert_not_include(key_classes, String) + end + end + + def temp_file_containing(text, file_prefix = '') + raise "This method must be called with a code block." unless block_given? + + Tempfile.create(file_prefix) do |file| + file << text + file.close + yield file.path + end + end end diff --git a/test/json/json_encoding_test.rb b/test/json/json_encoding_test.rb index cc7b71553a..7ac06b2a7b 100644 --- a/test/json/json_encoding_test.rb +++ b/test/json/json_encoding_test.rb @@ -1,29 +1,20 @@ -# encoding: utf-8 -#frozen_string_literal: false -require 'test_helper' +# frozen_string_literal: true + +require_relative 'test_helper' class JSONEncodingTest < Test::Unit::TestCase include JSON def setup @utf_8 = '"© ≠ €!"' - @ascii_8bit = @utf_8.dup.force_encoding('ascii-8bit') + @ascii_8bit = @utf_8.b @parsed = "© ≠ €!" @generated = '"\u00a9 \u2260 \u20ac!"' - if String.method_defined?(:encode) - @utf_16_data = @parsed.encode('utf-16be', 'utf-8') - @utf_16be = @utf_8.encode('utf-16be', 'utf-8') - @utf_16le = @utf_8.encode('utf-16le', 'utf-8') - @utf_32be = @utf_8.encode('utf-32be', 'utf-8') - @utf_32le = @utf_8.encode('utf-32le', 'utf-8') - else - require 'iconv' - @utf_16_data, = Iconv.iconv('utf-16be', 'utf-8', @parsed) - @utf_16be, = Iconv.iconv('utf-16be', 'utf-8', @utf_8) - @utf_16le, = Iconv.iconv('utf-16le', 'utf-8', @utf_8) - @utf_32be, = Iconv.iconv('utf-32be', 'utf-8', @utf_8) - @utf_32le, = Iconv.iconv('utf-32le', 'utf-8', @utf_8) - end + @utf_16_data = @parsed.encode(Encoding::UTF_16BE, Encoding::UTF_8) + @utf_16be = @utf_8.encode(Encoding::UTF_16BE, Encoding::UTF_8) + @utf_16le = @utf_8.encode(Encoding::UTF_16LE, Encoding::UTF_8) + @utf_32be = @utf_8.encode(Encoding::UTF_32BE, Encoding::UTF_8) + @utf_32le = @utf_8.encode(Encoding::UTF_32LE, Encoding::UTF_8) end def test_parse @@ -36,8 +27,20 @@ class JSONEncodingTest < Test::Unit::TestCase end def test_generate - assert_equal @generated, JSON.generate(@parsed, :ascii_only => true) - assert_equal @generated, JSON.generate(@utf_16_data, :ascii_only => true) + assert_equal @generated, JSON.generate(@parsed, ascii_only: true) + assert_equal @generated, JSON.generate(@utf_16_data, ascii_only: true) + end + + def test_generate_shared_string + # Ref: https://github.com/ruby/json/issues/859 + s = "01234567890" + assert_equal '"234567890"', JSON.dump(s[2..-1]) + s = '01234567890123456789"a"b"c"d"e"f"g"h' + assert_equal '"\"a\"b\"c\"d\"e\"f\"g\""', JSON.dump(s[20, 15]) + s = "0123456789001234567890012345678900123456789001234567890" + assert_equal '"23456789001234567890012345678900123456789001234567890"', JSON.dump(s[2..-1]) + s = "0123456789001234567890012345678900123456789001234567890" + assert_equal '"567890012345678900123456789001234567890012345678"', JSON.dump(s[5..-3]) end def test_unicode @@ -47,37 +50,37 @@ class JSONEncodingTest < Test::Unit::TestCase assert_equal '"\u001f"', 0x1f.chr.to_json assert_equal '" "', ' '.to_json assert_equal "\"#{0x7f.chr}\"", 0x7f.chr.to_json - utf8 = [ "© ≠ €! \01" ] + utf8 = ["© ≠ €! \01"] json = '["© ≠ €! \u0001"]' - assert_equal json, utf8.to_json(:ascii_only => false) + assert_equal json, utf8.to_json(ascii_only: false) assert_equal utf8, parse(json) json = '["\u00a9 \u2260 \u20ac! \u0001"]' - assert_equal json, utf8.to_json(:ascii_only => true) + assert_equal json, utf8.to_json(ascii_only: true) assert_equal utf8, parse(json) utf8 = ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"] json = "[\"\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212\"]" assert_equal utf8, parse(json) - assert_equal json, utf8.to_json(:ascii_only => false) + assert_equal json, utf8.to_json(ascii_only: false) utf8 = ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"] assert_equal utf8, parse(json) json = "[\"\\u3042\\u3044\\u3046\\u3048\\u304a\"]" - assert_equal json, utf8.to_json(:ascii_only => true) + assert_equal json, utf8.to_json(ascii_only: true) assert_equal utf8, parse(json) utf8 = ['საქართველო'] json = '["საქართველო"]' - assert_equal json, utf8.to_json(:ascii_only => false) + assert_equal json, utf8.to_json(ascii_only: false) json = "[\"\\u10e1\\u10d0\\u10e5\\u10d0\\u10e0\\u10d7\\u10d5\\u10d4\\u10da\\u10dd\"]" - assert_equal json, utf8.to_json(:ascii_only => true) + assert_equal json, utf8.to_json(ascii_only: true) assert_equal utf8, parse(json) - assert_equal '["Ã"]', generate(["Ã"], :ascii_only => false) - assert_equal '["\\u00c3"]', generate(["Ã"], :ascii_only => true) + assert_equal '["Ã"]', generate(["Ã"], ascii_only: false) + assert_equal '["\\u00c3"]', generate(["Ã"], ascii_only: true) assert_equal ["€"], parse('["\u20ac"]') utf8 = ["\xf0\xa0\x80\x81"] json = "[\"\xf0\xa0\x80\x81\"]" - assert_equal json, generate(utf8, :ascii_only => false) + assert_equal json, generate(utf8, ascii_only: false) assert_equal utf8, parse(json) json = '["\ud840\udc01"]' - assert_equal json, generate(utf8, :ascii_only => true) + assert_equal json, generate(utf8, ascii_only: true) assert_equal utf8, parse(json) assert_raise(JSON::ParserError) { parse('"\u"') } assert_raise(JSON::ParserError) { parse('"\ud800"') } @@ -85,23 +88,188 @@ class JSONEncodingTest < Test::Unit::TestCase def test_chars (0..0x7f).each do |i| - json = '["\u%04x"]' % i - if RUBY_VERSION >= "1.9." - i = i.chr - end - assert_equal i, parse(json).first[0] - if i == ?\b - generated = generate(["" << i]) - assert '["\b"]' == generated || '["\10"]' == generated - elsif [?\n, ?\r, ?\t, ?\f].include?(i) - assert_equal '[' << ('' << i).dump << ']', generate(["" << i]) + json = '"\u%04x"' % i + i = i.chr + assert_equal i, parse(json)[0] + if i == "\b" + generated = generate(i) + assert ['"\b"', '"\10"'].include?(generated) + elsif ["\n", "\r", "\t", "\f"].include?(i) + assert_equal i.dump, generate(i) elsif i.chr < 0x20.chr - assert_equal json, generate(["" << i]) + assert_equal json, generate(i) end end assert_raise(JSON::GeneratorError) do - generate(["\x80"], :ascii_only => true) + generate(["\x80"], ascii_only: true) + end + assert_equal "\302\200", parse('"\u0080"') + end + + def test_deeply_nested_structures + # Test for deeply nested arrays + nesting_level = 100 + deeply_nested = [] + current = deeply_nested + + (nesting_level - 1).times do + current << [] + current = current[0] end - assert_equal "\302\200", parse('["\u0080"]').first + + json = generate(deeply_nested) + assert_equal deeply_nested, parse(json) + + # Test for deeply nested objects/hashes + deeply_nested_hash = {} + current_hash = deeply_nested_hash + + (nesting_level - 1).times do |i| + current_hash["key#{i}"] = {} + current_hash = current_hash["key#{i}"] + end + + json = generate(deeply_nested_hash) + assert_equal deeply_nested_hash, parse(json) + end + + def test_very_large_json_strings + # Create a large array with repeated elements + large_array = Array.new(10_000) { |i| "item#{i}" } + + json = generate(large_array) + parsed = parse(json) + + assert_equal large_array.size, parsed.size + assert_equal large_array.first, parsed.first + assert_equal large_array.last, parsed.last + + # Create a large hash + large_hash = {} + 10_000.times { |i| large_hash["key#{i}"] = "value#{i}" } + + json = generate(large_hash) + parsed = parse(json) + + assert_equal large_hash.size, parsed.size + assert_equal large_hash["key0"], parsed["key0"] + assert_equal large_hash["key9999"], parsed["key9999"] + end + + def test_invalid_utf8_sequences + invalid_utf8 = "\xFF\xFF" + error = assert_raise(JSON::GeneratorError) do + generate(invalid_utf8) + end + assert_match(%r{source sequence is illegal/malformed utf-8}, error.message) + end + + def test_surrogate_pair_handling + # Test valid surrogate pairs + assert_equal "\u{10000}", parse('"\ud800\udc00"') + assert_equal "\u{10FFFF}", parse('"\udbff\udfff"') + + # The existing test already checks for orphaned high surrogate + assert_raise(JSON::ParserError) { parse('"\ud800"') } + + # Test generating surrogate pairs + utf8_string = "\u{10437}" + generated = generate(utf8_string, ascii_only: true) + assert_match(/\\ud801\\udc37/, generated) + end + + def test_json_escaping_edge_cases + # Test escaping forward slashes + assert_equal "/", parse('"\/"') + + # Test escaping backslashes + assert_equal "\\", parse('"\\\\"') + + # Test escaping quotes + assert_equal '"', parse('"\\""') + + # Multiple escapes in sequence - different JSON parsers might handle escaped forward slashes differently + # Some parsers preserve the escaping, others don't + escaped_result = parse('"\\\\\\"\\/"') + assert_match(/\\"/, escaped_result) + assert_match(%r{/}, escaped_result) + + # Generate string with all special characters + special_chars = "\b\f\n\r\t\"\\" + escaped_json = generate(special_chars) + assert_equal special_chars, parse(escaped_json) + end + + def test_empty_objects_and_arrays + # Test empty objects with different encodings + assert_equal({}, parse('{}')) + assert_equal({}, parse('{}'.encode(Encoding::UTF_16BE))) + assert_equal({}, parse('{}'.encode(Encoding::UTF_16LE))) + assert_equal({}, parse('{}'.encode(Encoding::UTF_32BE))) + assert_equal({}, parse('{}'.encode(Encoding::UTF_32LE))) + + # Test empty arrays with different encodings + assert_equal([], parse('[]')) + assert_equal([], parse('[]'.encode(Encoding::UTF_16BE))) + assert_equal([], parse('[]'.encode(Encoding::UTF_16LE))) + assert_equal([], parse('[]'.encode(Encoding::UTF_32BE))) + assert_equal([], parse('[]'.encode(Encoding::UTF_32LE))) + + # Test generating empty objects and arrays + assert_equal '{}', generate({}) + assert_equal '[]', generate([]) + end + + def test_null_character_handling + # Test parsing null character + assert_equal "\u0000", parse('"\u0000"') + + # Test generating null character + string_with_null = "\u0000" + generated = generate(string_with_null) + assert_equal '"\u0000"', generated + + # Test null characters in middle of string + mixed_string = "before\u0000after" + generated = generate(mixed_string) + assert_equal mixed_string, parse(generated) + end + + def test_whitespace_handling + # Test parsing with various whitespace patterns + assert_equal({}, parse(' { } ')) + assert_equal({}, parse("{\r\n}")) + assert_equal([], parse(" [ \n ] ")) + assert_equal(["a", "b"], parse(" [ \n\"a\",\r\n \"b\"\n ] ")) + assert_equal({ "a" => "b" }, parse(" { \n\"a\" \r\n: \t\"b\"\n } ")) + + # Test with excessive whitespace + excessive_whitespace = " \n\r\t" * 10 + "{}" + " \n\r\t" * 10 + assert_equal({}, parse(excessive_whitespace)) + + # Mixed whitespace in keys and values + mixed_json = '{"a \n b":"c \r\n d"}' + assert_equal({ "a \n b" => "c \r\n d" }, parse(mixed_json)) + end + + def test_control_character_handling + # Test all control characters (U+0000 to U+001F) + (0..0x1F).each do |i| + # Skip already tested ones + next if [0x08, 0x0A, 0x0D, 0x0C, 0x09].include?(i) + + control_char = i.chr('UTF-8') + escaped_json = '"' + "\\u%04x" % i + '"' + assert_equal control_char, parse(escaped_json) + + # Check that the character is properly escaped when generating + assert_match(/\\u00[0-1][0-9a-f]/, generate(control_char)) + end + + # Test string with multiple control characters + control_str = "\u0001\u0002\u0003\u0004" + generated = generate(control_str) + assert_equal control_str, parse(generated) + assert_match(/\\u0001\\u0002\\u0003\\u0004/, generated) end end diff --git a/test/json/json_ext_parser_test.rb b/test/json/json_ext_parser_test.rb index c5a030ea8a..e610f642f1 100644 --- a/test/json/json_ext_parser_test.rb +++ b/test/json/json_ext_parser_test.rb @@ -1,15 +1,71 @@ -#frozen_string_literal: false -require 'test_helper' +# frozen_string_literal: true +require_relative 'test_helper' class JSONExtParserTest < Test::Unit::TestCase - if defined?(JSON::Ext::Parser) - def test_allocate - parser = JSON::Ext::Parser.new("{}") - assert_raise(TypeError, '[ruby-core:35079]') do - parser.__send__(:initialize, "{}") - end - parser = JSON::Ext::Parser.allocate - assert_raise(TypeError, '[ruby-core:35079]') { parser.source } + include JSON + + def test_allocate + parser = JSON::Ext::Parser.new("{}") + parser.__send__(:initialize, "{}") + assert_equal "{}", parser.source + + parser = JSON::Ext::Parser.allocate + assert_nil parser.source + end + + def test_error_messages + ex = assert_raise(ParserError) { parse('Infinity something') } + unless RUBY_PLATFORM =~ /java/ + assert_equal "unexpected token 'Infinity' at line 1 column 1", ex.message + end + + ex = assert_raise(ParserError) { parse('foo bar') } + unless RUBY_PLATFORM =~ /java/ + assert_equal "unexpected token 'foo' at line 1 column 1", ex.message + end + + ex = assert_raise(ParserError) { parse('-Infinity something') } + unless RUBY_PLATFORM =~ /java/ + assert_equal "unexpected token '-Infinity' at line 1 column 1", ex.message + end + + ex = assert_raise(ParserError) { parse('NaN something') } + unless RUBY_PLATFORM =~ /java/ + assert_equal "unexpected token 'NaN' at line 1 column 1", ex.message + end + + ex = assert_raise(ParserError) { parse(' ') } + unless RUBY_PLATFORM =~ /java/ + assert_equal "unexpected end of input at line 1 column 4", ex.message end + + ex = assert_raise(ParserError) { parse('{ ') } + unless RUBY_PLATFORM =~ /java/ + assert_equal "expected object key, got EOF at line 1 column 5", ex.message + end + end + + if GC.respond_to?(:stress=) + def test_gc_stress_parser_new + payload = JSON.dump([{ foo: 1, bar: 2, baz: 3, egg: { spam: 4 } }] * 10) + + previous_stress = GC.stress + JSON::Parser.new(payload).parse + ensure + GC.stress = previous_stress + end + + def test_gc_stress + payload = JSON.dump([{ foo: 1, bar: 2, baz: 3, egg: { spam: 4 } }] * 10) + + previous_stress = GC.stress + JSON.parse(payload) + ensure + GC.stress = previous_stress + end + end + + def parse(json) + JSON::Ext::Parser.new(json).parse end end diff --git a/test/json/json_fixtures_test.rb b/test/json/json_fixtures_test.rb index 01954fe707..c0d1037939 100644 --- a/test/json/json_fixtures_test.rb +++ b/test/json/json_fixtures_test.rb @@ -1,28 +1,24 @@ -#frozen_string_literal: false -require 'test_helper' +# frozen_string_literal: true +require_relative 'test_helper' class JSONFixturesTest < Test::Unit::TestCase - def setup - fixtures = File.join(File.dirname(__FILE__), 'fixtures/{fail,pass}.json') - passed, failed = Dir[fixtures].partition { |f| f['pass'] } - @passed = passed.inject([]) { |a, f| a << [ f, File.read(f) ] }.sort - @failed = failed.inject([]) { |a, f| a << [ f, File.read(f) ] }.sort - end + fixtures = File.join(File.dirname(__FILE__), 'fixtures/{fail,pass}*.json') + passed, failed = Dir[fixtures].partition { |f| f['pass'] } - def test_passing - for name, source in @passed - begin - assert JSON.parse(source), - "Did not pass for fixture '#{name}': #{source.inspect}" - rescue => e - warn "\nCaught #{e.class}(#{e}) for fixture '#{name}': #{source.inspect}\n#{e.backtrace * "\n"}" - raise e - end + passed.each do |f| + name = File.basename(f).gsub(".", "_") + source = File.read(f) + define_method("test_#{name}") do + assert JSON.parse(source), "Did not pass for fixture '#{File.basename(f)}': #{source.inspect}" + rescue JSON::ParserError + raise "#{File.basename(f)} parsing failure" end end - def test_failing - for name, source in @failed + failed.each do |f| + name = File.basename(f).gsub(".", "_") + source = File.read(f) + define_method("test_#{name}") do assert_raise(JSON::ParserError, JSON::NestingError, "Did not fail for fixture '#{name}': #{source.inspect}") do JSON.parse(source) diff --git a/test/json/json_generator_test.rb b/test/json/json_generator_test.rb index 86be398f46..d7c4173e8e 100644..100755 --- a/test/json/json_generator_test.rb +++ b/test/json/json_generator_test.rb @@ -1,8 +1,7 @@ #!/usr/bin/env ruby -# encoding: utf-8 -# frozen_string_literal: false +# frozen_string_literal: true -require 'test_helper' +require_relative 'test_helper' class JSONGeneratorTest < Test::Unit::TestCase include JSON @@ -20,24 +19,32 @@ class JSONGeneratorTest < Test::Unit::TestCase } @json2 = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},' + '"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}' - @json3 = <<'EOT'.chomp -{ - "a": 2, - "b": 3.141, - "c": "c", - "d": [ - 1, - "b", - 3.14 - ], - "e": { - "foo": "bar" - }, - "g": "\"\u0000\u001f", - "h": 1000.0, - "i": 0.001 -} -EOT + @json3 = <<~'JSON'.chomp + { + "a": 2, + "b": 3.141, + "c": "c", + "d": [ + 1, + "b", + 3.14 + ], + "e": { + "foo": "bar" + }, + "g": "\"\u0000\u001f", + "h": 1000.0, + "i": 0.001 + } + JSON + end + + def silence + v = $VERBOSE + $VERBOSE = nil + yield + ensure + $VERBOSE = v end def test_generate @@ -54,7 +61,90 @@ EOT assert_equal '666', generate(666) end + def test_dump_unenclosed_hash + assert_equal '{"a":1,"b":2}', dump(a: 1, b: 2) + end + + def test_dump_strict + assert_equal '{}', dump({}, strict: true) + + assert_equal '{"array":[42,4.2,"forty-two",true,false,null]}', dump({ + "array" => [42, 4.2, "forty-two", true, false, nil] + }, strict: true) + + assert_equal '{"int":42,"float":4.2,"string":"forty-two","true":true,"false":false,"nil":null,"hash":{}}', dump({ + "int" => 42, + "float" => 4.2, + "string" => "forty-two", + "true" => true, + "false" => false, + "nil" => nil, + "hash" => {}, + }, strict: true) + + assert_equal '[]', dump([], strict: true) + + assert_equal '42', dump(42, strict: true) + assert_equal 'true', dump(true, strict: true) + + assert_equal '"hello"', dump(:hello, strict: true) + assert_equal '"hello"', :hello.to_json(strict: true) + assert_equal '"World"', "World".to_json(strict: true) + end + + def test_state_depth_to_json + depth = Object.new + def depth.to_json(state) + JSON::State.from_state(state).depth.to_s + end + + assert_equal "0", JSON.generate(depth) + assert_equal "[1]", JSON.generate([depth]) + assert_equal %({"depth":1}), JSON.generate(depth: depth) + assert_equal "[[2]]", JSON.generate([[depth]]) + assert_equal %([{"depth":2}]), JSON.generate([{depth: depth}]) + + state = JSON::State.new + assert_equal "0", state.generate(depth) + assert_equal "[1]", state.generate([depth]) + assert_equal %({"depth":1}), state.generate(depth: depth) + assert_equal "[[2]]", state.generate([[depth]]) + assert_equal %([{"depth":2}]), state.generate([{depth: depth}]) + end + + def test_state_depth_to_json_recursive + recur = Object.new + def recur.to_json(state = nil, *) + state = JSON::State.from_state(state) + if state.depth < 3 + state.generate([state.depth, self]) + else + state.generate([state.depth]) + end + end + + assert_raise(NestingError) { JSON.generate(recur, max_nesting: 3) } + assert_equal "[0,[1,[2,[3]]]]", JSON.generate(recur, max_nesting: 4) + + state = JSON::State.new(max_nesting: 3) + assert_raise(NestingError) { state.generate(recur) } + state.max_nesting = 4 + assert_equal "[0,[1,[2,[3]]]]", JSON.generate(recur, max_nesting: 4) + end + def test_generate_pretty + json = pretty_generate({}) + assert_equal('{}', json) + + json = pretty_generate({1=>{}, 2=>[], 3=>4}) + assert_equal(<<~'JSON'.chomp, json) + { + "1": {}, + "2": [], + "3": 4 + } + JSON + json = pretty_generate(@hash) # hashes aren't (insertion) ordered on every ruby implementation # assert_equal(@json3, json) @@ -62,39 +152,57 @@ EOT parsed_json = parse(json) assert_equal(@hash, parsed_json) json = pretty_generate({1=>2}) - assert_equal(<<'EOT'.chomp, json) -{ - "1": 2 -} -EOT + assert_equal(<<~'JSON'.chomp, json) + { + "1": 2 + } + JSON parsed_json = parse(json) assert_equal({"1"=>2}, parsed_json) assert_equal '666', pretty_generate(666) end + def test_generate_pretty_custom + state = State.new(:space_before => "<psb>", :space => "<ps>", :indent => "<pi>", :object_nl => "\n<po_nl>\n", :array_nl => "<pa_nl>") + json = pretty_generate({1=>{}, 2=>['a','b'], 3=>4}, state) + assert_equal(<<~'JSON'.chomp, json) + { + <po_nl> + <pi>"1"<psb>:<ps>{}, + <po_nl> + <pi>"2"<psb>:<ps>[<pa_nl><pi><pi>"a",<pa_nl><pi><pi>"b"<pa_nl><pi>], + <po_nl> + <pi>"3"<psb>:<ps>4 + <po_nl> + } + JSON + end + def test_generate_custom state = State.new(:space_before => " ", :space => " ", :indent => "<i>", :object_nl => "\n", :array_nl => "<a_nl>") json = generate({1=>{2=>3,4=>[5,6]}}, state) - assert_equal(<<'EOT'.chomp, json) -{ -<i>"1" : { -<i><i>"2" : 3, -<i><i>"4" : [<a_nl><i><i><i>5,<a_nl><i><i><i>6<a_nl><i><i>] -<i>} -} -EOT + assert_equal(<<~'JSON'.chomp, json) + { + <i>"1" : { + <i><i>"2" : 3, + <i><i>"4" : [<a_nl><i><i><i>5,<a_nl><i><i><i>6<a_nl><i><i>] + <i>} + } + JSON end def test_fast_generate - json = fast_generate(@hash) - assert_equal(parse(@json2), parse(json)) - parsed_json = parse(json) - assert_equal(@hash, parsed_json) - json = fast_generate({1=>2}) - assert_equal('{"1":2}', json) - parsed_json = parse(json) - assert_equal({"1"=>2}, parsed_json) - assert_equal '666', fast_generate(666) + assert_deprecated_warning(/fast_generate/) do + json = fast_generate(@hash) + assert_equal(parse(@json2), parse(json)) + parsed_json = parse(json) + assert_equal(@hash, parsed_json) + json = fast_generate({1=>2}) + assert_equal('{"1":2}', json) + parsed_json = parse(json) + assert_equal({"1"=>2}, parsed_json) + assert_equal '666', fast_generate(666) + end end def test_own_state @@ -115,7 +223,9 @@ EOT assert_equal('{"1":2}', json) s = JSON.state.new assert s.check_circular? - assert s[:check_circular?] + assert_deprecated_warning(/JSON::State/) do + assert s[:check_circular?] + end h = { 1=>2 } h[3] = h assert_raise(JSON::NestingError) { generate(h) } @@ -125,51 +235,63 @@ EOT a << a assert_raise(JSON::NestingError) { generate(a, s) } assert s.check_circular? - assert s[:check_circular?] + assert_deprecated_warning(/JSON::State/) do + assert s[:check_circular?] + end end - def test_pretty_state - state = PRETTY_STATE_PROTOTYPE.dup - assert_equal({ - :allow_nan => false, - :array_nl => "\n", - :ascii_only => false, - :buffer_initial_length => 1024, - :depth => 0, - :indent => " ", - :max_nesting => 100, - :object_nl => "\n", - :space => " ", - :space_before => "", - }.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s }) + def test_falsy_state + object = { foo: [1, 2], bar: { egg: :spam }} + expected_json = JSON.generate( + object, + array_nl: "", + indent: "", + object_nl: "", + space: "", + space_before: "", + ) + + assert_equal expected_json, JSON.generate( + object, + array_nl: nil, + indent: nil, + object_nl: nil, + space: nil, + space_before: nil, + ) end - def test_safe_state - state = SAFE_STATE_PROTOTYPE.dup + def test_state_defaults + state = JSON::State.new assert_equal({ :allow_nan => false, :array_nl => "", + :as_json => false, :ascii_only => false, :buffer_initial_length => 1024, :depth => 0, + :script_safe => false, + :strict => false, :indent => "", :max_nesting => 100, :object_nl => "", :space => "", :space_before => "", }.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s }) - end - def test_fast_state - state = FAST_STATE_PROTOTYPE.dup + state = JSON::State.new(allow_duplicate_key: true) assert_equal({ + :allow_duplicate_key => true, :allow_nan => false, :array_nl => "", + :as_json => false, :ascii_only => false, :buffer_initial_length => 1024, :depth => 0, + :script_safe => false, + :strict => false, :indent => "", - :max_nesting => 0, + :max_nesting => 100, :object_nl => "", :space => "", :space_before => "", @@ -177,35 +299,122 @@ EOT end def test_allow_nan - assert_raise(GeneratorError) { generate([JSON::NaN]) } - assert_equal '[NaN]', generate([JSON::NaN], :allow_nan => true) - assert_raise(GeneratorError) { fast_generate([JSON::NaN]) } - assert_raise(GeneratorError) { pretty_generate([JSON::NaN]) } - assert_equal "[\n NaN\n]", pretty_generate([JSON::NaN], :allow_nan => true) - assert_raise(GeneratorError) { generate([JSON::Infinity]) } - assert_equal '[Infinity]', generate([JSON::Infinity], :allow_nan => true) - assert_raise(GeneratorError) { fast_generate([JSON::Infinity]) } - assert_raise(GeneratorError) { pretty_generate([JSON::Infinity]) } - assert_equal "[\n Infinity\n]", pretty_generate([JSON::Infinity], :allow_nan => true) - assert_raise(GeneratorError) { generate([JSON::MinusInfinity]) } - assert_equal '[-Infinity]', generate([JSON::MinusInfinity], :allow_nan => true) - assert_raise(GeneratorError) { fast_generate([JSON::MinusInfinity]) } - assert_raise(GeneratorError) { pretty_generate([JSON::MinusInfinity]) } - assert_equal "[\n -Infinity\n]", pretty_generate([JSON::MinusInfinity], :allow_nan => true) + assert_deprecated_warning(/fast_generate/) do + error = assert_raise(GeneratorError) { generate([JSON::NaN]) } + assert_same JSON::NaN, error.invalid_object + assert_equal '[NaN]', generate([JSON::NaN], :allow_nan => true) + assert_raise(GeneratorError) { fast_generate([JSON::NaN]) } + assert_raise(GeneratorError) { pretty_generate([JSON::NaN]) } + assert_equal "[\n NaN\n]", pretty_generate([JSON::NaN], :allow_nan => true) + error = assert_raise(GeneratorError) { generate([JSON::Infinity]) } + assert_same JSON::Infinity, error.invalid_object + assert_equal '[Infinity]', generate([JSON::Infinity], :allow_nan => true) + assert_raise(GeneratorError) { fast_generate([JSON::Infinity]) } + assert_raise(GeneratorError) { pretty_generate([JSON::Infinity]) } + assert_equal "[\n Infinity\n]", pretty_generate([JSON::Infinity], :allow_nan => true) + error = assert_raise(GeneratorError) { generate([JSON::MinusInfinity]) } + assert_same JSON::MinusInfinity, error.invalid_object + assert_equal '[-Infinity]', generate([JSON::MinusInfinity], :allow_nan => true) + assert_raise(GeneratorError) { fast_generate([JSON::MinusInfinity]) } + assert_raise(GeneratorError) { pretty_generate([JSON::MinusInfinity]) } + assert_equal "[\n -Infinity\n]", pretty_generate([JSON::MinusInfinity], :allow_nan => true) + end + end + + # An object that changes state.depth when it receives to_json(state) + def bad_to_json + obj = Object.new + def obj.to_json(state) + state.depth += 1 + "{#{state.object_nl}"\ + "#{state.indent * state.depth}\"foo\":#{state.space}1#{state.object_nl}"\ + "#{state.indent * (state.depth - 1)}}" + end + obj + end + + def test_depth_restored_bad_to_json + state = JSON::State.new + state.generate(bad_to_json) + assert_equal 0, state.depth + end + + def test_depth_restored_bad_to_json_in_Array + assert_equal <<~JSON.chomp, JSON.pretty_generate([bad_to_json] * 2) + [ + { + "foo": 1 + }, + { + "foo": 1 + } + ] + JSON + state = JSON::State.new + state.generate([bad_to_json]) + assert_equal 0, state.depth + end + + def test_depth_restored_bad_to_json_in_Hash + assert_equal <<~JSON.chomp, JSON.pretty_generate(a: bad_to_json, b: bad_to_json) + { + "a": { + "foo": 1 + }, + "b": { + "foo": 1 + } + } + JSON + state = JSON::State.new + state.generate(a: bad_to_json) + assert_equal 0, state.depth end def test_depth + pretty = { object_nl: "\n", array_nl: "\n", space: " ", indent: " " } + state = JSON.state.new(**pretty) + assert_equal %({\n "foo": 42\n}), JSON.generate({ foo: 42 }, pretty) + assert_equal %({\n "foo": 42\n}), state.generate(foo: 42) + state.depth = 1 + assert_equal %({\n "foo": 42\n }), JSON.generate({ foo: 42 }, pretty.merge(depth: 1)) + assert_equal %({\n "foo": 42\n }), state.generate(foo: 42) + end + + def test_depth_nesting_error ary = []; ary << ary - assert_equal 0, JSON::SAFE_STATE_PROTOTYPE.depth assert_raise(JSON::NestingError) { generate(ary) } - assert_equal 0, JSON::SAFE_STATE_PROTOTYPE.depth - assert_equal 0, JSON::PRETTY_STATE_PROTOTYPE.depth assert_raise(JSON::NestingError) { JSON.pretty_generate(ary) } - assert_equal 0, JSON::PRETTY_STATE_PROTOTYPE.depth - s = JSON.state.new - assert_equal 0, s.depth + end + + def test_depth_nesting_error_to_json + ary = []; ary << ary + s = JSON.state.new(depth: 1) assert_raise(JSON::NestingError) { ary.to_json(s) } - assert_equal 100, s.depth + assert_equal 1, s.depth + end + + def test_depth_nesting_error_Hash_to_json + hash = {}; hash[:a] = hash + s = JSON.state.new(depth: 1) + assert_raise(JSON::NestingError) { hash.to_json(s) } + assert_equal 1, s.depth + end + + def test_depth_nesting_error_generate + ary = []; ary << ary + s = JSON.state.new(depth: 1) + assert_raise(JSON::NestingError) { s.generate(ary) } + assert_equal 1, s.depth + end + + def test_depth_exception_calling_to_json + def (obj = Object.new).to_json(*) + raise + end + s = JSON.state.new(depth: 1).freeze + assert_raise(RuntimeError) { s.generate([{ hash: obj }]) } + assert_equal 1, s.depth end def test_buffer_initial_length @@ -220,19 +429,19 @@ EOT end def test_gc - if respond_to?(:assert_in_out_err) - assert_in_out_err(%w[-rjson --disable-gems], <<-EOS, [], []) - bignum_too_long_to_embed_as_string = 1234567890123456789012345 - expect = bignum_too_long_to_embed_as_string.to_s - GC.stress = true - - 10.times do |i| - tmp = bignum_too_long_to_embed_as_string.to_json - raise "'\#{expect}' is expected, but '\#{tmp}'" unless tmp == expect - end - EOS + pid = fork do + bignum_too_long_to_embed_as_string = 1234567890123456789012345 + expect = bignum_too_long_to_embed_as_string.to_s + GC.stress = true + + 10.times do |i| + tmp = bignum_too_long_to_embed_as_string.to_json + raise "#{expect}' is expected, but '#{tmp}'" unless tmp == expect + end end - end if GC.respond_to?(:stress=) + _, status = Process.waitpid2(pid) + assert_predicate status, :success? + end if GC.respond_to?(:stress=) && Process.respond_to?(:fork) def test_configure_using_configure_and_merge numbered_state = { @@ -263,68 +472,101 @@ EOT state.configure(:indent => '1') assert_equal '1', state.indent state = JSON.state.new - foo = 'foo' + foo = 'foo'.dup assert_raise(TypeError) do state.configure(foo) end def foo.to_h - { :indent => '2' } + { indent: '2' } end state.configure(foo) assert_equal '2', state.indent end - if defined?(JSON::Ext::Generator) - def test_broken_bignum # [ruby-core:38867] - pid = fork do - x = 1 << 64 - x.class.class_eval do - def to_s - end - end - begin - JSON::Ext::Generator::State.new.generate(x) - exit 1 - rescue TypeError - exit 0 + def test_broken_bignum # [ruby-core:38867] + pid = fork do + x = 1 << 64 + x.class.class_eval do + def to_s end end - _, status = Process.waitpid2(pid) - assert status.success? - rescue NotImplementedError - # forking to avoid modifying core class of a parent process and - # introducing race conditions of tests are run in parallel + begin + JSON::Ext::Generator::State.new.generate(x) + exit 1 + rescue TypeError + exit 0 + end end + _, status = Process.waitpid2(pid) + assert status.success? + rescue NotImplementedError + # forking to avoid modifying core class of a parent process and + # introducing race conditions of tests are run in parallel end def test_hash_likeness_set_symbol - state = JSON.state.new - assert_equal nil, state[:foo] - assert_equal nil.class, state[:foo].class - assert_equal nil, state['foo'] - state[:foo] = :bar - assert_equal :bar, state[:foo] - assert_equal :bar, state['foo'] - state_hash = state.to_hash - assert_kind_of Hash, state_hash - assert_equal :bar, state_hash[:foo] + assert_deprecated_warning(/JSON::State/) do + state = JSON.state.new + assert_equal nil, state[:foo] + assert_equal nil.class, state[:foo].class + assert_equal nil, state['foo'] + state[:foo] = :bar + assert_equal :bar, state[:foo] + assert_equal :bar, state['foo'] + state_hash = state.to_hash + assert_kind_of Hash, state_hash + assert_equal :bar, state_hash[:foo] + end end def test_hash_likeness_set_string + assert_deprecated_warning(/JSON::State/) do + state = JSON.state.new + assert_equal nil, state[:foo] + assert_equal nil, state['foo'] + state['foo'] = :bar + assert_equal :bar, state[:foo] + assert_equal :bar, state['foo'] + state_hash = state.to_hash + assert_kind_of Hash, state_hash + assert_equal :bar, state_hash[:foo] + end + end + + def test_json_state_to_h_roundtrip state = JSON.state.new - assert_equal nil, state[:foo] - assert_equal nil, state['foo'] - state['foo'] = :bar - assert_equal :bar, state[:foo] - assert_equal :bar, state['foo'] - state_hash = state.to_hash - assert_kind_of Hash, state_hash - assert_equal :bar, state_hash[:foo] + assert_equal state.to_h, JSON.state.new(state.to_h).to_h end def test_json_generate assert_raise JSON::GeneratorError do - assert_equal true, generate(["\xea"]) + generate(["\xea"]) + end + end + + def test_json_generate_error_detailed_message + error = assert_raise JSON::GeneratorError do + generate(["\xea"]) + end + + assert_not_nil(error.detailed_message) + end + + def test_json_generate_unsupported_types + assert_raise JSON::GeneratorError do + generate(Object.new, strict: true) + end + + assert_raise JSON::GeneratorError do + generate([Object.new], strict: true) + end + + assert_raise JSON::GeneratorError do + generate({ "key" => Object.new }, strict: true) + end + + assert_raise JSON::GeneratorError do + generate({ Object.new => "value" }, strict: true) end end @@ -348,21 +590,155 @@ EOT json = '["\\\\.(?i:gif|jpe?g|png)$"]' assert_equal json, generate(data) # - data = [ '\\"' ] - json = '["\\\\\""]' + data = [ '\\.(?i:gif|jpe?g|png)$\\.(?i:gif|jpe?g|png)$\\.(?i:gif|jpe?g|png)$\\.(?i:gif|jpe?g|png)$\\.(?i:gif|jpe?g|png)$\\.(?i:gif|jpe?g|png)$\\.(?i:gif|jpe?g|png)$\\.(?i:gif|jpe?g|png)$\\.(?i:gif|jpe?g|png)$\\.(?i:gif|jpe?g|png)$\\.(?i:gif|jpe?g|png)$' ] + json = '["\\\\.(?i:gif|jpe?g|png)$\\\\.(?i:gif|jpe?g|png)$\\\\.(?i:gif|jpe?g|png)$\\\\.(?i:gif|jpe?g|png)$\\\\.(?i:gif|jpe?g|png)$\\\\.(?i:gif|jpe?g|png)$\\\\.(?i:gif|jpe?g|png)$\\\\.(?i:gif|jpe?g|png)$\\\\.(?i:gif|jpe?g|png)$\\\\.(?i:gif|jpe?g|png)$\\\\.(?i:gif|jpe?g|png)$"]' + assert_equal json, generate(data) + # + data = [ '\\"\\"\\"\\"\\"\\"\\"\\"\\"\\"\\"' ] + json = '["\\\\\"\\\\\"\\\\\"\\\\\"\\\\\"\\\\\"\\\\\"\\\\\"\\\\\"\\\\\"\\\\\""]' assert_equal json, generate(data) # data = [ '/' ] json = '["/"]' assert_equal json, generate(data) # + data = [ '////////////////////////////////////////////////////////////////////////////////////' ] + json = '["////////////////////////////////////////////////////////////////////////////////////"]' + assert_equal json, generate(data) + # + data = [ '/' ] + json = '["\/"]' + assert_equal json, generate(data, :script_safe => true) + # + data = [ '///////////' ] + json = '["\/\/\/\/\/\/\/\/\/\/\/"]' + assert_equal json, generate(data, :script_safe => true) + # + data = [ '///////////////////////////////////////////////////////' ] + json = '["\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/"]' + assert_equal json, generate(data, :script_safe => true) + # + data = [ "\u2028\u2029" ] + json = '["\u2028\u2029"]' + assert_equal json, generate(data, :script_safe => true) + # + data = [ "ABC \u2028 DEF \u2029 GHI" ] + json = '["ABC \u2028 DEF \u2029 GHI"]' + assert_equal json, generate(data, :script_safe => true) + # + data = [ "/\u2028\u2029" ] + json = '["\/\u2028\u2029"]' + assert_equal json, generate(data, :escape_slash => true) + # data = ['"'] json = '["\""]' assert_equal json, generate(data) # + data = ['"""""""""""""""""""""""""'] + json = '["\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\""]' + assert_equal json, generate(data) + # + data = '"""""' + json = '"\"\"\"\"\""' + assert_equal json, generate(data) + # + data = "abc\n" + json = '"abc\\n"' + assert_equal json, generate(data) + # + data = "\nabc" + json = '"\\nabc"' + assert_equal json, generate(data) + # + data = "\n" + json = '"\\n"' + assert_equal json, generate(data) + # + (0..16).each do |i| + data = ('a' * i) + "\n" + json = '"' + ('a' * i) + '\\n"' + assert_equal json, generate(data) + end + # + (0..16).each do |i| + data = "\n" + ('a' * i) + json = '"' + '\\n' + ('a' * i) + '"' + assert_equal json, generate(data) + end + # data = ["'"] json = '["\\\'"]' assert_equal '["\'"]', generate(data) + # + data = ["倩", "瀨"] + json = '["倩","瀨"]' + assert_equal json, generate(data, script_safe: true) + # + data = '["This is a "test" of the emergency broadcast system."]' + json = "\"[\\\"This is a \\\"test\\\" of the emergency broadcast system.\\\"]\"" + assert_equal json, generate(data) + # + data = '\tThis is a test of the emergency broadcast system.' + json = "\"\\\\tThis is a test of the emergency broadcast system.\"" + assert_equal json, generate(data) + # + data = 'This\tis a test of the emergency broadcast system.' + json = "\"This\\\\tis a test of the emergency broadcast system.\"" + assert_equal json, generate(data) + # + data = 'This is\ta test of the emergency broadcast system.' + json = "\"This is\\\\ta test of the emergency broadcast system.\"" + assert_equal json, generate(data) + # + data = 'This is a test of the emergency broadcast\tsystem.' + json = "\"This is a test of the emergency broadcast\\\\tsystem.\"" + assert_equal json, generate(data) + # + data = 'This is a test of the emergency broadcast\tsystem.\n' + json = "\"This is a test of the emergency broadcast\\\\tsystem.\\\\n\"" + assert_equal json, generate(data) + data = '"' * 15 + json = "\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\"" + assert_equal json, generate(data) + data = "\"\"\"\"\"\"\"\"\"\"\"\"\"\"a" + json = "\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"a\"" + assert_equal json, generate(data) + data = "\u0001\u0001\u0001\u0001" + json = "\"\\u0001\\u0001\\u0001\\u0001\"" + assert_equal json, generate(data) + data = "\u0001a\u0001a\u0001a\u0001a" + json = "\"\\u0001a\\u0001a\\u0001a\\u0001a\"" + assert_equal json, generate(data) + data = "\u0001aa\u0001aa" + json = "\"\\u0001aa\\u0001aa\"" + assert_equal json, generate(data) + data = "\u0001aa\u0001aa\u0001aa" + json = "\"\\u0001aa\\u0001aa\\u0001aa\"" + assert_equal json, generate(data) + data = "\u0001aa\u0001aa\u0001aa\u0001aa\u0001aa\u0001aa" + json = "\"\\u0001aa\\u0001aa\\u0001aa\\u0001aa\\u0001aa\\u0001aa\"" + assert_equal json, generate(data) + data = "\u0001a\u0002\u0001a\u0002\u0001a\u0002\u0001a\u0002\u0001a\u0002\u0001a\u0002\u0001a\u0002\u0001a\u0002" + json = "\"\\u0001a\\u0002\\u0001a\\u0002\\u0001a\\u0002\\u0001a\\u0002\\u0001a\\u0002\\u0001a\\u0002\\u0001a\\u0002\\u0001a\\u0002\"" + assert_equal json, generate(data) + data = "ab\u0002c" + json = "\"ab\\u0002c\"" + assert_equal json, generate(data) + data = "ab\u0002cab\u0002cab\u0002cab\u0002c" + json = "\"ab\\u0002cab\\u0002cab\\u0002cab\\u0002c\"" + assert_equal json, generate(data) + data = "ab\u0002cab\u0002cab\u0002cab\u0002cab\u0002cab\u0002c" + json = "\"ab\\u0002cab\\u0002cab\\u0002cab\\u0002cab\\u0002cab\\u0002c\"" + assert_equal json, generate(data) + data = "\n\t\f\b\n\t\f\b\n\t\f\b\n\t\f" + json = "\"\\n\\t\\f\\b\\n\\t\\f\\b\\n\\t\\f\\b\\n\\t\\f\"" + assert_equal json, generate(data) + data = "\n\t\f\b\n\t\f\b\n\t\f\b\n\t\f\b" + json = "\"\\n\\t\\f\\b\\n\\t\\f\\b\\n\\t\\f\\b\\n\\t\\f\\b\"" + assert_equal json, generate(data) + data = "a\n\t\f\b\n\t\f\b\n\t\f\b\n\t" + json = "\"a\\n\\t\\f\\b\\n\\t\\f\\b\\n\\t\\f\\b\\n\\t\"" + assert_equal json, generate(data) end def test_string_subclass @@ -374,4 +750,330 @@ EOT assert_equal '["foo"]', JSON.generate([s.new('foo')]) end end + + def test_invalid_encoding_string + error = assert_raise(JSON::GeneratorError) do + "\x82\xAC\xEF".to_json + end + assert_includes error.message, "source sequence is illegal/malformed utf-8" + + error = assert_raise(JSON::GeneratorError) do + JSON.dump("\x82\xAC\xEF") + end + assert_includes error.message, "source sequence is illegal/malformed utf-8" + + assert_raise(JSON::GeneratorError) do + JSON.dump("\x82\xAC\xEF".b) + end + + assert_raise(JSON::GeneratorError) do + "\x82\xAC\xEF".b.to_json + end + + assert_raise(JSON::GeneratorError) do + ["\x82\xAC\xEF".b].to_json + end + + badly_encoded = "\x82\xAC\xEF".b + exception = assert_raise(JSON::GeneratorError) do + { foo: badly_encoded }.to_json + end + + assert_kind_of EncodingError, exception.cause + assert_same badly_encoded, exception.invalid_object + end + + class MyCustomString < String + def to_json(_state = nil) + '"my_custom_key"' + end + + def to_s + self + end + end + + def test_string_subclass_as_keys + # Ref: https://github.com/ruby/json/issues/667 + # if key.to_s doesn't return a bare string, we call `to_json` on it. + key = MyCustomString.new("won't be used") + assert_equal '{"my_custom_key":1}', JSON.generate(key => 1) + end + + class FakeString + def to_json(_state = nil) + raise "Shouldn't be called" + end + + def to_s + self + end + end + + def test_custom_object_as_keys + key = FakeString.new + error = assert_raise(TypeError) do + JSON.generate(key => 1) + end + assert_match "FakeString", error.message + end + + def test_to_json_called_with_state_object + object = Object.new + called = false + argument = nil + object.singleton_class.define_method(:to_json) do |state| + called = true + argument = state + "<hello>" + end + + assert_equal "<hello>", JSON.dump(object) + assert called, "#to_json wasn't called" + assert_instance_of JSON::State, argument + end + + module CustomToJSON + def to_json(*) + %{"#{self.class.name}#to_json"} + end + end + + module CustomToS + def to_s + "#{self.class.name}#to_s" + end + end + + class ArrayWithToJSON < Array + include CustomToJSON + end + + def test_array_subclass_with_to_json + assert_equal '["JSONGeneratorTest::ArrayWithToJSON#to_json"]', JSON.generate([ArrayWithToJSON.new]) + assert_equal '{"[]":1}', JSON.generate(ArrayWithToJSON.new => 1) + end + + class ArrayWithToS < Array + include CustomToS + end + + def test_array_subclass_with_to_s + assert_equal '[[]]', JSON.generate([ArrayWithToS.new]) + assert_equal '{"JSONGeneratorTest::ArrayWithToS#to_s":1}', JSON.generate(ArrayWithToS.new => 1) + end + + class HashWithToJSON < Hash + include CustomToJSON + end + + def test_hash_subclass_with_to_json + assert_equal '["JSONGeneratorTest::HashWithToJSON#to_json"]', JSON.generate([HashWithToJSON.new]) + assert_equal '{"{}":1}', JSON.generate(HashWithToJSON.new => 1) + end + + class HashWithToS < Hash + include CustomToS + end + + def test_hash_subclass_with_to_s + assert_equal '[{}]', JSON.generate([HashWithToS.new]) + assert_equal '{"JSONGeneratorTest::HashWithToS#to_s":1}', JSON.generate(HashWithToS.new => 1) + end + + class StringWithToJSON < String + include CustomToJSON + end + + def test_string_subclass_with_to_json + assert_equal '["JSONGeneratorTest::StringWithToJSON#to_json"]', JSON.generate([StringWithToJSON.new]) + assert_equal '{"":1}', JSON.generate(StringWithToJSON.new => 1) + end + + class StringWithToS < String + include CustomToS + end + + def test_string_subclass_with_to_s + assert_equal '[""]', JSON.generate([StringWithToS.new]) + assert_equal '{"JSONGeneratorTest::StringWithToS#to_s":1}', JSON.generate(StringWithToS.new => 1) + end + + def test_string_subclass_with_broken_to_s + klass = Class.new(String) do + def to_s + false + end + end + s = klass.new("test") + assert_equal '["test"]', JSON.generate([s]) + + omit("Can't figure out how to match behavior in java code") if RUBY_PLATFORM == "java" + + assert_raise TypeError do + JSON.generate(s => 1) + end + end + + if defined?(JSON::Ext::Generator) and RUBY_PLATFORM != "java" + def test_valid_utf8_in_different_encoding + utf8_string = "€™" + wrong_encoding_string = utf8_string.b + # This behavior is historical. Not necessary desirable. We should deprecated it. + # The pure and java version of the gem already don't behave this way. + assert_warning(/UTF-8 string passed as BINARY, this will raise an encoding error in json 3.0/) do + assert_equal utf8_string.to_json, wrong_encoding_string.to_json + end + + assert_warning(/UTF-8 string passed as BINARY, this will raise an encoding error in json 3.0/) do + assert_equal JSON.dump(utf8_string), JSON.dump(wrong_encoding_string) + end + end + + def test_string_ext_included_calls_super + included = false + + Module.send(:alias_method, :included_orig, :included) + Module.send(:remove_method, :included) + Module.send(:define_method, :included) do |base| + included_orig(base) + included = true + end + + Class.new(String) do + include JSON::Ext::Generator::GeneratorMethods::String + end + + assert included + ensure + if Module.private_method_defined?(:included_orig) + Module.send(:remove_method, :included) if Module.method_defined?(:included) + Module.send(:alias_method, :included, :included_orig) + Module.send(:remove_method, :included_orig) + end + end + end + + def test_nonutf8_encoding + assert_equal("\"5\u{b0}\"", "5\xb0".dup.force_encoding(Encoding::ISO_8859_1).to_json) + end + + def test_utf8_multibyte + assert_equal('["foßbar"]', JSON.generate(["foßbar"])) + assert_equal('"n€ßt€ð2"', JSON.generate("n€ßt€ð2")) + assert_equal('"\"\u0000\u001f"', JSON.generate("\"\u0000\u001f")) + end + + def test_fragment + fragment = JSON::Fragment.new(" 42") + assert_equal '{"number": 42}', JSON.generate({ number: fragment }) + assert_equal '{"number": 42}', JSON.generate({ number: fragment }, strict: true) + end + + def test_json_generate_as_json_convert_to_proc + object = Object.new + assert_equal object.object_id.to_json, JSON.generate(object, strict: true, as_json: -> (o, is_key) { o.object_id }) + end + + def test_as_json_nan_does_not_call_to_json + def (obj = Object.new).to_json(*) + "null" + end + assert_raise(JSON::GeneratorError) do + JSON.generate(Float::NAN, strict: true, as_json: proc { obj }) + end + end + + def assert_float_roundtrip(expected, actual) + assert_equal(expected, JSON.generate(actual)) + assert_equal(actual, JSON.parse(JSON.generate(actual)), "JSON: #{JSON.generate(actual)}") + end + + def test_json_generate_float + assert_float_roundtrip "-1.0", -1.0 + assert_float_roundtrip "1.0", 1.0 + assert_float_roundtrip "0.0", 0.0 + assert_float_roundtrip "12.2", 12.2 + assert_float_roundtrip "2.34375", 7.5 / 3.2 + assert_float_roundtrip "12.0", 12.0 + assert_float_roundtrip "100.0", 100.0 + assert_float_roundtrip "1000.0", 1000.0 + + if RUBY_ENGINE == "jruby" + assert_float_roundtrip "1.7468619377842371E9", 1746861937.7842371 + else + assert_float_roundtrip "1746861937.7842371", 1746861937.7842371 + end + + if RUBY_ENGINE == "ruby" + assert_float_roundtrip "100000000000000.0", 100000000000000.0 + assert_float_roundtrip "1e+15", 1e+15 + assert_float_roundtrip "-100000000000000.0", -100000000000000.0 + assert_float_roundtrip "-1e+15", -1e+15 + assert_float_roundtrip "1111111111111111.1", 1111111111111111.1 + assert_float_roundtrip "1.1111111111111112e+16", 11111111111111111.1 + assert_float_roundtrip "-1111111111111111.1", -1111111111111111.1 + assert_float_roundtrip "-1.1111111111111112e+16", -11111111111111111.1 + + assert_float_roundtrip "-0.000000022471348024634545", -2.2471348024634545e-08 + assert_float_roundtrip "-0.0000000022471348024634545", -2.2471348024634545e-09 + assert_float_roundtrip "-2.2471348024634546e-10", -2.2471348024634545e-10 + end + end + + def test_numbers_of_various_sizes + numbers = [ + 0, 1, -1, 9, -9, 13, -13, 91, -91, 513, -513, 7513, -7513, + 17591, -17591, -4611686018427387904, 4611686018427387903, + 2**62, 2**63, 2**64, -(2**62), -(2**63), -(2**64) + ] + + numbers.each do |number| + assert_equal "[#{number}]", JSON.generate([number]) + end + end + + def test_generate_duplicate_keys_allowed + hash = { foo: 1, "foo" => 2 } + assert_equal %({"foo":1,"foo":2}), JSON.generate(hash, allow_duplicate_key: true) + end + + def test_generate_duplicate_keys_deprecated + hash = { foo: 1, "foo" => 2 } + assert_deprecated_warning(/allow_duplicate_key/) do + assert_equal %({"foo":1,"foo":2}), JSON.generate(hash) + end + end + + def test_generate_duplicate_keys_disallowed + hash = { foo: 1, "foo" => 2 } + error = assert_raise JSON::GeneratorError do + JSON.generate(hash, allow_duplicate_key: false) + end + assert_equal %(detected duplicate key "foo" in #{hash.inspect}), error.message + end + + def test_frozen + state = JSON::State.new.freeze + assert_raise(FrozenError) do + state.configure(max_nesting: 1) + end + setters = state.methods.grep(/\w=$/) + assert_not_empty setters + setters.each do |setter| + assert_raise(FrozenError) do + state.send(setter, 1) + end + end + end + + # The case when the State is frozen is tested in JSONCoderTest#test_nesting_recovery + def test_nesting_recovery + state = JSON::State.new + ary = [] + ary << ary + assert_raise(JSON::NestingError) { state.generate(ary) } + assert_equal 0, state.depth + assert_equal '{"a":1}', state.generate({ a: 1 }) + end end diff --git a/test/json/json_generic_object_test.rb b/test/json/json_generic_object_test.rb index 82742dcd63..57e3bf3c52 100644 --- a/test/json/json_generic_object_test.rb +++ b/test/json/json_generic_object_test.rb @@ -1,11 +1,20 @@ -#frozen_string_literal: false -require 'test_helper' +# frozen_string_literal: true +require_relative 'test_helper' -class JSONGenericObjectTest < Test::Unit::TestCase - include JSON +# ostruct is required to test JSON::GenericObject +begin + require "ostruct" +rescue LoadError + return +end +class JSONGenericObjectTest < Test::Unit::TestCase def setup - @go = GenericObject[ :a => 1, :b => 2 ] + if defined?(JSON::GenericObject) + @go = JSON::GenericObject[ :a => 1, :b => 2 ] + else + omit("JSON::GenericObject is not available") + end end def test_attributes @@ -37,23 +46,23 @@ class JSONGenericObjectTest < Test::Unit::TestCase ) assert_equal 1, l.a assert_equal @go, - l = JSON('{ "a": 1, "b": 2 }', :object_class => GenericObject) + l = JSON('{ "a": 1, "b": 2 }', :object_class => JSON::GenericObject) assert_equal 1, l.a - assert_equal GenericObject[:a => GenericObject[:b => 2]], - l = JSON('{ "a": { "b": 2 } }', :object_class => GenericObject) + assert_equal JSON::GenericObject[:a => JSON::GenericObject[:b => 2]], + l = JSON('{ "a": { "b": 2 } }', :object_class => JSON::GenericObject) assert_equal 2, l.a.b end end def test_from_hash - result = GenericObject.from_hash( + result = JSON::GenericObject.from_hash( :foo => { :bar => { :baz => true }, :quux => [ { :foobar => true } ] }) - assert_kind_of GenericObject, result.foo - assert_kind_of GenericObject, result.foo.bar + assert_kind_of JSON::GenericObject, result.foo + assert_kind_of JSON::GenericObject, result.foo.bar assert_equal true, result.foo.bar.baz - assert_kind_of GenericObject, result.foo.quux.first + assert_kind_of JSON::GenericObject, result.foo.quux.first assert_equal true, result.foo.quux.first.foobar - assert_equal true, GenericObject.from_hash(true) + assert_equal true, JSON::GenericObject.from_hash(true) end def test_json_generic_object_load diff --git a/test/json/json_parser_test.rb b/test/json/json_parser_test.rb index 9946dd93e7..ac53ba9f0c 100644 --- a/test/json/json_parser_test.rb +++ b/test/json/json_parser_test.rb @@ -1,10 +1,15 @@ -# encoding: utf-8 -# frozen_string_literal: false -require 'test_helper' +# frozen_string_literal: true +require_relative 'test_helper' require 'stringio' require 'tempfile' -require 'ostruct' -require 'bigdecimal' +begin + require 'ostruct' +rescue LoadError +end +begin + require 'bigdecimal' +rescue LoadError +end class JSONParserTest < Test::Unit::TestCase include JSON @@ -14,21 +19,28 @@ class JSONParserTest < Test::Unit::TestCase assert_equal 'test', parser.source end - def test_argument_encoding - source = "{}".encode("UTF-16") + def test_argument_encoding_unmodified + source = "{}".encode(Encoding::UTF_16) JSON::Parser.new(source) assert_equal Encoding::UTF_16, source.encoding - end if defined?(Encoding::UTF_16) + end + + def test_argument_encoding_for_binary_unmodified + source = "{}".b + JSON::Parser.new(source) + assert_equal Encoding::ASCII_8BIT, source.encoding + end def test_error_message_encoding bug10705 = '[ruby-core:67386] [Bug #10705]' - json = ".\"\xE2\x88\x9A\"".force_encoding(Encoding::UTF_8) + json = ".\"\xE2\x88\x9A\"" + assert_equal(Encoding::UTF_8, json.encoding) e = assert_raise(JSON::ParserError) { JSON::Ext::Parser.new(json).parse } assert_equal(Encoding::UTF_8, e.message.encoding, bug10705) assert_include(e.message, json, bug10705) - end if defined?(Encoding::UTF_8) and defined?(JSON::Ext::Parser) + end def test_parsing parser = JSON::Parser.new('"test"') @@ -84,6 +96,7 @@ class JSONParserTest < Test::Unit::TestCase assert_equal({ "a" => 23 }, parse(' { "a" : 23 } ')) assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } ')) assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } ')) + assert_equal({ "" => 123 }, parse('{"":123}')) end def test_parse_numbers @@ -91,6 +104,14 @@ class JSONParserTest < Test::Unit::TestCase assert_raise(JSON::ParserError) { parse('+23') } assert_raise(JSON::ParserError) { parse('.23') } assert_raise(JSON::ParserError) { parse('023') } + assert_raise(JSON::ParserError) { parse('-023') } + assert_raise(JSON::ParserError) { parse('023.12') } + assert_raise(JSON::ParserError) { parse('-023.12') } + assert_raise(JSON::ParserError) { parse('023e12') } + assert_raise(JSON::ParserError) { parse('-023e12') } + assert_raise(JSON::ParserError) { parse('-') } + assert_raise(JSON::ParserError) { parse('-.1') } + assert_raise(JSON::ParserError) { parse('-e0') } assert_equal(23, parse('23')) assert_equal(-23, parse('-23')) assert_equal_float(3.141, parse('3.141')) @@ -107,33 +128,56 @@ class JSONParserTest < Test::Unit::TestCase assert_equal(1.0/0, parse('Infinity', :allow_nan => true)) assert_raise(ParserError) { parse('-Infinity') } assert_equal(-1.0/0, parse('-Infinity', :allow_nan => true)) + capture_output { assert_equal(Float::INFINITY, parse("23456789012E666")) } + end + + def test_parse_bignum + bignum = Integer('1234567890' * 10) + assert_equal(bignum, JSON.parse(bignum.to_s)) + assert_equal(bignum.to_f, JSON.parse(bignum.to_s + ".0")) end def test_parse_bigdecimals assert_equal(BigDecimal, JSON.parse('{"foo": 9.01234567890123456789}', decimal_class: BigDecimal)["foo"].class) assert_equal(BigDecimal("0.901234567890123456789E1"),JSON.parse('{"foo": 9.01234567890123456789}', decimal_class: BigDecimal)["foo"] ) + end if defined?(::BigDecimal) + + def test_parse_string_mixed_unicode + assert_equal(["éé"], JSON.parse("[\"\\u00e9é\"]")) end - if Array.method_defined?(:permutation) - def test_parse_more_complex_arrays - a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }] - a.permutation.each do |perm| - json = pretty_generate(perm) - assert_equal perm, parse(json) - end + def test_parse_more_complex_arrays + a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }] + a.permutation.each do |perm| + json = pretty_generate(perm) + assert_equal perm, parse(json) end + end - def test_parse_complex_objects - a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }] - a.permutation.each do |perm| - s = "a" - orig_obj = perm.inject({}) { |h, x| h[s.dup] = x; s = s.succ; h } - json = pretty_generate(orig_obj) - assert_equal orig_obj, parse(json) + def test_parse_complex_objects + a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }] + a.permutation.each do |perm| + s = "a" + orig_obj = perm.inject({}) { |h, x| h[s.dup] = x; s = s.succ; h } + json = pretty_generate(orig_obj) + assert_equal orig_obj, parse(json) + end + end + + def test_parse_control_chars_in_string + 0.upto(31) do |ord| + assert_raise JSON::ParserError do + parse(%("#{ord.chr}")) end end end + def test_parse_allowed_control_chars_in_string + 0.upto(31) do |ord| + assert_equal ord.chr, parse(%("#{ord.chr}"), allow_control_characters: true) + end + end + def test_parse_arrays assert_equal([1,2,3], parse('[1,2,3]')) assert_equal([1.2,2,3], parse('[1.2,2,3]')) @@ -165,7 +209,93 @@ class JSONParserTest < Test::Unit::TestCase assert parse('NaN', :allow_nan => true).nan? assert parse('Infinity', :allow_nan => true).infinite? assert parse('-Infinity', :allow_nan => true).infinite? - assert_raise(JSON::ParserError) { parse('[ 1, ]') } + end + + def test_parse_arrays_with_allow_trailing_comma + assert_equal([], parse('[]', allow_trailing_comma: true)) + assert_equal([], parse('[]', allow_trailing_comma: false)) + assert_raise(JSON::ParserError) { parse('[,]', allow_trailing_comma: true) } + assert_raise(JSON::ParserError) { parse('[,]', allow_trailing_comma: false) } + + assert_equal([1], parse('[1]', allow_trailing_comma: true)) + assert_equal([1], parse('[1]', allow_trailing_comma: false)) + assert_equal([1], parse('[1,]', allow_trailing_comma: true)) + assert_raise(JSON::ParserError) { parse('[1,]', allow_trailing_comma: false) } + + assert_equal([1, 2, 3], parse('[1,2,3]', allow_trailing_comma: true)) + assert_equal([1, 2, 3], parse('[1,2,3]', allow_trailing_comma: false)) + assert_equal([1, 2, 3], parse('[1,2,3,]', allow_trailing_comma: true)) + assert_raise(JSON::ParserError) { parse('[1,2,3,]', allow_trailing_comma: false) } + + assert_equal([1, 2, 3], parse('[ 1 , 2 , 3 ]', allow_trailing_comma: true)) + assert_equal([1, 2, 3], parse('[ 1 , 2 , 3 ]', allow_trailing_comma: false)) + assert_equal([1, 2, 3], parse('[ 1 , 2 , 3 , ]', allow_trailing_comma: true)) + assert_raise(JSON::ParserError) { parse('[ 1 , 2 , 3 , ]', allow_trailing_comma: false) } + + assert_equal({'foo' => [1, 2, 3]}, parse('{ "foo": [1,2,3] }', allow_trailing_comma: true)) + assert_equal({'foo' => [1, 2, 3]}, parse('{ "foo": [1,2,3] }', allow_trailing_comma: false)) + assert_equal({'foo' => [1, 2, 3]}, parse('{ "foo": [1,2,3,] }', allow_trailing_comma: true)) + assert_raise(JSON::ParserError) { parse('{ "foo": [1,2,3,] }', allow_trailing_comma: false) } + end + + def test_parse_object_with_allow_trailing_comma + assert_equal({}, parse('{}', allow_trailing_comma: true)) + assert_equal({}, parse('{}', allow_trailing_comma: false)) + assert_raise(JSON::ParserError) { parse('{,}', allow_trailing_comma: true) } + assert_raise(JSON::ParserError) { parse('{,}', allow_trailing_comma: false) } + + assert_equal({'foo'=>'bar'}, parse('{"foo":"bar"}', allow_trailing_comma: true)) + assert_equal({'foo'=>'bar'}, parse('{"foo":"bar"}', allow_trailing_comma: false)) + assert_equal({'foo'=>'bar'}, parse('{"foo":"bar",}', allow_trailing_comma: true)) + assert_raise(JSON::ParserError) { parse('{"foo":"bar",}', allow_trailing_comma: false) } + + assert_equal( + {'foo'=>'bar', 'baz'=>'qux', 'quux'=>'garply'}, + parse('{"foo":"bar","baz":"qux","quux":"garply"}', allow_trailing_comma: true) + ) + assert_equal( + {'foo'=>'bar', 'baz'=>'qux', 'quux'=>'garply'}, + parse('{"foo":"bar","baz":"qux","quux":"garply"}', allow_trailing_comma: false) + ) + assert_equal( + {'foo'=>'bar', 'baz'=>'qux', 'quux'=>'garply'}, + parse('{"foo":"bar","baz":"qux","quux":"garply",}', allow_trailing_comma: true) + ) + assert_raise(JSON::ParserError) { + parse('{"foo":"bar","baz":"qux","quux":"garply",}', allow_trailing_comma: false) + } + + assert_equal( + {'foo'=>'bar', 'baz'=>'qux', 'quux'=>'garply'}, + parse('{ "foo":"bar" , "baz":"qux" , "quux":"garply" }', allow_trailing_comma: true) + ) + assert_equal( + {'foo'=>'bar', 'baz'=>'qux', 'quux'=>'garply'}, + parse('{ "foo":"bar" , "baz":"qux" , "quux":"garply" }', allow_trailing_comma: false) + ) + assert_equal( + {'foo'=>'bar', 'baz'=>'qux', 'quux'=>'garply'}, + parse('{ "foo":"bar" , "baz":"qux" , "quux":"garply" , }', allow_trailing_comma: true) + ) + assert_raise(JSON::ParserError) { + parse('{ "foo":"bar" , "baz":"qux" , "quux":"garply" , }', allow_trailing_comma: false) + } + + assert_equal( + [{'foo'=>'bar', 'baz'=>'qux', 'quux'=>'garply'}], + parse('[{"foo":"bar","baz":"qux","quux":"garply"}]', allow_trailing_comma: true) + ) + assert_equal( + [{'foo'=>'bar', 'baz'=>'qux', 'quux'=>'garply'}], + parse('[{"foo":"bar","baz":"qux","quux":"garply"}]', allow_trailing_comma: false) + ) + assert_equal( + [{'foo'=>'bar', 'baz'=>'qux', 'quux'=>'garply'}], + parse('[{"foo":"bar","baz":"qux","quux":"garply",}]', allow_trailing_comma: true) + ) + assert_raise(JSON::ParserError) { + parse('[{"foo":"bar","baz":"qux","quux":"garply",}]', allow_trailing_comma: false) + } end def test_parse_some_strings @@ -179,6 +309,50 @@ class JSONParserTest < Test::Unit::TestCase ) end + if RUBY_ENGINE != "jruby" # https://github.com/ruby/json/issues/138 + def test_parse_broken_string + s = parse(%{["\x80"]})[0] + assert_equal("\x80", s) + assert_equal Encoding::UTF_8, s.encoding + assert_equal false, s.valid_encoding? + + s = parse(%{["\x80"]}.b)[0] + assert_equal("\x80", s) + assert_equal Encoding::UTF_8, s.encoding + assert_equal false, s.valid_encoding? + + input = %{["\x80"]}.dup.force_encoding(Encoding::US_ASCII) + assert_raise(Encoding::InvalidByteSequenceError) { parse(input) } + end + end + + def test_invalid_unicode_escape + assert_raise(JSON::ParserError) { parse('"\u"') } + assert_raise(JSON::ParserError) { parse('"\ua"') } + assert_raise(JSON::ParserError) { parse('"\uaa"') } + assert_raise(JSON::ParserError) { parse('"\uaaa"') } + assert_equal "\uaaaa", parse('"\uaaaa"') + + assert_raise(JSON::ParserError) { parse('"\u______"') } + assert_raise(JSON::ParserError) { parse('"\u1_____"') } + assert_raise(JSON::ParserError) { parse('"\u11____"') } + assert_raise(JSON::ParserError) { parse('"\u111___"') } + end + + def test_unicode_followed_by_newline + # Ref: https://github.com/ruby/json/issues/912 + assert_equal "🌌\n".bytes, JSON.parse('"\ud83c\udf0c\n"').bytes + assert_equal "🌌\n", JSON.parse('"\ud83c\udf0c\n"') + assert_predicate JSON.parse('"\ud83c\udf0c\n"'), :valid_encoding? + end + + def test_invalid_surogates + assert_raise(JSON::ParserError) { parse('"\\uD800"') } + assert_raise(JSON::ParserError) { parse('"\\uD800_________________"') } + assert_raise(JSON::ParserError) { parse('"\\uD800\\u0041"') } + assert_raise(JSON::ParserError) { parse('"\\uD800\\u004') } + end + def test_parse_big_integers json1 = JSON(orig = (1 << 31) - 1) assert_equal orig, parse(json1) @@ -192,6 +366,52 @@ class JSONParserTest < Test::Unit::TestCase assert_equal orig, parse(json5) end + def test_parse_escaped_key + doc = { + "test\r1" => 1, + "entries" => [ + "test\t2" => 2, + "test\n3" => 3, + ] + } + + assert_equal doc, parse(JSON.generate(doc)) + end + + def test_parse_duplicate_key + expected = {"a" => 2} + expected_sym = {a: 2} + + assert_equal expected, parse('{"a": 1, "a": 2}', allow_duplicate_key: true) + assert_raise(ParserError) { parse('{"a": 1, "a": 2}', allow_duplicate_key: false) } + assert_raise(ParserError) { parse('{"a": 1, "a": 2}', allow_duplicate_key: false, symbolize_names: true) } + + assert_deprecated_warning(/duplicate key "a"/) do + assert_equal expected, parse('{"a": 1, "a": 2}') + end + assert_deprecated_warning(/duplicate key "a"/) do + assert_equal expected_sym, parse('{"a": 1, "a": 2}', symbolize_names: true) + end + + if RUBY_ENGINE == 'ruby' + assert_deprecated_warning(/#{File.basename(__FILE__)}\:#{__LINE__ + 1}/) do + assert_equal expected, parse('{"a": 1, "a": 2}') + end + end + + unless RUBY_ENGINE == 'jruby' + assert_raise(ParserError) do + fake_key = Object.new + JSON.load('{"a": 1, "a": 2}', -> (obj) { obj == "a" ? fake_key : obj }, allow_duplicate_key: false) + end + + assert_deprecated_warning(/duplicate key #<Object:0x/) do + fake_key = Object.new + JSON.load('{"a": 1, "a": 2}', -> (obj) { obj == "a" ? fake_key : obj }) + end + end + end + def test_some_wrong_inputs assert_raise(ParserError) { parse('[] bla') } assert_raise(ParserError) { parse('[] 1') } @@ -218,44 +438,66 @@ class JSONParserTest < Test::Unit::TestCase end end + def test_freeze + assert_predicate parse('{}', :freeze => true), :frozen? + assert_predicate parse('[]', :freeze => true), :frozen? + assert_predicate parse('"foo"', :freeze => true), :frozen? + + assert_same(-'foo', parse('"foo"', :freeze => true)) + assert_same(-'foo', parse('{"foo": 1}', :freeze => true).keys.first) + end + def test_parse_comments - json = <<EOT -{ - "key1":"value1", // eol comment - "key2":"value2" /* multi line - * comment */, - "key3":"value3" /* multi line - // nested eol comment - * comment */ -} -EOT + json = <<~JSON + { + "key1":"value1", // eol comment + "key2":"value2" /* multi line + * comment */, + "key3":"value3" /* multi line + // nested eol comment + * comment */ + } + JSON assert_equal( { "key1" => "value1", "key2" => "value2", "key3" => "value3" }, parse(json)) - json = <<EOT -{ - "key1":"value1" /* multi line - // nested eol comment - /* illegal nested multi line comment */ - * comment */ -} -EOT + json = <<~JSON + { + "key1":"value1" /* multi line + // nested eol comment + /* illegal nested multi line comment */ + * comment */ + } + JSON assert_raise(ParserError) { parse(json) } - json = <<EOT -{ - "key1":"value1" /* multi line - // nested eol comment - closed multi comment */ - and again, throw an Error */ -} -EOT + json = <<~JSON + { + "key1":"value1" /* multi line + // nested eol comment + /* legal nested multi line comment start sequence */ + } + JSON + assert_equal({ "key1" => "value1" }, parse(json)) + json = <<~JSON + { + "key1":"value1" /* multi line + // nested eol comment + closed multi comment */ + and again, throw an Error */ + } + JSON assert_raise(ParserError) { parse(json) } - json = <<EOT -{ - "key1":"value1" /*/*/ -} -EOT + json = <<~JSON + { + "key1":"value1" /*/*/ + } + JSON assert_equal({ "key1" => "value1" }, parse(json)) + assert_equal({}, parse('{} /**/')) + assert_raise(ParserError) { parse('{} /* comment not closed') } + assert_raise(ParserError) { parse('{} /*/') } + assert_raise(ParserError) { parse('{} /x wrong comment') } + assert_raise(ParserError) { parse('{} /') } end def test_nesting @@ -290,8 +532,96 @@ EOT data = ['"'] assert_equal data, parse(json) # - json = '["\\\'"]' - data = ["'"] + json = '["\\/"]' + data = ["/"] + assert_equal data, parse(json) + + json = '["\/"]' + data = [ '/' ] + assert_equal data, parse(json) + + data = ['"""""""""""""""""""""""""'] + json = '["\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\""]' + assert_equal data, parse(json) + + data = '["This is a "test" of the emergency broadcast system."]' + json = "\"[\\\"This is a \\\"test\\\" of the emergency broadcast system.\\\"]\"" + assert_equal data, parse(json) + + data = '\tThis is a test of the emergency broadcast system.' + json = "\"\\\\tThis is a test of the emergency broadcast system.\"" + assert_equal data, parse(json) + + data = 'This\tis a test of the emergency broadcast system.' + json = "\"This\\\\tis a test of the emergency broadcast system.\"" + assert_equal data, parse(json) + + data = 'This is\ta test of the emergency broadcast system.' + json = "\"This is\\\\ta test of the emergency broadcast system.\"" + assert_equal data, parse(json) + + data = 'This is a test of the emergency broadcast\tsystem.' + json = "\"This is a test of the emergency broadcast\\\\tsystem.\"" + assert_equal data, parse(json) + + data = 'This is a test of the emergency broadcast\tsystem.\n' + json = "\"This is a test of the emergency broadcast\\\\tsystem.\\\\n\"" + assert_equal data, parse(json) + + data = '"' * 15 + json = "\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\"" + assert_equal data, parse(json) + + data = "\"\"\"\"\"\"\"\"\"\"\"\"\"\"a" + json = "\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\"a\"" + assert_equal data, parse(json) + + data = "\u0001\u0001\u0001\u0001" + json = "\"\\u0001\\u0001\\u0001\\u0001\"" + assert_equal data, parse(json) + + data = "\u0001a\u0001a\u0001a\u0001a" + json = "\"\\u0001a\\u0001a\\u0001a\\u0001a\"" + assert_equal data, parse(json) + + data = "\u0001aa\u0001aa" + json = "\"\\u0001aa\\u0001aa\"" + assert_equal data, parse(json) + + data = "\u0001aa\u0001aa\u0001aa" + json = "\"\\u0001aa\\u0001aa\\u0001aa\"" + assert_equal data, parse(json) + + data = "\u0001aa\u0001aa\u0001aa\u0001aa\u0001aa\u0001aa" + json = "\"\\u0001aa\\u0001aa\\u0001aa\\u0001aa\\u0001aa\\u0001aa\"" + assert_equal data, parse(json) + + data = "\u0001a\u0002\u0001a\u0002\u0001a\u0002\u0001a\u0002\u0001a\u0002\u0001a\u0002\u0001a\u0002\u0001a\u0002" + json = "\"\\u0001a\\u0002\\u0001a\\u0002\\u0001a\\u0002\\u0001a\\u0002\\u0001a\\u0002\\u0001a\\u0002\\u0001a\\u0002\\u0001a\\u0002\"" + assert_equal data, parse(json) + + data = "ab\u0002c" + json = "\"ab\\u0002c\"" + assert_equal data, parse(json) + + data = "ab\u0002cab\u0002cab\u0002cab\u0002c" + json = "\"ab\\u0002cab\\u0002cab\\u0002cab\\u0002c\"" + assert_equal data, parse(json) + + data = "ab\u0002cab\u0002cab\u0002cab\u0002cab\u0002cab\u0002c" + json = "\"ab\\u0002cab\\u0002cab\\u0002cab\\u0002cab\\u0002cab\\u0002c\"" + assert_equal data, parse(json) + + data = "\n\t\f\b\n\t\f\b\n\t\f\b\n\t\f" + json = "\"\\n\\t\\f\\b\\n\\t\\f\\b\\n\\t\\f\\b\\n\\t\\f\"" + assert_equal data, parse(json) + + data = "\n\t\f\b\n\t\f\b\n\t\f\b\n\t\f\b" + json = "\"\\n\\t\\f\\b\\n\\t\\f\\b\\n\\t\\f\\b\\n\\t\\f\\b\"" + assert_equal data, parse(json) + + data = "a\n\t\f\b\n\t\f\b\n\t\f\b\n\t" + json = "\"a\\n\\t\\f\\b\\n\\t\\f\\b\\n\\t\\f\\b\\n\\t\"" assert_equal data, parse(json) end @@ -351,6 +681,7 @@ EOT def test_parse_array_custom_non_array_derived_class res = parse('[1,2]', :array_class => SubArrayWrapper) assert_equal([1,2], res.data) + assert_equal(1, res[0]) assert_equal(SubArrayWrapper, res.class) assert res.shifted? end @@ -386,21 +717,6 @@ EOT end end - class SubOpenStruct < OpenStruct - def [](k) - __send__(k) - end - - def []=(k, v) - @item_set = true - __send__("#{k}=", v) - end - - def item_set? - @item_set - end - end - def test_parse_object_custom_hash_derived_class res = parse('{"foo":"bar"}', :object_class => SubHash) assert_equal({"foo" => "bar"}, res) @@ -408,24 +724,42 @@ EOT assert res.item_set? end - def test_parse_object_custom_non_hash_derived_class - res = parse('{"foo":"bar"}', :object_class => SubOpenStruct) - assert_equal "bar", res.foo - assert_equal(SubOpenStruct, res.class) - assert res.item_set? - end + if defined?(::OpenStruct) + class SubOpenStruct < OpenStruct + def [](k) + __send__(k) + end - def test_parse_generic_object - res = parse( - '{"foo":"bar", "baz":{}}', - :object_class => JSON::GenericObject - ) - assert_equal(JSON::GenericObject, res.class) - assert_equal "bar", res.foo - assert_equal "bar", res["foo"] - assert_equal "bar", res[:foo] - assert_equal "bar", res.to_hash[:foo] - assert_equal(JSON::GenericObject, res.baz.class) + def []=(k, v) + @item_set = true + __send__("#{k}=", v) + end + + def item_set? + @item_set + end + end + + def test_parse_object_custom_non_hash_derived_class + res = parse('{"foo":"bar"}', :object_class => SubOpenStruct) + assert_equal "bar", res.foo + assert_equal "bar", res[:foo] + assert_equal(SubOpenStruct, res.class) + assert res.item_set? + end + + def test_parse_generic_object + res = parse( + '{"foo":"bar", "baz":{}}', + :object_class => JSON::GenericObject + ) + assert_equal(JSON::GenericObject, res.class) + assert_equal "bar", res.foo + assert_equal "bar", res["foo"] + assert_equal "bar", res[:foo] + assert_equal "bar", res.to_hash[:foo] + assert_equal(JSON::GenericObject, res.baz.class) + end end def test_generate_core_subclasses_with_new_to_json @@ -458,10 +792,63 @@ EOT def test_parsing_frozen_ascii8bit_string assert_equal( { 'foo' => 'bar' }, - JSON('{ "foo": "bar" }'.force_encoding(Encoding::ASCII_8BIT).freeze) + JSON('{ "foo": "bar" }'.b.freeze) ) end + def test_parse_error_message_length + # Error messages aren't consistent across backends, but we can at least + # enforce that if they include fragments of the source it should be of + # reasonable size. + error = assert_raise(JSON::ParserError) do + JSON.parse('{"foo": ' + ('A' * 500) + '}') + end + assert_operator 80, :>, error.message.bytesize + end + + def test_parse_error_incomplete_hash + error = assert_raise(JSON::ParserError) do + JSON.parse('{"input":{"firstName":"Bob","lastName":"Mob","email":"bob@example.com"}') + end + if RUBY_ENGINE == "ruby" + assert_equal %(expected ',' or '}' after object value, got: EOF at line 1 column 72), error.message + end + end + + def test_parse_error_snippet + omit "C ext only test" unless RUBY_ENGINE == "ruby" + + error = assert_raise(JSON::ParserError) { JSON.parse("あああああああああああああああああああああああ") } + assert_equal "unexpected character: 'ああああああああああ' at line 1 column 1", error.message + + error = assert_raise(JSON::ParserError) { JSON.parse("aあああああああああああああああああああああああ") } + assert_equal "unexpected character: 'aああああああああああ' at line 1 column 1", error.message + + error = assert_raise(JSON::ParserError) { JSON.parse("abあああああああああああああああああああああああ") } + assert_equal "unexpected character: 'abあああああああああ' at line 1 column 1", error.message + + error = assert_raise(JSON::ParserError) { JSON.parse("abcあああああああああああああああああああああああ") } + assert_equal "unexpected character: 'abcあああああああああ' at line 1 column 1", error.message + end + + def test_parse_leading_slash + # ref: https://github.com/ruby/ruby/pull/12598 + assert_raise(JSON::ParserError) do + JSON.parse("/foo/bar") + end + end + + def test_parse_whitespace_after_newline + assert_equal [], JSON.parse("[\n#{' ' * (8 + 8 + 4 + 3)}]") + end + + def test_frozen + parser_config = JSON::Parser::Config.new({}).freeze + assert_raise FrozenError do + parser_config.send(:initialize, {}) + end + end + private def assert_equal_float(expected, actual, delta = 1e-2) diff --git a/test/json/json_ryu_fallback_test.rb b/test/json/json_ryu_fallback_test.rb new file mode 100644 index 0000000000..59ba76d392 --- /dev/null +++ b/test/json/json_ryu_fallback_test.rb @@ -0,0 +1,169 @@ +# frozen_string_literal: true +require_relative 'test_helper' +begin + require 'bigdecimal' +rescue LoadError +end + +class JSONRyuFallbackTest < Test::Unit::TestCase + include JSON + + # Test that numbers with more than 17 significant digits fall back to rb_cstr_to_dbl + def test_more_than_17_significant_digits + # These numbers have > 17 significant digits and should use fallback path + # They should still parse correctly, just not via the Ryu optimization + + test_cases = [ + # input, expected (rounded to double precision) + ["1.23456789012345678901234567890", 1.2345678901234567], + ["123456789012345678.901234567890", 1.2345678901234568e+17], + ["0.123456789012345678901234567890", 0.12345678901234568], + ["9999999999999999999999999999.9", 1.0e+28], + # Edge case: exactly 18 digits + ["123456789012345678", 123456789012345680.0], + # Many fractional digits + ["0.12345678901234567890123456789", 0.12345678901234568], + ] + + test_cases.each do |input, expected| + result = JSON.parse(input) + assert_in_delta(expected, result, 1e-10, + "Failed to parse #{input} correctly (>17 digits, fallback path)") + end + end + + # Test decimal_class option forces fallback + def test_decimal_class_option + input = "3.141" + + # Without decimal_class: uses Ryu, returns Float + result_float = JSON.parse(input) + assert_instance_of(Float, result_float) + assert_equal(3.141, result_float) + + # With decimal_class: uses fallback, returns BigDecimal + result_bigdecimal = JSON.parse(input, decimal_class: BigDecimal) + assert_instance_of(BigDecimal, result_bigdecimal) + assert_equal(BigDecimal("3.141"), result_bigdecimal) + end if defined?(::BigDecimal) + + # Test that numbers with <= 17 digits use Ryu optimization + def test_ryu_optimization_used_for_normal_numbers + test_cases = [ + ["3.141", 3.141], + ["1.23456789012345e100", 1.23456789012345e100], + ["0.00000000000001", 1.0e-14], + ["123456789012345.67", 123456789012345.67], + ["-1.7976931348623157e+308", -1.7976931348623157e+308], + ["2.2250738585072014e-308", 2.2250738585072014e-308], + # Exactly 17 significant digits + ["12345678901234567", 12345678901234567.0], + ["1.2345678901234567", 1.2345678901234567], + ] + + test_cases.each do |input, expected| + result = JSON.parse(input) + assert_in_delta(expected, result, expected.abs * 1e-15, + "Failed to parse #{input} correctly (<=17 digits, Ryu path)") + end + end + + # Test edge cases at the boundary (17 digits) + def test_seventeen_digit_boundary + # Exactly 17 significant digits should use Ryu + input_17 = "12345678901234567.0" # Force it to be a float with .0 + result = JSON.parse(input_17) + assert_in_delta(12345678901234567.0, result, 1e-10) + + # 18 significant digits should use fallback + input_18 = "123456789012345678.0" + result = JSON.parse(input_18) + # Note: This will be rounded to double precision + assert_in_delta(123456789012345680.0, result, 1e-10) + end + + # Test that leading zeros don't count toward the 17-digit limit + def test_leading_zeros_dont_count + test_cases = [ + ["0.00012345678901234567", 0.00012345678901234567], # 17 significant digits + ["0.000000000000001234567890123456789", 1.234567890123457e-15], # >17 significant + ] + + test_cases.each do |input, expected| + result = JSON.parse(input) + assert_in_delta(expected, result, expected.abs * 1e-10, + "Failed to parse #{input} correctly") + end + end + + # Test that Ryu handles special values correctly + def test_special_double_values + test_cases = [ + ["1.7976931348623157e+308", Float::MAX], # Largest finite double + ["2.2250738585072014e-308", Float::MIN], # Smallest normalized double + ] + + test_cases.each do |input, expected| + result = JSON.parse(input) + assert_in_delta(expected, result, expected.abs * 1e-10, + "Failed to parse #{input} correctly") + end + + # Test zero separately + result_pos_zero = JSON.parse("0.0") + assert_equal(0.0, result_pos_zero) + + # Note: JSON.parse doesn't preserve -0.0 vs +0.0 distinction in standard mode + result_neg_zero = JSON.parse("-0.0") + assert_equal(0.0, result_neg_zero.abs) + end + + # Test subnormal numbers that caused precision issues before fallback was added + # These are extreme edge cases discovered by fuzzing (4 in 6 billion numbers tested) + def test_subnormal_edge_cases_round_trip + # These subnormal numbers (~1e-310) had 1 ULP rounding errors in original Ryu + # They now use rb_cstr_to_dbl fallback for exact precision + test_cases = [ + "-3.2652630314355e-310", + "3.9701623107025e-310", + "-3.6607772435415e-310", + "2.9714076801985e-310", + ] + + test_cases.each do |input| + # Parse the number + result = JSON.parse(input) + + # Should be bit-identical + assert_equal(result, JSON.parse(result.to_s), + "Subnormal #{input} failed round-trip test") + + # Should be bit-identical + assert_equal(result, JSON.parse(JSON.dump(result)), + "Subnormal #{input} failed round-trip test") + + # Verify the value is in the expected subnormal range + assert(result.abs < 2.225e-308, + "#{input} should be subnormal (< 2.225e-308)") + end + end + + # Test invalid numbers are properly rejected + def test_invalid_numbers_rejected + invalid_cases = [ + "-", + ".", + "-.", + "-.e10", + "1.2.3", + "1e", + "1e+", + ] + + invalid_cases.each do |input| + assert_raise(JSON::ParserError, "Should reject invalid number: #{input}") do + JSON.parse(input) + end + end + end +end diff --git a/test/json/json_string_matching_test.rb b/test/json/json_string_matching_test.rb index 5d55dc31b0..21cd649025 100644 --- a/test/json/json_string_matching_test.rb +++ b/test/json/json_string_matching_test.rb @@ -1,5 +1,5 @@ -#frozen_string_literal: false -require 'test_helper' +# frozen_string_literal: true +require_relative 'test_helper' require 'time' class JSONStringMatchingTest < Test::Unit::TestCase diff --git a/test/json/ractor_test.rb b/test/json/ractor_test.rb new file mode 100644 index 0000000000..e53c405a74 --- /dev/null +++ b/test/json/ractor_test.rb @@ -0,0 +1,114 @@ +# frozen_string_literal: true + +require_relative 'test_helper' + +begin + require_relative './lib/helper' +rescue LoadError +end + +class JSONInRactorTest < Test::Unit::TestCase + unless Ractor.method_defined?(:value) + module RactorBackport + refine Ractor do + alias_method :value, :take + end + end + + using RactorBackport + end + + def test_generate + pid = fork do + Warning[:experimental] = false + r = Ractor.new do + json = JSON.generate({ + 'a' => 2, + 'b' => 3.141, + 'c' => 'c', + 'd' => [ 1, "b", 3.14 ], + 'e' => { 'foo' => 'bar' }, + 'g' => "\"\0\037", + 'h' => 1000.0, + 'i' => 0.001 + }) + JSON.parse(json) + end + expected_json = JSON.parse('{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},' + + '"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}') + actual_json = r.value + + if expected_json == actual_json + exit 0 + else + puts "Expected:" + puts expected_json + puts "Actual:" + puts actual_json + puts + exit 1 + end + end + _, status = Process.waitpid2(pid) + assert_predicate status, :success? + end + + def test_coder + coder = JSON::Coder.new.freeze + assert Ractor.shareable?(coder) + pid = fork do + Warning[:experimental] = false + r = Ractor.new(coder) do |coder| + json = coder.dump({ + 'a' => 2, + 'b' => 3.141, + 'c' => 'c', + 'd' => [ 1, "b", 3.14 ], + 'e' => { 'foo' => 'bar' }, + 'g' => "\"\0\037", + 'h' => 1000.0, + 'i' => 0.001 + }) + coder.load(json) + end + expected_json = JSON.parse('{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},' + + '"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}') + actual_json = r.value + + if expected_json == actual_json + exit 0 + else + puts "Expected:" + puts expected_json + puts "Actual:" + puts actual_json + puts + exit 1 + end + end + _, status = Process.waitpid2(pid) + assert_predicate status, :success? + end + + class NonNative + def initialize(value) + @value = value + end + end + + def test_coder_proc + block = Ractor.shareable_proc { |value| value.as_json } + coder = JSON::Coder.new(&block).freeze + assert Ractor.shareable?(coder) + + pid = fork do + Warning[:experimental] = false + assert_equal [{}], Ractor.new(coder) { |coder| + coder.load('[{}]') + }.value + end + + _, status = Process.waitpid2(pid) + assert_predicate status, :success? + end if Ractor.respond_to?(:shareable_proc) +end if defined?(Ractor) && Process.respond_to?(:fork) diff --git a/test/json/test_helper.rb b/test/json/test_helper.rb index c5ec0fca7b..24cde4348c 100644 --- a/test/json/test_helper.rb +++ b/test/json/test_helper.rb @@ -1,17 +1,53 @@ -case ENV['JSON'] -when 'pure' - $:.unshift 'lib' - require 'json/pure' -when 'ext' - $:.unshift 'ext', 'lib' - require 'json/ext' -else - $:.unshift 'ext', 'lib' - require 'json' +$LOAD_PATH.unshift(File.expand_path('../../../ext', __FILE__), File.expand_path('../../../lib', __FILE__)) + +if ENV["JSON_COVERAGE"] + # This test helper is loaded inside Ruby's own test suite, so we try to not mess it up. + require 'coverage' + + branches_supported = Coverage.respond_to?(:supported?) && Coverage.supported?(:branches) + + # Coverage module must be started before SimpleCov to work around the cyclic require order. + # Track both branches and lines, or else SimpleCov misleadingly reports 0/0 = 100% for non-branching files. + Coverage.start(lines: true, + branches: branches_supported) + + require 'simplecov' + SimpleCov.start do + # Enabling both coverage types to let SimpleCov know to output them together in reports + enable_coverage :line + enable_coverage :branch if branches_supported + + # Can't always trust SimpleCov to find files implicitly + track_files 'lib/**/*.rb' + + add_filter 'lib/json/truffle_ruby' unless RUBY_ENGINE == 'truffleruby' + end end +require 'json' require 'test/unit' -begin - require 'byebug' -rescue LoadError + +if ENV["JSON_COMPACT"] + if GC.respond_to?(:verify_compaction_references) + # This method was added in Ruby 3.0.0. Calling it this way asks the GC to + # move objects around, helping to find object movement bugs. + begin + GC.verify_compaction_references(expand_heap: true, toward: :empty) + rescue NotImplementedError, ArgumentError + # Some platforms don't support compaction + end + end + + if GC.respond_to?(:auto_compact=) + begin + GC.auto_compact = true + rescue NotImplementedError + # Some platforms don't support compaction + end + end +end + +unless defined?(Test::Unit::CoreAssertions) + require "core_assertions" + Test::Unit::TestCase.include Test::Unit::CoreAssertions end |
