From ebf7a68da897b47a85e3e7f35f4cf4cbef2175ca Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Thu, 16 Jan 2025 12:36:04 +0100 Subject: [PATCH] Add test for unecaped control characters in strings The JSON spec doesn't allow for unescaped control characters (`< 0x20`) in strings, but `Oj.load` does. `Oj::Parser` correctly raises a parse error though. --- test/test_parser_usual.rb | 10 ++++++++++ test/test_strict.rb | 8 ++++++++ 2 files changed, 18 insertions(+) diff --git a/test/test_parser_usual.rb b/test/test_parser_usual.rb index d181cce2..0be3b69b 100755 --- a/test/test_parser_usual.rb +++ b/test/test_parser_usual.rb @@ -79,6 +79,16 @@ def test_strings assert_equal({'ぴ' => '', 'ぴ ' => 'x', 'c' => 'ぴーたー', 'd' => ' ぴーたー '}, doc) end + def test_unescaped_ASCII_control_characters + p = Oj::Parser.new(:usual) + + (0..31).each do |control_ord| + assert_raises(Oj::ParseError) do + p.parse(%{"invalid character: #{control_ord.chr}"}) + end + end + end + def test_capacity p = Oj::Parser.new(:usual, capacity: 1000) assert_equal(4096, p.capacity) diff --git a/test/test_strict.rb b/test/test_strict.rb index 54502c56..ea0bd369 100755 --- a/test/test_strict.rb +++ b/test/test_strict.rb @@ -133,6 +133,14 @@ def test_encode Oj.default_options = opts end + def test_unescaped_ASCII_control_characters + (0..31).each do |control_ord| + assert_raises(Oj::ParseError) do + Oj.load(%{"invalid character: #{control_ord.chr}"}) + end + end + end + def test_unicode # hits the 3 normal ranges and one extended surrogate pair json = %{"\\u019f\\u05e9\\u3074\\ud834\\udd1e"}