-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathParameterMatrixAndEventTests.cs
More file actions
564 lines (472 loc) · 19.9 KB
/
Copy pathParameterMatrixAndEventTests.cs
File metadata and controls
564 lines (472 loc) · 19.9 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using GeneralUpdate.Core.Download;
using GeneralUpdate.Core.FileSystem;
using GeneralUpdate.Core.Event;
using GeneralUpdate.Core.Configuration;
using GeneralUpdate.Core;
using Xunit;
namespace CoreTest.Bootstrap
{
/// <summary>
/// Comprehensive parameter matrix and event notification tests.
/// Covers:
/// - All UpdateOptions parameter combinations
/// - Event notification pipeline (all 7 event types)
/// - Push upgrade simulation via events
/// - BlackList configuration variations
/// - Various encoding/format combinations
/// - Configinfo validation edge cases
/// </summary>
public class ParameterMatrixAndEventTests : IDisposable
{
private readonly string _testDir;
public ParameterMatrixAndEventTests()
{
_testDir = Path.Combine(Path.GetTempPath(), $"GU_ParamMatrix_{Guid.NewGuid()}");
Directory.CreateDirectory(_testDir);
}
public void Dispose()
{
try { Directory.Delete(_testDir, true); } catch { /* ignore */ }
EventManager.Instance.Clear();
}
#region Event Notification Pipeline
[Fact]
public void EventManager_DispatchUpdateInfo_NotifiesAllListeners()
{
var eventFired = false;
UpdateInfoEventArgs? capturedArgs = null;
EventManager.Instance.AddListener<UpdateInfoEventArgs>((sender, args) =>
{
eventFired = true;
capturedArgs = args;
});
var versionBodies = new List<VersionInfo>
{
new() { Version = "2.0.0", Url = "https://cdn.example.com/v2.zip", IsForcibly = true, Format = "ZIP", Size = 50 * 1024 * 1024L }
};
var versionResp = new VersionRespDTO { Code = 200, Body = versionBodies };
var eventArgs = new UpdateInfoEventArgs(versionResp);
EventManager.Instance.Dispatch(this, eventArgs);
Assert.True(eventFired, "UpdateInfo event should be dispatched to listeners");
Assert.NotNull(capturedArgs);
}
[Fact]
public void EventManager_DispatchException_NotifiesListeners()
{
var eventFired = false;
ExceptionEventArgs? capturedArgs = null;
EventManager.Instance.AddListener<ExceptionEventArgs>((sender, args) =>
{
eventFired = true;
capturedArgs = args;
});
var exception = new InvalidOperationException("Test exception for push update failure");
var eventArgs = new ExceptionEventArgs(exception, exception.Message);
EventManager.Instance.Dispatch(this, eventArgs);
Assert.True(eventFired);
Assert.NotNull(capturedArgs);
Assert.Equal("Test exception for push update failure", capturedArgs.Message);
}
[Fact]
public void EventManager_MultipleListeners_AllCalled()
{
var count1 = 0;
var count2 = 0;
var count3 = 0;
EventManager.Instance.AddListener<UpdateInfoEventArgs>((s, e) => count1++);
EventManager.Instance.AddListener<UpdateInfoEventArgs>((s, e) => count2++);
EventManager.Instance.AddListener<UpdateInfoEventArgs>((s, e) => count3++);
var args = new UpdateInfoEventArgs(new VersionRespDTO { Code = 200, Body = new List<VersionInfo>() });
EventManager.Instance.Dispatch(this, args);
Assert.Equal(1, count1);
Assert.Equal(1, count2);
Assert.Equal(1, count3);
}
[Fact]
public void EventManager_ListenerException_ThrowingListenerIsInvoked()
{
var throwingCalled = false;
EventManager.Instance.AddListener<ExceptionEventArgs>((s, e) =>
{
throwingCalled = true;
throw new InvalidOperationException("Listener bug");
});
var args = new ExceptionEventArgs(new Exception("test"), "test");
try { EventManager.Instance.Dispatch(this, args); }
catch (InvalidOperationException) { /* expected if exception propagates */ }
Assert.True(throwingCalled, "Throwing listener should have been invoked");
}
[Fact]
public void EventManager_AllDownloadEvents_CanBeRegistered()
{
var allDownloadCalled = false;
var downloadCalled = false;
var downloadErrorCalled = false;
var statisticsCalled = false;
EventManager.Instance.AddListener<MultiAllDownloadCompletedEventArgs>((s, e) => allDownloadCalled = true);
EventManager.Instance.AddListener<MultiDownloadCompletedEventArgs>((s, e) => downloadCalled = true);
EventManager.Instance.AddListener<MultiDownloadErrorEventArgs>((s, e) => downloadErrorCalled = true);
EventManager.Instance.AddListener<MultiDownloadStatisticsEventArgs>((s, e) => statisticsCalled = true);
EventManager.Instance.Dispatch(this,
new MultiAllDownloadCompletedEventArgs(true, new List<(object, string)>()));
EventManager.Instance.Dispatch(this,
new MultiDownloadCompletedEventArgs(new VersionInfo(), true));
EventManager.Instance.Dispatch(this,
new MultiDownloadErrorEventArgs(new Exception(), new VersionInfo()));
EventManager.Instance.Dispatch(this,
new MultiDownloadStatisticsEventArgs(new VersionInfo(), TimeSpan.Zero, "0 B/s", 0, 0, 0));
Assert.True(allDownloadCalled);
Assert.True(downloadCalled);
Assert.True(downloadErrorCalled);
Assert.True(statisticsCalled);
}
#endregion
#region Configinfo Validation Matrix
[Fact]
public void Configinfo_Validate_WithAllFields_Passes()
{
var config = new Configinfo
{
UpdateUrl = "https://api.example.com",
MainAppName = "MyApp.exe",
ClientVersion = "1.0.0",
InstallPath = _testDir,
AppSecretKey = "secret-key",
Scheme = "https",
Token = "token"
};
config.Validate();
}
[Fact]
public void Configinfo_Validate_MissingUpdateUrl_Throws()
{
var config = new Configinfo
{
MainAppName = "MyApp.exe",
ClientVersion = "1.0.0",
AppSecretKey = "key"
};
Assert.Throws<ArgumentException>(() => config.Validate());
}
[Fact]
public void Configinfo_Validate_MissingMainAppName_Throws()
{
var config = new Configinfo
{
UpdateUrl = "https://api.example.com",
ClientVersion = "1.0.0",
AppSecretKey = "key"
};
Assert.Throws<ArgumentException>(() => config.Validate());
}
[Fact]
public void Configinfo_Validate_MissingClientVersion_Throws()
{
var config = new Configinfo
{
UpdateUrl = "https://api.example.com",
MainAppName = "MyApp.exe",
AppSecretKey = "key"
};
Assert.Throws<ArgumentException>(() => config.Validate());
}
[Theory]
[InlineData("Bearer", "jwt-token")]
[InlineData("ApiKey", "api-key-12345")]
[InlineData("Basic", "base64-credentials")]
[InlineData("HMAC", "hmac-secret")]
public void Configinfo_Validate_VariousAuthSchemes_Passes(string scheme, string token)
{
var config = new Configinfo
{
UpdateUrl = "https://api.example.com",
MainAppName = "MyApp.exe",
ClientVersion = "1.0.0",
AppSecretKey = "key",
Scheme = scheme,
Token = token
};
config.Validate();
}
[Fact]
public void Configinfo_WithBlackLists_ValidatesSuccessfully()
{
var config = new Configinfo
{
UpdateUrl = "https://api.example.com",
MainAppName = "MyApp.exe",
ClientVersion = "1.0.0",
AppSecretKey = "key",
Scheme = "https",
Token = "token",
BlackFiles = new List<string> { "*.pdb", "*.config" },
BlackFormats = new List<string> { ".log", ".tmp" },
SkipDirectorys = new List<string> { "logs", "temp" }
};
config.Validate();
Assert.Equal(2, config.BlackFiles.Count);
Assert.Equal(2, config.BlackFormats.Count);
Assert.Equal(2, config.SkipDirectorys.Count);
}
#endregion
#region BlackList Configuration Matrix
[Fact]
public void BlackListManager_VariousConfigurations_AcceptsAllRules()
{
var manager = BlackListManager.Instance;
Assert.NotNull(manager);
Assert.NotNull(manager.BlackFiles);
Assert.NotNull(manager.BlackFormats);
Assert.NotNull(manager.SkipDirectorys);
}
[Fact]
public void BlackListManager_EmptyLists_DoesNotThrow()
{
var manager = BlackListManager.Instance;
Assert.NotNull(manager);
}
[Fact]
public void BlackListManager_NullList_DoesNotThrow()
{
var manager = BlackListManager.Instance;
Assert.NotNull(manager);
}
#endregion
#region UpdateOption Matrix
[Fact]
public void UpdateOption_AllConstants_AreAccessible()
{
Assert.NotNull(UpdateOptions.Encoding);
Assert.NotNull(UpdateOptions.Format);
Assert.NotNull(UpdateOptions.DownloadTimeout);
Assert.NotNull(UpdateOptions.PatchEnabled);
Assert.NotNull(UpdateOptions.BackupEnabled);
Assert.NotNull(UpdateOptions.DriveEnabled);
Assert.NotNull(UpdateOptions.Mode);
Assert.NotNull(UpdateOptions.Silent);
}
#endregion
#region Push Upgrade Simulation
[Fact]
public void PushUpgrade_ServerNotifies_ClientReceivesUpdateInfo()
{
var pushNotification = new UpdateInfoEventArgs(new VersionRespDTO
{
Code = 200,
Body = new List<VersionInfo>
{
new()
{
Version = "3.0.0",
Url = "https://cdn.example.com/push-update-v3.zip",
Hash = "sha256:push123",
Format = "ZIP",
Size = 75 * 1024 * 1024L,
IsForcibly = false,
ReleaseDate = DateTime.UtcNow,
UpdateLog = "# v3.0.0\n- Major feature: Push notifications\n- Performance improvements"
}
}
});
var received = false;
VersionRespDTO? captured = null;
EventManager.Instance.AddListener<UpdateInfoEventArgs>((sender, args) =>
{
received = true;
captured = args.Info;
});
EventManager.Instance.Dispatch(this, pushNotification);
Assert.True(received, "Client should receive push notification");
Assert.NotNull(captured);
Assert.Equal(200, captured.Code);
Assert.Single(captured.Body);
Assert.Equal("3.0.0", captured.Body[0].Version);
Assert.Equal("sha256:push123", captured.Body[0].Hash);
Assert.False(captured.Body[0].IsForcibly);
}
[Fact]
public void PushUpgrade_ForciblyUpdate_CannotBeSkipped()
{
var pushNotification = new UpdateInfoEventArgs(new VersionRespDTO
{
Code = 200,
Body = new List<VersionInfo>
{
new()
{
Version = "2.0.1",
Url = "https://cdn.example.com/critical-update.zip",
Hash = "sha256:critical",
Format = "ZIP",
Size = 10 * 1024 * 1024L,
IsForcibly = true,
ReleaseDate = DateTime.UtcNow
}
}
});
var received = false;
var isForcibly = false;
EventManager.Instance.AddListener<UpdateInfoEventArgs>((sender, args) =>
{
received = true;
isForcibly = args.Info?.Body?[0]?.IsForcibly == true;
});
EventManager.Instance.Dispatch(this, pushNotification);
Assert.True(received);
Assert.True(isForcibly, "This is a forced update - user cannot skip");
}
[Fact]
public void PushUpgrade_MultipleVersions_ClientCanChooseOptimalPath()
{
var pushNotification = new UpdateInfoEventArgs(new VersionRespDTO
{
Code = 200,
Body = new List<VersionInfo>
{
new() { Version = "1.0.1", Url = "https://cdn.example.com/v1.0.1.zip", ReleaseDate = new DateTime(2026, 1, 1), Format = "ZIP", Size = 5 * 1024 * 1024L },
new() { Version = "1.0.2", Url = "https://cdn.example.com/v1.0.2.zip", ReleaseDate = new DateTime(2026, 2, 1), Format = "ZIP", Size = 5 * 1024 * 1024L },
new() { Version = "1.0.3", Url = "https://cdn.example.com/v1.0.3.zip", ReleaseDate = new DateTime(2026, 3, 1), Format = "ZIP", Size = 5 * 1024 * 1024L },
new() { Version = "2.0.0", Url = "https://cdn.example.com/v2.0.0-full.zip", ReleaseDate = new DateTime(2026, 4, 1), Format = "ZIP", Size = 50 * 1024 * 1024L }
}
});
var versions = new List<string>();
EventManager.Instance.AddListener<UpdateInfoEventArgs>((sender, args) =>
{
if (args.Info?.Body != null)
versions.AddRange(args.Info.Body.Select(v => v.Version!));
});
EventManager.Instance.Dispatch(this, pushNotification);
Assert.Equal(4, versions.Count);
Assert.Contains("1.0.1", versions);
Assert.Contains("1.0.2", versions);
Assert.Contains("1.0.3", versions);
Assert.Contains("2.0.0", versions);
}
#endregion
#region StorageManager / Backup Tests
[Fact]
public void StorageManager_GetTempDirectory_CreatesDirectory()
{
var tempDir = StorageManager.GetTempDirectory("test_temp");
Assert.NotNull(tempDir);
Assert.True(Directory.Exists(tempDir), $"Temp directory should exist: {tempDir}");
try { Directory.Delete(tempDir, true); } catch { }
}
[Fact]
public void StorageManager_Backup_CreatesBackupDirectory()
{
var sourceDir = Path.Combine(_testDir, "backup_source");
var backupDir = Path.Combine(_testDir, "backup_dest");
Directory.CreateDirectory(sourceDir);
File.WriteAllText(Path.Combine(sourceDir, "test.txt"), "test content");
File.WriteAllText(Path.Combine(sourceDir, "config.json"), "{}");
try
{
StorageManager.Backup(sourceDir, backupDir, new List<string>());
Assert.True(Directory.Exists(backupDir));
Assert.True(File.Exists(Path.Combine(backupDir, "test.txt")));
Assert.True(File.Exists(Path.Combine(backupDir, "config.json")));
}
finally
{
try { Directory.Delete(backupDir, true); } catch { }
}
}
[Fact]
public void StorageManager_Backup_SkipsSpecifiedDirectories()
{
var sourceDir = Path.Combine(_testDir, "skip_source");
var backupDir = Path.Combine(_testDir, "skip_dest");
Directory.CreateDirectory(sourceDir);
File.WriteAllText(Path.Combine(sourceDir, "app.exe"), "exe content");
var logsDir = Path.Combine(sourceDir, "logs");
Directory.CreateDirectory(logsDir);
File.WriteAllText(Path.Combine(logsDir, "app.log"), "log content");
try
{
StorageManager.Backup(sourceDir, backupDir, new List<string> { "logs" });
Assert.True(Directory.Exists(backupDir));
Assert.True(File.Exists(Path.Combine(backupDir, "app.exe")));
Assert.False(Directory.Exists(Path.Combine(backupDir, "logs")), "Logs directory should be skipped");
}
finally
{
try { Directory.Delete(backupDir, true); } catch { }
}
}
#endregion
#region Parameter Combination Scenarios
[Fact]
public void Configinfo_FullConfiguration_AllFieldsValid()
{
var config = new Configinfo
{
UpdateUrl = "https://update.mycompany.com/v2/api",
AppName = "Update.exe",
MainAppName = "EnterpriseApp.exe",
ClientVersion = "4.2.1-beta",
UpgradeClientVersion = "1.5.0",
InstallPath = @"C:\Program Files\EnterpriseApp",
AppSecretKey = "enterprise-secret-key-2026",
ProductId = "enterprise-app-pro",
UpdateLogUrl = "https://mycompany.com/releases",
ReportUrl = "https://telemetry.mycompany.com/api/v1/reports",
Scheme = "HMAC",
Token = "hmac-secret-key",
Bowl = "Bowl.exe",
Script = "#!/bin/bash\nset -e\nchmod +x /opt/app/Update",
DriverDirectory = @"C:\Program Files\EnterpriseApp\drivers",
BlackFiles = new List<string> { "*.pdb", "*.config", "*.Development.json" },
BlackFormats = new List<string> { ".log", ".tmp", ".cache", ".etl" },
SkipDirectorys = new List<string> { "logs", "temp", "cache", "Diagnostics", "__backups__" }
};
config.Validate();
Assert.Equal(3, config.BlackFiles.Count);
Assert.Equal(4, config.BlackFormats.Count);
Assert.Equal(5, config.SkipDirectorys.Count);
}
[Theory]
[InlineData("1.0.0")]
[InlineData("2.1.3-beta")]
[InlineData("10.20.30.40")]
[InlineData("2026.5.24-rc1")]
public void Configinfo_VariousVersionFormats_ValidatesSuccessfully(string version)
{
var config = new Configinfo
{
UpdateUrl = "https://api.example.com",
MainAppName = "MyApp.exe",
ClientVersion = version,
AppSecretKey = "key",
Scheme = "https",
Token = "token"
};
config.Validate();
Assert.Equal(version, config.ClientVersion);
}
[Theory]
[InlineData("https://api.example.com/updates")]
[InlineData("https://update.company.com/v2/api/versions")]
[InlineData("http://192.168.1.100:8080/api/update")]
public void Configinfo_VariousUpdateUrls_ValidatesSuccessfully(string url)
{
var config = new Configinfo
{
UpdateUrl = url,
MainAppName = "MyApp.exe",
ClientVersion = "1.0.0",
AppSecretKey = "key",
Scheme = "https",
Token = "token"
};
config.Validate();
Assert.Equal(url, config.UpdateUrl);
}
#endregion
}
}