MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System;5using System.Diagnostics;6using System.IO;7using System.Management.Automation;8using Xunit;9 10namespace PSTests.Parallel11{12 public static class PlatformTests13 {14 [Fact]15 public static void TestIsCoreCLR()16 {17 Assert.True(Platform.IsCoreCLR);18 }19 20#if Unix21 [Fact]22 public static void TestGetUserName()23 {24 var startInfo = new ProcessStartInfo25 {26 FileName = @"/usr/bin/env",27 Arguments = "whoami",28 RedirectStandardOutput = true,29 UseShellExecute = false30 };31 using (Process process = Process.Start(startInfo))32 {33 // Get output of call to whoami without trailing newline34 string username = process.StandardOutput.ReadToEnd().Trim();35 process.WaitForExit();36 37 // The process should return an exit code of 0 on success38 Assert.Equal(0, process.ExitCode);39 Assert.Equal(username, Environment.UserName);40 }41 }42 43 [Fact]44 public static void TestGetMachineName()45 {46 var startInfo = new ProcessStartInfo47 {48 FileName = @"/usr/bin/env",49 Arguments = "hostname",50 RedirectStandardOutput = true,51 UseShellExecute = false52 };53 using (Process process = Process.Start(startInfo))54 {55 // Get output of call to hostname without trailing newline56 string hostname = process.StandardOutput.ReadToEnd().Trim();57 process.WaitForExit();58 59 // The process should return an exit code of 0 on success60 Assert.Equal(0, process.ExitCode);61 // It should be the same as what our platform code returns62 Assert.Equal(hostname, Environment.MachineName);63 }64 }65 66 [Fact]67 public static void TestGetFQDN()68 {69 var startInfo = new ProcessStartInfo70 {71 FileName = @"/usr/bin/env",72 Arguments = "hostname --fqdn",73 RedirectStandardOutput = true,74 UseShellExecute = false75 };76 using (Process process = Process.Start(startInfo))77 {78 // Get output of call to hostname without trailing newline79 string hostname = process.StandardOutput.ReadToEnd().Trim();80 process.WaitForExit();81 82 // The process should return an exit code of 0 on success83 Assert.Equal(0, process.ExitCode);84 // It should be the same as what our platform code returns85 Assert.Equal(hostname, Platform.NonWindowsGetHostName());86 }87 }88 89 [Fact]90 public static void TestIsExecutable()91 {92 Assert.True(Platform.NonWindowsIsExecutable("/bin/ls"));93 }94 95 [Fact]96 public static void TestIsNotExecutable()97 {98 Assert.False(Platform.NonWindowsIsExecutable("/etc/hosts"));99 }100 101 [Fact]102 public static void TestDirectoryIsNotExecutable()103 {104 Assert.False(Platform.NonWindowsIsExecutable("/etc"));105 }106 107 [Fact]108 public static void TestFileIsNotHardLink()109 {110 string path = @"/tmp/nothardlink";111 if (File.Exists(path))112 {113 File.Delete(path);114 }115 116 File.Create(path);117 118 FileSystemInfo fd = new FileInfo(path);119 120 // Since this is the only reference to the file, it is not considered a121 // hardlink by our API (though all files are hardlinks on Linux)122 Assert.False(Platform.NonWindowsIsHardLink(fd));123 124 File.Delete(path);125 }126 127 [Fact]128 public static void TestFileIsHardLink()129 {130 string path = @"/tmp/originallink";131 if (File.Exists(path))132 {133 File.Delete(path);134 }135 136 File.Create(path);137 138 string link = "/tmp/newlink";139 140 if (File.Exists(link))141 {142 File.Delete(link);143 }144 145 var startInfo = new ProcessStartInfo146 {147 FileName = @"/usr/bin/env",148 Arguments = "ln " + path + " " + link,149 RedirectStandardOutput = true,150 UseShellExecute = false151 };152 153 using (Process process = Process.Start(startInfo))154 {155 process.WaitForExit();156 Assert.Equal(0, process.ExitCode);157 }158 159 // Since there are now two references to the file, both are considered160 // hardlinks by our API (though all files are hardlinks on Linux)161 FileSystemInfo fd = new FileInfo(path);162 Assert.True(Platform.NonWindowsIsHardLink(fd));163 164 fd = new FileInfo(link);165 Assert.True(Platform.NonWindowsIsHardLink(fd));166 167 File.Delete(path);168 File.Delete(link);169 }170 171 [Fact]172 public static void TestDirectoryIsNotHardLink()173 {174 string path = @"/tmp";175 176 FileSystemInfo fd = new FileInfo(path);177 178 Assert.False(Platform.NonWindowsIsHardLink(fd));179 }180 181 [Fact]182 public static void TestNonExistentIsHardLink()183 {184 // A file that should *never* exist on a test machine:185 string path = @"/tmp/ThisFileShouldNotExistOnTestMachines";186 187 // If the file exists, then there's a larger issue that needs to be looked at188 Assert.False(File.Exists(path));189 190 // Convert `path` string to FileSystemInfo data type. And now, it should return true191 FileSystemInfo fd = new FileInfo(path);192 Assert.False(Platform.NonWindowsIsHardLink(fd));193 }194 195 [Fact]196 public static void TestFileIsSymLink()197 {198 string path = @"/tmp/originallink";199 if (File.Exists(path))200 {201 File.Delete(path);202 }203 204 File.Create(path);205 206 string link = "/tmp/newlink";207 208 if (File.Exists(link))209 {210 File.Delete(link);211 }212 213 var startInfo = new ProcessStartInfo214 {215 FileName = @"/usr/bin/env",216 Arguments = "ln -s " + path + " " + link,217 RedirectStandardOutput = true,218 UseShellExecute = false219 };220 221 using (Process process = Process.Start(startInfo))222 {223 process.WaitForExit();224 Assert.Equal(0, process.ExitCode);225 }226 227 FileSystemInfo fd = new FileInfo(path);228 Assert.False(Platform.NonWindowsIsSymLink(fd));229 230 fd = new FileInfo(link);231 Assert.True(Platform.NonWindowsIsSymLink(fd));232 233 File.Delete(path);234 File.Delete(link);235 }236#endif237 }238}239 