-
Notifications
You must be signed in to change notification settings - Fork 43
/
SanitizedTests.cs
184 lines (169 loc) · 6 KB
/
SanitizedTests.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Abblix OIDC Server Library
// Copyright (c) Abblix LLP. All rights reserved.
//
// DISCLAIMER: This software is provided 'as-is', without any express or implied
// warranty. Use at your own risk. Abblix LLP is not liable for any damages
// arising from the use of this software.
//
// LICENSE RESTRICTIONS: This code may not be modified, copied, or redistributed
// in any form outside of the official GitHub repository at:
// https://github.com/Abblix/OIDC.Server. All development and modifications
// must occur within the official repository and are managed solely by Abblix LLP.
//
// Unauthorized use, modification, or distribution of this software is strictly
// prohibited and may be subject to legal action.
//
// For full licensing terms, please visit:
//
// https://oidc.abblix.com/license
//
// CONTACT: For license inquiries or permissions, contact Abblix LLP at
// info@abblix.com
namespace Abblix.Utils.UnitTests;
using Xunit;
/// <summary>
/// Contains unit tests for the <see cref="Sanitized"/> struct to ensure it correctly sanitizes input strings.
/// </summary>
public class SanitizedTests
{
/// <summary>
/// Tests that the original string is returned when no special characters are present.
/// </summary>
[Fact]
public void ToString_ShouldReturnOriginalString_WhenNoSpecialCharacters()
{
const string input = "HelloWorld";
var sanitizedValue = new Sanitized(input);
Assert.Equal(input, sanitizedValue.ToString());
}
/// <summary>
/// Tests that control characters are removed from the string.
/// </summary>
[Fact]
public void ToString_ShouldRemoveControlCharacters()
{
const string input = "Hello\x01\x02\x03World";
const string expected = "HelloWorld";
var sanitizedValue = new Sanitized(input);
Assert.Equal(expected, sanitizedValue.ToString());
}
/// <summary>
/// Tests that newline characters are replaced with their escaped representation.
/// </summary>
[Fact]
public void ToString_ShouldReplaceNewline()
{
const string input = "Hello\nWorld";
const string expected = "Hello\\nWorld";
var sanitizedValue = new Sanitized(input);
Assert.Equal(expected, sanitizedValue.ToString());
}
/// <summary>
/// Tests that carriage return characters are replaced with their escaped representation.
/// </summary>
[Fact]
public void ToString_ShouldReplaceCarriageReturn()
{
const string input = "Hello\rWorld";
const string expected = "Hello\\rWorld";
var sanitizedValue = new Sanitized(input);
Assert.Equal(expected, sanitizedValue.ToString());
}
/// <summary>
/// Tests that tab characters are replaced with their escaped representation.
/// </summary>
[Fact]
public void ToString_ShouldReplaceTab()
{
const string input = "Hello\tWorld";
const string expected = "Hello\\tWorld";
var sanitizedValue = new Sanitized(input);
Assert.Equal(expected, sanitizedValue.ToString());
}
/// <summary>
/// Tests that double quote characters are replaced with their escaped representation.
/// </summary>
[Fact]
public void ToString_ShouldReplaceDoubleQuote()
{
const string input = "Hello\"World";
const string expected = "Hello\\\"World";
var sanitizedValue = new Sanitized(input);
Assert.Equal(expected, sanitizedValue.ToString());
}
/// <summary>
/// Tests that single quote characters are replaced with their escaped representation.
/// </summary>
[Fact]
public void ToString_ShouldReplaceSingleQuote()
{
const string input = "Hello'World";
const string expected = "Hello\\'World";
var sanitizedValue = new Sanitized(input);
Assert.Equal(expected, sanitizedValue.ToString());
}
/// <summary>
/// Tests that backslash characters are replaced with their escaped representation.
/// </summary>
[Fact]
public void ToString_ShouldReplaceBackslash()
{
const string input = "Hello\\World";
const string expected = "Hello\\\\World";
var sanitizedValue = new Sanitized(input);
Assert.Equal(expected, sanitizedValue.ToString());
}
/// <summary>
/// Tests that comma characters are replaced with their escaped representation.
/// </summary>
[Fact]
public void ToString_ShouldReplaceComma()
{
const string input = "Hello,World";
const string expected = "Hello\\,World";
var sanitizedValue = new Sanitized(input);
Assert.Equal(expected, sanitizedValue.ToString());
}
/// <summary>
/// Tests that semicolon characters are replaced with their escaped representation.
/// </summary>
[Fact]
public void ToString_ShouldReplaceSemicolon()
{
const string input = "Hello;World";
const string expected = "Hello\\;World";
var sanitizedValue = new Sanitized(input);
Assert.Equal(expected, sanitizedValue.ToString());
}
/// <summary>
/// Tests that a null input returns null.
/// </summary>
[Fact]
public void ToString_ShouldHandleNullInput()
{
const string? input = null;
var sanitizedValue = new Sanitized(input);
Assert.Equal(string.Empty, sanitizedValue.ToString());
}
/// <summary>
/// Tests that an empty string remains unchanged.
/// </summary>
[Fact]
public void ToString_ShouldHandleEmptyString()
{
const string input = "";
var sanitizedValue = new Sanitized(input);
Assert.Equal(input, sanitizedValue.ToString());
}
/// <summary>
/// Tests that a string with only control characters is sanitized to an empty string.
/// </summary>
[Fact]
public void ToString_ShouldHandleStringWithOnlyControlCharacters()
{
const string input = "\x01\x02\x03";
const string expected = "";
var sanitizedValue = new Sanitized(input);
Assert.Equal(expected, sanitizedValue.ToString());
}
}