1 // *********************************************************************** 2 // Copyright (c) 2008 Charlie Poole 3 // 4 // Permission is hereby granted, free of charge, to any person obtaining 5 // a copy of this software and associated documentation files (the 6 // "Software"), to deal in the Software without restriction, including 7 // without limitation the rights to use, copy, modify, merge, publish, 8 // distribute, sublicense, and/or sell copies of the Software, and to 9 // permit persons to whom the Software is furnished to do so, subject to 10 // the following conditions: 11 // 12 // The above copyright notice and this permission notice shall be 13 // included in all copies or substantial portions of the Software. 14 // 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 // *********************************************************************** 23 24 using System; 25 using System.Runtime.InteropServices; 26 27 namespace NUnit.Framework.Internal 28 { 29 /// <summary> 30 /// OSPlatform represents a particular operating system platform 31 /// </summary> 32 public class OSPlatform 33 { 34 PlatformID platform; 35 Version version; 36 ProductType product; 37 38 #region Static Members 39 private static OSPlatform currentPlatform; 40 41 42 /// <summary> 43 /// Platform ID for Unix as defined by Microsoft .NET 2.0 and greater 44 /// </summary> 45 public static readonly PlatformID UnixPlatformID_Microsoft = (PlatformID)4; 46 47 /// <summary> 48 /// Platform ID for Unix as defined by Mono 49 /// </summary> 50 public static readonly PlatformID UnixPlatformID_Mono = (PlatformID)128; 51 52 /// <summary> 53 /// Get the OSPlatform under which we are currently running 54 /// </summary> 55 public static OSPlatform CurrentPlatform 56 { 57 get 58 { 59 if (currentPlatform == null) 60 { 61 OperatingSystem os = Environment.OSVersion; 62 63 #if SILVERLIGHT || __MOBILE__ 64 // TODO: Runtime silverlight detection? 65 currentPlatform = new OSPlatform(os.Platform, os.Version); 66 #else 67 if (os.Platform == PlatformID.Win32NT && os.Version.Major >= 5) 68 { 69 OSVERSIONINFOEX osvi = new OSVERSIONINFOEX(); 70 osvi.dwOSVersionInfoSize = (uint)Marshal.SizeOf(osvi); 71 GetVersionEx(ref osvi); 72 currentPlatform = new OSPlatform(os.Platform, os.Version, (ProductType)osvi.ProductType); 73 } 74 else 75 currentPlatform = new OSPlatform(os.Platform, os.Version); 76 #endif 77 } 78 79 return currentPlatform; 80 } 81 } 82 #endregion 83 84 #region Members used for Win32NT platform only 85 /// <summary> 86 /// Product Type Enumeration used for Windows 87 /// </summary> 88 public enum ProductType 89 { 90 /// <summary> 91 /// Product type is unknown or unspecified 92 /// </summary> 93 Unknown, 94 95 /// <summary> 96 /// Product type is Workstation 97 /// </summary> 98 WorkStation, 99 100 /// <summary> 101 /// Product type is Domain Controller 102 /// </summary> 103 DomainController, 104 105 /// <summary> 106 /// Product type is Server 107 /// </summary> 108 Server, 109 } 110 111 #if !__MOBILE__ 112 [StructLayout(LayoutKind.Sequential)] 113 struct OSVERSIONINFOEX 114 { 115 public uint dwOSVersionInfoSize; 116 public uint dwMajorVersion; 117 public uint dwMinorVersion; 118 public uint dwBuildNumber; 119 public uint dwPlatformId; 120 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] 121 public string szCSDVersion; 122 public Int16 wServicePackMajor; 123 public Int16 wServicePackMinor; 124 public Int16 wSuiteMask; 125 public Byte ProductType; 126 public Byte Reserved; 127 } 128 129 [DllImport("Kernel32.dll")] GetVersionEx(ref OSVERSIONINFOEX osvi)130 private static extern bool GetVersionEx(ref OSVERSIONINFOEX osvi); 131 #endif 132 #endregion 133 134 /// <summary> 135 /// Construct from a platform ID and version 136 /// </summary> OSPlatform(PlatformID platform, Version version)137 public OSPlatform(PlatformID platform, Version version) 138 { 139 this.platform = platform; 140 this.version = version; 141 } 142 143 /// <summary> 144 /// Construct from a platform ID, version and product type 145 /// </summary> OSPlatform(PlatformID platform, Version version, ProductType product)146 public OSPlatform(PlatformID platform, Version version, ProductType product) 147 : this( platform, version ) 148 { 149 this.product = product; 150 } 151 152 /// <summary> 153 /// Get the platform ID of this instance 154 /// </summary> 155 public PlatformID Platform 156 { 157 get { return platform; } 158 } 159 160 /// <summary> 161 /// Get the Version of this instance 162 /// </summary> 163 public Version Version 164 { 165 get { return version; } 166 } 167 168 /// <summary> 169 /// Get the Product Type of this instance 170 /// </summary> 171 public ProductType Product 172 { 173 get { return product; } 174 } 175 176 /// <summary> 177 /// Return true if this is a windows platform 178 /// </summary> 179 public bool IsWindows 180 { 181 get 182 { 183 return platform == PlatformID.Win32NT 184 || platform == PlatformID.Win32Windows 185 || platform == PlatformID.Win32S 186 || platform == PlatformID.WinCE; 187 } 188 } 189 190 /// <summary> 191 /// Return true if this is a Unix or Linux platform 192 /// </summary> 193 public bool IsUnix 194 { 195 get 196 { 197 return platform == UnixPlatformID_Microsoft 198 || platform == UnixPlatformID_Mono; 199 } 200 } 201 202 /// <summary> 203 /// Return true if the platform is Win32S 204 /// </summary> 205 public bool IsWin32S 206 { 207 get { return platform == PlatformID.Win32S; } 208 } 209 210 /// <summary> 211 /// Return true if the platform is Win32Windows 212 /// </summary> 213 public bool IsWin32Windows 214 { 215 get { return platform == PlatformID.Win32Windows; } 216 } 217 218 /// <summary> 219 /// Return true if the platform is Win32NT 220 /// </summary> 221 public bool IsWin32NT 222 { 223 get { return platform == PlatformID.Win32NT; } 224 } 225 226 /// <summary> 227 /// Return true if the platform is Windows CE 228 /// </summary> 229 public bool IsWinCE 230 { 231 get { return (int)platform == 3; } // PlatformID.WinCE not defined in .NET 1.0 232 } 233 234 #if (CLR_2_0 || CLR_4_0) && !NETCF 235 /// <summary> 236 /// Return true if the platform is Xbox 237 /// </summary> 238 public bool IsXbox 239 { 240 get { return platform == PlatformID.Xbox; } 241 } 242 243 /// <summary> 244 /// Return true if the platform is MacOSX 245 /// </summary> 246 public bool IsMacOSX 247 { 248 get { return platform == PlatformID.MacOSX; } 249 } 250 #endif 251 252 /// <summary> 253 /// Return true if the platform is Windows 95 254 /// </summary> 255 public bool IsWin95 256 { 257 get { return platform == PlatformID.Win32Windows && version.Major == 4 && version.Minor == 0; } 258 } 259 260 /// <summary> 261 /// Return true if the platform is Windows 98 262 /// </summary> 263 public bool IsWin98 264 { 265 get { return platform == PlatformID.Win32Windows && version.Major == 4 && version.Minor == 10; } 266 } 267 268 /// <summary> 269 /// Return true if the platform is Windows ME 270 /// </summary> 271 public bool IsWinME 272 { 273 get { return platform == PlatformID.Win32Windows && version.Major == 4 && version.Minor == 90; } 274 } 275 276 /// <summary> 277 /// Return true if the platform is NT 3 278 /// </summary> 279 public bool IsNT3 280 { 281 get { return platform == PlatformID.Win32NT && version.Major == 3; } 282 } 283 284 /// <summary> 285 /// Return true if the platform is NT 4 286 /// </summary> 287 public bool IsNT4 288 { 289 get { return platform == PlatformID.Win32NT && version.Major == 4; } 290 } 291 292 /// <summary> 293 /// Return true if the platform is NT 5 294 /// </summary> 295 public bool IsNT5 296 { 297 get { return platform == PlatformID.Win32NT && version.Major == 5; } 298 } 299 300 /// <summary> 301 /// Return true if the platform is Windows 2000 302 /// </summary> 303 public bool IsWin2K 304 { 305 get { return IsNT5 && version.Minor == 0; } 306 } 307 308 /// <summary> 309 /// Return true if the platform is Windows XP 310 /// </summary> 311 public bool IsWinXP 312 { 313 get { return IsNT5 && (version.Minor == 1 || version.Minor == 2 && Product == ProductType.WorkStation); } 314 } 315 316 /// <summary> 317 /// Return true if the platform is Windows 2003 Server 318 /// </summary> 319 public bool IsWin2003Server 320 { 321 get { return IsNT5 && version.Minor == 2 && Product == ProductType.Server; } 322 } 323 324 /// <summary> 325 /// Return true if the platform is NT 6 326 /// </summary> 327 public bool IsNT6 328 { 329 get { return platform == PlatformID.Win32NT && version.Major == 6; } 330 } 331 332 /// <summary> 333 /// Return true if the platform is Vista 334 /// </summary> 335 public bool IsVista 336 { 337 get { return IsNT6 && version.Minor == 0 && Product == ProductType.WorkStation; } 338 } 339 340 /// <summary> 341 /// Return true if the platform is Windows 2008 Server (original or R2) 342 /// </summary> 343 public bool IsWin2008Server 344 { 345 get { return IsNT6 && Product == ProductType.Server; } 346 } 347 348 /// <summary> 349 /// Return true if the platform is Windows 2008 Server (original) 350 /// </summary> 351 public bool IsWin2008ServerR1 352 { 353 get { return IsNT6 && version.Minor == 0 && Product == ProductType.Server; } 354 } 355 356 /// <summary> 357 /// Return true if the platform is Windows 2008 Server R2 358 /// </summary> 359 public bool IsWin2008ServerR2 360 { 361 get { return IsNT6 && version.Minor == 1 && Product == ProductType.Server; } 362 } 363 364 /// <summary> 365 /// Return true if the platform is Windows 2012 Server 366 /// </summary> 367 public bool IsWin2012Server 368 { 369 get { return IsNT6 && version.Minor == 2 && Product == ProductType.Server; } 370 } 371 372 /// <summary> 373 /// Return true if the platform is Windows 7 374 /// </summary> 375 public bool IsWindows7 376 { 377 get { return IsNT6 && version.Minor == 1 && Product == ProductType.WorkStation; } 378 } 379 380 /// <summary> 381 /// Return true if the platform is Windows 8 382 /// </summary> 383 public bool IsWindows8 384 { 385 get { return IsNT6 && version.Minor == 8 && Product == ProductType.WorkStation; } 386 } 387 } 388 } 389