Beispiel #1
0
        /// <summary>
        /// Defines the compression mode
        /// for the transport
        /// </summary>
        /// <param name="compression">The compression mode</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="System.NotSupportedException"></exception>
        public virtual Task SetCompressionAsync(SessionCompression compression, CancellationToken cancellationToken)
        {
            if (compression != SessionCompression.None)
            {
                throw new NotSupportedException();
            }

            return(Task.FromResult <object>(null));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="TransportInformation"/> class.
 /// </summary>
 /// <param name="compression">The compression.</param>
 /// <param name="encryption">The encryption.</param>
 /// <param name="isConnected">if set to <c>true</c> [is connected].</param>
 /// <param name="localEndPoint">The local end point.</param>
 /// <param name="remoteEndPoint">The remote end point.</param>
 /// <param name="options">The options.</param>
 public TransportInformation(SessionCompression compression, SessionEncryption encryption, bool isConnected, EndPoint localEndPoint, EndPoint remoteEndPoint, IReadOnlyDictionary <string, object> options)
 {
     Compression    = compression;
     Encryption     = encryption;
     IsConnected    = isConnected;
     LocalEndPoint  = localEndPoint;
     RemoteEndPoint = remoteEndPoint;
     Options        = options;
 }
Beispiel #3
0
        public async Task NegotiateSessionAsync_InvalidStateValidOptions_ThrowsInvalidOperationException()
        {
            var target = GetTarget(SessionState.Negotiating);

            var compressionOptions = new SessionCompression[] { SessionCompression.None };
            var encryptionOptions  = new SessionEncryption[] { SessionEncryption.None, SessionEncryption.TLS };

            var cancellationToken = DataUtil.CreateCancellationToken();

            var actual = await target.NegotiateSessionAsync(compressionOptions, encryptionOptions, cancellationToken);
        }
Beispiel #4
0
        public async Task NegotiateSessionAsync_EmptyEncryptionOptions_ThrowsArgumentException()
        {
            var target = GetTarget();

            var compressionOptions = new SessionCompression[] { SessionCompression.None };
            var encryptionOptions  = new SessionEncryption[0];

            var cancellationToken = DataUtil.CreateCancellationToken();

            var actual = await target.NegotiateSessionAsync(compressionOptions, encryptionOptions, cancellationToken);
        }
Beispiel #5
0
        public async Task NegotiateSessionAsync_EmptyCompressionOptions_ThrowsArgumentNullException()
        {
            var target             = GetTarget();
            var compressionOptions = new SessionCompression[0];
            var encryptionOptions  = new SessionEncryption[] { SessionEncryption.None, SessionEncryption.TLS };
            var cancellationToken  = Dummy.CreateCancellationToken();

            var actual =
                await
                target.NegotiateSessionAsync(compressionOptions, encryptionOptions, cancellationToken)
                .ShouldThrowAsync <ArgumentException>();
        }
Beispiel #6
0
        public async Task NegotiateSessionAsync_InvalidStateValidOptions_ThrowsInvalidOperationException()
        {
            // Arrange
            var target             = GetTarget(SessionState.Negotiating);
            var compressionOptions = new SessionCompression[] { SessionCompression.None };
            var encryptionOptions  = new SessionEncryption[] { SessionEncryption.None, SessionEncryption.TLS };
            var cancellationToken  = Dummy.CreateCancellationToken();

            // Act
            var actual =
                await
                target.NegotiateSessionAsync(compressionOptions, encryptionOptions, cancellationToken)
                .ShouldThrowAsync <InvalidOperationException>();
        }
Beispiel #7
0
        /// <summary>
        /// Sends a negotiate session envelope
        /// to accepts the session negotiation options
        /// and awaits for the server confirmation.
        /// </summary>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <param name="sessionCompression">The session compression option</param>
        /// <param name="sessionEncryption">The session encryption option</param>
        /// <returns></returns>
        /// <exception cref="System.InvalidOperationException">
        /// Cannot await for a session response since there's already a listener.
        /// </exception>
        public async Task<Session> NegotiateSessionAsync(SessionCompression sessionCompression, SessionEncryption sessionEncryption, CancellationToken cancellationToken)
        {
            if (this.State != SessionState.Negotiating)
            {
                throw new InvalidOperationException(string.Format("Cannot negotiate a session in the '{0}' state", this.State));
            }

            var session = new Session()
            {
                Id = this.SessionId,
                State = SessionState.Negotiating,
                Compression = sessionCompression,
                Encryption = sessionEncryption
            };

            await base.SendSessionAsync(session).ConfigureAwait(false);
            return await this.ReceiveSessionAsync(cancellationToken).ConfigureAwait(false);
        }
Beispiel #8
0
        /// <summary>
        /// Send a negotiate session envelope
        /// to the remote node to confirm the
        /// session negotiation options.
        /// </summary>
        /// <param name="sessionCompression">The session compression option</param>
        /// <param name="sessionEncryption">The session encryption option</param>
        /// <returns></returns>
        /// <exception cref="System.InvalidOperationException"></exception>
        public Task SendNegotiatingSessionAsync(SessionCompression sessionCompression, SessionEncryption sessionEncryption)
        {
            if (this.State != SessionState.Negotiating)
            {
                throw new InvalidOperationException(string.Format("Cannot negotiate a session in the '{0}' state", this.State));
            }

            var session = new Session()
            {
                Id          = this.SessionId,
                From        = this.LocalNode,
                State       = base.State,
                Compression = sessionCompression,
                Encryption  = sessionEncryption
            };

            return(this.SendSessionAsync(session));
        }
Beispiel #9
0
        /// <summary>
        /// Send a negotiate session envelope to the remote node to confirm the session negotiation options.
        /// </summary>
        /// <param name="sessionCompression">The session compression option</param>
        /// <param name="sessionEncryption">The session encryption option</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="System.InvalidOperationException"></exception>
        public Task SendNegotiatingSessionAsync(SessionCompression sessionCompression, SessionEncryption sessionEncryption, CancellationToken cancellationToken)
        {
            if (State != SessionState.Negotiating)
            {
                throw new InvalidOperationException($"Cannot negotiate a session in the '{State}' state");
            }

            var session = new Session
            {
                Id          = SessionId,
                From        = LocalNode,
                State       = State,
                Compression = sessionCompression,
                Encryption  = sessionEncryption
            };

            return(SendSessionAsync(session, cancellationToken));
        }
Beispiel #10
0
        /// <summary>
        /// Sends a negotiate session envelope
        /// to accepts the session negotiation options
        /// and awaits for the server confirmation.
        /// </summary>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <param name="sessionCompression">The session compression option</param>
        /// <param name="sessionEncryption">The session encryption option</param>
        /// <returns></returns>
        /// <exception cref="System.InvalidOperationException">
        /// Cannot await for a session response since there's already a listener.
        /// </exception>
        public async Task <Session> NegotiateSessionAsync(SessionCompression sessionCompression, SessionEncryption sessionEncryption, CancellationToken cancellationToken)
        {
            if (this.State != SessionState.Negotiating)
            {
                throw new InvalidOperationException(string.Format("Cannot negotiate a session in the '{0}' state", this.State));
            }

            var session = new Session()
            {
                Id          = this.SessionId,
                State       = SessionState.Negotiating,
                Compression = sessionCompression,
                Encryption  = sessionEncryption
            };

            await base.SendSessionAsync(session).ConfigureAwait(false);

            return(await this.ReceiveSessionAsync(cancellationToken).ConfigureAwait(false));
        }
Beispiel #11
0
        /// <summary>
        /// Sends a negotiate session envelope
        /// to accepts the session negotiation options
        /// and awaits for the server confirmation.
        /// </summary>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <param name="sessionCompression">The session compression option</param>
        /// <param name="sessionEncryption">The session encryption option</param>
        /// <returns></returns>
        /// <exception cref="System.InvalidOperationException">
        /// Cannot await for a session response since there's already a listener.
        /// </exception>
        public async Task <Session> NegotiateSessionAsync(SessionCompression sessionCompression, SessionEncryption sessionEncryption, CancellationToken cancellationToken)
        {
            if (State != SessionState.Negotiating)
            {
                throw new InvalidOperationException($"Cannot negotiate a session in the '{State}' state");
            }

            var session = new Session
            {
                Id          = SessionId,
                State       = SessionState.Negotiating,
                Compression = sessionCompression,
                Encryption  = sessionEncryption
            };

            await SendSessionAsync(session, cancellationToken).ConfigureAwait(false);

            return(await ReceiveSessionAsync(cancellationToken).ConfigureAwait(false));
        }
Beispiel #12
0
        /// <summary>
        /// Changes the session state and 
        /// sends a negotiate session envelope
        /// to the node with the available 
        /// options and awaits for the client
        /// selected option.
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <param name="compressionOptions">The session compression options.</param>
        /// <param name="encryptionOptions"></param>
        /// <returns>
        /// A negotiating session envelope with the client node selected options.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// compressionOptions
        /// or
        /// encryptionOptions
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// No available options for compression negotiation
        /// or
        /// No available options for encryption negotiation
        /// </exception>
        /// <exception cref="System.InvalidOperationException">Cannot await for a session response since there's already a listener.</exception>
        public async Task<Session> NegotiateSessionAsync(SessionCompression[] compressionOptions, SessionEncryption[] encryptionOptions, CancellationToken cancellationToken)
        {
            if (base.State != SessionState.New)
            {
                throw new InvalidOperationException(string.Format("Cannot start a session negotiating in the '{0}' state", this.State));
            }

            if (compressionOptions == null)
            {
                throw new ArgumentNullException("compressionOptions");
            }

            if (compressionOptions.Length == 0)
            {
                throw new ArgumentException("No available options for compression negotiation");
            }

            if (encryptionOptions == null)
            {
                throw new ArgumentNullException("encryptionOptions");
            }

            if (encryptionOptions.Length == 0)
            {
                throw new ArgumentException("No available options for encryption negotiation");
            }

            base.State = SessionState.Negotiating;

            var session = new Session()
            {
                Id = base.SessionId,
                From = base.LocalNode,
                State = base.State,
                CompressionOptions = compressionOptions,
                EncryptionOptions = encryptionOptions
            };

            await base.SendSessionAsync(session).ConfigureAwait(false);
            return await this.ReceiveSessionAsync(cancellationToken).ConfigureAwait(false);
        }
Beispiel #13
0
        public async Task NegotiateSessionAsync_NewStateValidOptions_CallsTransportAndReadsFromBuffer()
        {
            var session = DataUtil.CreateSession(SessionState.Negotiating);


            var target = GetTarget(sessionId: session.Id);

            var compressionOptions = new SessionCompression[] { SessionCompression.None };
            var encryptionOptions  = new SessionEncryption[] { SessionEncryption.None, SessionEncryption.TLS };

            var cancellationToken = DataUtil.CreateCancellationToken();

            _transport
            .Setup(t => t.ReceiveAsync(It.IsAny <CancellationToken>()))
            .Returns(() => Task.FromResult <Envelope>(session))
            .Verifiable();

            var actual = await target.NegotiateSessionAsync(compressionOptions, encryptionOptions, cancellationToken);

            _transport.Verify();

            _transport.Verify(
                t => t.SendAsync(
                    It.Is <Session>(e => e.State == SessionState.Negotiating &&
                                    e.CompressionOptions == compressionOptions &&
                                    e.EncryptionOptions == encryptionOptions &&
                                    e.From.Equals(target.LocalNode) &&
                                    e.To == null &&
                                    e.SchemeOptions == null &&
                                    e.Compression == null &&
                                    e.Encryption == null &&
                                    e.Authentication == null &&
                                    e.Id == target.SessionId),
                    It.IsAny <CancellationToken>()),
                Times.Once());

            Assert.AreEqual(SessionState.Negotiating, target.State);
            Assert.AreEqual(session, actual);
        }
Beispiel #14
0
        /// <summary>
        /// Defines the compression mode
        /// for the transport
        /// </summary>
        /// <param name="compression">The compression mode</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="System.NotSupportedException"></exception>
        public virtual Task SetCompressionAsync(SessionCompression compression, CancellationToken cancellationToken)
        {
            if (compression != SessionCompression.None)
            {
                throw new NotSupportedException();
            }

            return Task.FromResult<object>(null);
        }
Beispiel #15
0
        /// <summary>
        /// Send a negotiate session envelope
        /// to the remote node to confirm the 
        /// session negotiation options.
        /// </summary>
        /// <param name="sessionCompression">The session compression option</param>
        /// <param name="sessionEncryption">The session encryption option</param>
        /// <returns></returns>
        /// <exception cref="System.InvalidOperationException"></exception>
        public Task SendNegotiatingSessionAsync(SessionCompression sessionCompression, SessionEncryption sessionEncryption)
        {
            if (this.State != SessionState.Negotiating)
            {
                throw new InvalidOperationException(string.Format("Cannot negotiate a session in the '{0}' state", this.State));
            }

            var session = new Session()
            {
                Id = this.SessionId,
                From = this.LocalNode,
                State = base.State,
                Compression = sessionCompression,
                Encryption = sessionEncryption
            };

            return this.SendSessionAsync(session);
        }
 public Task SetCompressionAsync(SessionCompression compression, CancellationToken cancellationToken)
 {
     return(_transport.SetCompressionAsync(compression, cancellationToken).WithCancellation(cancellationToken));
 }
 public TConfigurator UsingCompression(SessionCompression sessionCompression)
 {
     Compression = sessionCompression;
     return((TConfigurator)this);
 }
Beispiel #18
0
        public async Task NegotiateSessionAsync_InvalidStateValidOptions_ThrowsInvalidOperationException()
        {
            var target = GetTarget(SessionState.Negotiating);

            var compressionOptions = new SessionCompression[] { SessionCompression.None };
            var encryptionOptions = new SessionEncryption[] { SessionEncryption.None, SessionEncryption.TLS };

            var cancellationToken = DataUtil.CreateCancellationToken();

            var actual = await target.NegotiateSessionAsync(compressionOptions, encryptionOptions, cancellationToken);
        }
Beispiel #19
0
        public async Task NegotiateSessionAsync_EmptyEncryptionOptions_ThrowsArgumentException()
        {
            var target = GetTarget();

            var compressionOptions = new SessionCompression[] { SessionCompression.None };
            var encryptionOptions = new SessionEncryption[0];

            var cancellationToken = DataUtil.CreateCancellationToken();

            var actual = await target.NegotiateSessionAsync(compressionOptions, encryptionOptions, cancellationToken);
        }
Beispiel #20
0
        public async Task NegotiateSessionAsync_NewStateValidOptions_CallsTransportAndReadsFromBuffer()
        {
            var session = DataUtil.CreateSession(SessionState.Negotiating);


            var target = GetTarget(sessionId: session.Id);           

            var compressionOptions = new SessionCompression[] { SessionCompression.None };
            var encryptionOptions = new SessionEncryption[] { SessionEncryption.None, SessionEncryption.TLS };

            var cancellationToken = DataUtil.CreateCancellationToken();

            _transport
                .Setup(t => t.ReceiveAsync(It.IsAny<CancellationToken>()))
                .Returns(() => Task.FromResult<Envelope>(session))
                .Verifiable();

            var actual = await target.NegotiateSessionAsync(compressionOptions, encryptionOptions, cancellationToken);

            _transport.Verify();

            _transport.Verify(
                t => t.SendAsync(
                    It.Is<Session>(e => e.State == SessionState.Negotiating &&
                                        e.CompressionOptions == compressionOptions &&
                                        e.EncryptionOptions == encryptionOptions &&
                                        e.From.Equals(target.LocalNode) &&
                                        e.To == null &&
                                        e.SchemeOptions == null &&
                                        e.Compression == null &&
                                        e.Encryption == null &&
                                        e.Authentication == null &&
                                        e.Id == target.SessionId),
                    It.IsAny<CancellationToken>()),
                    Times.Once());

            Assert.AreEqual(SessionState.Negotiating, target.State);
            Assert.AreEqual(session, actual);
        }
 public BlipClientBuilder UsingCompression(SessionCompression sessionCompression)
 {
     Compression = sessionCompression;
     return(this);
 }
 /// <summary>
 /// Sets the compression option to be used in the session establishment.
 /// </summary>
 /// <param name="compression">The compression.</param>
 /// <returns></returns>
 public EstablishedClientChannelBuilder WithCompression(SessionCompression compression)
 {
     return(WithCompression(options => compression));
 }