class
Ruby::MarshalTest
- Ruby::MarshalTest
- Minitest::Test
- Minitest::Runnable
- Reference
- Object
Overview
Every expectation here was produced by real Ruby, not derived by hand:
ruby -ropenssl -e 'd = Marshal.dump({format: "jpg"}) puts d.bytes.inspect puts OpenSSL::Digest::SHA1.base64digest(d)'
Ruby 4.0.7 was used, but the format is version 4.8 and has been stable for many years. :nodoc:
Defined in:
ruby/marshal_test.crInstance Method Summary
- #test_digest_matches_ruby_for_the_built_in_variations
- #test_digest_matches_ruby_for_the_edge_cases
-
#test_dump_an_empty_string
Ruby: Marshal.dump("") => "\x04\bI"\x00\x06:\x06ET"
-
#test_dump_counts_bytes_not_characters
Ruby: Marshal.dump("héllo") => "\x04\bI"\vh\xC3\xA9llo\x06:\x06ET"
-
#test_dump_encodes_a_long_length_over_multiple_bytes
A length of 122 or less fits in one byte, as
length + 5. -
#test_dump_wraps_a_utf8_string_in_an_ivar_record
Every expectation below was taken from Ruby 4.0.7:
-
#test_dumps_a_bare_value
The dump is not limited to a hash at the top level.
- #test_dumps_a_float
- #test_dumps_a_hash_with_one_string
- #test_dumps_a_hash_with_three_strings
-
#test_dumps_a_hash_with_two_strings
The second string reuses the
:Eencoding symbol through a symbol link (;). - #test_dumps_a_mixed_array
-
#test_dumps_a_multibyte_string
The string length is a byte count, not a character count.
- #test_dumps_a_nested_hash
-
#test_dumps_a_nested_hash_of_strings
A symbol link is resolved against the whole document, not against the enclosing hash.
- #test_dumps_an_empty_array
- #test_dumps_an_empty_hash
- #test_dumps_booleans_and_nil
-
#test_dumps_integers
Covers every branch of Ruby's
w_long: the zero byte, the packed positive and negative ranges, and the multi-byte form with a positive and a negative byte count. -
#test_dumps_symbol_values_on_request
With
symbolize_string_valuesevery string value is written as a symbol, which is how a value that travelled through a signed URL key dumps when the Rails app wrote it as a Symbol. -
#test_load_a_binary_string_without_an_ivar_record
Ruby: Marshal.dump("foo".b) => "\x04\b"\bfoo"
-
#test_load_a_string_carrying_a_named_encoding
Ruby: Marshal.dump("foo".encode("Windows-1251")) => "\x04\bI"\bfoo\x06:\rencoding"\x11Windows-1251"
-
#test_load_a_us_ascii_string
Ruby: Marshal.dump("foo".encode("US-ASCII")) => "\x04\bI"\bfoo\x06:\x06EF"
- #test_load_a_utf8_string
-
#test_load_rejects_a_length_longer_than_the_payload
A declared length longer than what follows must not read past the payload.
-
#test_load_rejects_a_payload_that_is_not_a_string
Ruby: Marshal.dump(1) => "\x04\bi\x06", Marshal.dump([]) => "\x04\b[\x00"
- #test_load_rejects_a_truncated_payload
- #test_load_rejects_an_unknown_format_version
-
#test_load_rejects_bytes_that_are_not_valid_utf8
Invalid bytes are reported rather than replaced with U+FFFD, which would corrupt the value without saying so.
- #test_load_rejects_trailing_bytes
- #test_round_trip
-
#test_the_key_order_changes_the_digest
Marshal writes a hash in insertion order, so two orderings of the same pairs do not share a digest.
Instance Method Detail
Ruby: Marshal.dump("héllo") => "\x04\bI"\vh\xC3\xA9llo\x06:\x06ET"
The length is the byte size, not the character count: "héllo" is 5 characters and 6 bytes, so the length byte is 0x0b (6 + 5).
A length of 122 or less fits in one byte, as length + 5. Anything longer is written as a byte
count followed by that many little-endian bytes.
Ruby: Marshal.dump("a" * 300)[0, 7].bytes => [4, 8, 73, 34, 2, 44, 1] Marshal.dump("a" * 117)[0, 5].bytes => [4, 8, 73, 34, 122]
Every expectation below was taken from Ruby 4.0.7:
Marshal.dump("foo").bytes.map { |b| "%02x" % b }.join(" ")
=> "04 08 49 22 08 66 6f 6f 06 3a 06 45 54"
The second string reuses the :E encoding symbol through a symbol link (;).
A symbol link is resolved against the whole document, not against the enclosing hash.
Covers every branch of Ruby's w_long: the zero byte, the packed positive and negative ranges,
and the multi-byte form with a positive and a negative byte count.
With symbolize_string_values every string value is written as a symbol, which is how a value
that travelled through a signed URL key dumps when the Rails app wrote it as a Symbol. The
expectations come from real Ruby:
ruby -e 'puts Marshal.dump({format: :jpg}).bytes.inspect' ruby -e 'puts Marshal.dump({format: :format}).bytes.inspect' ruby -e 'puts Marshal.dump({a: [:x]}).bytes.inspect'
Ruby: Marshal.dump("foo".b) => "\x04\b"\bfoo"
A binary string carries no ivar record at all. Rails writes this form whenever it re-encrypts a
file it read back with File#binread, which is every bin/rails credentials:edit.
Ruby: Marshal.dump("foo".encode("Windows-1251")) => "\x04\bI"\bfoo\x06:\rencoding"\x11Windows-1251"
The encoding name is read and dropped. The bytes themselves still have to be valid UTF-8, which they are here because the string is ASCII.
Ruby: Marshal.dump("foo".encode("US-ASCII")) => "\x04\bI"\bfoo\x06:\x06EF"
A declared length longer than what follows must not read past the payload.
Ruby: Marshal.dump(1) => "\x04\bi\x06", Marshal.dump([]) => "\x04\b[\x00"
Invalid bytes are reported rather than replaced with U+FFFD, which would corrupt the value without saying so.
Marshal writes a hash in insertion order, so two orderings of the same pairs do not share a digest. Rails is unaffected: the signed variation key carries the order the Rails app used.