Il client del sito ha bisogno della sessione del browser di Febe: ora la copia c'è, cifrata e dietro un interruttore, senza cambiare nulla di predefinito. - Opzione «raccolta» (pagine predefinita, richieste): solo con «richieste» e almeno un'operazione configurata si costruiscono sessione salvata, client del sito e copia. Con Instagram nulla cambia: le dieci operazioni restano non configurate (mancano __spin_*, __req, __s, __dyn, __csr e altri). - Cookie con dominio, percorso, solo-host, Secure, HttpOnly e scadenza, mandati solo dove il browser li manderebbe (RFC 6265). - Sezione «page» del catalogo: dove la pagina tiene ogni valore; nuovi campi rev, hs, hsi, dpr, ccg, comet_req, entry_nonce. Il caricatore rifiuta un'operazione configurata con un valore di sessione senza fonte nella pagina. - Dati/sessione/sessione.json: DPAPI su Windows, AES-256-GCM in Docker (chiave da FEBE_CHIAVE_SESSIONE o chiave.bin), scrittura atomica, file illeggibile = nessuna sessione. «Scollega» la cancella; chiave.bin resta. - Copia a «Ho finito» con Network.getCookies e una sola Runtime.evaluate in sola lettura, solo da una pagina del sito e finché dura il permesso di guida. - FEBE_PASSWORD e FEBE_CHIAVE_SESSIONE non passano a Chromium, Xvfb, ffmpeg. - Sito di prova: csrftoken, cookie limitato a /api/, registro di moduli suo. - Revisione su tre lenti: 11 punti confermati, tutti corretti. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
394 lines
20 KiB
C#
394 lines
20 KiB
C#
using System.Collections.Concurrent;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using Febe.Core.Collection.Requests;
|
|
using Febe.Core.Library;
|
|
using Febe.Core.Security;
|
|
using Febe.Tests.Support;
|
|
|
|
namespace Febe.Tests.Collection;
|
|
|
|
/// <summary>
|
|
/// The saved session on disk (issue #36, S4, spec §6.0, §13.2): Dati/sessione/sessione.json encrypted, owner-only,
|
|
/// replaced at once; read back by a new start as a new capture; renewals serialised and kept in memory when the disk
|
|
/// refuses them; «Scollega» deletes it for good; a file that cannot be read is no session, never a crash; no value in
|
|
/// clear on disk, in a text or in a log line.
|
|
/// </summary>
|
|
[TestClass]
|
|
public sealed class FileSiteSessionTests
|
|
{
|
|
private static readonly DateTimeOffset Now = new(2026, 11, 17, 10, 0, 0, TimeSpan.Zero);
|
|
private static readonly Uri Graph = new("https://www.instagram.com/api/graphql");
|
|
|
|
private static readonly string[] Secrets =
|
|
[
|
|
"segreto-sessione-1", "segreto-csrf-2", "segreto-percorso-3", "segreto-lsd-4", "segreto-dtsg-5", "segreto-rev-6",
|
|
"segreto-nonce-7", "segreto-docid-8", "segreto-agente-9", "segreto-claim-10",
|
|
];
|
|
|
|
[TestMethod]
|
|
public async Task WhatIsWrittenIsReadBackByANewInstance()
|
|
{
|
|
using var folder = new TempFolder("sessione");
|
|
var layout = new DataFolderLayout(folder.Path);
|
|
var protector = Protector();
|
|
var store = new FileSiteSession(layout.SessionFile, protector);
|
|
Assert.AreEqual(SavedSessionState.None, store.State);
|
|
Assert.IsNull(store.Read());
|
|
|
|
var values = Values();
|
|
Assert.IsTrue(await store.ReplaceAsync(values, CancellationToken.None));
|
|
Assert.AreSame(values, store.Read());
|
|
Assert.AreEqual(SavedSessionState.Present, store.State);
|
|
Assert.IsFalse(store.Unsaved);
|
|
Assert.AreEqual(Path.Combine(folder.Path, "sessione", "sessione.json"), store.FilePath, "Dati/sessione/sessione.json.");
|
|
|
|
var restored = new FileSiteSession(layout.SessionFile, protector).Read();
|
|
Assert.IsNotNull(restored);
|
|
Assert.AreEqual(SavedSessionState.Present, new FileSiteSession(layout.SessionFile, protector).State);
|
|
CollectionAssert.AreEqual(
|
|
values.Cookies.Select(Describe).ToArray(),
|
|
restored.Cookies.Select(Describe).ToArray(),
|
|
"Name, value, domain, host-only, path, secure, HTTP-only and expiry, in order.");
|
|
Assert.AreEqual(values.UserAgent, restored.UserAgent);
|
|
CollectionAssert.AreEquivalent(values.Fields.ToArray(), restored.Fields.ToArray());
|
|
CollectionAssert.AreEquivalent(values.DocIds.ToArray(), restored.DocIds.ToArray());
|
|
Assert.AreEqual(values.CopiedAt, restored.CopiedAt);
|
|
Assert.AreEqual("segreto-percorso-3", restored.CookieFor("ds_user_id", Graph, Now), "The path cookie still goes to /api/.");
|
|
Assert.IsNull(restored.CookieFor("ds_user_id", new Uri("https://www.instagram.com/marta/"), Now));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task AValueReadBackIsANewCaptureSoALateRenewalOfTheOldProcessTouchesNothing()
|
|
{
|
|
using var folder = new TempFolder("sessione");
|
|
var file = Path.Combine(folder.Path, "sessione", "sessione.json");
|
|
var protector = Protector();
|
|
var before = new FileSiteSession(file, protector);
|
|
var values = Values();
|
|
await before.ReplaceAsync(values, CancellationToken.None);
|
|
|
|
var after = new FileSiteSession(file, protector);
|
|
var restored = after.Read()!;
|
|
Assert.AreNotEqual(values.Capture, restored.Capture, "A new start is a new capture.");
|
|
Assert.AreNotEqual(restored.Capture, new SiteSessionValues([], "agente").Capture, "And later captures never reuse its id.");
|
|
|
|
await after.UpdateAsync(new SiteSessionUpdate([Cookie("sessionid", "vecchia-risposta")], capture: values.Capture), CancellationToken.None);
|
|
Assert.AreEqual("segreto-sessione-1", after.Read()!.Cookie("sessionid"));
|
|
Assert.AreEqual("segreto-sessione-1", new FileSiteSession(file, protector).Read()!.Cookie("sessionid"), "Nor the file.");
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task TheFileHoldsNoValueInClear()
|
|
{
|
|
using var folder = new TempFolder("sessione");
|
|
var file = Path.Combine(folder.Path, "sessione.json");
|
|
var store = new FileSiteSession(file, Protector());
|
|
await store.ReplaceAsync(Values(), CancellationToken.None);
|
|
|
|
var bytes = File.ReadAllBytes(file);
|
|
Assert.AreEqual("FEBS", Encoding.ASCII.GetString(bytes, 0, 4));
|
|
foreach (var secret in Secrets.Append("sessionid").Append("instagram"))
|
|
{
|
|
foreach (var form in new[] { Encoding.UTF8.GetBytes(secret), Encoding.Unicode.GetBytes(secret), Encoding.ASCII.GetBytes(Convert.ToBase64String(Encoding.UTF8.GetBytes(secret))) })
|
|
{
|
|
Assert.AreEqual(-1, bytes.AsSpan().IndexOf(form), $"«{secret}» is on disk in clear.");
|
|
}
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task AFailedWriteKeepsTheRenewedCookiesInMemoryAndIsTriedAgainLater()
|
|
{
|
|
using var folder = new TempFolder("sessione");
|
|
var file = Path.Combine(folder.Path, "sessione.json");
|
|
var protector = new FlakyProtector(Protector());
|
|
var log = new ConcurrentQueue<string>();
|
|
var store = new FileSiteSession(file, protector, log.Enqueue);
|
|
await store.ReplaceAsync(Values(), CancellationToken.None);
|
|
var capture = store.Read()!.Capture;
|
|
|
|
// The site rotated csrftoken: the disk refuses, the memory keeps the new value (the old one no longer works).
|
|
protector.Fail = true;
|
|
await store.UpdateAsync(new SiteSessionUpdate([Csrf("segreto-rinnovo-11")], capture: capture), CancellationToken.None);
|
|
await store.UpdateAsync(new SiteSessionUpdate([Csrf("segreto-rinnovo-12")], capture: capture), CancellationToken.None);
|
|
Assert.AreEqual("segreto-rinnovo-12", store.Read()!.Cookie("csrftoken"));
|
|
Assert.IsTrue(store.Unsaved);
|
|
Assert.AreEqual("segreto-csrf-2", new FileSiteSession(file, Protector(protector)).Read()!.Cookie("csrftoken"), "The file is the old one, whole.");
|
|
Assert.AreEqual(1, log.Count(l => l.Contains("non scritta", StringComparison.Ordinal)), "One line for a run of failures:\n" + string.Join("\n", log));
|
|
Assert.IsFalse(Directory.GetFiles(folder.Path).Any(f => f.EndsWith(OwnerOnlyFiles.PartSuffix, StringComparison.Ordinal)));
|
|
|
|
// A failed copy at «Ho finito» still replaces the session in memory, and says it is not on disk.
|
|
Assert.IsFalse(await store.ReplaceAsync(Values(csrf: "segreto-nuovo-accesso-13"), CancellationToken.None));
|
|
Assert.AreEqual("segreto-nuovo-accesso-13", store.Read()!.Cookie("csrftoken"));
|
|
capture = store.Read()!.Capture;
|
|
|
|
protector.Fail = false;
|
|
await store.UpdateAsync(new SiteSessionUpdate([Cookie("mid", "segreto-mid-14")], capture: capture), CancellationToken.None);
|
|
Assert.IsFalse(store.Unsaved);
|
|
var reread = new FileSiteSession(file, Protector(protector)).Read()!;
|
|
Assert.AreEqual("segreto-nuovo-accesso-13", reread.Cookie("csrftoken"), "The next write carries everything kept in memory.");
|
|
Assert.AreEqual("segreto-mid-14", reread.Cookie("mid"));
|
|
Assert.IsTrue(log.Any(l => l.Contains("di nuovo", StringComparison.Ordinal)));
|
|
AssertNoSecret(log);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task AWriteTheDiskRefusesAfterEncryptionLeavesNoPartFileKeepsTheMemoryAndTheNextWriteSucceeds()
|
|
{
|
|
using var folder = new TempFolder("sessione");
|
|
var file = Path.Combine(folder.Path, "sessione.json");
|
|
var protector = Protector();
|
|
var log = new ConcurrentQueue<string>();
|
|
var store = new FileSiteSession(file, protector, log.Enqueue);
|
|
|
|
// Something the rename cannot replace stands where the session goes: the part file is written, the move fails.
|
|
Directory.CreateDirectory(file);
|
|
Assert.IsFalse(await store.ReplaceAsync(Values(), CancellationToken.None));
|
|
Assert.IsTrue(store.Unsaved);
|
|
Assert.AreEqual("segreto-csrf-2", store.Read()!.Cookie("csrftoken"), "Kept in memory.");
|
|
Assert.IsEmpty(Directory.GetFiles(folder.Path, "*" + OwnerOnlyFiles.PartSuffix), "A failed write leaves no part file.");
|
|
Assert.AreEqual(1, log.Count(l => l.Contains("non scritta", StringComparison.Ordinal)), string.Join("\n", log));
|
|
|
|
// The obstacle goes: the next write carries what memory kept.
|
|
Directory.Delete(file);
|
|
var capture = store.Read()!.Capture;
|
|
await store.UpdateAsync(new SiteSessionUpdate([Cookie("mid", "segreto-mid-14")], capture: capture), CancellationToken.None);
|
|
Assert.IsFalse(store.Unsaved);
|
|
var reread = new FileSiteSession(file, protector).Read()!;
|
|
Assert.AreEqual("segreto-csrf-2", reread.Cookie("csrftoken"));
|
|
Assert.AreEqual("segreto-mid-14", reread.Cookie("mid"));
|
|
|
|
if (OperatingSystem.IsWindows())
|
|
{
|
|
// On Windows a file held open without sharing cannot be replaced: the old file stays whole.
|
|
var before = File.ReadAllBytes(file);
|
|
using (new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.None))
|
|
{
|
|
await store.UpdateAsync(new SiteSessionUpdate([Cookie("mid", "segreto-mid-15")], capture: capture), CancellationToken.None);
|
|
}
|
|
|
|
Assert.IsTrue(store.Unsaved);
|
|
CollectionAssert.AreEqual(before, File.ReadAllBytes(file), "The old file is intact.");
|
|
Assert.IsEmpty(Directory.GetFiles(folder.Path, "*" + OwnerOnlyFiles.PartSuffix));
|
|
}
|
|
|
|
AssertNoSecret(log);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task ConcurrentUpdatesAreSerializedAndNoneIsLost()
|
|
{
|
|
using var folder = new TempFolder("sessione");
|
|
var file = Path.Combine(folder.Path, "sessione.json");
|
|
var protector = Protector();
|
|
var store = new FileSiteSession(file, protector);
|
|
await store.ReplaceAsync(Values(), CancellationToken.None);
|
|
var capture = store.Read()!.Capture;
|
|
|
|
await Task.WhenAll(Enumerable.Range(0, 40).Select(i => Task.Run(() =>
|
|
store.UpdateAsync(new SiteSessionUpdate([Cookie("c" + i.ToString(System.Globalization.CultureInfo.InvariantCulture), "v")], capture: capture), CancellationToken.None))));
|
|
|
|
Assert.AreEqual(Values().Cookies.Count + 40, store.Read()!.Cookies.Count);
|
|
Assert.AreEqual(Values().Cookies.Count + 40, new FileSiteSession(file, protector).Read()!.Cookies.Count, "The last write holds every update.");
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task DeleteRemovesTheFileAndItsLeftoversAndNoLateUpdateBringsItBack()
|
|
{
|
|
using var folder = new TempFolder("sessione");
|
|
var file = Path.Combine(folder.Path, "sessione.json");
|
|
var protector = new FlakyProtector(Protector());
|
|
var log = new List<string>();
|
|
var store = new FileSiteSession(file, protector, log.Add);
|
|
await store.ReplaceAsync(Values(), CancellationToken.None);
|
|
var capture = store.Read()!.Capture;
|
|
File.WriteAllText(file + ".1234abcd" + OwnerOnlyFiles.PartSuffix, "resto");
|
|
|
|
// An update waits behind a write that is in progress; «Scollega» comes; whatever the order they get the gate
|
|
// in, no session is left.
|
|
using var hold = new ManualResetEventSlim(false);
|
|
protector.Hold = hold;
|
|
var replacing = Task.Run(() => store.ReplaceAsync(store.Read()!, CancellationToken.None));
|
|
Assert.IsTrue(protector.Entered.Wait(TimeSpan.FromSeconds(30)));
|
|
var updating = store.UpdateAsync(new SiteSessionUpdate([Cookie("tardivo", "x")], capture: capture), CancellationToken.None);
|
|
var deleting = store.DeleteAsync(CancellationToken.None);
|
|
hold.Set();
|
|
await Task.WhenAll(replacing, updating, deleting);
|
|
await store.UpdateAsync(new SiteSessionUpdate([Cookie("tardivo", "y")], capture: capture), CancellationToken.None);
|
|
|
|
Assert.IsNull(store.Read());
|
|
Assert.AreEqual(SavedSessionState.None, store.State);
|
|
Assert.IsFalse(File.Exists(file));
|
|
Assert.AreEqual(0, Directory.GetFiles(folder.Path).Length, "No part file left either.");
|
|
Assert.IsNull(new FileSiteSession(file, protector).Read());
|
|
Assert.IsTrue(log.Any(l => l.Contains("cancellata", StringComparison.Ordinal)));
|
|
|
|
await store.DeleteAsync(CancellationToken.None);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task AnUnreadableFileIsNoSessionAndIsLeftAsItIs()
|
|
{
|
|
using var folder = new TempFolder("sessione");
|
|
var file = Path.Combine(folder.Path, "sessione.json");
|
|
var protector = Protector();
|
|
await new FileSiteSession(file, protector).ReplaceAsync(Values(), CancellationToken.None);
|
|
var written = File.ReadAllBytes(file);
|
|
|
|
// Another key (a new FEBE_CHIAVE_SESSIONE, another user for DPAPI), a damaged file, another format.
|
|
var cases = new (string File, ISessionProtector Protector, byte[] Bytes)[]
|
|
{
|
|
(file, Protector(), written),
|
|
(Path.Combine(folder.Path, "rovinato.json"), protector, [.. written.Take(written.Length - 3)]),
|
|
(Path.Combine(folder.Path, "testo.json"), protector, Encoding.UTF8.GetBytes("""{"format":1,"cookies":[]}""")),
|
|
(Path.Combine(folder.Path, "formato.json"), protector, protector.Protect(Encoding.UTF8.GetBytes("""{"format":9,"segreto":"segreto-formato-15"}"""))),
|
|
(Path.Combine(folder.Path, "cookie.json"), protector, protector.Protect(Encoding.UTF8.GetBytes("""{"format":1,"userAgent":"a","cookies":[{"name":"a=b","value":"segreto-cookie-16","domain":"x","hostOnly":true,"path":"/","secure":false,"httpOnly":false}],"fields":{},"docIds":{}}"""))),
|
|
};
|
|
foreach (var (path, reader, bytes) in cases)
|
|
{
|
|
if (path != file)
|
|
{
|
|
File.WriteAllBytes(path, bytes);
|
|
}
|
|
|
|
var log = new List<string>();
|
|
var store = new FileSiteSession(path, reader, log.Add);
|
|
Assert.IsNull(store.Read(), path);
|
|
Assert.AreEqual(SavedSessionState.Unreadable, store.State, path);
|
|
CollectionAssert.AreEqual(bytes, File.ReadAllBytes(path), "Left as it is.");
|
|
Assert.AreEqual(1, log.Count);
|
|
StringAssert.Contains(log[0], "illeggibile");
|
|
Assert.IsFalse(log[0].Contains("segreto", StringComparison.Ordinal) || log[0].Contains(path, StringComparison.Ordinal), log[0]);
|
|
Assert.AreEqual("Sessione salvata illeggibile.", store.ToString());
|
|
|
|
// A renewal finds no session; a new login replaces the file.
|
|
await store.UpdateAsync(new SiteSessionUpdate([Cookie("x", "y")]), CancellationToken.None);
|
|
Assert.IsNull(store.Read());
|
|
Assert.IsTrue(await store.ReplaceAsync(Values(), CancellationToken.None));
|
|
Assert.AreEqual(SavedSessionState.Present, new FileSiteSession(path, reader).State);
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task TheFolderAndTheFileAreOwnerOnlyOnUnix()
|
|
{
|
|
using var folder = new TempFolder("sessione");
|
|
var layout = new DataFolderLayout(folder.Path);
|
|
await new FileSiteSession(layout.SessionFile, Protector()).ReplaceAsync(Values(), CancellationToken.None);
|
|
|
|
Assert.IsTrue(File.Exists(layout.SessionFile));
|
|
Assert.AreEqual(layout.Session, Path.GetDirectoryName(layout.SessionFile));
|
|
Assert.AreEqual(layout.Session, Path.GetDirectoryName(layout.SessionKeyFile));
|
|
if (!OperatingSystem.IsWindows())
|
|
{
|
|
Assert.AreEqual(OwnerOnlyFiles.OwnerFile, File.GetUnixFileMode(layout.SessionFile));
|
|
Assert.AreEqual(OwnerOnlyFiles.OwnerFolder, File.GetUnixFileMode(layout.Session));
|
|
}
|
|
|
|
// The daily copies and the diagnostic report live elsewhere and never look in Dati/sessione.
|
|
Assert.IsFalse(layout.Session.StartsWith(layout.Backups, StringComparison.Ordinal));
|
|
Assert.IsFalse(layout.Session.StartsWith(layout.Diagnostics, StringComparison.Ordinal));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task TheTextAndTheLogLinesShowCountsOnly()
|
|
{
|
|
using var folder = new TempFolder("sessione");
|
|
var file = Path.Combine(folder.Path, "sessione.json");
|
|
var log = new List<string>();
|
|
var protector = Protector();
|
|
var store = new FileSiteSession(file, protector, log.Add);
|
|
Assert.AreEqual("Nessuna sessione salvata.", store.ToString());
|
|
await store.ReplaceAsync(Values(), CancellationToken.None);
|
|
await store.UpdateAsync(new SiteSessionUpdate([Csrf("segreto-rinnovo-17")], claim: "segreto-claim-18"), CancellationToken.None);
|
|
_ = new FileSiteSession(file, protector, log.Add);
|
|
await store.DeleteAsync(CancellationToken.None);
|
|
|
|
var texts = log.Append(store.ToString()).Append(protector.ToString()).ToList();
|
|
AssertNoSecret(texts);
|
|
foreach (var text in texts)
|
|
{
|
|
Assert.IsFalse(text.Contains("csrftoken", StringComparison.Ordinal) || text.Contains(folder.Path, StringComparison.Ordinal), text);
|
|
}
|
|
|
|
Assert.IsTrue(log.Any(l => l.Contains("Sessione salvata scritta: 4 cookie", StringComparison.Ordinal)), string.Join("\n", log));
|
|
Assert.IsTrue(log.Any(l => l.Contains("Sessione salvata letta", StringComparison.Ordinal)));
|
|
}
|
|
|
|
/// <summary>A session with a cookie of each kind, every page value and a doc_id; values from <see cref="Secrets"/>.</summary>
|
|
private static SiteSessionValues Values(string csrf = "segreto-csrf-2") => new(
|
|
[
|
|
new SiteCookie("sessionid", "segreto-sessione-1", ".instagram.com", "/", hostOnly: false, secure: true, httpOnly: true, expires: Now.AddDays(365)),
|
|
Csrf(csrf),
|
|
SiteCookie.ForHost("ds_user_id", "segreto-percorso-3", "www.instagram.com", "/api/", secure: true),
|
|
SiteCookie.ForHost("rur", "\"CLN\\054segreto\"", "www.instagram.com"),
|
|
],
|
|
"Mozilla/5.0 segreto-agente-9",
|
|
new Dictionary<string, string>
|
|
{
|
|
["lsd"] = "segreto-lsd-4", ["fb_dtsg"] = "segreto-dtsg-5", ["rev"] = "segreto-rev-6", ["entry_nonce"] = "segreto-nonce-7",
|
|
["claim"] = "segreto-claim-10", ["dpr"] = "2",
|
|
},
|
|
new Dictionary<string, string> { ["PolarisProfilePostsQuery"] = "segreto-docid-8" },
|
|
Now);
|
|
|
|
/// <summary>The csrftoken of Instagram as the browser keeps it (Domain=.instagram.com, secure): a renewal has the same key.</summary>
|
|
private static SiteCookie Csrf(string value) =>
|
|
new("csrftoken", value, ".instagram.com", "/", hostOnly: false, secure: true, expires: Now.AddDays(30));
|
|
|
|
private static SiteCookie Cookie(string name, string value) => SiteCookie.ForHost(name, value, "www.instagram.com");
|
|
|
|
private static string Describe(SiteCookie c) => $"{c.Name}|{c.Value}|{c.Domain}|{c.HostOnly}|{c.Path}|{c.Secure}|{c.HttpOnly}|{c.Expires:O}";
|
|
|
|
private static readonly ConcurrentDictionary<ISessionProtector, byte[]> Keys = new();
|
|
|
|
/// <summary>A protector with a key made now (never a key in the repository).</summary>
|
|
private static AesGcmSessionProtector Protector()
|
|
{
|
|
var key = RandomNumberGenerator.GetBytes(SessionKey.Length);
|
|
var protector = new AesGcmSessionProtector(key);
|
|
Keys[protector] = key;
|
|
return protector;
|
|
}
|
|
|
|
/// <summary>Another protector with the same key as <paramref name="flaky"/>'s.</summary>
|
|
private static AesGcmSessionProtector Protector(FlakyProtector flaky) => new(Keys[flaky.Inner]);
|
|
|
|
private static void AssertNoSecret(IEnumerable<string> texts)
|
|
{
|
|
foreach (var text in texts)
|
|
{
|
|
Assert.IsFalse(text.Contains("segreto", StringComparison.Ordinal), text);
|
|
}
|
|
}
|
|
|
|
/// <summary>A protector that can refuse to write (as a full or read-only disk would) or hold a write halfway.</summary>
|
|
private sealed class FlakyProtector(AesGcmSessionProtector inner) : ISessionProtector
|
|
{
|
|
public AesGcmSessionProtector Inner => inner;
|
|
|
|
public bool Fail { get; set; }
|
|
|
|
public ManualResetEventSlim? Hold { get; set; }
|
|
|
|
public ManualResetEventSlim Entered { get; } = new(false);
|
|
|
|
public string Scheme => inner.Scheme;
|
|
|
|
public byte[] Protect(ReadOnlySpan<byte> plain)
|
|
{
|
|
if (Hold is { } hold)
|
|
{
|
|
Hold = null;
|
|
Entered.Set();
|
|
hold.Wait(TimeSpan.FromSeconds(30));
|
|
}
|
|
|
|
return Fail ? throw new IOException("Disco pieno.") : inner.Protect(plain);
|
|
}
|
|
|
|
public byte[] Unprotect(ReadOnlySpan<byte> blob) => inner.Unprotect(blob);
|
|
}
|
|
}
|