public async Task CreateZipAsync_SystemTest_NewZip()
        {
            var zipPath = @"TestContainer\Create.zip";

            if (File.Exists(zipPath))
            {
                File.Delete(zipPath);
            }

            var cancellationToken = CancellationToken.None;
            var outputFileName    = "Create.zip";
            var container         = "TestContainer";

            var fileNames = new List <string>
            {
                "TestFile1.csv",
                "TestFile2.csv",
                "TestFile3.csv"
            };

            var fileService        = new FileSystemFileService();
            var zipArchivieService = new ZipArchiveService();

            await NewService(fileService, zipArchivieService).CreateZipAsync(outputFileName, fileNames, container, cancellationToken);

            using (Stream stream = new FileStream(zipPath, FileMode.Open))
            {
                var count = new ZipArchive(stream, ZipArchiveMode.Read).Entries.Count;
                count.Should().Be(3);
            }
        }
Exemplo n.º 2
0
            public void NullCheck()
            {
                IReadOnlyCollection <ZipArchiveEntry> input = null;

                // Act + Assert
                Assert.Throws <ArgumentNullException>(() => ZipArchiveService.ReadFilesFromZipStream(input, ".exe"));
            }
        public async Task CreateZipAsync_SystemTest_UpdateZip()
        {
            var cancellationToken = CancellationToken.None;
            var outputFileName    = "Update.zip";
            var container         = "TestContainer";

            var fileNames = new List <string>
            {
                "TestFile1.csv",
                "TestFile2.csv",
                "TestFile3.csv"
            };

            var fileService        = new FileSystemFileService();
            var zipArchivieService = new ZipArchiveService();

            int countBefore;
            int countAfter;

            using (Stream stream = new FileStream(@"TestContainer\Update.zip", FileMode.Open))
            {
                countBefore = new ZipArchive(stream, ZipArchiveMode.Read).Entries.Count;
            }

            await NewService(fileService, zipArchivieService).CreateZipAsync(outputFileName, fileNames, container, cancellationToken);

            using (Stream stream = new FileStream(@"TestContainer\Update.zip", FileMode.Open))
            {
                countAfter = new ZipArchive(stream, ZipArchiveMode.Read).Entries.Count;
            }

            countBefore.Should().Be(1);
            countAfter.Should().Be(3);
        }
Exemplo n.º 4
0
            public void NullChecks()
            {
                IEnumerable <string> input = null;

                // Act + Assert
                Assert.Null(ZipArchiveService.RemoveExtension(input));
            }
Exemplo n.º 5
0
            public void NullCheck()
            {
                // Arrange
                IReadOnlyCollection <ZipArchiveEntry> input = new ReadOnlyCollection <ZipArchiveEntry>(new List <ZipArchiveEntry>());
                var service = new ZipArchiveService();

                // Act + Assert
                Assert.Throws <ArgumentNullException>(() => service.Extract(null, "Dir1", new List <string> {
                    ".pdb"
                }));
                Assert.Throws <ArgumentNullException>(() => ZipArchiveService.ReadFilesFromZipStream(input, null));
            }
Exemplo n.º 6
0
            public void TheFileExtensionsAreRemoved()
            {
                IEnumerable <string> input = new string[] { "foo1.exe", "foo2.pdb", "foo3.dll", @"a\foo1.exe", @"a\foo2.pdb" };

                string[] expected = new string[] { "foo1", "foo2", "foo3", @"a\foo1", @"a\foo2" };

                // Act
                var result = ZipArchiveService.RemoveExtension(input);

                Assert.Equal(expected.Length, result.Count());
                foreach (string s in expected)
                {
                    Assert.Contains(s, result);
                }
            }
        /// <summary>
        /// Based on the snupkg, nupkg folder structure validate that the symbols have associated binary files.
        /// </summary>
        /// <param name="symbols">Symbol list extracted from the compressed folder.</param>
        /// <param name="PEs">The list of PE files extracted from the compressed folder.</param>
        /// <returns></returns>
        public static bool SymbolsHaveMatchingPEFiles(IEnumerable <string> symbols, IEnumerable <string> PEs)
        {
            if (symbols == null)
            {
                throw new ArgumentNullException(nameof(symbols));
            }
            if (PEs == null)
            {
                throw new ArgumentNullException(nameof(PEs));
            }
            var symbolsWithoutExtension = ZipArchiveService.RemoveExtension(symbols);
            var PEsWithoutExtensions    = ZipArchiveService.RemoveExtension(PEs);

            return(!symbolsWithoutExtension.Except(PEsWithoutExtensions, StringComparer.OrdinalIgnoreCase).Any());
        }
Exemplo n.º 8
0
            public async Task ValidateSymbolsAsyncWillFailIfSnupkgIsNotSafeForExtract()
            {
                // Arrange
                _symbolsFileService.
                Setup(sfs => sfs.DownloadSnupkgFileAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())).
                ReturnsAsync(CreateZipSlipStream());
                var zipService = new ZipArchiveService(new Mock <ILogger <ZipArchiveService> >().Object);
                var service    = new TestSymbolsValidatorService(_symbolsFileService.Object, zipService, _telemetryService.Object, _logger.Object);

                // Act
                var result = await service.ValidateSymbolsAsync(Message, CancellationToken.None);

                // Assert
                Assert.Equal(ValidationResult.Failed.Status, result.Status);
                Assert.Equal(1, result.Issues.Count);
            }
Exemplo n.º 9
0
            public void TheCorrectExtensionsAreFiltered()
            {
                // Arrange
                IReadOnlyCollection <ZipArchiveEntry> input = CreateTestZipEntries(new string[] { "foo.pdb", "bar.txt", "foobar.dll", @"a\bar.pdb" });

                string[] expected = new string[] { "foo.pdb", @"a\bar.pdb" };

                // Act
                var result = ZipArchiveService.ReadFilesFromZipStream(input, ".pdb");

                // Assert
                Assert.Equal(expected.Length, result.Count());
                foreach (string s in expected)
                {
                    Assert.Contains(s, result);
                }
            }
Exemplo n.º 10
0
        public async Task ZipArchiveServiceCompressTestAsync()
        {
            ICompressionService service = new ZipArchiveService(new LocalStorageService());

            var file = await storageService.CreateOrGetFileAsync("data.zip");

            var folder = await storageService.CreateOrGetFolderAsync("Data");

            var result = await service.CompressAsync(folder, file);

            Assert.IsTrue(result);

            // Unzip
            var unzipFolder = await storageService.CreateOrGetFolderAsync("UnzipedData");

            result = await service.UncompressAsync(file, unzipFolder);

            Assert.IsTrue(result);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Initialize object.
        /// </summary>
        public OneDriveViewModel()
        {
            // Init services.
            oneDriveService     = new OneDriveStorageService();
            localStorageService = new LocalStorageService();
            zipArchiveService   = new ZipArchiveService(localStorageService);

            // Init commands.
            SignInRetryCommand = new DelegateCommand(async() =>
            {
                await AuthenticateAsync();
                await ListBackupAsync();
            },
                                                     () =>
            {
                if (SignInIsIndeterminate)
                {
                    return(false);
                }

                if (IsAuthenticated)
                {
                    return(false);
                }
                return(true);
            });

            UploadCommand = new DelegateCommand(async() =>
            {
                await UploadBackupAsync();
                await ListBackupAsync();
            },
                                                () =>
            {
                // Maybe add length between 3 and 20
                if (UploadName.Length == 0)
                {
                    UploadValidationText = "Your backup needs a name.";
                    return(false);
                }

                if (oneDriveService.OneDriveReservedCharacters.Count(a => UploadName.Contains(a)) > 0)
                {
                    UploadValidationText = @"Contains reserved char /\<>:? or |.";
                    return(false);
                }

                UploadValidationText = string.Empty;
                return(true);
            });


            DownloadCommand = new DelegateCommand(async() =>
            {
                await DownloadBackupAsync();
            },
                                                  () =>
            {
                // Maybe add length between 3 and 20
                if (SelectedOneDriveItem == null)
                {
                    return(false);
                }

                return(true);
            });

            // Init properties.
            IsAuthenticated      = false;
            SignInPaneVisibility = Visibility.Visible;
            SignInStatusText     = "Could not sign in automatically. Please retry.";

            oneDriveItems        = new ObservableCollection <Item>();
            SelectedOneDriveItem = null;

            DataPath   = "Data";
            BackupPath = @"Apps/UWPCore.Demo/Backup/";
            UploadPath = BackupPath;
            UploadName = string.Empty;

            BackupIsIndeterminate = false;
            BackupStatusText      = string.Empty;

            SignInRetryCommand.RaiseCanExecuteChanged();
            UploadCommand.RaiseCanExecuteChanged();
            DownloadCommand.RaiseCanExecuteChanged();
        }