private static void RunArrayTest(Type serializerType)
        {
            ISerializerAdapter serializer = (ISerializerAdapter)Activator.CreateInstance(serializerType);

            TestCustomObject[] messages = new TestCustomObject[] { new TestCustomObject {
                                                                       Value = 10
                                                                   } };
            Type messagesType = typeof(TestCustomObject[]);

            using (MemoryStream ms = new MemoryStream())
            {
                object output = null;
                bool   ex     = false;
                try
                {
                    serializer.Serialize(new IndisposableStream(ms), messages);

                    ms.Flush();
                    ms.Seek(0, SeekOrigin.Begin);

                    output = serializer.Deserialize(new IndisposableStream(ms), messagesType);
                }
                catch (Exception x)
                {
                    Assert.Inconclusive("The the serializer has at least thrown an exception instead of unexpected results {0}", x);
                    ex = true;
                }
                if (!ex)
                {
                    Assert.IsInstanceOfType(messagesType, output);
                    TestCustomObject[] deserialized = output as TestCustomObject[];
                    Assert.AreEqual(messages.Single().Value.ToString(), deserialized.Single().Value.ToString());
                }
            }
        }
        private static void RunTest(Type serializerType, Type messageType)
        {
            ISerializerAdapter serializer = (ISerializerAdapter)Activator.CreateInstance(serializerType);
            IAssertEquality    message    = (IAssertEquality)messageType.GetMethod("CreateInstance",
                                                                                   BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy).Invoke(null, null);

            using (MemoryStream ms = new MemoryStream())
            {
                object output = null;
                bool   ex     = false;
                try
                {
                    serializer.Serialize(new IndisposableStream(ms), message);
                    ms.Flush();
                    ms.Seek(0, SeekOrigin.Begin);
                    output = serializer.Deserialize(new IndisposableStream(ms), messageType);
                }
                catch (Exception x)
                {
                    Assert.Inconclusive("The the serializer has at least thrown an exception instead of unexpected results {0}", x);
                    ex = true;
                }
                if (!ex)
                {
                    message.AssertEquality(output);
                }
            }
        }
        private static void RunObjectTreeTest(Type serializerType, Type messageType)
        {
            ISerializerAdapter serializer = (ISerializerAdapter)Activator.CreateInstance(serializerType);

            object message = catalog.CreateInstance(messageType);

            using (MemoryStream ms = new MemoryStream())
            {
                object output = null;
                bool   ex     = false;
                try
                {
                    serializer.Serialize(new IndisposableStream(ms), message);

                    ms.Flush();
                    ms.Seek(0, SeekOrigin.Begin);

                    output = serializer.Deserialize(new IndisposableStream(ms), messageType);
                }
                catch (Exception x)
                {
                    Assert.Inconclusive("The the serializer has at least thrown an exception instead of unexpected results {0}", x);
                    ex = true;
                }
                if (!ex)
                {
                    ObjectDataTree messageData = new ObjectDataTree(message);
                    ObjectDataTree outputData  = new ObjectDataTree(output);

                    Assert.AreEqual(messageData.StringValue(), outputData.StringValue());
                    (message as IAssertEquality).AssertEquality(output);
                }
            }
        }
 protected override object Deserialize(ISerializerAdapter serializer, Stream stream, out long operationTime)
 {
     try
     {
         return(serializer.Deserialize <T>(stream, out operationTime));
     }
     finally { stream.Seek(0, SeekOrigin.Begin); }
 }
        /// <summary>
        /// Gets decrypted data from a wire message.
        /// </summary>
        /// <param name="message">Wire message</param>
        /// <param name="serializer">Serializer used to deserialized the signed content</param>
        /// <param name="sharedSecret">Shared secret (null, if the wire message is not encrypted)</param>
        /// <param name="sendersPublicKeyBlob">Public key of the sender used for RSA signature verification</param>
        /// <param name="sendersPublicKeySize">Sender's public key size</param>
        /// <returns>Decrypted raw data</returns>
        public byte[] GetDecryptedMessageData(
            WireMessage message,
            ISerializerAdapter serializer,
            byte[] sharedSecret         = null,
            byte[] sendersPublicKeyBlob = null,
            int sendersPublicKeySize    = 0)
        {
            if (message.Iv.Length > 0 && sharedSecret != null)
            {
                var decryptedRawData =
                    AesEncryption.Decrypt(
                        encryptedData: message.Data,
                        sharedSecret: sharedSecret,
                        iv: message.Iv);

                var signedMessageData = serializer.Deserialize <SignedMessageData>(decryptedRawData);

                if (sendersPublicKeyBlob != null && signedMessageData.Signature != null)
                {
                    if (RsaSignature.VerifySignature(
                            keySize: sendersPublicKeySize,
                            sendersPublicKeyBlob: sendersPublicKeyBlob,
                            rawData: signedMessageData.MessageRawData,
                            signature: signedMessageData.Signature))
                    {
                        return(signedMessageData.MessageRawData);
                    }
                    else
                    {
                        throw new SecurityException("Verification of message signature failed.");
                    }
                }
                else
                {
                    return(decryptedRawData);
                }
            }
            else
            {
                return(message.Data);
            }
        }
Beispiel #6
0
        public static RunResult TestRun <T>(ISerializerAdapter serializer, T instance)
            where T : IAssertEquality
        {
            RunResult result = new RunResult();

            var w = Stopwatch.StartNew();

            byte[] data;
            T      output = default(T);

            try
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    var serW = Stopwatch.StartNew();
                    serializer.Serialize(new IndisposableStream(ms), instance);
                    result.Serialization = serW.Elapsed;
                    data = ms.ToArray();
                }


                using (MemoryStream ms = new MemoryStream(data))
                {
                    ms.Seek(0, SeekOrigin.Begin);
                    var deserW = Stopwatch.StartNew();
                    output = (T)serializer.Deserialize(new IndisposableStream(ms), typeof(T));
                    result.Deserialization = deserW.Elapsed;
                }
                result.TotalTime = w.Elapsed;
                instance.AssertEquality(output);
            }
            catch (Exception x)
            {
                Console.WriteLine(x.Message);
                result.Serialization   = TimeSpan.FromSeconds(0);
                result.Deserialization = TimeSpan.FromSeconds(0);
                result.TotalTime       = TimeSpan.FromSeconds(0);
            }

            return(result);
        }