public void Parse_Handles_Empty_Directory() { var name = Guid.NewGuid().ToString(); var msg = new MessageBuilder() .Code(MessageCode.PeerBrowseResponse) .WriteInteger(1) // directory count .WriteString(name) // first directory name .WriteInteger(0) // first directory file count .Compress() .Build(); BrowseResponse r = default(BrowseResponse); var ex = Record.Exception(() => r = BrowseResponse.Parse(msg)); Assert.Null(ex); Assert.Equal(1, r.DirectoryCount); Assert.Single(r.Directories); var d = r.Directories.ToList(); Assert.Equal(name, d[0].Directoryname); Assert.Equal(0, d[0].FileCount); Assert.Empty(d[0].Files); }
public void Parse_Handles_A_Complete_Response() { var dirs = new List <Directory>(); for (int i = 0; i < 5; i++) { dirs.Add(GetRandomDirectory(i)); } var builder = new MessageBuilder() .Code(MessageCode.PeerBrowseResponse) .WriteInteger(dirs.Count); foreach (var dir in dirs) { BuildDirectory(builder, dir); } var msg = builder .Compress() .Build(); BrowseResponse r = default(BrowseResponse); var ex = Record.Exception(() => r = BrowseResponse.Parse(msg)); Assert.Null(ex); Assert.Equal(dirs.Count, r.DirectoryCount); Assert.Equal(dirs.Count, r.Directories.Count); var msgDirs = r.Directories.ToList(); for (int i = 0; i < msgDirs.Count; i++) { Assert.Equal(dirs[i].Directoryname, msgDirs[i].Directoryname); Assert.Equal(dirs[i].FileCount, msgDirs[i].FileCount); var files = dirs[i].Files.ToList(); var msgFiles = msgDirs[i].Files.ToList(); for (int j = 0; j < msgDirs[i].FileCount; j++) { Assert.Equal(files[j].Code, msgFiles[j].Code); Assert.Equal(files[j].Filename, msgFiles[j].Filename); Assert.Equal(files[j].Size, msgFiles[j].Size); Assert.Equal(files[j].Extension, msgFiles[j].Extension); Assert.Equal(files[j].AttributeCount, msgFiles[j].AttributeCount); var attr = files[j].Attributes.ToList(); var msgAttr = files[j].Attributes.ToList(); for (int k = 0; k < msgFiles[j].AttributeCount; k++) { Assert.Equal(attr[k].Type, msgAttr[k].Type); Assert.Equal(attr[k].Value, msgAttr[k].Value); } } } }
public void Parse_Throws_MessageException_On_Code_Mismatch() { var msg = new MessageBuilder() .Code(MessageCode.PeerTransferResponse) .Build(); var ex = Record.Exception(() => BrowseResponse.Parse(msg)); Assert.NotNull(ex); Assert.IsType <MessageException>(ex); }
public void Parse_Throws_MessageCompressionException_On_Uncompressed_Payload() { var msg = new MessageBuilder() .Code(MessageCode.PeerBrowseResponse) .WriteBytes(new byte[] { 0x0, 0x1, 0x2, 0x3 }) .Build(); var ex = Record.Exception(() => BrowseResponse.Parse(msg)); Assert.NotNull(ex); Assert.IsType <MessageCompressionException>(ex); Assert.IsType <ZStreamException>(ex.InnerException); }
public void Parse_Returns_Empty_Response_Given_Empty_Message() { var msg = new MessageBuilder() .Code(MessageCode.PeerBrowseResponse) .WriteInteger(0) .Compress() .Build(); BrowseResponse r = default(BrowseResponse); var ex = Record.Exception(() => r = BrowseResponse.Parse(msg)); Assert.Null(ex); Assert.Equal(0, r.DirectoryCount); Assert.Empty(r.Directories); }
public void Parse_Throws_MessageReadException_On_Missing_Data() { var name = Guid.NewGuid().ToString(); var msg = new MessageBuilder() .Code(MessageCode.PeerBrowseResponse) .WriteInteger(1) // directory count .WriteString(name) // first directory name // missing count .Compress() .Build(); BrowseResponse r = default(BrowseResponse); var ex = Record.Exception(() => r = BrowseResponse.Parse(msg)); Assert.NotNull(ex); Assert.IsType <MessageReadException>(ex); }
public void Parse_Handles_Files_With_No_Attributes() { var name = Guid.NewGuid().ToString(); var msg = new MessageBuilder() .Code(MessageCode.PeerBrowseResponse) .WriteInteger(1) // directory count .WriteString(name) // first directory name .WriteInteger(1) // first directory file count .WriteByte(0x0) // file code .WriteString("foo") // name .WriteLong(12) // size .WriteString("bar") // extension .WriteInteger(0) // attribute count .Compress() .Build(); BrowseResponse r = default(BrowseResponse); var ex = Record.Exception(() => r = BrowseResponse.Parse(msg)); Assert.Null(ex); Assert.Equal(1, r.DirectoryCount); Assert.Single(r.Directories); var d = r.Directories.ToList(); Assert.Equal(name, d[0].Directoryname); Assert.Equal(1, d[0].FileCount); Assert.Single(d[0].Files); var f = d[0].Files.ToList(); Assert.Equal(0x0, f[0].Code); Assert.Equal("foo", f[0].Filename); Assert.Equal(12, f[0].Size); Assert.Equal("bar", f[0].Extension); Assert.Equal(0, f[0].AttributeCount); Assert.Empty(f[0].Attributes); }
public async void HandleMessage(object sender, byte[] message) { var connection = (IMessageConnection)sender; var code = new MessageReader <MessageCode.Peer>(message).ReadCode(); Diagnostic.Debug($"Peer message received: {code} from {connection.Username} ({connection.IPAddress}:{connection.Port})"); try { switch (code) { case MessageCode.Peer.SearchResponse: var searchResponse = SearchResponseSlim.Parse(message); if (SoulseekClient.Searches.TryGetValue(searchResponse.Token, out var search)) { search.AddResponse(searchResponse); } break; case MessageCode.Peer.BrowseResponse: var browseWaitKey = new WaitKey(MessageCode.Peer.BrowseResponse, connection.Username); try { SoulseekClient.Waiter.Complete(browseWaitKey, BrowseResponse.Parse(message)); } catch (Exception ex) { SoulseekClient.Waiter.Throw(browseWaitKey, new MessageReadException("The peer returned an invalid browse response.", ex)); throw; } break; case MessageCode.Peer.InfoResponse: var infoResponse = UserInfoResponse.Parse(message); SoulseekClient.Waiter.Complete(new WaitKey(MessageCode.Peer.InfoResponse, connection.Username), infoResponse); break; case MessageCode.Peer.TransferResponse: var transferResponse = TransferResponse.Parse(message); Console.WriteLine($"Got response from {connection.Username}: {transferResponse.Token}"); SoulseekClient.Waiter.Complete(new WaitKey(MessageCode.Peer.TransferResponse, connection.Username, transferResponse.Token), transferResponse); break; case MessageCode.Peer.QueueDownload: // the end state here is to wait until there's actually a free slot, then send this request to the peer to // let them know we are ready to start the actual transfer. var queueDownloadRequest = QueueDownloadRequest.Parse(message); var(queueAllowed, queueRejectionMessage) = SoulseekClient.Resolvers.QueueDownloadResponse(connection.Username, connection.IPAddress, connection.Port, queueDownloadRequest.Filename); if (!queueAllowed) { await connection.WriteAsync(new QueueFailedResponse(queueDownloadRequest.Filename, queueRejectionMessage)).ConfigureAwait(false); } break; case MessageCode.Peer.TransferRequest: var transferRequest = TransferRequest.Parse(message); if (transferRequest.Direction == TransferDirection.Upload) { SoulseekClient.Waiter.Complete(new WaitKey(MessageCode.Peer.TransferRequest, connection.Username, transferRequest.Filename), transferRequest); } else { var(transferAllowed, transferRejectionMessage) = SoulseekClient.Resolvers.QueueDownloadResponse(connection.Username, connection.IPAddress, connection.Port, transferRequest.Filename); if (!transferAllowed) { await connection.WriteAsync(new TransferResponse(transferRequest.Token, transferRejectionMessage)).ConfigureAwait(false); await connection.WriteAsync(new QueueFailedResponse(transferRequest.Filename, transferRejectionMessage)).ConfigureAwait(false); } else { await connection.WriteAsync(new TransferResponse(transferRequest.Token, "Queued.")).ConfigureAwait(false); } } break; case MessageCode.Peer.QueueFailed: var queueFailedResponse = QueueFailedResponse.Parse(message); SoulseekClient.Waiter.Throw(new WaitKey(MessageCode.Peer.TransferRequest, connection.Username, queueFailedResponse.Filename), new TransferRejectedException(queueFailedResponse.Message)); break; case MessageCode.Peer.PlaceInQueueResponse: var placeInQueueResponse = PeerPlaceInQueueResponse.Parse(message); SoulseekClient.Waiter.Complete(new WaitKey(MessageCode.Peer.PlaceInQueueResponse, connection.Username, placeInQueueResponse.Filename), placeInQueueResponse); break; case MessageCode.Peer.UploadFailed: var uploadFailedResponse = PeerUploadFailedResponse.Parse(message); var msg = $"Download of {uploadFailedResponse.Filename} reported as failed by {connection.Username}."; var download = SoulseekClient.Downloads.Values.FirstOrDefault(d => d.Username == connection.Username && d.Filename == uploadFailedResponse.Filename); if (download != null) { SoulseekClient.Waiter.Throw(new WaitKey(MessageCode.Peer.TransferRequest, download.Username, download.Filename), new TransferException(msg)); SoulseekClient.Waiter.Throw(download.WaitKey, new TransferException(msg)); } Diagnostic.Debug(msg); break; case MessageCode.Peer.BrowseRequest: var browseResponse = SoulseekClient.Resolvers.BrowseResponse(connection.Username, connection.IPAddress, connection.Port); await connection.WriteAsync(browseResponse).ConfigureAwait(false); break; default: Diagnostic.Debug($"Unhandled peer message: {code} from {connection.Username} ({connection.IPAddress}:{connection.Port}); {message.Length} bytes"); break; } } catch (Exception ex) { Diagnostic.Warning($"Error handling peer message: {code} from {connection.Username} ({connection.IPAddress}:{connection.Port}); {ex.Message}", ex); } }