-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathDelegateTests.cs
376 lines (317 loc) · 14.9 KB
/
DelegateTests.cs
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using System.Security.Principal;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.InternalTesting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.Security;
namespace Microsoft.AspNetCore.Server.HttpSys.FunctionalTests;
public class DelegateTests : LoggedTest
{
private static readonly string _expectedResponseString = "Hello from delegatee";
[ConditionalFact]
[DelegateSupportedCondition(true)]
public void IServerDelegationFeature_IsAvailableFromServices()
{
var builder = new HostBuilder();
builder.ConfigureWebHost(webHost =>
{
webHost.UseHttpSys();
});
using var host = builder.Build();
var server = host.Services.GetRequiredService<IServer>();
var delegationFeature = host.Services.GetRequiredService<IServerDelegationFeature>();
Assert.Same(server, delegationFeature);
}
[ConditionalFact]
[DelegateSupportedCondition(true)]
public async Task DelegateRequestTest()
{
var queueName = Guid.NewGuid().ToString();
using var receiver = Utilities.CreateHttpServer(out var receiverAddress, async httpContext =>
{
await httpContext.Response.WriteAsync(_expectedResponseString);
},
options =>
{
options.RequestQueueName = queueName;
}, LoggerFactory);
DelegationRule destination = default;
using var delegator = Utilities.CreateHttpServer(out var delegatorAddress, httpContext =>
{
var delegateFeature = httpContext.Features.Get<IHttpSysRequestDelegationFeature>();
delegateFeature.DelegateRequest(destination);
return Task.CompletedTask;
}, LoggerFactory);
var delegationProperty = delegator.Features.Get<IServerDelegationFeature>();
destination = delegationProperty.CreateDelegationRule(queueName, receiverAddress);
var responseString = await SendRequestAsync(delegatorAddress);
Assert.Equal(_expectedResponseString, responseString);
destination?.Dispose();
}
[ConditionalFact]
[DelegateSupportedCondition(true)]
public async Task DelegateAfterWriteToResponseBodyShouldThrowTest()
{
var queueName = Guid.NewGuid().ToString();
using var receiver = Utilities.CreateHttpServer(out var receiverAddress, httpContext =>
{
httpContext.Response.StatusCode = StatusCodes.Status418ImATeapot;
return Task.CompletedTask;
},
options =>
{
options.RequestQueueName = queueName;
}, LoggerFactory);
DelegationRule destination = default;
using var delegator = Utilities.CreateHttpServer(out var delegatorAddress, async httpContext =>
{
await httpContext.Response.WriteAsync(_expectedResponseString);
var delegateFeature = httpContext.Features.Get<IHttpSysRequestDelegationFeature>();
Assert.False(delegateFeature.CanDelegate);
Assert.Throws<InvalidOperationException>(() => delegateFeature.DelegateRequest(destination));
}, LoggerFactory);
var delegationProperty = delegator.Features.Get<IServerDelegationFeature>();
destination = delegationProperty.CreateDelegationRule(queueName, receiverAddress);
var responseString = await SendRequestAsync(delegatorAddress);
Assert.Equal(_expectedResponseString, responseString);
destination?.Dispose();
}
[ConditionalFact]
[DelegateSupportedCondition(true)]
public async Task WriteToBodyAfterDelegateShouldNoOp()
{
var queueName = Guid.NewGuid().ToString();
using var receiver = Utilities.CreateHttpServer(out var receiverAddress, async httpContext =>
{
await httpContext.Response.WriteAsync(_expectedResponseString);
},
options =>
{
options.RequestQueueName = queueName;
}, LoggerFactory);
DelegationRule destination = default;
using var delegator = Utilities.CreateHttpServer(out var delegatorAddress, httpContext =>
{
var delegateFeature = httpContext.Features.Get<IHttpSysRequestDelegationFeature>();
delegateFeature.DelegateRequest(destination);
Assert.False(delegateFeature.CanDelegate);
httpContext.Response.WriteAsync(_expectedResponseString);
return Task.CompletedTask;
}, LoggerFactory);
var delegationProperty = delegator.Features.Get<IServerDelegationFeature>();
destination = delegationProperty.CreateDelegationRule(queueName, receiverAddress);
var responseString = await SendRequestAsync(delegatorAddress);
Assert.Equal(_expectedResponseString, responseString);
destination?.Dispose();
}
[ConditionalFact]
[DelegateSupportedCondition(true)]
public async Task DelegateAfterRequestBodyReadShouldThrow()
{
var queueName = Guid.NewGuid().ToString();
using var receiver = Utilities.CreateHttpServer(out var receiverAddress, httpContext =>
{
httpContext.Response.StatusCode = StatusCodes.Status418ImATeapot;
return Task.CompletedTask;
},
options =>
{
options.RequestQueueName = queueName;
}, LoggerFactory);
DelegationRule destination = default;
using var delegator = Utilities.CreateHttpServer(out var delegatorAddress, async httpContext =>
{
var memoryStream = new MemoryStream();
await httpContext.Request.Body.CopyToAsync(memoryStream);
var delegateFeature = httpContext.Features.Get<IHttpSysRequestDelegationFeature>();
Assert.Throws<InvalidOperationException>(() => delegateFeature.DelegateRequest(destination));
}, LoggerFactory);
var delegationProperty = delegator.Features.Get<IServerDelegationFeature>();
destination = delegationProperty.CreateDelegationRule(queueName, receiverAddress);
_ = await SendRequestWithBodyAsync(delegatorAddress);
destination?.Dispose();
}
[ConditionalFact]
[DelegateSupportedCondition(false)]
public async Task DelegationFeaturesAreNull()
{
// Testing the DelegateSupportedCondition
Assert.True(Environment.OSVersion.Version < new Version(10, 0, 22000), "This should be supported on Win 11.");
using var delegator = Utilities.CreateHttpServer(out var delegatorAddress, httpContext =>
{
var delegateFeature = httpContext.Features.Get<IHttpSysRequestDelegationFeature>();
Assert.Null(delegateFeature);
return Task.CompletedTask;
}, LoggerFactory);
var delegationProperty = delegator.Features.Get<IServerDelegationFeature>();
Assert.Null(delegationProperty);
_ = await SendRequestAsync(delegatorAddress);
}
[ConditionalFact]
[DelegateSupportedCondition(true)]
public async Task UpdateDelegationRuleTest()
{
var queueName = Guid.NewGuid().ToString();
using var receiver = Utilities.CreateHttpServer(out var receiverAddress, async httpContext =>
{
await httpContext.Response.WriteAsync(_expectedResponseString);
},
options =>
{
options.RequestQueueName = queueName;
}, LoggerFactory);
DelegationRule destination = default;
using var delegator = Utilities.CreateHttpServer(out var delegatorAddress, httpContext =>
{
var delegateFeature = httpContext.Features.Get<IHttpSysRequestDelegationFeature>();
delegateFeature.DelegateRequest(destination);
return Task.CompletedTask;
}, LoggerFactory);
var delegationProperty = delegator.Features.Get<IServerDelegationFeature>();
destination = delegationProperty.CreateDelegationRule(queueName, receiverAddress);
// Send a request to ensure the rule is fully active
var responseString = await SendRequestAsync(delegatorAddress);
destination?.Dispose();
destination = delegationProperty.CreateDelegationRule(queueName, receiverAddress);
responseString = await SendRequestAsync(delegatorAddress);
Assert.Equal(_expectedResponseString, responseString);
destination?.Dispose();
}
[ConditionalFact]
[DelegateSupportedCondition(true)]
[QuarantinedTest("https://github.com/dotnet/aspnetcore/issues/60141")]
public async Task DelegateAfterReceiverRestart()
{
var queueName = Guid.NewGuid().ToString();
using var receiver = Utilities.CreateHttpServer(out var receiverAddress, async httpContext =>
{
await httpContext.Response.WriteAsync(_expectedResponseString);
},
options =>
{
options.RequestQueueName = queueName;
}, LoggerFactory);
DelegationRule destination = default;
using var delegator = Utilities.CreateHttpServer(out var delegatorAddress, httpContext =>
{
var delegateFeature = httpContext.Features.Get<IHttpSysRequestDelegationFeature>();
delegateFeature.DelegateRequest(destination);
return Task.CompletedTask;
}, LoggerFactory);
var delegationProperty = delegator.Features.Get<IServerDelegationFeature>();
destination = delegationProperty.CreateDelegationRule(queueName, receiverAddress);
var responseString = await SendRequestAsync(delegatorAddress);
Assert.Equal(_expectedResponseString, responseString);
// Stop the receiver
receiver?.Dispose();
// Start the receiver again but this time we need to use CreateOrAttach to attach to the existing queue and setup the UrlPrefixes
using var receiverRestarted = (MessagePump)Utilities.CreateHttpServer(out receiverAddress, async httpContext =>
{
await httpContext.Response.WriteAsync(_expectedResponseString);
},
options =>
{
options.RequestQueueName = queueName;
options.RequestQueueMode = RequestQueueMode.CreateOrAttach;
options.UrlPrefixes.Clear();
options.UrlPrefixes.Add(receiverAddress);
}, LoggerFactory);
responseString = await SendRequestAsync(delegatorAddress);
Assert.Equal(_expectedResponseString, responseString);
destination?.Dispose();
}
[ConditionalFact]
[DelegateSupportedCondition(true)]
public async Task DelegateRequestTestCanSetSecurityDescriptor()
{
// Create a new security descriptor
CommonSecurityDescriptor securityDescriptor = new CommonSecurityDescriptor(false, false, string.Empty);
// Create a discretionary access control list (DACL)
DiscretionaryAcl dacl = new DiscretionaryAcl(false, false, 2);
dacl.AddAccess(AccessControlType.Allow, new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null), -1, InheritanceFlags.None, PropagationFlags.None);
dacl.AddAccess(AccessControlType.Deny, new SecurityIdentifier(WellKnownSidType.BuiltinGuestsSid, null), -1, InheritanceFlags.None, PropagationFlags.None);
// Assign the DACL to the security descriptor
securityDescriptor.DiscretionaryAcl = dacl;
var queueName = Guid.NewGuid().ToString();
using var receiver = Utilities.CreateHttpServer(out var receiverAddress, async httpContext =>
{
await httpContext.Response.WriteAsync(_expectedResponseString);
},
options =>
{
options.RequestQueueName = queueName;
options.RequestQueueSecurityDescriptor = securityDescriptor;
}, LoggerFactory);
DelegationRule destination = default;
using var delegator = Utilities.CreateHttpServer(out var delegatorAddress, httpContext =>
{
var delegateFeature = httpContext.Features.Get<IHttpSysRequestDelegationFeature>();
delegateFeature.DelegateRequest(destination);
return Task.CompletedTask;
}, LoggerFactory);
var delegationProperty = delegator.Features.Get<IServerDelegationFeature>();
destination = delegationProperty.CreateDelegationRule(queueName, receiverAddress);
AssertPermissions(destination.Queue.Handle);
unsafe void AssertPermissions(SafeHandle handle)
{
PSECURITY_DESCRIPTOR pSecurityDescriptor = new();
WIN32_ERROR result = PInvoke.GetSecurityInfo(
handle,
Windows.Win32.Security.Authorization.SE_OBJECT_TYPE.SE_KERNEL_OBJECT,
4, // DACL_SECURITY_INFORMATION
null,
null,
null,
null,
&pSecurityDescriptor);
var length = (int)PInvoke.GetSecurityDescriptorLength(pSecurityDescriptor);
// Copy the security descriptor to a managed byte array
byte[] securityDescriptorBytes = new byte[length];
Marshal.Copy(new IntPtr(pSecurityDescriptor.Value), securityDescriptorBytes, 0, length);
// Convert the byte array to a RawSecurityDescriptor
var securityDescriptor = new RawSecurityDescriptor(securityDescriptorBytes, 0);
var checkedAllowUser = false;
var checkedDenyGuest = false;
foreach (CommonAce ace in securityDescriptor.DiscretionaryAcl)
{
if (ace.SecurityIdentifier.IsWellKnown(WellKnownSidType.BuiltinGuestsSid))
{
Assert.Equal(AceType.AccessDenied, ace.AceType);
checkedDenyGuest = true;
}
else if (ace.SecurityIdentifier.IsWellKnown(WellKnownSidType.BuiltinUsersSid))
{
Assert.Equal(AceType.AccessAllowed, ace.AceType);
checkedAllowUser = true;
}
}
PInvoke.LocalFree((HLOCAL)pSecurityDescriptor.Value);
Assert.True(checkedDenyGuest && checkedAllowUser, "DACL does not contain the expected ACEs");
}
var responseString = await SendRequestAsync(delegatorAddress);
Assert.Equal(_expectedResponseString, responseString);
destination?.Dispose();
}
private async Task<string> SendRequestAsync(string uri)
{
using var client = new HttpClient();
return await client.GetStringAsync(uri);
}
private async Task<string> SendRequestWithBodyAsync(string uri)
{
using var client = new HttpClient();
var content = new StringContent("Sample request body");
var response = await client.PostAsync(uri, content);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
}