Skip to content

Commit dc56b08

Browse files
authored
[release/6.0] Port test-only test fixes (#63507)
* Outerloop ports * Port EFS test fix * Partially disable test (#59760) * fix build
1 parent d575a6f commit dc56b08

File tree

11 files changed

+154
-15
lines changed

11 files changed

+154
-15
lines changed

src/libraries/Common/tests/TestUtilities/System/PlatformDetection.Windows.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ public static bool IsInAppContainer
234234
}
235235
}
236236

237+
public static bool CanRunImpersonatedTests => PlatformDetection.IsNotWindowsNanoServer && PlatformDetection.IsWindowsAndElevated;
238+
237239
private static int s_isWindowsElevated = -1;
238240
public static bool IsWindowsAndElevated
239241
{

src/libraries/Common/tests/TestUtilities/System/WindowsIdentityFixture.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Security.Principal;
1111
using System.Threading.Tasks;
1212
using Microsoft.Win32.SafeHandles;
13+
using Xunit;
1314

1415
namespace System
1516
{
@@ -37,6 +38,8 @@ public sealed class WindowsTestAccount : IDisposable
3738

3839
public WindowsTestAccount(string userName)
3940
{
41+
Assert.True(PlatformDetection.IsWindowsAndElevated);
42+
4043
_userName = userName;
4144
CreateUser();
4245
}
@@ -77,6 +80,10 @@ private void CreateUser()
7780
throw new Win32Exception((int)result);
7881
}
7982
}
83+
else if (result != 0)
84+
{
85+
throw new Win32Exception((int)result);
86+
}
8087

8188
const int LOGON32_PROVIDER_DEFAULT = 0;
8289
const int LOGON32_LOGON_INTERACTIVE = 2;

src/libraries/System.IO.FileSystem/tests/Enumeration/SpecialDirectoryTests.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@ public void SkippingHiddenFiles()
6060
{
6161
// Files that begin with periods are considered hidden on Unix
6262
string[] paths = GetNames(TestDirectory, new EnumerationOptions { ReturnSpecialDirectories = true, AttributesToSkip = 0 });
63-
Assert.Contains(".", paths);
63+
64+
if (!PlatformDetection.IsWindows10Version22000OrGreater)
65+
{
66+
// Sometimes this is not returned - presumably an OS bug.
67+
// This occurs often on Windows 11, very rarely otherwise.
68+
Assert.Contains(".", paths);
69+
}
70+
6471
Assert.Contains("..", paths);
6572
}
6673
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
4+
using Microsoft.DotNet.XUnitExtensions;
5+
using System.Diagnostics;
6+
using System.Diagnostics.Eventing.Reader;
7+
using System.Linq;
8+
using System.Security;
9+
using System.ServiceProcess;
10+
using Xunit;
11+
using Xunit.Abstractions;
12+
13+
namespace System.IO.Tests
14+
{
15+
public partial class EncryptDecrypt
16+
{
17+
partial void EnsureEFSServiceStarted()
18+
{
19+
try
20+
{
21+
using var sc = new ServiceController("EFS");
22+
_output.WriteLine($"EFS service is: {sc.Status}");
23+
if (sc.Status != ServiceControllerStatus.Running)
24+
{
25+
_output.WriteLine("Trying to start EFS service");
26+
sc.Start();
27+
_output.WriteLine($"EFS service is now: {sc.Status}");
28+
}
29+
}
30+
catch (Exception e)
31+
{
32+
_output.WriteLine(e.ToString());
33+
}
34+
}
35+
36+
partial void LogEFSDiagnostics()
37+
{
38+
int hours = 1; // how many hours to look backwards
39+
string query = @$"
40+
<QueryList>
41+
<Query Id='0' Path='System'>
42+
<Select Path='System'>
43+
*[System[Provider/@Name='Server']]
44+
</Select>
45+
<Select Path='System'>
46+
*[System[Provider/@Name='Service Control Manager']]
47+
</Select>
48+
<Select Path='System'>
49+
*[System[Provider/@Name='Microsoft-Windows-EFS']]
50+
</Select>
51+
<Suppress Path='System'>
52+
*[System[TimeCreated[timediff(@SystemTime) &gt;= {hours * 60 * 60 * 1000L}]]]
53+
</Suppress>
54+
</Query>
55+
</QueryList> ";
56+
57+
var eventQuery = new EventLogQuery("System", PathType.LogName, query);
58+
59+
using var eventReader = new EventLogReader(eventQuery);
60+
61+
EventRecord record = eventReader.ReadEvent();
62+
var garbage = new string[] { "Background Intelligent", "Intel", "Defender", "Intune", "BITS", "NetBT"};
63+
64+
_output.WriteLine("===== Dumping recent relevant events: =====");
65+
while (record != null)
66+
{
67+
string description = "";
68+
try
69+
{
70+
description = record.FormatDescription();
71+
}
72+
catch (EventLogException) { }
73+
74+
if (!garbage.Any(term => description.Contains(term, StringComparison.OrdinalIgnoreCase)))
75+
{
76+
_output.WriteLine($"{record.TimeCreated} {record.ProviderName} [{record.LevelDisplayName} {record.Id}] {description.Replace("\r\n", " ")}");
77+
}
78+
79+
record = eventReader.ReadEvent();
80+
}
81+
82+
_output.WriteLine("==== Finished dumping =====");
83+
}
84+
}
85+
}

src/libraries/System.IO.FileSystem/tests/File/EncryptDecrypt.cs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using Microsoft.DotNet.XUnitExtensions;
45
using System.Diagnostics;
6+
using System.Diagnostics.Eventing.Reader;
57
using System.Security;
8+
using System.ServiceProcess;
69
using Xunit;
10+
using Xunit.Abstractions;
711

812
namespace System.IO.Tests
913
{
1014
[ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
11-
public class EncryptDecrypt : FileSystemTest
15+
public partial class EncryptDecrypt : FileSystemTest
1216
{
17+
private readonly ITestOutputHelper _output;
18+
19+
public EncryptDecrypt(ITestOutputHelper output)
20+
{
21+
_output = output;
22+
}
23+
1324
[Fact]
14-
public static void NullArg_ThrowsException()
25+
public void NullArg_ThrowsException()
1526
{
1627
AssertExtensions.Throws<ArgumentNullException>("path", () => File.Encrypt(null));
1728
AssertExtensions.Throws<ArgumentNullException>("path", () => File.Decrypt(null));
1829
}
1930

2031
[SkipOnTargetFramework(TargetFrameworkMonikers.Netcoreapp)]
2132
[Fact]
22-
public static void EncryptDecrypt_NotSupported()
33+
public void EncryptDecrypt_NotSupported()
2334
{
2435
Assert.Throws<PlatformNotSupportedException>(() => File.Encrypt("path"));
2536
Assert.Throws<PlatformNotSupportedException>(() => File.Decrypt("path"));
@@ -29,7 +40,8 @@ public static void EncryptDecrypt_NotSupported()
2940
// because EFS (Encrypted File System), its underlying technology, is not available on these operating systems.
3041
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer), nameof(PlatformDetection.IsNotWindowsHomeEdition))]
3142
[PlatformSpecific(TestPlatforms.Windows)]
32-
public static void EncryptDecrypt_Read()
43+
[OuterLoop] // Occasional failures: https://github.com/dotnet/runtime/issues/12339
44+
public void EncryptDecrypt_Read()
3345
{
3446
string tmpFileName = Path.GetTempFileName();
3547
string textContentToEncrypt = "Content to encrypt";
@@ -39,14 +51,26 @@ public static void EncryptDecrypt_Read()
3951
string fileContentRead = File.ReadAllText(tmpFileName);
4052
Assert.Equal(textContentToEncrypt, fileContentRead);
4153

54+
EnsureEFSServiceStarted();
55+
4256
try
4357
{
4458
File.Encrypt(tmpFileName);
4559
}
46-
catch (IOException e) when (e.HResult == unchecked((int)0x80070490))
60+
catch (IOException e) when (e.HResult == unchecked((int)0x80070490) ||
61+
e.HResult == unchecked((int)0x80071776) ||
62+
e.HResult == unchecked((int)0x800701AE))
4763
{
4864
// Ignore ERROR_NOT_FOUND 1168 (0x490). It is reported when EFS is disabled by domain policy.
49-
return;
65+
// Ignore ERROR_NO_USER_KEYS (0x1776). This occurs when no user key exists to encrypt with.
66+
// Ignore ERROR_ENCRYPTION_DISABLED (0x1AE). This occurs when EFS is disabled by group policy on the volume.
67+
throw new SkipTestException($"Encrypt not available. Error 0x{e.HResult:X}");
68+
}
69+
catch (IOException e)
70+
{
71+
_output.WriteLine($"Encrypt failed with {e.Message} 0x{e.HResult:X}");
72+
LogEFSDiagnostics();
73+
throw;
5074
}
5175

5276
Assert.Equal(fileContentRead, File.ReadAllText(tmpFileName));
@@ -61,5 +85,9 @@ public static void EncryptDecrypt_Read()
6185
File.Delete(tmpFileName);
6286
}
6387
}
88+
89+
partial void EnsureEFSServiceStarted(); // no-op on Unix
90+
91+
partial void LogEFSDiagnostics(); // no-op on Unix currently
6492
}
6593
}

src/libraries/System.IO.FileSystem/tests/Net5CompatTests/System.IO.FileSystem.Net5Compat.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<Compile Include="$(CommonPath)System\Text\ValueStringBuilder.cs" Link="Common\System\Text\ValueStringBuilder.cs" />
3333
<Compile Include="$(CoreLibSharedDir)System\IO\PathInternal.cs" Link="Common\System\IO\PathInternal.cs" />
3434
<Compile Include="$(CoreLibSharedDir)System\IO\PathInternal.Windows.cs" Link="Common\System\IO\PathInternal.Windows.cs" />
35+
<ProjectReference Include="$(LibrariesProjectRoot)System.ServiceProcess.ServiceController\src\System.ServiceProcess.ServiceController.csproj" />
3536
</ItemGroup>
3637
<ItemGroup Condition="'$(TargetsBrowser)' == 'true'">
3738
<Compile Remove="..\**\*.Unix.cs" />

src/libraries/System.IO.FileSystem/tests/System.IO.FileSystem.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<ItemGroup Condition="'$(TargetsWindows)' == 'true'">
7878
<Compile Include="Base\SymbolicLinks\BaseSymbolicLinks.Windows.cs" />
7979
<Compile Include="Directory\Delete.Windows.cs" />
80+
<Compile Include="File\EncryptDecrypt.Windows.cs" />
8081
<Compile Include="FileSystemTest.Windows.cs" />
8182
<Compile Include="FileStream\ctor_options.Windows.cs" />
8283
<Compile Include="FileStream\FileStreamConformanceTests.Windows.cs" />

src/libraries/System.IO.Ports/tests/SerialPort/DosDevices.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Collections;
55
using System.Collections.Generic;
6+
using System.ComponentModel;
67
using System.Runtime.InteropServices;
78

89
namespace System.IO.Ports.Tests
@@ -127,14 +128,20 @@ private static char[] CallQueryDosDevice(string name, out int dataSize)
127128
buffer = new char[buffer.Length * 2];
128129
dataSize = QueryDosDevice(null, buffer, buffer.Length);
129130
}
131+
else if (lastError == ERROR_ACCESS_DENIED) // Access denied eg for "MSSECFLTSYS" - just skip
132+
{
133+
dataSize = 0;
134+
break;
135+
}
130136
else
131137
{
132-
throw new Exception("Unknown Win32 Error: " + lastError);
138+
throw new Exception($"Error {lastError} calling QueryDosDevice for '{name}' with buffer size {buffer.Length}. {new Win32Exception((int)lastError).Message}");
133139
}
134140
}
135141
return buffer;
136142
}
137143

144+
public const int ERROR_ACCESS_DENIED = 5;
138145
public const int ERROR_INSUFFICIENT_BUFFER = 122;
139146
public const int ERROR_MORE_DATA = 234;
140147

src/libraries/System.Net.Http/tests/FunctionalTests/ImpersonatedAuthTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ namespace System.Net.Http.Functional.Tests
1414
{
1515
public class ImpersonatedAuthTests: IClassFixture<WindowsIdentityFixture>
1616
{
17-
public static bool CanRunImpersonatedTests = PlatformDetection.IsWindows && PlatformDetection.IsNotWindowsNanoServer;
1817
private readonly WindowsIdentityFixture _fixture;
1918
private readonly ITestOutputHelper _output;
2019

@@ -28,11 +27,11 @@ public ImpersonatedAuthTests(WindowsIdentityFixture windowsIdentityFixture, ITe
2827
}
2928

3029
[OuterLoop]
31-
[ConditionalTheory(nameof(CanRunImpersonatedTests))]
30+
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.CanRunImpersonatedTests))]
3231
[InlineData(true)]
3332
[InlineData(false)]
3433
[PlatformSpecific(TestPlatforms.Windows)]
35-
public async Task DefaultHandler_ImpersonificatedUser_Success(bool useNtlm)
34+
public async Task DefaultHandler_ImpersonatedUser_Success(bool useNtlm)
3635
{
3736
await LoopbackServer.CreateClientAndServerAsync(
3837
async uri =>

src/libraries/System.Net.Ping/tests/FunctionalTests/PingTest.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,9 @@ public async Task SendPingToExternalHostWithLowTtlTest()
878878
options.Ttl = 1;
879879
// This should always fail unless host is one IP hop away.
880880
pingReply = await ping.SendPingAsync(host, TestSettings.PingTimeout, TestSettings.PayloadAsBytesShort, options);
881-
Assert.NotEqual(IPStatus.Success, pingReply.Status);
881+
882+
Assert.True(pingReply.Status == IPStatus.TimeExceeded || pingReply.Status == IPStatus.TtlExpired);
883+
Assert.NotEqual(IPAddress.Any, pingReply.Address);
882884
}
883885

884886
[Fact]

src/libraries/System.Security.Principal.Windows/tests/WindowsIdentityImpersonatedTests.netcoreapp.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public WindowsIdentityImpersonatedTests(WindowsIdentityFixture windowsIdentityFi
2626
Assert.False(string.IsNullOrEmpty(_fixture.TestAccount.AccountName));
2727
}
2828

29-
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))]
29+
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.CanRunImpersonatedTests))]
3030
[OuterLoop]
3131
public async Task RunImpersonatedAsync_TaskAndTaskOfT()
3232
{
@@ -60,7 +60,7 @@ void Asserts(WindowsIdentity currentWindowsIdentity)
6060
}
6161
}
6262

63-
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))]
63+
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.CanRunImpersonatedTests))]
6464
[OuterLoop]
6565
public void RunImpersonated_NameResolution()
6666
{
@@ -78,7 +78,7 @@ public void RunImpersonated_NameResolution()
7878
});
7979
}
8080

81-
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))]
81+
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.CanRunImpersonatedTests))]
8282
[OuterLoop]
8383
public async Task RunImpersonatedAsync_NameResolution()
8484
{

0 commit comments

Comments
 (0)