public void PackAndSign_CertFromFile_AbsoluteInRelativeOutPath()
        {
            var certPass          = GetTestCertPassword();
            X509Certificate2 cert = Templatex.LoadCert(@"Locations\TestCert.pfx", certPass);

            var inFile  = Path.Combine(Environment.CurrentDirectory, @"Locations\SampleContent.txt");
            var outFile = @"Locations\SignedContent.package";

            Templatex.PackAndSign(inFile, outFile, cert, MediaTypeNames.Text.Plain);
            Assert.True(File.Exists(outFile));
            File.Delete(outFile);
        }
        public void PackAndSign_CertFromFile_RelativeInAbsouluteOutPath()
        {
            var certPass          = GetTestCertPassword();
            X509Certificate2 cert = Templatex.LoadCert(@"Locations\TestCert.pfx", certPass);

            var inFile  = @"Locations\SampleContent.txt";
            var outFile = @"C:\temp\Locations\SignedContent.package";

            Templatex.PackAndSign(inFile, outFile, cert, MediaTypeNames.Text.Plain);
            Assert.True(File.Exists(outFile));
            File.Delete(outFile);
        }
        private static void EnsureTestCertificateInStore()
        {
            SecureString ss = GetTestCertPassword();

            if (Templatex.LoadCert("B584589A382B2AD20B54D2DD1634BB487792A970") == null)
            {
                X509Certificate2 c = new X509Certificate2(@"Locations\TestCert.pfx", ss, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);

                X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
                store.Open(OpenFlags.ReadWrite);
                store.Add(c);
                store.Close();
            }
        }
        public void ValidateSignatureTamperedPackage()
        {
            var certPass          = GetTestCertPassword();
            X509Certificate2 cert = Templatex.LoadCert(@"Locations\TestCert.pfx", certPass);

            var inFile  = @"Locations\SampleContent.txt";
            var outFile = @"Locations\ToExtract.package";

            Templatex.PackAndSign(inFile, outFile, cert, MediaTypeNames.Text.Plain);

            ModifyContent(outFile, "SampleContent.txt");

            Assert.False(Templatex.ValidateSignatures(outFile));

            File.Delete(outFile);
        }
        public void ExtractFileCurrentDir()
        {
            var certPass          = GetTestCertPassword();
            X509Certificate2 cert = Templatex.LoadCert(@"Locations\TestCert.pfx", certPass);

            var inFile        = @"Locations\SampleContent.txt";
            var outFile       = @"ToExtract.package";
            var extractionDir = "";

            Templatex.PackAndSign(inFile, outFile, cert, MediaTypeNames.Text.Plain);

            Templatex.Extract(outFile, extractionDir);

            Assert.True(File.Exists(outFile));

            File.Delete(outFile);
        }
        public void ExtractFileAndPacksInCurrentDir()
        {
            var certPass          = GetTestCertPassword();
            X509Certificate2 cert = Templatex.LoadCert(@"Locations\TestCert.pfx", certPass);

            File.Copy(@"Locations\SampleContent.txt", Path.Combine(Environment.CurrentDirectory, "NewFile.txt"), true);
            var inFile        = "NewFile.txt";
            var outFile       = @"ToExtract.package";
            var extractionDir = Environment.CurrentDirectory;

            Templatex.PackAndSign(inFile, outFile, cert, MediaTypeNames.Text.Plain);

            Templatex.Extract(outFile, extractionDir);

            Assert.True(Directory.Exists(extractionDir));
            Assert.True(File.Exists(Path.Combine(extractionDir, Path.GetFileName(inFile))));

            File.Delete(outFile);
        }
        public void ExtractAbsoluteDirs()
        {
            var certPass          = GetTestCertPassword();
            X509Certificate2 cert = Templatex.LoadCert(@"Locations\TestCert.pfx", certPass);

            var inFile        = Path.Combine(Environment.CurrentDirectory, @"Locations\SampleContent.txt");
            var outFile       = @"C:\Temp\MyPackage\ToExtract.package";
            var extractionDir = @"C:\Temp\NewContent\Extracted";

            Templatex.PackAndSign(inFile, outFile, cert, MediaTypeNames.Text.Plain);

            Templatex.Extract(outFile, extractionDir);

            Assert.True(Directory.Exists(extractionDir));
            Assert.True(File.Exists(Path.Combine(extractionDir, @"Locations\SampleContent.txt")));

            File.Delete(outFile);
            Directory.Delete(extractionDir, true);
        }
        public void ExtractRelativeDirs()
        {
            var certPass          = GetTestCertPassword();
            X509Certificate2 cert = Templatex.LoadCert(@"Locations\TestCert.pfx", certPass);

            var inFile        = @"Locations\SampleContent.txt";
            var outFile       = @"Locations\ToExtract.package";
            var extractionDir = "NewDirToExtract";

            Templatex.PackAndSign(inFile, outFile, cert, MediaTypeNames.Text.Plain);

            Templatex.Extract(outFile, extractionDir);

            Assert.True(Directory.Exists(extractionDir));
            Assert.True(File.Exists(Path.Combine(extractionDir, inFile)));

            File.Delete(outFile);
            Directory.Delete(extractionDir, true);
        }
        public void PackFolderExtractToAbsoluteDir()
        {
            var certPass          = GetTestCertPassword();
            X509Certificate2 cert = Templatex.LoadCert(@"Locations\TestCert.pfx", certPass);

            int filesInCurrentFolder = new DirectoryInfo(Environment.CurrentDirectory).GetFiles("*", SearchOption.AllDirectories).Count();
            var inFolder             = Environment.CurrentDirectory;
            var outDir = @"C:\Temp\OutFolder\Extraction";

            string signedFile = Templatex.PackAndSign(inFolder, cert);

            Templatex.Extract(signedFile, outDir);

            int filesInExtractionFolder = new DirectoryInfo(outDir).GetFiles("*", SearchOption.AllDirectories).Count();

            Assert.Equal(filesInCurrentFolder, filesInExtractionFolder);

            File.Delete(signedFile);
            Directory.Delete(outDir, true);
        }
        public void ExtractFileTampered()
        {
            var certPass          = GetTestCertPassword();
            X509Certificate2 cert = Templatex.LoadCert(@"Locations\TestCert.pfx", certPass);

            var inFile        = @"Locations\SampleContent.txt";
            var outFile       = @"Locations\ToExtract.package";
            var extractionDir = "SubDir";

            Templatex.PackAndSign(inFile, outFile, cert, MediaTypeNames.Text.Plain);

            ModifyContent(outFile, "SampleContent.txt");

            Exception ex = Assert.Throws <InvalidSignatureException>(() => {
                Templatex.Extract(outFile, extractionDir);
            });

            File.Delete(outFile);
            Directory.Delete(extractionDir, true);
        }