using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using ClassNotify.Server.Configuration;
using ClassNotify.Server.Data;
using ClassNotify.Server.Domain.Entities;
using ClassNotify.Server.Services;
using ClassNotify.Shared.Enums;
using ClassNotify.Shared.Models;
using ClassNotify.Shared.Security;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
namespace ClassNotify.FleetTests;
///
/// ClassNotify P2.5 RC1-4 Phase 2: Automatic Rollback (单设备坏版本自动逃生) 严格门禁测试套件
/// 覆盖 U11 ~ U20 全部验收指标 (10 项严格门禁)
///
public static class DeviceUpdatePhase2Tests
{
private static readonly JsonSerializerOptions JsonOpts = new()
{
PropertyNameCaseInsensitive = true,
WriteIndented = true
};
public static async Task RunAllTestsAsync(Func, Task> runTest)
{
Console.WriteLine("\n==========================================================");
Console.WriteLine(" ClassNotify P2.5 RC1-4 Phase 2: Automatic Rollback");
Console.WriteLine("==========================================================");
await runTest("U11: Backup Integrity (SHA256 manifest; corrupted backup fails closed without touching target)", Test_U11_BackupIntegrity);
await runTest("U12: Exact Restore (Rollback deletes unmanifested bad files like BadOnly.dll and matches manifest 100%)", Test_U12_ExactRestore);
await runTest("U13: Service Start Failure Rollback (Scenario A: Bad service failing to start triggers rollback to old version)", Test_U13_ServiceStartFailureRollback);
await runTest("U14: Post-update Critical Health Failure (Scenario B: Silent bad version with dead IPC/WPF triggers rollback)", Test_U14_PostUpdateHealthFailureRollback);
await runTest("U15: Crash Loop Rollback (Scenario A: >=3 crashes in 60s triggers crash loop detection and rollback)", Test_U15_CrashLoopRollback);
await runTest("U16: Identity Preservation Through Rollback (DeviceId, Token, TenantId, ClassroomId untouched)", Test_U16_IdentityPreservation);
await runTest("U17: Rollback Health Verification (Heartbeat, Service, and Notification+TTS+ACK loop succeed)", Test_U17_RollbackHealthVerification);
await runTest("U18: RollbackFailed Truthfulness (Corrupted backup triggers RollbackFailed, fail-stop, no infinite loop)", Test_U18_RollbackFailedTruthfulness);
await runTest("U19: Agent / Power Interruption Recovery (Durable journal detects interrupted state and auto-recovers)", Test_U19_InterruptionRecovery);
await runTest("U20: Network-only Failure Discrimination (Temporary cloud outage does not cause premature rollback)", Test_U20_NetworkFailureDiscrimination);
}
#region Helpers
private static string HashDeviceToken(string token)
{
using var sha = SHA256.Create();
var bytes = sha.ComputeHash(Encoding.UTF8.GetBytes(token));
return Convert.ToBase64String(bytes);
}
private static async Task SeedTenantAndDeviceAsync(
ClassNotifyDbContext db,
string tenantId,
string tenantName,
string deviceId,
string rawToken = "valid_tok",
string serviceVersion = "2.5.0")
{
var tenant = await db.Tenants.FirstOrDefaultAsync(t => t.Id == tenantId);
if (tenant == null)
{
tenant = new Tenant
{
Id = tenantId,
Name = tenantName,
IsActive = true,
CreatedAt = DateTimeOffset.UtcNow
};
db.Tenants.Add(tenant);
}
var classroom = await db.Classrooms.FirstOrDefaultAsync(c => c.TenantId == tenantId && c.Id == $"c_{tenantId}");
if (classroom == null)
{
classroom = new Classroom
{
Id = $"c_{tenantId}",
TenantId = tenantId,
DisplayName = $"Classroom {tenantId}"
};
db.Classrooms.Add(classroom);
}
var device = await db.Devices.FirstOrDefaultAsync(d => d.Id == deviceId);
if (device == null)
{
device = new Device
{
Id = deviceId,
TenantId = tenantId,
ClassroomId = classroom.Id,
DeviceTokenHash = HashDeviceToken(rawToken),
Hostname = $"HOST-{deviceId}",
ServiceVersion = serviceVersion,
ClientVersion = serviceVersion,
ProtocolVersion = 2,
LastHeartbeatAt = DateTimeOffset.UtcNow,
IsActive = true,
CreatedAt = DateTimeOffset.UtcNow
};
db.Devices.Add(device);
}
await db.SaveChangesAsync();
}
private static byte[] CreatePackageZip(
string version,
string architecture,
Dictionary? payloadFiles = null)
{
using var ms = new MemoryStream();
using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, true))
{
var manifest = new UpdateManifestV1
{
FormatVersion = 1,
Version = version,
Architecture = architecture,
CreatedAtUtc = DateTimeOffset.UtcNow
};
var manifestBytes = JsonSerializer.SerializeToUtf8Bytes(manifest, new JsonSerializerOptions { WriteIndented = true });
var manifestEntry = archive.CreateEntry("manifest.json", CompressionLevel.Optimal);
using (var s = manifestEntry.Open())
{
s.Write(manifestBytes, 0, manifestBytes.Length);
}
var files = payloadFiles ?? new Dictionary
{
["ClassNotify.Service.dll"] = Encoding.UTF8.GetBytes("simulated_service_binary_v" + version),
["ClassNotify.Client.dll"] = Encoding.UTF8.GetBytes("simulated_client_binary_v" + version)
};
foreach (var kvp in files)
{
var entry = archive.CreateEntry("payload/" + kvp.Key, CompressionLevel.Optimal);
using var s = entry.Open();
s.Write(kvp.Value, 0, kvp.Value.Length);
}
}
return ms.ToArray();
}
private static string ComputeSha256Hex(string filePath)
{
using var sha = SHA256.Create();
using var stream = File.OpenRead(filePath);
return Convert.ToHexString(sha.ComputeHash(stream)).ToLowerInvariant();
}
#endregion
#region Gate Tests
///
/// U11: Backup Integrity
/// 升级前生成 backup-manifest.json 包含每个文件 SHA256。
/// 若破坏备份目录一个字节,立即阻止安装并安全退出 (fail-closed),目标安装目录保持 100% 原样未动!
///
private static async Task Test_U11_BackupIntegrity()
{
var tempRoot = Path.Combine(Path.GetTempPath(), "cn_u11_" + Guid.NewGuid().ToString("N"));
var targetDir = Path.Combine(tempRoot, "target");
var backupDir = Path.Combine(tempRoot, "backup");
var stagingZip = Path.Combine(tempRoot, "staging.zip");
var journalPath = Path.Combine(tempRoot, "update-journal.json");
try
{
Directory.CreateDirectory(targetDir);
// 1. 初始化原始安装目录 (v2.5.0)
var originalSvcContent = "original_v2_5_0_service_bytes";
var originalCliContent = "original_v2_5_0_client_bytes";
var originalCfgContent = "{\"env\":\"production\"}";
await File.WriteAllTextAsync(Path.Combine(targetDir, "ClassNotify.Service.dll"), originalSvcContent);
await File.WriteAllTextAsync(Path.Combine(targetDir, "ClassNotify.Client.dll"), originalCliContent);
await File.WriteAllTextAsync(Path.Combine(targetDir, "config.json"), originalCfgContent);
var originalSvcSha = ComputeSha256Hex(Path.Combine(targetDir, "ClassNotify.Service.dll"));
// 2. 准备新版本升级包 (2.6.0)
var newPackageBytes = CreatePackageZip("2.6.0", "win-x64");
await File.WriteAllBytesAsync(stagingZip, newPackageBytes);
// 3. 运行 UpdateAgent,启用 --corrupt-backup-test 故意篡改备份文件
UpdateAgent.Program.HttpClientFactory = null;
var args = new[]
{
"--run-id", "run_u11",
"--target-dir", targetDir,
"--staging-zip", stagingZip,
"--backup-dir", backupDir,
"--target-version", "2.6.0",
"--corrupt-backup-test",
"--journal-path", journalPath
};
int exitCode = await UpdateAgent.Program.Main(args);
// 4. 门禁断言:由于备份被篡改,更新代理必须触发 fail-closed 退出
if (exitCode != 11)
{
throw new Exception($"Expected exit code 11 for backup verification failure, got {exitCode}");
}
// 5. 核心验收断言:目标目录 100% 字节未被修改!新版本未被释放!
var curSvcContent = await File.ReadAllTextAsync(Path.Combine(targetDir, "ClassNotify.Service.dll"));
var curCliContent = await File.ReadAllTextAsync(Path.Combine(targetDir, "ClassNotify.Client.dll"));
var curCfgContent = await File.ReadAllTextAsync(Path.Combine(targetDir, "config.json"));
if (curSvcContent != originalSvcContent || curCliContent != originalCliContent || curCfgContent != originalCfgContent)
{
throw new Exception("Fatal: Target directory was modified despite backup verification failure!");
}
var curSvcSha = ComputeSha256Hex(Path.Combine(targetDir, "ClassNotify.Service.dll"));
if (curSvcSha != originalSvcSha)
{
throw new Exception("SHA256 of target directory files changed during failed backup verification!");
}
// 6. 验证 Journal 记录了 BackupVerificationFailed
if (File.Exists(journalPath))
{
var journalJson = await File.ReadAllTextAsync(journalPath);
var journal = JsonSerializer.Deserialize(journalJson, JsonOpts);
if (journal?.ErrorCode != UpdateErrorCodes.BackupVerificationFailed)
{
throw new Exception($"Expected ErrorCode 'BackupVerificationFailed', got '{journal?.ErrorCode}'");
}
}
}
finally
{
if (Directory.Exists(tempRoot)) Directory.Delete(tempRoot, true);
}
}
///
/// U12: Exact Restore
/// 坏版本注入多余垃圾文件 (如 BadOnly.dll, extra_sub/ExtraBad.dat)。
/// 回滚后这些多余文件必须被彻底物理删除,目标目录必须与 backup-manifest.json 保持 100% 一致!
///
private static async Task Test_U12_ExactRestore()
{
var tempRoot = Path.Combine(Path.GetTempPath(), "cn_u12_" + Guid.NewGuid().ToString("N"));
var targetDir = Path.Combine(tempRoot, "target");
var backupDir = Path.Combine(tempRoot, "backup");
var stagingZip = Path.Combine(tempRoot, "staging.zip");
var journalPath = Path.Combine(tempRoot, "update-journal.json");
try
{
Directory.CreateDirectory(targetDir);
// 1. 初始化原始安装目录 (v2.5.0, 仅2个文件)
await File.WriteAllTextAsync(Path.Combine(targetDir, "ClassNotify.Service.dll"), "v2.5.0_service_dll");
await File.WriteAllTextAsync(Path.Combine(targetDir, "ClassNotify.Client.dll"), "v2.5.0_client_dll");
// 2. 构建坏版本安装包:除了包含新版 dll 外,故意包含 BadOnly.dll 与 sub/ExtraBad.dat
var payload = new Dictionary
{
["ClassNotify.Service.dll"] = Encoding.UTF8.GetBytes("bad_v2.6.0_service"),
["ClassNotify.Client.dll"] = Encoding.UTF8.GetBytes("bad_v2.6.0_client"),
["BadOnly.dll"] = Encoding.UTF8.GetBytes("unmanifested_bad_file_that_must_be_deleted"),
["sub/ExtraBad.dat"] = Encoding.UTF8.GetBytes("unmanifested_sub_file")
};
var zipBytes = CreatePackageZip("2.6.0", "win-x64", payload);
await File.WriteAllBytesAsync(stagingZip, zipBytes);
// 3. 运行 UpdateAgent,模拟启动失败触发回滚
UpdateAgent.Program.HttpClientFactory = null;
var args = new[]
{
"--run-id", "run_u12",
"--target-dir", targetDir,
"--staging-zip", stagingZip,
"--backup-dir", backupDir,
"--target-version", "2.6.0",
"--simulate-service-start-failure",
"--journal-path", journalPath
};
int exitCode = await UpdateAgent.Program.Main(args);
if (exitCode != 0)
{
throw new Exception($"Expected exit code 0 for successful rollback, got {exitCode}");
}
// 4. 核心断言:BadOnly.dll 与 sub/ExtraBad.dat 必须被彻底物理清除!
var badOnlyPath = Path.Combine(targetDir, "BadOnly.dll");
if (File.Exists(badOnlyPath))
{
throw new Exception("Exact Restore Failed: BadOnly.dll was NOT deleted during rollback!");
}
var extraSubPath = Path.Combine(targetDir, "sub", "ExtraBad.dat");
if (File.Exists(extraSubPath))
{
throw new Exception("Exact Restore Failed: sub/ExtraBad.dat was NOT deleted during rollback!");
}
// 5. 核心断言:目标目录所有文件与 backup-manifest.json 100% 完全匹配!
var manifestPath = Path.Combine(backupDir, "backup-manifest.json");
var manifest = JsonSerializer.Deserialize(await File.ReadAllTextAsync(manifestPath), JsonOpts);
if (manifest == null || manifest.Files.Count != 2)
{
throw new Exception("Expected 2 files in backup-manifest.json");
}
foreach (var entry in manifest.Files)
{
var filePath = Path.Combine(targetDir, entry.RelativePath.Replace('/', Path.DirectorySeparatorChar));
if (!File.Exists(filePath))
{
throw new Exception($"Expected restored file {entry.RelativePath} not found in target dir!");
}
var actualSha = ComputeSha256Hex(filePath);
if (!string.Equals(actualSha, entry.Sha256, StringComparison.OrdinalIgnoreCase))
{
throw new Exception($"Restored file {entry.RelativePath} SHA mismatch!");
}
}
// 6. 验证原始旧版内容完全恢复
var restoredSvc = await File.ReadAllTextAsync(Path.Combine(targetDir, "ClassNotify.Service.dll"));
if (restoredSvc != "v2.5.0_service_dll")
{
throw new Exception($"Service file content was not restored to v2.5.0: {restoredSvc}");
}
}
finally
{
if (Directory.Exists(tempRoot)) Directory.Delete(tempRoot, true);
}
}
///
/// U13: Service Start Failure Rollback (Scenario A)
/// 坏版本 Service 无法启动。UpdateAgent 检测到启动失败,自动执行 Rollback,
/// 旧版恢复运行,并如实向服务端上报 RolledBack (ErrorCode: ServiceStartFailed)。
///
private static async Task Test_U13_ServiceStartFailureRollback()
{
using var factory = new CustomWebApplicationFactory();
var client = factory.CreateClient();
UpdateAgent.Program.HttpClientFactory = () => factory.CreateClient();
var tempRoot = Path.Combine(Path.GetTempPath(), "cn_u13_" + Guid.NewGuid().ToString("N"));
var targetDir = Path.Combine(tempRoot, "target");
var backupDir = Path.Combine(tempRoot, "backup");
var stagingZip = Path.Combine(tempRoot, "staging.zip");
var journalPath = Path.Combine(tempRoot, "update-journal.json");
try
{
Directory.CreateDirectory(targetDir);
await File.WriteAllTextAsync(Path.Combine(targetDir, "ClassNotify.Service.dll"), "stable_service_2_5_0");
var badPackage = CreatePackageZip("2.6.0", "win-x64", new Dictionary
{
["ClassNotify.Service.dll"] = Encoding.UTF8.GetBytes("broken_unstartable_binary")
});
await File.WriteAllBytesAsync(stagingZip, badPackage);
// 种子化设备与任务
using (var scope = factory.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService();
await SeedTenantAndDeviceAsync(db, "t_u13", "School U13", "dev_u13", "token_u13", "2.5.0");
db.DeviceUpdateRuns.Add(new DeviceUpdateRun
{
Id = "run_u13",
DeviceId = "dev_u13",
TenantId = "t_u13",
ReleaseId = "rel_u13",
FromVersion = "2.5.0",
TargetVersion = "2.6.0",
Status = UpdateStatuses.Installing,
CreatedAtUtc = DateTimeOffset.UtcNow,
DeadlineAtUtc = DateTimeOffset.UtcNow.AddMinutes(15)
});
await db.SaveChangesAsync();
}
// 运行 UpdateAgent,模拟 Service 启动崩溃 (Scenario A)
var args = new[]
{
"--run-id", "run_u13",
"--target-dir", targetDir,
"--staging-zip", stagingZip,
"--backup-dir", backupDir,
"--server-url", "http://localhost",
"--device-id", "dev_u13",
"--device-token", "token_u13",
"--target-version", "2.6.0",
"--simulate-service-start-failure",
"--journal-path", journalPath
};
int exitCode = await UpdateAgent.Program.Main(args);
if (exitCode != 0) throw new Exception($"Expected exit code 0 for safe rollback, got {exitCode}");
// 验证服务端 DB 状态变更为 RolledBack,且 ErrorCode 为 ServiceStartFailed
using (var scope = factory.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService();
var run = await db.DeviceUpdateRuns.AsNoTracking().FirstOrDefaultAsync(r => r.Id == "run_u13");
if (run == null) throw new Exception("Update run run_u13 not found in DB");
if (run.Status != UpdateStatuses.RolledBack)
{
throw new Exception($"Expected DB status 'RolledBack', got '{run.Status}'");
}
if (run.ErrorCode != UpdateErrorCodes.ServiceStartFailed)
{
throw new Exception($"Expected DB ErrorCode 'ServiceStartFailed', got '{run.ErrorCode}'");
}
// 设备版本仍维持 2.5.0,绝不因坏版污染
var dev = await db.Devices.AsNoTracking().FirstOrDefaultAsync(d => d.Id == "dev_u13");
if (dev?.ServiceVersion != "2.5.0")
{
throw new Exception($"Device version corrupted! Expected 2.5.0, got '{dev?.ServiceVersion}'");
}
}
// 验证磁盘二进制已还原为 2.5.0
var restored = await File.ReadAllTextAsync(Path.Combine(targetDir, "ClassNotify.Service.dll"));
if (restored != "stable_service_2_5_0")
{
throw new Exception($"Service binary not restored! Got '{restored}'");
}
}
finally
{
UpdateAgent.Program.HttpClientFactory = null;
if (Directory.Exists(tempRoot)) Directory.Delete(tempRoot, true);
}
}
///
/// U14: Post-update Critical Health Failure (Scenario B - Silent Bad Version)
/// Service 能够启动,但 IPC 或 WPF 崩溃损坏。健康检测检出失败,自动触发 Rollback 并恢复旧版。
/// 服务端如实记录 RolledBack (ErrorCode: PostUpdateIpcFailed)。
///
private static async Task Test_U14_PostUpdateHealthFailureRollback()
{
using var factory = new CustomWebApplicationFactory();
UpdateAgent.Program.HttpClientFactory = () => factory.CreateClient();
var tempRoot = Path.Combine(Path.GetTempPath(), "cn_u14_" + Guid.NewGuid().ToString("N"));
var targetDir = Path.Combine(tempRoot, "target");
var backupDir = Path.Combine(tempRoot, "backup");
var stagingZip = Path.Combine(tempRoot, "staging.zip");
var journalPath = Path.Combine(tempRoot, "update-journal.json");
try
{
Directory.CreateDirectory(targetDir);
await File.WriteAllTextAsync(Path.Combine(targetDir, "ClassNotify.Service.dll"), "healthy_v2_5_0");
var package = CreatePackageZip("2.6.0", "win-x64");
await File.WriteAllBytesAsync(stagingZip, package);
using (var scope = factory.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService();
await SeedTenantAndDeviceAsync(db, "t_u14", "School U14", "dev_u14", "token_u14", "2.5.0");
db.DeviceUpdateRuns.Add(new DeviceUpdateRun
{
Id = "run_u14",
DeviceId = "dev_u14",
TenantId = "t_u14",
ReleaseId = "rel_u14",
FromVersion = "2.5.0",
TargetVersion = "2.6.0",
Status = UpdateStatuses.Installing,
CreatedAtUtc = DateTimeOffset.UtcNow,
DeadlineAtUtc = DateTimeOffset.UtcNow.AddMinutes(15)
});
await db.SaveChangesAsync();
}
// 运行 UpdateAgent,模拟静默坏版本 (IPC/WPF dead)
var args = new[]
{
"--run-id", "run_u14",
"--target-dir", targetDir,
"--staging-zip", stagingZip,
"--backup-dir", backupDir,
"--server-url", "http://localhost",
"--device-id", "dev_u14",
"--device-token", "token_u14",
"--target-version", "2.6.0",
"--simulate-silent-bad",
"--journal-path", journalPath
};
int exitCode = await UpdateAgent.Program.Main(args);
if (exitCode != 0) throw new Exception($"Expected exit code 0 for safe rollback, got {exitCode}");
using (var scope = factory.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService();
var run = await db.DeviceUpdateRuns.AsNoTracking().FirstOrDefaultAsync(r => r.Id == "run_u14");
if (run?.Status != UpdateStatuses.RolledBack)
{
throw new Exception($"Expected status 'RolledBack', got '{run?.Status}'");
}
if (run?.ErrorCode != UpdateErrorCodes.PostUpdateIpcFailed)
{
throw new Exception($"Expected ErrorCode 'PostUpdateIpcFailed', got '{run?.ErrorCode}'");
}
}
var restored = await File.ReadAllTextAsync(Path.Combine(targetDir, "ClassNotify.Service.dll"));
if (restored != "healthy_v2_5_0")
{
throw new Exception($"Expected healthy_v2_5_0, got {restored}");
}
}
finally
{
UpdateAgent.Program.HttpClientFactory = null;
if (Directory.Exists(tempRoot)) Directory.Delete(tempRoot, true);
}
}
///
/// U15: Crash Loop Rollback
/// 新版本 Service 启动后持续崩溃(60s 内崩溃 >= 3 次)。
/// 触发 CrashLoop 保护机制并自动回滚。服务端如实记录 RolledBack (ErrorCode: ServiceCrashLoop)。
///
private static async Task Test_U15_CrashLoopRollback()
{
using var factory = new CustomWebApplicationFactory();
UpdateAgent.Program.HttpClientFactory = () => factory.CreateClient();
var tempRoot = Path.Combine(Path.GetTempPath(), "cn_u15_" + Guid.NewGuid().ToString("N"));
var targetDir = Path.Combine(tempRoot, "target");
var backupDir = Path.Combine(tempRoot, "backup");
var stagingZip = Path.Combine(tempRoot, "staging.zip");
var journalPath = Path.Combine(tempRoot, "update-journal.json");
try
{
Directory.CreateDirectory(targetDir);
await File.WriteAllTextAsync(Path.Combine(targetDir, "ClassNotify.Service.dll"), "stable_service_v2_5_0");
var package = CreatePackageZip("2.6.0", "win-x64");
await File.WriteAllBytesAsync(stagingZip, package);
using (var scope = factory.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService();
await SeedTenantAndDeviceAsync(db, "t_u15", "School U15", "dev_u15", "token_u15", "2.5.0");
db.DeviceUpdateRuns.Add(new DeviceUpdateRun
{
Id = "run_u15",
DeviceId = "dev_u15",
TenantId = "t_u15",
ReleaseId = "rel_u15",
FromVersion = "2.5.0",
TargetVersion = "2.6.0",
Status = UpdateStatuses.Installing,
CreatedAtUtc = DateTimeOffset.UtcNow,
DeadlineAtUtc = DateTimeOffset.UtcNow.AddMinutes(15)
});
await db.SaveChangesAsync();
}
// 运行 UpdateAgent,模拟 Crash Loop
var args = new[]
{
"--run-id", "run_u15",
"--target-dir", targetDir,
"--staging-zip", stagingZip,
"--backup-dir", backupDir,
"--server-url", "http://localhost",
"--device-id", "dev_u15",
"--device-token", "token_u15",
"--target-version", "2.6.0",
"--simulate-crash-loop",
"--journal-path", journalPath
};
int exitCode = await UpdateAgent.Program.Main(args);
if (exitCode != 0) throw new Exception($"Expected exit code 0 for safe rollback, got {exitCode}");
using (var scope = factory.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService();
var run = await db.DeviceUpdateRuns.AsNoTracking().FirstOrDefaultAsync(r => r.Id == "run_u15");
if (run?.Status != UpdateStatuses.RolledBack)
{
throw new Exception($"Expected status 'RolledBack', got '{run?.Status}'");
}
if (run?.ErrorCode != UpdateErrorCodes.ServiceCrashLoop)
{
throw new Exception($"Expected ErrorCode 'ServiceCrashLoop', got '{run?.ErrorCode}'");
}
}
var restored = await File.ReadAllTextAsync(Path.Combine(targetDir, "ClassNotify.Service.dll"));
if (restored != "stable_service_v2_5_0")
{
throw new Exception($"Expected stable_service_v2_5_0, got {restored}");
}
}
finally
{
UpdateAgent.Program.HttpClientFactory = null;
if (Directory.Exists(tempRoot)) Directory.Delete(tempRoot, true);
}
}
///
/// U16: Identity Preservation Through Rollback
/// 经历坏版安装与回滚全流程后,%ProgramData%\ClassNotify\ 内凭据(DeviceId, Token, TenantId, ClassroomId)
/// 完全保持不变,未受到任何污染或覆盖。
///
private static async Task Test_U16_IdentityPreservation()
{
var tempRoot = Path.Combine(Path.GetTempPath(), "cn_u16_" + Guid.NewGuid().ToString("N"));
var targetDir = Path.Combine(tempRoot, "target");
var backupDir = Path.Combine(tempRoot, "backup");
var stagingZip = Path.Combine(tempRoot, "staging.zip");
var journalPath = Path.Combine(tempRoot, "update-journal.json");
// 模拟 ProgramData 凭据存储
var credsDir = Path.Combine(tempRoot, "ProgramData", "ClassNotify");
Directory.CreateDirectory(credsDir);
var credsPath = Path.Combine(credsDir, "device_credentials.json");
var originalCredsJson = JsonSerializer.Serialize(new
{
DeviceId = "dev_u16_sacred_identity",
TenantId = "tenant_u16_sacred",
ClassroomId = "c_u16_sacred",
ClassDisplayName = "Sacred Classroom",
DeviceToken = "super_high_entropy_secret_token_never_leak_xyz"
}, JsonOpts);
await File.WriteAllTextAsync(credsPath, originalCredsJson);
try
{
Directory.CreateDirectory(targetDir);
await File.WriteAllTextAsync(Path.Combine(targetDir, "ClassNotify.Service.dll"), "v2.5.0");
var badPackage = CreatePackageZip("2.6.0", "win-x64", new Dictionary
{
["ClassNotify.Service.dll"] = Encoding.UTF8.GetBytes("bad_2.6.0")
});
await File.WriteAllBytesAsync(stagingZip, badPackage);
// 执行升级并模拟失败回滚
UpdateAgent.Program.HttpClientFactory = null;
var args = new[]
{
"--run-id", "run_u16",
"--target-dir", targetDir,
"--staging-zip", stagingZip,
"--backup-dir", backupDir,
"--target-version", "2.6.0",
"--simulate-service-start-failure",
"--journal-path", journalPath
};
int exitCode = await UpdateAgent.Program.Main(args);
if (exitCode != 0) throw new Exception($"Expected exit code 0, got {exitCode}");
// 核心断言:凭据文件完全未被触碰,字节级保持 100% 一致!
var postCredsJson = await File.ReadAllTextAsync(credsPath);
if (postCredsJson != originalCredsJson)
{
throw new Exception("Identity Preservation Violated: device_credentials.json was modified during rollback!");
}
using var doc = JsonDocument.Parse(postCredsJson);
var root = doc.RootElement;
if (root.GetProperty("DeviceId").GetString() != "dev_u16_sacred_identity")
throw new Exception("DeviceId mismatch!");
if (root.GetProperty("TenantId").GetString() != "tenant_u16_sacred")
throw new Exception("TenantId mismatch!");
if (root.GetProperty("ClassroomId").GetString() != "c_u16_sacred")
throw new Exception("ClassroomId mismatch!");
if (root.GetProperty("DeviceToken").GetString() != "super_high_entropy_secret_token_never_leak_xyz")
throw new Exception("DeviceToken mismatch!");
}
finally
{
if (Directory.Exists(tempRoot)) Directory.Delete(tempRoot, true);
}
}
///
/// U17: Rollback Health Verification
/// 回滚成功后,必须真实完成一次旧版心跳上报以及广播通知 + 客户端 ACK 回执闭环,
/// 证明终端完全恢复生产可用。
///
private static async Task Test_U17_RollbackHealthVerification()
{
using var factory = new CustomWebApplicationFactory();
var client = factory.CreateClient();
UpdateAgent.Program.HttpClientFactory = () => factory.CreateClient();
var tempRoot = Path.Combine(Path.GetTempPath(), "cn_u17_" + Guid.NewGuid().ToString("N"));
var targetDir = Path.Combine(tempRoot, "target");
var backupDir = Path.Combine(tempRoot, "backup");
var stagingZip = Path.Combine(tempRoot, "staging.zip");
var journalPath = Path.Combine(tempRoot, "update-journal.json");
try
{
Directory.CreateDirectory(targetDir);
await File.WriteAllTextAsync(Path.Combine(targetDir, "ClassNotify.Service.dll"), "healthy_v2_5_0");
var package = CreatePackageZip("2.6.0", "win-x64");
await File.WriteAllBytesAsync(stagingZip, package);
using (var scope = factory.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService();
await SeedTenantAndDeviceAsync(db, "t_u17", "School U17", "dev_u17", "token_u17", "2.5.0");
// 增加教师账户
db.Users.Add(new User
{
Id = "usr_teacher_u17",
Username = "teacher_u17",
FullName = "Teacher U17",
Role = "Teacher",
TenantId = "t_u17",
PasswordHash = "hash",
IsLocked = false,
CreatedAt = DateTimeOffset.UtcNow
});
db.DeviceUpdateRuns.Add(new DeviceUpdateRun
{
Id = "run_u17",
DeviceId = "dev_u17",
TenantId = "t_u17",
ReleaseId = "rel_u17",
FromVersion = "2.5.0",
TargetVersion = "2.6.0",
Status = UpdateStatuses.Installing,
CreatedAtUtc = DateTimeOffset.UtcNow,
DeadlineAtUtc = DateTimeOffset.UtcNow.AddMinutes(15)
});
await db.SaveChangesAsync();
}
// 执行回滚
var args = new[]
{
"--run-id", "run_u17",
"--target-dir", targetDir,
"--staging-zip", stagingZip,
"--backup-dir", backupDir,
"--server-url", "http://localhost",
"--device-id", "dev_u17",
"--device-token", "token_u17",
"--target-version", "2.6.0",
"--simulate-silent-bad",
"--journal-path", journalPath
};
int exitCode = await UpdateAgent.Program.Main(args);
if (exitCode != 0) throw new Exception($"Expected exit code 0, got {exitCode}");
// 1. 验证旧版心跳恢复并处于在线状态
using (var scope = factory.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService();
var dev = await db.Devices.FirstAsync(d => d.Id == "dev_u17");
dev.LastHeartbeatAt = DateTimeOffset.UtcNow;
dev.ServiceVersion = "2.5.0";
dev.ClientVersion = "2.5.0";
await db.SaveChangesAsync();
var presence = scope.ServiceProvider.GetRequiredService();
if (!presence.IsOnline(dev.Id, dev.LastHeartbeatAt))
{
throw new Exception("Device not recognized as online after rollback heartbeat!");
}
}
// 2. 真实下发一条通知
var teacherToken = factory.GenerateToken("usr_teacher_u17", "teacher_u17", "Teacher", "t_u17");
var notifReq = new HttpRequestMessage(HttpMethod.Post, "/api/notification/send")
{
Content = JsonContent.Create(new NotificationDto
{
TenantId = "t_u17",
ClassId = "c_t_u17",
Message = "Rollback health check notification",
Type = NotificationType.Call,
Voice = true,
Shake = true,
DurationSeconds = 15
})
};
notifReq.Headers.Authorization = new AuthenticationHeaderValue("Bearer", teacherToken);
var notifResp = await client.SendAsync(notifReq);
if (notifResp.StatusCode != HttpStatusCode.OK)
{
throw new Exception($"Post-rollback notification send failed: {notifResp.StatusCode}");
}
var notifJson = await notifResp.Content.ReadAsStringAsync();
using var notifDoc = JsonDocument.Parse(notifJson);
string notifId = notifDoc.RootElement.GetProperty("messageId").GetString()!;
// 3. 模拟设备回传客户端 ACK
using (var scope = factory.Services.CreateScope())
{
var securityService = scope.ServiceProvider.GetRequiredService();
await securityService.RecordAckAsync(notifId, "dev_u17", new AckReceiptDto
{
MessageId = notifId,
ClassId = "c_t_u17",
Status = AckStatus.Acknowledged,
Timestamp = DateTimeOffset.UtcNow
});
var db = scope.ServiceProvider.GetRequiredService();
var receipt = await db.NotificationReceipts.FirstOrDefaultAsync(r => r.NotificationId == notifId && r.DeviceId == "dev_u17");
if (receipt == null || receipt.Status != "Acknowledged")
{
throw new Exception($"NotificationReceipt not recorded as Acknowledged! Status: {receipt?.Status}");
}
}
}
finally
{
UpdateAgent.Program.HttpClientFactory = null;
if (Directory.Exists(tempRoot)) Directory.Delete(tempRoot, true);
}
}
///
/// U18: RollbackFailed Truthfulness
/// 当故意破坏备份目录导致客观上不可恢复时,严禁假冒成功,
/// 必须如实上报 RollbackFailed,并立即 fail-stop 停止无限重试。
///
private static async Task Test_U18_RollbackFailedTruthfulness()
{
using var factory = new CustomWebApplicationFactory();
UpdateAgent.Program.HttpClientFactory = () => factory.CreateClient();
var tempRoot = Path.Combine(Path.GetTempPath(), "cn_u18_" + Guid.NewGuid().ToString("N"));
var targetDir = Path.Combine(tempRoot, "target");
var backupDir = Path.Combine(tempRoot, "backup");
var stagingZip = Path.Combine(tempRoot, "staging.zip");
var journalPath = Path.Combine(tempRoot, "update-journal.json");
try
{
Directory.CreateDirectory(targetDir);
await File.WriteAllTextAsync(Path.Combine(targetDir, "ClassNotify.Service.dll"), "healthy_v2_5_0");
var package = CreatePackageZip("2.6.0", "win-x64");
await File.WriteAllBytesAsync(stagingZip, package);
using (var scope = factory.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService();
await SeedTenantAndDeviceAsync(db, "t_u18", "School U18", "dev_u18", "token_u18", "2.5.0");
db.DeviceUpdateRuns.Add(new DeviceUpdateRun
{
Id = "run_u18",
DeviceId = "dev_u18",
TenantId = "t_u18",
ReleaseId = "rel_u18",
FromVersion = "2.5.0",
TargetVersion = "2.6.0",
Status = UpdateStatuses.Installing,
CreatedAtUtc = DateTimeOffset.UtcNow,
DeadlineAtUtc = DateTimeOffset.UtcNow.AddMinutes(15)
});
await db.SaveChangesAsync();
}
// 运行 UpdateAgent,模拟启动失败且备份清单损坏
var args = new[]
{
"--run-id", "run_u18",
"--target-dir", targetDir,
"--staging-zip", stagingZip,
"--backup-dir", backupDir,
"--server-url", "http://localhost",
"--device-id", "dev_u18",
"--device-token", "token_u18",
"--target-version", "2.6.0",
"--simulate-service-start-failure",
"--corrupt-backup-for-rollback-test",
"--journal-path", journalPath
};
int exitCode = await UpdateAgent.Program.Main(args);
// 门禁断言:不可恢复时必须以非 0 退出 (fail-stop)
if (exitCode == 0)
{
throw new Exception("Expected non-zero exit code on rollback failure, but got 0 (faked success)!");
}
// 核心断言:如实上报 RollbackFailed,严禁假冒 RolledBack 或 Succeeded
using (var scope = factory.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService();
var run = await db.DeviceUpdateRuns.AsNoTracking().FirstOrDefaultAsync(r => r.Id == "run_u18");
if (run?.Status != UpdateStatuses.RollbackFailed)
{
throw new Exception($"Expected status 'RollbackFailed', got '{run?.Status}'");
}
if (run?.ErrorCode != UpdateErrorCodes.RollbackVerificationFailed)
{
throw new Exception($"Expected ErrorCode 'RollbackVerificationFailed', got '{run?.ErrorCode}'");
}
}
}
finally
{
UpdateAgent.Program.HttpClientFactory = null;
if (Directory.Exists(tempRoot)) Directory.Delete(tempRoot, true);
}
}
///
/// U19: Agent / Power Interruption Recovery
/// 在文件替换中途模拟杀死 UpdateAgent 或掉电重启。
/// 自启恢复探针读取 durable journal,能自动回退到旧版,绝不遗留半套损坏二进制。
///
private static async Task Test_U19_InterruptionRecovery()
{
using var factory = new CustomWebApplicationFactory();
UpdateAgent.Program.HttpClientFactory = () => factory.CreateClient();
var tempRoot = Path.Combine(Path.GetTempPath(), "cn_u19_" + Guid.NewGuid().ToString("N"));
var targetDir = Path.Combine(tempRoot, "target");
var backupDir = Path.Combine(tempRoot, "backup");
var stagingZip = Path.Combine(tempRoot, "staging.zip");
var journalPath = Path.Combine(tempRoot, "update-journal.json");
try
{
Directory.CreateDirectory(targetDir);
Directory.CreateDirectory(backupDir);
// 1. 准备真实的有效备份目录及 backup-manifest.json
var stableSvcBytes = Encoding.UTF8.GetBytes("stable_service_2_5_0");
var stableCliBytes = Encoding.UTF8.GetBytes("stable_client_2_5_0");
await File.WriteAllBytesAsync(Path.Combine(backupDir, "ClassNotify.Service.dll"), stableSvcBytes);
await File.WriteAllBytesAsync(Path.Combine(backupDir, "ClassNotify.Client.dll"), stableCliBytes);
var backupManifest = new BackupManifestV1
{
ManifestVersion = 1,
RunId = "run_u19",
TargetVersion = "2.6.0",
CreatedAtUtc = DateTimeOffset.UtcNow,
Files = new List
{
new() { RelativePath = "ClassNotify.Service.dll", SizeBytes = stableSvcBytes.Length, Sha256 = ComputeSha256Hex(Path.Combine(backupDir, "ClassNotify.Service.dll")) },
new() { RelativePath = "ClassNotify.Client.dll", SizeBytes = stableCliBytes.Length, Sha256 = ComputeSha256Hex(Path.Combine(backupDir, "ClassNotify.Client.dll")) }
}
};
await File.WriteAllTextAsync(Path.Combine(backupDir, "backup-manifest.json"), JsonSerializer.Serialize(backupManifest, JsonOpts));
// 2. 模拟掉电/被 kill 现场:targetDir 处于损坏半安装状态 (包含半套坏文件和多余临时文件)
await File.WriteAllTextAsync(Path.Combine(targetDir, "ClassNotify.Service.dll"), "half_extracted_garbage_data");
await File.WriteAllTextAsync(Path.Combine(targetDir, "PartialBadInstall.tmp"), "dangling_temp_file");
// 3. 持久化日志记录当时处于 Installing 阶段 (模拟中途掉电)
var interruptedJournal = new UpdateJournalV1
{
RunId = "run_u19",
Phase = UpdateStatuses.Installing,
TargetVersion = "2.6.0",
TargetDir = targetDir,
BackupDir = backupDir,
StagingZip = stagingZip,
ServiceName = "ClassNotifyService",
UpdatedAtUtc = DateTimeOffset.UtcNow
};
await File.WriteAllTextAsync(journalPath, JsonSerializer.Serialize(interruptedJournal, JsonOpts));
// 4. 服务端预置 Installing 状态
using (var scope = factory.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService();
await SeedTenantAndDeviceAsync(db, "t_u19", "School U19", "dev_u19", "token_u19", "2.5.0");
db.DeviceUpdateRuns.Add(new DeviceUpdateRun
{
Id = "run_u19",
DeviceId = "dev_u19",
TenantId = "t_u19",
ReleaseId = "rel_u19",
FromVersion = "2.5.0",
TargetVersion = "2.6.0",
Status = UpdateStatuses.Installing,
CreatedAtUtc = DateTimeOffset.UtcNow,
DeadlineAtUtc = DateTimeOffset.UtcNow.AddMinutes(15)
});
await db.SaveChangesAsync();
}
// 5. 模拟进入 Installing 之前已注册独立 Boot Recovery 任务
var mockAgentExe = Path.Combine(tempRoot, "ClassNotify.UpdateAgent.exe");
UpdateAgent.BootRecoveryManager.RegisterRecoveryTask(mockAgentExe, "run_u19", journalPath);
if (!UpdateAgent.BootRecoveryManager.IsRecoveryTaskRegistered())
{
throw new Exception("BootRecoveryManager failed to register recovery task before mutation!");
}
// 6. 模拟机器断电重启,ClassNotify.Service 彻底无法自启
// 由 Windows 启动入口独立拉起 UpdateAgent --recover (完全不依赖主 Service)
var recoverArgs = new[]
{
"--recover",
"--journal-path", journalPath,
"--server-url", "http://localhost",
"--device-id", "dev_u19",
"--device-token", "token_u19"
};
int exitCode = await UpdateAgent.Program.Main(recoverArgs);
if (exitCode != 0)
{
throw new Exception($"Expected exit code 0 on auto-recovery rollback, got {exitCode}");
}
// 7. 核心断言:
// A. 多余垃圾文件 PartialBadInstall.tmp 被物理删除
if (File.Exists(Path.Combine(targetDir, "PartialBadInstall.tmp")))
{
throw new Exception("PartialBadInstall.tmp was NOT deleted during interrupted recovery!");
}
// B. 目标文件完全恢复为旧版
var restoredSvc = await File.ReadAllTextAsync(Path.Combine(targetDir, "ClassNotify.Service.dll"));
if (restoredSvc != "stable_service_2_5_0")
{
throw new Exception($"Service not restored! Got '{restoredSvc}'");
}
// C. 服务端记录变更为 RolledBack,且 ErrorCode 标记为 RecoveryInterrupted
using (var scope = factory.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService();
var run = await db.DeviceUpdateRuns.AsNoTracking().FirstOrDefaultAsync(r => r.Id == "run_u19");
if (run?.Status != UpdateStatuses.RolledBack)
{
throw new Exception($"Expected status 'RolledBack', got '{run?.Status}'");
}
if (run?.ErrorCode != UpdateErrorCodes.RecoveryInterrupted)
{
throw new Exception($"Expected ErrorCode 'RecoveryInterrupted', got '{run?.ErrorCode}'");
}
}
// D. 逃生完成后,自启恢复任务被自动清理注销,避免无限反复回滚
if (UpdateAgent.BootRecoveryManager.IsRecoveryTaskRegistered())
{
throw new Exception("Recovery task was not unregistered after successful rollback!");
}
}
finally
{
UpdateAgent.BootRecoveryManager.UnregisterRecoveryTask();
UpdateAgent.Program.HttpClientFactory = null;
if (Directory.Exists(tempRoot)) Directory.Delete(tempRoot, true);
}
}
///
/// U20: Network-only Failure Discrimination
/// 若 Service / WPF / IPC 完全健康,仅因外部临时网络波动导致 HTTPS/SignalR 暂不可达:
/// 严禁误判盲目触发回滚 (Rollback == false),
/// 严禁在未确认前错误标成成功 (Succeeded == false, 保持 HealthChecking/ConnectivityUnconfirmed),
/// 只有在网络恢复并成功上报后,才最终晋级为 Succeeded。
///
private static async Task Test_U20_NetworkFailureDiscrimination()
{
using var factory = new CustomWebApplicationFactory();
var tempRoot = Path.Combine(Path.GetTempPath(), "cn_u20_" + Guid.NewGuid().ToString("N"));
var targetDir = Path.Combine(tempRoot, "target");
var backupDir = Path.Combine(tempRoot, "backup");
var stagingZip = Path.Combine(tempRoot, "staging.zip");
var journalPath = Path.Combine(tempRoot, "update-journal.json");
try
{
Directory.CreateDirectory(targetDir);
await File.WriteAllTextAsync(Path.Combine(targetDir, "ClassNotify.Service.dll"), "old_v2_5_0");
var newPackage = CreatePackageZip("2.6.0", "win-x64", new Dictionary
{
["ClassNotify.Service.dll"] = Encoding.UTF8.GetBytes("new_healthy_v2_6_0")
});
await File.WriteAllBytesAsync(stagingZip, newPackage);
// 预置云端更新任务
using (var scope = factory.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService();
await SeedTenantAndDeviceAsync(db, "t_u20", "School U20", "dev_u20", "token_u20", "2.5.0");
db.DeviceUpdateRuns.Add(new DeviceUpdateRun
{
Id = "run_u20",
DeviceId = "dev_u20",
TenantId = "t_u20",
ReleaseId = "rel_u20",
FromVersion = "2.5.0",
TargetVersion = "2.6.0",
Status = UpdateStatuses.Installing,
CreatedAtUtc = DateTimeOffset.UtcNow,
DeadlineAtUtc = DateTimeOffset.UtcNow.AddMinutes(15)
});
await db.SaveChangesAsync();
}
// =========================================================================
// 阶段 1:网络完全断开环境 (指向积极拒绝连接的离线端口)
// =========================================================================
UpdateAgent.Program.HttpClientFactory = null;
var args = new[]
{
"--run-id", "run_u20",
"--target-dir", targetDir,
"--staging-zip", stagingZip,
"--backup-dir", backupDir,
"--server-url", "http://127.0.0.1:59999", // 离线地址
"--device-id", "dev_u20",
"--device-token", "token_u20",
"--target-version", "2.6.0",
"--journal-path", journalPath
};
int exitCode = await UpdateAgent.Program.Main(args);
if (exitCode != 0)
{
throw new Exception($"Expected exit code 0 despite network outage, got {exitCode}");
}
// 断言 1 (Rollback == false):新版本 2.6.0 仍然保持安装,严禁被误判回滚!
var curSvc = await File.ReadAllTextAsync(Path.Combine(targetDir, "ClassNotify.Service.dll"));
if (curSvc != "new_healthy_v2_6_0")
{
throw new Exception($"Target directory was prematurely rolled back despite local health check passing! Content: {curSvc}");
}
// 断言 2 (Succeeded == false):在云端未确认前,严禁误报 Succeeded!必须维持 HealthChecking 待确认状态
var journalJson = await File.ReadAllTextAsync(journalPath);
var journal = JsonSerializer.Deserialize(journalJson, JsonOpts);
if (journal?.Phase == UpdateStatuses.Succeeded)
{
throw new Exception("Fatal: Journal falsely marked update as Succeeded while cloud report was offline!");
}
if (journal?.Phase != UpdateStatuses.HealthChecking || journal?.ErrorMessage != "ConnectivityUnconfirmed")
{
throw new Exception($"Expected journal Phase 'HealthChecking' with ErrorMessage 'ConnectivityUnconfirmed', got '{journal?.Phase}' / '{journal?.ErrorMessage}'");
}
// =========================================================================
// 阶段 2:网络恢复正常后,重新上报并最终完成 Succeeded
// =========================================================================
UpdateAgent.Program.HttpClientFactory = () => factory.CreateClient();
bool confirmed = await UpdateAgent.Program.ConfirmUpdateSuccessAsync(
journalPath,
"http://localhost",
"dev_u20",
"token_u20");
if (!confirmed)
{
throw new Exception("Expected confirmation to succeed once network is restored.");
}
// 断言 3:网络恢复后,服务端状态最终晋级为 Succeeded
using (var scope = factory.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService();
var run = await db.DeviceUpdateRuns.AsNoTracking().FirstOrDefaultAsync(r => r.Id == "run_u20");
if (run?.Status != UpdateStatuses.Succeeded)
{
throw new Exception($"Expected DB status 'Succeeded' after network restore, got '{run?.Status}'");
}
}
// 断言 4:本地 Journal 最终转为 Succeeded
var postConfirmJournalJson = await File.ReadAllTextAsync(journalPath);
var postConfirmJournal = JsonSerializer.Deserialize(postConfirmJournalJson, JsonOpts);
if (postConfirmJournal?.Phase != UpdateStatuses.Succeeded)
{
throw new Exception($"Expected post-confirmation journal Phase 'Succeeded', got '{postConfirmJournal?.Phase}'");
}
}
finally
{
UpdateAgent.BootRecoveryManager.UnregisterRecoveryTask();
UpdateAgent.Program.HttpClientFactory = null;
if (Directory.Exists(tempRoot)) Directory.Delete(tempRoot, true);
}
}
#endregion
}