Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw XLLoadException when failing to load an OpenXML package #892

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion ClosedXML/ClosedXML.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<LangVersion>7.2</LangVersion>
<AssemblyName>ClosedXML</AssemblyName>
<PackageId>ClosedXML</PackageId>
<Version>0.93.0</Version>
<Version>0.94.0</Version>
<Authors>Manuel de Leon, Amir Ghezelbash, Francois Botha, Aleksei Pankratev</Authors>
<Company />
<Product>ClosedXML</Product>
Expand Down
47 changes: 40 additions & 7 deletions ClosedXML/Excel/XLWorkbook_Load.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using ClosedXML.Exceptions;
using ClosedXML.Extensions;
using ClosedXML.Utils;
using DocumentFormat.OpenXml;
Expand Down Expand Up @@ -38,20 +39,41 @@ private void Load(Stream stream)

private void LoadSheets(String fileName)
{
using (var dSpreadsheet = SpreadsheetDocument.Open(fileName, false))
LoadSpreadsheetDocument(dSpreadsheet);
try
{
using (var dSpreadsheet = SpreadsheetDocument.Open(fileName, false))
LoadSpreadsheetDocument(dSpreadsheet);
}
catch (Exception ex)
{
throw new XLLoadException($"Unable to parse the contents of the file '{fileName}'", ex);
}
}

private void LoadSheets(Stream stream)
{
using (var dSpreadsheet = SpreadsheetDocument.Open(stream, false))
LoadSpreadsheetDocument(dSpreadsheet);
try
{
using (var dSpreadsheet = SpreadsheetDocument.Open(stream, false))
LoadSpreadsheetDocument(dSpreadsheet);
}
catch (Exception ex)
{
throw new XLLoadException($"Unable to parse the stream's contents", ex);
}
}

private void LoadSheetsFromTemplate(String fileName)
{
using (var dSpreadsheet = SpreadsheetDocument.CreateFromTemplate(fileName))
LoadSpreadsheetDocument(dSpreadsheet);
try
{
using (var dSpreadsheet = SpreadsheetDocument.CreateFromTemplate(fileName))
LoadSpreadsheetDocument(dSpreadsheet);
}
catch (Exception ex)
{
throw new XLLoadException($"Unable to parse the contents of the file '{fileName}'", ex);
}
}

private void LoadSpreadsheetDocument(SpreadsheetDocument dSpreadsheet)
Expand Down Expand Up @@ -2169,7 +2191,18 @@ private static void LoadSheetProtection(SheetProtection sp, XLWorksheet ws)
if (sp == null) return;

if (sp.Sheet != null) ws.Protection.Protected = sp.Sheet.Value;
if (sp.Password != null) ws.Protection.PasswordHash = sp.Password.Value;

var algorithmName = sp.AlgorithmName?.Value ?? string.Empty;
if (String.IsNullOrEmpty(algorithmName))
{
ws.Protection.PasswordHash = sp.Password?.Value ?? string.Empty;
}
else
{
var hashValue = sp.HashValue?.Value ?? string.Empty;
var saltValue = sp.SaltValue?.Value ?? string.Empty;
// Continue for now.
}
if (sp.FormatCells != null) ws.Protection.FormatCells = !sp.FormatCells.Value;
if (sp.FormatColumns != null) ws.Protection.FormatColumns = !sp.FormatColumns.Value;
if (sp.FormatRows != null) ws.Protection.FormatRows = !sp.FormatRows.Value;
Expand Down
19 changes: 19 additions & 0 deletions ClosedXML/Exceptions/XLLoadException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace ClosedXML.Exceptions
{
public class XLLoadException : Exception
{
internal XLLoadException()
: base()
{ }

internal XLLoadException(string message)
: base(message)
{ }

internal XLLoadException(string message, Exception innerException)
: base(message, innerException)
{ }
}
}
2 changes: 1 addition & 1 deletion ClosedXML_Examples/ClosedXML_Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netcoreapp2.0;net40;net46</TargetFrameworks>
<LangVersion>7.2</LangVersion>
<OutputType>Exe</OutputType>
<Version>0.93.0</Version>
<Version>0.94.0</Version>
<NoWarn>$(NoWarn);NU1605</NoWarn>
<Configurations>Debug;Release;Release.Signed</Configurations>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion ClosedXML_Sandbox/ClosedXML_Sandbox.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp2.0;net40;net46</TargetFrameworks>
<LangVersion>7.2</LangVersion>
<Version>0.93.0</Version>
<Version>0.94.0</Version>
<NoWarn>$(NoWarn);NU1605</NoWarn>
<Configurations>Debug;Release;Release.Signed</Configurations>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion ClosedXML_Tests/ClosedXML_Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;net40;net46</TargetFrameworks>
<LangVersion>7.2</LangVersion>
<Version>0.93.0</Version>
<Version>0.94.0</Version>
<IsPackable>false</IsPackable>
<NoWarn>$(NoWarn);NU1605</NoWarn>
<Configurations>Debug;Release;Release.Signed</Configurations>
Expand Down
11 changes: 11 additions & 0 deletions ClosedXML_Tests/Excel/Loading/LoadingTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ClosedXML.Excel;
using ClosedXML.Excel.Drawings;
using ClosedXML.Exceptions;
using ClosedXML_Tests.Utils;
using NUnit.Framework;
using System;
Expand Down Expand Up @@ -76,6 +77,16 @@ public void CanSuccessfullyLoadLOFiles()
}
}

[Test]
public void InvalidFileThrowsCustomException()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there have to be 3 tests - one for each method throwing an exception - so we won't lose it in case of future refactoring.

{
using (var stream = TestHelper.GetStreamFromResource(TestHelper.GetResourcePath(@"Misc\LO\xlsx\tdf100709.xlsx")))
Assert.Throws<XLLoadException>(() =>
{
var wb = new XLWorkbook(stream);
});
}

[Test]
public void CanLoadAndManipulateFileWithEmptyTable()
{
Expand Down
40 changes: 40 additions & 0 deletions ClosedXML_Tests/Excel/Worksheets/XLSheetProtectionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using ClosedXML.Excel;
using NUnit.Framework;
using System;

namespace ClosedXML_Tests.Excel.Worksheets
{
[TestFixture]
public class XLSheetProtectionTests
{
[Test]
public void TestUnprotectWorksheetWithNoPassword()
{
using (var stream = TestHelper.GetStreamFromResource(TestHelper.GetResourcePath(@"Misc\SHA512PasswordProtection.xlsx")))
using (var wb = new XLWorkbook(stream))
{
var ws = wb.Worksheet("Sheet1");
Assert.IsTrue(ws.Protection.Protected);
ws.Unprotect();
Assert.IsFalse(ws.Protection.Protected);
}
}

[Test]
public void TestWorksheetWithSHA512Protection()
{
using (var stream = TestHelper.GetStreamFromResource(TestHelper.GetResourcePath(@"Misc\SHA512PasswordProtection.xlsx")))
using (var wb = new XLWorkbook(stream))
{
var ws = wb.Worksheet("Sheet2");
Assert.IsTrue(ws.Protection.Protected);
// Protected with SHA-512 password - not yet supported.
Assert.Throws<ArgumentException>(() => ws.Unprotect());

// Protected with SHA-512 password - not yet supported.
Assert.Throws<ArgumentException>(() => ws.Unprotect("abc"));
Assert.IsTrue(ws.Protection.Protected);
}
}
}
}
Binary file not shown.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 0.93.0.{build}
version: 0.94.0.{build}

os: Visual Studio 2017
image: Visual Studio 2017
Expand Down