PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/interfaces/OSPlatform.cs

#
C# | 329 lines | 179 code | 40 blank | 110 comment | 77 complexity | 3473ecc5115fe01fb92dd4ec85085834 MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // Copyright 2008, Charlie Poole
  3. // This is free software licensed under the NUnit license. You may
  4. // obtain a copy of the license at http://nunit.org
  5. // ****************************************************************
  6. using System;
  7. using System.Runtime.InteropServices;
  8. namespace NUnit.Core
  9. {
  10. /// <summary>
  11. /// OSPlatform represents a particular operating system platform
  12. /// </summary>
  13. public class OSPlatform
  14. {
  15. PlatformID platform;
  16. Version version;
  17. ProductType product;
  18. #region Static Members
  19. private static OSPlatform currentPlatform;
  20. /// <summary>
  21. /// Platform ID for Unix as defined by Microsoft .NET 2.0 and greater
  22. /// </summary>
  23. public static readonly PlatformID UnixPlatformID_Microsoft = (PlatformID)4;
  24. /// <summary>
  25. /// Platform ID for Unix as defined by Mono
  26. /// </summary>
  27. public static readonly PlatformID UnixPlatformID_Mono = (PlatformID)128;
  28. /// <summary>
  29. /// Get the OSPlatform under which we are currently running
  30. /// </summary>
  31. public static OSPlatform CurrentPlatform
  32. {
  33. get
  34. {
  35. if (currentPlatform == null)
  36. {
  37. OperatingSystem os = Environment.OSVersion;
  38. if (os.Platform == PlatformID.Win32NT && os.Version.Major >= 5)
  39. {
  40. OSVERSIONINFOEX osvi = new OSVERSIONINFOEX();
  41. osvi.dwOSVersionInfoSize = (uint)Marshal.SizeOf(osvi);
  42. GetVersionEx(ref osvi);
  43. currentPlatform = new OSPlatform(os.Platform, os.Version, (ProductType)osvi.ProductType);
  44. }
  45. else
  46. currentPlatform = new OSPlatform(os.Platform, os.Version);
  47. }
  48. return currentPlatform;
  49. }
  50. }
  51. #endregion
  52. #region Members used for Win32NT platform only
  53. /// <summary>
  54. /// Product Type Enumeration used for Windows
  55. /// </summary>
  56. public enum ProductType
  57. {
  58. /// <summary>
  59. /// Product type is unknown or unspecified
  60. /// </summary>
  61. Unknown,
  62. /// <summary>
  63. /// Product type is Workstation
  64. /// </summary>
  65. WorkStation,
  66. /// <summary>
  67. /// Product type is Domain Controller
  68. /// </summary>
  69. DomainController,
  70. /// <summary>
  71. /// Product type is Server
  72. /// </summary>
  73. Server,
  74. }
  75. [StructLayout(LayoutKind.Sequential)]
  76. struct OSVERSIONINFOEX
  77. {
  78. public uint dwOSVersionInfoSize;
  79. public uint dwMajorVersion;
  80. public uint dwMinorVersion;
  81. public uint dwBuildNumber;
  82. public uint dwPlatformId;
  83. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  84. public string szCSDVersion;
  85. public Int16 wServicePackMajor;
  86. public Int16 wServicePackMinor;
  87. public Int16 wSuiteMask;
  88. public Byte ProductType;
  89. public Byte Reserved;
  90. }
  91. [DllImport("Kernel32.dll")]
  92. private static extern bool GetVersionEx(ref OSVERSIONINFOEX osvi);
  93. #endregion
  94. /// <summary>
  95. /// Construct from a platform ID and version
  96. /// </summary>
  97. public OSPlatform(PlatformID platform, Version version)
  98. {
  99. this.platform = platform;
  100. this.version = version;
  101. }
  102. /// <summary>
  103. /// Construct from a platform ID, version and product type
  104. /// </summary>
  105. public OSPlatform(PlatformID platform, Version version, ProductType product)
  106. : this( platform, version )
  107. {
  108. this.product = product;
  109. }
  110. /// <summary>
  111. /// Get the platform ID of this instance
  112. /// </summary>
  113. public PlatformID Platform
  114. {
  115. get { return platform; }
  116. }
  117. /// <summary>
  118. /// Get the Version of this instance
  119. /// </summary>
  120. public Version Version
  121. {
  122. get { return version; }
  123. }
  124. /// <summary>
  125. /// Get the Product Type of this instance
  126. /// </summary>
  127. public ProductType Product
  128. {
  129. get { return product; }
  130. }
  131. /// <summary>
  132. /// Return true if this is a windows platform
  133. /// </summary>
  134. public bool IsWindows
  135. {
  136. get
  137. {
  138. return platform == PlatformID.Win32NT
  139. || platform == PlatformID.Win32Windows
  140. || platform == PlatformID.Win32S
  141. || platform == PlatformID.WinCE;
  142. }
  143. }
  144. /// <summary>
  145. /// Return true if this is a Unix or Linux platform
  146. /// </summary>
  147. public bool IsUnix
  148. {
  149. get
  150. {
  151. return platform == UnixPlatformID_Microsoft
  152. || platform == UnixPlatformID_Mono;
  153. }
  154. }
  155. /// <summary>
  156. /// Return true if the platform is Win32S
  157. /// </summary>
  158. public bool IsWin32S
  159. {
  160. get { return platform == PlatformID.Win32S; }
  161. }
  162. /// <summary>
  163. /// Return true if the platform is Win32Windows
  164. /// </summary>
  165. public bool IsWin32Windows
  166. {
  167. get { return platform == PlatformID.Win32Windows; }
  168. }
  169. /// <summary>
  170. /// Return true if the platform is Win32NT
  171. /// </summary>
  172. public bool IsWin32NT
  173. {
  174. get { return platform == PlatformID.Win32NT; }
  175. }
  176. /// <summary>
  177. /// Return true if the platform is Windows CE
  178. /// </summary>
  179. public bool IsWinCE
  180. {
  181. get { return (int)platform == 3; } // PlatformID.WinCE not defined in .NET 1.0
  182. }
  183. /// <summary>
  184. /// Return true if the platform is Windows 95
  185. /// </summary>
  186. public bool IsWin95
  187. {
  188. get { return platform == PlatformID.Win32Windows && version.Major == 4 && version.Minor == 0; }
  189. }
  190. /// <summary>
  191. /// Return true if the platform is Windows 98
  192. /// </summary>
  193. public bool IsWin98
  194. {
  195. get { return platform == PlatformID.Win32Windows && version.Major == 4 && version.Minor == 10; }
  196. }
  197. /// <summary>
  198. /// Return true if the platform is Windows ME
  199. /// </summary>
  200. public bool IsWinME
  201. {
  202. get { return platform == PlatformID.Win32Windows && version.Major == 4 && version.Minor == 90; }
  203. }
  204. /// <summary>
  205. /// Return true if the platform is NT 3
  206. /// </summary>
  207. public bool IsNT3
  208. {
  209. get { return platform == PlatformID.Win32NT && version.Major == 3; }
  210. }
  211. /// <summary>
  212. /// Return true if the platform is NT 4
  213. /// </summary>
  214. public bool IsNT4
  215. {
  216. get { return platform == PlatformID.Win32NT && version.Major == 4; }
  217. }
  218. /// <summary>
  219. /// Return true if the platform is NT 5
  220. /// </summary>
  221. public bool IsNT5
  222. {
  223. get { return platform == PlatformID.Win32NT && version.Major == 5; }
  224. }
  225. /// <summary>
  226. /// Return true if the platform is Windows 2000
  227. /// </summary>
  228. public bool IsWin2K
  229. {
  230. get { return IsNT5 && version.Minor == 0; }
  231. }
  232. /// <summary>
  233. /// Return true if the platform is Windows XP
  234. /// </summary>
  235. public bool IsWinXP
  236. {
  237. get { return IsNT5 && version.Minor == 1 || (version.Minor == 2 && Product == ProductType.WorkStation); }
  238. }
  239. /// <summary>
  240. /// Return true if the platform is Windows 2003 Server
  241. /// </summary>
  242. public bool IsWin2003Server
  243. {
  244. get { return IsNT5 && version.Minor == 2 && Product == ProductType.Server; }
  245. }
  246. /// <summary>
  247. /// Return true if the platform is NT 6
  248. /// </summary>
  249. public bool IsNT6
  250. {
  251. get { return platform == PlatformID.Win32NT && version.Major == 6; }
  252. }
  253. /// <summary>
  254. /// Return true if the platform is Vista
  255. /// </summary>
  256. public bool IsVista
  257. {
  258. get { return IsNT6 && version.Minor == 0 && Product == ProductType.WorkStation; }
  259. }
  260. /// <summary>
  261. /// Return true if the platform is Windows 2008 Server (original or R2)
  262. /// </summary>
  263. public bool IsWin2008Server
  264. {
  265. get { return IsNT6 && Product == ProductType.Server; }
  266. }
  267. /// <summary>
  268. /// Return true if the platform is Windows 2008 Server (original)
  269. /// </summary>
  270. public bool IsWin2008ServerR1
  271. {
  272. get { return IsNT6 && version.Minor == 0 && Product == ProductType.Server; }
  273. }
  274. /// <summary>
  275. /// Return true if the platform is Windows 2008 Server R2
  276. /// </summary>
  277. public bool IsWin2008ServerR2
  278. {
  279. get { return IsNT6 && version.Minor == 1 && Product == ProductType.Server; }
  280. }
  281. /// <summary>
  282. /// Return true if the platform is Windows 7
  283. /// </summary>
  284. public bool IsWindows7
  285. {
  286. get { return IsNT6 && version.Minor == 1 && Product == ProductType.WorkStation; }
  287. }
  288. }
  289. }