summaryrefslogtreecommitdiff
path: root/test/psych
diff options
context:
space:
mode:
Diffstat (limited to 'test/psych')
-rw-r--r--test/psych/test_exception.rb26
-rw-r--r--test/psych/test_psych.rb86
-rw-r--r--test/psych/test_safe_load.rb81
-rw-r--r--test/psych/test_yaml_special_cases.rb130
4 files changed, 309 insertions, 14 deletions
diff --git a/test/psych/test_exception.rb b/test/psych/test_exception.rb
index 3040bfb7a2..3c72f4a97a 100644
--- a/test/psych/test_exception.rb
+++ b/test/psych/test_exception.rb
@@ -30,9 +30,15 @@ module Psych
assert_nil ex.file
ex = assert_raises(Psych::SyntaxError) do
- Psych.load '--- `', 'meow'
+ Psych.load '--- `', filename: 'meow'
end
assert_equal 'meow', ex.file
+
+ # deprecated interface
+ ex = assert_raises(Psych::SyntaxError) do
+ Psych.load '--- `', 'deprecated'
+ end
+ assert_equal 'deprecated', ex.file
end
def test_psych_parse_stream_takes_file
@@ -43,7 +49,7 @@ module Psych
assert_match '(<unknown>)', ex.message
ex = assert_raises(Psych::SyntaxError) do
- Psych.parse_stream '--- `', 'omg!'
+ Psych.parse_stream '--- `', filename: 'omg!'
end
assert_equal 'omg!', ex.file
assert_match 'omg!', ex.message
@@ -57,9 +63,15 @@ module Psych
assert_match '(<unknown>)', ex.message
ex = assert_raises(Psych::SyntaxError) do
- Psych.load_stream '--- `', 'omg!'
+ Psych.load_stream '--- `', filename: 'omg!'
end
assert_equal 'omg!', ex.file
+
+ # deprecated interface
+ ex = assert_raises(Psych::SyntaxError) do
+ Psych.load_stream '--- `', 'deprecated'
+ end
+ assert_equal 'deprecated', ex.file
end
def test_parse_file_exception
@@ -94,9 +106,15 @@ module Psych
assert_nil ex.file
ex = assert_raises(Psych::SyntaxError) do
- Psych.parse '--- `', 'omg!'
+ Psych.parse '--- `', filename: 'omg!'
end
assert_match 'omg!', ex.message
+
+ # deprecated interface
+ ex = assert_raises(Psych::SyntaxError) do
+ Psych.parse '--- `', 'deprecated'
+ end
+ assert_match 'deprecated', ex.message
end
def test_attributes
diff --git a/test/psych/test_psych.rb b/test/psych/test_psych.rb
index 8f9a10013d..3a04a3fe70 100644
--- a/test/psych/test_psych.rb
+++ b/test/psych/test_psych.rb
@@ -60,6 +60,22 @@ class TestPsych < Psych::TestCase
end
end
+ def test_parse
+ assert_equal %w[a b], Psych.parse("- a\n- b").to_ruby
+ end
+
+ def test_parse_default_fallback
+ assert_equal false, Psych.parse("")
+ end
+
+ def test_parse_raises_on_bad_input
+ assert_raises(Psych::SyntaxError) { Psych.parse("--- `") }
+ end
+
+ def test_parse_with_fallback
+ assert_equal 42, Psych.parse("", fallback: 42)
+ end
+
def test_non_existing_class_on_deserialize
e = assert_raises(ArgumentError) do
Psych.load("--- !ruby/object:NonExistent\nfoo: 1")
@@ -103,9 +119,44 @@ class TestPsych < Psych::TestCase
assert_equal %w{ foo bar }, docs
end
+ def test_load_stream_default_fallback
+ assert_equal [], Psych.load_stream("")
+ end
+
+ def test_load_stream_raises_on_bad_input
+ assert_raises(Psych::SyntaxError) { Psych.load_stream("--- `") }
+ end
+
def test_parse_stream
docs = Psych.parse_stream("--- foo\n...\n--- bar\n...")
- assert_equal %w{ foo bar }, docs.children.map { |x| x.transform }
+ assert_equal(%w[foo bar], docs.children.map(&:transform))
+ end
+
+ def test_parse_stream_with_block
+ docs = []
+ Psych.parse_stream("--- foo\n...\n--- bar\n...") do |node|
+ docs << node
+ end
+
+ assert_equal %w[foo bar], docs.map(&:to_ruby)
+ end
+
+ def test_parse_stream_default_fallback
+ docs = Psych.parse_stream("")
+ assert_equal [], docs.children.map(&:to_ruby)
+ end
+
+ def test_parse_stream_with_block_default_fallback
+ docs = []
+ Psych.parse_stream("") do |node|
+ docs << node
+ end
+
+ assert_equal [], docs.map(&:to_ruby)
+ end
+
+ def test_parse_stream_raises_on_bad_input
+ assert_raises(Psych::SyntaxError) { Psych.parse_stream("--- `") }
end
def test_add_builtin_type
@@ -135,6 +186,31 @@ class TestPsych < Psych::TestCase
assert_equal({ 'hello' => 'world' }, got)
end
+ def test_load_default_fallback
+ assert_equal false, Psych.load("")
+ end
+
+ def test_load_with_fallback
+ assert_equal 42, Psych.load("", "file", fallback: 42)
+ end
+
+ def test_load_with_fallback_nil_or_false
+ assert_nil Psych.load("", "file", fallback: nil)
+ assert_equal false, Psych.load("", "file", fallback: false)
+ end
+
+ def test_load_with_fallback_hash
+ assert_equal Hash.new, Psych.load("", "file", fallback: Hash.new)
+ end
+
+ def test_load_with_fallback_for_nil
+ assert_nil Psych.load("--- null", "file", fallback: 42)
+ end
+
+ def test_load_with_fallback_for_false
+ assert_equal false, Psych.load("--- false", "file", fallback: 42)
+ end
+
def test_load_file
Tempfile.create(['yikes', 'yml']) {|t|
t.binmode
@@ -144,7 +220,7 @@ class TestPsych < Psych::TestCase
}
end
- def test_load_file_default_return_value
+ def test_load_file_default_fallback
Tempfile.create(['empty', 'yml']) {|t|
assert_equal false, Psych.load_file(t.path)
}
@@ -196,6 +272,12 @@ class TestPsych < Psych::TestCase
}
end
+ def test_parse_file_default_fallback
+ Tempfile.create(['empty', 'yml']) do |t|
+ assert_equal false, Psych.parse_file(t.path)
+ end
+ end
+
def test_degenerate_strings
assert_equal false, Psych.load(' ')
assert_equal false, Psych.parse(' ')
diff --git a/test/psych/test_safe_load.rb b/test/psych/test_safe_load.rb
index f3fdb9b9a2..82a5f19c36 100644
--- a/test/psych/test_safe_load.rb
+++ b/test/psych/test_safe_load.rb
@@ -22,6 +22,8 @@ module Psych
def test_explicit_recursion
x = []
x << x
+ assert_equal(x, Psych.safe_load(Psych.dump(x), whitelist_classes: [], whitelist_symbols: [], aliases: true))
+ # deprecated interface
assert_equal(x, Psych.safe_load(Psych.dump(x), [], [], true))
end
@@ -30,6 +32,16 @@ module Psych
assert_raises(Psych::DisallowedClass) do
Psych.safe_load yml
end
+ assert_equal(
+ :foo,
+ Psych.safe_load(
+ yml,
+ whitelist_classes: [Symbol],
+ whitelist_symbols: [:foo]
+ )
+ )
+
+ # deprecated interface
assert_equal(:foo, Psych.safe_load(yml, [Symbol], [:foo]))
end
@@ -38,32 +50,71 @@ module Psych
assert_safe_cycle :foo
end
assert_raises(Psych::DisallowedClass) do
+ Psych.safe_load '--- !ruby/symbol foo', whitelist_classes: []
+ end
+
+ # deprecated interface
+ assert_raises(Psych::DisallowedClass) do
Psych.safe_load '--- !ruby/symbol foo', []
end
- assert_safe_cycle :foo, [Symbol]
- assert_safe_cycle :foo, %w{ Symbol }
+
+ assert_safe_cycle :foo, whitelist_classes: [Symbol]
+ assert_safe_cycle :foo, whitelist_classes: %w{ Symbol }
+ assert_equal :foo, Psych.safe_load('--- !ruby/symbol foo', whitelist_classes: [Symbol])
+
+ # deprecated interface
assert_equal :foo, Psych.safe_load('--- !ruby/symbol foo', [Symbol])
end
def test_foo
assert_raises(Psych::DisallowedClass) do
+ Psych.safe_load '--- !ruby/object:Foo {}', whitelist_classes: [Foo]
+ end
+
+ # deprecated interface
+ assert_raises(Psych::DisallowedClass) do
Psych.safe_load '--- !ruby/object:Foo {}', [Foo]
end
+
assert_raises(Psych::DisallowedClass) do
assert_safe_cycle Foo.new
end
+ assert_kind_of(Foo, Psych.safe_load(Psych.dump(Foo.new), whitelist_classes: [Foo]))
+
+ # deprecated interface
assert_kind_of(Foo, Psych.safe_load(Psych.dump(Foo.new), [Foo]))
end
X = Struct.new(:x)
def test_struct_depends_on_sym
- assert_safe_cycle(X.new, [X, Symbol])
+ assert_safe_cycle(X.new, whitelist_classes: [X, Symbol])
assert_raises(Psych::DisallowedClass) do
- cycle X.new, [X]
+ cycle X.new, whitelist_classes: [X]
end
end
def test_anon_struct
+ assert Psych.safe_load(<<-eoyml, whitelist_classes: [Struct, Symbol])
+--- !ruby/struct
+ foo: bar
+ eoyml
+
+ assert_raises(Psych::DisallowedClass) do
+ Psych.safe_load(<<-eoyml, whitelist_classes: [Struct])
+--- !ruby/struct
+ foo: bar
+ eoyml
+ end
+
+ assert_raises(Psych::DisallowedClass) do
+ Psych.safe_load(<<-eoyml, whitelist_classes: [Symbol])
+--- !ruby/struct
+ foo: bar
+ eoyml
+ end
+ end
+
+ def test_deprecated_anon_struct
assert Psych.safe_load(<<-eoyml, [Struct, Symbol])
--- !ruby/struct
foo: bar
@@ -84,14 +135,28 @@ module Psych
end
end
+ def test_safe_load_default_fallback
+ assert_nil Psych.safe_load("")
+ end
+
+ def test_safe_load
+ assert_equal %w[a b], Psych.safe_load("- a\n- b")
+ end
+
+ def test_safe_load_raises_on_bad_input
+ assert_raises(Psych::SyntaxError) { Psych.safe_load("--- `") }
+ end
+
private
- def cycle object, whitelist = []
- Psych.safe_load(Psych.dump(object), whitelist)
+ def cycle object, whitelist_classes: []
+ Psych.safe_load(Psych.dump(object), whitelist_classes: whitelist_classes)
+ # deprecated interface test
+ Psych.safe_load(Psych.dump(object), whitelist_classes)
end
- def assert_safe_cycle object, whitelist = []
- other = cycle object, whitelist
+ def assert_safe_cycle object, whitelist_classes: []
+ other = cycle object, whitelist_classes: whitelist_classes
assert_equal object, other
end
end
diff --git a/test/psych/test_yaml_special_cases.rb b/test/psych/test_yaml_special_cases.rb
new file mode 100644
index 0000000000..66448d4efa
--- /dev/null
+++ b/test/psych/test_yaml_special_cases.rb
@@ -0,0 +1,130 @@
+# frozen_string_literal: true
+
+require_relative 'helper'
+
+require 'stringio'
+require 'tempfile'
+
+module Psych
+ class TestYamlSpecialCases < TestCase
+ def setup
+ super
+ end
+
+ def test_empty_string
+ s = ""
+ assert_equal false, Psych.load(s)
+ assert_equal [], Psych.load_stream(s)
+ assert_equal false, Psych.parse(s)
+ assert_equal [], Psych.parse_stream(s).transform
+ assert_equal nil, Psych.safe_load(s)
+ end
+
+ def test_false
+ s = "false"
+ assert_equal false, Psych.load(s)
+ assert_equal [false], Psych.load_stream(s)
+ assert_equal false, Psych.parse(s).transform
+ assert_equal [false], Psych.parse_stream(s).transform
+ assert_equal false, Psych.safe_load(s)
+ end
+
+ def test_n
+ s = "n"
+ assert_equal "n", Psych.load(s)
+ assert_equal ["n"], Psych.load_stream(s)
+ assert_equal "n", Psych.parse(s).transform
+ assert_equal ["n"], Psych.parse_stream(s).transform
+ assert_equal "n", Psych.safe_load(s)
+ end
+
+ def test_off
+ s = "off"
+ assert_equal false, Psych.load(s)
+ assert_equal [false], Psych.load_stream(s)
+ assert_equal false, Psych.parse(s).transform
+ assert_equal [false], Psych.parse_stream(s).transform
+ assert_equal false, Psych.safe_load(s)
+ end
+
+ def test_inf
+ s = "-.inf"
+ assert_equal -Float::INFINITY, Psych.load(s)
+ assert_equal [-Float::INFINITY], Psych.load_stream(s)
+ assert_equal -Float::INFINITY, Psych.parse(s).transform
+ assert_equal [-Float::INFINITY], Psych.parse_stream(s).transform
+ assert_equal -Float::INFINITY, Psych.safe_load(s)
+ end
+
+ def test_NaN
+ s = ".NaN"
+ assert Float::NAN, Psych.load(s).nan?
+ assert [Float::NAN], Psych.load_stream(s).first.nan?
+ assert Psych.parse(s).transform.nan?
+ assert Psych.parse_stream(s).transform.first.nan?
+ assert Psych.safe_load(s).nan?
+ end
+
+ def test_0xC
+ s = "0xC"
+ assert_equal 12, Psych.load(s)
+ assert_equal [12], Psych.load_stream(s)
+ assert_equal 12, Psych.parse(s).transform
+ assert_equal [12], Psych.parse_stream(s).transform
+ assert_equal 12, Psych.safe_load(s)
+ end
+
+ def test_arrows
+ s = "<<"
+ assert_equal "<<", Psych.load(s)
+ assert_equal ["<<"], Psych.load_stream(s)
+ assert_equal "<<", Psych.parse(s).transform
+ assert_equal ["<<"], Psych.parse_stream(s).transform
+ assert_equal "<<", Psych.safe_load(s)
+ end
+
+ def test_arrows_hash
+ s = "<<: {}"
+ assert_equal({}, Psych.load(s))
+ assert_equal [{}], Psych.load_stream(s)
+ assert_equal({}, Psych.parse(s).transform)
+ assert_equal [{}], Psych.parse_stream(s).transform
+ assert_equal({}, Psych.safe_load(s))
+ end
+
+ def test_thousand
+ s = "- 1000\n- +1000\n- 1_000"
+ assert_equal [1000, 1000, 1000], Psych.load(s)
+ assert_equal [[1000, 1000, 1000]], Psych.load_stream(s)
+ assert_equal [1000, 1000, 1000], Psych.parse(s).transform
+ assert_equal [[1000, 1000, 1000]], Psych.parse_stream(s).transform
+ assert_equal [1000, 1000, 1000], Psych.safe_load(s)
+ end
+
+ def test_8
+ s = "[8, 08, 0o10, 010]"
+ assert_equal [8, "08", "0o10", 8], Psych.load(s)
+ assert_equal [[8, "08", "0o10", 8]], Psych.load_stream(s)
+ assert_equal [8, "08", "0o10", 8], Psych.parse(s).transform
+ assert_equal [[8, "08", "0o10", 8]], Psych.parse_stream(s).transform
+ assert_equal [8, "08", "0o10", 8], Psych.safe_load(s)
+ end
+
+ def test_null
+ s = "null"
+ assert_equal nil, Psych.load(s)
+ assert_equal [nil], Psych.load_stream(s)
+ assert_equal nil, Psych.parse(s).transform
+ assert_equal [nil], Psych.parse_stream(s).transform
+ assert_equal nil, Psych.safe_load(s)
+ end
+
+ private
+
+ def special_case_cycle(object)
+ %w[load load_stream parse parse_stream safe_load].map do |m|
+ Psych.public_send(m, object)
+ end
+ end
+ end
+end