Ejemplo n.º 1
0
        public void SaveRootElement_Chart_SuccessfullySaved()
        {
            using MemoryStream stream = FileCloner.ReadAllBytesToMemoryStream("Resources\\ChartTemplate.crtx");

            using (Package package = Package.Open(stream, FileMode.Open, FileAccess.ReadWrite))
            {
                // Get the package part, its root element, and the c:lang element.
                // Note that the val attribute value is "en-US".
                PackagePart packagePart = package.GetPart(new Uri("/chart/chart.xml", UriKind.Relative));
                XElement    rootElement = LoadRootElement(packagePart);
                XElement    lang        = rootElement.Elements(C.lang).First();

                Assert.Equal("en-US", (string)lang.Attribute(C.val));

                // Change the val attribute value to "de-DE" and save the root element.
                lang.SetAttributeValue(C.val, "de-DE");

                // Act, saving then root element.
                SaveRootElement(packagePart, rootElement);
            }

            // Save the modified chart template for manual inspection.
            File.WriteAllBytes("ModifiedChartTemplate.crtx", stream.ToArray());

            // Assert that we have indeed changed the part.
            using (Package package = Package.Open("ModifiedChartTemplate.crtx"))
            {
                PackagePart packagePart = package.GetPart(new Uri("/chart/chart.xml", UriKind.Relative));
                XElement    rootElement = LoadRootElement(packagePart);
                XElement    lang        = rootElement.Elements(C.lang).First();

                Assert.Equal("de-DE", (string)lang.Attribute(C.val));
            }
        }
Ejemplo n.º 2
0
        public void DoWorkUsingCopyFileStreamToMemoryStream()
        {
            using MemoryStream destStream = FileCloner.CopyFileStreamToMemoryStream(SourcePath);

            using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(destStream, true))
            {
                ChangeWordprocessingDocument(wordDocument);
            }

            File.WriteAllBytes(DestPath, destStream.GetBuffer());
        }
Ejemplo n.º 3
0
        public void CopyFileStreamToMemoryStream_TestDocument_SuccessfullyCloned()
        {
            // Arrange.
            const string path = "CopyFileStreamToMemoryStream_Source.docx";

            CreateTestDocument(path);

            // Act.
            MemoryStream destStream = FileCloner.CopyFileStreamToMemoryStream(path);

            // Assert.
            AssertDocumentHasExpectedContents(destStream);
        }
Ejemplo n.º 4
0
        public void CopyFileAndOpenFileStream_TestDocument_SuccessfullyCloned()
        {
            // Arrange.
            const string sourcePath = "CopyFileAndOpenFileStream_Source.docx";
            const string destPath   = "CopyFileAndOpenFileStream_Destination.docx";

            CreateTestDocument(sourcePath);
            File.Delete(destPath);

            // Act.
            FileStream destStream = FileCloner.CopyFileAndOpenFileStream(sourcePath, destPath);

            // Assert.
            AssertDocumentHasExpectedContents(destStream);
        }
Ejemplo n.º 5
0
        public void Sign_CertificateNotPassed_Success()
        {
            const string path = "Resources\\UnsignedDocument.docx";

            using MemoryStream stream = FileCloner.CopyFileStreamToMemoryStream(path);

            using (OpenXmlPackage openXmlPackage = Open(stream, true, path))
            {
                PackageDigitalSignature signature = OpenXmlDigitalSignatureManager.Sign(openXmlPackage);

                VerifyResult verifyResult = signature.Verify();
                Assert.Equal(VerifyResult.Success, verifyResult);
            }

            File.WriteAllBytes("OpenXml_CertNotPassed_SignedDocument.docx", stream.ToArray());
        }
        public void Sign_NoCertificatePassed_ExcelWorkbookSigned()
        {
            const string path = "Resources\\UnsignedWorkbook.xlsx";
            using MemoryStream stream = FileCloner.CopyFileStreamToMemoryStream(path);

            using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(stream, true))
            {
                DigitalSignatureManager.Sign(spreadsheetDocument.Package);
            }

            using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(stream, false))
            {
                VerifyResult verifyResult = DigitalSignatureManager.VerifySignature(spreadsheetDocument.Package);
                Assert.Equal(VerifyResult.Success, verifyResult);
            }

            File.WriteAllBytes("SignedWorkbook.xlsx", stream.ToArray());
        }
Ejemplo n.º 7
0
        public void Sign_CertificatePassed_Success(string sourcePath, string destPath)
        {
            using MemoryStream stream = FileCloner.CopyFileStreamToMemoryStream(sourcePath);

            using (OpenXmlPackage openXmlPackage = Open(stream, true, sourcePath))
            {
                X509Certificate2 certificate = DigitalSignatureManager
                                               .GetSigningCertificates()
                                               .OfType <X509Certificate2>()
                                               .First();

                PackageDigitalSignature signature = OpenXmlDigitalSignatureManager.Sign(openXmlPackage, certificate);

                VerifyResult verifyResult = signature.Verify();
                Assert.Equal(VerifyResult.Success, verifyResult);
            }

            File.WriteAllBytes(destPath, stream.ToArray());
        }
Ejemplo n.º 8
0
        public void Sign_UnsignedWorkbook_SuccessfullySigned()
        {
            // Load our unsigned Excel workbook into a MemoryStream for processing.
            const string path = "Resources\\UnsignedWorkbook.xlsx";

            using MemoryStream stream = FileCloner.ReadAllBytesToMemoryStream(path);

            // Open the SpreadsheetDocument on the MemoryStream and sign it, using
            // the first digital signature certificate that we find.
            using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(stream, true))
            {
                X509Certificate2 certificate = DigitalSignatureManager.GetDigitalSignatureCertificates().First();
                DigitalSignatureManager.Sign(spreadsheetDocument, certificate);
            }

            // Save the signed Excel workbook to disk. When you open this workbook,
            // Excel should tell you that it has found a valid signature.
            File.WriteAllBytes("SignedWorkbook.xlsx", stream.ToArray());
        }
Ejemplo n.º 9
0
 public void DoWorkUsingCopyFileAndOpenFileStream()
 {
     using FileStream destStream = FileCloner.CopyFileAndOpenFileStream(SourcePath, DestPath);
     using WordprocessingDocument wordDocument = WordprocessingDocument.Open(destStream, true);
     ChangeWordprocessingDocument(wordDocument);
 }