Пример #1
0
        public void ReadObject_Objects_Externalizable_AMF3()
        {
            IExternalizable externalizableValue = Mocks.CreateMock <IExternalizable>();

            externalizableValue.ReadExternal(input);
            LastCall.Do((ReadExternalDelegate) delegate(IDataInput inputToUse)
            {
                // Note: inputToUse will be the same instance of AMFDataInput which we've already
                // tested so we don't need to try all combinations here.  Just a few as a sanity check.
                Assert.AreEqual("abc", inputToUse.ReadUTF());
                Assert.AreEqual(10, inputToUse.ReadInt());
                Assert.AreEqual(new ASString("def"), inputToUse.ReadObject());
            });

            Expect.Call(serializer.CreateExternalizableInstance("class")).Return(externalizableValue);

            Mocks.ReplayAll();

            SetStreamContents(new byte[] { (byte)AMF0ObjectTypeCode.AMF3Data, (byte)AMF3ObjectTypeCode.Object, 0x07,
                                           0x0b, 0x63, 0x6c, 0x61, 0x73, 0x73,                     // class def
                                           0x00, 0x03, 0x61, 0x62, 0x63,                           // write utf "abc"
                                           0x00, 0x00, 0x00, 0x0a,                                 // write int 10
                                           (byte)AMF3ObjectTypeCode.String, 0x07, 0x64, 0x65, 0x66 // write object "def"
                              });

            input.BeginObjectStream();
            ASExternalizableObject obj = (ASExternalizableObject)input.ReadObject();

            Assert.AreEqual(AMFObjectEncoding.AMF3, input.ObjectEncoding);
            input.EndObjectStream();

            Assert.AreEqual("class", obj.Class.ClassAlias);
            Assert.AreEqual(ASClassLayout.Externalizable, obj.Class.Layout);
            Assert.AreSame(externalizableValue, obj.ExternalizableValue);
        }
Пример #2
0
        public void WriteObject_Objects_Externalizable_AMF3()
        {
            IExternalizable externalizableValue = Mocks.CreateMock <IExternalizable>();

            externalizableValue.WriteExternal(output);
            LastCall.Do((WriteExternalDelegate) delegate(IDataOutput outputToUse)
            {
                // Note: outputToUse will be the same instance as output which we've already
                // tested so we don't need to try all combinations here.  Just a few as a sanity check.
                outputToUse.WriteUTF("abc");
                outputToUse.WriteInt(10);
                outputToUse.WriteObject(new ASString("def"));
            });

            ASClass @class             = new ASClass("class", ASClassLayout.Externalizable, EmptyArray <string> .Instance);
            ASExternalizableObject obj = new ASExternalizableObject(@class, externalizableValue);

            Mocks.ReplayAll();

            output.ObjectEncoding = AMFObjectEncoding.AMF3;
            output.BeginObjectStream();
            output.WriteObject(obj);
            output.EndObjectStream();

            byte[] expected = new byte[] { (byte)AMF0ObjectTypeCode.AMF3Data, (byte)AMF3ObjectTypeCode.Object, 0x07,
                                           0x0b, 0x63, 0x6c, 0x61, 0x73, 0x73,                     // class def
                                           0x00, 0x03, 0x61, 0x62, 0x63,                           // write utf "abc"
                                           0x00, 0x00, 0x00, 0x0a,                                 // write int 10
                                           (byte)AMF3ObjectTypeCode.String, 0x07, 0x64, 0x65, 0x66 // write object "def"
            };

            CollectionAssert.AreElementsEqual(expected, stream.ToArray());
        }
Пример #3
0
        public void ConstructorSetsProperties()
        {
            ASExternalizableObject obj = new ASExternalizableObject(externalizableClass, externalizable);

            Assert.AreSame(externalizableClass, obj.Class);
            Assert.AreSame(externalizable, obj.ExternalizableValue);
        }
Пример #4
0
        public void SetPropertiesWithInitializedInstanceThrows()
        {
            IExternalizable externalizableValue = Mocks.CreateMock <IExternalizable>();

            Mocks.ReplayAll();

            ASExternalizableObject obj = new ASExternalizableObject(externalizableClass, externalizableValue);

            obj.SetProperties(externalizableValue);
        }
Пример #5
0
        public void CreateUninitializedInstanceAndSetProperties()
        {
            IExternalizable externalizableValue = Mocks.CreateMock <IExternalizable>();

            Mocks.ReplayAll();

            ASExternalizableObject obj = ASExternalizableObject.CreateUninitializedInstance(externalizableClass);

            Assert.AreSame(externalizableClass, obj.Class);

            obj.SetProperties(externalizableValue);
            Assert.AreSame(externalizableValue, obj.ExternalizableValue);
        }
Пример #6
0
        private IASValue ReadObjectValue()
        {
            int bits = input.ReadVWInt29();

            // Handle cached objects.
            if ((bits & 1) == 0)
            {
                int referenceId = bits >> 1;
                return(GetObjectFromCache(AMF3ObjectTypeCode.Object, referenceId));
            }

            // Handle cached class definitions.
            ASClass classDefinition;

            if ((bits & 2) == 0)
            {
                int referenceId = bits >> 2;

                // Note: Object might be an ASExternalizableObject.
                IASValue obj = GetObjectFromCache(AMF3ObjectTypeCode.Object, referenceId);
                classDefinition = obj.Class;
            }
            else
            {
                // Read the class definition.
                ASClassLayout classLayout = (ASClassLayout)((bits & 0x0c) >> 2);
                if (classLayout > ASClassLayout.Dynamic)
                {
                    throw new AMFException(String.Format(CultureInfo.CurrentCulture,
                                                         ExceptionPrefix + "Encountered Object token with invalid class layout '{0}'.", classLayout));
                }

                int memberCount = bits >> 4;
                if (memberCount < 0)
                {
                    throw new AMFException(String.Format(CultureInfo.CurrentCulture,
                                                         ExceptionPrefix + "Encountered Object token with invalid member count '{0}'.", memberCount));
                }

                if (classLayout == ASClassLayout.Externalizable && memberCount != 0)
                {
                    throw new AMFException(String.Format(CultureInfo.CurrentCulture,
                                                         ExceptionPrefix + "Encountered Object token with Externalizable class layout and non-zero member count '{0}'.", memberCount));
                }

                string classAlias = ReadStringData();

                string[] memberNames;
                if (memberCount != 0)
                {
                    memberNames = new string[memberCount];

                    for (int i = 0; i < memberCount; i++)
                    {
                        memberNames[i] = ReadStringData();
                    }
                }
                else
                {
                    memberNames = EmptyArray <string> .Instance;
                }

                // Look up the class in the cache.
                classDefinition = ASClassCache.GetClass(classAlias, classLayout, memberNames);
            }

            // Read the instance data.
            if (classDefinition.Layout == ASClassLayout.Externalizable)
            {
                // Important: Add the object to the cache before deserializing its properties!
                ASExternalizableObject result = ASExternalizableObject.CreateUninitializedInstance(classDefinition);
                AddObjectToCache(AMF3ObjectTypeCode.Object, result);

                // Use custom serialization for the externalizable object.
                IExternalizable externalizableValue = input.Serializer.CreateExternalizableInstance(classDefinition.ClassAlias);
                externalizableValue.ReadExternal(input);

                result.SetProperties(externalizableValue);
                return(result);
            }
            else
            {
                // Important: Add the object to the cache before deserializing its properties!
                ASObject result = ASObject.CreateUninitializedInstance(classDefinition);
                AddObjectToCache(AMF3ObjectTypeCode.Object, result);

                // Read the member values.
                int        memberCount = classDefinition.MemberNames.Count;
                IASValue[] memberValues;

                if (memberCount != 0)
                {
                    memberValues = new IASValue[memberCount];

                    for (int i = 0; i < memberCount; i++)
                    {
                        memberValues[i] = ReadObject();
                    }
                }
                else
                {
                    memberValues = EmptyArray <IASValue> .Instance;
                }

                // Read the dynamic property values.
                IDictionary <string, IASValue> dynamicProperties;

                if (classDefinition.Layout == ASClassLayout.Dynamic)
                {
                    string key = ReadStringData();
                    if (key.Length != 0)
                    {
                        dynamicProperties = new Dictionary <string, IASValue>();

                        for (; ;)
                        {
                            IASValue value = ReadObject();
                            dynamicProperties.Add(key, value);

                            key = ReadStringData();
                            if (key.Length == 0)
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        dynamicProperties = EmptyDictionary <string, IASValue> .Instance;
                    }
                }
                else
                {
                    dynamicProperties = EmptyDictionary <string, IASValue> .Instance;
                }

                result.SetProperties(memberValues, dynamicProperties);
                return(result);
            }
        }