-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests of __wakeup in serialization working as expected
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
1 parent
b8d26b3
commit 56fabb4
Showing
4 changed files
with
154 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |