class Ruby::MarshalTest

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.cr

Instance Method Summary

Instance Method Detail

def test_digest_matches_ruby_for_the_built_in_variations #

def test_digest_matches_ruby_for_the_edge_cases #

def test_dump_an_empty_string #

Ruby: Marshal.dump("") => "\x04\bI"\x00\x06:\x06ET"


def test_dump_counts_bytes_not_characters #

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).


def test_dump_encodes_a_long_length_over_multiple_bytes #

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]


def test_dump_wraps_a_utf8_string_in_an_ivar_record #

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"


def test_dumps_a_bare_value #

The dump is not limited to a hash at the top level.


def test_dumps_a_float #

def test_dumps_a_hash_with_one_string #

def test_dumps_a_hash_with_three_strings #

def test_dumps_a_hash_with_two_strings #

The second string reuses the :E encoding symbol through a symbol link (;).


def test_dumps_a_mixed_array #

def test_dumps_a_multibyte_string #

The string length is a byte count, not a character count.


def test_dumps_a_nested_hash #

def test_dumps_a_nested_hash_of_strings #

A symbol link is resolved against the whole document, not against the enclosing hash.


def test_dumps_an_empty_array #

def test_dumps_an_empty_hash #

def test_dumps_booleans_and_nil #

def 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.


def test_dumps_symbol_values_on_request #

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'


def test_load_a_binary_string_without_an_ivar_record #

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.


def test_load_a_string_carrying_a_named_encoding #

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.


def test_load_a_us_ascii_string #

Ruby: Marshal.dump("foo".encode("US-ASCII")) => "\x04\bI"\bfoo\x06:\x06EF"


def test_load_a_utf8_string #

def test_load_rejects_a_length_longer_than_the_payload #

A declared length longer than what follows must not read past the payload.


def test_load_rejects_a_payload_that_is_not_a_string #

Ruby: Marshal.dump(1) => "\x04\bi\x06", Marshal.dump([]) => "\x04\b[\x00"


def test_load_rejects_a_truncated_payload #

def test_load_rejects_an_unknown_format_version #

def 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.


def test_load_rejects_trailing_bytes #

def test_round_trip #

def 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. Rails is unaffected: the signed variation key carries the order the Rails app used.