Permalink
Browse files

improved reference deserialization

  • Loading branch information...
1 parent d2ef072 commit 8a2e7009c84419d71037febaa39d453b5e571515 @arielsom arielsom committed Jan 9, 2012
View
@@ -5,7 +5,7 @@ nbproject/
#Flex Unit temp files
###################
AmfphpFlexUnit/bin-debug/
-AmfphpFlexUnit/src/
+AmfphpFlexUnit/src/FlexUnitApplication.mxml
AmfphpFlexUnit/.FlexUnitSettings/
@@ -63,9 +63,6 @@ public function __construct($raw) {
$this->rawData = $raw;
$this->currentByte = 0;
$this->content_length = strlen($this->rawData); // grab the total length of this stream
- $this->storedStrings = array();
- $this->storedObjects = array();
- $this->storedDefinitions = array();
}
/**
@@ -79,6 +76,14 @@ public function deserialize() {
$this->readMessages(); // read the binary Messages
return $this->deserializedPacket;
}
+
+ protected function resetReferences(){
+ $this->amf0storedObjects = array();
+ $this->storedStrings = array();
+ $this->storedObjects = array();
+ $this->storedDefinitions = array();
+
+ }
/**
* readHeaders converts that header section of the amf Packet into php obects.
@@ -96,6 +101,7 @@ protected function readHeaders() {
$this->headersLeftToProcess = $this->readInt(); // find the total number of header elements
while ($this->headersLeftToProcess--) { // loop over all of the header elements
+ $this->resetReferences();
$name = $this->readUTF();
$required = $this->readByte() == 1; // find the must understand flag
//$length = $this->readLong(); // grab the length of the header element
@@ -112,11 +118,7 @@ protected function readHeaders() {
protected function readMessages() {
$this->messagesLeftToProcess = $this->readInt(); // find the total number of Message elements
while ($this->messagesLeftToProcess--) { // loop over all of the Message elements
- $this->amf0storedObjects = array();
- $this->storedStrings = array();
- $this->storedObjects = array();
- $this->storedDefinitions = array();
-
+ $this->resetReferences();
$target = $this->readUTF();
$response = $this->readUTF(); // the response that the client understands
//$length = $this->readLong(); // grab the length of the Message element
@@ -509,7 +511,7 @@ protected function readAmf3Xml() {
$handle = $handle >> 1;
if ($inline) {
$xml = $this->readBuffer($handle);
- $this->storedObjects[] = $xml;
+ $this->storedObjects[] = & $xml;
} else {
$xml = $this->storedObjects[$handle];
}
@@ -526,7 +528,7 @@ protected function readAmf3XmlDocument() {
$handle = $handle >> 1;
if ($inline) {
$xml = $this->readBuffer($handle);
- $this->storedObjects[] = $xml;
+ $this->storedObjects[] = & $xml;
} else {
$xml = $this->storedObjects[$handle];
}
@@ -539,7 +541,7 @@ protected function readAmf3ByteArray() {
$handle = $handle >> 1;
if ($inline) {
$ba = new Amfphp_Core_Amf_Types_ByteArray($this->readBuffer($handle));
- $this->storedObjects[] = $ba;
+ $this->storedObjects[] = & $ba;
} else {
$ba = $this->storedObjects[$handle];
}
@@ -10,7 +10,7 @@ package flexUnitTests
import org.amfphp.test.ObjEvent;
/**
- * adds on the tests that work for both amf0 and am3
+ * adds amf3 specific tests to the tests that work for both amf0 and am3
* */
public class Amf3Tests extends AmfCommonTests
{
@@ -139,19 +139,6 @@ package flexUnitTests
assertTrue(event.obj as Number == undefined);
}
-
- /**
- * send something with a reference ([["bla"], ["bla"]], really, but using a reference to a ["bla"] array)
- * @todo not really sure to provoke the writing of an array with references in php.
- * */
- public function testReference():void{
- _nc.addEventListener(EnhancedNetConnection.EVENT_ONRESULT, addAsync(verifyReturnReference, 1000));
- var refferedArray:Array = ["bla"];
- var testVar:Array = [refferedArray, refferedArray];
- _nc.callWithEvents("TestService/returnOneParam", testVar);
-
- }
-
private function verifyReturnReference(event:ObjEvent):void{
assertTrue(event.obj is Array);
assertTrue(event.obj[0] == "bla");
@@ -0,0 +1,62 @@
+package flexUnitTests
+{
+ import flash.net.ObjectEncoding;
+ import flash.utils.ByteArray;
+ import flash.xml.XMLDocument;
+
+ import flexunit.framework.TestCase;
+
+ import org.amfphp.test.EnhancedNetConnection;
+ import org.amfphp.test.ObjEvent;
+
+ /**
+ * adds amf3 specific tests to the tests that work for both amf0 and am3
+ * */
+ public class ReferenceAmf3Tests extends ReferenceCommonTests
+ {
+
+ [Before]
+ override public function setUp():void
+ {
+ super.setUp();
+ _nc.objectEncoding = ObjectEncoding.AMF3;
+
+ }
+
+ /**
+ * send a reference of byte arrays containing a boolean set to false
+ * flash somehow can't send this. what's wrong here?
+ * */
+ public function testByteArrayReference():void{
+ _nc.addEventListener(EnhancedNetConnection.EVENT_ONRESULT, addAsync(verifyReturnByteArray, 1000));
+ var testByteArray:ByteArray = new ByteArray();
+ testByteArray.writeBoolean(false);
+ _nc.callWithEvents("TestService/returnOneParam", new Array[testByteArray, testByteArray]);
+
+ }
+
+ private function verifyReturnByteArray(event:ObjEvent):void{
+ assertTrue(event.obj is ByteArray);
+ var retByteArray:ByteArray = event.obj as ByteArray;
+ assertEquals(false, retByteArray.readBoolean());
+ }
+
+ /**
+ * send an xml object (the as3 E4X object, or the as2 XML object)
+ * the as3 netconnection trips up on this when using AMF0...
+ * */
+ public function testXmlReference():void{
+ _nc.addEventListener(EnhancedNetConnection.EVENT_ONRESULT, addAsync(verifyReturnXml, 1000));
+ var testVar:XML = new XML("<root>bla</root>");
+ _nc.callWithEvents("TestService/returnOneParam", new Array[testVar, testVar]);
+
+ }
+
+ private function verifyReturnXml(event:ObjEvent):void{
+ assertTrue(event.obj is XML);
+ }
+
+
+
+ }
+}
@@ -0,0 +1,133 @@
+package flexUnitTests
+{
+ import flash.net.ObjectEncoding;
+ import flash.utils.ByteArray;
+ import flash.xml.XMLDocument;
+
+ import flexUnitTests.voTests.VoWithArrays;
+
+ import flexunit.framework.TestCase;
+
+ import org.amfphp.test.EnhancedNetConnection;
+ import org.amfphp.test.ObjEvent;
+
+ /**
+ * test sending stock messages in AMF0. for AM3, just override the setup method
+ * note: references are tricky, as they can only be indirectly observed
+ *
+ * note: in AMF0, it seems flash implementation is bugged. Sending [["bla"], ["bla"]](the second one being a reference to the first)
+ * results in a looped reference. The referred array is sent with reference '1' whereas it should be '2'
+ * In Charles this shows up as a circular reference, as it means the main array contains itself.
+ * This is because the parameters to the call is an array itself. IE what is being decoded is [ [["bla"], ["bla"]] ]
+ * Unoortnuately this means a lot of tests fail with AMF0.
+ * */
+ public class ReferenceCommonTests extends TestCase
+ {
+
+ protected var _nc:EnhancedNetConnection;
+
+ [Before]
+ override public function setUp():void
+ {
+
+ _nc = new EnhancedNetConnection();
+ _nc.objectEncoding = ObjectEncoding.AMF0;
+ _nc.connect(TestConfig.NC_GATEWAY_URL);
+
+ }
+
+ [After]
+ override public function tearDown():void
+ {
+ }
+
+
+
+ /**
+ * send something with a reference ([["bla"], ["bla"]], really, but using a reference to a ["bla"] array)
+ * @todo not really sure to provoke the writing of an array with references in php.
+ *
+ * */
+ public function testArrayReference():void{
+ _nc.addEventListener(EnhancedNetConnection.EVENT_ONRESULT, addAsync(verifyArrayReturnReference, 1000));
+ var refferedArray:Array = ["bla"];
+ var testVar:Array = [refferedArray, refferedArray];
+ _nc.callWithEvents("TestService/returnOneParam", testVar);
+
+ }
+
+ /**
+ * this only checks that the data is valid. Array references in the serailizer are disabled
+ * */
+ private function verifyArrayReturnReference(event:ObjEvent):void{
+ assertTrue(event.obj is Array);
+ assertEquals("bla", event.obj[0]);
+ assertEquals("bla", event.obj[1]);
+ }
+
+ public function testObjectReference():void{
+ _nc.addEventListener(EnhancedNetConnection.EVENT_ONRESULT, addAsync(verifyObjectReturnReference, 1000));
+ var reffered:* = {bla:"bla"};
+ var testVar:Array = [reffered, reffered];
+ _nc.callWithEvents("TestService/returnOneParam", testVar);
+
+ }
+
+ /**
+ * this only checks that the data is valid.
+ * */
+ private function verifyObjectReturnReference(event:ObjEvent):void{
+ assertTrue(event.obj is Array);
+ assertEquals("bla", event.obj[0].bla);
+ assertEquals("bla", event.obj[1].bla);
+ }
+
+
+ public function testTypedObjectReference():void{
+ _nc.addEventListener(EnhancedNetConnection.EVENT_ONRESULT, addAsync(verifyTypedObjectReturnReference, 1000));
+ VoWithArrays.register();
+ var reffered:* = new VoWithArrays();
+ var testVar:Array = [reffered, reffered];
+ _nc.callWithEvents("TestService/returnOneParam", testVar);
+
+ }
+
+ /**
+ * this only checks that the data is valid.
+ * */
+ private function verifyTypedObjectReturnReference(event:ObjEvent):void{
+ assertTrue(event.obj[0] is VoWithArrays);
+ assertTrue(event.obj[1] is VoWithArrays);
+
+ }
+
+ /**
+ * test xhat happens beyond MAX_STORE_SIZE(1024)
+ * */
+ public function testManyObjectsReference():void{
+ _nc.addEventListener(EnhancedNetConnection.EVENT_ONRESULT, addAsync(verifyManyObjectsReturnReference, 5000));
+ var testVar:Array = new Array();
+ for(var i:int = 0; i < 600;i++){
+ var reffered:VoWithArrays = new VoWithArrays();
+ reffered.test1_arr.push(i);
+ testVar.push(reffered);
+ testVar.push(reffered);
+ }
+ _nc.callWithEvents("TestService/returnOneParam", testVar);
+
+ }
+
+ /**
+ * this only checks that the data is valid.
+ * */
+ private function verifyManyObjectsReturnReference(event:ObjEvent):void{
+ assertTrue(event.obj is Array);
+ //test in space where references are made
+ assertEquals(300, event.obj[600].test1_arr[0]);
+ //test beyond
+ assertEquals(599, event.obj[1199].test1_arr[0]);
+ }
+
+
+ }
+}
@@ -7,7 +7,7 @@ package flexUnitTests.voTests {
public var test2_arr:Array = new Array();
static public function register():void {
- registerClassAlias("ItemVO", VoWithArrays);
+ registerClassAlias("VoWithArrays", VoWithArrays);
}
}

0 comments on commit 8a2e700

Please sign in to comment.