public void DisposedIsPassedOn()
 {
     var innerConnection = new TestConnection();
     var outerConnection = new EncryptingConnection(innerConnection);
     outerConnection.Dispose();
     Assert.IsTrue(innerConnection.Disposed);
 }
 /// <summary>
 /// Builds a server connection based on the start-up options.
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="settings"></param>
 /// <returns></returns>
 public static IServerConnection BuildUpConnection(IServerConnection connection, ClientStartUpSettings settings)
 {
     if (settings.UseEncryption)
     {
         connection = new EncryptingConnection(connection);
     }
     return(connection);
 }
        public void SendMessageEncryptsMessage()
        {
            var innerConnection = new TestConnection();
            var outerConnection = new EncryptingConnection(innerConnection);
            var request = new ServerRequest();
            var expectedResponse = new Response(request);
            var actionName = "DoSomething";
            string iv = null;
            string key = null;

            innerConnection.SendMessageAction = (a, r) =>
            {
                Response sendResponse = null;
                if (a == "RetrievePublicKey")
                {
                    sendResponse = this.GenerateKeyResponse(r);
                }
                else if (a == "InitialiseSecureConnection")
                {
                    Assert.IsInstanceOf<LoginRequest>(r);
                    sendResponse = this.GenerateConnectioResponse(r as LoginRequest, out iv, out key);
                }
                else if (a == "ProcessSecureRequest")
                {
                    Assert.IsInstanceOf<EncryptedRequest>(r);
                    var actualRequest = r as EncryptedRequest;
                    Assert.AreEqual(actionName, actualRequest.Action);

                    var crypto = new RijndaelManaged();
                    crypto.Key = Convert.FromBase64String(key);
                    crypto.IV = Convert.FromBase64String(iv);
                    var requestData = DecryptMessage(crypto, actualRequest.EncryptedData);
                    Assert.AreEqual(request.ToString(), requestData);

                    var encryptedResponse = new EncryptedResponse();
                    encryptedResponse.Result = ResponseResult.Success;
                    encryptedResponse.EncryptedData = EncryptMessage(crypto, expectedResponse.ToString());
                    sendResponse = encryptedResponse;
                }
                else
                {
                    Assert.Fail("Unknown action: " + a);
                }

                return sendResponse;
            };
            var response = outerConnection.SendMessage(actionName, request);
            Assert.IsNotNull(response);
            Assert.AreEqual(expectedResponse.RequestIdentifier, response.RequestIdentifier);
        }
 public void PropertiesAreRetrievedFromInnerConnection()
 {
     var innerConnection = new TestConnection
     {
         Type = "innerTest",
         ServerName = "theServerName",
         Address = "http://lostintranslation",
         IsBusy = false
     };
     var outerConnection = new EncryptingConnection(innerConnection);
     Assert.AreEqual(innerConnection.Type, outerConnection.Type);
     Assert.AreEqual(innerConnection.ServerName, outerConnection.ServerName);
     Assert.AreEqual(innerConnection.Address, outerConnection.Address);
     Assert.AreEqual(innerConnection.IsBusy, outerConnection.IsBusy);
     innerConnection.IsBusy = true;
     Assert.AreEqual(innerConnection.IsBusy, outerConnection.IsBusy);
 }
 public void ConstructorWiresUpEvents()
 {
     var innerConnection = new TestConnection();
     var outerConnection = new EncryptingConnection(innerConnection);
     var sendMessageFired = false;
     var requestSendingFired = false;
     var responseReceivedFired = false;
     outerConnection.SendMessageCompleted += (o, e) => { sendMessageFired = true; };
     outerConnection.RequestSending += (o, e) => { requestSendingFired = true; };
     outerConnection.ResponseReceived += (o, e) => { responseReceivedFired = true; };
     innerConnection.TriggerSendMessageCompleted(null, null, false, null);
     innerConnection.TriggerRequestSending(null, null);
     innerConnection.TriggerResponseReceived(null, null);
     Assert.IsTrue(sendMessageFired);
     Assert.IsTrue(requestSendingFired);
     Assert.IsTrue(responseReceivedFired);
 }
 /// <summary>
 /// Builds a server connection based on the start-up options.
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="settings"></param>
 /// <returns></returns>
 public static IServerConnection BuildUpConnection(IServerConnection connection, ClientStartUpSettings settings)
 {
     if (settings.UseEncryption) connection = new EncryptingConnection(connection);
     return connection;
 }