forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EncryptDecrypt.cs
88 lines (76 loc) · 3.59 KB
/
EncryptDecrypt.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
// 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.Security;
using Xunit;
using Xunit.Abstractions;
namespace System.IO.Tests
{
public partial class EncryptDecrypt : FileSystemTest
{
private readonly ITestOutputHelper _output;
public EncryptDecrypt(ITestOutputHelper output)
{
_output = output;
}
[Fact]
public void NullArg_ThrowsException()
{
AssertExtensions.Throws<ArgumentNullException>("path", () => File.Encrypt(null));
AssertExtensions.Throws<ArgumentNullException>("path", () => File.Decrypt(null));
}
[SkipOnTargetFramework(TargetFrameworkMonikers.Netcoreapp)]
[Fact]
public void EncryptDecrypt_NotSupported()
{
Assert.Throws<PlatformNotSupportedException>(() => File.Encrypt("path"));
Assert.Throws<PlatformNotSupportedException>(() => File.Decrypt("path"));
}
// On Windows Nano Server and Home Edition, file encryption with File.Encrypt(string path) throws an IOException
// 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)]
[OuterLoop] // Occasional failures: https://github.com/dotnet/runtime/issues/12339
public void EncryptDecrypt_Read()
{
string tmpFileName = Path.GetTempFileName();
string textContentToEncrypt = "Content to encrypt";
File.WriteAllText(tmpFileName, textContentToEncrypt);
try
{
string fileContentRead = File.ReadAllText(tmpFileName);
Assert.Equal(textContentToEncrypt, fileContentRead);
EnsureEFSServiceStarted();
try
{
File.Encrypt(tmpFileName);
}
catch (IOException e) when (e.HResult == unchecked((int)0x80070490) ||
(e.HResult == unchecked((int)0x80071776)))
{
// 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.
throw new SkipTestException($"Encrypt not available. Error 0x{e.HResult:X}");
}
catch (IOException e)
{
_output.WriteLine($"Encrypt failed with {e.Message} 0x{e.HResult:X}");
LogEFSDiagnostics();
throw;
}
Assert.Equal(fileContentRead, File.ReadAllText(tmpFileName));
Assert.Equal(FileAttributes.Encrypted, (FileAttributes.Encrypted & File.GetAttributes(tmpFileName)));
File.Decrypt(tmpFileName);
Assert.Equal(fileContentRead, File.ReadAllText(tmpFileName));
Assert.NotEqual(FileAttributes.Encrypted, (FileAttributes.Encrypted & File.GetAttributes(tmpFileName)));
}
finally
{
File.Delete(tmpFileName);
}
}
partial void EnsureEFSServiceStarted(); // no-op on Unix
partial void LogEFSDiagnostics(); // no-op on Unix currently
}
}