-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidatorTests.cs
More file actions
121 lines (101 loc) · 4.36 KB
/
Copy pathValidatorTests.cs
File metadata and controls
121 lines (101 loc) · 4.36 KB
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
using SmartFileKit.Domain;
using SmartFileKit.Validation;
using System.Text;
namespace SmartFileKit.Tests
{
public class ValidatorTests
{
[Fact]
public void Validate_ValidFile_ShouldPass()
{
// A valid PNG file named photo.png
byte[] fileBytes = new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
var result = FileValidator.Validate(fileBytes, "photo.png")
.MaxSize(5.MB())
.AllowedExtensions(".png", ".jpg")
.AllowedCategories(FileCategory.Image)
.Execute();
Assert.True(result.IsValid);
Assert.Empty(result.Errors);
}
[Fact]
public void Validate_TooLargeFile_ShouldFail()
{
// A 10KB PNG file but max size set to 5KB
byte[] fileBytes = new byte[10240];
fileBytes[0] = 0x89; fileBytes[1] = 0x50; fileBytes[2] = 0x4E; fileBytes[3] = 0x47;
var result = FileValidator.Validate(fileBytes, "photo.png")
.MaxSize(5.KB())
.Execute();
Assert.False(result.IsValid);
Assert.Contains(result.Errors, e => e.Contains("exceeds the maximum limit"));
}
[Fact]
public void Validate_ExtensionNotAllowed_ShouldFail()
{
byte[] fileBytes = new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
var result = FileValidator.Validate(fileBytes, "photo.png")
.AllowedExtensions(".jpg", ".gif")
.Execute();
Assert.False(result.IsValid);
Assert.Contains(result.Errors, e => e.Contains("extension '.png' is not allowed"));
}
[Fact]
public void Validate_CategoryNotAllowed_ShouldFail()
{
byte[] fileBytes = new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
var result = FileValidator.Validate(fileBytes, "photo.png")
.AllowedCategories(FileCategory.Document)
.Execute();
Assert.False(result.IsValid);
Assert.Contains(result.Errors, e => e.Contains("category 'Image' is not allowed"));
}
[Fact]
public void Validate_SpoofedFile_ShouldFail()
{
// Named safe.jpg, but contains Executable (MZ) bytes!
byte[] fileBytes = Encoding.ASCII.GetBytes("MZ\x90\x00\x03\x00\x00\x00...");
var result = FileValidator.Validate(fileBytes, "safe.jpg")
.Execute();
Assert.False(result.IsValid);
Assert.Contains(result.Errors, e => e.Contains("spoofing") || e.Contains("mismatch"));
}
[Fact]
public void Validate_SpoofedOfficeDoc_ShouldFail()
{
// Named report.docx, but is just a generic ZIP file containing malware script rather than word documents
byte[] fileBytes = new byte[100];
fileBytes[0] = 0x50; fileBytes[1] = 0x4B; fileBytes[2] = 0x03; fileBytes[3] = 0x04; // ZIP header
// But NO "word/" string inside!
var result = FileValidator.Validate(fileBytes, "report.docx")
.Execute();
Assert.False(result.IsValid);
Assert.Contains(result.Errors, e => e.Contains("spoofing") || e.Contains("mismatch"));
}
[Fact]
public void Validate_ThrowIfInvalid_ShouldThrowException()
{
byte[] fileBytes = Encoding.ASCII.GetBytes("MZ...");
var validator = FileValidator.Validate(fileBytes, "virus.jpg");
Assert.Throws<FileValidationException>(() => validator.ThrowIfInvalid());
}
[Fact]
public void Validate_EmptyFileNotAllowed_ShouldFail()
{
byte[] fileBytes = new byte[0];
var result = FileValidator.Validate(fileBytes, "empty.png")
.Execute();
Assert.False(result.IsValid);
Assert.Contains(result.Errors, e => e.Contains("empty"));
}
[Fact]
public void Validate_EmptyFileAllowed_ShouldPass()
{
byte[] fileBytes = new byte[0];
var result = FileValidator.Validate(fileBytes, "empty.png")
.AllowEmpty()
.Execute();
Assert.True(result.IsValid);
}
}
}