This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
AuthenticationManagerTest.cs
150 lines (126 loc) · 5.07 KB
/
AuthenticationManagerTest.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections;
using System.Diagnostics;
using Microsoft.DotNet.RemoteExecutor;
using Xunit;
#pragma warning disable CS0618 // obsolete warnings
namespace System.Net.Tests
{
public class AuthenticationManagerTest
{
[Fact]
public void Authenticate_NotSupported()
{
Assert.Throws<PlatformNotSupportedException>(() => AuthenticationManager.Authenticate(null, null, null));
Assert.Throws<PlatformNotSupportedException>(() => AuthenticationManager.PreAuthenticate(null, null));
}
[Fact]
public void Register_Null_Throws()
{
Assert.Throws<ArgumentNullException>(() => AuthenticationManager.Register(null));
}
[Fact]
public void Register_Unregister_ModuleCountUnchanged()
{
RemoteExecutor.Invoke(() =>
{
int initialCount = GetModuleCount();
IAuthenticationModule module = new CustomModule();
AuthenticationManager.Register(module);
AuthenticationManager.Unregister(module);
Assert.Equal(initialCount, GetModuleCount());
return RemoteExecutor.SuccessExitCode;
}).Dispose();
}
[Fact]
public void Register_UnregisterByScheme_ModuleCountUnchanged()
{
RemoteExecutor.Invoke(() =>
{
int initialCount = GetModuleCount();
IAuthenticationModule module = new CustomModule();
AuthenticationManager.Register(module);
AuthenticationManager.Unregister("custom");
Assert.Equal(initialCount, GetModuleCount());
return RemoteExecutor.SuccessExitCode;
}).Dispose();
}
[Fact]
public void RegisteredModules_DefaultCount_ExpectedValue()
{
int count = 0;
IEnumerator modules = AuthenticationManager.RegisteredModules;
while (modules.MoveNext()) count++;
Assert.Equal(0, count);
}
[Fact]
public void CredentialPolicy_Roundtrip()
{
Assert.Null(AuthenticationManager.CredentialPolicy);
RemoteExecutor.Invoke(() =>
{
ICredentialPolicy cp = new DummyCredentialPolicy();
AuthenticationManager.CredentialPolicy = cp;
Assert.Same(cp, AuthenticationManager.CredentialPolicy);
AuthenticationManager.CredentialPolicy = null;
Assert.Null(AuthenticationManager.CredentialPolicy);
return RemoteExecutor.SuccessExitCode;
}).Dispose();
}
[Fact]
public void CustomTargetNameDictionary_ValidCollection()
{
Assert.NotNull(AuthenticationManager.CustomTargetNameDictionary);
Assert.Empty(AuthenticationManager.CustomTargetNameDictionary);
Assert.Same(AuthenticationManager.CustomTargetNameDictionary, AuthenticationManager.CustomTargetNameDictionary);
RemoteExecutor.Invoke(() =>
{
string theKey = "http://www.contoso.com";
string theValue = "HTTP/www.contoso.com";
AuthenticationManager.CustomTargetNameDictionary.Add(theKey, theValue);
Assert.Equal(theValue, AuthenticationManager.CustomTargetNameDictionary[theKey]);
AuthenticationManager.CustomTargetNameDictionary.Clear();
Assert.Equal(0, AuthenticationManager.CustomTargetNameDictionary.Count);
return RemoteExecutor.SuccessExitCode;
}).Dispose();
}
private static int GetModuleCount()
{
int count = 0;
IEnumerator modules = AuthenticationManager.RegisteredModules;
while (modules.MoveNext()) count++;
return count;
}
private sealed class DummyCredentialPolicy : ICredentialPolicy
{
public bool ShouldSendCredential(Uri challengeUri, WebRequest request, NetworkCredential credential, IAuthenticationModule authenticationModule) => true;
}
private sealed class CustomModule : IAuthenticationModule
{
public bool CanPreAuthenticate
{
get
{
return false;
}
}
public string AuthenticationType
{
get
{
return "custom";
}
}
public Authorization Authenticate(string challenge, WebRequest request, ICredentials credentials)
{
throw new NotImplementedException();
}
public Authorization PreAuthenticate(WebRequest request, ICredentials credentials)
{
throw new NotImplementedException();
}
}
}
}