using System; using System.Collections.Generic; using System.IdentityModel.Tokens.Jwt; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Net.Http.Json; using System.Security.Claims; 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 Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Tokens; namespace ClassNotify.FleetTests; public class CustomWebApplicationFactory : WebApplicationFactory { public const string TestJwtKey = "THIS_IS_A_VERY_LONG_TEST_SIGNING_KEY_EXCEEDING_SIXTY_FOUR_BYTES_IN_LENGTH_FOR_FLEET_TESTS!"; public const string TestIssuer = "ClassNotify.Server"; public const string TestAudience = "ClassNotify.Clients"; private readonly string _testDbName; public CustomWebApplicationFactory(string? dbName = null) { _testDbName = dbName ?? ("classnotify_fleet_test_" + Guid.NewGuid().ToString("N")); } public string DbName => _testDbName; protected override void ConfigureWebHost(IWebHostBuilder builder) { var dir = new DirectoryInfo(AppContext.BaseDirectory); while (dir != null && !File.Exists(Path.Combine(dir.FullName, "ClassNotify.sln"))) { dir = dir.Parent; } var rootDir = dir?.FullName ?? Directory.GetCurrentDirectory(); var serverPath = Path.Combine(rootDir, "src", "Server", "ClassNotify.Server"); builder.UseContentRoot(serverPath); builder.UseEnvironment("Development"); builder.ConfigureAppConfiguration((context, config) => { config.AddInMemoryCollection(new Dictionary { ["RateLimit:AuthPermitLimit"] = "200", ["RateLimit:ActivationPermitLimit"] = "200", ["RateLimit:SendPermitLimit"] = "200", ["Jwt:SigningKey"] = TestJwtKey, ["Jwt:Issuer"] = TestIssuer, ["Jwt:Audience"] = TestAudience, ["Jwt:LifetimeDays"] = "14", ["ConnectionStrings:DefaultConnection"] = "Host=127.0.0.1;Database=classnotify_test;Username=postgres;", ["DeviceVersionPolicy:MinimumServiceVersion"] = "2.4.0", ["DeviceVersionPolicy:RecommendedServiceVersion"] = "2.5.0", ["DeviceVersionPolicy:MinimumClientVersion"] = "2.4.0", ["DeviceVersionPolicy:RecommendedClientVersion"] = "2.5.0", ["DeviceVersionPolicy:MinimumProtocolVersion"] = "2" }); }); builder.ConfigureServices(services => { var descriptors = services.Where(d => d.ServiceType == typeof(DbContextOptions) || d.ServiceType == typeof(ClassNotifyDbContext)).ToList(); foreach (var d in descriptors) { services.Remove(d); } services.AddDbContext(options => { options.UseInMemoryDatabase(_testDbName) .ConfigureWarnings(w => w.Ignore(Microsoft.EntityFrameworkCore.Diagnostics.InMemoryEventId.TransactionIgnoredWarning)); }); }); } public string GenerateToken(string userId, string username, string role, string tenantId, int tokenVersion = 1) { var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(TestJwtKey)); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); var claims = new[] { new Claim(ClaimTypes.NameIdentifier, userId), new Claim(ClaimTypes.Name, username), new Claim(ClaimTypes.Role, role), new Claim("tenant_id", tenantId), new Claim("token_version", tokenVersion.ToString()) }; var token = new JwtSecurityToken( issuer: TestIssuer, audience: TestAudience, claims: claims, expires: DateTime.UtcNow.AddHours(2), signingCredentials: creds ); return new JwtSecurityTokenHandler().WriteToken(token); } } public class Program { private static int _passedTests = 0; private static int _failedTests = 0; public static async Task Main(string[] args) { Console.WriteLine("=========================================================="); Console.WriteLine(" ClassNotify P2.5 RC1-1: Fleet Version Verification Suite"); Console.WriteLine("=========================================================="); await RunTestAsync("FV1: Current Evaluation (2.5.0 / 2.5.0 / Proto 2 -> Current)", Test_FV1_Current); await RunTestAsync("FV2: Outdated Evaluation (2.4.0 / 2.4.0 / Proto 2 -> Outdated)", Test_FV2_Outdated); await RunTestAsync("FV3: Unsupported Evaluation (2.1.0 / 2.5.0 / Proto 2 -> Unsupported)", Test_FV3_Unsupported); await RunTestAsync("FV4: Unknown Evaluation (ServiceVersion = NULL -> Unknown)", Test_FV4_Unknown); await RunTestAsync("FV5: Protocol Check (Proto 1 < Minimum 2 -> Unsupported)", Test_FV5_Protocol); await RunTestAsync("FV6: Fleet Summary Invariant (Current + Outdated + Unsupported + Unknown == Total)", Test_FV6_SummaryInvariant); await RunTestAsync("FV7: RBAC Access Control (Anon 401, Teacher 403, SchoolAdmin 403, SuperAdmin 200)", Test_FV7_RbacSecurity); await RunTestAsync("FV8: Observe-Only Execution Invariant (Unsupported device remains unblocked)", Test_FV8_ObserveOnlyInvariant); await RunTestAsync("FV9: Fleet Device Query Filtering (?status=Unsupported, tenantId, online)", Test_FV9_QueryFiltering); await RunTestAsync("FV10: Zero Mutation Invariant (Read-only endpoints produce 0 audits and 0 mutations)", Test_FV10_ZeroMutation); // Run P2.5 RC1-2 SelfCheck Fleet Operations Gate Tests (SCB1 ~ SCB15) await SelfCheckBatchTests.RunAllTestsAsync(RunTestAsync); // Run P2.5 RC1-3 Phase 1 Support Bundle Gate Tests (P1-1 ~ P1-10) await SupportBundlePhase1Tests.RunAllTestsAsync(RunTestAsync); // Run P2.5 RC1-3 Phase 2 Support Bundle Gate Tests (P2-1 ~ P2-14 + P2-10B) await SupportBundlePhase2Tests.RunAllTestsAsync(RunTestAsync); // Run P2.5 RC1-3 Phase 3 Support Bundle Gate Tests (P3-1 ~ P3-12) await SupportBundlePhase3Tests.RunAllTestsAsync(RunTestAsync); // Run P2.5 RC1-4 Phase 1 Update & Verification Gate Tests (U1 ~ U10) await DeviceUpdatePhase1Tests.RunAllTestsAsync(RunTestAsync); // Run P2.5 RC1-4 Phase 2 Automatic Rollback Gate Tests (U11 ~ U20) await DeviceUpdatePhase2Tests.RunAllTestsAsync(RunTestAsync); // Run P2.5 RC1-4 Phase 3 Small Staged Rollout Gate Tests (U21 ~ U30) await DeviceUpdatePhase3Tests.RunAllTestsAsync(RunTestAsync); Console.WriteLine("\n=========================================================="); Console.WriteLine($" Test Results: {_passedTests} Passed, {_failedTests} Failed"); Console.WriteLine("=========================================================="); return _failedTests == 0 ? 0 : 1; } private static async Task RunTestAsync(string testName, Func testAction) { try { await testAction(); Console.WriteLine($"[PASS] {testName}"); _passedTests++; } catch (Exception ex) { Console.WriteLine($"[FAIL] {testName}: {ex.Message}"); if (ex.InnerException != null) { Console.WriteLine($" Inner: {ex.InnerException.Message}"); } _failedTests++; } } private static IDeviceVersionEvaluator CreateEvaluator() { var options = Options.Create(new DeviceVersionPolicyOptions { MinimumServiceVersion = "2.4.0", RecommendedServiceVersion = "2.5.0", MinimumClientVersion = "2.4.0", RecommendedClientVersion = "2.5.0", MinimumProtocolVersion = 2 }); return new DeviceVersionEvaluator(options); } private static Task Test_FV1_Current() { var evaluator = CreateEvaluator(); var result = evaluator.Evaluate("2.5.0", "2.5.0", 2); if (result.Status != FleetVersionStatus.Current) { throw new Exception($"Expected Current, got {result.Status}"); } if (result.Reasons.Count != 0) { throw new Exception($"Expected 0 reasons for Current, got {result.Reasons.Count}"); } return Task.CompletedTask; } private static Task Test_FV2_Outdated() { var evaluator = CreateEvaluator(); var result = evaluator.Evaluate("2.4.0", "2.4.0", 2); if (result.Status != FleetVersionStatus.Outdated) { throw new Exception($"Expected Outdated, got {result.Status}"); } if (result.Reasons.Count == 0 || !result.Reasons.Any(r => r.Contains("建议升级"))) { throw new Exception($"Expected upgrade recommendation reason, got: {string.Join("; ", result.Reasons)}"); } return Task.CompletedTask; } private static Task Test_FV3_Unsupported() { var evaluator = CreateEvaluator(); var result = evaluator.Evaluate("2.1.0", "2.5.0", 2); if (result.Status != FleetVersionStatus.Unsupported) { throw new Exception($"Expected Unsupported, got {result.Status}"); } if (!result.Reasons.Any(r => r.Contains("低于最低支持版本"))) { throw new Exception($"Expected minimum version rejection reason, got: {string.Join("; ", result.Reasons)}"); } return Task.CompletedTask; } private static Task Test_FV4_Unknown() { var evaluator = CreateEvaluator(); var result = evaluator.Evaluate(null, "2.5.0", 2); if (result.Status != FleetVersionStatus.Unknown) { throw new Exception($"Expected Unknown, got {result.Status}"); } if (!result.Reasons.Any(r => r.Contains("版本信息不完整"))) { throw new Exception($"Expected incomplete version reason, got: {string.Join("; ", result.Reasons)}"); } return Task.CompletedTask; } private static Task Test_FV5_Protocol() { var evaluator = CreateEvaluator(); var result = evaluator.Evaluate("2.5.0", "2.5.0", 1); // 1 < 2 if (result.Status != FleetVersionStatus.Unsupported) { throw new Exception($"Expected Unsupported due to low protocol version, got {result.Status}"); } if (!result.Reasons.Any(r => r.Contains("低于最低协议"))) { throw new Exception($"Expected protocol reason, got: {string.Join("; ", result.Reasons)}"); } return Task.CompletedTask; } private static async Task Test_FV6_SummaryInvariant() { using var factory = new CustomWebApplicationFactory(); using var scope = factory.Services.CreateScope(); var db = scope.ServiceProvider.GetRequiredService(); // Seed 4 devices representing each state db.Tenants.Add(new Tenant { Id = "t_bj4", Name = "北京四中", IsActive = true }); db.Classrooms.Add(new Classroom { Id = "c_801", TenantId = "t_bj4", DisplayName = "八一班" }); db.Classrooms.Add(new Classroom { Id = "c_802", TenantId = "t_bj4", DisplayName = "八二班" }); db.Classrooms.Add(new Classroom { Id = "c_803", TenantId = "t_bj4", DisplayName = "八三班" }); db.Classrooms.Add(new Classroom { Id = "c_804", TenantId = "t_bj4", DisplayName = "八四班" }); db.Devices.AddRange(new[] { new Device { Id = "dev_curr", TenantId = "t_bj4", ClassroomId = "c_801", DeviceTokenHash = "h1", ServiceVersion = "2.5.0", ClientVersion = "2.5.0", ProtocolVersion = 2, IsActive = true }, new Device { Id = "dev_outd", TenantId = "t_bj4", ClassroomId = "c_802", DeviceTokenHash = "h2", ServiceVersion = "2.4.0", ClientVersion = "2.4.0", ProtocolVersion = 2, IsActive = true }, new Device { Id = "dev_unsp", TenantId = "t_bj4", ClassroomId = "c_803", DeviceTokenHash = "h3", ServiceVersion = "2.1.0", ClientVersion = "2.4.0", ProtocolVersion = 2, IsActive = true }, new Device { Id = "dev_unkn", TenantId = "t_bj4", ClassroomId = "c_804", DeviceTokenHash = "h4", ServiceVersion = null, ClientVersion = "2.4.0", ProtocolVersion = 2, IsActive = true } }); await db.SaveChangesAsync(); var fleetService = scope.ServiceProvider.GetRequiredService(); var summary = await fleetService.GetFleetVersionSummaryAsync(); if (summary.Total != 4) { throw new Exception($"Expected Total 4, got {summary.Total}"); } if (summary.Current != 1 || summary.Outdated != 1 || summary.Unsupported != 1 || summary.Unknown != 1) { throw new Exception($"Mismatch in states: Current={summary.Current}, Outdated={summary.Outdated}, Unsupported={summary.Unsupported}, Unknown={summary.Unknown}"); } if (summary.Current + summary.Outdated + summary.Unsupported + summary.Unknown != summary.Total) { throw new Exception("Summary Invariant Violated: Current + Outdated + Unsupported + Unknown != Total"); } } private static async Task Test_FV7_RbacSecurity() { using var factory = new CustomWebApplicationFactory(); var client = factory.CreateClient(); // 1. Anonymous -> 401 var anonRes = await client.GetAsync("/api/admin/fleet/version-summary"); if (anonRes.StatusCode != HttpStatusCode.Unauthorized) { throw new Exception($"Expected 401 for anonymous, got {anonRes.StatusCode}"); } // 2. Teacher -> 403 var teacherToken = factory.GenerateToken("usr_101", "teacher_zhang", "Teacher", "t_beijing_no4"); var teacherReq = new HttpRequestMessage(HttpMethod.Get, "/api/admin/fleet/version-summary"); teacherReq.Headers.Authorization = new AuthenticationHeaderValue("Bearer", teacherToken); var teacherRes = await client.SendAsync(teacherReq); if (teacherRes.StatusCode != HttpStatusCode.Forbidden) { throw new Exception($"Expected 403 for Teacher, got {teacherRes.StatusCode}"); } // 3. SchoolAdmin -> 403 var schoolToken = factory.GenerateToken("usr_admin_bj", "admin_bj4", "SchoolAdmin", "t_beijing_no4"); var schoolReq = new HttpRequestMessage(HttpMethod.Get, "/api/admin/fleet/version-summary"); schoolReq.Headers.Authorization = new AuthenticationHeaderValue("Bearer", schoolToken); var schoolRes = await client.SendAsync(schoolReq); if (schoolRes.StatusCode != HttpStatusCode.Forbidden) { throw new Exception($"Expected 403 for SchoolAdmin, got {schoolRes.StatusCode}"); } // 4. SuperAdmin -> 200 var superToken = factory.GenerateToken("usr_super", "admin", "SuperAdmin", "global"); var superReq = new HttpRequestMessage(HttpMethod.Get, "/api/admin/fleet/version-summary"); superReq.Headers.Authorization = new AuthenticationHeaderValue("Bearer", superToken); var superRes = await client.SendAsync(superReq); if (superRes.StatusCode != HttpStatusCode.OK) { throw new Exception($"Expected 200 for SuperAdmin, got {superRes.StatusCode}"); } } private static async Task Test_FV8_ObserveOnlyInvariant() { using var factory = new CustomWebApplicationFactory(); using var scope = factory.Services.CreateScope(); var db = scope.ServiceProvider.GetRequiredService(); // Seed an Unsupported device var dev = new Device { Id = "dev_unsupported_prod", TenantId = "t_observe", ClassroomId = "c_obs1", DeviceTokenHash = "tok_obs1_hash", ServiceVersion = "2.1.0", ClientVersion = "2.1.0", ProtocolVersion = 1, IsActive = true }; db.Tenants.Add(new Tenant { Id = "t_observe", Name = "观测试验校区", IsActive = true }); db.Classrooms.Add(new Classroom { Id = "c_obs1", TenantId = "t_observe", DisplayName = "观测一班" }); db.Devices.Add(dev); await db.SaveChangesAsync(); var fleetService = scope.ServiceProvider.GetRequiredService(); var summary = await fleetService.GetFleetVersionSummaryAsync(); if (summary.Unsupported < 1) { throw new Exception("Device was not classified as Unsupported"); } // Verify Device remains active and ready to receive notifications var dbDev = await db.Devices.FindAsync("dev_unsupported_prod"); if (dbDev == null || !dbDev.IsActive) { throw new Exception("Observe-Only Invariant Violated: Unsupported device was deactivated!"); } } private static async Task Test_FV9_QueryFiltering() { using var factory = new CustomWebApplicationFactory(); using var scope = factory.Services.CreateScope(); var db = scope.ServiceProvider.GetRequiredService(); db.Tenants.Add(new Tenant { Id = "t_f1", Name = "实验一中", IsActive = true }); db.Tenants.Add(new Tenant { Id = "t_f2", Name = "实验二中", IsActive = true }); db.Classrooms.Add(new Classroom { Id = "c_f1", TenantId = "t_f1", DisplayName = "高一甲班" }); db.Classrooms.Add(new Classroom { Id = "c_f2", TenantId = "t_f2", DisplayName = "高一乙班" }); db.Devices.AddRange(new[] { new Device { Id = "dev_filter_unsp", TenantId = "t_f1", ClassroomId = "c_f1", DeviceTokenHash = "h1", Hostname = "ROOM-101", ServiceVersion = "2.1.0", ClientVersion = "2.1.0", ProtocolVersion = 2, IsActive = true }, new Device { Id = "dev_filter_curr", TenantId = "t_f2", ClassroomId = "c_f2", DeviceTokenHash = "h2", Hostname = "ROOM-202", ServiceVersion = "2.5.0", ClientVersion = "2.5.0", ProtocolVersion = 2, IsActive = true } }); await db.SaveChangesAsync(); var fleetService = scope.ServiceProvider.GetRequiredService(); // Query by Status var unspOnly = await fleetService.GetFleetDevicesAsync(new FleetDeviceVersionQuery { Status = "Unsupported" }); if (unspOnly.Count != 1 || unspOnly[0].DeviceId != "dev_filter_unsp") { throw new Exception($"Expected 1 Unsupported device dev_filter_unsp, got {unspOnly.Count}"); } // Query by TenantId var t2Only = await fleetService.GetFleetDevicesAsync(new FleetDeviceVersionQuery { TenantId = "t_f2" }); if (t2Only.Count != 1 || t2Only[0].DeviceId != "dev_filter_curr") { throw new Exception($"Expected 1 device for t_f2, got {t2Only.Count}"); } // Query by Keyword var qResults = await fleetService.GetFleetDevicesAsync(new FleetDeviceVersionQuery { Q = "ROOM-101" }); if (qResults.Count != 1 || qResults[0].DeviceId != "dev_filter_unsp") { throw new Exception($"Expected 1 device for keyword ROOM-101, got {qResults.Count}"); } } private static async Task Test_FV10_ZeroMutation() { using var factory = new CustomWebApplicationFactory(); var client = factory.CreateClient(); var superToken = factory.GenerateToken("usr_super", "admin", "SuperAdmin", "global"); using (var scope = factory.Services.CreateScope()) { var db = scope.ServiceProvider.GetRequiredService(); db.Tenants.Add(new Tenant { Id = "t_mut", Name = "变动测试校", IsActive = true }); db.Classrooms.Add(new Classroom { Id = "c_mut", TenantId = "t_mut", DisplayName = "测试班" }); db.Devices.Add(new Device { Id = "dev_mut", TenantId = "t_mut", ClassroomId = "c_mut", DeviceTokenHash = "h_mut", ServiceVersion = "2.4.0", ClientVersion = "2.4.0", ProtocolVersion = 2, IsActive = true }); await db.SaveChangesAsync(); } int initialAuditCount; int initialCommandCount; using (var scope = factory.Services.CreateScope()) { var db = scope.ServiceProvider.GetRequiredService(); initialAuditCount = await db.AdminAuditEvents.CountAsync(); initialCommandCount = await db.DeviceCommands.CountAsync(); } // Invoke both endpoints var req1 = new HttpRequestMessage(HttpMethod.Get, "/api/admin/fleet/version-summary"); req1.Headers.Authorization = new AuthenticationHeaderValue("Bearer", superToken); var res1 = await client.SendAsync(req1); if (res1.StatusCode != HttpStatusCode.OK) throw new Exception("Summary API failed"); var req2 = new HttpRequestMessage(HttpMethod.Get, "/api/admin/fleet/devices?status=Outdated"); req2.Headers.Authorization = new AuthenticationHeaderValue("Bearer", superToken); var res2 = await client.SendAsync(req2); if (res2.StatusCode != HttpStatusCode.OK) throw new Exception("Devices API failed"); using (var scope = factory.Services.CreateScope()) { var db = scope.ServiceProvider.GetRequiredService(); int finalAuditCount = await db.AdminAuditEvents.CountAsync(); int finalCommandCount = await db.DeviceCommands.CountAsync(); if (finalAuditCount != initialAuditCount) { throw new Exception($"Zero Mutation Invariant Violated: Audit event count changed from {initialAuditCount} to {finalAuditCount}"); } if (finalCommandCount != initialCommandCount) { throw new Exception($"Zero Mutation Invariant Violated: Device command count changed from {initialCommandCount} to {finalCommandCount}"); } } } }