1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
|
# -*- encoding: binary -*-
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/marshal_data', __FILE__)
describe "Marshal.dump" do
it "dumps nil" do
Marshal.dump(nil).should == "\004\b0"
end
it "dumps true" do
Marshal.dump(true).should == "\004\bT"
end
it "dumps false" do
Marshal.dump(false).should == "\004\bF"
end
describe "with a Fixnum" do
it "dumps a Fixnum" do
[ [Marshal, 0, "\004\bi\000"],
[Marshal, 5, "\004\bi\n"],
[Marshal, 8, "\004\bi\r"],
[Marshal, 122, "\004\bi\177"],
[Marshal, 123, "\004\bi\001{"],
[Marshal, 1234, "\004\bi\002\322\004"],
[Marshal, -8, "\004\bi\363"],
[Marshal, -123, "\004\bi\200"],
[Marshal, -124, "\004\bi\377\204"],
[Marshal, -1234, "\004\bi\376.\373"],
[Marshal, -4516727, "\004\bi\375\211\024\273"],
[Marshal, 2**8, "\004\bi\002\000\001"],
[Marshal, 2**16, "\004\bi\003\000\000\001"],
[Marshal, 2**24, "\004\bi\004\000\000\000\001"],
[Marshal, -2**8, "\004\bi\377\000"],
[Marshal, -2**16, "\004\bi\376\000\000"],
[Marshal, -2**24, "\004\bi\375\000\000\000"],
].should be_computed_by(:dump)
end
platform_is wordsize: 64 do
it "dumps a positive Fixnum > 31 bits as a Bignum" do
Marshal.dump(2**31 + 1).should == "\x04\bl+\a\x01\x00\x00\x80"
end
it "dumps a negative Fixnum > 31 bits as a Bignum" do
Marshal.dump(-2**31 - 1).should == "\x04\bl-\a\x01\x00\x00\x80"
end
end
end
describe "with a Symbol" do
it "dumps a Symbol" do
Marshal.dump(:symbol).should == "\004\b:\vsymbol"
end
it "dumps a big Symbol" do
Marshal.dump(('big' * 100).to_sym).should == "\004\b:\002,\001#{'big' * 100}"
end
it "dumps an encoded Symbol" do
s = "\u2192"
[ [Marshal, s.encode("utf-8").to_sym,
"\x04\bI:\b\xE2\x86\x92\x06:\x06ET"],
[Marshal, s.encode("utf-16").to_sym,
"\x04\bI:\t\xFE\xFF!\x92\x06:\rencoding\"\vUTF-16"],
[Marshal, s.encode("euc-jp").to_sym,
"\x04\bI:\a\xA2\xAA\x06:\rencoding\"\vEUC-JP"],
[Marshal, s.encode("sjis").to_sym,
"\x04\bI:\a\x81\xA8\x06:\rencoding\"\x10Windows-31J"]
].should be_computed_by(:dump)
end
it "dumps a binary encoded Symbol" do
s = "\u2192".force_encoding("binary").to_sym
Marshal.dump(s).should == "\x04\b:\b\xE2\x86\x92"
end
end
it "dumps an extended_object" do
Marshal.dump(Object.new.extend(Meths)).should == "\x04\be:\nMethso:\vObject\x00"
end
it "dumps an object that has had an ivar added and removed as though the ivar never was set" do
obj = Object.new
initial = Marshal.dump(obj)
obj.instance_variable_set(:@ivar, 1)
Marshal.dump(obj).should == "\004\bo:\vObject\006:\n@ivari\006"
obj.send :remove_instance_variable, :@ivar
Marshal.dump(obj).should == initial
end
describe "with an object responding to #marshal_dump" do
it "dumps the object returned by #marshal_dump" do
Marshal.dump(UserMarshal.new).should == "\x04\bU:\x10UserMarshal:\tdata"
end
it "does not use Class#name" do
UserMarshal.should_not_receive(:name)
Marshal.dump(UserMarshal.new)
end
end
describe "with an object responding to #_dump" do
it "dumps the object returned by #marshal_dump" do
Marshal.dump(UserDefined.new).should == "\004\bu:\020UserDefined\022\004\b[\a:\nstuff;\000"
end
it "raises a TypeError if _dump returns a non-string" do
m = mock("marshaled")
m.should_receive(:_dump).and_return(0)
lambda { Marshal.dump(m) }.should raise_error(TypeError)
end
it "favors marshal_dump over _dump" do
m = mock("marshaled")
m.should_receive(:marshal_dump).and_return(0)
m.should_not_receive(:_dump)
Marshal.dump(m)
end
end
describe "with a Class" do
it "dumps a builtin Class" do
Marshal.dump(String).should == "\004\bc\vString"
end
it "dumps a user Class" do
Marshal.dump(UserDefined).should == "\x04\bc\x10UserDefined"
end
it "dumps a nested Class" do
Marshal.dump(UserDefined::Nested).should == "\004\bc\030UserDefined::Nested"
end
it "raises TypeError with an anonymous Class" do
lambda { Marshal.dump(Class.new) }.should raise_error(TypeError)
end
it "raises TypeError with a singleton Class" do
lambda { Marshal.dump(class << self; self end) }.should raise_error(TypeError)
end
end
describe "with a Module" do
it "dumps a builtin Module" do
Marshal.dump(Marshal).should == "\004\bm\fMarshal"
end
it "raises TypeError with an anonymous Module" do
lambda { Marshal.dump(Module.new) }.should raise_error(TypeError)
end
end
describe "with a Float" do
it "dumps a Float" do
[ [Marshal, 0.0, "\004\bf\0060"],
[Marshal, -0.0, "\004\bf\a-0"],
[Marshal, 1.0, "\004\bf\0061"],
[Marshal, 123.4567, "\004\bf\r123.4567"],
[Marshal, -0.841, "\x04\bf\v-0.841"],
[Marshal, -9876.345, "\x04\bf\x0E-9876.345"],
[Marshal, infinity_value, "\004\bf\binf"],
[Marshal, -infinity_value, "\004\bf\t-inf"],
[Marshal, nan_value, "\004\bf\bnan"],
].should be_computed_by(:dump)
end
end
describe "with a Bignum" do
it "dumps a Bignum" do
[ [Marshal, -4611686018427387903, "\004\bl-\t\377\377\377\377\377\377\377?"],
[Marshal, -2361183241434822606847, "\004\bl-\n\377\377\377\377\377\377\377\377\177\000"],
].should be_computed_by(:dump)
end
it "dumps a Bignum" do
[ [Marshal, 2**64, "\004\bl+\n\000\000\000\000\000\000\000\000\001\000"],
[Marshal, 2**90, "\004\bl+\v#{"\000" * 11}\004"],
[Marshal, -2**63, "\004\bl-\t\000\000\000\000\000\000\000\200"],
[Marshal, -2**64, "\004\bl-\n\000\000\000\000\000\000\000\000\001\000"],
].should be_computed_by(:dump)
end
end
describe "with a String" do
it "dumps a blank String" do
Marshal.dump("".force_encoding("binary")).should == "\004\b\"\000"
end
it "dumps a short String" do
Marshal.dump("short".force_encoding("binary")).should == "\004\b\"\012short"
end
it "dumps a long String" do
Marshal.dump(("big" * 100).force_encoding("binary")).should == "\004\b\"\002,\001#{"big" * 100}"
end
it "dumps a String extended with a Module" do
Marshal.dump("".extend(Meths).force_encoding("binary")).should == "\004\be:\nMeths\"\000"
end
it "dumps a String subclass" do
Marshal.dump(UserString.new.force_encoding("binary")).should == "\004\bC:\017UserString\"\000"
end
it "dumps a String subclass extended with a Module" do
Marshal.dump(UserString.new.extend(Meths).force_encoding("binary")).should == "\004\be:\nMethsC:\017UserString\"\000"
end
it "dumps a String with instance variables" do
str = ""
str.instance_variable_set("@foo", "bar")
Marshal.dump(str.force_encoding("binary")).should == "\x04\bI\"\x00\x06:\t@foo\"\bbar"
end
with_feature :encoding do
it "dumps a US-ASCII String" do
str = "abc".force_encoding("us-ascii")
Marshal.dump(str).should == "\x04\bI\"\babc\x06:\x06EF"
end
it "dumps a UTF-8 String" do
str = "\x6d\xc3\xb6\x68\x72\x65".force_encoding("utf-8")
Marshal.dump(str).should == "\x04\bI\"\vm\xC3\xB6hre\x06:\x06ET"
end
it "dumps a String in another encoding" do
str = "\x6d\x00\xf6\x00\x68\x00\x72\x00\x65\x00".force_encoding("utf-16le")
result = "\x04\bI\"\x0Fm\x00\xF6\x00h\x00r\x00e\x00\x06:\rencoding\"\rUTF-16LE"
Marshal.dump(str).should == result
end
it "dumps multiple strings using symlinks for the :E (encoding) symbol" do
Marshal.dump(["".encode("us-ascii"), "".encode("utf-8")]).should == "\x04\b[\aI\"\x00\x06:\x06EFI\"\x00\x06;\x00T"
end
end
end
describe "with a Regexp" do
it "dumps a Regexp" do
Marshal.dump(/\A.\Z/).should == "\x04\bI/\n\\A.\\Z\x00\x06:\x06EF"
end
it "dumps a Regexp with flags" do
Marshal.dump(//im).should == "\x04\bI/\x00\x05\x06:\x06EF"
end
it "dumps a Regexp with instance variables" do
o = //
o.instance_variable_set(:@ivar, :ivar)
Marshal.dump(o).should == "\x04\bI/\x00\x00\a:\x06EF:\n@ivar:\tivar"
end
it "dumps an extended Regexp" do
Marshal.dump(//.extend(Meths)).should == "\x04\bIe:\nMeths/\x00\x00\x06:\x06EF"
end
it "dumps a Regexp subclass" do
Marshal.dump(UserRegexp.new("")).should == "\x04\bIC:\x0FUserRegexp/\x00\x00\x06:\x06EF"
end
it "dumps a binary Regexp" do
o = Regexp.new("".force_encoding("binary"), Regexp::FIXEDENCODING)
Marshal.dump(o).should == "\x04\b/\x00\x10"
end
it "dumps a UTF-8 Regexp" do
o = Regexp.new("".force_encoding("utf-8"), Regexp::FIXEDENCODING)
Marshal.dump(o).should == "\x04\bI/\x00\x10\x06:\x06ET"
end
it "dumps a Regexp in another encoding" do
o = Regexp.new("".force_encoding("utf-16le"), Regexp::FIXEDENCODING)
Marshal.dump(o).should == "\x04\bI/\x00\x10\x06:\rencoding\"\rUTF-16LE"
end
end
describe "with an Array" do
it "dumps an empty Array" do
Marshal.dump([]).should == "\004\b[\000"
end
it "dumps a non-empty Array" do
Marshal.dump([:a, 1, 2]).should == "\004\b[\b:\006ai\006i\a"
end
it "dumps an Array subclass" do
Marshal.dump(UserArray.new).should == "\004\bC:\016UserArray[\000"
end
it "dumps a recursive Array" do
a = []
a << a
Marshal.dump(a).should == "\x04\b[\x06@\x00"
end
it "dumps an Array with instance variables" do
a = []
a.instance_variable_set(:@ivar, 1)
Marshal.dump(a).should == "\004\bI[\000\006:\n@ivari\006"
end
it "dumps an extended Array" do
Marshal.dump([].extend(Meths)).should == "\004\be:\nMeths[\000"
end
end
describe "with a Hash" do
it "dumps a Hash" do
Marshal.dump({}).should == "\004\b{\000"
end
it "dumps a Hash subclass" do
Marshal.dump(UserHash.new).should == "\004\bC:\rUserHash{\000"
end
it "dumps a Hash with a default value" do
Marshal.dump(Hash.new(1)).should == "\004\b}\000i\006"
end
it "raises a TypeError with hash having default proc" do
lambda { Marshal.dump(Hash.new {}) }.should raise_error(TypeError)
end
it "dumps a Hash with instance variables" do
a = {}
a.instance_variable_set(:@ivar, 1)
Marshal.dump(a).should == "\004\bI{\000\006:\n@ivari\006"
end
it "dumps an extended Hash" do
Marshal.dump({}.extend(Meths)).should == "\004\be:\nMeths{\000"
end
it "dumps an Hash subclass with a parameter to initialize" do
Marshal.dump(UserHashInitParams.new(1)).should == "\004\bIC:\027UserHashInitParams{\000\006:\a@ai\006"
end
end
describe "with a Struct" do
it "dumps a Struct" do
Marshal.dump(Struct::Pyramid.new).should == "\004\bS:\024Struct::Pyramid\000"
end
it "dumps a Struct" do
Marshal.dump(Struct::Useful.new(1, 2)).should == "\004\bS:\023Struct::Useful\a:\006ai\006:\006bi\a"
end
it "dumps a Struct with instance variables" do
st = Struct.new("Thick").new
st.instance_variable_set(:@ivar, 1)
Marshal.dump(st).should == "\004\bIS:\022Struct::Thick\000\006:\n@ivari\006"
Struct.send(:remove_const, :Thick)
end
it "dumps an extended Struct" do
st = Struct.new("Extended", :a, :b).new
Marshal.dump(st.extend(Meths)).should == "\004\be:\nMethsS:\025Struct::Extended\a:\006a0:\006b0"
Struct.send(:remove_const, :Extended)
end
end
describe "with an Object" do
it "dumps an Object" do
Marshal.dump(Object.new).should == "\004\bo:\x0BObject\x00"
end
it "dumps an extended Object" do
Marshal.dump(Object.new.extend(Meths)).should == "\004\be:\x0AMethso:\x0BObject\x00"
end
it "dumps an Object with an instance variable" do
obj = Object.new
obj.instance_variable_set(:@ivar, 1)
Marshal.dump(obj).should == "\004\bo:\vObject\006:\n@ivari\006"
end
it "dumps an Object that has had an instance variable added and removed as though it was never set" do
obj = Object.new
obj.instance_variable_set(:@ivar, 1)
obj.send(:remove_instance_variable, :@ivar)
Marshal.dump(obj).should == "\004\bo:\x0BObject\x00"
end
it "dumps an Object if it has a singleton class but no singleton methods" do
obj = Object.new
obj.singleton_class
Marshal.dump(obj).should == "\004\bo:\x0BObject\x00"
end
it "raises if an Object has a singleton class and singleton methods" do
obj = Object.new
def obj.foo; end
lambda {
Marshal.dump(obj)
}.should raise_error(TypeError, "singleton can't be dumped")
end
it "dumps a BasicObject subclass if it defines respond_to?" do
obj = MarshalSpec::BasicObjectSubWithRespondToFalse.new
Marshal.dump(obj).should == "\x04\bo:2MarshalSpec::BasicObjectSubWithRespondToFalse\x00"
end
end
describe "with a Range" do
it "dumps a Range inclusive of end (with indeterminant order)" do
dump = Marshal.dump(1..2)
load = Marshal.load(dump)
load.should == (1..2)
end
it "dumps a Range exclusive of end (with indeterminant order)" do
dump = Marshal.dump(1...2)
load = Marshal.load(dump)
load.should == (1...2)
end
end
describe "with a Time" do
before :each do
@internal = Encoding.default_internal
Encoding.default_internal = Encoding::UTF_8
@utc = Time.utc(2012, 1, 1)
@utc_dump = @utc.send(:_dump)
with_timezone 'AST', 3 do
@t = Time.local(2012, 1, 1)
@fract = Time.local(2012, 1, 1, 1, 59, 56.2)
@t_dump = @t.send(:_dump)
@fract_dump = @fract.send(:_dump)
end
end
after :each do
Encoding.default_internal = @internal
end
it "dumps the zone and the offset" do
with_timezone 'AST', 3 do
dump = Marshal.dump(@t)
base = "\x04\bIu:\tTime\r#{@t_dump}\a"
offset = ":\voffseti\x020*"
zone = ":\tzoneI\"\bAST\x06:\x06EF" # Last is 'F' (US-ASCII)
[ "#{base}#{offset}#{zone}", "#{base}#{zone}#{offset}" ].should include(dump)
end
it "dumps the zone, but not the offset if zone is UTC" do
dump = Marshal.dump(@utc)
zone = ":\tzoneI\"\bUTC\x06:\x06EF" # Last is 'F' (US-ASCII)
dump.should == "\x04\bIu:\tTime\r#{@utc_dump}\x06#{zone}"
end
end
end
describe "with an Exception" do
it "dumps an empty Exception" do
Marshal.dump(Exception.new).should == "\x04\bo:\x0EException\a:\tmesg0:\abt0"
end
it "dumps the message for the exception" do
Marshal.dump(Exception.new("foo")).should == "\x04\bo:\x0EException\a:\tmesg\"\bfoo:\abt0"
end
it "contains the filename in the backtrace" do
obj = Exception.new("foo")
obj.set_backtrace(["foo/bar.rb:10"])
Marshal.dump(obj).should == "\x04\bo:\x0EException\a:\tmesg\"\bfoo:\abt[\x06\"\x12foo/bar.rb:10"
end
end
it "dumps subsequent appearances of a symbol as a link" do
Marshal.dump([:a, :a]).should == "\004\b[\a:\006a;\000"
end
it "dumps subsequent appearances of an object as a link" do
o = Object.new
Marshal.dump([o, o]).should == "\004\b[\ao:\vObject\000@\006"
end
MarshalSpec::DATA_19.each do |description, (object, marshal, attributes)|
it "#{description} returns a binary string" do
Marshal.dump(object).encoding.should == Encoding::BINARY
end
end
it "raises an ArgumentError when the recursion limit is exceeded" do
h = {'one' => {'two' => {'three' => 0}}}
lambda { Marshal.dump(h, 3) }.should raise_error(ArgumentError)
lambda { Marshal.dump([h], 4) }.should raise_error(ArgumentError)
lambda { Marshal.dump([], 0) }.should raise_error(ArgumentError)
lambda { Marshal.dump([[[]]], 1) }.should raise_error(ArgumentError)
end
it "ignores the recursion limit if the limit is negative" do
Marshal.dump([], -1).should == "\004\b[\000"
Marshal.dump([[]], -1).should == "\004\b[\006[\000"
Marshal.dump([[[]]], -1).should == "\004\b[\006[\006[\000"
end
describe "when passed an IO" do
it "writes the serialized data to the IO-Object" do
(obj = mock('test')).should_receive(:write).at_least(1)
Marshal.dump("test", obj)
end
it "returns the IO-Object" do
(obj = mock('test')).should_receive(:write).at_least(1)
Marshal.dump("test", obj).should == obj
end
it "raises an Error when the IO-Object does not respond to #write" do
obj = mock('test')
lambda { Marshal.dump("test", obj) }.should raise_error(TypeError)
end
with_feature :encoding do
it "calls binmode when it's defined" do
obj = mock('test')
obj.should_receive(:write).at_least(1)
obj.should_receive(:binmode).at_least(1)
Marshal.dump("test", obj)
end
end
end
it "raises a TypeError if marshalling a Method instance" do
lambda { Marshal.dump(Marshal.method(:dump)) }.should raise_error(TypeError)
end
it "raises a TypeError if marshalling a Proc" do
lambda { Marshal.dump(proc {}) }.should raise_error(TypeError)
end
it "raises a TypeError if dumping a IO/File instance" do
lambda { Marshal.dump(STDIN) }.should raise_error(TypeError)
lambda { File.open(__FILE__) { |f| Marshal.dump(f) } }.should raise_error(TypeError)
end
it "raises a TypeError if dumping a MatchData instance" do
lambda { Marshal.dump(/(.)/.match("foo")) }.should raise_error(TypeError)
end
it "returns an untainted string if object is untainted" do
Marshal.dump(Object.new).tainted?.should be_false
end
it "returns a tainted string if object is tainted" do
Marshal.dump(Object.new.taint).tainted?.should be_true
end
it "returns a tainted string if nested object is tainted" do
Marshal.dump([[Object.new.taint]]).tainted?.should be_true
end
it "returns a trusted string if object is trusted" do
Marshal.dump(Object.new).untrusted?.should be_false
end
it "returns an untrusted string if object is untrusted" do
Marshal.dump(Object.new.untrust).untrusted?.should be_true
end
it "returns an untrusted string if nested object is untrusted" do
Marshal.dump([[Object.new.untrust]]).untrusted?.should be_true
end
end
|