Skip to content

Commit f8fdcaa

Browse files
committed
add tests for IsEncryptedOfficeFile and GetTestFilePath method
1 parent f697d4e commit f8fdcaa

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

test/DocumentFormat.OpenXml.Packaging.Tests/OpenXmlPackageTests.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,5 +342,55 @@ public void SucceedWithMissingCalcChainPart()
342342

343343
Assert.NotNull(spd);
344344
}
345+
346+
[Fact]
347+
public void IsEncryptedOfficeFile_ReturnsTrue_ForEncryptedFile()
348+
{
349+
using (Stream stream = GetStream(TestFiles.Encrypted_pptx, false))
350+
{
351+
Assert.True(OpenXmlPackage.IsEncryptedOfficeFile(stream));
352+
}
353+
}
354+
355+
[Fact]
356+
public void IsEncryptedOfficeFile_ReturnsFalse_ForUnencryptedFile()
357+
{
358+
using (Stream stream = GetStream(TestFiles.Presentation, false))
359+
{
360+
Assert.False(OpenXmlPackage.IsEncryptedOfficeFile(stream));
361+
}
362+
}
363+
364+
[Fact]
365+
public void IsEncryptedOfficeFile_ThrowsArgumentNullException_ForNullStream()
366+
{
367+
Assert.Throws<ArgumentNullException>(() => OpenXmlPackage.IsEncryptedOfficeFile((Stream)null!));
368+
}
369+
370+
[Fact]
371+
public void IsEncryptedOfficeFile_ThrowsArgumentException_ForUnseekableStream()
372+
{
373+
var unseekable = new UnseekableStream();
374+
Assert.Throws<ArgumentException>(() => OpenXmlPackage.IsEncryptedOfficeFile(unseekable));
375+
}
376+
377+
private class UnseekableStream : MemoryStream
378+
{
379+
public override bool CanSeek => false;
380+
}
381+
382+
[Fact]
383+
public void IsEncryptedOfficeFile_ReturnsTrue_ForEncryptedFilePath()
384+
{
385+
string filePath = GetTestFilePath(TestFiles.Encrypted_pptx);
386+
Assert.True(OpenXmlPackage.IsEncryptedOfficeFile(filePath));
387+
}
388+
389+
[Fact]
390+
public void IsEncryptedOfficeFile_ReturnsFalse_ForUnencryptedFile_FromString()
391+
{
392+
string filePath = GetTestFilePath(TestFiles.Presentation);
393+
Assert.False(OpenXmlPackage.IsEncryptedOfficeFile(filePath));
394+
}
345395
}
346396
}

test/DocumentFormat.OpenXml.Tests.Assets/TestAssets.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,26 @@ public static Stream GetStream(string name, bool isEditable)
7171
return isEditable ? stream.AsMemoryStream() : stream;
7272
}
7373

74+
/// <summary>
75+
/// Extracts an embedded test resource to a temporary file and returns its file path.
76+
/// </summary>
77+
/// <param name="resourceName">The name of the embedded resource to extract.</param>
78+
/// <returns>The full path to the temporary file containing the resource data.</returns>
79+
/// <remarks>
80+
/// The caller is responsible for deleting the temporary file after use.
81+
/// </remarks>
82+
public static string GetTestFilePath(string resourceName)
83+
{
84+
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + Path.GetExtension(resourceName));
85+
86+
using (Stream stream = GetStream(resourceName, false))
87+
using (FileStream fileStream = File.Create(tempPath))
88+
{
89+
stream.CopyTo(fileStream);
90+
return tempPath;
91+
}
92+
}
93+
7494
private static Stream AsMemoryStream(this Stream stream)
7595
{
7696
if (stream is MemoryStream ms)

0 commit comments

Comments
 (0)