Skip to content

Commit

Permalink
Add test for unecaped control characters in strings
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
byroot committed Jan 16, 2025
1 parent 7388e5b commit ebf7a68
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions test/test_parser_usual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions test/test_strict.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down

0 comments on commit ebf7a68

Please sign in to comment.