public async Task LeaveRoomAsync_Throws_NoResponseException_On_Wait_Timeout(string roomName)
        {
            var conn = new Mock <IMessageConnection>();

            conn.Setup(m => m.State)
            .Returns(ConnectionState.Connected);

            var key    = new WaitKey(MessageCode.Server.LeaveRoom, roomName);
            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait(It.Is <WaitKey>(k => k.Equals(key)), It.IsAny <int?>(), It.IsAny <CancellationToken?>()))
            .Returns(Task.FromException(new TimeoutException()));

            using (var s = new SoulseekClient(serverConnection: conn.Object, waiter: waiter.Object))
            {
                s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

                var ex = await Record.ExceptionAsync(() => s.LeaveRoomAsync(roomName));

                Assert.NotNull(ex);
                Assert.IsType <NoResponseException>(ex);
            }
        }
        public async Task GetUserAddressAsync_Throws_PeerOfflineException_When_Peer_Is_Offline(string username)
        {
            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait <UserAddressResponse>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(new UserAddressResponse(username, IPAddress.Parse("0.0.0.0"), 0)));

            var conn = new Mock <IMessageConnection>();

            conn.Setup(m => m.WriteAsync(It.IsAny <byte[]>(), It.IsAny <CancellationToken?>()))
            .Returns(Task.CompletedTask);

            using (var s = new SoulseekClient("127.0.0.1", 1, serverConnection: conn.Object, waiter: waiter.Object))
            {
                s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

                var ex = await Record.ExceptionAsync(async() => await s.GetUserAddressAsync(username));

                Assert.NotNull(ex);
                Assert.IsType <UserAddressException>(ex);
                Assert.IsType <PeerOfflineException>(ex.InnerException);
            }
        }
Esempio n. 3
0
        public async Task AddUserAsync_Returns_Expected_Info(string username, UserData userData)
        {
            var result = new AddUserResponse(username, true, userData);

            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait <AddUserResponse>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(result));

            var serverConn = new Mock <IMessageConnection>();

            serverConn.Setup(m => m.WriteAsync(It.IsAny <byte[]>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            using (var s = new SoulseekClient("127.0.0.1", 1, waiter: waiter.Object, serverConnection: serverConn.Object))
            {
                s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

                var add = await s.AddUserAsync(username);

                Assert.Equal(result.UserData, add);
            }
        }
        public async Task JoinRoomAsync_Throws_RoomJoinForbiddenException_When_Server_Rejects(string roomName)
        {
            var conn = new Mock <IMessageConnection>();

            conn.Setup(m => m.State)
            .Returns(ConnectionState.Connected);

            var key    = new WaitKey(MessageCode.Server.JoinRoom, roomName);
            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait <RoomData>(It.Is <WaitKey>(k => k.Equals(key)), It.IsAny <int?>(), It.IsAny <CancellationToken?>()))
            .Throws(new RoomJoinForbiddenException());

            using (var s = new SoulseekClient(serverConnection: conn.Object, waiter: waiter.Object))
            {
                s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

                var ex = await Record.ExceptionAsync(() => s.JoinRoomAsync(roomName));

                Assert.NotNull(ex);
                Assert.IsType <RoomJoinForbiddenException>(ex);
            }
        }
Esempio n. 5
0
        public async Task LoginAsync_Uses_Given_CancellationToken(string user, string password)
        {
            var cancellationToken = new CancellationToken();

            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait <LoginResponse>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(new LoginResponse(true, string.Empty)));

            var conn = new Mock <IMessageConnection>();

            using (var s = new SoulseekClient(serverConnection: conn.Object, waiter: waiter.Object))
            {
                s.SetProperty("State", SoulseekClientStates.Connected);

                await s.LoginAsync(user, password, cancellationToken);

                Assert.Equal(SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn, s.State);
                Assert.Equal(user, s.Username);
            }

            conn.Verify(m => m.WriteAsync(It.IsAny <IOutgoingMessage>(), cancellationToken), Times.AtLeastOnce);
        }
        public void Throws_Wait_For_PeerBrowseResponse_Given_Bad_Message(string username, IPAddress ip, int port)
        {
            var conn = new Mock <IMessageConnection>();

            conn.Setup(m => m.Username)
            .Returns(username);
            conn.Setup(m => m.IPAddress)
            .Returns(ip);
            conn.Setup(m => m.Port)
            .Returns(port);

            var waiter = new Mock <IWaiter>();

            var msg = new MessageBuilder()
                      .Code(MessageCode.PeerBrowseResponse)
                      .Build();

            var s = new SoulseekClient("127.0.0.1", 1, waiter: waiter.Object);

            s.InvokeMethod("PeerConnection_MessageRead", conn.Object, msg);

            waiter.Verify(m => m.Throw(new WaitKey(MessageCode.PeerBrowseResponse, username), It.IsAny <MessageReadException>()), Times.Once);
        }
        public async Task ChangePasswordAsync_Throws_On_Mismatching_Confirmation(string password)
        {
            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait <string>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(password + "!"));

            var serverConn = new Mock <IMessageConnection>();

            serverConn.Setup(m => m.WriteAsync(It.IsAny <byte[]>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            using (var s = new SoulseekClient(waiter: waiter.Object, serverConnection: serverConn.Object))
            {
                s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

                var ex = await Record.ExceptionAsync(() => s.ChangePasswordAsync(password));

                Assert.NotNull(ex);
                Assert.IsType <ChangePasswordException>(ex);
                Assert.True(ex.Message.ContainsInsensitive("doesn't match the specified password"));
            }
        }
Esempio n. 8
0
        public async Task GrantUserPrivilegesAsync_Uses_Given_CancellationToken(string username, int days)
        {
            var cancellationToken = new CancellationToken();

            var serverConn = new Mock <IMessageConnection>();

            serverConn.Setup(m => m.WriteAsync(It.IsAny <IOutgoingMessage>(), cancellationToken))
            .Returns(Task.CompletedTask);

            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait(It.Is <WaitKey>(k => k == new WaitKey(MessageCode.Server.GivePrivileges)), null, It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            using (var s = new SoulseekClient(serverConnection: serverConn.Object, waiter: waiter.Object))
            {
                s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

                await s.GrantUserPrivilegesAsync(username, days, cancellationToken);
            }

            serverConn.Verify(m => m.WriteAsync(It.IsAny <IOutgoingMessage>(), cancellationToken), Times.Once);
        }
        public async Task ChangePasswordAsync_Throws_ChangePasswordException_On_Throw(string password)
        {
            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait <string>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(password));

            var serverConn = new Mock <IMessageConnection>();

            serverConn.Setup(m => m.WriteAsync(It.IsAny <byte[]>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromException(new ConnectionException()));

            using (var s = new SoulseekClient(waiter: waiter.Object, serverConnection: serverConn.Object))
            {
                s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

                var ex = await Record.ExceptionAsync(() => s.ChangePasswordAsync(password));

                Assert.NotNull(ex);
                Assert.IsType <ChangePasswordException>(ex);
                Assert.IsType <ConnectionException>(ex.InnerException);
            }
        }
Esempio n. 10
0
        public async Task DropPrivateRoomMembershipAsync_Completes_When_Wait_Completes(string roomName)
        {
            var conn = new Mock <IMessageConnection>();

            conn.Setup(m => m.State)
            .Returns(ConnectionState.Connected);
            conn.Setup(m => m.WriteAsync(It.IsAny <IOutgoingMessage>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            using (var s = new SoulseekClient(serverConnection: conn.Object, waiter: waiter.Object))
            {
                s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

                var ex = await Record.ExceptionAsync(() => s.DropPrivateRoomMembershipAsync(roomName));

                Assert.Null(ex);
            }
        }
Esempio n. 11
0
        public async Task AddUserAsync_Uses_Given_CancellationToken(string username, UserData userData, CancellationToken cancellationToken)
        {
            var result = new AddUserResponse(username, true, userData);

            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait <AddUserResponse>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(result));

            var serverConn = new Mock <IMessageConnection>();

            serverConn.Setup(m => m.WriteAsync(It.IsAny <byte[]>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            using (var s = new SoulseekClient(waiter: waiter.Object, serverConnection: serverConn.Object))
            {
                s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

                await s.AddUserAsync(username, cancellationToken);
            }

            serverConn.Verify(m => m.WriteAsync(It.IsAny <byte[]>(), cancellationToken), Times.Once);
        }
Esempio n. 12
0
        public async Task LoginAsync_Sets_Listen_Port_On_Success_If_Set(string user, string password, int port)
        {
            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait <LoginResponse>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(new LoginResponse(true, string.Empty)));

            var conn = new Mock <IMessageConnection>();

            using (var s = new SoulseekClient("127.0.0.1", 1, serverConnection: conn.Object, waiter: waiter.Object, options: new ClientOptions(listenPort: port)))
            {
                s.SetProperty("State", SoulseekClientStates.Connected);

                await s.LoginAsync(user, password);

                Assert.Equal(SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn, s.State);
                Assert.Equal(user, s.Username);
            }

            var expectedBytes = new SetListenPort(port).ToByteArray();

            conn.Verify(m => m.WriteAsync(It.Is <byte[]>(b => b.Matches(expectedBytes)), It.IsAny <CancellationToken?>()));
        }
Esempio n. 13
0
        public async Task SearchInternalAsync_Throws_SearchException_On_Error(string searchText, int token)
        {
            var options  = new SearchOptions();
            var response = new SearchResponse("username", token, 1, 1, 1, 0, new List <File>()
            {
                new File(1, "foo", 1, "bar", 0)
            });

            var search = new Search(searchText, token, options)
            {
                State = SearchStates.InProgress
            };

            search.SetProperty("ResponseList", new List <SearchResponse>()
            {
                response
            });

            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.WaitIndefinitely <Search>(It.IsAny <WaitKey>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromException <Search>(new Exception()));

            var conn = new Mock <IMessageConnection>();

            conn.Setup(m => m.WriteMessageAsync(It.IsAny <Message>()))
            .Returns(Task.CompletedTask);

            var s = new SoulseekClient("127.0.0.1", 1, messageWaiter: waiter.Object, serverConnection: conn.Object);

            s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

            var ex = await Record.ExceptionAsync(() => s.SearchAsync(searchText, token, options, null));

            Assert.NotNull(ex);
            Assert.IsType <SearchException>(ex);
        }
Esempio n. 14
0
        public async Task GetPeerInfoAsync_Throws_PeerInfoException_On_Throw(string username, string description, byte[] picture, int uploadSlots, int queueLength, bool hasFreeSlot)
        {
            var result = new PeerInfoResponse(description, true, picture, uploadSlots, queueLength, hasFreeSlot);

            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait <PeerInfoResponse>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(result));
            waiter.Setup(m => m.Wait <GetPeerAddressResponse>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(new GetPeerAddressResponse(username, IPAddress.Parse("127.0.0.1"), 1)));

            var serverConn = new Mock <IMessageConnection>();

            serverConn.Setup(m => m.WriteMessageAsync(It.IsAny <Message>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            var conn = new Mock <IMessageConnection>();

            conn.Setup(m => m.WriteMessageAsync(It.IsAny <Message>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromException(new ConnectionException("foo")));

            var connManager = new Mock <IConnectionManager>();

            connManager.Setup(m => m.GetOrAddUnsolicitedConnectionAsync(It.IsAny <ConnectionKey>(), It.IsAny <string>(), It.IsAny <EventHandler <Message> >(), It.IsAny <ConnectionOptions>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(conn.Object));

            var s = new SoulseekClient("127.0.0.1", 1, waiter: waiter.Object, serverConnection: serverConn.Object, connectionManager: connManager.Object);

            s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

            PeerInfoResponse info = null;
            var ex = await Record.ExceptionAsync(async() => info = await s.GetPeerInfoAsync(username));

            Assert.NotNull(ex);
            Assert.IsType <PeerInfoException>(ex);
            Assert.IsType <ConnectionException>(ex.InnerException);
        }
        public async Task GetUserInfoAsync_Throws_OperationCanceledException_On_Cancellation(string username, string description, byte[] picture, int uploadSlots, int queueLength, bool hasFreeSlot)
        {
            var result = new UserInfo(description, uploadSlots, queueLength, hasFreeSlot, picture);

            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait <UserInfo>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(result));
            waiter.Setup(m => m.Wait <UserAddressResponse>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(new UserAddressResponse(username, IPAddress.Parse("127.0.0.1"), 1)));

            var serverConn = new Mock <IMessageConnection>();

            serverConn.Setup(m => m.WriteAsync(It.IsAny <IOutgoingMessage>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            var conn = new Mock <IMessageConnection>();

            conn.Setup(m => m.WriteAsync(It.IsAny <IOutgoingMessage>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromException(new OperationCanceledException()));

            var connManager = new Mock <IPeerConnectionManager>();

            connManager.Setup(m => m.GetOrAddMessageConnectionAsync(username, It.IsAny <IPEndPoint>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(conn.Object));

            using (var s = new SoulseekClient(waiter: waiter.Object, serverConnection: serverConn.Object, peerConnectionManager: connManager.Object))
            {
                s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

                UserInfo info = null;
                var      ex   = await Record.ExceptionAsync(async() => info = await s.GetUserInfoAsync(username));

                Assert.NotNull(ex);
                Assert.IsType <OperationCanceledException>(ex);
            }
        }
        public void Attempts_Connection_On_Expected_ConnectToPeerResponse_F(string filename, string username, int token, IPAddress ip, int port)
        {
            var ipBytes = ip.GetAddressBytes();

            Array.Reverse(ipBytes);

            var msg = new MessageBuilder()
                      .Code(MessageCode.ServerConnectToPeer)
                      .WriteString(username)
                      .WriteString("F")
                      .WriteBytes(ipBytes)
                      .WriteInteger(port)
                      .WriteInteger(token)
                      .Build();

            var conn = new Mock <IConnection>();

            conn.Setup(m => m.ReadAsync(4, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(new byte[] { 0, 0, 0, 0 }));

            var connManager = new Mock <IConnectionManager>();

            connManager.Setup(m => m.AddSolicitedTransferConnectionAsync(It.IsAny <ConnectToPeerResponse>(), It.IsAny <ConnectionOptions>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(conn.Object));

            using (var s = new SoulseekClient("127.0.0.1", 1, connectionManager: connManager.Object))
            {
                var active = new ConcurrentDictionary <int, Download>();
                active.TryAdd(token, new Download(username, filename, token));

                s.SetProperty("Downloads", active);

                s.InvokeMethod("ServerConnection_MessageRead", null, msg);

                connManager.Verify(m => m.AddSolicitedTransferConnectionAsync(It.IsAny <ConnectToPeerResponse>(), It.IsAny <ConnectionOptions>(), It.IsAny <CancellationToken>()), Times.Once);
            }
        }
Esempio n. 17
0
        public async Task GetDirectoryContentsAsync_Throws_TimeoutException_On_Timeout(string username, string directory)
        {
            var result = new Directory(directory);

            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait <Directory>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(result));
            waiter.Setup(m => m.Wait <UserAddressResponse>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(new UserAddressResponse(username, IPAddress.Parse("127.0.0.1"), 1)));

            var serverConn = new Mock <IMessageConnection>();

            serverConn.Setup(m => m.WriteAsync(It.IsAny <byte[]>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            var conn = new Mock <IMessageConnection>();

            conn.Setup(m => m.WriteAsync(It.IsAny <byte[]>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromException(new TimeoutException()));

            var connManager = new Mock <IPeerConnectionManager>();

            connManager.Setup(m => m.GetOrAddMessageConnectionAsync(username, It.IsAny <IPEndPoint>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(conn.Object));

            using (var s = new SoulseekClient(waiter: waiter.Object, serverConnection: serverConn.Object, peerConnectionManager: connManager.Object))
            {
                s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

                Directory dir = null;
                var       ex  = await Record.ExceptionAsync(async() => dir = await s.GetDirectoryContentsAsync(username, directory));

                Assert.NotNull(ex);
                Assert.IsType <TimeoutException>(ex);
            }
        }
Esempio n. 18
0
        public async Task BrowseAsync_Uses_Given_CancellationToken(string username, IPEndPoint endpoint, string localUsername, List <Directory> directories, CancellationToken cancellationToken)
        {
            var response = new BrowseResponse(directories);

            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.WaitIndefinitely <BrowseResponse>(It.IsAny <WaitKey>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(response));
            waiter.Setup(m => m.Wait <UserAddressResponse>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(new UserAddressResponse(username, endpoint.Address, endpoint.Port)));

            var conn = new Mock <IMessageConnection>();

            conn.Setup(m => m.State)
            .Returns(ConnectionState.Connected);
            conn.Setup(m => m.WriteAsync(It.IsAny <IOutgoingMessage>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            var connManager = new Mock <IPeerConnectionManager>();

            connManager.Setup(m => m.GetOrAddMessageConnectionAsync(username, endpoint, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(conn.Object));

            waiter.Setup(m => m.Wait <(MessageReceivedEventArgs, IMessageConnection)>(It.IsAny <WaitKey>(), It.IsAny <int?>(), It.IsAny <CancellationToken?>()))
            .Returns(Task.FromResult((new MessageReceivedEventArgs(1, new byte[] { 0x0 }), conn.Object)));

            using (var s = new SoulseekClient(waiter: waiter.Object, serverConnection: conn.Object, peerConnectionManager: connManager.Object))
            {
                s.SetProperty("Username", localUsername);
                s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

                await s.BrowseAsync(username, cancellationToken : cancellationToken);
            }

            conn.Verify(m => m.WriteAsync(It.IsAny <IOutgoingMessage>(), cancellationToken), Times.AtLeastOnce);
        }
        public async Task GetUserInfoAsync_Uses_Given_CancellationToken(string username, string description, byte[] picture, int uploadSlots, int queueLength, bool hasFreeSlot)
        {
            var cancellationToken = new CancellationToken();
            var result            = new UserInfo(description, uploadSlots, queueLength, hasFreeSlot, picture);

            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait <UserInfo>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(result));
            waiter.Setup(m => m.Wait <UserAddressResponse>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(new UserAddressResponse(username, IPAddress.Parse("127.0.0.1"), 1)));

            var serverConn = new Mock <IMessageConnection>();

            serverConn.Setup(m => m.WriteAsync(It.IsAny <IOutgoingMessage>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            var conn = new Mock <IMessageConnection>();

            conn.Setup(m => m.WriteAsync(It.IsAny <IOutgoingMessage>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            var connManager = new Mock <IPeerConnectionManager>();

            connManager.Setup(m => m.GetOrAddMessageConnectionAsync(username, It.IsAny <IPEndPoint>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(conn.Object));

            using (var s = new SoulseekClient(waiter: waiter.Object, serverConnection: serverConn.Object, peerConnectionManager: connManager.Object))
            {
                s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

                await s.GetUserInfoAsync(username, cancellationToken);
            }

            conn.Verify(m => m.WriteAsync(It.IsAny <IOutgoingMessage>(), cancellationToken), Times.Once);
        }
        public async Task GetUserStatisticsAsync_Throws_OperationCanceledException_On_Cancellation(string username, int averageSpeed, long uploadCount, int fileCount, int directoryCount)
        {
            var result = new UserStatistics(username, averageSpeed, uploadCount, fileCount, directoryCount);

            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait <UserStatistics>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(result));

            var serverConn = new Mock <IMessageConnection>();

            serverConn.Setup(m => m.WriteAsync(It.IsAny <IOutgoingMessage>(), It.IsAny <CancellationToken>()))
            .Throws(new OperationCanceledException());

            using (var s = new SoulseekClient(waiter: waiter.Object, serverConnection: serverConn.Object))
            {
                s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

                var ex = await Record.ExceptionAsync(() => s.GetUserStatisticsAsync(username));

                Assert.NotNull(ex);
                Assert.IsType <OperationCanceledException>(ex);
            }
        }
        public async Task GetUserStatisticsAsync_Uses_Given_CancellationToken(string username, int averageSpeed, long uploadCount, int fileCount, int directoryCount)
        {
            var cancellationToken = new CancellationToken();
            var result            = new UserStatistics(username, averageSpeed, uploadCount, fileCount, directoryCount);

            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait <UserStatistics>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(result));

            var serverConn = new Mock <IMessageConnection>();

            serverConn.Setup(m => m.WriteAsync(It.IsAny <IOutgoingMessage>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            using (var s = new SoulseekClient(waiter: waiter.Object, serverConnection: serverConn.Object))
            {
                s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

                await s.GetUserStatisticsAsync(username, cancellationToken);
            }

            serverConn.Verify(m => m.WriteAsync(It.IsAny <IOutgoingMessage>(), cancellationToken));
        }
        public async Task Address_Throws_ListenPortException_On_Bad_Listen_Port()
        {
            var port = Mocks.Port;

            using (var s = new SoulseekClient(new SoulseekClientOptions(enableListener: true, listenPort: port)))
            {
                Listener listener = null;

                try
                {
                    listener = new Listener(port, new ConnectionOptions());
                    listener.Start();

                    var ex = await Record.ExceptionAsync(() => s.ConnectAsync("u", "p"));

                    Assert.NotNull(ex);
                    Assert.IsType <ListenPortException>(ex);
                }
                finally
                {
                    listener.Stop();
                }
            }
        }
Esempio n. 23
0
        public async Task BrowseAsync_Returns_Expected_Response_On_Success(string username, IPEndPoint endpoint, string localUsername, List <Directory> directories)
        {
            var response = new BrowseResponse(directories.Count, directories);

            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.WaitIndefinitely <BrowseResponse>(It.IsAny <WaitKey>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(response));
            waiter.Setup(m => m.Wait <UserAddressResponse>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(new UserAddressResponse(username, endpoint.Address, endpoint.Port)));

            var conn = new Mock <IMessageConnection>();

            conn.Setup(m => m.State)
            .Returns(ConnectionState.Connected);
            conn.Setup(m => m.WriteAsync(It.IsAny <byte[]>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            var connManager = new Mock <IPeerConnectionManager>();

            connManager.Setup(m => m.GetOrAddMessageConnectionAsync(username, endpoint, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(conn.Object));

            waiter.Setup(m => m.Wait <(MessageReceivedEventArgs, IMessageConnection)>(It.IsAny <WaitKey>(), It.IsAny <int?>(), It.IsAny <CancellationToken?>()))
            .Returns(Task.FromResult((new MessageReceivedEventArgs(1, new byte[] { 0x0 }), conn.Object)));

            using (var s = new SoulseekClient("127.0.0.1", 1, waiter: waiter.Object, serverConnection: conn.Object, peerConnectionManager: connManager.Object))
            {
                s.SetProperty("Username", localUsername);
                s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

                var result = await s.BrowseAsync(username);

                Assert.Equal(response.Directories, result);
            }
        }
Esempio n. 24
0
        public async Task BrowseAsync_Throws_BrowseException_On_Disconnect(string username, IPEndPoint endpoint, string localUsername)
        {
            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait <UserAddressResponse>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(new UserAddressResponse(username, endpoint.Address, endpoint.Port)));
            waiter.Setup(m => m.WaitIndefinitely <BrowseResponse>(It.IsAny <WaitKey>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromException <BrowseResponse>(new ConnectionException("disconnected unexpectedly")));

            var conn = new Mock <IMessageConnection>();

            conn.Setup(m => m.WriteAsync(It.IsAny <byte[]>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask)
            .Raises(m => m.Disconnected += null, conn.Object, new ConnectionDisconnectedEventArgs(string.Empty));

            var connManager = new Mock <IPeerConnectionManager>();

            connManager.Setup(m => m.GetOrAddMessageConnectionAsync(username, endpoint, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(conn.Object));

            waiter.Setup(m => m.Wait <(MessageReceivedEventArgs, IMessageConnection)>(It.IsAny <WaitKey>(), It.IsAny <int?>(), It.IsAny <CancellationToken?>()))
            .Returns(Task.FromResult((new MessageReceivedEventArgs(1, new byte[] { 0x0 }), conn.Object)));

            using (var s = new SoulseekClient("127.0.0.1", 1, waiter: waiter.Object, serverConnection: conn.Object, peerConnectionManager: connManager.Object))
            {
                s.SetProperty("Username", localUsername);
                s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

                var ex = await Record.ExceptionAsync(() => s.BrowseAsync(username));

                Assert.NotNull(ex);
                Assert.IsType <BrowseException>(ex);
                Assert.IsType <ConnectionException>(ex.InnerException);
                Assert.Contains("disconnected unexpectedly", ex.InnerException.Message, StringComparison.InvariantCultureIgnoreCase);
            }
        }
Esempio n. 25
0
        public async Task GetUserStatusAsync_Uses_Given_CancellationToken(string username, UserPresence presence, bool privileged)
        {
            var cancellationToken = new CancellationToken();
            var result            = new UserStatusResponse(username, presence, privileged);

            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait <UserStatusResponse>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(result));

            var serverConn = new Mock <IMessageConnection>();

            serverConn.Setup(m => m.WriteAsync(It.IsAny <IOutgoingMessage>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            using (var s = new SoulseekClient(waiter: waiter.Object, serverConnection: serverConn.Object))
            {
                s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

                await s.GetUserStatusAsync(username, cancellationToken);
            }

            serverConn.Verify(m => m.WriteAsync(It.IsAny <IOutgoingMessage>(), cancellationToken));
        }
Esempio n. 26
0
        public async Task GetUserStatusAsync_Throws_OperationCanceledException_On_Cancellation(string username, UserPresence status, bool privileged)
        {
            var result = new UserStatusResponse(username, status, privileged);

            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait <UserStatusResponse>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(result));

            var serverConn = new Mock <IMessageConnection>();

            serverConn.Setup(m => m.WriteAsync(It.IsAny <IOutgoingMessage>(), It.IsAny <CancellationToken>()))
            .Throws(new OperationCanceledException());

            using (var s = new SoulseekClient(waiter: waiter.Object, serverConnection: serverConn.Object))
            {
                s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

                var ex = await Record.ExceptionAsync(() => s.GetUserStatusAsync(username));

                Assert.NotNull(ex);
                Assert.IsType <OperationCanceledException>(ex);
            }
        }
Esempio n. 27
0
        public async Task SearchInternalAsync_Delegate_Creates_Token_When_Not_Given(string searchText)
        {
            var conn = new Mock <IMessageConnection>();

            conn.Setup(m => m.WriteAsync(It.IsAny <byte[]>(), null))
            .Returns(Task.CompletedTask);

            using (var cts = new CancellationTokenSource(1000))
                using (var s = new SoulseekClient(serverConnection: conn.Object))
                {
                    s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

                    var task = s.SearchAsync(SearchQuery.FromText(searchText), (r) => { }, cancellationToken: cts.Token);

                    var active = s.GetProperty <ConcurrentDictionary <int, SearchInternal> >("Searches").ToList();

                    cts.Cancel();

                    await Record.ExceptionAsync(() => task); // swallow the cancellation exception

                    Assert.Single(active);
                    Assert.Contains(active, kvp => kvp.Value.SearchText == searchText);
                }
        }
Esempio n. 28
0
        public async Task GetRoomListAsync_Returns_Expected_Response_On_Success(IReadOnlyCollection <Room> rooms)
        {
            var conn = new Mock <IMessageConnection>();

            conn.Setup(m => m.State)
            .Returns(ConnectionState.Connected);

            var key    = new WaitKey(MessageCode.Server.RoomList);
            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait <IReadOnlyCollection <Room> >(It.Is <WaitKey>(k => k.Equals(key)), It.IsAny <int?>(), It.IsAny <CancellationToken?>()))
            .Returns(Task.FromResult(rooms));

            using (var s = new SoulseekClient(serverConnection: conn.Object, waiter: waiter.Object))
            {
                s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

                IReadOnlyCollection <Room> response;

                response = await s.GetRoomListAsync();

                Assert.Equal(rooms, response);
            }
        }
Esempio n. 29
0
        public async Task AddUserAsyncAsync_Throws_TimeoutException_On_Timeout(string username, bool exists, UserData userData)
        {
            var result = new AddUserResponse(username, exists, userData);

            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait <AddUserResponse>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(result));

            var serverConn = new Mock <IMessageConnection>();

            serverConn.Setup(m => m.WriteAsync(It.IsAny <IOutgoingMessage>(), It.IsAny <CancellationToken>()))
            .Throws(new TimeoutException());

            using (var s = new SoulseekClient(waiter: waiter.Object, serverConnection: serverConn.Object))
            {
                s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

                var ex = await Record.ExceptionAsync(() => s.AddUserAsync(username));

                Assert.NotNull(ex);
                Assert.IsType <TimeoutException>(ex);
            }
        }
Esempio n. 30
0
        public async Task GetUnsolicitedPeerConnectionAsync_Returns_New_Connection_If_Not_Existing(string name, IPAddress ipAddress, int port)
        {
            var options = new ConnectionOptions();

            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait <GetPeerAddressResponse>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(new GetPeerAddressResponse(name, ipAddress, port)));

            var serverConn = new Mock <IMessageConnection>();

            serverConn.Setup(m => m.WriteMessageAsync(It.IsAny <Message>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            var s = new SoulseekClient("127.0.0.1", 1, serverConnection: serverConn.Object, messageWaiter: waiter.Object);

            var conn = await s.InvokeMethod <Task <IMessageConnection> >("GetUnsolicitedPeerConnectionAsync", name, options, CancellationToken.None);

            Assert.NotNull(conn);
            Assert.Equal(name, conn.Username);
            Assert.Equal(ipAddress, conn.IPAddress);
            Assert.Equal(port, conn.Port);
            Assert.Equal(options, conn.Options);
        }