Example #1
0
        public void IsAuthorized_ReturnsTrue_WhenConnectionTheSame()
        {
            ListenerConnection connection = Construct.Uninitialized <ListenerConnection>();
            var securityContext           = new SecurityContext();

            securityContext.Authorize(connection);

            var result = securityContext.IsAuthorized(connection);

            result.ShouldBeTrue();
        }
Example #2
0
        public void IsAuthorized_ReturnsFalse_WhenConnectionNotTheSame()
        {
            ListenerConnection connection1 = Construct.Uninitialized <ListenerConnection>();
            Connection         connection2 = Construct.Uninitialized <Connection>();
            var securityContext            = new SecurityContext();

            securityContext.Authorize(connection1);

            var result = securityContext.IsAuthorized(connection2);

            result.ShouldBeFalse();
        }
Example #3
0
        /// <summary>
        /// Invoked once a connection is accepted by StreamSocketListener.
        /// </summary>
        /// <param name="sender">The listener that accepted the connection.</param>
        /// <param name="args">Parameters associated with the accepted connection.</param>
        private async void OnConnection(
            StreamSocketListener sender,
            StreamSocketListenerConnectionReceivedEventArgs args)
        {
            DataReader reader = new DataReader(args.Socket.InputStream);

            try
            {
                //Windows.Storage.Streams.Buffer Out = new Windows.Storage.Streams.Buffer(100);
                //await args.Socket.OutputStream.WriteAsync(Out);
                while (true)
                {
                    // Read first 4 bytes (length of the subsequent string).
                    Sox.StartToken();
                    uint sizeFieldCount = await reader.LoadAsync(sizeof(uint)).AsTask(Sox.CancelToken);

                    if (sizeFieldCount != sizeof(uint))
                    {
                        // The underlying socket was closed before we were able to read the whole data.
                        return;
                    }

                    // Read the string.
                    uint stringLength = reader.ReadUInt32();
                    Sox.StartToken();
                    uint actualStringLength = await reader.LoadAsync(stringLength).AsTask(Sox.CancelToken);

                    if (stringLength != actualStringLength)
                    {
                        // The underlying socket was closed before we were able to read the whole data.
                        return;
                    }
                    string data = reader.ReadString(actualStringLength);
                    await NotifyUserFromAsyncThread(
                        String.Format("Received data: \"{0}\"", data),
                        NotifyType.StatusMessage);

                    //data should be the string Passkey + comamnd char + Parameter string (can be "")
                    //If of that format that get the command and parameter and fire the ListenerConnection Event
                    if (data.Length > 0)
                    {
                        if (data.Length >= Passkey.Length)
                        {
                            if (Passkey == data.Substring(0, Passkey.Length))
                            {
                                char   command = data[Passkey.Length];
                                string param   = "";
                                if (data.Length > Passkey.Length + 1)
                                {
                                    param = data.Substring(Passkey.Length + 1);
                                }
                                string msg = string.Format("(InConnection) Command: '{0}' Param= \"{1}\" recvd", command, param);
                                Sox.Instance.Log(msg, NotifyType.StatusMessage);
                                //Fire the ListenerConnection Event
                                await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                                                                                                                            () =>
                                {
                                    ListenerConnection.Invoke(command, param);
                                }).AsTask(Sox.CancelToken);
                            }
                            else
                            {
                                Sox.Instance.Log("Null Command recvd.", NotifyType.StatusMessage);
                            }
                        }
                        else
                        {
                            Sox.Instance.Log("Null Message recvd.", NotifyType.ErrorMessage);
                        }
                    }
                    else
                    {
                        Sox.Instance.Log("Null data recvd.", NotifyType.ErrorMessage);
                    }
                }
            }
            catch (TaskCanceledException)
            {
                Sox.Instance.Log("Cancelled.", NotifyType.StatusMessage);
            }
            catch (Exception exception)
            {
                // If this is an unknown status it means that the error is fatal and retry will likely fail.
                if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
                {
                    throw;
                }

                await NotifyUserFromAsyncThread(
                    "Read stream failed with error: " + exception.Message,
                    NotifyType.ErrorMessage);
            }
        }
Example #4
0
        bool IContainer.AttachLink(ListenerConnection connection, ListenerSession session, Link link, Attach attach)
        {
            Source source  = attach.Source as Source;
            Target target  = attach.Target as Target;
            bool   dynamic = false;
            string address = null;

            if (attach.Role)
            {
                address = source.Address;
                dynamic = source.Dynamic;
            }
            else
            {
                if (target != null)
                {
                    address = target.Address;
                    dynamic = target.Dynamic;
                }
                else if (attach.Target is Coordinator)
                {
                    this.txnManager.AddCoordinator((ListenerLink)link);
                    return(true);
                }
            }

            if (dynamic)
            {
                address = string.Format("$dynamic.{0}", Interlocked.Increment(ref this.dynamidId));
                if (attach.Role)
                {
                    source.Dynamic = false;
                    source.Address = address;
                }
                else
                {
                    target.Address = address;
                    target.Dynamic = false;
                }
            }

            TestQueue queue;

            lock (this.queues)
            {
                if (!this.queues.TryGetValue(address, out queue))
                {
                    if (dynamic || (this.implicitQueue && !link.Name.StartsWith("$explicit:")))
                    {
                        queue = new TestQueue(this);
                        this.queues.Add(address, queue);
                        connection.Closed += (o, e) => this.RemoveQueue(address);
                    }
                    else
                    {
                        throw new AmqpException(ErrorCode.NotFound, string.Format("Node '{0}' not found", address));
                    }
                }
            }

            if (attach.Role)
            {
                queue.CreateConsumer((ListenerLink)link);
            }
            else
            {
                queue.CreatePublisher((ListenerLink)link);
            }

            return(true);
        }
Example #5
0
 Link IContainer.CreateLink(ListenerConnection connection, ListenerSession session, Attach attach)
 {
     return(new ListenerLink(session, attach));
 }
        bool IContainer.AttachLink(ListenerConnection connection, ListenerSession session, Link link, Attach attach)
        {
            Source source = attach.Source as Source;
            Target target = attach.Target as Target;
            bool dynamic = false;
            string address = null;
            if (attach.Role)
            {
                address = source.Address;
                dynamic = source.Dynamic;
            }
            else
            {
                if (target != null)
                {
                    address = target.Address;
                    dynamic = target.Dynamic;
                }
                else if (attach.Target is Coordinator)
                {
                    this.txnManager.AddCoordinator((ListenerLink)link);
                    return true;
                }
            }

            if (dynamic)
            {
                address = string.Format("$dynamic.{0}", Interlocked.Increment(ref this.dynamidId));
                if (attach.Role)
                {
                    source.Dynamic = false;
                    source.Address = address;
                }
                else
                {
                    target.Address = address;
                    target.Dynamic = false;
                }
            }

            TestQueue queue;
            lock (this.queues)
            {
                if (!this.queues.TryGetValue(address, out queue))
                {
                    if (dynamic || this.implicitQueue)
                    {
                        queue = new TestQueue(this);
                        this.queues.Add(address, queue);
                        connection.Closed += (o, e) => this.RemoveQueue(address);
                    }
                    else
                    {
                        throw new AmqpException(ErrorCode.NotFound, string.Format("Node '{0}' not found", address));
                    }
                }
            }

            if (attach.Role)
            {
                queue.CreateConsumer((ListenerLink)link);
            }
            else
            {
                queue.CreatePublisher((ListenerLink)link);
            }

            return true;
        }
 Link IContainer.CreateLink(ListenerConnection connection, ListenerSession session, Attach attach)
 {
     return new ListenerLink(session, attach);
 }
Example #8
0
 private static void SetPrincipal(ListenerConnection connection, IPrincipal principal)
 => connection
 .GetType()
 .GetProperty(nameof(ListenerConnection.Principal), BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
 .SetValue(connection, principal);