|
| 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 System; |
| 5 | +using System.Security.Cryptography; |
| 6 | +using System.Text; |
| 7 | +using Microsoft.Build.Experimental; |
| 8 | +using Microsoft.Build.Internal; |
| 9 | +using Shouldly; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace Microsoft.Build.UnitTests.BackEnd |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Tests that the names MSBuild server derives from <see cref="ServerNodeHandshake"/> are scoped |
| 16 | + /// to the current user. Named pipes and named mutexes live in machine-wide namespaces, but the |
| 17 | + /// server pipes are created current-user-only, so user-agnostic names would let one user's server |
| 18 | + /// lock every other user on the machine out of the feature. |
| 19 | + /// </summary> |
| 20 | + public class ServerNodeHandshake_Tests |
| 21 | + { |
| 22 | + [Fact] |
| 23 | + public void ComputeHash_IncludesCurrentUser() |
| 24 | + { |
| 25 | + ServerNodeHandshake handshake = new(HandshakeOptions.None); |
| 26 | + |
| 27 | + // The user name must participate in the hash, otherwise every user on the machine |
| 28 | + // computes the same pipe and mutex names. |
| 29 | + handshake.ComputeHash().ShouldBe(Hash($"{handshake.GetKey()} {Environment.UserName}")); |
| 30 | + handshake.ComputeHash().ShouldNotBe(Hash(handshake.GetKey())); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public void ComputeHash_IsStableAcrossInstances() |
| 35 | + { |
| 36 | + // A client and the server it launches are separate processes that must agree on the |
| 37 | + // derived names, so the hash has to depend only on the handshake and the user. |
| 38 | + ServerNodeHandshake first = new(HandshakeOptions.None); |
| 39 | + ServerNodeHandshake second = new(HandshakeOptions.None); |
| 40 | + |
| 41 | + first.ComputeHash().ShouldBe(second.ComputeHash()); |
| 42 | + } |
| 43 | + |
| 44 | + [Fact] |
| 45 | + public void ComputeHash_DoesNotChangeHandshakeSentOverTheWire() |
| 46 | + { |
| 47 | + // The user discriminator is a naming concern only. Adding it to the wire handshake would |
| 48 | + // be a protocol change, so the exchanged components must stay derived from the key alone. |
| 49 | + ServerNodeHandshake handshake = new(HandshakeOptions.None); |
| 50 | + |
| 51 | + HandshakeComponents components = handshake.RetrieveHandshakeComponents(); |
| 52 | + |
| 53 | + components.Options.ShouldBe(CommunicationsUtilities.AvoidEndOfHandshakeSignal(components.Options)); |
| 54 | + handshake.GetKey().ShouldNotContain(Environment.UserName, Case.Insensitive); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void ServerNames_AreAllDerivedFromTheUserScopedHash() |
| 59 | + { |
| 60 | + ServerNodeHandshake handshake = new(HandshakeOptions.None); |
| 61 | + string hash = handshake.ComputeHash(); |
| 62 | + |
| 63 | + // Every machine-wide name the server uses must carry the user-scoped hash. |
| 64 | + OutOfProcServerNode.GetPipeName(handshake).ShouldContain(hash); |
| 65 | + OutOfProcServerNode.GetRunningServerMutexName(handshake).ShouldContain(hash); |
| 66 | + OutOfProcServerNode.GetBusyServerMutexName(handshake).ShouldContain(hash); |
| 67 | + } |
| 68 | + |
| 69 | + private static string Hash(string input) |
| 70 | + { |
| 71 | + byte[] utf8 = Encoding.UTF8.GetBytes(input); |
| 72 | +#if NET |
| 73 | + byte[] bytes = SHA256.HashData(utf8); |
| 74 | +#else |
| 75 | + using SHA256 sha = SHA256.Create(); |
| 76 | + byte[] bytes = sha.ComputeHash(utf8); |
| 77 | +#endif |
| 78 | + |
| 79 | + return Convert.ToBase64String(bytes) |
| 80 | + .Replace("/", "_") |
| 81 | + .Replace("=", string.Empty); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments