PageRenderTime 180ms CodeModel.GetById 164ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 1ms

/src/NUnit/interfaces/OSPlatform.cs

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