Skip to content

Commit 0d71e46

Browse files
committed
test: add coverage for LoadValidatedImage size limits
1 parent f7d3fed commit 0d71e46

3 files changed

Lines changed: 138 additions & 0 deletions

File tree

tests/PrivatePdfConverter.Tests/IntegrationTests/Commands/DirToPdfIntegrationTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,35 @@ public void ConvertDirectoryToOnePdf_ShouldBehaveCorrectly()
4444
// Clean up
4545
Directory.Delete(inputDirPath, true);
4646
}
47+
48+
[Fact]
49+
public void ConvertDirectoryToOnePdf_ShouldNotCreatePdf_WhenImageExceedsLimits()
50+
{
51+
// End-to-end smoke: dir command must not write a PDF when LoadValidatedImage rejects input.
52+
var inputDirPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
53+
const string outputFileName = "final.pdf";
54+
var outputPath = Path.Combine(inputDirPath, outputFileName);
55+
Directory.CreateDirectory(inputDirPath);
56+
57+
var oversizedImagePath = Path.Combine(inputDirPath, "oversized.png");
58+
59+
try
60+
{
61+
using (var image = new MagickImage(MagickColors.Red, 10_001, 100))
62+
{
63+
image.Write(oversizedImagePath);
64+
}
65+
66+
DirToPdf.ConvertDirectoryToOnePdf(inputDirPath, outputFileName);
67+
68+
File.Exists(outputPath).Should().BeFalse();
69+
}
70+
finally
71+
{
72+
if (Directory.Exists(inputDirPath))
73+
{
74+
Directory.Delete(inputDirPath, true);
75+
}
76+
}
77+
}
4778
}

tests/PrivatePdfConverter.Tests/IntegrationTests/Commands/ImgToPdfIntegrationTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,36 @@ public void ConvertImageToOnePdf_ShouldBehaveCorrectly()
3030
File.Delete(inputFilePath);
3131
File.Delete(outputPdfPath);
3232
}
33+
34+
[Fact]
35+
public void ConvertImageToOnePdf_ShouldNotCreatePdf_WhenImageExceedsLimits()
36+
{
37+
// End-to-end smoke: command must not write a PDF when LoadValidatedImage rejects input.
38+
var inputFilePath = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid():N}.png");
39+
var expectedOutputPath = Path.ChangeExtension(inputFilePath, ".pdf");
40+
41+
try
42+
{
43+
using (var image = new MagickImage(MagickColors.Red, 10_001, 100))
44+
{
45+
image.Write(inputFilePath);
46+
}
47+
48+
ImgToPdf.ConvertImageToOnePdf(inputFilePath, null);
49+
50+
File.Exists(expectedOutputPath).Should().BeFalse();
51+
}
52+
finally
53+
{
54+
if (File.Exists(inputFilePath))
55+
{
56+
File.Delete(inputFilePath);
57+
}
58+
59+
if (File.Exists(expectedOutputPath))
60+
{
61+
File.Delete(expectedOutputPath);
62+
}
63+
}
64+
}
3365
}

tests/PrivatePdfConverter.Tests/UnitTests/FileServiceUnitTests.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using ImageMagick;
12
using PrivatePdfConverter.Services;
23

34
namespace PrivatePdfConverter.Tests.UnitTests;
@@ -83,4 +84,78 @@ public void PrepareOutputFileName_WithEmptyOutput_ShouldDeriveFromSourcePath()
8384
// Assert
8485
result.Should().Be("file.pdf");
8586
}
87+
88+
[Fact]
89+
public void LoadValidatedImage_ShouldReturnImage_WhenWithinLimits()
90+
{
91+
var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid():N}.png");
92+
93+
try
94+
{
95+
using (var image = new MagickImage(MagickColors.Red, 100, 100))
96+
{
97+
image.Write(path);
98+
}
99+
100+
using var result = FileService.LoadValidatedImage(path);
101+
102+
result.Should().NotBeNull();
103+
result.Width.Should().Be(100);
104+
result.Height.Should().Be(100);
105+
}
106+
finally
107+
{
108+
if (File.Exists(path))
109+
{
110+
File.Delete(path);
111+
}
112+
}
113+
}
114+
115+
[Fact]
116+
public void LoadValidatedImage_ShouldReturnNull_WhenWidthExceedsLimit()
117+
{
118+
var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid():N}.png");
119+
120+
try
121+
{
122+
using (var image = new MagickImage(MagickColors.Red, 10_001, 100))
123+
{
124+
image.Write(path);
125+
}
126+
127+
FileService.LoadValidatedImage(path).Should().BeNull();
128+
}
129+
finally
130+
{
131+
if (File.Exists(path))
132+
{
133+
File.Delete(path);
134+
}
135+
}
136+
}
137+
138+
[Fact]
139+
public void LoadValidatedImage_ShouldReturnNull_WhenAreaExceedsLimit()
140+
{
141+
var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid():N}.png");
142+
143+
try
144+
{
145+
// Within per-side limits (10_000) but 5000 * 20_001 > 100_000_000 total area.
146+
using (var image = new MagickImage(MagickColors.Red, 5000, 20_001))
147+
{
148+
image.Write(path);
149+
}
150+
151+
FileService.LoadValidatedImage(path).Should().BeNull();
152+
}
153+
finally
154+
{
155+
if (File.Exists(path))
156+
{
157+
File.Delete(path);
158+
}
159+
}
160+
}
86161
}

0 commit comments

Comments
 (0)