public async Task WhenCreateBlankVhdThenDataIzZeroFilled() { // arrange var path = $"{Guid.NewGuid()}.vhd"; var size = 512 * 512; var fakeCommandHelper = new FakeCommandHelper(); var cancellationTokenSource = new CancellationTokenSource(); // act - create blank var blankCommand = new BlankCommand(new NullLogger <BlankCommand>(), fakeCommandHelper, path, size); var result = await blankCommand.Execute(cancellationTokenSource.Token); Assert.True(result.IsSuccess); // get destination bytes from vhd var destinationBytes = await ReadMediaBytes(fakeCommandHelper, path, size); var destinationPathSize = new FileInfo(path).Length; // assert vhd is less than size Assert.True(destinationPathSize < size); // assert data is zero filled var sourceBytes = new byte[size]; Assert.Equal(sourceBytes, destinationBytes); // delete vhd file File.Delete(path); }
public async ValueTask Handle(IBackgroundTaskContext context) { if (context.BackgroundTask is not BlankBackgroundTask blankBackgroundTask) { return; } try { await progressHubContext.SendProgress(new Progress { Title = blankBackgroundTask.Title, IsComplete = false, PercentComplete = 50, }, context.Token); var commandHelper = new CommandHelper(); var blankCommand = new BlankCommand(loggerFactory.CreateLogger <BlankCommand>(), commandHelper, blankBackgroundTask.Path, blankBackgroundTask.CompatibleSize ? Convert.ToInt64(blankBackgroundTask.Size * 0.95) : blankBackgroundTask.Size); var result = await blankCommand.Execute(context.Token); await Task.Delay(1000, context.Token); await progressHubContext.SendProgress(new Progress { Title = blankBackgroundTask.Title, IsComplete = true, HasError = result.IsFaulted, ErrorMessage = result.IsFaulted ? result.Error.Message : null, PercentComplete = 100 }, context.Token); } catch (Exception e) { await progressHubContext.SendProgress(new Progress { Title = blankBackgroundTask.Title, IsComplete = true, HasError = true, ErrorMessage = e.Message, PercentComplete = 100 }, context.Token); } }
public async Task WhenCreateBlankImgThenDataIzZeroFilled() { // arrange var path = $"{Guid.NewGuid()}.img"; var size = 512 * 512; var fakeCommandHelper = new FakeCommandHelper(writeableMediaPaths: new[] { path }); var cancellationTokenSource = new CancellationTokenSource(); // act - create blank var blankCommand = new BlankCommand(new NullLogger <BlankCommand>(), fakeCommandHelper, path, size); var result = await blankCommand.Execute(cancellationTokenSource.Token); Assert.True(result.IsSuccess); // assert data is zero filled var sourceBytes = new byte[size]; var destinationBytes = fakeCommandHelper.GetMedia(path).GetBytes(); Assert.Equal(sourceBytes, destinationBytes); }
static async Task Run(Arguments arguments) { Log.Logger = new LoggerConfiguration() .Enrich.FromLogContext() .WriteTo.Console() .CreateLogger(); var serviceProvider = new ServiceCollection() .AddLogging(loggingBuilder => loggingBuilder.AddSerilog(dispose: true)) .BuildServiceProvider(); var loggerFactory = serviceProvider.GetService <ILoggerFactory>(); var isAdministrator = OperatingSystem.IsAdministrator(); if (!isAdministrator) { Console.WriteLine("Requires administrator rights!"); } var commandHelper = new CommandHelper(); var physicalDrives = (await GetPhysicalDrives(arguments)).ToList(); var cancellationTokenSource = new CancellationTokenSource(); switch (arguments.Command) { case Arguments.CommandEnum.List: var listCommand = new ListCommand(loggerFactory.CreateLogger <ListCommand>(), commandHelper, physicalDrives); listCommand.ListRead += (_, args) => { // // await Task.Run(() => // { // Console.WriteLine(JsonSerializer.Serialize(physicalDrivesList, JsonSerializerOptions)); // }); InfoPresenter.PresentInfo(args.MediaInfos); }; var listResult = await listCommand.Execute(cancellationTokenSource.Token); Console.WriteLine(listResult.IsSuccess ? "Done" : $"ERROR: Read failed, {listResult.Error}"); break; case Arguments.CommandEnum.Info: var infoCommand = new InfoCommand(loggerFactory.CreateLogger <InfoCommand>(), commandHelper, physicalDrives, arguments.SourcePath); infoCommand.DiskInfoRead += (_, args) => { InfoPresenter.PresentInfo(args.MediaInfo); }; var infoResult = await infoCommand.Execute(cancellationTokenSource.Token); Console.WriteLine(infoResult.IsSuccess ? "Done" : $"ERROR: Read failed, {infoResult.Error}"); break; case Arguments.CommandEnum.Read: Console.WriteLine("Reading physical drive to image file"); GenericPresenter.PresentPaths(arguments); var readCommand = new ReadCommand(loggerFactory.CreateLogger <ReadCommand>(), commandHelper, physicalDrives, arguments.SourcePath, arguments.DestinationPath, arguments.Size); readCommand.DataProcessed += (_, args) => { GenericPresenter.Present(args); }; var readResult = await readCommand.Execute(cancellationTokenSource.Token); Console.WriteLine(readResult.IsSuccess ? "Done" : $"ERROR: Read failed, {readResult.Error}"); break; case Arguments.CommandEnum.Convert: Console.WriteLine("Converting source image to destination image file"); GenericPresenter.PresentPaths(arguments); var convertCommand = new ConvertCommand(loggerFactory.CreateLogger <ConvertCommand>(), commandHelper, arguments.SourcePath, arguments.DestinationPath, arguments.Size); convertCommand.DataProcessed += (_, args) => { GenericPresenter.Present(args); }; var convertResult = await convertCommand.Execute(cancellationTokenSource.Token); Console.WriteLine( convertResult.IsSuccess ? "Done" : $"ERROR: Convert failed, {convertResult.Error}"); break; case Arguments.CommandEnum.Write: Console.WriteLine("Writing source image file to physical drive"); GenericPresenter.PresentPaths(arguments); var writeCommand = new WriteCommand(loggerFactory.CreateLogger <WriteCommand>(), commandHelper, physicalDrives, arguments.SourcePath, arguments.DestinationPath, arguments.Size); writeCommand.DataProcessed += (_, args) => { GenericPresenter.Present(args); }; var writeResult = await writeCommand.Execute(cancellationTokenSource.Token); Console.WriteLine(writeResult.IsSuccess ? "Done" : $"ERROR: Write failed, {writeResult.Error}"); break; case Arguments.CommandEnum.Verify: Console.WriteLine("Verifying source image to destination"); GenericPresenter.PresentPaths(arguments); var verifyCommand = new VerifyCommand(loggerFactory.CreateLogger <VerifyCommand>(), commandHelper, physicalDrives, arguments.SourcePath, arguments.DestinationPath, arguments.Size); verifyCommand.DataProcessed += (_, args) => { GenericPresenter.Present(args); }; var verifyResult = await verifyCommand.Execute(cancellationTokenSource.Token); Console.WriteLine(verifyResult.IsSuccess ? "Done" : $"ERROR: Verify failed, {verifyResult.Error}"); break; case Arguments.CommandEnum.Blank: Console.WriteLine("Creating blank image"); Console.WriteLine($"Path: {arguments.SourcePath}"); var blankCommand = new BlankCommand(loggerFactory.CreateLogger <BlankCommand>(), commandHelper, arguments.SourcePath, arguments.Size); var blankResult = await blankCommand.Execute(cancellationTokenSource.Token); Console.WriteLine(blankResult.IsSuccess ? "Done" : $"ERROR: Blank failed, {blankResult.Error}"); break; case Arguments.CommandEnum.Optimize: Console.WriteLine("Optimizing image file"); Console.WriteLine($"Path: {arguments.SourcePath}"); var optimizeCommand = new OptimizeCommand(loggerFactory.CreateLogger <OptimizeCommand>(), commandHelper, arguments.SourcePath); var optimizeResult = await optimizeCommand.Execute(cancellationTokenSource.Token); Console.WriteLine(optimizeResult.IsSuccess ? "Done" : $"ERROR: Optimize failed, {optimizeResult.Error}"); break; } }