Skip to content

Commit

Permalink
Add tests of __wakeup in serialization working as expected
Browse files Browse the repository at this point in the history
Test adding a lot of unique dynamic properties in __wakeup
Print the values before and after serialization.

And modify another test of serialization

- It referred to a value that was deleted in __wakeup, so it used to be
  converted to UNDEF.
  This conversion to UNDEF is no longer necessary, since we create the copy/reference *before* __wakeup is
  called
  • Loading branch information
TysonAndre committed Aug 31, 2016
1 parent b8d26b3 commit 56fabb4
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 2 deletions.
8 changes: 6 additions & 2 deletions ext/standard/tests/serialize/bug70963.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var_dump(unserialize('a:2:{i:0;O:9:"exception":1:{s:16:"'."\0".'Exception'."\0".
var_dump(unserialize('a:2:{i:0;O:9:"exception":1:{s:16:"'."\0".'Exception'."\0".'trace";s:4:"test";}i:1;r:3;}'));
?>
--EXPECTF--
array(1) {
array(2) {
[0]=>
object(Exception)#%d (6) {
["message":protected]=>
Expand All @@ -22,8 +22,10 @@ array(1) {
["previous":"Exception":private]=>
NULL
}
[1]=>
string(4) "test"
}
array(1) {
array(2) {
[0]=>
object(Exception)#%d (6) {
["message":protected]=>
Expand All @@ -39,4 +41,6 @@ array(1) {
["previous":"Exception":private]=>
NULL
}
[1]=>
string(4) "test"
}
65 changes: 65 additions & 0 deletions ext/standard/tests/serialize/bug72601.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
--TEST--
__wakeup should be able to add dynamic properties without affecting other dynamic properties
--FILE--
<?php
error_reporting(E_ALL|E_STRICT);

class Obj {
// Testing $this->a being a dynamic property.

function __construct($a) {
$this->a = $a;
}

public function __wakeup() {
echo "Calling __wakeup\n";
for ($i = 0; $i < 10000; $i++) {
$this->{'b' . $i} = 42;
}
}
}

function main() {
$obj = new stdClass();
$obj->test = 'foo';
$variable = [new Obj($obj), new Obj($obj)];
$serialized = serialize($variable);
printf("%s\n", $serialized);
$unserialized = unserialize($serialized);
for ($i = 0; $i < 10000; $i++) {
if ($unserialized[0]->{'b' . $i} !== 42) {
echo "Fail 0 b$i\n";
return;
}
if ($unserialized[1]->{'b' . $i} !== 42) {
echo "Fail 1 b$i\n";
return;
}
unset($unserialized[0]->{'b' . $i});
unset($unserialized[1]->{'b' . $i});
}
var_dump($unserialized);
}
main();
--EXPECTF--
a:2:{i:0;O:3:"Obj":1:{s:1:"a";O:8:"stdClass":1:{s:4:"test";s:3:"foo";}}i:1;O:3:"Obj":1:{s:1:"a";r:3;}}
Calling __wakeup
Calling __wakeup
array(2) {
[0]=>
object(Obj)#%d (1) {
["a"]=>
object(stdClass)#%d (1) {
["test"]=>
string(3) "foo"
}
}
[1]=>
object(Obj)#%d (1) {
["a"]=>
object(stdClass)#%d (1) {
["test"]=>
string(3) "foo"
}
}
}
33 changes: 33 additions & 0 deletions ext/standard/tests/serialize/bug72601_2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
__wakeup can replace a copy of the object referring to the root node.
--SKIPIF--
--FILE--
<?php
/* This bug never happened, but adding this test to make sure that further changes to unserialize don't allow freeing the root in __wakeup. */
class Obj {
function __construct($a) {
$this->a = $a;
}

public function __wakeup() {
echo "Calling __wakeup\n";
$this->a = "replaced";
}
}

$a = new stdClass();
$a->obj = new Obj($a);;
$serialized = serialize($a);
printf("%s\n", $serialized);
$unserialized = unserialize($serialized);
var_dump($unserialized);
--EXPECTF--
O:8:"stdClass":1:{s:3:"obj";O:3:"Obj":1:{s:1:"a";r:1;}}
Calling __wakeup
object(stdClass)#%d (1) {
["obj"]=>
object(Obj)#%d (1) {
["a"]=>
string(8) "replaced"
}
}
50 changes: 50 additions & 0 deletions ext/standard/tests/serialize/bug72601_3.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
--TEST--
__wakeup should be able to modify dynamic properties without affecting copies of those properties
--FILE--
<?php
error_reporting(E_ALL|E_STRICT);

class Obj {
// Testing $this->a being a dynamic property.

function __construct($a) {
$this->a = $a;
}

public function __wakeup() {
echo "Calling __wakeup " . json_encode($this->a) . "\n";
$this->a = "roh";
}
}

function main() {
$obj = new stdClass();
$obj->c = null;
$variable = [new Obj($obj), new Obj($obj), $obj];
$serialized = serialize($variable);
printf("%s\n", $serialized);
$unserialized = unserialize($serialized);
var_dump($unserialized);
}
main();
--EXPECTF--
a:3:{i:0;O:3:"Obj":1:{s:1:"a";O:8:"stdClass":1:{s:1:"c";N;}}i:1;O:3:"Obj":1:{s:1:"a";r:3;}i:2;r:3;}
Calling __wakeup {"c":null}
Calling __wakeup {"c":null}
array(3) {
[0]=>
object(Obj)#%d (1) {
["a"]=>
string(3) "roh"
}
[1]=>
object(Obj)#%d (1) {
["a"]=>
string(3) "roh"
}
[2]=>
object(stdClass)#%d (1) {
["c"]=>
NULL
}
}

0 comments on commit 56fabb4

Please sign in to comment.