﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Diagnostics;

namespace openeb.tst
{
    [TestClass]
    public class EncryptCSDecryptDBPosh
    {
        private void DoEncryptCSDecryptDBPosh(Architecture archiEnc, Architecture archiDec, bool dbPathAbsolute, Compression compr, 
            HashAlgForFileNames hash, bool useHashForEnc, int usePathSeparator, int pathSeparatorWidth, long chunkSize, string pwd )
        {
            Functions.InitializeWorkFolder();

            Functions.CreateInputFolder();

            Functions.CreateOutputFolder();

            Functions.CreateTestConfigEnc(dbPathAbsolute, compr, hash, useHashForEnc, usePathSeparator, pathSeparatorWidth, chunkSize, pwd);

            // Backup

            ProcessStartInfo psi1 = new ProcessStartInfo();
            switch (archiEnc) {
                case Architecture.bit32: psi1.FileName = Config.exeOpenEBBackup32; break;
                case Architecture.bit64: psi1.FileName = Config.exeOpenEBBackup64; break;
            }
            psi1.Arguments = "-c \"" + Config.configFile + "\"";
            psi1.CreateNoWindow = !Config.useConsole;
            psi1.UseShellExecute = Config.useConsole;
            psi1.RedirectStandardError = !Config.useConsole;
            psi1.RedirectStandardInput = !Config.useConsole;
            psi1.RedirectStandardOutput = !Config.useConsole;

            var p1 = Process.Start(psi1);

            p1.WaitForExit();


            // Restore FS

            Functions.CleanupOutputFolderDec();

            ProcessStartInfo psi2 = new ProcessStartInfo();
            switch (archiEnc)
            {
                case Architecture.bit32: psi2.FileName = Config.poshExe32; break;
                case Architecture.bit64: psi2.FileName = Config.poshExe64; break;
            }


            psi2.Arguments = string.Format("-command \".'{2}' -c '{0}' -d '{1}'\"", Config.configFile, Config.subFolderOutputDec, Config.psOpenEBRestoreDB); 
            psi2.CreateNoWindow = !Config.useConsole;
            psi2.UseShellExecute = Config.useConsole;
            psi2.RedirectStandardError = !Config.useConsole;
            psi2.RedirectStandardInput = !Config.useConsole;
            psi2.RedirectStandardOutput = !Config.useConsole;

            var p2 = Process.Start(psi2);

            p2.WaitForExit();

            Functions.CheckOutputFiles();
        }

        [TestMethod]
        public void EncryptCSDecryptDBPosh_Basic1()
        {
            DoEncryptCSDecryptDBPosh(Architecture.bit64, Architecture.bit64, true, Compression.No, HashAlgForFileNames.Sha1, true, 0, 0, 0, "test");
        }

        [TestMethod]
        public void EncryptCSDecryptDBPosh_Basic2()
        {
            DoEncryptCSDecryptDBPosh(Architecture.bit32, Architecture.bit32, true, Compression.No, HashAlgForFileNames.Sha1, true, 0, 0, 0, "test");
        }

        [TestMethod]
        public void EncryptCSDecryptDBPosh_Basic3()
        {
            DoEncryptCSDecryptDBPosh(Architecture.bit32, Architecture.bit64, true, Compression.No, HashAlgForFileNames.Sha1, true, 0, 0, 0, "test");
        }

        [TestMethod]
        public void EncryptCSDecryptDBPosh_Basic4()
        {
            DoEncryptCSDecryptDBPosh(Architecture.bit64, Architecture.bit32, true, Compression.No, HashAlgForFileNames.Sha1, true, 0, 0, 0, "test");
        }

        //Compression

        [TestMethod]
        public void EncryptCSDecryptDBPosh_Compr1()
        {
            DoEncryptCSDecryptDBPosh(Architecture.bit64, Architecture.bit32, true, Compression.LZMA, HashAlgForFileNames.Sha256, true, 0, 0, 0, "test");
        }

        [TestMethod]
        public void EncryptCSDecryptDBPosh_Compr2()
        {
            DoEncryptCSDecryptDBPosh(Architecture.bit32, Architecture.bit64, true, Compression.LZMA, HashAlgForFileNames.Sha256, true, 0, 0, 0, "test");
        }

        [TestMethod]
        public void EncryptCSDecryptDBPosh_Compr3()
        {
            DoEncryptCSDecryptDBPosh(Architecture.bit32, Architecture.bit32, true, Compression.LZMA, HashAlgForFileNames.Sha256, true, 0, 0, 0, "test");
        }

        [TestMethod]
        public void EncryptCSDecryptDBPosh_Compr4()
        {
            DoEncryptCSDecryptDBPosh(Architecture.bit64, Architecture.bit64, true, Compression.LZMA, HashAlgForFileNames.Sha256, true, 0, 0, 0, "test");
        }

        //Chunks

        [TestMethod]
        public void EncryptCSDecryptDBPosh_Chunk1()
        {
            DoEncryptCSDecryptDBPosh(Architecture.bit32, Architecture.bit64, true, Compression.LZMA, HashAlgForFileNames.Sha256, true, 0, 0, 1000, "test");
        }

        [TestMethod]
        public void EncryptCSDecryptDBPosh_Chunk2()
        {
            DoEncryptCSDecryptDBPosh(Architecture.bit64, Architecture.bit32, true, Compression.No, HashAlgForFileNames.Sha256, true, 0, 0, 1000, "test");
        }


        //PathSep

        [TestMethod]
        public void EncryptCSDecryptDBPosh_PathSep1()
        {
            DoEncryptCSDecryptDBPosh(Architecture.bit32, Architecture.bit64, true, Compression.No, HashAlgForFileNames.Sha512, true, 1, 2, 10000, "test");
        }

        [TestMethod]
        public void EncryptCSDecryptDBPosh_PathSep2()
        {
            DoEncryptCSDecryptDBPosh(Architecture.bit64, Architecture.bit32, true, Compression.No, HashAlgForFileNames.Sha512, true, 1, 2, 10000, "test");
        }

        [TestMethod]
        public void EncryptCSDecryptDBPosh_PathSep3()
        {
            DoEncryptCSDecryptDBPosh(Architecture.bit32, Architecture.bit64, true, Compression.LZMA, HashAlgForFileNames.Sha1, true, 2, 4, 100000, "test");
        }

        [TestMethod]
        public void EncryptCSDecryptDBPosh_PathSep4()
        {
            DoEncryptCSDecryptDBPosh(Architecture.bit64, Architecture.bit32, true, Compression.LZMA, HashAlgForFileNames.Sha1, true, 2, 4, 100000, "test");
        }
    }
}
