Ejemplo n.º 1
0
        public void MultiPartFileShouldBeDecoded()
        {
            const string expectedFileName = "joystick.jpg";

            byte[] expected = testData.GetEmbeddedFile(@"yenc.multipart.joystick.jpg").ReadAllBytes();

            YencArticle part1 = YencArticleDecoder.Decode(
                testData.GetEmbeddedFile(@"yenc.multipart.00000020.ntx").ReadAllLines(UsenetEncoding.Default));
            YencArticle part2 = YencArticleDecoder.Decode(
                testData.GetEmbeddedFile(@"yenc.multipart.00000021.ntx").ReadAllLines(UsenetEncoding.Default));

            using (var actual = new MemoryStream())
            {
                actual.Seek(part1.Header.PartOffset, SeekOrigin.Begin);
                actual.Write(part1.Data.ToArray(), 0, (int)part1.Header.PartSize);

                actual.Seek(part2.Header.PartOffset, SeekOrigin.Begin);
                actual.Write(part2.Data.ToArray(), 0, (int)part2.Header.PartSize);

                string actualFileName = part1.Header.FileName;

                Assert.Equal(expectedFileName, actualFileName);
                Assert.Equal(expected, actual.ToArray());
            }
        }
Ejemplo n.º 2
0
        public void ArticleShouldBeInvalid(string fileName, string errorCode)
        {
            YencArticle article = YencArticleDecoder.Decode(
                testData.GetEmbeddedFile(fileName).ReadAllLines(UsenetEncoding.Default));

            ValidationResult result = YencValidator.Validate(article);

            Assert.False(result.IsValid);
            Assert.Equal(errorCode, result.Failures.Single().Code);
        }
Ejemplo n.º 3
0
        public void ArticleShouldBeValid(string fileName)
        {
            YencArticle article = YencArticleDecoder.Decode(
                testData.GetEmbeddedFile(fileName).ReadAllLines(UsenetEncoding.Default));


            bool actual = YencValidator.Validate(article).IsValid;

            Assert.True(actual);
        }
Ejemplo n.º 4
0
        public void FilePartShouldBeDecoded()
        {
            const int expectedDataLength = 11250;

            YencArticle actualArticle = YencArticleDecoder.Decode(
                testData.GetEmbeddedFile(@"yenc.multipart.00000020.ntx").ReadAllLines(UsenetEncoding.Default));

            Assert.True(actualArticle.Header.IsFilePart);
            Assert.Equal(expectedDataLength, actualArticle.Data.Length);
        }
Ejemplo n.º 5
0
        public void SinglePartFileShouldBeDecoded()
        {
            byte[] expectedData = testData.GetEmbeddedFile(@"yenc.singlepart.testfile.txt").ReadAllBytes();

            YencArticle actualArticle = YencArticleDecoder.Decode(
                testData.GetEmbeddedFile(@"yenc.singlepart.00000005.ntx").ReadAllLines(UsenetEncoding.Default));

            Assert.False(actualArticle.Header.IsFilePart);
            Assert.Equal(128, actualArticle.Header.LineLength);
            Assert.Equal(584, actualArticle.Header.FileSize);
            Assert.Equal("testfile.txt", actualArticle.Header.FileName);
            Assert.Equal(584, actualArticle.Footer.PartSize);
            Assert.Equal("ded29f4f", ((int)actualArticle.Footer.Crc32.GetValueOrDefault()).ToString("x"));
            Assert.Equal(expectedData, actualArticle.Data);
        }
Ejemplo n.º 6
0
        private static void TestDownloadNzb(NntpClient client, string nzbFileName)
        {
            string fullPath = Path.Combine(nzbFileName);
            string nzbData = File.ReadAllText(fullPath, UsenetEncoding.Default);
            NzbDocument nzbDocument = NzbParser.Parse(nzbData);

            string downloadDir = Path.Combine("downloads", nzbFileName);
            Directory.CreateDirectory(downloadDir);

            log.LogInformation("Downloading nzb {nzbFileName}", nzbFileName);
            foreach (NzbFile file in nzbDocument.Files)
            {
                log.LogInformation("Downloading file {subject}", file.Subject);
                foreach (NzbSegment segment in file.Segments)
                {
                    log.LogInformation("Downloading article {messageId}", segment.MessageId);
                    NntpArticleResponse response = client.Article(segment.MessageId);
                    YencArticle decodedArticle = YencArticleDecoder.Decode(response.Article.Body);
                    YencHeader header = decodedArticle.Header;

                    string fileName = Path.Combine(downloadDir, header.FileName);
                    if (!File.Exists(fileName))
                    {
                        log.LogInformation("Creating file {fileName}", fileName);
                        // create file and pre-allocate disk space for it
                        using (FileStream stream = File.Create(fileName))
                        {
                            stream.SetLength(decodedArticle.Header.FileSize);
                        }
                    }

                    log.LogInformation("Writing {size} bytes to file {fileName} at offset {offset}",
                        header.FileSize, fileName, header.PartOffset);

                    using (FileStream stream = File.Open(
                        fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
                    {
                        stream.Seek(header.PartOffset, SeekOrigin.Begin);
                        stream.Write(decodedArticle.Data, 0, decodedArticle.Data.Length);
                    }
                }
            }
        }