|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Net; |
| 7 | +using System.Reflection; |
| 8 | +using System.ServiceModel.Security; |
| 9 | +using Infrastructure.Common; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +public static class SecurityUtilsTest |
| 13 | +{ |
| 14 | + [WcfFact] |
| 15 | + public static void FixNetworkCredential_AppContext_EnableLegacyUpnUsernameFix() |
| 16 | + { |
| 17 | + Type t = Assembly.GetAssembly(typeof(WindowsClientCredential)) |
| 18 | + .GetType(typeof(WindowsClientCredential).Namespace + ".SecurityUtils"); |
| 19 | + |
| 20 | + MethodInfo method = t.GetMethod("FixNetworkCredential", BindingFlags.NonPublic | BindingFlags.Static, |
| 21 | + null, new[] { typeof(NetworkCredential).MakeByRefType() }, null); |
| 22 | + |
| 23 | + FieldInfo f = t.GetField("s_enableLegacyUpnUsernameFix", BindingFlags.Static | BindingFlags.NonPublic); |
| 24 | + |
| 25 | + //default |
| 26 | + var credential = new NetworkCredential("[email protected]", "password"); |
| 27 | + var parameters = new object[] { credential }; |
| 28 | + method.Invoke(null, parameters); |
| 29 | + credential = (NetworkCredential)parameters[0]; |
| 30 | + Assert.NotNull(credential); |
| 31 | + Assert.Equal("[email protected]", credential.UserName); |
| 32 | + Assert.Equal("password", credential.Password); |
| 33 | + Assert.Equal(string.Empty, credential.Domain); |
| 34 | + |
| 35 | + //switch on |
| 36 | + f.SetValue(t, true); |
| 37 | + credential = new NetworkCredential("[email protected]", "password"); |
| 38 | + parameters = new object[] { credential }; |
| 39 | + method.Invoke(null, parameters); |
| 40 | + credential = (NetworkCredential)parameters[0]; |
| 41 | + Assert.NotNull(credential); |
| 42 | + Assert.Equal("user", credential.UserName); |
| 43 | + Assert.Equal("password", credential.Password); |
| 44 | + Assert.Equal("domain.com", credential.Domain); |
| 45 | + |
| 46 | + //switch off |
| 47 | + f.SetValue(t, false); |
| 48 | + credential = new NetworkCredential("[email protected]", "password"); |
| 49 | + parameters = new object[] { credential }; |
| 50 | + method.Invoke(null, parameters); |
| 51 | + credential = (NetworkCredential)parameters[0]; |
| 52 | + Assert.NotNull(credential); |
| 53 | + Assert.Equal("[email protected]", credential.UserName); |
| 54 | + Assert.Equal("password", credential.Password); |
| 55 | + Assert.Equal(string.Empty, credential.Domain); |
| 56 | + } |
| 57 | +} |
0 commit comments