[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Internal / OSPlatform.cs
1 // ***********************************************************************
2 // Copyright (c) 2008-2016 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 #define PORTABLE
24 #define TIZEN
25 #define NUNIT_FRAMEWORK
26 #define NUNITLITE
27 #define NET_4_5
28 #define PARALLEL
29 #if !PORTABLE
30 using Microsoft.Win32;
31 using System;
32 using System.Runtime.InteropServices;
33
34 namespace NUnit.Framework.Internal
35 {
36     /// <summary>
37     /// OSPlatform represents a particular operating system platform
38     /// </summary>
39     public class OSPlatform
40     {
41         readonly PlatformID _platform;
42         readonly Version _version;
43         readonly ProductType _product;
44
45 #region Static Members
46         private static readonly Lazy<OSPlatform> currentPlatform = new Lazy<OSPlatform> (() =>
47         {
48             OSPlatform currentPlatform;
49
50             OperatingSystem os = Environment.OSVersion;
51
52 #if SILVERLIGHT || NETCF
53             // TODO: Runtime silverlight detection?
54             currentPlatform = new OSPlatform(os.Platform, os.Version);
55 #else
56             if (os.Platform == PlatformID.Win32NT && os.Version.Major >= 5)
57             {
58                 OSVERSIONINFOEX osvi = new OSVERSIONINFOEX();
59                 osvi.dwOSVersionInfoSize = (uint)Marshal.SizeOf(osvi);
60                 GetVersionEx(ref osvi);
61                 if (os.Version.Major == 6 && os.Version.Minor >= 2)
62                     os = new OperatingSystem(os.Platform, GetWindows81PlusVersion(os.Version));
63                 currentPlatform = new OSPlatform(os.Platform, os.Version, (ProductType)osvi.ProductType);
64             }
65             else if (CheckIfIsMacOSX(os.Platform))
66             {
67                 // Mono returns PlatformID.Unix for OSX (see http://www.mono-project.com/docs/faq/technical/#how-to-detect-the-execution-platform)
68                 // The above check uses uname to confirm it is MacOSX and we change the PlatformId here.
69                 currentPlatform = new OSPlatform(PlatformID.MacOSX, os.Version);
70             }
71             else
72                 currentPlatform = new OSPlatform(os.Platform, os.Version);
73 #endif
74             return currentPlatform;
75         });
76
77
78         /// <summary>
79         /// Platform ID for Unix as defined by Microsoft .NET 2.0 and greater
80         /// </summary>
81         public static readonly PlatformID UnixPlatformID_Microsoft = (PlatformID)4;
82
83         /// <summary>
84         /// Platform ID for Unix as defined by Mono
85         /// </summary>
86         public static readonly PlatformID UnixPlatformID_Mono = (PlatformID)128;
87
88         /// <summary>
89         /// Platform ID for XBox as defined by .NET and Mono, but not CF
90         /// </summary>
91         public static readonly PlatformID XBoxPlatformID = (PlatformID)5;
92
93         /// <summary>
94         /// Platform ID for MacOSX as defined by .NET and Mono, but not CF
95         /// </summary>
96         public static readonly PlatformID MacOSXPlatformID = (PlatformID)6;
97
98         /// <summary>
99         /// Get the OSPlatform under which we are currently running
100         /// </summary>
101         public static OSPlatform CurrentPlatform
102         {
103             get
104             {
105                 return currentPlatform.Value;
106             }
107         }
108
109 #if !SILVERLIGHT && !NETCF
110         /// <summary>
111         /// Gets the actual OS Version, not the incorrect value that might be 
112         /// returned for Win 8.1 and Win 10
113         /// </summary>
114         /// <remarks>
115         /// If an application is not manifested as Windows 8.1 or Windows 10,
116         /// the version returned from Environment.OSVersion will not be 6.3 and 10.0
117         /// respectively, but will be 6.2 and 6.3. The correct value can be found in
118         /// the registry.
119         /// </remarks>
120         /// <param name="version">The original version</param>
121         /// <returns>The correct OS version</returns>
122         private static Version GetWindows81PlusVersion(Version version)
123         {
124             try
125             {
126                 using (var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion"))
127                 {
128                     if (key != null)
129                     {
130                         var buildStr = key.GetValue("CurrentBuildNumber") as string;
131                         int build = 0;
132                         int.TryParse(buildStr, out build);
133
134                         // These two keys are in Windows 10 only and are DWORDS
135                         var major = key.GetValue("CurrentMajorVersionNumber") as int?;
136                         var minor = key.GetValue("CurrentMinorVersionNumber") as int?;
137                         if (major.HasValue && minor.HasValue)
138                         {
139                             return new Version(major.Value, minor.Value, build);
140                         }
141
142                         // If we get here, we are not Windows 10, so we are Windows 8
143                         // or 8.1. 8.1 might report itself as 6.2, but will have 6.3
144                         // in the registry. We can't do this earlier because for backwards
145                         // compatibility, Windows 10 also has 6.3 for this key.
146                         var currentVersion = key.GetValue("CurrentVersion") as string;
147                         if(currentVersion == "6.3")
148                         {
149                             return new Version(6, 3, build);
150                         }
151                     }
152                 }
153             }
154             catch (Exception)
155             {
156             }
157             return version;
158         }
159 #endif
160 #endregion
161
162 #region Members used for Win32NT platform only
163         /// <summary>
164         /// Product Type Enumeration used for Windows
165         /// </summary>
166         public enum ProductType
167         {
168             /// <summary>
169             /// Product type is unknown or unspecified
170             /// </summary>
171             Unknown,
172
173             /// <summary>
174             /// Product type is Workstation
175             /// </summary>
176             WorkStation,
177
178             /// <summary>
179             /// Product type is Domain Controller
180             /// </summary>
181             DomainController,
182
183             /// <summary>
184             /// Product type is Server
185             /// </summary>
186             Server,
187         }
188
189         [StructLayout(LayoutKind.Sequential)]
190         struct OSVERSIONINFOEX
191         {
192             public uint dwOSVersionInfoSize;
193             public readonly uint dwMajorVersion;
194             public readonly uint dwMinorVersion;
195             public readonly uint dwBuildNumber;
196             public readonly uint dwPlatformId;
197             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
198             public readonly string szCSDVersion;
199             public readonly Int16 wServicePackMajor;
200             public readonly Int16 wServicePackMinor;
201             public readonly Int16 wSuiteMask;
202             public readonly Byte ProductType;
203             public readonly Byte Reserved;
204         }
205
206         [DllImport("Kernel32.dll")]
207         private static extern bool GetVersionEx(ref OSVERSIONINFOEX osvi);
208 #endregion
209
210         /// <summary>
211         /// Construct from a platform ID and version
212         /// </summary>
213         public OSPlatform(PlatformID platform, Version version)
214         {
215             _platform = platform;
216             _version = version;
217         }
218
219         /// <summary>
220         /// Construct from a platform ID, version and product type
221         /// </summary>
222         public OSPlatform(PlatformID platform, Version version, ProductType product)
223             : this( platform, version )
224         {
225             _product = product;
226         }
227
228         /// <summary>
229         /// Get the platform ID of this instance
230         /// </summary>
231         public PlatformID Platform
232         {
233             get { return _platform; }
234         }
235
236         /// <summary>
237         /// Get the Version of this instance
238         /// </summary>
239         public Version Version
240         {
241             get { return _version; }
242         }
243
244         /// <summary>
245         /// Get the Product Type of this instance
246         /// </summary>
247         public ProductType Product
248         {
249             get { return _product; }
250         }
251
252         /// <summary>
253         /// Return true if this is a windows platform
254         /// </summary>
255         public bool IsWindows
256         {
257             get
258             {
259                 return _platform == PlatformID.Win32NT
260                     || _platform == PlatformID.Win32Windows
261                     || _platform == PlatformID.Win32S
262                     || _platform == PlatformID.WinCE;
263             }
264         }
265
266         /// <summary>
267         /// Return true if this is a Unix or Linux platform
268         /// </summary>
269         public bool IsUnix
270         {
271             get
272             {
273                 return _platform == UnixPlatformID_Microsoft
274                     || _platform == UnixPlatformID_Mono;
275             }
276         }
277
278         /// <summary>
279         /// Return true if the platform is Win32S
280         /// </summary>
281         public bool IsWin32S
282         {
283             get { return _platform == PlatformID.Win32S; }
284         }
285
286         /// <summary>
287         /// Return true if the platform is Win32Windows
288         /// </summary>
289         public bool IsWin32Windows
290         {
291             get { return _platform == PlatformID.Win32Windows; }
292         }
293
294         /// <summary>
295         ///  Return true if the platform is Win32NT
296         /// </summary>
297         public bool IsWin32NT
298         {
299             get { return _platform == PlatformID.Win32NT; }
300         }
301
302         /// <summary>
303         /// Return true if the platform is Windows CE
304         /// </summary>
305         public bool IsWinCE
306         {
307             get { return _platform == PlatformID.WinCE; }
308         }
309         
310         /// <summary>
311         /// Return true if the platform is Xbox
312         /// </summary>
313         public bool IsXbox
314         {
315             get { return _platform == XBoxPlatformID; }
316         }
317
318         /// <summary>
319         /// Return true if the platform is MacOSX
320         /// </summary>
321         public bool IsMacOSX 
322         {
323             get { return _platform == MacOSXPlatformID; }
324         }
325
326 #if !NETCF && !SILVERLIGHT
327         [DllImport("libc")]
328         static extern int uname(IntPtr buf);
329
330         static bool CheckIfIsMacOSX(PlatformID platform)
331         {
332             if (platform == PlatformID.MacOSX)
333             return true;
334
335             if (platform != PlatformID.Unix)
336                 return false;
337
338             IntPtr buf = Marshal.AllocHGlobal(8192);
339             bool isMacOSX = false;
340             if (uname(buf) == 0)
341             {
342                 string os = Marshal.PtrToStringAnsi(buf);
343                 isMacOSX = os.Equals("Darwin");
344             }
345             Marshal.FreeHGlobal(buf);
346             return isMacOSX;
347         }
348 #endif
349
350         /// <summary>
351         /// Return true if the platform is Windows 95
352         /// </summary>
353         public bool IsWin95
354         {
355             get { return _platform == PlatformID.Win32Windows && _version.Major == 4 && _version.Minor == 0; }
356         }
357
358         /// <summary>
359         /// Return true if the platform is Windows 98
360         /// </summary>
361         public bool IsWin98
362         {
363             get { return _platform == PlatformID.Win32Windows && _version.Major == 4 && _version.Minor == 10; }
364         }
365
366         /// <summary>
367         /// Return true if the platform is Windows ME
368         /// </summary>
369         public bool IsWinME
370         {
371             get { return _platform == PlatformID.Win32Windows && _version.Major == 4 && _version.Minor == 90; }
372         }
373
374         /// <summary>
375         /// Return true if the platform is NT 3
376         /// </summary>
377         public bool IsNT3
378         {
379             get { return _platform == PlatformID.Win32NT && _version.Major == 3; }
380         }
381
382         /// <summary>
383         /// Return true if the platform is NT 4
384         /// </summary>
385         public bool IsNT4
386         {
387             get { return _platform == PlatformID.Win32NT && _version.Major == 4; }
388         }
389
390         /// <summary>
391         /// Return true if the platform is NT 5
392         /// </summary>
393         public bool IsNT5
394         {
395             get { return _platform == PlatformID.Win32NT && _version.Major == 5; }
396         }
397
398         /// <summary>
399         /// Return true if the platform is Windows 2000
400         /// </summary>
401         public bool IsWin2K
402         {
403             get { return IsNT5 && _version.Minor == 0; }
404         }
405
406         /// <summary>
407         /// Return true if the platform is Windows XP
408         /// </summary>
409         public bool IsWinXP
410         {
411             get { return IsNT5 && (_version.Minor == 1  || _version.Minor == 2 && Product == ProductType.WorkStation); }
412         }
413
414         /// <summary>
415         /// Return true if the platform is Windows 2003 Server
416         /// </summary>
417         public bool IsWin2003Server
418         {
419             get { return IsNT5 && _version.Minor == 2 && Product == ProductType.Server; }
420         }
421
422         /// <summary>
423         /// Return true if the platform is NT 6
424         /// </summary>
425         public bool IsNT6
426         {
427             get { return _platform == PlatformID.Win32NT && _version.Major == 6; }
428         }
429
430         /// <summary>
431         /// Return true if the platform is NT 6.0
432         /// </summary>
433         public bool IsNT60
434         {
435             get { return IsNT6 && _version.Minor == 0; }
436         }
437
438         /// <summary>
439         /// Return true if the platform is NT 6.1
440         /// </summary>
441         public bool IsNT61
442         {
443             get { return IsNT6 && _version.Minor == 1; }
444         }
445
446         /// <summary>
447         /// Return true if the platform is NT 6.2
448         /// </summary>
449         public bool IsNT62
450         {
451             get { return IsNT6 && _version.Minor == 2; }
452         }
453
454         /// <summary>
455         /// Return true if the platform is NT 6.3
456         /// </summary>
457         public bool IsNT63
458         {
459             get { return IsNT6 && _version.Minor == 3; }
460         }
461
462         /// <summary>
463         /// Return true if the platform is Vista
464         /// </summary>
465         public bool IsVista
466         {
467             get { return IsNT60 && Product == ProductType.WorkStation; }
468         }
469
470         /// <summary>
471         /// Return true if the platform is Windows 2008 Server (original or R2)
472         /// </summary>
473         public bool IsWin2008Server
474         {
475             get { return IsWin2008ServerR1 || IsWin2008ServerR2; }
476         }
477
478         /// <summary>
479         /// Return true if the platform is Windows 2008 Server (original)
480         /// </summary>
481         public bool IsWin2008ServerR1
482         {
483             get { return IsNT60 && Product == ProductType.Server; }
484         }
485
486         /// <summary>
487         /// Return true if the platform is Windows 2008 Server R2
488         /// </summary>
489         public bool IsWin2008ServerR2
490         {
491             get { return IsNT61 && Product == ProductType.Server; }
492         }
493
494         /// <summary>
495         /// Return true if the platform is Windows 2012 Server (original or R2)
496         /// </summary>
497         public bool IsWin2012Server
498         {
499             get { return IsWin2012ServerR1 || IsWin2012ServerR2; }
500         }
501
502         /// <summary>
503         /// Return true if the platform is Windows 2012 Server (original)
504         /// </summary>
505         public bool IsWin2012ServerR1
506         {
507             get { return IsNT62 && Product == ProductType.Server; }
508         }
509
510         /// <summary>
511         /// Return true if the platform is Windows 2012 Server R2
512         /// </summary>
513         public bool IsWin2012ServerR2
514         {
515             get { return IsNT63 && Product == ProductType.Server; }
516         }
517
518         /// <summary>
519         /// Return true if the platform is Windows 7
520         /// </summary>
521         public bool IsWindows7
522         {
523             get { return IsNT61 && Product == ProductType.WorkStation; }
524         }
525
526         /// <summary>
527         /// Return true if the platform is Windows 8
528         /// </summary>
529         public bool IsWindows8
530         {
531             get { return IsNT62 && Product == ProductType.WorkStation; }
532         }
533
534         /// <summary>
535         /// Return true if the platform is Windows 8.1
536         /// </summary>
537         public bool IsWindows81
538         {
539             get { return IsNT63 && Product == ProductType.WorkStation; }
540         }
541
542         /// <summary>
543         /// Return true if the platform is Windows 10
544         /// </summary>
545         public bool IsWindows10
546         {
547             get { return _platform == PlatformID.Win32NT && _version.Major == 10 && Product == ProductType.WorkStation; }
548         }
549
550         /// <summary>
551         /// Return true if the platform is Windows Server. This is named Windows
552         /// Server 10 to distinguish it from previous versions of Windows Server.
553         /// </summary>
554         public bool IsWindowsServer10
555         {
556             get { return _platform == PlatformID.Win32NT && _version.Major == 10 && Product == ProductType.Server; }
557         }
558     }
559 }
560 #endif