Пример #1
0
        public async Task Save_ToFileOverwriteAndExpand_OutputEqualExpectedSize()
        {
            const string  FileName           = nameof(Save_ToFileOverwriteAndExpand_OutputEqualExpectedSize);
            string        headerPath         = FileName + HeaderFileExtension;
            string        dataPath           = FileName + DataFileExtension;
            string        input              = "inputdata123" + nameof(Save_ToFileOverwriteAndExpand_OutputEqualExpectedSize);
            string        encryptedInput     = "encrypteddata654" + nameof(Save_ToFileOverwriteAndExpand_OutputEqualExpectedSize);
            var           sizeMock           = CreateSizeMock(1, 20, 8);
            MS2CryptoMode expectedCryptoMode = (MS2CryptoMode)12345;
            IMS2ArchiveCryptoRepository repo = new FakeCryptoRepository(expectedCryptoMode, EncodingTest, input, encryptedInput, sizeMock.Object);
            int expectedHeaderLength         = 60 + encryptedInput.Length * 2;
            int expectedDataLength           = encryptedInput.Length;

            SetFileLength(headerPath, 1 << 10);
            SetFileLength(dataPath, 1 << 10);
            var archive = new MS2Archive(repo);

            AddDataStringToArchive(archive, input, encryptedInput, sizeMock, 1, "overwritefile", CompressionType.None);
            await archive.SaveAsync(headerPath, dataPath);

            int actualHeaderLength = File.ReadAllText(headerPath).Length;
            int actualDataLength   = File.ReadAllText(dataPath).Length;

            Assert.AreEqual(expectedHeaderLength, actualHeaderLength);
            Assert.AreEqual(expectedDataLength, actualDataLength);
        }
Пример #2
0
        public async Task Save_ThreeFilesCompressedToFile_DataEqualsExpected()
        {
            const string  FileName           = nameof(Save_ThreeFilesToFile_DataEqualsExpected);
            string        headerPath         = FileName + HeaderFileExtension;
            string        dataPath           = FileName + DataFileExtension;
            string        input              = "inputdata123" + nameof(Save_ThreeFilesToFile_DataEqualsExpected);
            string        encryptedInput     = "encrypteddata654" + nameof(Save_ThreeFilesToFile_DataEqualsExpected);
            var           sbExpected         = new StringBuilder();
            var           sizeMock           = CreateSizeMock(20, 30, 40);
            MS2CryptoMode cryptoMode         = (MS2CryptoMode)12345;
            IMS2ArchiveCryptoRepository repo = new FakeCryptoRepository(cryptoMode, EncodingTest, input, encryptedInput, sizeMock.Object);
            const int fileCount              = 3;

            var archive = MS2Archive.GetArchiveMS2F();

            for (uint i = 1; i <= fileCount; i++)
            {
                sbExpected.Append(input);
                AddDataStringToArchive(archive, input, encryptedInput, sizeMock, i, "file" + i, CompressionType.Zlib);
            }
            await archive.SaveAsync(headerPath, dataPath);

            using var fsData = File.OpenRead(dataPath);
            StringBuilder sbActual = new StringBuilder();

            for (uint i = 1; i <= fileCount; i++)
            {
                string actual = await StreamToString(await repo.GetDecryptionStreamAsync(fsData, sizeMock.Object, false));

                sbActual.Append(actual);
            }
            Assert.AreEqual(sbExpected.ToString(), sbActual.ToString());
        }
Пример #3
0
        public async Task Save_OneFileToFile_FileInfoHeaderEqualsExpectedData()
        {
            const string  FileName           = nameof(Save_OneFileToFile_FileInfoHeaderEqualsExpectedData);
            string        headerPath         = FileName + HeaderFileExtension;
            string        dataPath           = FileName + DataFileExtension;
            string        input              = "inputdata123," + nameof(Save_OneFileToFile_FileInfoHeaderEqualsExpectedData);
            string        encryptedInput     = "encrypteddata654," + nameof(Save_OneFileToFile_FileInfoHeaderEqualsExpectedData);
            var           sizeMock           = CreateSizeMock(1, 20, 8);
            MS2CryptoMode expectedCryptoMode = (MS2CryptoMode)12345;
            IMS2FileInfo  expectedFileInfo   = CreateFileInfoMock(1.ToString(), "singlefile").Object;
            IMS2ArchiveCryptoRepository repo = new FakeCryptoRepository(expectedCryptoMode, EncodingTest, "1,singlefile", "1,singlefile", sizeMock.Object);

            var archive = new MS2Archive(repo);

            AddDataStringToArchive(archive, input, encryptedInput, sizeMock, 1, "singlefile", CompressionType.Zlib);
            await archive.SaveAsync(headerPath, dataPath);

            using var fsHeader = File.OpenRead(headerPath);
            using var br       = new BinaryReader(fsHeader, EncodingTest, true);
            MS2CryptoMode actualCryptoMode = (MS2CryptoMode)br.ReadUInt32();

            var(actualFileInfoSize, actualFileDataSize, actualFileCount) = await repo.GetArchiveHeaderCrypto().ReadAsync(fsHeader);

            var msFileInfo = await repo.GetDecryptionStreamAsync(fsHeader, actualFileInfoSize, true);

            using var srFileInfo = new StreamReader(msFileInfo, EncodingTest, true, -1, true);
            IMS2FileInfo actualFileInfo = await repo.GetFileInfoReaderCrypto().ReadAsync(srFileInfo);

            Assert.AreEqual(expectedFileInfo.Id, actualFileInfo.Id);
            Assert.AreEqual(expectedFileInfo.Path, actualFileInfo.Path);
            Assert.AreEqual(expectedFileInfo.RootFolderId, actualFileInfo.RootFolderId);
        }
Пример #4
0
        public async Task Save_OneFileToPath_ArchiveHeaderEqualsExpectedData()
        {
            const string   FileName             = nameof(Save_OneFileToPath_ArchiveHeaderEqualsExpectedData);
            string         headerPath           = FileName + HeaderFileExtension;
            string         dataPath             = FileName + DataFileExtension;
            string         input                = "inputdata123" + nameof(Save_OneFileToPath_ArchiveHeaderEqualsExpectedData);
            string         encryptedInput       = "encrypteddata654" + nameof(Save_OneFileToPath_ArchiveHeaderEqualsExpectedData);
            var            sizeMock             = CreateSizeMock(1, 20, 8);
            IMS2SizeHeader expectedFileInfoSize = sizeMock.Object;
            IMS2SizeHeader expectedFileDataSize = sizeMock.Object;
            long           expectedFileCount    = 1;
            MS2CryptoMode  expectedCryptoMode   = (MS2CryptoMode)12345;
            IMS2ArchiveCryptoRepository repo    = new FakeCryptoRepository(expectedCryptoMode, EncodingTest, input, encryptedInput, sizeMock.Object);

            var archive = new MS2Archive(repo);

            AddDataStringToArchive(archive, input, encryptedInput, sizeMock, 1, "singlefile", CompressionType.Zlib);
            await archive.SaveAsync(headerPath, dataPath);

            using var fsHeader = File.OpenRead(headerPath);
            using var br       = new BinaryReader(fsHeader, EncodingTest, true);
            MS2CryptoMode actualCryptoMode = (MS2CryptoMode)br.ReadUInt32();

            var(actualFileInfoSize, actualFileDataSize, actualFileCount) = await repo.GetArchiveHeaderCrypto().ReadAsync(fsHeader);

            Assert.AreEqual(expectedCryptoMode, actualCryptoMode);
            Assert.AreEqual(expectedFileInfoSize.EncodedSize, actualFileInfoSize.EncodedSize);
            Assert.AreEqual(expectedFileInfoSize.CompressedSize, actualFileInfoSize.CompressedSize);
            Assert.AreEqual(expectedFileInfoSize.Size, actualFileInfoSize.Size);
            Assert.AreEqual(expectedFileDataSize.EncodedSize, actualFileDataSize.EncodedSize);
            Assert.AreEqual(expectedFileDataSize.CompressedSize, actualFileDataSize.CompressedSize);
            Assert.AreEqual(expectedFileDataSize.Size, actualFileDataSize.Size);
            Assert.AreEqual(expectedFileCount, actualFileCount);
        }
Пример #5
0
        public async Task GetStreamAsync_StreamEncryptedTrueAndCompressionZlib_EqualsExpected()
        {
            string        input              = "testdatainputencryptedcompressed";
            string        expected           = "testdataexpected";
            MS2CryptoMode cryptoMode         = (MS2CryptoMode)12345;
            var           sizeMock           = CreateSizeMock(10, 20, 30);
            IMS2ArchiveCryptoRepository repo = new FakeCryptoRepository(cryptoMode, EncodingTest, expected, input, sizeMock.Object);

            using MemoryStream inputStream = StringToStream(input);
            var archiveMock = CreateArchiveMock(repo);
            var infoMock    = CreateFileInfoMock("1", "testfile");
            var headerMock  = CreateFileHeaderMock(sizeMock, 1, 0, CompressionType.Zlib);
            var file        = new MS2File(archiveMock.Object, inputStream, infoMock.Object, headerMock.Object, true);

            var actualStream = await file.GetStreamAsync();

            string actual = await StreamToString(actualStream);

            Assert.AreEqual(expected, actual);
        }
Пример #6
0
        public async Task Save_OneFileToPath_DataEqualsInput()
        {
            const string  FileName           = nameof(Save_OneFileToPath_DataEqualsInput);
            string        headerPath         = FileName + HeaderFileExtension;
            string        dataPath           = FileName + DataFileExtension;
            string        input              = "inputdata123" + nameof(Save_OneFileToPath_DataEqualsInput);
            string        encryptedInput     = "encrypteddata654" + nameof(Save_OneFileToPath_DataEqualsInput);
            var           sizeMock           = CreateSizeMock(20, 30, 40);
            MS2CryptoMode cryptoMode         = (MS2CryptoMode)12345;
            IMS2ArchiveCryptoRepository repo = new FakeCryptoRepository(cryptoMode, EncodingTest, input, encryptedInput, sizeMock.Object);

            var archive = new MS2Archive(repo);

            AddDataStringToArchive(archive, input, encryptedInput, sizeMock, 1, "singlefile", CompressionType.Zlib);
            await archive.SaveAsync(headerPath, dataPath);

            using var fsData = File.OpenRead(dataPath);
            string actual = await StreamToString(await repo.GetDecryptionStreamAsync(fsData, sizeMock.Object, false));

            Assert.AreEqual(input, actual);
        }
Пример #7
0
        public async Task GetStreamForArchivingAsync_StreamEncryptedTrueAndCompressionNone_EqualsInput()
        {
            string        input              = "testdatainput";
            string        expected           = input;
            MS2CryptoMode cryptoMode         = (MS2CryptoMode)12345;
            var           sizeMock           = CreateSizeMock(10, 10, 10);
            var           expectedSize       = sizeMock.Object;
            IMS2ArchiveCryptoRepository repo = new FakeCryptoRepository(cryptoMode, EncodingTest, input, expected, sizeMock.Object);

            using MemoryStream inputStream = StringToStream(input);
            var archiveMock = CreateArchiveMock(repo);
            var infoMock    = CreateFileInfoMock("1", "testfile");
            var headerMock  = CreateFileHeaderMock(sizeMock, 1, 0, CompressionType.None);
            var file        = new MS2File(archiveMock.Object, inputStream, infoMock.Object, headerMock.Object, true);

            var(actualStream, actualSize) = await file.GetStreamForArchivingAsync();

            string actual = await StreamToString(actualStream);

            Assert.AreEqual(expected, actual);
            Assert.AreEqual(expectedSize.EncodedSize, actualSize.EncodedSize);
            Assert.AreEqual(expectedSize.CompressedSize, actualSize.CompressedSize);
            Assert.AreEqual(expectedSize.Size, actualSize.Size);
        }