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.Diagnostics;
6 using System.IO;
7 using System.Runtime.InteropServices;
8 using System.Security.Authentication;
9 using System.Security.Cryptography.X509Certificates;
10 using System.Threading.Tasks;
11 
12 using Xunit;
13 
14 namespace System.Net.Security.Tests
15 {
16     internal static class TestConfiguration
17     {
18         public const int PassingTestTimeoutMilliseconds = 4 * 60 * 1000;
19         public const int FailingTestTimeoutMiliseconds = 250;
20 
21         public const string Realm = "TEST.COREFX.NET";
22         public const string KerberosUser = "krb_user";
23         public const string DefaultPassword = "password";
24         public const string HostTarget = "TESTHOST/testfqdn.test.corefx.net";
25         public const string HttpTarget = "TESTHTTP@localhost";
26         public const string Domain = "TEST";
27         public const string NtlmUser = "ntlm_user";
28         public const string NtlmPassword = "ntlm_password";
29         public const string NtlmUserFilePath = "/var/tmp/ntlm_user_file";
30 
31         public static bool SupportsNullEncryption { get { return s_supportsNullEncryption.Value; } }
32 
33         public static Task WhenAllOrAnyFailedWithTimeout(params Task[] tasks)
34             => tasks.WhenAllOrAnyFailed(PassingTestTimeoutMilliseconds);
35 
36         private static Lazy<bool> s_supportsNullEncryption = new Lazy<bool>(() =>
37         {
38             // On Windows, null ciphers (no encryption) are supported.
39             if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
40             {
41                 return true;
42             }
43 
44             // On macOS, the null cipher (no encryption) is not supported.
45             if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
46             {
47                 return false;
48             }
49 
50             // On Unix, it depends on how openssl was built.  So we ask openssl if it has any.
51             try
52             {
53                 using (Process p = Process.Start(new ProcessStartInfo("openssl", "ciphers NULL") { RedirectStandardOutput = true, RedirectStandardError = true }))
54                 {
55                     // On some platforms (openSUSE 13.2 is one example), doing this query can print error messages to standard error
56                     // when the tests are run via MSBuild, this error message gets picked up and treated as an error from the test itself
57                     // causing the task to fail.  We don't actually care about the error text at all, so we just ignore it.
58                     p.ErrorDataReceived += ((object sendingProcess, DataReceivedEventArgs errorText) => { /* ignore */ });
59                     p.BeginErrorReadLine();
60 
61                     return p.StandardOutput.ReadToEnd().Trim().Length > 0;
62                 }
63             }
64             catch { return false; }
65         });
66     }
67 }
68