-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBluetoothControlFrameProcessorTests.cs
More file actions
239 lines (202 loc) · 8.84 KB
/
Copy pathBluetoothControlFrameProcessorTests.cs
File metadata and controls
239 lines (202 loc) · 8.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using System.Text.Json;
using System.Text.Json.Nodes;
using SwitchifyPc.Core.Bluetooth;
using SwitchifyPc.Core.Control;
using SwitchifyPc.Core.Input;
using SwitchifyPc.Core.Pairing;
using SwitchifyPc.Core.Settings;
using SwitchifyPc.Protocol;
namespace SwitchifyPc.Tests;
public sealed class BluetoothControlFrameProcessorTests
{
private const string DeviceId = "android-1";
private const string Token = "shared-token";
private const double Now = 1_000_000;
[Fact]
public async Task ReassemblesRequestAndFramesAckResponse()
{
FakeInputAdapter adapter = new();
BluetoothControlFrameProcessor processor = new(CreateSession(adapter), maxResponseFramePayloadBytes: 20);
IReadOnlyList<BluetoothFrame> frames = BluetoothFrameCodec.CreateFrames(SignedCommand("keyboard.key", new { key = "Meta" }), "incoming-1", maxPayloadBytes: 40);
BluetoothControlFrameResult incomplete = BluetoothControlFrameResult.Incomplete();
BluetoothControlFrameResult complete = BluetoothControlFrameResult.Incomplete();
for (int index = 0; index < frames.Count; index++)
{
BluetoothControlFrameResult result = await processor.AcceptAsync("ble", frames[index]);
if (index == 0)
{
incomplete = result;
}
complete = result;
}
Assert.False(incomplete.MessageComplete);
Assert.True(complete.MessageComplete);
Assert.Null(complete.ErrorReason);
Assert.NotEmpty(complete.ResponseFrames);
Assert.Equal(["pressKey:Meta"], adapter.Calls);
AssertResponseType(complete.ResponseFrames, "ack");
}
[Fact]
public async Task NoAckRequestsCompleteWithoutResponseFrames()
{
FakeInputAdapter adapter = new();
BluetoothControlFrameProcessor processor = new(CreateSession(adapter));
IReadOnlyList<BluetoothFrame> frames = BluetoothFrameCodec.CreateFrames(SignedCommand("mouse.move", new { dx = 4, dy = 5 }, responseMode: "none"), "incoming-1");
BluetoothControlFrameResult result = BluetoothControlFrameResult.Incomplete();
foreach (BluetoothFrame frame in frames)
{
result = await processor.AcceptAsync("ble", frame);
}
Assert.True(result.MessageComplete);
Assert.Empty(result.ResponseFrames);
Assert.Equal(["moveMouseBy:4,5"], adapter.Calls);
}
[Fact]
public async Task InvalidFrameReturnsErrorWithoutSessionExecution()
{
FakeInputAdapter adapter = new();
BluetoothControlFrameProcessor processor = new(CreateSession(adapter));
BluetoothFrame invalid = new(99, "incoming-1", 0, true, 2, "e30=");
BluetoothControlFrameResult result = await processor.AcceptAsync("ble", invalid);
Assert.True(result.MessageComplete);
Assert.Equal("invalid_frame", result.ErrorReason);
Assert.Empty(result.ResponseFrames);
Assert.Empty(adapter.Calls);
}
[Fact]
public async Task RemoveConnectionDropsPartialMessages()
{
BluetoothControlFrameProcessor processor = new(CreateSession(new FakeInputAdapter()));
IReadOnlyList<BluetoothFrame> frames = BluetoothFrameCodec.CreateFrames(SignedCommand("keyboard.key", new { key = "Meta" }), "incoming-1", maxPayloadBytes: 40);
Assert.False((await processor.AcceptAsync("ble", frames[0])).MessageComplete);
processor.RemoveConnection("ble");
BluetoothControlFrameResult result = await processor.AcceptAsync("ble", frames[^1]);
Assert.False(result.MessageComplete);
Assert.Equal("incomplete", result.ErrorReason);
}
private static void AssertResponseType(IReadOnlyList<BluetoothFrame> frames, string expectedType)
{
BluetoothFrameReassembler reassembler = new();
BluetoothFrameReassemblyResult result = BluetoothFrameReassemblyResult.Incomplete("incomplete");
foreach (BluetoothFrame frame in frames)
{
result = reassembler.Accept(frame);
}
Assert.True(result.Ok);
using JsonDocument response = JsonDocument.Parse(result.Message!);
Assert.Equal(expectedType, response.RootElement.GetProperty("type").GetString());
}
private static ControlSession CreateSession(FakeInputAdapter adapter)
{
MemoryPairingStore store = new(new PairingState(
DesktopId: "desktop-1",
PairedDevices:
[
new PairedDevice(DeviceId, "Phone", Token, PairedAt: 1, LastSeenAt: null)
]));
PointerMovementProfile profile = new(
DisplayId: "display-1",
ScaleFactor: 1,
Bounds: new Bounds(0, 0, 1920, 1080),
MaxDelta: ProtocolConstants.MaxPointerDelta,
RecommendedDeltas: new RecommendedDeltas(49, 130, 281),
Capabilities: TestPointerCapabilities());
return new ControlSession(
new CommandAuthValidator(store, () => Now),
new DesktopCommandExecutor(adapter),
new FixedPointerProfileProvider(profile));
}
private static PointerCapabilities TestPointerCapabilities()
{
return new PointerCapabilities(
true,
ProtocolConstants.NoAckControlCommandTypes.ToArray(),
ProtocolConstants.CommandTypes.ToArray(),
new MouseRepeatCapabilities(true, true, 250, 250, 250, 100, 2000),
PointerProfile.PointerSpeedFor(PointerMovementSettingsModel.Default));
}
private static string SignedCommand(string type, object payload, string id = "request-1", string? responseMode = null)
{
JsonObject command = new()
{
["version"] = ProtocolConstants.ProtocolVersion,
["id"] = id,
["deviceId"] = DeviceId,
["timestamp"] = Now,
["type"] = type,
["payload"] = JsonSerializer.SerializeToNode(payload),
["auth"] = ""
};
if (responseMode is not null)
{
command["responseMode"] = responseMode;
}
using JsonDocument unsignedDocument = JsonDocument.Parse(command.ToJsonString());
command["auth"] = CommandAuth.CreateCommandAuthProof(unsignedDocument.RootElement, Token);
return command.ToJsonString();
}
private sealed class FakeInputAdapter : IDesktopInputAdapter
{
public List<string> Calls { get; } = [];
public Task MoveMouseByAsync(double dx, double dy, CancellationToken cancellationToken = default)
{
Calls.Add($"moveMouseBy:{dx},{dy}");
return Task.CompletedTask;
}
public Task SetMouseButtonDownAsync(string button, bool down, CancellationToken cancellationToken = default)
{
Calls.Add($"setMouseButtonDown:{button}:{down}");
return Task.CompletedTask;
}
public Task ClickMouseAsync(string button, CancellationToken cancellationToken = default)
{
Calls.Add($"clickMouse:{button}");
return Task.CompletedTask;
}
public Task DoubleClickMouseAsync(string button, CancellationToken cancellationToken = default)
{
Calls.Add($"doubleClickMouse:{button}");
return Task.CompletedTask;
}
public Task ScrollMouseAsync(double dx, double dy, CancellationToken cancellationToken = default)
{
Calls.Add($"scrollMouse:{dx},{dy}");
return Task.CompletedTask;
}
public Task PressKeyAsync(string key, CancellationToken cancellationToken = default)
{
Calls.Add($"pressKey:{key}");
return Task.CompletedTask;
}
public Task SetKeyDownAsync(string key, bool down, CancellationToken cancellationToken = default)
{
Calls.Add($"setKeyDown:{key}:{down}");
return Task.CompletedTask;
}
public Task PressShortcutAsync(IReadOnlyList<string> keys, CancellationToken cancellationToken = default)
{
Calls.Add($"pressShortcut:{string.Join("+", keys)}");
return Task.CompletedTask;
}
public Task TypeTextAsync(string text, CancellationToken cancellationToken = default)
{
Calls.Add($"typeText:{text}");
return Task.CompletedTask;
}
public Task TypeCharacterAsync(string text, CancellationToken cancellationToken = default)
{
Calls.Add($"typeCharacter:{text}");
return Task.CompletedTask;
}
public Task MediaControlAsync(string action, CancellationToken cancellationToken = default)
{
Calls.Add($"mediaControl:{action}");
return Task.CompletedTask;
}
public Task ControlWindowAsync(string action, CancellationToken cancellationToken = default)
{
Calls.Add($"controlWindow:{action}");
return Task.CompletedTask;
}
}
}