Exemple #1
0
        public void Serialization()
        {
            var dict = new Dictionary <Type, ushort> {
                { typeof(string), 0 }, { typeof(int), 1 }
            };
            TypeMap map = new TypeMap(dict);

            byte[] buffer = new byte[20480];
            var    writer = new BufferValueWriter(buffer);

            map.Serialize(null, writer);
            writer.Flush();

            var reader = new BufferValueReader(buffer);

            map = new TypeMap();
            map.Deserialize(null, reader);

            Type type;

            Assert.IsTrue(map.TryGetType(0, out type));
            Assert.AreEqual(typeof(string), type);
            Assert.IsTrue(map.TryGetType(1, out type));
            Assert.AreEqual(typeof(int), type);

            ushort id;

            Assert.IsFalse(map.GetTypeId(typeof(string), out id));
            Assert.AreEqual(0, id);
            Assert.IsFalse(map.GetTypeId(typeof(int), out id));
            Assert.AreEqual(1, id);
        }
        public void ISerializable()
        {
            byte[] buffer = new byte[20480];
            var    writer = new BufferValueWriter(buffer);

            SerializableTester tester = new SerializableTester
            {
                Name    = "MONKEY!",
                Numbers = new[] { 1, 2, 4, 8, 16, 32 }
            };

            var test = new AsyncTest();

            tester.SerializeCalled += test.PassHandler;

            writer.Write(context, tester);
            writer.Flush();

            var reader     = new BufferValueReader(buffer);
            var serialized = SerializerExtensions.Read <SerializableTester> (reader, context);

            Assert.IsNotNull(serialized);
            Assert.AreEqual(tester.Name, serialized.Name);
            Assert.IsTrue(tester.Numbers.SequenceEqual(serialized.Numbers), "Numbers does not match");

            test.Assert(1000);
        }
Exemple #3
0
        public void Serialize()
        {
            var crypto = new RSACryptoServiceProvider();

            var p = new RSAAsymmetricKey(crypto.ExportCspBlob(true));

            byte[] buffer = new byte[20480];
            var    writer = new BufferValueWriter(buffer);

            p.Serialize(null, writer);
            int len = writer.Length;

            writer.Flush();

            var reader = new BufferValueReader(buffer);
            var key    = new RSAAsymmetricKey(null, reader);

            Assert.AreEqual(len, reader.Position);

            Assert.IsNull(key.D);
            Assert.IsNull(key.DP);
            Assert.IsNull(key.DQ);
            AssertArrayMatches(p.Exponent, key.Exponent);
            Assert.IsNull(key.InverseQ);
            AssertArrayMatches(p.Modulus, key.Modulus);
            Assert.IsNull(key.P);
            Assert.IsNull(key.Q);

            Assert.IsNotNull(key.PublicSignature);
        }
        public void Enum()
        {
            byte[] buffer = new byte[1024];
            var writer = new BufferValueWriter (buffer);
            writer.Write (context, TestEnum.High);
            int len = writer.Length;
            writer.Flush();

            var reader = new BufferValueReader (buffer);

            Assert.AreEqual (TestEnum.High, reader.Read<TestEnum> (context));
            Assert.AreEqual (len, reader.Position);
        }
        public void ReadWriteLongSet()
        {
            var writer = new BufferValueWriter (new byte[1]);

            for (int i = 0; i < 20480; ++i)
                writer.WriteInt32(i);

            writer.Flush();

            var reader = new BufferValueReader (writer.Buffer);
            for (int i = 0; i < 20480; ++i)
                Assert.AreEqual(i, reader.ReadInt32());
        }
Exemple #6
0
        public void ReadWriteUniversalDate()
        {
            byte[] buffer = new byte[20480];
            var    writer = new BufferValueWriter(buffer);

            DateTime d = DateTime.Now;

            writer.WriteUniversalDate(d);
            writer.Flush();

            var reader = new BufferValueReader(buffer);

            Assert.AreEqual(d.ToUniversalTime(), reader.ReadUniversalDate());
        }
Exemple #7
0
        public void ReadWriteUniversalDate()
        {
            byte[] buffer = new byte[20480];
            var writer = new BufferValueWriter (buffer);

            DateTime d = DateTime.Now;

            writer.WriteUniversalDate (d);
            writer.Flush();

            var reader = new BufferValueReader (buffer);

            Assert.AreEqual (d.ToUniversalTime(), reader.ReadUniversalDate());
        }
        public void Enum()
        {
            byte[] buffer = new byte[1024];
            var    writer = new BufferValueWriter(buffer);

            writer.Write(context, TestEnum.High);
            int len = writer.Length;

            writer.Flush();

            var reader = new BufferValueReader(buffer);

            Assert.AreEqual(TestEnum.High, reader.Read <TestEnum> (context));
            Assert.AreEqual(len, reader.Position);
        }
Exemple #9
0
        public void ReadWrite7BitInt()
        {
            var writer = new BufferValueWriter (new byte[20480]);

            writer.Write7BitEncodedInt (Int32.MinValue);
            writer.Write7BitEncodedInt (0);
            writer.Write7BitEncodedInt (Int32.MaxValue);
            writer.Flush();

            var reader = new BufferValueReader (writer.Buffer);

            Assert.AreEqual (Int32.MinValue, reader.Read7BitEncodedInt());
            Assert.AreEqual (0, reader.Read7BitEncodedInt());
            Assert.AreEqual (Int32.MaxValue, reader.Read7BitEncodedInt());
        }
Exemple #10
0
        public void ReadWrite7BitInt()
        {
            var writer = new BufferValueWriter(new byte[20480]);

            writer.Write7BitEncodedInt(Int32.MinValue);
            writer.Write7BitEncodedInt(0);
            writer.Write7BitEncodedInt(Int32.MaxValue);
            writer.Flush();

            var reader = new BufferValueReader(writer.Buffer);

            Assert.AreEqual(Int32.MinValue, reader.Read7BitEncodedInt());
            Assert.AreEqual(0, reader.Read7BitEncodedInt());
            Assert.AreEqual(Int32.MaxValue, reader.Read7BitEncodedInt());
        }
Exemple #11
0
        public void Serializer()
        {
            byte[] buffer = new byte[1024];
            var    writer = new BufferValueWriter(buffer);

            var c = SerializationContextTests.GetContext(MockProtocol.Instance);
            var p = new Protocol(42, 248);

            p.Serialize(c, writer);
            writer.Flush();

            var reader = new BufferValueReader(buffer);
            var p2     = new Protocol(c, reader);

            Assert.AreEqual(p.id, p2.id);
            Assert.AreEqual(p.Version, p2.Version);
        }
Exemple #12
0
        public void ReadWriteLongSet()
        {
            var writer = new BufferValueWriter(new byte[1]);

            for (int i = 0; i < 20480; ++i)
            {
                writer.WriteInt32(i);
            }

            writer.Flush();

            var reader = new BufferValueReader(writer.Buffer);

            for (int i = 0; i < 20480; ++i)
            {
                Assert.AreEqual(i, reader.ReadInt32());
            }
        }
        // [Test] Testing core APIs that now have NETFX_CORE turned on everywhere
        public void Serializable()
        {
            var inner = new Exception("Inner exception");
            var ex    = new InvalidOperationException("Don't do this, fool.", inner);

            byte[] buffer = new byte[20480];
            var    writer = new BufferValueWriter(buffer);

            writer.Write(context, ex);
            writer.Flush();

            var reader = new BufferValueReader(buffer);

            InvalidOperationException ioex = SerializerExtensions.Read <InvalidOperationException> (reader, context);

            Assert.IsNotNull(ioex);
            Assert.AreEqual(ex.Message, ioex.Message);
            Assert.AreEqual(ex.StackTrace, ioex.StackTrace);

            Assert.IsNotNull(ioex.InnerException);
            Assert.AreEqual(inner.Message, ioex.InnerException.Message);
            Assert.AreEqual(inner.StackTrace, ioex.InnerException.StackTrace);
        }
Exemple #14
0
        public void Serializer()
        {
            byte[] buffer = new byte[1024];
            var writer = new BufferValueWriter (buffer);

            var c = SerializationContextTests.GetContext (MockProtocol.Instance);
            var p = new Protocol (42, 248);
            p.Serialize (c, writer);
            writer.Flush();

            var reader = new BufferValueReader (buffer);
            var p2 = new Protocol (c, reader);

            Assert.AreEqual (p.id, p2.id);
            Assert.AreEqual (p.Version, p2.Version);
        }
        public void ValueReaderCtor()
        {
            byte[] buffer = new byte[20480];
            var writer = new BufferValueWriter (buffer);

            ValueReaderTester test = new ValueReaderTester ("TheName");

            writer.Write (context, test);
            writer.Flush();

            var reader = new BufferValueReader (buffer);
            var serialized = SerializerExtensions.Read<ValueReaderTester> (reader, context);

            Assert.AreEqual (test.Name, serialized.Name);
        }
        public void SerializableTypeContainingNonSerializableField()
        {
            byte[] buffer = new byte[20480];
            var writer = new BufferValueWriter (buffer);

            ObjectSerializer serializer = ObjectSerializer.GetSerializer (typeof (SerializableWithNonSerializableField));

            var test = new SerializableWithNonSerializableField (2, 3, "hi");

            serializer.Serialize (context, writer, test);
            writer.Flush();

            var reader = new BufferValueReader (buffer);
            var serialized = (SerializableWithNonSerializableField)serializer.Deserialize (context, reader);

            Assert.AreEqual (2, serialized.X);
            Assert.AreEqual (3, serialized.Y);
            Assert.AreEqual ("hi", serialized.Content);
        }
Exemple #17
0
        public void Serialization()
        {
            var dict = new Dictionary<Type, ushort> { { typeof (string), 0 }, { typeof (int), 1 } };
            TypeMap map = new TypeMap (dict);

            byte[] buffer = new byte[20480];
            var writer = new BufferValueWriter (buffer);
            map.Serialize (null, writer);
            writer.Flush();

            var reader = new BufferValueReader (buffer);
            map = new TypeMap();
            map.Deserialize (null, reader);

            Type type;
            Assert.IsTrue (map.TryGetType (0, out type));
            Assert.AreEqual (typeof (string), type);
            Assert.IsTrue (map.TryGetType (1, out type));
            Assert.AreEqual (typeof (int), type);

            ushort id;
            Assert.IsFalse (map.GetTypeId (typeof (string), out id));
            Assert.AreEqual (0, id);
            Assert.IsFalse (map.GetTypeId (typeof (int), out id));
            Assert.AreEqual (1, id);
        }
        // [Test] Testing core APIs that now have NETFX_CORE turned on everywhere
        public void Serializable()
        {
            var inner = new Exception ("Inner exception");
            var ex = new InvalidOperationException ("Don't do this, fool.", inner);

            byte[] buffer = new byte[20480];
            var writer = new BufferValueWriter (buffer);

            writer.Write (context, ex);
            writer.Flush();

            var reader = new BufferValueReader (buffer);

            InvalidOperationException ioex = SerializerExtensions.Read<InvalidOperationException> (reader, context);

            Assert.IsNotNull (ioex);
            Assert.AreEqual (ex.Message, ioex.Message);
            Assert.AreEqual (ex.StackTrace, ioex.StackTrace);

            Assert.IsNotNull (ioex.InnerException);
            Assert.AreEqual (inner.Message, ioex.InnerException.Message);
            Assert.AreEqual (inner.StackTrace, ioex.InnerException.StackTrace);
        }
        public void Contracted()
        {
            byte[] buffer = new byte[20480];
            var writer = new BufferValueWriter (buffer);

            ISerializableTester test = new SerializableTester
            {
                Name = "MONKEY!",
                Numbers = new[] { 1, 2, 4, 8, 16, 32 }
            };

            writer.Write (context, test);
            writer.Flush();

            var reader = new BufferValueReader (buffer);
            var serialized = SerializerExtensions.Read<ISerializableTester> (reader, context);

            Assert.IsNotNull (serialized);
            Assert.AreEqual (test.Name, serialized.Name);
            Assert.IsTrue (test.Numbers.SequenceEqual (serialized.Numbers), "Numbers does not match");
        }
        public void ISerializable()
        {
            byte[] buffer = new byte[20480];
            var writer = new BufferValueWriter (buffer);

            SerializableTester tester = new SerializableTester
            {
                Name = "MONKEY!",
                Numbers = new[] { 1, 2, 4, 8, 16, 32 }
            };

            var test = new AsyncTest();
            tester.SerializeCalled += test.PassHandler;

            writer.Write (context, tester);
            writer.Flush();

            var reader = new BufferValueReader (buffer);
            var serialized = SerializerExtensions.Read<SerializableTester> (reader, context);

            Assert.IsNotNull (serialized);
            Assert.AreEqual (tester.Name, serialized.Name);
            Assert.IsTrue (tester.Numbers.SequenceEqual (serialized.Numbers), "Numbers does not match");

            test.Assert (1000);
        }
        public void PrimitiveAsObject()
        {
            byte[] buffer = new byte[1024];
            var writer = new BufferValueWriter (buffer);

            writer.Write (context, (object)20f);
            writer.Flush();

            var reader = new BufferValueReader (buffer);
            object value = SerializerExtensions.Read<object> (reader, context);

            Assert.IsNotNull (value);
            Assert.AreEqual (20f, value);
        }