public async Task WithBcl(string path)
        {
            // Arrange
            using (var stream = TestUtility.BufferTestData(path))
            {
                // Act
                var miniZip = await TestUtility.ReadWithMiniZipAsync(stream);

                var bcl = TestUtility.ReadWithBcl(stream);

                // Assert
                if (CasesHandledByBcl.TryGetValue(path, out var knownException))
                {
                    Assert.Equal(knownException.Type, miniZip.Exception.GetType());
                    Assert.Equal(knownException.Message, miniZip.Exception.Message);
                    Assert.True(bcl.Success);
                }
                else if (CasesNotHandledByBcl.TryGetValue(path, out knownException))
                {
                    Assert.Equal(knownException.Type, bcl.Exception.GetType());
                    Assert.Equal(knownException.Message, bcl.Exception.Message);
                    Assert.True(miniZip.Success);
                }
                else
                {
                    Assert.Equal(bcl.Success, miniZip.Success);
                    if (miniZip.Success)
                    {
                        Assert.Equal(bcl.Data.Count, miniZip.Data.Entries.Count);

                        var nameToBcl = bcl
                                        .Data
                                        .OrderBy(x => x.FullName)
                                        .ThenBy(x => x.CompressedLength)
                                        .ThenBy(x => x.Length)
                                        .ThenBy(x => x.ExternalAttributes)
                                        .ThenBy(x => x.LastWriteTime)
                                        .ToLookup(x => x.FullName);

                        var nameToMiniZip = miniZip
                                            .Data
                                            .Entries
                                            .OrderBy(x => x.GetName())
                                            .ThenBy(x => x.CompressedSize)
                                            .ThenBy(x => x.UncompressedSize)
                                            .ThenBy(x => x.ExternalAttributes)
                                            .ThenBy(x => x.GetLastModified())
                                            .ToLookup(x => x.GetName());

                        foreach (var name in nameToMiniZip.Select(x => x.Key))
                        {
                            var bclEntries     = nameToBcl[name].ToList();
                            var miniZipEntries = nameToMiniZip[name].ToList();

                            Assert.Equal(bclEntries.Count, miniZipEntries.Count);
                            for (var i = 0; i < bclEntries.Count; i++)
                            {
                                Assert.Equal((ulong)bclEntries[i].CompressedLength, miniZipEntries[i].GetCompressedSize());
                                Assert.Equal((ulong)bclEntries[i].Length, miniZipEntries[i].GetUncompressedSize());
                                Assert.Equal((uint)bclEntries[i].ExternalAttributes, miniZipEntries[i].ExternalAttributes);
                                Assert.Equal(bclEntries[i].LastWriteTime.DateTime, miniZipEntries[i].GetLastModified());
                            }
                        }
                    }
                }
            }
        }
Beispiel #2
0
            public async Task HandlesChangingETagProperly(ETagBehavior etagBehavior, bool success)
            {
                // Arrange
                using (var directory = TestDirectory.Create())
                {
                    var fileName  = "normal.zip";
                    var serverDir = Path.Combine(directory, TestUtility.TestServerDirectory);
                    var filePath  = Path.Combine(serverDir, fileName);
                    Directory.CreateDirectory(Path.GetDirectoryName(filePath));

                    using (var server = TestUtility.GetTestServer(
                               serverDir,
                               etags: true,
                               middleware: async(context, next) =>
                    {
                        await next.Invoke();

                        File.SetLastWriteTimeUtc(filePath, DateTime.UtcNow);
                    }))
                        using (var client = server.CreateClient())
                        {
                            File.Copy(
                                Path.Combine(TestUtility.TestDataDirectory, "System.IO.Compression/refzipfiles/normal.zip"),
                                filePath);
                            var requestUri = new Uri(new Uri(server.BaseAddress, TestUtility.TestServerDirectory + "/"), fileName);
                            var target     = new HttpZipProvider(client)
                            {
                                ETagBehavior = etagBehavior
                            };
                            var reader = await target.GetReaderAsync(requestUri);

                            // Act & Assert
                            if (success)
                            {
                                var actual = await reader.ReadAsync();

                                var expected = await TestUtility.ReadWithMiniZipAsync(TestUtility.BufferTestData(filePath));

                                TestUtility.VerifyJsonEquals(expected.Data, actual);
                            }
                            else
                            {
                                var ex = await Assert.ThrowsAsync <MiniZipHttpStatusCodeException>(() => reader.ReadAsync());

                                Assert.Equal(
                                    "The HTTP response did not have the expected status code HTTP 206 Partial Content. The response was 412 Precondition Failed.",
                                    ex.Message);
                            }
                        }
                }
            }
        public async Task WithSharpZipLib(string path)
        {
            // Arrange
            using (var stream = TestUtility.BufferTestData(path))
            {
                // Act
                var miniZip = await TestUtility.ReadWithMiniZipAsync(stream);

                var sharpZipLib = TestUtility.ReadWithSharpZipLib(stream);

                // Assert
                if (CasesHandledBySharpZipLib.TryGetValue(path, out var knownException))
                {
                    Assert.Equal(knownException.Type, miniZip.Exception.GetType());
                    Assert.Equal(knownException.Message, miniZip.Exception.Message);
                    Assert.True(sharpZipLib.Success);
                }
                else
                {
                    Assert.Equal(sharpZipLib.Success, miniZip.Success);
                    if (miniZip.Success)
                    {
                        Assert.Equal(sharpZipLib.Data.Count, miniZip.Data.Entries.Count);

                        var nameToSharpZipLib = sharpZipLib
                                                .Data
                                                .OrderBy(x => x.Name)
                                                .ThenBy(x => x.CompressedSize)
                                                .ThenBy(x => x.Size)
                                                .ThenBy(x => x.ExternalFileAttributes)
                                                .ThenBy(x => x.DateTime)
                                                .ToLookup(x => x.Name);

                        var nameToMiniZip = miniZip
                                            .Data
                                            .Entries
                                            .OrderBy(x => x.GetName())
                                            .ThenBy(x => x.CompressedSize)
                                            .ThenBy(x => x.UncompressedSize)
                                            .ThenBy(x => x.ExternalAttributes)
                                            .ThenBy(x => x.GetLastModified())
                                            .ToLookup(x => x.GetName());

                        foreach (var name in nameToMiniZip.Select(x => x.Key))
                        {
                            var sharpZipLibEntries = nameToSharpZipLib[name].ToList();
                            var miniZipEntries     = nameToMiniZip[name].ToList();

                            Assert.Equal(sharpZipLibEntries.Count, miniZipEntries.Count);
                            for (var i = 0; i < sharpZipLibEntries.Count; i++)
                            {
                                Assert.Equal((ulong)sharpZipLibEntries[i].CompressedSize, miniZipEntries[i].GetCompressedSize());
                                Assert.Equal((ulong)sharpZipLibEntries[i].Size, miniZipEntries[i].GetUncompressedSize());
                                Assert.Equal((uint)sharpZipLibEntries[i].ExternalFileAttributes, miniZipEntries[i].ExternalAttributes);

                                var sharpZipLibLastModified = sharpZipLibEntries[i].DateTime;
                                var miniZipLastModified     = miniZipEntries[i].GetLastModified();
                                if (!LastModifiedDifferencesFromSharpZipLib.Contains(path))
                                {
                                    Assert.Equal(sharpZipLibLastModified, miniZipLastModified);
                                }
                            }
                        }
                    }
                }
            }
        }