예제 #1
0
        public async Task WhenVerifySourceIsLargerThanDestinationThenResultIsSizeNotEqualError()
        {
            // arrange
            var sourcePath              = $"{Guid.NewGuid()}.img";
            var destinationPath         = $"{Guid.NewGuid()}.img";
            var fakeCommandHelper       = new FakeCommandHelper();
            var cancellationTokenSource = new CancellationTokenSource();
            var testDataBytes           = fakeCommandHelper.CreateTestData();

            // create source
            fakeCommandHelper.ReadableMedias.Add(new Media(sourcePath, sourcePath, testDataBytes.Length * 2,
                                                           Media.MediaType.Raw, false,
                                                           new MemoryStream(testDataBytes.Concat(testDataBytes).ToArray())));

            // create destination
            fakeCommandHelper.ReadableMedias.Add(new Media(destinationPath, destinationPath, testDataBytes.Length,
                                                           Media.MediaType.Raw, false,
                                                           new MemoryStream(testDataBytes)));

            // act - verify source img to destination img
            var verifyCommand =
                new VerifyCommand(new NullLogger <VerifyCommand>(), fakeCommandHelper, new List <IPhysicalDrive>(),
                                  sourcePath, destinationPath);
            var result = await verifyCommand.Execute(cancellationTokenSource.Token);

            Assert.False(result.IsSuccess);

            Assert.Equal(typeof(SizeNotEqualError), result.Error.GetType());
            var sizeNotEqualError = (SizeNotEqualError)result.Error;

            Assert.Equal(0, sizeNotEqualError.Offset);
            Assert.Equal(testDataBytes.Length * 2, sizeNotEqualError.Size);
        }
예제 #2
0
        public async Task WhenListPhysicalDrivesThenListReadIsTriggered()
        {
            var physicalDrives = new[]
            {
                new FakePhysicalDrive("Path", "Type", "Model", 8192)
            };
            var fakeCommandHelper       = new FakeCommandHelper();
            var cancellationTokenSource = new CancellationTokenSource();

            var listCommand = new ListCommand(new NullLogger <ListCommand>(), fakeCommandHelper, physicalDrives);
            IEnumerable <MediaInfo> mediaInfos = null;

            listCommand.ListRead += (sender, args) =>
            {
                mediaInfos = args?.MediaInfos;
            };
            var result = await listCommand.Execute(cancellationTokenSource.Token);

            Assert.True(result.IsSuccess);

            var mediaInfosList = mediaInfos.ToList();

            Assert.Single(mediaInfosList);

            var mediaInfo = mediaInfosList.First();

            Assert.Equal("Path", mediaInfo.Path);
            Assert.Equal(Media.MediaType.Raw, mediaInfo.Type);
            Assert.True(mediaInfo.IsPhysicalDrive);
            Assert.Equal("Model", mediaInfo.Model);
            Assert.Equal(8192, mediaInfo.DiskSize);
            Assert.Null(mediaInfo.RigidDiskBlock);
        }
        public async Task WhenWriteSourceToImgDestinationThenReadDataIsIdentical()
        {
            // arrange
            var sourcePath              = $"{Guid.NewGuid()}.img";
            var destinationPath         = $"{Guid.NewGuid()}.img";
            var fakeCommandHelper       = new FakeCommandHelper(new[] { sourcePath }, new [] { destinationPath });
            var cancellationTokenSource = new CancellationTokenSource();

            // act - write source img to destination img
            var writeCommand =
                new WriteCommand(new NullLogger <WriteCommand>(), fakeCommandHelper, new List <IPhysicalDrive>(), sourcePath, destinationPath);
            DataProcessedEventArgs dataProcessedEventArgs = null;

            writeCommand.DataProcessed += (_, args) =>
            {
                dataProcessedEventArgs = args;
            };
            var result = await writeCommand.Execute(cancellationTokenSource.Token);

            Assert.True(result.IsSuccess);

            Assert.NotNull(dataProcessedEventArgs);
            Assert.NotEqual(0, dataProcessedEventArgs.PercentComplete);
            Assert.NotEqual(0, dataProcessedEventArgs.BytesProcessed);
            Assert.Equal(0, dataProcessedEventArgs.BytesRemaining);
            Assert.NotEqual(0, dataProcessedEventArgs.BytesTotal);

            // assert data is identical
            var sourceBytes      = fakeCommandHelper.GetMedia(sourcePath).GetBytes();
            var destinationBytes = fakeCommandHelper.GetMedia(destinationPath).GetBytes();

            Assert.Equal(sourceBytes, destinationBytes);
        }
예제 #4
0
        public async Task WhenVerifySourceSmallerThanDestinationThenDataIsIdentical()
        {
            // arrange
            var sourcePath              = $"{Guid.NewGuid()}.img";
            var destinationPath         = $"{Guid.NewGuid()}.img";
            var fakeCommandHelper       = new FakeCommandHelper();
            var cancellationTokenSource = new CancellationTokenSource();
            var testDataBytes           = fakeCommandHelper.CreateTestData();

            // create source
            fakeCommandHelper.ReadableMedias.Add(new Media(sourcePath, sourcePath, testDataBytes.Length,
                                                           Media.MediaType.Raw, false,
                                                           new MemoryStream(testDataBytes)));

            // create destination
            fakeCommandHelper.ReadableMedias.Add(new Media(destinationPath, destinationPath, testDataBytes.Length * 2,
                                                           Media.MediaType.Raw, false,
                                                           new MemoryStream(testDataBytes.Concat(testDataBytes).ToArray())));

            // act - verify source img to destination img
            var verifyCommand =
                new VerifyCommand(new NullLogger <VerifyCommand>(), fakeCommandHelper, new List <IPhysicalDrive>(),
                                  sourcePath, destinationPath);
            var result = await verifyCommand.Execute(cancellationTokenSource.Token);

            Assert.True(result.IsSuccess);
        }
        public async Task WhenConvertSourceToVhdDestinationThenReadDataIsIdentical()
        {
            // arrange
            var sourcePath              = $"{Guid.NewGuid()}.img";
            var destinationPath         = $"{Guid.NewGuid()}.vhd";
            var fakeCommandHelper       = new FakeCommandHelper(new [] { sourcePath }, new [] { destinationPath });
            var cancellationTokenSource = new CancellationTokenSource();

            // act - read source img to destination vhd
            var convertCommand = new ConvertCommand(new NullLogger <ConvertCommand>(), fakeCommandHelper, sourcePath, destinationPath);
            var result         = await convertCommand.Execute(cancellationTokenSource.Token);

            Assert.True(result.IsSuccess);

            // get source bytes
            var sourceBytes = fakeCommandHelper.GetMedia(sourcePath).GetBytes();

            // get destination bytes from vhd
            var destinationBytes = await ReadMediaBytes(fakeCommandHelper, destinationPath, sourceBytes.Length);

            var destinationPathSize = new FileInfo(destinationPath).Length;

            // assert length is not the same (vhd file format different than img) and bytes are the same
            Assert.NotEqual(sourceBytes.Length, destinationPathSize);
            Assert.Equal(sourceBytes, destinationBytes);

            // delete destination path vhd
            File.Delete(destinationPath);
        }
예제 #6
0
        public async Task WhenListPhysicalDrivesWithRigidDiskBlockThenListReadIsTriggered()
        {
            var path           = Path.Combine("TestData", "rigid-disk-block.img");
            var physicalDrives = new[]
            {
                new FakePhysicalDrive(path, "Type", "Model", await File.ReadAllBytesAsync(path))
            };
            var fakeCommandHelper       = new FakeCommandHelper(new[] { path });
            var cancellationTokenSource = new CancellationTokenSource();

            var listCommand = new ListCommand(new NullLogger <ListCommand>(), fakeCommandHelper, physicalDrives);
            IEnumerable <MediaInfo> mediaInfos = null;

            listCommand.ListRead += (sender, args) =>
            {
                mediaInfos = args?.MediaInfos;
            };
            var result = await listCommand.Execute(cancellationTokenSource.Token);

            Assert.True(result.IsSuccess);

            var mediaInfosList = mediaInfos.ToList();

            Assert.Single(mediaInfosList);

            var mediaInfo = mediaInfosList.First();

            Assert.Equal(path, mediaInfo.Path);
            Assert.Equal(Media.MediaType.Raw, mediaInfo.Type);
            Assert.True(mediaInfo.IsPhysicalDrive);
            Assert.Equal("Model", mediaInfo.Model);
            Assert.Equal(131072, mediaInfo.DiskSize);
            Assert.NotNull(mediaInfo.RigidDiskBlock);
        }
예제 #7
0
        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 Task WhenOptimizeImgWithRigidDiskBlockThenSizeIsChanged()
        {
            // arrange
            var path = $"{Guid.NewGuid()}.img";
            var rigidDiskBlockSize = 8192;
            var fakeCommandHelper  = new FakeCommandHelper(rigidDiskBlock: new RigidDiskBlock
            {
                DiskSize = rigidDiskBlockSize
            });

            // var bytes = fakeCommandHelper.CreateTestData();
            fakeCommandHelper.WriteableMedias.Add(new Media(path, path, rigidDiskBlockSize, Media.MediaType.Raw, false,
                                                            new MemoryStream(new byte[16384])));
            var cancellationTokenSource = new CancellationTokenSource();

            // optimize
            var optimizeCommand = new OptimizeCommand(new NullLogger <OptimizeCommand>(), fakeCommandHelper, path);
            var result          = await optimizeCommand.Execute(cancellationTokenSource.Token);

            Assert.True(result.IsSuccess);

            // assert media contains optimized rigid disk block size
            var optimizedBytes = fakeCommandHelper.GetMedia(path).GetBytes();

            Assert.Equal(rigidDiskBlockSize, optimizedBytes.Length);
        }
        public async Task WhenWriteSourceToVhdDestinationThenReadDataIsIdentical()
        {
            // arrange
            var sourcePath              = $"{Guid.NewGuid()}.img";
            var destinationPath         = $"{Guid.NewGuid()}.vhd";
            var fakeCommandHelper       = new FakeCommandHelper(new[] { sourcePath });
            var cancellationTokenSource = new CancellationTokenSource();

            // arrange destination vhd has copy of source img data
            var sourceBytes = fakeCommandHelper.GetMedia(sourcePath).GetBytes();

            // act - write source img to destination vhd
            var writeCommand = new WriteCommand(new NullLogger <WriteCommand>(), fakeCommandHelper, new List <IPhysicalDrive>(), sourcePath, destinationPath, sourceBytes.Length);
            var result       = await writeCommand.Execute(cancellationTokenSource.Token);

            Assert.True(result.IsSuccess);

            // delete destination path vhd
            File.Delete(destinationPath);
        }
예제 #10
0
        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);
        }
예제 #11
0
        public async Task WhenVerifySourceToImgDestinationWithDifferentBytesAtOffsetThenResultIsByteNotEqualError()
        {
            // arrange
            const int  offsetWithError         = 8390;
            const byte sourceByte              = 178;
            const byte destinationByte         = 250;
            var        sourcePath              = $"{Guid.NewGuid()}.img";
            var        destinationPath         = $"{Guid.NewGuid()}.img";
            var        fakeCommandHelper       = new FakeCommandHelper();
            var        cancellationTokenSource = new CancellationTokenSource();

            // create source
            var sourceBytes = fakeCommandHelper.CreateTestData();

            sourceBytes[offsetWithError] = sourceByte;
            fakeCommandHelper.ReadableMedias.Add(new Media(sourcePath, sourcePath, sourceBytes.Length,
                                                           Media.MediaType.Raw, false,
                                                           new MemoryStream(sourceBytes)));

            // create destination
            var destinationBytesWithError = fakeCommandHelper.CreateTestData();

            destinationBytesWithError[offsetWithError] = destinationByte;
            fakeCommandHelper.ReadableMedias.Add(new Media(destinationPath, destinationPath,
                                                           destinationBytesWithError.Length, Media.MediaType.Raw, false,
                                                           new MemoryStream(destinationBytesWithError)));

            // act - verify source img to destination img
            var verifyCommand =
                new VerifyCommand(new NullLogger <VerifyCommand>(), fakeCommandHelper, new List <IPhysicalDrive>(),
                                  sourcePath, destinationPath);
            var result = await verifyCommand.Execute(cancellationTokenSource.Token);

            Assert.False(result.IsSuccess);
            Assert.Equal(typeof(ByteNotEqualError), result.Error.GetType());
            var byteNotEqualError = (ByteNotEqualError)result.Error;

            Assert.Equal(offsetWithError, byteNotEqualError.Offset);
            Assert.Equal(sourceByte, byteNotEqualError.SourceByte);
            Assert.Equal(destinationByte, byteNotEqualError.DestinationByte);
        }
예제 #12
0
        public async Task WhenReadInfoFromSourceImgWithRigidDiskBlockThenDiskInfoIsReturned()
        {
            // arrange
            var path = Path.Combine("TestData", "rigid-disk-block.img");
            var fakeCommandHelper       = new FakeCommandHelper(new[] { path });
            var cancellationTokenSource = new CancellationTokenSource();

            // read info from path
            var       infoCommand = new InfoCommand(new NullLogger <InfoCommand>(), fakeCommandHelper, Enumerable.Empty <IPhysicalDrive>(), path);
            MediaInfo mediaInfo   = null;

            infoCommand.DiskInfoRead += (_, args) =>
            {
                mediaInfo = args.MediaInfo;
            };
            await infoCommand.Execute(cancellationTokenSource.Token);

            // assert media info
            Assert.NotNull(mediaInfo);
            //Assert.Equal(mediaInfo.DiskSize, FakeCommandHelper.ImageSize);
            Assert.NotNull(mediaInfo.RigidDiskBlock);
        }
예제 #13
0
        public async Task WhenVerifySourceToImgDestinationWithDifferentSizesThenResultIsSizeNotEqualError()
        {
            // arrange
            var sourcePath              = $"{Guid.NewGuid()}.img";
            var destinationPath         = $"{Guid.NewGuid()}.img";
            var fakeCommandHelper       = new FakeCommandHelper();
            var cancellationTokenSource = new CancellationTokenSource();

            // create source
            var sourceBytes = fakeCommandHelper.CreateTestData();

            fakeCommandHelper.ReadableMedias.Add(new Media(sourcePath, sourcePath, sourceBytes.Length,
                                                           Media.MediaType.Raw, false,
                                                           new MemoryStream(sourceBytes)));

            // create destination
            var destinationSize       = sourceBytes.Length / 2;
            var destinationBytesChunk = new byte[Convert.ToInt32(destinationSize)];

            Array.Copy(sourceBytes, 0, destinationBytesChunk, 0, destinationBytesChunk.Length);
            fakeCommandHelper.ReadableMedias.Add(new Media(destinationPath, destinationPath, destinationSize,
                                                           Media.MediaType.Raw, false,
                                                           new MemoryStream(destinationBytesChunk)));

            // act - verify source img to destination img
            var verifyCommand =
                new VerifyCommand(new NullLogger <VerifyCommand>(), fakeCommandHelper, new List <IPhysicalDrive>(),
                                  sourcePath, destinationPath);
            var result = await verifyCommand.Execute(cancellationTokenSource.Token);

            Assert.False(result.IsSuccess);

            Assert.Equal(typeof(SizeNotEqualError), result.Error.GetType());
            var sizeNotEqualError = (SizeNotEqualError)result.Error;

            Assert.Equal(0, sizeNotEqualError.Offset);
            Assert.Equal(sourceBytes.Length, sizeNotEqualError.Size);
        }
        public async Task WhenConvertSourceToImgDestinationWithSizeThenReadDataIsIdentical()
        {
            // arrange
            var sourcePath              = $"{Guid.NewGuid()}.img";
            var destinationPath         = $"{Guid.NewGuid()}.img";
            var size                    = 16 * 512;
            var fakeCommandHelper       = new FakeCommandHelper(new [] { sourcePath }, new [] { destinationPath });
            var cancellationTokenSource = new CancellationTokenSource();

            // act - convert source img to destination img
            var convertCommand = new ConvertCommand(new NullLogger <ConvertCommand>(), fakeCommandHelper, sourcePath, destinationPath, size);
            var result         = await convertCommand.Execute(cancellationTokenSource.Token);

            Assert.True(result.IsSuccess);

            // assert data is identical within defined size
            var sourceBytes = fakeCommandHelper.GetMedia(sourcePath).GetBytes(size);

            Assert.Equal(size, sourceBytes.Length);
            var destinationBytes = fakeCommandHelper.GetMedia(destinationPath).GetBytes();

            Assert.Equal(sourceBytes, destinationBytes);
        }
예제 #15
0
        public async Task WhenVerifySourceToImgDestinationWithSizeThenReadDataIsIdentical()
        {
            // arrange
            var sourcePath              = $"{Guid.NewGuid()}.img";
            var destinationPath         = $"{Guid.NewGuid()}.img";
            var size                    = 16 * 512;
            var fakeCommandHelper       = new FakeCommandHelper(new[] { sourcePath, destinationPath });
            var cancellationTokenSource = new CancellationTokenSource();

            // act - verify source img to destination img
            var verifyCommand = new VerifyCommand(new NullLogger <VerifyCommand>(), fakeCommandHelper,
                                                  new List <IPhysicalDrive>(), sourcePath,
                                                  destinationPath, size);
            var result = await verifyCommand.Execute(cancellationTokenSource.Token);

            Assert.True(result.IsSuccess);

            // assert data is identical
            var sourceBytes      = fakeCommandHelper.GetMedia(sourcePath).GetBytes(size);
            var destinationBytes = fakeCommandHelper.GetMedia(destinationPath).GetBytes(size);

            Assert.Equal(sourceBytes, destinationBytes);
        }
        public async Task WhenOptimizeImgWithoutRigidDiskBlockThenSizeIsNotChanged()
        {
            // arrange
            var path = $"{Guid.NewGuid()}.img";
            var fakeCommandHelper = new FakeCommandHelper();
            var bytes             = fakeCommandHelper.CreateTestData();

            fakeCommandHelper.WriteableMedias.Add(new Media(path, path, bytes.Length, Media.MediaType.Raw, false,
                                                            new MemoryStream(bytes)));
            var cancellationTokenSource = new CancellationTokenSource();

            // optimize
            var optimizeCommand = new OptimizeCommand(new NullLogger <OptimizeCommand>(), fakeCommandHelper, path);
            var result          = await optimizeCommand.Execute(cancellationTokenSource.Token);

            Assert.True(result.IsSuccess);

            // assert bytes and media optimized bytes are identical
            var optimizedBytes = fakeCommandHelper.GetMedia(path).GetBytes();

            Assert.Equal(bytes.Length, optimizedBytes.Length);
            Assert.Equal(bytes, optimizedBytes);
        }
예제 #17
0
        public async Task WhenReadInfoFromSourceImgThenDiskInfoIsReturned()
        {
            // arrange
            var path = $"{Guid.NewGuid()}.img";
            var fakeCommandHelper       = new FakeCommandHelper(new[] { path });
            var cancellationTokenSource = new CancellationTokenSource();

            // read info from path
            var       infoCommand = new InfoCommand(new NullLogger <InfoCommand>(), fakeCommandHelper, Enumerable.Empty <IPhysicalDrive>(), path);
            MediaInfo mediaInfo   = null;

            infoCommand.DiskInfoRead += (_, args) =>
            {
                mediaInfo = args.MediaInfo;
            };
            var result = await infoCommand.Execute(cancellationTokenSource.Token);

            Assert.True(result.IsSuccess);

            // assert media info
            Assert.NotNull(mediaInfo);
            Assert.Equal(mediaInfo.DiskSize, FakeCommandHelper.ImageSize);
            Assert.Null(mediaInfo.RigidDiskBlock);
        }