Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
diagnostics
  • Loading branch information
danmoseley committed Dec 31, 2021
commit fe44944a0ddfa794a8f5a7e974ac8aec6a8422c0
94 changes: 90 additions & 4 deletions src/libraries/System.IO.FileSystem/tests/File/EncryptDecrypt.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.DotNet.XUnitExtensions;
using System.Diagnostics;
using System.Diagnostics.Eventing.Reader;
using System.Security;
using System.ServiceProcess;
using Xunit;
using Xunit.Abstractions;

namespace System.IO.Tests
{
[ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
public class EncryptDecrypt : FileSystemTest
{
private readonly ITestOutputHelper _output;

public EncryptDecrypt(ITestOutputHelper output)
{
_output = output;
}

[Fact]
public static void NullArg_ThrowsException()
public void NullArg_ThrowsException()
{
AssertExtensions.Throws<ArgumentNullException>("path", () => File.Encrypt(null));
AssertExtensions.Throws<ArgumentNullException>("path", () => File.Decrypt(null));
}

[SkipOnTargetFramework(TargetFrameworkMonikers.Netcoreapp)]
[Fact]
public static void EncryptDecrypt_NotSupported()
public void EncryptDecrypt_NotSupported()
{
Assert.Throws<PlatformNotSupportedException>(() => File.Encrypt("path"));
Assert.Throws<PlatformNotSupportedException>(() => File.Decrypt("path"));
Expand All @@ -29,7 +40,7 @@ public static void EncryptDecrypt_NotSupported()
// because EFS (Encrypted File System), its underlying technology, is not available on these operating systems.
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer), nameof(PlatformDetection.IsNotWindowsHomeEdition))]
[PlatformSpecific(TestPlatforms.Windows)]
public static void EncryptDecrypt_Read()
public void EncryptDecrypt_Read()
{
string tmpFileName = Path.GetTempFileName();
string textContentToEncrypt = "Content to encrypt";
Expand All @@ -48,7 +59,13 @@ public static void EncryptDecrypt_Read()
{
// Ignore ERROR_NOT_FOUND 1168 (0x490). It is reported when EFS is disabled by domain policy.
// Ignore ERROR_NO_USER_KEYS (0x1776). This occurs when no user key exists to encrypt with.
return;
throw new SkipTestException($"Encrypt not available. Error 0x{e.HResult:X}");
}
catch (IOException e)
{
_output.WriteLine($"Encrypt failed with {e.Message}. Logging some EFS diagnostics..");
LogEFSDiagnostics();
throw;
}

Assert.Equal(fileContentRead, File.ReadAllText(tmpFileName));
Expand All @@ -63,5 +80,74 @@ public static void EncryptDecrypt_Read()
File.Delete(tmpFileName);
}
}

private void LogEFSDiagnostics()
{
try
{
using var sc = new ServiceController("EFS");
_output.WriteLine($"EFS service is: {sc.Status}");
if (sc.Status != ServiceControllerStatus.Running)
{
_output.WriteLine("Trying to start EFS service");
sc.Start();
_output.WriteLine($"EFS service is now: {sc.Status}");
}
}
catch(Exception e)
{
_output.WriteLine(e.ToString());
}

var hours = 1; // how many hours to look backwards
var query = @$"
<QueryList>
<Query Id='0' Path='System'>
<Select Path='System'>
*[System[Provider/@Name='Server']]
</Select>
<Select Path='System'>
*[System[Provider/@Name='Service Control Manager']]
</Select>
<Select Path='System'>
*[System[Provider/@Name='Microsoft-Windows-EFS']]
</Select>
<Suppress Path='System'>
*[System[TimeCreated[timediff(@SystemTime) &gt;= {hours * 60 * 60 * 1000L}]]]
</Suppress>
</Query>
</QueryList> ";

var eventQuery = new EventLogQuery("System", PathType.LogName, query);

var eventReader = new EventLogReader(eventQuery);

EventRecord record = eventReader.ReadEvent();
var garbage = new string[] { "Background Intelligent", "Intel", "Defender", "Intune", "BITS", "NetBT"};

_output.WriteLine("===== Dumping recent relevant events: =====");
while (record != null)
{
string description = "";
try
{
description = record.FormatDescription();
}
catch (EventLogException) { }

foreach (string term in garbage)
{
if (description.Contains(term, StringComparison.OrdinalIgnoreCase))
goto next;
}

_output.WriteLine($"{record.TimeCreated} {record.ProviderName} [{record.LevelDisplayName} {record.Id}] {description.Replace("\r\n", " ")}");

next:
record = eventReader.ReadEvent();
}

_output.WriteLine("==== Finished dumping =====");
}
}
}