Skip to content

Commit 22873bb

Browse files
authored
Fix serializability of RPC calls to IAuthorizationService (#381)
This regressed in #376
2 parents f50d6a5 + 8e7f612 commit 22873bb

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

src/Microsoft.ServiceHub.Framework/FrameworkServices.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ private partial class Witness;
117117
[JsonSerializable(typeof(ServiceActivationOptions))]
118118
[JsonSerializable(typeof(ServiceBrokerClientMetadata))]
119119
[JsonSerializable(typeof(BrokeredServicesChangedEventArgs))]
120+
[JsonSerializable(typeof(ProtectedOperation))]
120121
[JsonSerializable(typeof(Guid))]
122+
[JsonSerializable(typeof(EventArgs))]
121123
private partial class SourceGenerationContext : JsonSerializerContext;
122124
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Microsoft.ServiceHub.Framework;
5+
using Microsoft.ServiceHub.Framework.Services;
6+
using Microsoft.VisualStudio.Threading;
7+
8+
public class AuthorizationServiceTests : RpcTestBase<IAuthorizationService, AuthorizationServiceMock>
9+
{
10+
public AuthorizationServiceTests(ITestOutputHelper logger)
11+
: base(logger, FrameworkServices.Authorization)
12+
{
13+
}
14+
15+
[Fact]
16+
public async Task AuthorizeOrThrowAsync()
17+
{
18+
ProtectedOperation expected = new ProtectedOperation("moniker", 5);
19+
await this.ClientProxy.AuthorizeOrThrowAsync(expected, this.TimeoutToken);
20+
Assert.Equal(expected, this.Service.LastReceivedOperation);
21+
}
22+
23+
[Fact]
24+
public async Task GetCredentialsAsync()
25+
{
26+
this.Service.CredentialsToReturn = new Dictionary<string, string>
27+
{
28+
["token"] = "abc",
29+
};
30+
31+
IReadOnlyDictionary<string, string> actual = await this.ClientProxy.GetCredentialsAsync(this.TimeoutToken);
32+
Assert.Equal("abc", actual["token"]);
33+
}
34+
35+
[Fact]
36+
public async Task AuthorizationChanged()
37+
{
38+
AsyncManualResetEvent authorizationChangedEvent = new();
39+
this.ClientProxy.AuthorizationChanged += (s, e) => authorizationChangedEvent.Set();
40+
this.Service.OnAuthorizationChanged();
41+
await authorizationChangedEvent.WaitAsync(this.TimeoutToken);
42+
}
43+
44+
[Fact]
45+
public async Task CredentialsChanged()
46+
{
47+
AsyncManualResetEvent credentialsChangedEvent = new();
48+
this.ClientProxy.CredentialsChanged += (s, e) => credentialsChangedEvent.Set();
49+
this.Service.OnCredentialsChanged();
50+
await credentialsChangedEvent.WaitAsync(this.TimeoutToken);
51+
}
52+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Microsoft.ServiceHub.Framework.Services;
5+
6+
public class AuthorizationServiceMock : IAuthorizationService
7+
{
8+
public event EventHandler? CredentialsChanged;
9+
10+
public event EventHandler? AuthorizationChanged;
11+
12+
internal ProtectedOperation? LastReceivedOperation { get; set; }
13+
14+
internal IReadOnlyDictionary<string, string>? CredentialsToReturn { get; set; }
15+
16+
public ValueTask<bool> CheckAuthorizationAsync(ProtectedOperation operation, CancellationToken cancellationToken = default)
17+
{
18+
this.LastReceivedOperation = operation;
19+
return new(true);
20+
}
21+
22+
public ValueTask<IReadOnlyDictionary<string, string>> GetCredentialsAsync(CancellationToken cancellationToken = default)
23+
{
24+
return new(this.CredentialsToReturn ?? new Dictionary<string, string>());
25+
}
26+
27+
public virtual void OnCredentialsChanged() => this.CredentialsChanged?.Invoke(this, EventArgs.Empty);
28+
29+
public virtual void OnAuthorizationChanged() => this.AuthorizationChanged?.Invoke(this, EventArgs.Empty);
30+
}

0 commit comments

Comments
 (0)