forked from omx5o/JoinFS
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProgramCodeTests.cs
More file actions
345 lines (271 loc) · 9.22 KB
/
Copy pathProgramCodeTests.cs
File metadata and controls
345 lines (271 loc) · 9.22 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
namespace JoinFS.Tests;
/// <summary>
/// Tests for the Program.Code(...) method which provides encoding/decoding functionality.
/// The Code method is a bidirectional cipher that uses Caesar substitution and random number generation.
/// </summary>
public class ProgramCodeTests
{
// Standard test key for consistency
private const int TestKey = 1234;
#region Null and Empty String Tests
[Fact]
public void Code_WithNullString_ReturnsNull()
{
// Arrange
string? input = null;
// Act
string? encodedResult = ProgramCode.Code(input, true, TestKey);
string? decodedResult = ProgramCode.Code(input, false, TestKey);
// Assert
Assert.Null(encodedResult);
Assert.Null(decodedResult);
}
[Fact]
public void Code_WithEmptyString_ReturnsEmptyString()
{
// Arrange
string input = string.Empty;
// Act
string? encodedResult = ProgramCode.Code(input, true, TestKey);
string? decodedResult = ProgramCode.Code(input, false, TestKey);
// Assert
Assert.Equal(string.Empty, encodedResult);
Assert.Equal(string.Empty, decodedResult);
}
#endregion
#region Round-Trip Tests
[Theory]
[InlineData("Hello World")]
[InlineData("Test123")]
[InlineData("https://example.com")]
[InlineData("user@email.com")]
[InlineData("Path/To/File.txt")]
[InlineData("!@#$%^&*()")]
[InlineData("ABC")]
[InlineData("a")]
public void Code_RoundTrip_RestoresOriginalString(string original)
{
// Act
string? encoded = ProgramCode.Code(original, true, TestKey);
string? decoded = ProgramCode.Code(encoded, false, TestKey);
// Assert
Assert.NotEqual(original, encoded); // Encoded should be different
Assert.Equal(original, decoded); // Decoded should match original
}
[Fact]
public void Code_RoundTrip_WithAllPrintableAsciiCharacters()
{
// Arrange - Build a string with all printable ASCII characters (! to ~)
var sb = new System.Text.StringBuilder();
for (char c = '!'; c <= '~'; c++)
{
sb.Append(c);
}
string original = sb.ToString();
// Act
string? encoded = ProgramCode.Code(original, true, TestKey);
string? decoded = ProgramCode.Code(encoded, false, TestKey);
// Assert
Assert.NotEqual(original, encoded);
Assert.Equal(original, decoded);
}
#endregion
#region Key Sensitivity Tests
[Fact]
public void Code_DifferentKeys_ProduceDifferentEncodings()
{
// Arrange
string input = "Test String";
int key1 = 1234;
int key2 = 5678;
// Act
string? encoded1 = ProgramCode.Code(input, true, key1);
string? encoded2 = ProgramCode.Code(input, true, key2);
// Assert
Assert.NotEqual(encoded1, encoded2);
}
[Fact]
public void Code_WrongKey_DoesNotRestoreOriginal()
{
// Arrange
string input = "Secret Message";
int encodeKey = 1234;
int wrongKey = 5678;
// Act
string? encoded = ProgramCode.Code(input, true, encodeKey);
string? decodedWithWrongKey = ProgramCode.Code(encoded, false, wrongKey);
// Assert
Assert.NotEqual(input, decodedWithWrongKey);
}
[Fact]
public void Code_SameKeyUsedTwice_RestoresOriginal()
{
// Arrange
string input = "Consistent Key Test";
int key = 9999;
// Act
string? encoded = ProgramCode.Code(input, true, key);
string? decoded = ProgramCode.Code(encoded, false, key);
// Assert
Assert.Equal(input, decoded);
}
#endregion
#region Special Character Tests
[Theory]
[InlineData("!")]
[InlineData("~")]
[InlineData("!~")]
[InlineData("!!!")]
[InlineData("~~~")]
public void Code_WithBoundaryCharacters_RoundTripsCorrectly(string input)
{
// Act
string? encoded = ProgramCode.Code(input, true, TestKey);
string? decoded = ProgramCode.Code(encoded, false, TestKey);
// Assert
Assert.Equal(input, decoded);
}
[Fact]
public void Code_WithMixedSpecialCharacters_RoundTripsCorrectly()
{
// Arrange
string input = "Hello!@#$%^&*()_+-=[]{}|;':\",./<>?`~World";
// Act
string? encoded = ProgramCode.Code(input, true, TestKey);
string? decoded = ProgramCode.Code(encoded, false, TestKey);
// Assert
Assert.Equal(input, decoded);
}
#endregion
#region Edge Cases with Invalid Characters
[Fact]
public void Code_WithOnlyInvalidCharacters_ReturnsEmptyString()
{
// Arrange - Characters outside the valid range (! to ~)
string input = "\t\n\r";
// Act
string? encoded = ProgramCode.Code(input, true, TestKey);
// Assert
Assert.Equal(string.Empty, encoded);
}
[Fact]
public void Code_WithInvalidCharactersAtBoundaries_TrimsCorrectly()
{
// Arrange - Invalid chars at start and end
string input = "\t\nHello World\r\n";
string expectedTrimmed = "Hello World";
// Act
string? encoded = ProgramCode.Code(input, true, TestKey);
string? decoded = ProgramCode.Code(encoded, false, TestKey);
// Assert
Assert.Equal(expectedTrimmed, decoded);
}
#endregion
#region Encoding/Decoding Properties
[Fact]
public void Code_Encoding_ProducesDifferentOutput()
{
// Arrange
string input = "This should be different";
// Act
string? encoded = ProgramCode.Code(input, true, TestKey);
// Assert
Assert.NotEqual(input, encoded);
Assert.NotEmpty(encoded!);
}
[Fact]
public void Code_EncodingSameStringTwice_ProducesSameResult()
{
// Arrange
string input = "Deterministic Test";
// Act
string? encoded1 = ProgramCode.Code(input, true, TestKey);
string? encoded2 = ProgramCode.Code(input, true, TestKey);
// Assert
Assert.Equal(encoded1, encoded2);
}
[Fact]
public void Code_EncodedLength_MatchesValidCharacterCount()
{
// Arrange
string input = "Test";
// Act
string? encoded = ProgramCode.Code(input, true, TestKey);
// Assert
// The encoded string should have the same length as the original
// (assuming all characters are valid)
Assert.Equal(input.Length, encoded?.Length);
}
#endregion
#region Multiple Round-Trips
[Fact]
public void Code_MultipleRoundTrips_MaintainsIntegrity()
{
// Arrange
string original = "Multiple Round Trip Test";
// Act & Assert - Perform 5 round trips
string? current = original;
for (int i = 0; i < 5; i++)
{
string? encoded = ProgramCode.Code(current, true, TestKey);
string? decoded = ProgramCode.Code(encoded, false, TestKey);
Assert.Equal(current, decoded);
current = decoded;
}
Assert.Equal(original, current);
}
#endregion
#region Real-World Usage Scenarios
[Fact]
public void Code_WithUrl_RoundTripsCorrectly()
{
// Arrange
string url = "https://joinfs.net/download/file.zip";
// Act
string? encoded = ProgramCode.Code(url, true, TestKey);
string? decoded = ProgramCode.Code(encoded, false, TestKey);
// Assert
Assert.Equal(url, decoded);
}
[Fact]
public void Code_WithFilePath_RoundTripsCorrectly()
{
// Arrange
string path = "C:/Program Files/JoinFS/config.xml";
// Act
string? encoded = ProgramCode.Code(path, true, TestKey);
string? decoded = ProgramCode.Code(encoded, false, TestKey);
// Assert
Assert.Equal(path, decoded);
}
[Fact]
public void Code_WithLongString_RoundTripsCorrectly()
{
// Arrange
string longString = new string('A', 1000) + " test " + new string('Z', 1000);
// Act
string? encoded = ProgramCode.Code(longString, true, TestKey);
string? decoded = ProgramCode.Code(encoded, false, TestKey);
// Assert
Assert.Equal(longString, decoded);
}
#endregion
#region Key Value Tests
[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(-1)]
[InlineData(int.MaxValue)]
[InlineData(int.MinValue)]
public void Code_WithVariousKeys_RoundTripsCorrectly(int key)
{
// Arrange
string input = "Key variation test";
// Act
string? encoded = ProgramCode.Code(input, true, key);
string? decoded = ProgramCode.Code(encoded, false, key);
// Assert
Assert.Equal(input, decoded);
}
#endregion
}