Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
278 lines
14 KiB
C#
278 lines
14 KiB
C#
using System.Collections.Concurrent;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Nodes;
|
|
using Febe.Browser.Capture;
|
|
using Febe.Browser.Download;
|
|
using Febe.Browser.Hosting;
|
|
using Febe.Browser.RemoteView;
|
|
using Febe.Core.Collection;
|
|
using Febe.Tests.Browser.TestSupport;
|
|
|
|
namespace Febe.Tests.Browser;
|
|
|
|
[TestClass]
|
|
public sealed class CaptureUnitTests
|
|
{
|
|
private static readonly HostClassifier Hosts = new(["127.0.0.1:5000"], ["127.0.0.1:5001"]);
|
|
|
|
[TestMethod]
|
|
public async Task JsonBodiesReachConsumersWithASanitizedDescriptor()
|
|
{
|
|
var clock = new VirtualClock();
|
|
var cdp = new FakeCdpSession(clock);
|
|
cdp.Responder = (method, p) => method switch
|
|
{
|
|
"Page.getFrameTree" => new JsonObject { ["frameTree"] = new JsonObject { ["frame"] = new JsonObject { ["id"] = "F1", ["url"] = "about:blank" } } },
|
|
"Network.getResponseBody" when p!["requestId"]!.GetValue<string>() == "r1" => new JsonObject { ["body"] = """{"a":1}""", ["base64Encoded"] = false },
|
|
"Network.getResponseBody" => new JsonObject { ["body"] = Convert.ToBase64String(Encoding.UTF8.GetBytes("""{"b":[2]}""")), ["base64Encoded"] = true },
|
|
_ => null,
|
|
};
|
|
var consumer = new CollectingConsumer();
|
|
var observer = new CollectingObserver();
|
|
await using var tap = new ResponseTap(cdp, Hosts, clock);
|
|
tap.AddConsumer(consumer);
|
|
tap.AddObserver(observer);
|
|
await tap.StartAsync();
|
|
|
|
Assert.IsTrue(cdp.Sent.Any(c => c.Method == "Network.enable"));
|
|
cdp.Raise("Network.responseReceived", Received("r1", "http://127.0.0.1:5000/api/dati.json?token=segreto", 200, "application/json; charset=utf-8"));
|
|
cdp.Raise("Network.loadingFinished", new JsonObject { ["requestId"] = "r1", ["encodedDataLength"] = 7 });
|
|
cdp.Raise("Network.responseReceived", Received("r2", "http://127.0.0.1:5000/api/b", 200, "application/json"));
|
|
cdp.Raise("Network.loadingFinished", new JsonObject { ["requestId"] = "r2", ["encodedDataLength"] = 9 });
|
|
cdp.Raise("Network.responseReceived", Received("r3", "http://127.0.0.1:5001/foto.jpg", 200, "image/jpeg"));
|
|
cdp.Raise("Network.loadingFinished", new JsonObject { ["requestId"] = "r3", ["encodedDataLength"] = 10 });
|
|
cdp.Raise("Network.responseReceived", Received("r4", "http://127.0.0.1:5000/troppe", 429, "application/json"));
|
|
|
|
await WaitUntilAsync(() => consumer.Items.Count == 2);
|
|
var first = consumer.Items.Single(i => i.Descriptor.RequestId == "r1");
|
|
Assert.AreEqual("http://127.0.0.1:5000/api/dati.json", first.Descriptor.Url, "No query string.");
|
|
Assert.AreEqual(HostKind.Site, first.Descriptor.HostKind);
|
|
Assert.AreEqual("application/json", first.Descriptor.MimeType);
|
|
Assert.AreEqual("""{"a":1}""", first.Json);
|
|
Assert.AreEqual("""{"b":[2]}""", consumer.Items.Single(i => i.Descriptor.RequestId == "r2").Json, "Base64 bodies are decoded.");
|
|
|
|
Assert.AreEqual(4, observer.Responses.Count, "Observers see every response, bodies or not.");
|
|
Assert.AreEqual(HostKind.Cdn, observer.Responses.Single(r => r.RequestId == "r3").HostKind);
|
|
Assert.AreEqual(429, observer.Responses.Single(r => r.RequestId == "r4").Status);
|
|
Assert.AreEqual(2, cdp.Sent.Count(c => c.Method == "Network.getResponseBody"), "Only JSON bodies are fetched.");
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task MainFrameNavigationsAreReported()
|
|
{
|
|
var clock = new VirtualClock();
|
|
var cdp = new FakeCdpSession(clock);
|
|
cdp.Responder = (method, _) => method == "Page.getFrameTree"
|
|
? new JsonObject { ["frameTree"] = new JsonObject { ["frame"] = new JsonObject { ["id"] = "F1", ["url"] = "about:blank" } } }
|
|
: null;
|
|
var observer = new CollectingObserver();
|
|
await using var tap = new ResponseTap(cdp, Hosts, clock);
|
|
tap.AddObserver(observer);
|
|
await tap.StartAsync();
|
|
|
|
cdp.Raise("Page.frameNavigated", new JsonObject { ["frame"] = new JsonObject { ["id"] = "F1", ["url"] = "http://127.0.0.1:5000/verifica?da=x" } });
|
|
cdp.Raise("Page.frameNavigated", new JsonObject { ["frame"] = new JsonObject { ["id"] = "F2", ["parentId"] = "F1", ["url"] = "http://127.0.0.1:5000/iframe" } });
|
|
|
|
var navigation = observer.Navigations.Single();
|
|
Assert.AreEqual("/verifica", navigation.Path);
|
|
Assert.AreEqual("http://127.0.0.1:5000/verifica", navigation.Url);
|
|
Assert.IsFalse(navigation.SameDocument);
|
|
|
|
// A single-page route change (history.pushState) is a navigation too.
|
|
cdp.Raise("Page.navigatedWithinDocument", new JsonObject { ["frameId"] = "F1", ["url"] = "http://127.0.0.1:5000/accesso/?next=x" });
|
|
cdp.Raise("Page.navigatedWithinDocument", new JsonObject { ["frameId"] = "F2", ["url"] = "http://127.0.0.1:5000/iframe-route" });
|
|
Assert.AreEqual(2, observer.Navigations.Count);
|
|
var route = observer.Navigations.Last();
|
|
Assert.IsTrue(route.SameDocument);
|
|
Assert.AreEqual("/accesso/", route.Path);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task BrowserFetcherStreamsChunksAndClosesTheHandle()
|
|
{
|
|
var payload = RandomNumberGenerator.GetBytes(300_000);
|
|
var offset = 0;
|
|
var cdp = new FakeCdpSession(new VirtualClock());
|
|
cdp.Responder = (method, p) =>
|
|
{
|
|
switch (method)
|
|
{
|
|
case "Page.getFrameTree":
|
|
return new JsonObject { ["frameTree"] = new JsonObject { ["frame"] = new JsonObject { ["id"] = "F1", ["url"] = "http://127.0.0.1:5000/" } } };
|
|
case "Network.loadNetworkResource":
|
|
Assert.AreEqual("F1", p!["frameId"]!.GetValue<string>());
|
|
return new JsonObject { ["resource"] = new JsonObject { ["success"] = true, ["httpStatusCode"] = 200, ["stream"] = "S1" } };
|
|
case "IO.read":
|
|
var size = p!["size"]!.GetValue<int>();
|
|
var take = Math.Min(size, payload.Length - offset);
|
|
var chunk = Convert.ToBase64String(payload, offset, take);
|
|
offset += take;
|
|
return new JsonObject { ["base64Encoded"] = true, ["data"] = chunk, ["eof"] = offset >= payload.Length };
|
|
default:
|
|
return null;
|
|
}
|
|
};
|
|
|
|
var fetcher = new BrowserFileFetcher(cdp, Hosts, options: new BrowserFetchOptions { ChunkBytes = 64 * 1024 });
|
|
using var destination = new MemoryStream();
|
|
var progress = new List<long>();
|
|
var result = await fetcher.FetchAsync(new Uri("http://127.0.0.1:5001/f.bin"), destination, new FetchOptions(Progress: new SyncProgress(progress)), CancellationToken.None);
|
|
|
|
Assert.AreEqual(FetchOutcome.Completed, result.Outcome);
|
|
Assert.AreEqual(payload.Length, result.Bytes);
|
|
CollectionAssert.AreEqual(payload, destination.ToArray());
|
|
Assert.AreEqual(5, cdp.Sent.Count(c => c.Method == "IO.read"));
|
|
Assert.AreEqual(1, cdp.Sent.Count(c => c.Method == "IO.close"));
|
|
Assert.AreEqual(payload.Length, progress[^1]);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task BrowserFetcherRefusesTheSiteAndStopsAtTheSizeLimit()
|
|
{
|
|
var cdp = new FakeCdpSession(new VirtualClock());
|
|
cdp.Responder = (method, _) => method switch
|
|
{
|
|
"Page.getFrameTree" => new JsonObject { ["frameTree"] = new JsonObject { ["frame"] = new JsonObject { ["id"] = "F1" } } },
|
|
"Network.loadNetworkResource" => new JsonObject { ["resource"] = new JsonObject { ["success"] = true, ["httpStatusCode"] = 200, ["stream"] = "S1" } },
|
|
"IO.read" => new JsonObject { ["data"] = Convert.ToBase64String(new byte[10_000]), ["base64Encoded"] = true, ["eof"] = false },
|
|
_ => null,
|
|
};
|
|
var fetcher = new BrowserFileFetcher(cdp, Hosts);
|
|
|
|
var refused = await fetcher.FetchAsync(new Uri("http://127.0.0.1:5000/api"), Stream.Null, null, CancellationToken.None);
|
|
Assert.AreEqual(FetchOutcome.HostNotAllowed, refused.Outcome);
|
|
Assert.AreEqual(0, cdp.Sent.Count, "Nothing is sent for an address outside the CDN.");
|
|
|
|
using var destination = new MemoryStream();
|
|
var tooLarge = await fetcher.FetchAsync(new Uri("http://127.0.0.1:5001/big"), destination, new FetchOptions(MaxBytes: 25_000), CancellationToken.None);
|
|
Assert.AreEqual(FetchOutcome.TooLarge, tooLarge.Outcome);
|
|
Assert.AreEqual(25_000, destination.Length);
|
|
Assert.AreEqual(1, cdp.Sent.Count(c => c.Method == "IO.close"), "The stream is closed even when stopping early.");
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task BrowserFetcherReportsHttpErrors()
|
|
{
|
|
var cdp = new FakeCdpSession(new VirtualClock());
|
|
cdp.Responder = (method, _) => method switch
|
|
{
|
|
"Page.getFrameTree" => new JsonObject { ["frameTree"] = new JsonObject { ["frame"] = new JsonObject { ["id"] = "F1" } } },
|
|
"Network.loadNetworkResource" => new JsonObject { ["resource"] = new JsonObject { ["success"] = false, ["httpStatusCode"] = 403, ["netErrorName"] = "net::ERR_HTTP_RESPONSE_CODE_FAILURE" } },
|
|
_ => null,
|
|
};
|
|
var result = await new BrowserFileFetcher(cdp, Hosts).FetchAsync(new Uri("http://127.0.0.1:5001/scaduto.jpg"), Stream.Null, null, CancellationToken.None);
|
|
Assert.AreEqual(FetchOutcome.HttpError, result.Outcome);
|
|
Assert.AreEqual(403, result.HttpStatus);
|
|
Assert.IsFalse(result.Error!.Contains("5001", StringComparison.Ordinal), "Errors never carry the address.");
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task ScreencastAcknowledgesEveryFrameAndDeliversIt()
|
|
{
|
|
var cdp = new FakeCdpSession(new VirtualClock());
|
|
await using var screencast = new ScreencastSession(cdp);
|
|
await screencast.StartAsync(new ScreencastOptions(Quality: 50, MaxWidth: 800, MaxHeight: 600, EveryNthFrame: 2));
|
|
|
|
var start = cdp.Sent.Single(c => c.Method == "Page.startScreencast").Parameters!;
|
|
Assert.AreEqual("jpeg", start["format"]!.GetValue<string>());
|
|
Assert.AreEqual(2, start["everyNthFrame"]!.GetValue<int>());
|
|
|
|
var jpeg = new byte[] { 0xFF, 0xD8, 0xFF, 0xD9 };
|
|
cdp.Raise("Page.screencastFrame", new JsonObject
|
|
{
|
|
["sessionId"] = 7,
|
|
["data"] = Convert.ToBase64String(jpeg),
|
|
["metadata"] = new JsonObject { ["deviceWidth"] = 1280, ["deviceHeight"] = 900, ["pageScaleFactor"] = 1, ["offsetTop"] = 0, ["scrollOffsetX"] = 0, ["scrollOffsetY"] = 120 },
|
|
});
|
|
|
|
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
|
|
await using var frames = screencast.ReadFramesAsync(cts.Token).GetAsyncEnumerator(cts.Token);
|
|
Assert.IsTrue(await frames.MoveNextAsync());
|
|
CollectionAssert.AreEqual(jpeg, frames.Current.Jpeg);
|
|
Assert.AreEqual(120, frames.Current.ScrollOffsetY);
|
|
await WaitUntilAsync(() => cdp.Sent.Any(c => c.Method == "Page.screencastFrameAck" && c.Parameters!["sessionId"]!.GetValue<int>() == 7));
|
|
|
|
await screencast.StopAsync();
|
|
Assert.AreEqual(1, cdp.Sent.Count(c => c.Method == "Page.stopScreencast"));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task RemoteViewHoldsTheDrivingLeaseAndMapsCoordinates()
|
|
{
|
|
var cdp = new FakeCdpSession(new VirtualClock());
|
|
var gate = new UserDrivingGate();
|
|
await using var view = new RemoteViewSession(cdp, gate);
|
|
await Assert.ThrowsExactlyAsync<InvalidOperationException>(() => view.MouseAsync(new RemoteMouseEvent(RemoteMouseAction.Move, 0.5, 0.5)));
|
|
|
|
await view.StartAsync();
|
|
Assert.IsTrue(gate.IsDriving, "The engine stays still while Alberto uses the remote view.");
|
|
cdp.Raise("Page.screencastFrame", new JsonObject
|
|
{
|
|
["sessionId"] = 1,
|
|
["data"] = Convert.ToBase64String([1, 2, 3]),
|
|
["metadata"] = new JsonObject { ["deviceWidth"] = 1000, ["deviceHeight"] = 800, ["pageScaleFactor"] = 1 },
|
|
});
|
|
|
|
await view.MouseAsync(new RemoteMouseEvent(RemoteMouseAction.Down, 0.25, 0.5, RemoteMouseButton.Left));
|
|
var press = cdp.Mouse("mousePressed").Single();
|
|
Assert.AreEqual(250, press.X);
|
|
Assert.AreEqual(400, press.Y);
|
|
Assert.AreEqual(1, press.Parameters!["buttons"]!.GetValue<int>());
|
|
|
|
await view.KeyAsync(new RemoteKeyEvent(true, Febe.Browser.Gestures.Keyboard.ForText("a")));
|
|
Assert.AreEqual("keyDown", cdp.Sent.Last(c => c.Method == "Input.dispatchKeyEvent").Type);
|
|
Assert.AreEqual("RemoteKeyEvent(…)", new RemoteKeyEvent(true, Febe.Browser.Gestures.Keyboard.ForText("p")).ToString());
|
|
|
|
await view.StopAsync();
|
|
Assert.IsFalse(gate.IsDriving);
|
|
}
|
|
|
|
private static JsonObject Received(string id, string url, int status, string mime) => new()
|
|
{
|
|
["requestId"] = id,
|
|
["loaderId"] = "L1",
|
|
["frameId"] = "F1",
|
|
["type"] = "Fetch",
|
|
["response"] = new JsonObject { ["url"] = url, ["status"] = status, ["mimeType"] = mime },
|
|
};
|
|
|
|
private static async Task WaitUntilAsync(Func<bool> condition)
|
|
{
|
|
for (var i = 0; i < 300 && !condition(); i++)
|
|
{
|
|
await Task.Delay(10);
|
|
}
|
|
|
|
Assert.IsTrue(condition(), "Condition not reached in time.");
|
|
}
|
|
|
|
private sealed class CollectingConsumer : IResponseConsumer
|
|
{
|
|
public ConcurrentBag<(ResponseDescriptor Descriptor, string Json)> Items { get; } = [];
|
|
|
|
public ValueTask ConsumeAsync(ResponseDescriptor response, JsonDocument body, CancellationToken ct)
|
|
{
|
|
Items.Add((response, body.RootElement.GetRawText()));
|
|
return ValueTask.CompletedTask;
|
|
}
|
|
}
|
|
|
|
private sealed class CollectingObserver : IResponseObserver
|
|
{
|
|
public ConcurrentQueue<ResponseDescriptor> Responses { get; } = new();
|
|
|
|
public ConcurrentQueue<NavigationDescriptor> Navigations { get; } = new();
|
|
|
|
public void OnResponse(ResponseDescriptor response) => Responses.Enqueue(response);
|
|
|
|
public void OnNavigation(NavigationDescriptor navigation) => Navigations.Enqueue(navigation);
|
|
}
|
|
|
|
private sealed class SyncProgress(List<long> values) : IProgress<long>
|
|
{
|
|
public void Report(long value) => values.Add(value);
|
|
}
|
|
}
|