-
Notifications
You must be signed in to change notification settings - Fork 544
/
DocumentOpenTests.cs
152 lines (124 loc) · 6.38 KB
/
DocumentOpenTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using DocumentFormat.OpenXml.Framework;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Validation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Xunit;
using static DocumentFormat.OpenXml.Tests.TestAssets;
namespace DocumentFormat.OpenXml.Tests
{
public class DocumentOpenTests
{
[Fact]
public void ThrowIfFileCannotBeFound()
{
const string NonExistantFile = "Unknown";
Assert.False(File.Exists(NonExistantFile));
Assert.Throws<FileNotFoundException>(() => SpreadsheetDocument.Open(NonExistantFile, true));
Assert.Throws<FileNotFoundException>(() => SpreadsheetDocument.Open(NonExistantFile, false));
Assert.Throws<FileNotFoundException>(() => SpreadsheetDocument.Open(NonExistantFile, true, new OpenSettings()));
Assert.Throws<FileNotFoundException>(() => SpreadsheetDocument.Open(NonExistantFile, false, new OpenSettings()));
Assert.Throws<FileNotFoundException>(() => WordprocessingDocument.Open(NonExistantFile, true));
Assert.Throws<FileNotFoundException>(() => WordprocessingDocument.Open(NonExistantFile, false));
Assert.Throws<FileNotFoundException>(() => WordprocessingDocument.Open(NonExistantFile, true, new OpenSettings()));
Assert.Throws<FileNotFoundException>(() => WordprocessingDocument.Open(NonExistantFile, false, new OpenSettings()));
Assert.Throws<FileNotFoundException>(() => PresentationDocument.Open(NonExistantFile, true));
Assert.Throws<FileNotFoundException>(() => PresentationDocument.Open(NonExistantFile, false));
Assert.Throws<FileNotFoundException>(() => PresentationDocument.Open(NonExistantFile, true, new OpenSettings()));
Assert.Throws<FileNotFoundException>(() => PresentationDocument.Open(NonExistantFile, false, new OpenSettings()));
}
[Theory]
[InlineData(FileAccess.Read)]
[InlineData(FileAccess.ReadWrite)]
public void RewriteMalformedUriLong(FileAccess access)
{
// Arrange
const string ExpectedMalformedUri = "mailto:test@test.com;%20test2@test.com;%252test3@test.com;%20test3@test.com;%20test4@test.com;%20test5@test.com?subject=Unsubscribe%20Request&body=Please%20unsubscribe%20me%20from%20all%20future%20communications";
const string Id = "rId1";
using var file = OpenFile(TestFiles.Malformed_uri_long_xlsx, access);
using var stream = file.Open();
using var doc = SpreadsheetDocument.Open(stream, isEditable: file.IsEditable);
// Act
var worksheetPart = doc.WorkbookPart.WorksheetParts.Single();
var hyperlinkRelationship = worksheetPart.HyperlinkRelationships.Single();
var worksheet = Assert.IsType<Worksheet>(worksheetPart.RootElement);
var hyperlink = Assert.Single(worksheet.Descendants<Hyperlink>());
// Assert
Assert.Equal(Id, hyperlinkRelationship.Id);
Assert.Equal(Id, hyperlink.Id);
Assert.Equal(ExpectedMalformedUri, hyperlinkRelationship.Uri.ToString());
}
[Theory]
[InlineData(FileAccess.Read)]
[InlineData(FileAccess.ReadWrite)]
public void RewriteMalformedUri(FileAccess access)
{
// Arrange
const string Id = "rId1";
using var file = OpenFile(TestFiles.Malformed_uri_xlsx, access);
using var stream = file.Open();
using var doc = SpreadsheetDocument.Open(stream, isEditable: file.IsEditable);
// Act
var worksheetPart = doc.WorkbookPart.WorksheetParts.Single();
var hyperlinkRelationship = worksheetPart.HyperlinkRelationships.Single();
var worksheet = Assert.IsType<Worksheet>(worksheetPart.RootElement);
var hyperlink = Assert.Single(worksheet.Descendants<Hyperlink>());
// Assert
Assert.Equal(Id, hyperlink.Id);
Assert.Equal(Id, hyperlinkRelationship.Id);
Assert.Equal("mailto:one@", hyperlinkRelationship.Uri.ToString());
}
[Fact]
public void NonSeekableRewriteMalformedUri()
{
// Arrange
using var stream = GetStream(TestFiles.Malformed_uri_xlsx);
var exception = Assert.Throws<OpenXmlPackageException>(() =>
{
using var doc = SpreadsheetDocument.Open(new NonSeekableStream(stream), isEditable: false);
// Act/Assert
var worksheetPart = doc.WorkbookPart.WorksheetParts.Single();
var link = worksheetPart.HyperlinkRelationships.Single();
});
#if NETFRAMEWORK
Assert.IsType<ArgumentException>(exception.InnerException);
#else
Assert.IsType<UriFormatException>(exception.InnerException);
#endif
}
[Fact]
public void NonSeekableRewriteMalformedUriCompatMode()
{
// Arrange
using var stream = GetStream(TestFiles.Malformed_uri_xlsx);
// Act/Assert
var exception = Assert.Throws<OpenXmlPackageException>(() => SpreadsheetDocument.Open(new NonSeekableStream(stream), isEditable: false, new OpenSettings { CompatibilityLevel = CompatibilityLevel.Version_2_20 }));
#if NETFRAMEWORK
Assert.IsType<ArgumentException>(exception.InnerException);
#else
Assert.IsType<UriFormatException>(exception.InnerException);
#endif
}
private sealed class NonSeekableStream : DelegatingStream
{
public NonSeekableStream(Stream innerStream)
: base(innerStream)
{
}
public override bool CanSeek => false;
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
public override long Position
{
get => throw new NotSupportedException();
set => throw new NotSupportedException();
}
public override long Length => throw new NotSupportedException();
public override void SetLength(long value) => throw new NotSupportedException();
}
}
}