feat(calls): add consent-based signaling to existing ChatHub
Wire shared PostIt call signaling, account-derived relay policy and temporary TURN credentials. Keep calls disabled pending native Android/Linux media integration. Document experimental Android crash and laboratory removal. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
56ca115f50
commit
5e48831d6d
20 changed files with 1368 additions and 16 deletions
39
src/test/Yavsc.Server.Tests/CallConnectionPresenceTests.cs
Normal file
39
src/test/Yavsc.Server.Tests/CallConnectionPresenceTests.cs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Yavsc.Models;
|
||||
using Yavsc.Services;
|
||||
|
||||
namespace Yavsc.Server.Tests;
|
||||
|
||||
public sealed class CallConnectionPresenceTests
|
||||
{
|
||||
[Fact]
|
||||
public void Disconnecting_an_unselected_device_preserves_the_accounts_chat_presence()
|
||||
{
|
||||
using var services = new ServiceCollection().AddLogging().AddLocalization()
|
||||
.AddDbContext<ApplicationDbContext>(o => o.UseInMemoryDatabase(Guid.NewGuid().ToString()))
|
||||
.BuildServiceProvider();
|
||||
var manager = new HubConnectionManager(services.GetRequiredService<IServiceScopeFactory>());
|
||||
var user = "call-" + Guid.NewGuid().ToString("N");
|
||||
var room = "call-" + Guid.NewGuid().ToString("N");
|
||||
var selected = Guid.NewGuid().ToString("N");
|
||||
var other = Guid.NewGuid().ToString("N");
|
||||
manager.OnConnected(selected, user, false);
|
||||
manager.OnConnected(other, user, false);
|
||||
manager.Join(room, selected);
|
||||
try
|
||||
{
|
||||
var snapshot = manager.GetConnexionIds(user);
|
||||
manager.OnDisconnected(other);
|
||||
Assert.True(manager.IsConnected(user));
|
||||
Assert.True(manager.IsPresent(room, user));
|
||||
Assert.Equal([selected], manager.GetConnexionIds(user));
|
||||
Assert.Equal(2, snapshot.Count());
|
||||
}
|
||||
finally
|
||||
{
|
||||
manager.OnDisconnected(selected);
|
||||
HubConnectionManager.Channels.TryRemove(room, out _);
|
||||
}
|
||||
}
|
||||
}
|
||||
162
src/test/Yavsc.Server.Tests/CallHubTests.cs
Normal file
162
src/test/Yavsc.Server.Tests/CallHubTests.cs
Normal file
|
|
@ -0,0 +1,162 @@
|
|||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Http.Features;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Yavsc.Abstract.Chat.Calls;
|
||||
using Yavsc.Models;
|
||||
using Yavsc.Server.Hubs;
|
||||
using Yavsc.Services;
|
||||
using Yavsc.Settings;
|
||||
using Yavsc.ViewModels.Chat;
|
||||
|
||||
namespace Yavsc.Server.Tests;
|
||||
|
||||
public sealed class CallHubTests
|
||||
{
|
||||
private sealed class Fixture : IDisposable
|
||||
{
|
||||
public ApplicationDbContext Db { get; } = new(
|
||||
new DbContextOptionsBuilder<ApplicationDbContext>()
|
||||
.UseInMemoryDatabase(Guid.NewGuid().ToString()).Options);
|
||||
public RecordingCallHub Delivery { get; } = new();
|
||||
public Connections Connections { get; } = new();
|
||||
public CallSignalingService Calls { get; }
|
||||
private readonly ServiceProvider _services;
|
||||
|
||||
public Fixture()
|
||||
{
|
||||
_services = new ServiceCollection().AddLogging().AddLocalization().BuildServiceProvider();
|
||||
Calls = new(Delivery, Options.Create(new AudioVideoCallOptions
|
||||
{
|
||||
Enabled = true, TurnUrls = ["turn:turn.example.test:3478"], TurnSharedSecret = "test-only"
|
||||
}), TimeProvider.System, NullLogger<CallSignalingService>.Instance);
|
||||
Db.Users.AddRange(
|
||||
new ApplicationUser { Id = "alice-id", UserName = "alice", Email = "alice@example.test" },
|
||||
new ApplicationUser { Id = "bob-id", UserName = "bob", Email = "bob@example.test" });
|
||||
Db.SaveChanges();
|
||||
Connections.OnConnected("alice-1", "alice", false);
|
||||
Connections.OnConnected("bob-1", "bob", false);
|
||||
}
|
||||
|
||||
public ChatHub Hub(string user, string connection) => new(Db,
|
||||
_services.GetRequiredService<ILoggerFactory>(),
|
||||
_services.GetRequiredService<Microsoft.Extensions.Localization.IStringLocalizerFactory>(),
|
||||
Connections, Calls) { Context = new Caller(user, connection) };
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Calls.Dispose();
|
||||
Db.Dispose();
|
||||
_services.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Invitation_requires_existing_accounts_presence_and_the_authenticated_connection()
|
||||
{
|
||||
using var f = new Fixture();
|
||||
using var hub = f.Hub("alice", "alice-1");
|
||||
f.Connections.Present.Remove("bob");
|
||||
await Assert.ThrowsAsync<HubException>(() => hub.InviteCall("General", "bob"));
|
||||
f.Connections.Present.Add("bob");
|
||||
using var spoof = f.Hub("bob", "alice-1");
|
||||
await Assert.ThrowsAsync<HubException>(() => spoof.InviteCall("General", "alice"));
|
||||
await Assert.ThrowsAsync<HubException>(() => hub.InviteCall("General", "alice"));
|
||||
Assert.Empty(f.Delivery.Messages);
|
||||
f.Db.Users.Remove(await f.Db.Users.SingleAsync(u => u.Id == "bob-id", TestContext.Current.CancellationToken));
|
||||
await f.Db.SaveChangesAsync(TestContext.Current.CancellationToken);
|
||||
await Assert.ThrowsAsync<HubException>(() => hub.InviteCall("General", "bob"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Persistent_preference_is_read_at_invitation_and_rechecked_at_acceptance()
|
||||
{
|
||||
using var f = new Fixture();
|
||||
using var caller = f.Hub("alice", "alice-1");
|
||||
using var callee = f.Hub("bob", "bob-1");
|
||||
var call = await caller.InviteCall("General", "bob");
|
||||
Assert.False(call.RelayOnly);
|
||||
var bob = await f.Db.Users.SingleAsync(u => u.Id == "bob-id", TestContext.Current.CancellationToken);
|
||||
bob.HideIpAddressInCalls = true;
|
||||
await f.Db.SaveChangesAsync(TestContext.Current.CancellationToken);
|
||||
await callee.AcceptCall(call.Id);
|
||||
Assert.True(Assert.IsType<CallAccepted>(f.Delivery.Messages.Last().Arguments[0]).Session.RelayOnly);
|
||||
await caller.EndCall(call.Id);
|
||||
Assert.True((await caller.InviteCall("General", "bob")).RelayOnly);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public async Task Blacklisting_either_participant_prevents_invitations(bool reverse)
|
||||
{
|
||||
using var f = new Fixture();
|
||||
f.Db.BlackListed.Add(new()
|
||||
{
|
||||
OwnerId = reverse ? "alice-id" : "bob-id",
|
||||
UserId = reverse ? "bob-id" : "alice-id"
|
||||
});
|
||||
await f.Db.SaveChangesAsync(TestContext.Current.CancellationToken);
|
||||
using var hub = f.Hub("alice", "alice-1");
|
||||
await Assert.ThrowsAsync<HubException>(() => hub.InviteCall("General", "bob"));
|
||||
Assert.Empty(f.Delivery.Messages);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Leaving_the_room_prevents_acceptance_and_privacy_change_prevents_direct_signaling()
|
||||
{
|
||||
using var f = new Fixture();
|
||||
using var caller = f.Hub("alice", "alice-1");
|
||||
using var callee = f.Hub("bob", "bob-1");
|
||||
var call = await caller.InviteCall("General", "bob");
|
||||
f.Connections.Present.Remove("bob");
|
||||
await Assert.ThrowsAsync<HubException>(() => callee.AcceptCall(call.Id));
|
||||
f.Connections.Present.Add("bob");
|
||||
await callee.AcceptCall(call.Id);
|
||||
var bob = await f.Db.Users.SingleAsync(u => u.Id == "bob-id", TestContext.Current.CancellationToken);
|
||||
bob.HideIpAddressInCalls = true;
|
||||
await f.Db.SaveChangesAsync(TestContext.Current.CancellationToken);
|
||||
await Assert.ThrowsAsync<HubException>(() => caller.SendCallSignal(call.Id,
|
||||
new("offer", "v=0\r\nm=audio 9 UDP/TLS/RTP/SAVPF 111\r\n")));
|
||||
Assert.DoesNotContain(f.Delivery.Messages, m => m.Method == CallHubMethods.SignalReceived);
|
||||
Assert.Equal("privacy-changed", Assert.IsType<CallEnded>(f.Delivery.Messages.Last().Arguments[0]).Reason);
|
||||
}
|
||||
|
||||
private sealed class Caller(string name, string connection) : HubCallerContext
|
||||
{
|
||||
public override string ConnectionId => connection;
|
||||
public override string? UserIdentifier => name;
|
||||
public override ClaimsPrincipal User { get; } = new(new ClaimsIdentity(
|
||||
[new Claim("name", name)], "test"));
|
||||
public override IDictionary<object, object?> Items { get; } = new Dictionary<object, object?>();
|
||||
public override IFeatureCollection Features { get; } = new FeatureCollection();
|
||||
public override CancellationToken ConnectionAborted => CancellationToken.None;
|
||||
public override void Abort() { }
|
||||
}
|
||||
|
||||
private sealed class Connections : IConnexionManager
|
||||
{
|
||||
public HashSet<string> Present { get; } = ["alice", "bob"];
|
||||
private readonly Dictionary<string, string> _names = [];
|
||||
public string GetUserName(string cxId) => _names.GetValueOrDefault(cxId)!;
|
||||
public bool IsConnected(string user) => _names.ContainsValue(user);
|
||||
public bool IsPresent(string room, string user) => room == "General" && Present.Contains(user);
|
||||
public IEnumerable<string> GetConnexionIds(string user) => _names.Where(n => n.Value == user).Select(n => n.Key);
|
||||
public void OnConnected(string id, string user, bool isCop) => _names[id] = user;
|
||||
public void OnDisconnected(string id) => _names.Remove(id);
|
||||
public void SetErrorHandler(Action<string, string> errorHandler) { }
|
||||
public ChatRoomInfo Join(string room, string id) => throw new NotSupportedException();
|
||||
public bool Part(string id, string room, string reason) => throw new NotSupportedException();
|
||||
public bool Kick(string id, string user, string room, string reason) => throw new NotSupportedException();
|
||||
public bool Op(string room, string user) => throw new NotSupportedException();
|
||||
public bool DeOp(string room, string user) => throw new NotSupportedException();
|
||||
public bool Hop(string room, string user) => throw new NotSupportedException();
|
||||
public bool DeHop(string room, string user) => throw new NotSupportedException();
|
||||
public bool TryGetChanInfo(string room, out ChatRoomInfo info) => throw new NotSupportedException();
|
||||
public IEnumerable<ChannelShortInfo> ListChannels(string pattern) => throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
313
src/test/Yavsc.Server.Tests/CallSignalingTests.cs
Normal file
313
src/test/Yavsc.Server.Tests/CallSignalingTests.cs
Normal file
|
|
@ -0,0 +1,313 @@
|
|||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Yavsc.Abstract.Chat.Calls;
|
||||
using Yavsc.Server.Hubs;
|
||||
using Yavsc.Services;
|
||||
using Yavsc.Settings;
|
||||
|
||||
namespace Yavsc.Server.Tests;
|
||||
|
||||
public sealed class CallSignalingTests
|
||||
{
|
||||
private const string Sdp = "v=0\r\no=- 1 1 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\nm=audio 9 UDP/TLS/RTP/SAVPF 111\r\nc=IN IP4 0.0.0.0\r\n";
|
||||
private static CallSignal Candidate(string type = "relay") =>
|
||||
new("candidate", Candidate: $"candidate:1 1 udp 100 192.0.2.1 5000 typ {type} raddr 0.0.0.0 rport 0",
|
||||
SdpMid: "0", SdpMLineIndex: 0);
|
||||
|
||||
private sealed class Clock : TimeProvider
|
||||
{
|
||||
public DateTimeOffset Now { get; set; } = DateTimeOffset.Parse("2026-10-26T00:00:00Z");
|
||||
public override DateTimeOffset GetUtcNow() => Now;
|
||||
}
|
||||
|
||||
private sealed class Fixture : IDisposable
|
||||
{
|
||||
public RecordingCallHub Hub { get; } = new();
|
||||
public Clock Clock { get; } = new();
|
||||
public AudioVideoCallOptions Options { get; } = new()
|
||||
{
|
||||
Enabled = true,
|
||||
StunUrls = ["stun:stun.example.test:3478"],
|
||||
TurnUrls = ["turn:turn.example.test:3478?transport=udp", "turns:turn.example.test:5349?transport=tcp"],
|
||||
TurnSharedSecret = "test-only-secret"
|
||||
};
|
||||
public CallSignalingService Service { get; }
|
||||
public Fixture()
|
||||
{
|
||||
Service = new(Hub, Microsoft.Extensions.Options.Options.Create(Options), Clock,
|
||||
NullLogger<CallSignalingService>.Instance);
|
||||
}
|
||||
public Task<CallSession> Invite(bool relay = false) =>
|
||||
Service.InviteAsync("General", "alice", "bob", "alice-device", ["bob-1", "bob-2"], relay);
|
||||
public void Dispose() => Service.Dispose();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Calls_are_disabled_by_default()
|
||||
{
|
||||
using var f = new Fixture();
|
||||
f.Options.Enabled = new AudioVideoCallOptions().Enabled;
|
||||
await Assert.ThrowsAsync<HubException>(() => f.Invite());
|
||||
Assert.Empty(f.Hub.Messages);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Invitation_does_not_grant_media_signaling_or_disclose_turn_credentials()
|
||||
{
|
||||
using var f = new Fixture();
|
||||
var call = await f.Invite();
|
||||
Assert.Equal(f.Clock.Now.AddSeconds(45), call.ExpiresAt);
|
||||
var message = Assert.Single(f.Hub.Messages);
|
||||
Assert.Equal(CallHubMethods.Invitation, message.Method);
|
||||
Assert.Equal(["bob-1", "bob-2"], message.Targets);
|
||||
Assert.IsType<CallSession>(Assert.Single(message.Arguments));
|
||||
await Assert.ThrowsAsync<HubException>(() =>
|
||||
f.Service.SignalAsync(call.Id, "alice", "alice-device", new("offer", Sdp)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task First_acceptance_wins_and_other_devices_cannot_signal_or_end_the_call()
|
||||
{
|
||||
using var f = new Fixture();
|
||||
var call = await f.Invite();
|
||||
await f.Service.AcceptAsync(call.Id, "bob", "bob-1", false);
|
||||
await Assert.ThrowsAsync<HubException>(() => f.Service.AcceptAsync(call.Id, "bob", "bob-2", false));
|
||||
await Assert.ThrowsAsync<HubException>(() => f.Service.SignalAsync(call.Id, "bob", "bob-2", Candidate()));
|
||||
await Assert.ThrowsAsync<HubException>(() => f.Service.EndAsync(call.Id, "bob", "bob-2"));
|
||||
var dismissed = Assert.Single(f.Hub.Messages, m => m.Method == CallHubMethods.Ended);
|
||||
Assert.Equal(["bob-2"], dismissed.Targets);
|
||||
Assert.Equal("answered-elsewhere", Assert.IsType<CallEnded>(dismissed.Arguments[0]).Reason);
|
||||
var ready = Assert.Single(f.Hub.Messages, m => m.Method == CallHubMethods.Accepted);
|
||||
Assert.Equal(["alice-device", "bob-1"], ready.Targets);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Concurrent_acceptance_selects_exactly_one_device()
|
||||
{
|
||||
using var f = new Fixture();
|
||||
var call = await f.Invite();
|
||||
var attempts = new[] { "bob-1", "bob-2" }.Select(async connection =>
|
||||
await Record.ExceptionAsync(() => f.Service.AcceptAsync(call.Id, "bob", connection, false)));
|
||||
var results = await Task.WhenAll(attempts);
|
||||
Assert.Single(results, r => r == null);
|
||||
Assert.Single(results, r => r is HubException);
|
||||
Assert.Single(f.Hub.Messages, m => m.Method == CallHubMethods.Accepted);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Privacy_can_only_be_strengthened_on_acceptance_and_turn_credentials_expire()
|
||||
{
|
||||
using var f = new Fixture();
|
||||
var call = await f.Invite();
|
||||
await f.Service.AcceptAsync(call.Id, "bob", "bob-1", true);
|
||||
var ready = Assert.IsType<CallAccepted>(
|
||||
f.Hub.Messages.Single(m => m.Method == CallHubMethods.Accepted).Arguments[0]);
|
||||
Assert.True(ready.Session.RelayOnly);
|
||||
var ice = Assert.Single(ready.IceServers);
|
||||
Assert.Equal(f.Options.TurnUrls, ice.Urls);
|
||||
Assert.NotNull(ice.Username);
|
||||
Assert.StartsWith($"{ready.Session.ExpiresAt.AddMinutes(5).ToUnixTimeSeconds()}:", ice.Username);
|
||||
Assert.Equal(Convert.ToBase64String(HMACSHA1.HashData(
|
||||
Encoding.UTF8.GetBytes(f.Options.TurnSharedSecret!), Encoding.UTF8.GetBytes(ice.Username))), ice.Credential);
|
||||
await f.Service.SignalAsync(call.Id, "alice", "alice-device", new("offer", Sdp));
|
||||
await Assert.ThrowsAsync<HubException>(() =>
|
||||
f.Service.SignalAsync(call.Id, "alice", "alice-device", Candidate("host")));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Relay_invitation_cannot_be_downgraded_and_missing_turn_fails_closed()
|
||||
{
|
||||
using var f = new Fixture();
|
||||
f.Options.TurnUrls = [];
|
||||
await Assert.ThrowsAsync<HubException>(() => f.Invite(true));
|
||||
Assert.Empty(f.Hub.Messages);
|
||||
f.Options.TurnUrls = ["turn:turn.example.test:3478"];
|
||||
var call = await f.Invite(true);
|
||||
await f.Service.AcceptAsync(call.Id, "bob", "bob-1", false);
|
||||
Assert.True(Assert.IsType<CallAccepted>(f.Hub.Messages.Last().Arguments[0]).Session.RelayOnly);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("https://turn.example.test")]
|
||||
[InlineData("turn:")]
|
||||
[InlineData("turn://turn.example.test")]
|
||||
[InlineData("turn:user:password@turn.example.test")]
|
||||
[InlineData("turn:turn.example.test:0")]
|
||||
[InlineData("turn:turn.example.test?transport=invalid")]
|
||||
[InlineData("turn:turn.example.test#fragment")]
|
||||
public async Task Invalid_turn_configuration_is_rejected_before_inviting(string url)
|
||||
{
|
||||
using var f = new Fixture();
|
||||
f.Options.TurnUrls = [url];
|
||||
await Assert.ThrowsAsync<HubException>(() => f.Invite(true));
|
||||
Assert.Empty(f.Hub.Messages);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Failed_delivery_does_not_leave_participants_busy()
|
||||
{
|
||||
using var f = new Fixture();
|
||||
f.Hub.FailMethod = CallHubMethods.Invitation;
|
||||
await Assert.ThrowsAsync<IOException>(() => f.Invite());
|
||||
f.Hub.FailMethod = null;
|
||||
var call = await f.Invite();
|
||||
f.Hub.FailMethod = CallHubMethods.Accepted;
|
||||
await Assert.ThrowsAsync<IOException>(() => f.Service.AcceptAsync(call.Id, "bob", "bob-1", false));
|
||||
f.Hub.FailMethod = null;
|
||||
Assert.NotEqual(call.Id, (await f.Invite()).Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Signaling_is_bound_to_identity_device_consent_and_initial_offer_order()
|
||||
{
|
||||
using var f = new Fixture();
|
||||
var call = await f.Invite();
|
||||
await Assert.ThrowsAsync<HubException>(() => f.Service.AcceptAsync(call.Id, "mallory", "bob-1", false));
|
||||
await Assert.ThrowsAsync<HubException>(() => f.Service.AcceptAsync(call.Id, "bob", "uninvited", false));
|
||||
await f.Service.AcceptAsync(call.Id, "bob", "bob-1", false);
|
||||
await Assert.ThrowsAsync<HubException>(() => f.Service.SignalAsync(call.Id, "bob", "bob-1", new("offer", Sdp)));
|
||||
await Assert.ThrowsAsync<HubException>(() => f.Service.SignalAsync(call.Id, "bob", "bob-1", new("answer", Sdp)));
|
||||
await Assert.ThrowsAsync<HubException>(() => f.Service.SignalAsync(call.Id, "alice", "other-device", new("offer", Sdp)));
|
||||
await Assert.ThrowsAsync<HubException>(() => f.Service.SignalAsync(call.Id, "mallory", "alice-device", new("offer", Sdp)));
|
||||
await f.Service.SignalAsync(call.Id, "alice", "alice-device", new("offer", Sdp));
|
||||
await f.Service.SignalAsync(call.Id, "alice", "alice-device", Candidate("host"));
|
||||
await f.Service.SignalAsync(call.Id, "bob", "bob-1", new("answer", Sdp));
|
||||
await f.Service.SignalAsync(call.Id, "bob", "bob-1", Candidate("host"));
|
||||
var signals = f.Hub.Messages.Where(m => m.Method == CallHubMethods.SignalReceived).ToArray();
|
||||
Assert.Equal(4, signals.Length);
|
||||
Assert.Equal(["bob-1"], signals[0].Targets);
|
||||
Assert.Equal(["alice-device"], signals[2].Targets);
|
||||
Assert.All(signals, m => Assert.Equal(call.Id, m.Arguments[0]));
|
||||
await Assert.ThrowsAsync<HubException>(() => f.Service.SignalAsync(call.Id, "alice", "alice-device", new("offer", Sdp)));
|
||||
await Assert.ThrowsAsync<HubException>(() => f.Service.SignalAsync(call.Id, "bob", "bob-1", new("answer", Sdp)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Busy_users_cannot_be_invited_again_and_expiry_releases_them()
|
||||
{
|
||||
using var f = new Fixture();
|
||||
var call = await f.Invite();
|
||||
await Assert.ThrowsAsync<HubException>(() => f.Invite());
|
||||
await Assert.ThrowsAsync<HubException>(() =>
|
||||
f.Service.InviteAsync("Other", "carol", "bob", "carol-1", ["bob-1"], false));
|
||||
f.Clock.Now = call.ExpiresAt;
|
||||
await f.Service.ExpireAsync();
|
||||
Assert.Equal("expired", Assert.IsType<CallEnded>(f.Hub.Messages.Last().Arguments[0]).Reason);
|
||||
await Assert.ThrowsAsync<HubException>(() => f.Service.AcceptAsync(call.Id, "bob", "bob-1", false));
|
||||
Assert.NotEqual(call.Id, (await f.Invite()).Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Unselected_device_disconnect_does_not_end_an_accepted_call()
|
||||
{
|
||||
using var f = new Fixture();
|
||||
var call = await f.Invite();
|
||||
await f.Service.AcceptAsync(call.Id, "bob", "bob-1", false);
|
||||
await f.Service.DisconnectAsync("bob-2");
|
||||
await f.Service.SignalAsync(call.Id, "alice", "alice-device", new("offer", Sdp));
|
||||
await f.Service.DisconnectAsync("bob-1");
|
||||
Assert.Equal("disconnected", Assert.IsType<CallEnded>(f.Hub.Messages.Last().Arguments[0]).Reason);
|
||||
await Assert.ThrowsAsync<HubException>(() =>
|
||||
f.Service.SignalAsync(call.Id, "alice", "alice-device", Candidate()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Decline_hangup_room_departure_and_profile_change_release_the_call()
|
||||
{
|
||||
using var f = new Fixture();
|
||||
var call = await f.Invite();
|
||||
await Assert.ThrowsAsync<HubException>(() => f.Service.EndAsync(call.Id, "mallory", "mallory-1"));
|
||||
await f.Service.EndAsync(call.Id, "bob", "bob-2");
|
||||
Assert.Equal("cancelled-or-declined", Assert.IsType<CallEnded>(f.Hub.Messages.Last().Arguments[0]).Reason);
|
||||
call = await f.Invite();
|
||||
await f.Service.AcceptAsync(call.Id, "bob", "bob-1", false);
|
||||
await f.Service.EndAsync(call.Id, "alice", "alice-device");
|
||||
Assert.Equal("hangup", Assert.IsType<CallEnded>(f.Hub.Messages.Last().Arguments[0]).Reason);
|
||||
call = await f.Invite();
|
||||
await f.Service.EndForUserAsync("bob", "left-room", "Other");
|
||||
Assert.Equal(call.Id, (await f.Service.GetSessionAsync(call.Id, "alice", "alice-device")).Id);
|
||||
await f.Service.EndForUserAsync("bob", "left-room", "General");
|
||||
Assert.Equal("left-room", Assert.IsType<CallEnded>(f.Hub.Messages.Last().Arguments[0]).Reason);
|
||||
await f.Invite();
|
||||
await f.Service.EndForUserAsync("alice", "profile-changed");
|
||||
Assert.Equal("profile-changed", Assert.IsType<CallEnded>(f.Hub.Messages.Last().Arguments[0]).Reason);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Candidate_count_is_bounded()
|
||||
{
|
||||
using var f = new Fixture();
|
||||
var call = await f.Invite();
|
||||
await f.Service.AcceptAsync(call.Id, "bob", "bob-1", false);
|
||||
await f.Service.SignalAsync(call.Id, "alice", "alice-device", new("offer", Sdp));
|
||||
for (var i = 0; i < 256; i++)
|
||||
await f.Service.SignalAsync(call.Id, "alice", "alice-device", Candidate());
|
||||
await Assert.ThrowsAsync<HubException>(() => f.Service.SignalAsync(call.Id, "alice", "alice-device", Candidate()));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("candidate:1 1 udp 100 192.0.2.1 5000 typ host")]
|
||||
[InlineData("candidate:1 1 udp 100 192.0.2.1 5000 typ host typ relay")]
|
||||
[InlineData("candidate:1 1 udp 100 192.0.2.1 5000 typ relay raddr 10.0.0.1 rport 5000")]
|
||||
public void Relay_policy_rejects_direct_forged_and_related_addresses(string candidate)
|
||||
{
|
||||
Assert.Throws<HubException>(() =>
|
||||
CallSignalValidator.Validate(new("candidate", Candidate: candidate, SdpMid: "0", SdpMLineIndex: 0), true));
|
||||
Assert.Throws<HubException>(() =>
|
||||
CallSignalValidator.Validate(new("offer", Sdp + "a=" + candidate + "\r\n"), true));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("o=- 1 1 IN IP4 10.0.0.1\r\n")]
|
||||
[InlineData("c=IN IP4 10.0.0.1\r\n")]
|
||||
[InlineData("a=rtcp:5000 IN IP4 10.0.0.1\r\n")]
|
||||
public void Relay_sdp_must_not_expose_a_direct_address(string line) =>
|
||||
Assert.Throws<HubException>(() => CallSignalValidator.Validate(new("offer", Sdp + line), true));
|
||||
|
||||
[Fact]
|
||||
public void Signal_validation_enforces_exact_shapes_and_size_limits()
|
||||
{
|
||||
CallSignalValidator.Validate(new("offer", Sdp), true);
|
||||
CallSignalValidator.Validate(Candidate(), true);
|
||||
CallSignal[] invalid =
|
||||
[
|
||||
new("bogus"), new("offer", "v=0"), new("offer", Sdp + new string('x', 24000)),
|
||||
new("offer", Sdp + new string('\u00e9', 12000)), new("offer", Sdp + "a=ignored\nc=IN IP4 10.0.0.1"),
|
||||
new("answer", Sdp, "unexpected"), Candidate() with { SdpMid = null },
|
||||
Candidate() with { SdpMLineIndex = -1 }, Candidate() with { SdpMLineIndex = 17 },
|
||||
Candidate() with { Candidate = new string('x', 2049) }, Candidate() with { Sdp = Sdp },
|
||||
Candidate() with { Candidate = Candidate().Candidate + "\r\ninjected" }
|
||||
];
|
||||
foreach (var signal in invalid) Assert.Throws<HubException>(() => CallSignalValidator.Validate(signal, false));
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class RecordingCallHub : IHubContext<ChatHub>, IHubClients
|
||||
{
|
||||
internal sealed record Message(string[] Targets, string Method, object?[] Arguments);
|
||||
public List<Message> Messages { get; } = [];
|
||||
public string? FailMethod { get; set; }
|
||||
IHubClients IHubContext<ChatHub>.Clients => this;
|
||||
IGroupManager IHubContext<ChatHub>.Groups => throw new NotSupportedException();
|
||||
private sealed class Proxy(RecordingCallHub hub, string[] targets) : IClientProxy
|
||||
{
|
||||
public Task SendCoreAsync(string method, object?[] args, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (hub.FailMethod == method) throw new IOException("Test delivery failed.");
|
||||
hub.Messages.Add(new(targets, method, args));
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
public IClientProxy Client(string id) => new Proxy(this, [id]);
|
||||
public IClientProxy Clients(IReadOnlyList<string> ids) => new Proxy(this, ids.ToArray());
|
||||
public IClientProxy All => throw new NotSupportedException();
|
||||
public IClientProxy AllExcept(IReadOnlyList<string> ids) => throw new NotSupportedException();
|
||||
public IClientProxy Group(string name) => throw new NotSupportedException();
|
||||
public IClientProxy GroupExcept(string name, IReadOnlyList<string> ids) => throw new NotSupportedException();
|
||||
public IClientProxy Groups(IReadOnlyList<string> names) => throw new NotSupportedException();
|
||||
public IClientProxy User(string id) => throw new NotSupportedException();
|
||||
public IClientProxy Users(IReadOnlyList<string> ids) => throw new NotSupportedException();
|
||||
}
|
||||
Loading…
Reference in a new issue