using System; using System.IO; using System.Net.Http; using System.Text; using System.Text.Json; using System.Threading.Tasks; using Deploymentcenter.Client; namespace Deploymentcenter.TestClient; class Program { private static readonly string BaseUrl = "https://dc.mhdf.de"; private static readonly HttpClient Client = new HttpClient(new HttpClientHandler { ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true }); static async Task Main(string[] args) { Console.OutputEncoding = Encoding.UTF8; if (args.Length > 0) { await HandleCliArgsAsync(args); return; } Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("================================================="); Console.WriteLine(" 🚀 Deploymentcenter Unified Client Test Suite "); Console.WriteLine("================================================="); Console.ResetColor(); Console.WriteLine($" Server Base URL: {BaseUrl}\n"); await TestHardwareIdV2ModuleAsync(); await TestStateStoreHardeningAsync(); await TestLicenseModuleAsync(); await TestWatchdogModuleAsync(); await TestUpdateServiceModuleAsync(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("\n[✔] Alle Systemprüfungen erfolgreich abgeschlossen!"); Console.ResetColor(); } private static async Task HandleCliArgsAsync(string[] args) { string cmd = args[0].ToLowerInvariant(); if (cmd == "--license-status") { var hwInfo = HardwareId.GetHardwareId("myapp"); string storageDir = LicenseConfig.GetStorageDirectory("myapp"); var cache = StateStore.Load("myapp", hwInfo.HardwareId); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("================================================="); Console.WriteLine(" 🔑 Deploymentcenter Lizenz & HWID Status "); Console.WriteLine("================================================="); Console.ResetColor(); Console.WriteLine($" Platform: {hwInfo.Platform.ToUpperInvariant()}"); Console.WriteLine($" HWID v2: {hwInfo.HardwareId}"); Console.WriteLine($" HWID Source: {hwInfo.HwidSource}"); Console.WriteLine($" Legacy HWID (v1): {hwInfo.LegacyHardwareId}"); Console.WriteLine($" Storage Directory:{storageDir}"); Console.WriteLine($" Local Cache: {(cache != null ? $"VALID (Status: {cache.Status}, Schema: {cache.SchemaVersion})" : "NONE / MISSING")}"); return; } if (cmd == "--license-deactivate") { string key = args.Length > 1 ? args[1] : "LLAB1-98A72-B3C4D-5E6F7-89012"; Console.WriteLine($"Deaktiviere Lizenz '{key}' für diesen Host..."); var licClient = new LicenseClient(Client); bool success = await licClient.DeactivateAsync("myapp", key, BaseUrl, "dc_shared_master_secret"); if (success) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("✔ Lizenz-Aktivierung erfolgreich am Server aufgehoben!"); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("✖ Fehler bei der Lizenz-Deaktivierung."); } Console.ResetColor(); return; } if (cmd == "--license-set-key") { if (args.Length < 2) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Fehler: Bitte Lizenzschlüssel angeben. Beispiel: --license-set-key LLAB1-XXXXX"); Console.ResetColor(); return; } string key = args[1].Trim(); Console.WriteLine($"Prüfe neuen Lizenzschlüssel '{key}'..."); var licClient = new LicenseClient(Client); var res = await licClient.ValidateAsync("myapp", key, BaseUrl); if (res.IsValid) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"✔ Lizenz '{key}' erfolgreich aktiviert! Status: {res.Status}"); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"✖ Lizenzprüfung fehlgeschlagen: {res.Message} (Status: {res.Status})"); } Console.ResetColor(); return; } Console.WriteLine($"Unbekanntes Argument: {args[0]}"); Console.WriteLine("Verfügbare Befehle:"); Console.WriteLine(" --license-status"); Console.WriteLine(" --license-deactivate [KEY]"); Console.WriteLine(" --license-set-key "); } private static Task TestHardwareIdV2ModuleAsync() { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("1. Teste Modul: HardwareId v2 Berechnungen & Quellenerkennung..."); Console.ResetColor(); var hwInfo = HardwareId.GetHardwareId("myapp"); Console.WriteLine($" Platform: {hwInfo.Platform}"); Console.WriteLine($" Hardware ID v2: {hwInfo.HardwareId}"); Console.WriteLine($" HWID Source: {hwInfo.HwidSource}"); Console.WriteLine($" Legacy HWID v1: {hwInfo.LegacyHardwareId}"); if (hwInfo.HardwareId.StartsWith("2:") && hwInfo.HardwareId.Length == 70) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(" ✔ Hardware-ID v2 Format validiert! (2::<64 hex>)"); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(" ✖ Fehlerhaftes Hardware-ID v2 Format!"); } Console.ResetColor(); Console.WriteLine(); return Task.CompletedTask; } private static Task TestStateStoreHardeningAsync() { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("2. Teste Modul: StateStore LLS2 AES-GCM Verschlüsselung & Integrität..."); Console.ResetColor(); string slug = "test_app_hardening"; var hwInfo = HardwareId.GetHardwareId(slug); var testCache = new LocalCacheData { SchemaVersion = 2, ProductSlug = slug, LicenseKey = "LLAB-TEST-12345", HardwareId = hwInfo.HardwareId, Status = "valid", IssuedAt = DateTimeOffset.UtcNow.ToUnixTimeSeconds(), ExpiresAt = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds(), MaxSeenTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds(), Checksum = hwInfo.HardwareId }; bool saved = StateStore.Save(slug, hwInfo.HardwareId, testCache); var loaded = StateStore.Load(slug, hwInfo.HardwareId); if (saved && loaded != null && loaded.Status == "valid" && loaded.SchemaVersion == 2) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(" ✔ StateStore LLS2 AES-GCM Speicher & Entschlüsselung OK!"); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(" ✖ Fehler beim StateStore Test!"); } Console.ResetColor(); Console.WriteLine(); return Task.CompletedTask; } private static async Task TestLicenseModuleAsync() { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("3. Teste Modul: Lizenzen (Online Validation & v1 Migration)..."); Console.ResetColor(); try { var licClient = new LicenseClient(Client); var result = await licClient.ValidateAsync("myapp", "LLAB1-98A72-B3C4D-5E6F7-89012", BaseUrl); Console.WriteLine($" Status: {result.Status}"); Console.WriteLine($" Hardware ID: {result.HardwareId}"); Console.WriteLine($" Message: {result.Message}"); if (result.IsValid) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(" ✔ License Validation Server Test: ERFOLGREICH!"); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($" ✖ License Validation Server Test: {result.Message}"); } } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($" ✖ Fehler beim Lizenzen-Test: {ex.Message}"); } Console.ResetColor(); Console.WriteLine(); } private static async Task TestWatchdogModuleAsync() { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("4. Teste Modul: Watchdog (Heartbeat Ping)..."); Console.ResetColor(); try { var payload = new { source = "srv-db-01", instance = "default", type = "heartbeat", status = "ok", message = "C# Client Test Suite running smoothly", interval = 60, group = "TestRunner", os = OperatingSystemHelpers.GetPlatformCode() }; string json = JsonSerializer.Serialize(payload); var content = new StringContent(json, Encoding.UTF8, "application/json"); var request = new HttpRequestMessage(HttpMethod.Post, $"{BaseUrl}/api/watchdog/v1/ping") { Content = content }; request.Headers.Add("X-Agent-Token", "wd_live_token_infra_01_secure"); HttpResponseMessage response = await Client.SendAsync(request); string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine($" HTTP Status: {(int)response.StatusCode} {response.StatusCode}"); if (response.IsSuccessStatusCode) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(" ✔ Watchdog Modul Test: ERFOLGREICH!"); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(" ✖ Watchdog Modul Test: FEHLER!"); } } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($" ✖ Fehler beim Watchdog-Test: {ex.Message}"); } Console.ResetColor(); Console.WriteLine(); } private static async Task TestUpdateServiceModuleAsync() { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("5. Teste Modul: UpdateService (Release Check)..."); Console.ResetColor(); try { HttpResponseMessage response = await Client.GetAsync($"{BaseUrl}/api/updateservice/v1/check?product=myapp&version=1.0.0"); string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine($" HTTP Status: {(int)response.StatusCode} {response.StatusCode}"); if (response.IsSuccessStatusCode) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(" ✔ UpdateService Modul Test: ERFOLGREICH!"); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(" ✖ UpdateService Modul Test: FEHLER!"); } } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($" ✖ Fehler beim UpdateService-Test: {ex.Message}"); } Console.ResetColor(); Console.WriteLine(); } }