<commit_before>// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;

using Microsoft.Build.Shared;
using Xunit;



namespace Microsoft.Build.UnitTests
{
    public sealed class NativeMethodsShared_Tests
    {
        #region Data

        // Create a delegate to test the GetProcessId method when using GetProcAddress
        private delegate uint GetProcessIdDelegate();

        #endregion

        #region Tests

        /// <summary>
        /// Confirms we can find a file on the system path.
        /// </summary>
        [Fact]
        public void FindFileOnPath()
        {
            string expectedCmdPath;
            string shellName;
            if (NativeMethodsShared.IsWindows)
            {
#if FEATURE_SPECIAL_FOLDERS
                expectedCmdPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "cmd.exe");
#else
                expectedCmdPath = Path.Combine(FileUtilities.GetFolderPath(FileUtilities.SpecialFolder.System), "cmd.exe");
#endif
                shellName = "cmd.exe";
            }
            else
            {
                expectedCmdPath = "/bin/sh";
                shellName = "sh";
            }

            string cmdPath = NativeMethodsShared.FindOnPath(shellName);
            Assert.NotNull(cmdPath);

            // for the NUnit "Standard Out" tab
            Console.WriteLine("Expected location of \"" + shellName + "\": " + expectedCmdPath);
            Console.WriteLine("Found \"" + shellName + "\" here: " + cmdPath);

            Assert.Equal(0, String.Compare(cmdPath, expectedCmdPath, StringComparison.OrdinalIgnoreCase));
        }

        /// <summary>
        /// Confirms we can find a file on the system path even if the path
        /// to the file is very long.
        /// </summary>
        [Fact]
        public void FindFileOnPathAfterResizingBuffer()
        {
            int savedMaxPath = NativeMethodsShared.MAX_PATH;

            try
            {
                // make the default buffer size very small -- intentionally don't use
                // zero, otherwise StringBuilder will use some default larger capacity
                NativeMethodsShared.MAX_PATH = 1;

                FindFileOnPath();
            }
            finally
            {
                NativeMethodsShared.MAX_PATH = savedMaxPath;
            }
        }
        /// <summary>
        /// Confirms we cannot find a bogus file on the system path.
        /// </summary>
        [Fact]
        public void DoNotFindFileOnPath()
        {
            string bogusFile = Path.ChangeExtension(Guid.NewGuid().ToString(), ".txt");<commit_msg>Disable FindFileOnPathAfterResizingBuffer test<commit_after>// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;

using Microsoft.Build.Shared;
using Xunit;



namespace Microsoft.Build.UnitTests
{
    public sealed class NativeMethodsShared_Tests
    {
        #region Data

        // Create a delegate to test the GetProcessId method when using GetProcAddress
        private delegate uint GetProcessIdDelegate();

        #endregion

        #region Tests

        /// <summary>
        /// Confirms we can find a file on the system path.
        /// </summary>
        [Fact]
        public void FindFileOnPath()
        {
            string expectedCmdPath;
            string shellName;
            if (NativeMethodsShared.IsWindows)
            {
#if FEATURE_SPECIAL_FOLDERS
                expectedCmdPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "cmd.exe");
#else
                expectedCmdPath = Path.Combine(FileUtilities.GetFolderPath(FileUtilities.SpecialFolder.System), "cmd.exe");
#endif
                shellName = "cmd.exe";
            }
            else
            {
                expectedCmdPath = "/bin/sh";
                shellName = "sh";
            }

            string cmdPath = NativeMethodsShared.FindOnPath(shellName);
            Assert.NotNull(cmdPath);

            // for the NUnit "Standard Out" tab
            Console.WriteLine("Expected location of \"" + shellName + "\": " + expectedCmdPath);
            Console.WriteLine("Found \"" + shellName + "\" here: " + cmdPath);

            Assert.Equal(0, String.Compare(cmdPath, expectedCmdPath, StringComparison.OrdinalIgnoreCase));
        }

        /// <summary>
        /// Confirms we can find a file on the system path even if the path
        /// to the file is very long.
        /// </summary>
        [Fact(Skip = "This test causes crashes on CoreCLR because it alters MAX_PATH, causing a buffer overflow in SHGetFolderPathW.")]
        public void FindFileOnPathAfterResizingBuffer()
        {
            int savedMaxPath = NativeMethodsShared.MAX_PATH;

            try
            {
                // make the default buffer size very small -- intentionally don't use
                // zero, otherwise StringBuilder will use some default larger capacity
                NativeMethodsShared.MAX_PATH = 1;

                FindFileOnPath();
            }
            finally
            {
                NativeMethodsShared.MAX_PATH = savedMaxPath;
            }
        }
        /// <summary>
        /// Confirms we cannot find a bogus file on the system path.
        /// </summary>
        [Fact]
        public void DoNotFindFileOnPath()
        {
            string bogusFile = Path.ChangeExtension(Guid.NewGuid().ToString(), ".txt");
{'fraction_non_alphanumeric': 0.08810344827586207, 'fraction_numerical': 0.000689655172413793, 'mean_word_length': 1.8703612073231073, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5556925', 'n_tokens_mistral': 1458, 'n_tokens_neox': 1403, 'n_words': 417}