1 // ***********************************************************************
2 // Copyright (c) 2007-2016 Charlie Poole
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:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
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 // ***********************************************************************
25 #define NUNIT_FRAMEWORK
33 namespace NUnit.Framework.Internal
36 /// PlatformHelper class is used by the PlatformAttribute class to
37 /// determine whether a platform is supported.
39 public class PlatformHelper
41 private readonly OSPlatform _os;
42 private readonly RuntimeFramework _rt;
44 // Set whenever we fail to support a list of platforms
45 private string _reason = string.Empty;
47 const string CommonOSPlatforms =
48 "Win,Win32,Win32S,Win32NT,Win32Windows,WinCE,Win95,Win98,WinMe,NT3,NT4,NT5,NT6," +
49 "Win2008Server,Win2008ServerR2,Win2012Server,Win2012ServerR2," +
50 "Win2K,WinXP,Win2003Server,Vista,Win7,Windows7,Win8,Windows8,"+
51 "Win8.1,Windows8.1,Win10,Windows10,WindowsServer10,Unix,Linux";
54 /// Comma-delimited list of all supported OS platform constants
57 public const string OSPlatforms = CommonOSPlatforms;
59 public const string OSPlatforms = CommonOSPlatforms + ",Xbox,MacOSX";
63 /// Comma-delimited list of all supported Runtime platform constants
65 public static readonly string RuntimePlatforms =
66 "Net,NetCF,SSCLI,Rotor,Mono,MonoTouch";
69 /// Default constructor uses the operating system and
70 /// common language runtime of the system.
72 public PlatformHelper()
74 _os = OSPlatform.CurrentPlatform;
75 _rt = RuntimeFramework.CurrentFramework;
79 /// Construct a PlatformHelper for a particular operating
80 /// system and common language runtime. Used in testing.
82 /// <param name="os">OperatingSystem to be used</param>
83 /// <param name="rt">RuntimeFramework to be used</param>
84 public PlatformHelper( OSPlatform os, RuntimeFramework rt )
91 /// Test to determine if one of a collection of platforms
92 /// is being used currently.
94 /// <param name="platforms"></param>
95 /// <returns></returns>
96 public bool IsPlatformSupported( string[] platforms )
98 return platforms.Any( IsPlatformSupported );
102 /// Tests to determine if the current platform is supported
103 /// based on a platform attribute.
105 /// <param name="platformAttribute">The attribute to examine</param>
106 /// <returns></returns>
107 public bool IsPlatformSupported( PlatformAttribute platformAttribute )
109 string include = platformAttribute.Include;
110 string exclude = platformAttribute.Exclude;
112 return IsPlatformSupported( include, exclude );
116 /// Tests to determine if the current platform is supported
117 /// based on a platform attribute.
119 /// <param name="testCaseAttribute">The attribute to examine</param>
120 /// <returns></returns>
121 public bool IsPlatformSupported(TestCaseAttribute testCaseAttribute)
123 string include = testCaseAttribute.IncludePlatform;
124 string exclude = testCaseAttribute.ExcludePlatform;
126 return IsPlatformSupported(include, exclude);
129 private bool IsPlatformSupported(string include, string exclude)
133 if (include != null && !IsPlatformSupported(include))
135 _reason = string.Format("Only supported on {0}", include);
139 if (exclude != null && IsPlatformSupported(exclude))
141 _reason = string.Format("Not supported on {0}", exclude);
147 _reason = ex.Message;
155 /// Test to determine if the a particular platform or comma-
156 /// delimited set of platforms is in use.
158 /// <param name="platform">Name of the platform or comma-separated list of platform ids</param>
159 /// <returns>True if the platform is in use on the system</returns>
160 public bool IsPlatformSupported( string platform )
162 if ( platform.IndexOf( ',' ) >= 0 )
163 return IsPlatformSupported( platform.Split(',') );
165 string platformName = platform.Trim();
168 // string versionSpecification = null;
170 // string[] parts = platformName.Split( new char[] { '-' } );
171 // if ( parts.Length == 2 )
173 // platformName = parts[0];
174 // versionSpecification = parts[1];
177 switch( platformName.ToUpper() )
181 isSupported = _os.IsWindows;
184 isSupported = _os.IsWin32S;
187 isSupported = _os.IsWin32Windows;
190 isSupported = _os.IsWin32NT;
193 isSupported = _os.IsWinCE;
196 isSupported = _os.IsWin95;
199 isSupported = _os.IsWin98;
202 isSupported = _os.IsWinME;
205 isSupported = _os.IsNT3;
208 isSupported = _os.IsNT4;
211 isSupported = _os.IsNT5;
214 isSupported = _os.IsWin2K;
217 isSupported = _os.IsWinXP;
219 case "WIN2003SERVER":
220 isSupported = _os.IsWin2003Server;
223 isSupported = _os.IsNT6;
226 isSupported = _os.IsVista;
228 case "WIN2008SERVER":
229 isSupported = _os.IsWin2008Server;
231 case "WIN2008SERVERR2":
232 isSupported = _os.IsWin2008ServerR2;
234 case "WIN2012SERVER":
235 isSupported = _os.IsWin2012ServerR1 || _os.IsWin2012ServerR2;
237 case "WIN2012SERVERR2":
238 isSupported = _os.IsWin2012ServerR2;
242 isSupported = _os.IsWindows7;
246 isSupported = _os.IsWindows8;
250 isSupported = _os.IsWindows81;
254 isSupported = _os.IsWindows10;
256 case "WINDOWSSERVER10":
257 isSupported = _os.IsWindowsServer10;
261 isSupported = _os.IsUnix;
264 isSupported = _os.IsXbox;
267 isSupported = _os.IsMacOSX;
269 // These bitness tests relate to the process, not the OS.
270 // We can't use Environment.Is64BitProcess because it's
271 // only supported in NET 4.0 and higher.
273 case "64-BIT-PROCESS":
274 isSupported = IntPtr.Size == 8;
277 case "32-BIT-PROCESS":
278 isSupported = IntPtr.Size == 4;
281 #if NET_4_0 || NET_4_5
282 // We only support bitness tests of the OS in .NET 4.0 and up
284 isSupported = Environment.Is64BitOperatingSystem;
287 isSupported = !Environment.Is64BitOperatingSystem;
292 isSupported = IsRuntimeSupported(platformName);
297 _reason = "Only supported on " + platform;
303 /// Return the last failure reason. Results are not
304 /// defined if called before IsSupported( Attribute )
309 get { return _reason; }
312 private bool IsRuntimeSupported(string platformName)
314 string versionSpecification = null;
315 string[] parts = platformName.Split('-');
316 if (parts.Length == 2)
318 platformName = parts[0];
319 versionSpecification = parts[1];
322 switch (platformName.ToUpper())
325 return IsRuntimeSupported(RuntimeType.Net, versionSpecification);
328 return IsRuntimeSupported(RuntimeType.NetCF, versionSpecification);
332 return IsRuntimeSupported(RuntimeType.SSCLI, versionSpecification);
335 return IsRuntimeSupported(RuntimeType.Mono, versionSpecification);
339 return IsRuntimeSupported(RuntimeType.Silverlight, versionSpecification);
342 return IsRuntimeSupported(RuntimeType.MonoTouch, versionSpecification);
345 throw new ArgumentException("Invalid platform name", platformName);
349 private bool IsRuntimeSupported(RuntimeType runtime, string versionSpecification)
351 Version version = versionSpecification == null
352 ? RuntimeFramework.DefaultVersion
353 : new Version(versionSpecification);
355 RuntimeFramework target = new RuntimeFramework(runtime, version);
357 return _rt.Supports(target);