Merge pull request #3142 from henricm/fix-for-win-mono_string_to_utf8
[mono.git] / mcs / nunit24 / NUnitCore / core / PlatformHelper.cs
1 // ****************************************************************\r
2 // This is free software licensed under the NUnit license. You\r
3 // may obtain a copy of the license as well as information regarding\r
4 // copyright ownership at http://nunit.org/?p=license&r=2.4.\r
5 // ****************************************************************\r
6 \r
7 using System;\r
8 using System.Reflection;\r
9 \r
10 namespace NUnit.Core\r
11 {\r
12         public class PlatformHelper\r
13         {\r
14                 private OperatingSystem os;\r
15                 private RuntimeFramework rt;\r
16 \r
17                 // Set whenever we fail to support a list of platforms\r
18                 private string reason = string.Empty;\r
19 \r
20                 // Defined here and used in tests. We can't use PlatformID.Unix\r
21                 // if we are building on .NET 1.0 or 1.1 and the values are different on Mono\r
22                 public static readonly PlatformID UnixPlatformID_Microsoft = (PlatformID) 4;\r
23         public static readonly PlatformID UnixPlatformID_Mono = (PlatformID)128;\r
24 \r
25                 /// <summary>\r
26                 /// Comma-delimited list of all supported OS platform constants\r
27                 /// </summary>\r
28                 public static readonly string OSPlatforms =\r
29                         "Win,Win32,Win32S,Win32NT,Win32Windows,WinCE,Win95,Win98,WinMe,NT3,NT4,NT5,NT6,Win2K,WinXP,Win2003Server,Vista,Win2008Server,Unix,Linux";\r
30                 \r
31                 /// <summary>\r
32                 /// Comma-delimited list of all supported Runtime platform constants\r
33                 /// </summary>\r
34                 public static readonly string RuntimePlatforms =\r
35                         "Net,NetCF,SSCLI,Rotor,Mono";\r
36 \r
37                 /// <summary>\r
38                 /// Default constructor uses the operating system and\r
39                 /// common language runtime of the system.\r
40                 /// </summary>\r
41                 public PlatformHelper()\r
42                 {\r
43                         this.os = Environment.OSVersion;\r
44                         this.rt = RuntimeFramework.CurrentFramework;\r
45                 }\r
46 \r
47                 /// <summary>\r
48                 /// Contruct a PlatformHelper for a particular operating\r
49                 /// system and common language runtime. Used in testing.\r
50                 /// </summary>\r
51                 /// <param name="os">OperatingSystem to be used</param>\r
52                 public PlatformHelper( OperatingSystem os, RuntimeFramework rt )\r
53                 {\r
54                         this.os = os;\r
55                         this.rt = rt;\r
56                 }\r
57 \r
58                 /// <summary>\r
59                 /// Test to determine if one of a collection of platforms\r
60                 /// is being used currently.\r
61                 /// </summary>\r
62                 /// <param name="platforms"></param>\r
63                 /// <returns></returns>\r
64                 public bool IsPlatformSupported( string[] platforms )\r
65                 {\r
66                         foreach( string platform in platforms )\r
67                                 if ( IsPlatformSupported( platform ) )\r
68                                         return true;\r
69 \r
70                         return false;\r
71                 }\r
72 \r
73                 /// <summary>\r
74                 /// Tests to determine if the current platform is supported\r
75                 /// based on a platform attribute.\r
76                 /// </summary>\r
77                 /// <param name="platformAttribute">The attribute to examine</param>\r
78                 /// <returns></returns>\r
79                 public bool IsPlatformSupported( Attribute platformAttribute )\r
80                 {\r
81                         //Use reflection to avoid dependency on a particular framework version\r
82                         string include = (string)Reflect.GetPropertyValue( \r
83                                 platformAttribute, "Include", \r
84                                 BindingFlags.Public | BindingFlags.Instance );\r
85 \r
86                         string exclude = (string)Reflect.GetPropertyValue(\r
87                                 platformAttribute, "Exclude", \r
88                                 BindingFlags.Public | BindingFlags.Instance );\r
89 \r
90                         try\r
91                         {\r
92                                 if (include != null && !IsPlatformSupported(include))\r
93                                 {\r
94                                         reason = string.Format("Only supported on {0}", include);\r
95                                         return false;\r
96                                 }\r
97 \r
98                                 if (exclude != null && IsPlatformSupported(exclude))\r
99                                 {\r
100                                         reason = string.Format("Not supported on {0}", exclude);\r
101                                         return false;\r
102                                 }\r
103                         }\r
104                         catch( ArgumentException ex )\r
105                         {\r
106                                 reason = string.Format( "Invalid platform name: {0}", ex.ParamName );\r
107                                 return false; \r
108                         }\r
109 \r
110                         return true;\r
111                 }\r
112 \r
113                 /// <summary>\r
114                 /// Test to determine if the a particular platform or comma-\r
115                 /// delimited set of platforms is in use.\r
116                 /// </summary>\r
117                 /// <param name="platform">Name of the platform or comma-separated list of platform names</param>\r
118                 /// <returns>True if the platform is in use on the system</returns>\r
119                 public bool IsPlatformSupported( string platform )\r
120                 {\r
121                         if ( platform.IndexOf( ',' ) >= 0 )\r
122                                 return IsPlatformSupported( platform.Split( new char[] { ',' } ) );\r
123 \r
124                         string platformName = platform.Trim();\r
125                         bool nameOK = false;\r
126 \r
127                         string versionSpecification = null;\r
128 \r
129                         string[] parts = platformName.Split( new char[] { '-' } );\r
130                         if ( parts.Length == 2 )\r
131                         {\r
132                                 platformName = parts[0];\r
133                                 versionSpecification = parts[1];\r
134                         }\r
135 \r
136                         switch( platformName.ToUpper() )\r
137                         {\r
138                                 case "WIN":\r
139                                 case "WIN32":\r
140                                         nameOK = os.Platform.ToString().StartsWith( "Win" );\r
141                                         break;\r
142                                 case "WIN32S":\r
143                                         nameOK = os.Platform == PlatformID.Win32S;\r
144                                         break;\r
145                                 case "WIN32WINDOWS":\r
146                                         nameOK = os.Platform == PlatformID.Win32Windows;\r
147                                         break;\r
148                                 case "WIN32NT":\r
149                                         nameOK = os.Platform == PlatformID.Win32NT;\r
150                                         break;\r
151                                 case "WINCE":\r
152                                         nameOK = (int)os.Platform == 3;  // Not defined in .NET 1.0\r
153                                         break;\r
154                                 case "WIN95":\r
155                                         nameOK = os.Platform == PlatformID.Win32Windows && os.Version.Major == 4 && os.Version.Minor == 0;\r
156                                         break;\r
157                                 case "WIN98": \r
158                                         nameOK = os.Platform == PlatformID.Win32Windows && os.Version.Major == 4 && os.Version.Minor == 10;\r
159                                         break;\r
160                                 case "WINME":\r
161                                         nameOK = os.Platform == PlatformID.Win32Windows && os.Version.Major == 4 && os.Version.Minor == 90;\r
162                                         break;\r
163                                 case "NT3":\r
164                                         nameOK = os.Platform == PlatformID.Win32NT && os.Version.Major == 3;\r
165                                         break;\r
166                                 case "NT4":\r
167                                         nameOK = os.Platform == PlatformID.Win32NT && os.Version.Major == 4;\r
168                                         break;\r
169                                 case "NT5":\r
170                                         nameOK = os.Platform == PlatformID.Win32NT && os.Version.Major == 5;\r
171                                         break;\r
172                                 case "WIN2K":\r
173                                         nameOK = os.Platform == PlatformID.Win32NT && os.Version.Major == 5 && os.Version.Minor == 0;\r
174                                         break;\r
175                                 case "WINXP":\r
176                                         nameOK = os.Platform == PlatformID.Win32NT && os.Version.Major == 5 && os.Version.Minor == 1;\r
177                                         break;\r
178                                 case "WIN2003SERVER":\r
179                                         nameOK = os.Platform == PlatformID.Win32NT && os.Version.Major == 5 && os.Version.Minor == 2;\r
180                                         break;\r
181                 case "NT6":\r
182                     nameOK = os.Platform == PlatformID.Win32NT && os.Version.Major == 6;\r
183                     break;\r
184                 // TODO: Distinguish Vista SP1 from Server 2008\r
185                 case "VISTA":\r
186                     nameOK = os.Platform == PlatformID.Win32NT && os.Version.Major == 6 && os.Version.Minor == 0 && os.Version.Build != 6001;\r
187                     break;\r
188                 case "WIN2008SERVER":\r
189                     nameOK = os.Platform == PlatformID.Win32NT && os.Version.Major == 6 && os.Version.Minor == 0 && os.Version.Build == 6001;\r
190                     break;\r
191                                 case "UNIX":\r
192                                 case "LINUX":\r
193                                         nameOK = os.Platform == UnixPlatformID_Microsoft\r
194                           || os.Platform == UnixPlatformID_Mono;\r
195                                         break;\r
196                                 case "NET":\r
197                                         nameOK = rt.Runtime == RuntimeType.Net;\r
198                                         break;\r
199                                 case "NETCF":\r
200                                         nameOK = rt.Runtime == RuntimeType.NetCF;\r
201                                         break;\r
202                                 case "SSCLI":\r
203                                 case "ROTOR":\r
204                                         nameOK = rt.Runtime == RuntimeType.SSCLI;\r
205                                         break;\r
206                                 case "MONO":\r
207                                         nameOK = rt.Runtime == RuntimeType.Mono;\r
208                                         // Special handling because Mono 1.0 profile has version 1.1\r
209                                         if ( versionSpecification == "1.0" )\r
210                                                 versionSpecification = "1.1";\r
211                                         break;\r
212                                 default:\r
213                                         throw new ArgumentException( "Invalid platform name", platform.ToString() );\r
214                         }\r
215 \r
216                         if ( nameOK ) \r
217                         {\r
218                                 if ( versionSpecification == null )\r
219                                         return true;\r
220 \r
221                                 Version version = new Version( versionSpecification );\r
222 \r
223                                 if ( rt.Version.Major == version.Major &&\r
224                                          rt.Version.Minor == version.Minor &&\r
225                                    ( version.Build == -1 || rt.Version.Build == version.Build ) &&\r
226                                    ( version.Revision == -1 || rt.Version.Revision == version.Revision ) )\r
227                                                 return true;\r
228                         }\r
229 \r
230                         this.reason = "Only supported on " + platform;\r
231                         return false;\r
232                 }\r
233 \r
234                 /// <summary>\r
235                 /// Return the last failure reason. Results are not\r
236                 /// defined if called before IsSupported( Attribute )\r
237                 /// is called.\r
238                 /// </summary>\r
239                 public string Reason\r
240                 {\r
241                         get { return reason; }\r
242                 }\r
243         }\r
244 }\r