Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }