Files
LMEMappeCatastaliApp/tools/LmeLicenseManager/Program.cs
T

81 lines
3.0 KiB
C#

namespace LmeLicenseManager;
internal static class Program
{
[STAThread]
private static void Main(string[] args)
{
if (args.Contains("--self-test", StringComparer.OrdinalIgnoreCase))
{
RunSelfTest();
return;
}
var repository = new LicenseRepository();
repository.Initialize();
if (args.Contains("--init", StringComparer.OrdinalIgnoreCase))
{
return;
}
ApplicationConfiguration.Initialize();
Application.Run(new MainForm(repository));
}
private static void RunSelfTest()
{
var testDirectory = Path.Combine(Path.GetTempPath(), $"LME-License-SelfTest-{Guid.NewGuid():N}");
var importedDirectory = Path.Combine(Path.GetTempPath(), $"LME-License-SelfTest-{Guid.NewGuid():N}");
const string backupPassword = "LME-Test-Backup-Password-2026";
try
{
var repository = new LicenseRepository(testDirectory, allowKeyGeneration: true);
repository.Initialize();
var license = repository.Issue(
"Mario",
"Rossi",
"mario.rossi@example.test",
DateTimeOffset.Now.AddDays(30),
"Test automatico");
if (!repository.VerifyToken(license.Token))
{
throw new InvalidOperationException("Il codice emesso non supera la verifica crittografica.");
}
var backupPath = Path.Combine(testDirectory, "signing-key.lmekey");
repository.ExportSigningKeyBackup(backupPath, backupPassword);
var importedRepository = new LicenseRepository(importedDirectory, allowKeyGeneration: true);
importedRepository.Initialize();
importedRepository.ImportSigningKeyBackup(backupPath, backupPassword);
var portableLicense = importedRepository.Issue(
"Giulia",
"Bianchi",
"giulia.bianchi@example.test",
DateTimeOffset.Now.AddYears(1),
"Test portabilita chiave");
if (!repository.VerifyToken(portableLicense.Token)
|| !importedRepository.VerifyToken(license.Token))
{
throw new InvalidOperationException("La chiave importata non produce licenze compatibili.");
}
Console.WriteLine("Self-test licenze e portabilita chiave completato.");
}
finally
{
foreach (var directory in new[] { testDirectory, importedDirectory })
{
var fullPath = Path.GetFullPath(directory);
var tempRoot = Path.GetFullPath(Path.GetTempPath());
if (Directory.Exists(fullPath)
&& fullPath.StartsWith(tempRoot, StringComparison.OrdinalIgnoreCase)
&& Path.GetFileName(fullPath).StartsWith("LME-License-SelfTest-", StringComparison.Ordinal))
{
Directory.Delete(fullPath, true);
}
}
}
}
}