Implement mono_gc_alloc_fixed on Boehm to be uncollectable. This matches SGen behavio...
[mono.git] / mcs / class / System.ServiceProcess / Test / System.ServiceProcess / ServiceControllerTest.cs
1 //
2 // ServiceControllerTest.cs -
3 //      NUnit Test Cases for ServiceController
4 //
5 // Author:
6 //      Gert Driesen  (drieseng@users.sourceforge.net)
7 //
8 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 // TODO:
30 // - Status
31 // - Start
32
33 using System;
34 using System.Collections.Generic;
35 using System.ComponentModel;
36 using System.ServiceProcess;
37 using TimeoutException = System.ServiceProcess.TimeoutException;
38
39 using NUnit.Framework;
40
41 namespace MonoTests.System.ServiceProcess
42 {
43         [TestFixture]
44         public class ServiceControllerTest
45         {
46                 class ServiceInfo
47                 {
48                         public string ServiceName;
49                         public string DisplayName;
50                         public ServiceType ServiceType = ServiceType.Win32ShareProcess;
51                         public string[] Dependents = new string[] {};
52                         public string[] DependedOn = new string[] {};
53                 }
54
55                 static ServiceInfo DISK_DRIVER_SERVICE = new ServiceInfo { ServiceName = "disk", DisplayName = "Disk Driver", ServiceType = ServiceType.KernelDriver };
56                 static ServiceInfo ROUTING_AND_REMOTE_ACCESS_SERVICE = new ServiceInfo { ServiceName = "RemoteAccess", DisplayName = "Routing and Remote Access", DependedOn = new [] { "bfe", "http", "rasman", "rpcss" } };
57                 static ServiceInfo SECONDARY_LOGON_SERVICE = new ServiceInfo { ServiceName = "seclogon", DisplayName = "Secondary Logon", Dependents = new [] { "te.service" } };
58                 static ServiceInfo SECURITY_ACCOUNTS_MANAGER_SERVICE = new ServiceInfo { ServiceName = "SamSs", DisplayName = "Security Accounts Manager", Dependents = new [] { "browser", "ktmrm", "lanmanserver", "msdtc" }, DependedOn = new [] { "rpcss" } };
59                 static ServiceInfo WINDOWS_IMAGE_ACQUISITION_SERVICE = new ServiceInfo { ServiceName = "stisvc", DisplayName = "Windows Image Acquisition (WIA)", ServiceType = ServiceType.Win32OwnProcess, DependedOn = new [] { "rpcss" } };
60                 static ServiceInfo WINDOWS_SEARCH_SERVICE = new ServiceInfo { ServiceName = "WSearch", DisplayName = "Windows Search", ServiceType = ServiceType.Win32OwnProcess, Dependents = new [] { "wmpnetworksvc", "workfolderssvc" }, DependedOn = new [] { "rpcss" } };
61                 static ServiceInfo WINDOWS_TIME_SERVICE = new ServiceInfo { ServiceName = "W32Time", DisplayName = "Windows Time" };
62                 static ServiceInfo WINDOWS_UPDATE_SERVICE = new ServiceInfo { ServiceName = "wuauserv", DisplayName = "Windows Update", DependedOn = new [] { "rpcss" } };
63                 static ServiceInfo WORKSTATION_SERVICE = new ServiceInfo { ServiceName = "LanmanWorkstation", DisplayName = "Workstation", Dependents = new [] { "browser", "netlogon", "sessionenv" }, DependedOn = new [] { "bowser", "mrxsmb20", "nsi" } };
64
65                 // A service which is expected to be disabled by default on all supported Windows versions.
66                 static ServiceInfo DISABLED_SERVICE = ROUTING_AND_REMOTE_ACCESS_SERVICE;
67                 // A service which is running by default and can be stopped/paused by the current user when running with admin rights.
68                 static ServiceInfo CONTROLLABLE_SERVICE = WINDOWS_IMAGE_ACQUISITION_SERVICE;
69                 // A service which cannot be stopped/paused.
70                 static ServiceInfo UNCONTROLLABLE_SERVICE = SECURITY_ACCOUNTS_MANAGER_SERVICE;
71                 // A service with ServiceType == KernelDriver
72                 static ServiceInfo KERNEL_SERVICE = DISK_DRIVER_SERVICE;
73                 // A service with ServiceType == Win32ShareProcess.
74                 static ServiceInfo SHARE_PROCESS_SERVICE = WORKSTATION_SERVICE;
75
76                 static ServiceInfo SERVICE_1_WITH_DEPENDENTS_AND_DEPENDED_ON = SECURITY_ACCOUNTS_MANAGER_SERVICE;
77                 static ServiceInfo SERVICE_2_WITH_DEPENDENTS_AND_DEPENDED_ON = WINDOWS_SEARCH_SERVICE;
78                 static ServiceInfo SERVICE_3_WITH_DEPENDENTS_AND_DEPENDED_ON = WORKSTATION_SERVICE;
79
80                 static ServiceInfo SERVICE_WITH_MANY_DEPENDENTS = WORKSTATION_SERVICE;
81                 static ServiceInfo SERVICE_WITH_ONE_DEPENDENT = SECONDARY_LOGON_SERVICE;
82                 static ServiceInfo SERVICE_WITH_NO_DEPENDENTS = WINDOWS_TIME_SERVICE;
83
84                 static ServiceInfo SERVICE_WITH_MANY_DEPENDED_ON = WORKSTATION_SERVICE;
85                 static ServiceInfo SERVICE_WITH_ONE_DEPENDED_ON = WINDOWS_UPDATE_SERVICE;
86                 static ServiceInfo SERVICE_WITH_NO_DEPENDED_ON = WINDOWS_TIME_SERVICE;
87
88                 [TestFixtureSetUp]
89                 public void FixtureSetUp ()
90                 {
91                         try {
92                                 EnsureServiceIsRunning (new ServiceController (CONTROLLABLE_SERVICE.ServiceName));
93                         } catch {
94                                 Assert.Ignore ($"Failed to start the service '{CONTROLLABLE_SERVICE.DisplayName}'. Ensure you are running the tests with admin privileges.");
95                         }
96                 }
97
98                 [Test]
99                 public void Constructor1 ()
100                 {
101                         if (RunningOnUnix)
102                                 Assert.Ignore ("Running on Unix.");
103
104                         ServiceController sc = new ServiceController ();
105
106                         try {
107                                 bool value = sc.CanPauseAndContinue;
108                                 Assert.Fail ("#A1: " + value.ToString ());
109                         } catch (ArgumentException ex) {
110                                 // Service name  contains invalid characters, is empty or is
111                                 // too long (max length = 80)
112                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
113                                 Assert.IsNotNull (ex.Message, "#A3");
114                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#A4");
115                                 Assert.IsTrue (ex.Message.IndexOf ("80") != -1, "#A5");
116                                 Assert.IsNull (ex.ParamName, "#A6");
117                                 Assert.IsNull (ex.InnerException, "#A7");
118                         }
119
120                         try {
121                                 bool value = sc.CanShutdown;
122                                 Assert.Fail ("#B1: " + value.ToString ());
123                         } catch (ArgumentException ex) {
124                                 // Service name  contains invalid characters, is empty or is
125                                 // too long (max length = 80)
126                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
127                                 Assert.IsNotNull (ex.Message, "#B3");
128                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#B4");
129                                 Assert.IsTrue (ex.Message.IndexOf ("80") != -1, "#B5");
130                                 Assert.IsNull (ex.ParamName, "#B6");
131                                 Assert.IsNull (ex.InnerException, "#B7");
132                         }
133
134                         try {
135                                 bool value = sc.CanStop;
136                                 Assert.Fail ("#C1: " + value.ToString ());
137                         } catch (ArgumentException ex) {
138                                 // Service name  contains invalid characters, is empty or is
139                                 // too long (max length = 80)
140                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
141                                 Assert.IsNotNull (ex.Message, "#C3");
142                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#C4");
143                                 Assert.IsTrue (ex.Message.IndexOf ("80") != -1, "#C5");
144                                 Assert.IsNull (ex.ParamName, "#C6");
145                                 Assert.IsNull (ex.InnerException, "#C7");
146                         }
147
148                         // closing the ServiceController does not result in exception
149                         sc.Close ();
150
151                         try {
152                                 sc.Continue ();
153                                 Assert.Fail ("#D1");
154                         } catch (ArgumentException ex) {
155                                 // Service name  contains invalid characters, is empty or is
156                                 // too long (max length = 80)
157                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
158                                 Assert.IsNotNull (ex.Message, "#D3");
159                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#D4");
160                                 Assert.IsTrue (ex.Message.IndexOf ("80") != -1, "#D5");
161                                 Assert.IsNull (ex.ParamName, "#D6");
162                                 Assert.IsNull (ex.InnerException, "#D7");
163                         }
164
165                         try {
166                                 Assert.Fail ("#E1: " + sc.DependentServices.Length);
167                         } catch (ArgumentException ex) {
168                                 // Service name  contains invalid characters, is empty or is
169                                 // too long (max length = 80)
170                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#E2");
171                                 Assert.IsNotNull (ex.Message, "#E3");
172                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#E4");
173                                 Assert.IsTrue (ex.Message.IndexOf ("80") != -1, "#E5");
174                                 Assert.IsNull (ex.ParamName, "#E6");
175                                 Assert.IsNull (ex.InnerException, "#E7");
176                         }
177
178                         Assert.IsNotNull (sc.DisplayName, "#F1");
179                         Assert.AreEqual (string.Empty, sc.DisplayName, "#F2");
180
181                         try {
182                                 sc.ExecuteCommand (0);
183                                 Assert.Fail ("#G1");
184                         } catch (ArgumentException ex) {
185                                 // Service name  contains invalid characters, is empty or is
186                                 // too long (max length = 80)
187                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#G2");
188                                 Assert.IsNotNull (ex.Message, "#G3");
189                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#G4");
190                                 Assert.IsTrue (ex.Message.IndexOf ("80") != -1, "#G5");
191                                 Assert.IsNull (ex.ParamName, "#G6");
192                                 Assert.IsNull (ex.InnerException, "#G7");
193                         }
194
195                         Assert.IsNotNull (sc.MachineName, "#H1");
196                         Assert.AreEqual (".", sc.MachineName, "#H2");
197
198
199                         try {
200                                 sc.Pause ();
201                                 Assert.Fail ("#I1");
202                         } catch (ArgumentException ex) {
203                                 // Service name  contains invalid characters, is empty or is
204                                 // too long (max length = 80)
205                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#I2");
206                                 Assert.IsNotNull (ex.Message, "#I3");
207                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#I4");
208                                 Assert.IsTrue (ex.Message.IndexOf ("80") != -1, "#I5");
209                                 Assert.IsNull (ex.ParamName, "#I6");
210                                 Assert.IsNull (ex.InnerException, "#I7");
211                         }
212                 }
213
214                 [Test]
215                 public void Constructor2 ()
216                 {
217                         if (RunningOnUnix)
218                                 Assert.Ignore ("Running on Unix.");
219
220                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName);
221
222                         Assert.IsTrue (sc.CanPauseAndContinue, "#A1");
223                         Assert.IsTrue (sc.CanShutdown, "#B1");
224                         Assert.IsTrue (sc.CanStop, "#C1");
225
226                         sc.Close ();
227                         sc.Continue ();
228
229                         ServiceController [] dependentServices = sc.DependentServices;
230                         Assert.IsNotNull (dependentServices, "#D1");
231                         Assert.AreEqual (CONTROLLABLE_SERVICE.Dependents, ServiceNames (dependentServices), "#D2");
232
233                         Assert.IsNotNull (sc.DisplayName, "#E1");
234                         Assert.AreEqual (CONTROLLABLE_SERVICE.DisplayName, sc.DisplayName, "#E2");
235
236                         Assert.IsNotNull (sc.MachineName, "#F1");
237                         Assert.AreEqual (".", sc.MachineName, "#F2");
238
239                         sc.Refresh ();
240
241                         Assert.IsNotNull (sc.ServiceName, "#G1");
242                         Assert.AreEqual (CONTROLLABLE_SERVICE.ServiceName, sc.ServiceName, "#G2");
243
244                         ServiceController [] servicesDependedOn = sc.ServicesDependedOn;
245                         Assert.IsNotNull (servicesDependedOn, "#H1");
246                         Assert.AreEqual (CONTROLLABLE_SERVICE.DependedOn, ServiceNames (servicesDependedOn), "#H2");
247
248                         Assert.AreEqual (CONTROLLABLE_SERVICE.ServiceType, sc.ServiceType, "#I1");
249                         Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#J1");
250                 }
251
252                 [Test]
253                 public void Constructor2_Name_Empty ()
254                 {
255                         try {
256                                 new ServiceController (string.Empty);
257                                 Assert.Fail ("#1");
258                         } catch (ArgumentException ex) {
259                                 // Invalid value  for parameter name
260                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
261                                 Assert.IsNotNull (ex.Message, "#3");
262                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#4");
263                                 Assert.IsTrue (ex.Message.IndexOf ("name") != -1, "#5");
264                                 Assert.IsNull (ex.ParamName, "#6");
265                                 Assert.IsNull (ex.InnerException, "#7");
266                         }
267                 }
268
269                 [Test]
270                 public void Constructor2_Name_DisplayName ()
271                 {
272                         if (RunningOnUnix)
273                                 Assert.Ignore ("Running on Unix.");
274
275                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.DisplayName);
276
277                         Assert.IsTrue (sc.CanPauseAndContinue, "#A1");
278                         Assert.IsTrue (sc.CanShutdown, "#B1");
279                         Assert.IsTrue (sc.CanStop, "#C1");
280                 }
281
282                 [Test]
283                 public void Constructor2_Name_Null ()
284                 {
285                         try {
286                                 new ServiceController (null);
287                                 Assert.Fail ("#1");
288                         } catch (ArgumentException ex) {
289                                 // Invalid value  for parameter name
290                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
291                                 Assert.IsNotNull (ex.Message, "#3");
292                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#4");
293                                 Assert.IsTrue (ex.Message.IndexOf ("name") != -1, "#5");
294                                 Assert.IsNull (ex.ParamName, "#6");
295                                 Assert.IsNull (ex.InnerException, "#7");
296                         }
297                 }
298
299                 [Test]
300                 public void Constructor2_Name_ServiceName ()
301                 {
302                         if (RunningOnUnix)
303                                 Assert.Ignore ("Running on Unix.");
304
305                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName);
306
307                         Assert.IsTrue (sc.CanPauseAndContinue, "#A1");
308                         Assert.IsTrue (sc.CanShutdown, "#B1");
309                         Assert.IsTrue (sc.CanStop, "#C1");
310                 }
311
312                 [Test]
313                 public void Constructor3 ()
314                 {
315                         if (RunningOnUnix)
316                                 Assert.Ignore ("Running on Unix.");
317
318                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName,
319                                 Environment.MachineName);
320
321                         Assert.IsTrue (sc.CanPauseAndContinue, "#A1");
322                         Assert.IsTrue (sc.CanShutdown, "#B1");
323                         Assert.IsTrue (sc.CanStop, "#C1");
324
325                         sc.Close ();
326                         sc.Continue ();
327
328                         ServiceController [] dependentServices = sc.DependentServices;
329                         Assert.IsNotNull (dependentServices, "#D1");
330                         Assert.AreEqual (CONTROLLABLE_SERVICE.Dependents, ServiceNames (dependentServices), "#D2");
331
332                         Assert.IsNotNull (sc.DisplayName, "#E1");
333                         Assert.AreEqual (CONTROLLABLE_SERVICE.DisplayName, sc.DisplayName, "#E2");
334
335                         Assert.IsNotNull (sc.MachineName, "#F1");
336                         Assert.AreEqual (Environment.MachineName, sc.MachineName, "#F2");
337
338                         sc.Refresh ();
339
340                         Assert.IsNotNull (sc.ServiceName, "#G1");
341                         Assert.AreEqual (CONTROLLABLE_SERVICE.ServiceName, sc.ServiceName, "#G2");
342
343                         ServiceController [] servicesDependedOn = sc.ServicesDependedOn;
344                         Assert.IsNotNull (servicesDependedOn, "#H1");
345                         Assert.AreEqual (CONTROLLABLE_SERVICE.DependedOn, ServiceNames (servicesDependedOn), "#H2");
346
347                         Assert.AreEqual (CONTROLLABLE_SERVICE.ServiceType, sc.ServiceType, "#I1");
348                         Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#J1");
349                 }
350
351                 [Test]
352                 public void Constructor3_MachineName_Empty ()
353                 {
354                         try {
355                                 new ServiceController (CONTROLLABLE_SERVICE.ServiceName, string.Empty);
356                                 Assert.Fail ("#1");
357                         } catch (ArgumentException ex) {
358                                 // MachineName value  is invalid
359                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
360                                 Assert.IsNotNull (ex.Message, "#3");
361                                 Assert.IsTrue (ex.Message.IndexOf ("MachineName") != -1, "#4");
362                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#5");
363                                 Assert.IsNull (ex.ParamName, "#6");
364                                 Assert.IsNull (ex.InnerException, "#7");
365                         }
366                 }
367
368                 [Test]
369                 public void Constructor3_MachineName_Null ()
370                 {
371                         try {
372                                 new ServiceController (CONTROLLABLE_SERVICE.ServiceName, null);
373                                 Assert.Fail ("#1");
374                         } catch (ArgumentException ex) {
375                                 // MachineName value  is invalid
376                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
377                                 Assert.IsNotNull (ex.Message, "#3");
378                                 Assert.IsTrue (ex.Message.IndexOf ("MachineName") != -1, "#4");
379                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#5");
380                                 Assert.IsNull (ex.ParamName, "#6");
381                                 Assert.IsNull (ex.InnerException, "#7");
382                         }
383                 }
384
385                 [Test]
386                 public void Constructor3_Name_Empty ()
387                 {
388                         try {
389                                 new ServiceController (string.Empty, ".");
390                                 Assert.Fail ("#1");
391                         } catch (ArgumentException ex) {
392                                 // Invalid value  for parameter name
393                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
394                                 Assert.IsNotNull (ex.Message, "#3");
395                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#4");
396                                 Assert.IsTrue (ex.Message.IndexOf ("name") != -1, "#5");
397                                 Assert.IsNull (ex.ParamName, "#6");
398                                 Assert.IsNull (ex.InnerException, "#7");
399                         }
400                 }
401
402                 [Test]
403                 public void Constructor3_Name_Null ()
404                 {
405                         try {
406                                 new ServiceController (null, ".");
407                                 Assert.Fail ("#1");
408                         } catch (ArgumentException ex) {
409                                 // Invalid value  for parameter name
410                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
411                                 Assert.IsNotNull (ex.Message, "#3");
412                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#4");
413                                 Assert.IsTrue (ex.Message.IndexOf ("name") != -1, "#5");
414                                 Assert.IsNull (ex.ParamName, "#6");
415                                 Assert.IsNull (ex.InnerException, "#7");
416                         }
417                 }
418
419                 [Test]
420                 public void CanPauseAndContinue ()
421                 {
422                         if (RunningOnUnix)
423                                 Assert.Ignore ("Running on Unix.");
424
425                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
426                         Assert.IsTrue (sc.CanPauseAndContinue, "#1");
427                         sc.ServiceName = UNCONTROLLABLE_SERVICE.ServiceName;
428                         Assert.IsFalse (sc.CanPauseAndContinue, "#2");
429                         sc.DisplayName = CONTROLLABLE_SERVICE.DisplayName;
430                         Assert.IsTrue (sc.CanPauseAndContinue, "#3");
431                         sc.MachineName = "doesnotexist";
432                         try {
433                                 bool value = sc.CanPauseAndContinue;
434                                 Assert.Fail ("#4: " + value.ToString ());
435                         } catch (InvalidOperationException ex) {
436                                 // Cannot open Service Control Manager on computer 'doesnotexist'.
437                                 // This operation might require other priviliges
438                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#5");
439                                 Assert.IsNotNull (ex.Message, "#6");
440                                 Assert.IsTrue (ex.Message.IndexOf ("'doesnotexist'") != -1, "#7");
441                                 Assert.IsNotNull (ex.InnerException, "#8");
442
443                                 // The RPC server is unavailable
444                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#9");
445                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
446                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#10");
447                                 Assert.IsNotNull (win32Error.Message, "#11");
448                                 Assert.AreEqual (1722, win32Error.NativeErrorCode, "#12");
449                                 Assert.IsNull (win32Error.InnerException, "#13");
450                         }
451                 }
452
453                 [Test]
454                 public void CanPauseAndContinue_Machine_DoesNotExist ()
455                 {
456                         if (RunningOnUnix)
457                                 Assert.Ignore ("Running on Unix.");
458
459                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName,
460                                 "doesnotexist");
461                         try {
462                                 bool canPauseAndContinue = sc.CanPauseAndContinue;
463                                 Assert.Fail ("#1: " + canPauseAndContinue);
464                         } catch (InvalidOperationException ex) {
465                                 // Cannot open Service Control Manager on computer 'doesnotexist'.
466                                 // This operation might require other priviliges
467                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
468                                 Assert.IsNotNull (ex.Message, "#3");
469                                 Assert.IsTrue (ex.Message.IndexOf ("'doesnotexist'") != -1, "#4");
470                                 Assert.IsNotNull (ex.InnerException, "#5");
471
472                                 // The RPC server is unavailable
473                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#6");
474                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
475                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#7");
476                                 Assert.IsNotNull (win32Error.Message, "#8");
477                                 Assert.AreEqual (1722, win32Error.NativeErrorCode, "#9");
478                                 Assert.IsNull (win32Error.InnerException, "#10");
479                         }
480                 }
481
482                 [Test]
483                 public void CanPauseAndContinue_Service_Disabled ()
484                 {
485                         if (RunningOnUnix)
486                                 Assert.Ignore ("Running on Unix.");
487
488                         ServiceController sc1 = new ServiceController (DISABLED_SERVICE.ServiceName, ".");
489                         Assert.IsFalse (sc1.CanPauseAndContinue);
490                 }
491
492                 [Test]
493                 public void CanPauseAndContinue_Service_DoesNotExist ()
494                 {
495                         if (RunningOnUnix)
496                                 Assert.Ignore ("Running on Unix.");
497
498                         ServiceController sc = new ServiceController ("doesnotexist", ".");
499                         try {
500                                 bool canPauseAndContinue = sc.CanPauseAndContinue;
501                                 Assert.Fail ("#1: " + canPauseAndContinue);
502                         } catch (InvalidOperationException ex) {
503                                 // Cannot open doesnotexist service on computer '.'
504                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
505                                 Assert.IsNotNull (ex.Message, "#3");
506                                 Assert.IsTrue (ex.Message.IndexOf ("doesnotexist") != -1, "#4");
507                                 Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#5");
508                                 Assert.IsNotNull (ex.InnerException, "#6");
509
510                                 // The filename, directory name, or volume label is incorrect
511                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#7");
512                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
513                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#8");
514                                 Assert.IsNotNull (win32Error.Message, "#9");
515                                 Assert.AreEqual (1060, win32Error.NativeErrorCode, "#10");
516                                 Assert.IsNull (win32Error.InnerException, "#11");
517                         }
518                 }
519
520                 [Test]
521                 public void CanPauseAndContinue_Service_OperationNotValid ()
522                 {
523                         if (RunningOnUnix)
524                                 Assert.Ignore ("Running on Unix.");
525
526                         ServiceController sc1 = new ServiceController (UNCONTROLLABLE_SERVICE.ServiceName, ".");
527                         Assert.IsFalse (sc1.CanPauseAndContinue);
528                 }
529
530                 [Test]
531                 public void CanPauseAndContinue_Service_Running ()
532                 {
533                         if (RunningOnUnix)
534                                 Assert.Ignore ("Running on Unix.");
535
536                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
537                         Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#1");
538                         Assert.IsTrue (sc.CanPauseAndContinue, "#2");
539                 }
540
541                 [Test]
542                 public void CanPauseAndContinue_Service_Paused ()
543                 {
544                         if (RunningOnUnix)
545                                 Assert.Ignore ("Running on Unix.");
546
547                         ServiceController sc1 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
548                         ServiceController sc2 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
549
550                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#A1");
551                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#A2");
552
553                         Assert.IsTrue (sc1.CanPauseAndContinue, "#B1");
554                         Assert.IsTrue (sc2.CanPauseAndContinue, "#B2");
555
556                         sc1.Pause ();
557
558                         try {
559                                 Assert.IsTrue (sc1.CanPauseAndContinue, "#C1");
560                                 Assert.IsTrue (sc2.CanPauseAndContinue, "#C2");
561
562                                 sc1.WaitForStatus (ServiceControllerStatus.Paused, new TimeSpan (0, 0, 5));
563
564                                 Assert.IsTrue (sc1.CanPauseAndContinue, "#D1");
565                                 Assert.IsTrue (sc2.CanPauseAndContinue, "#D2");
566                         } finally {
567                                 EnsureServiceIsRunning (sc1);
568                                 sc2.Refresh ();
569                         }
570
571                         Assert.IsTrue (sc1.CanPauseAndContinue, "#E1");
572                         Assert.IsTrue (sc2.CanPauseAndContinue, "#E2");
573
574                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#F1");
575                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#F2");
576                 }
577
578                 [Test]
579                 public void CanPauseAndContinue_Service_Stopped ()
580                 {
581                         if (RunningOnUnix)
582                                 Assert.Ignore ("Running on Unix.");
583
584                         ServiceController sc1 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
585                         ServiceController sc2 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
586
587                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#A1");
588                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#A2");
589
590                         Assert.IsTrue (sc1.CanPauseAndContinue, "#B1");
591                         Assert.IsTrue (sc2.CanPauseAndContinue, "#B2");
592
593                         sc1.Stop ();
594
595                         try {
596                                 Assert.IsTrue (sc1.CanPauseAndContinue, "#C1");
597                                 Assert.IsTrue (sc2.CanPauseAndContinue, "#C2");
598
599                                 sc1.WaitForStatus (ServiceControllerStatus.Stopped, new TimeSpan (0, 0, 5));
600
601                                 Assert.IsFalse (sc1.CanPauseAndContinue, "#D1");
602                                 Assert.IsTrue (sc2.CanPauseAndContinue, "#D2");
603                         } finally {
604                                 EnsureServiceIsRunning (sc1);
605                                 sc2.Refresh ();
606                         }
607
608                         Assert.IsTrue (sc1.CanPauseAndContinue, "#E1");
609                         Assert.IsTrue (sc2.CanPauseAndContinue, "#E2");
610
611                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#F1");
612                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#F2");
613                 }
614
615                 [Test]
616                 public void CanPauseAndContinue_ServiceName_Empty ()
617                 {
618                         ServiceController sc = new ServiceController ();
619                         try {
620                                 bool canPauseAndContinue = sc.CanPauseAndContinue;
621                                 Assert.Fail ("#1: " + canPauseAndContinue);
622                         } catch (ArgumentException ex) {
623                                 // Service name  contains invalid characters, is empty or is
624                                 // too long (max length = 80)
625                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
626                                 Assert.IsNotNull (ex.Message, "#3");
627                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#4");
628                                 Assert.IsTrue (ex.Message.IndexOf ("80") != -1, "#5");
629                                 Assert.IsNull (ex.ParamName, "#6");
630                                 Assert.IsNull (ex.InnerException, "#7");
631                         }
632                 }
633
634                 [Test]
635                 public void CanShutdown ()
636                 {
637                         if (RunningOnUnix)
638                                 Assert.Ignore ("Running on Unix.");
639
640                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
641                         Assert.IsTrue (sc.CanShutdown, "#1");
642                         sc.ServiceName = UNCONTROLLABLE_SERVICE.ServiceName;
643                         Assert.IsFalse (sc.CanShutdown, "#2");
644                         sc.DisplayName = CONTROLLABLE_SERVICE.DisplayName;
645                         Assert.IsTrue (sc.CanShutdown, "#3");
646                         sc.MachineName = "doesnotexist";
647                         try {
648                                 bool value = sc.CanShutdown;
649                                 Assert.Fail ("#4: " + value.ToString ());
650                         } catch (InvalidOperationException ex) {
651                                 // Cannot open Service Control Manager on computer 'doesnotexist'.
652                                 // This operation might require other priviliges
653                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#5");
654                                 Assert.IsNotNull (ex.Message, "#6");
655                                 Assert.IsTrue (ex.Message.IndexOf ("'doesnotexist'") != -1, "#7");
656                                 Assert.IsNotNull (ex.InnerException, "#8");
657
658                                 // The RPC server is unavailable
659                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#9");
660                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
661                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#10");
662                                 Assert.IsNotNull (win32Error.Message, "#11");
663                                 Assert.AreEqual (1722, win32Error.NativeErrorCode, "#12");
664                                 Assert.IsNull (win32Error.InnerException, "#13");
665                         }
666                 }
667
668                 [Test]
669                 public void CanShutdown_Machine_DoesNotExist ()
670                 {
671                         if (RunningOnUnix)
672                                 Assert.Ignore ("Running on Unix.");
673
674                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName,
675                                 "doesnotexist");
676                         try {
677                                 bool canShutdown = sc.CanShutdown;
678                                 Assert.Fail ("#1: " + canShutdown);
679                         } catch (InvalidOperationException ex) {
680                                 // Cannot open Service Control Manager on computer 'doesnotexist'.
681                                 // This operation might require other priviliges
682                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
683                                 Assert.IsNotNull (ex.Message, "#3");
684                                 Assert.IsTrue (ex.Message.IndexOf ("'doesnotexist'") != -1, "#4");
685                                 Assert.IsNotNull (ex.InnerException, "#5");
686
687                                 // The RPC server is unavailable
688                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#6");
689                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
690                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#7");
691                                 Assert.IsNotNull (win32Error.Message, "#8");
692                                 Assert.AreEqual (1722, win32Error.NativeErrorCode, "#9");
693                                 Assert.IsNull (win32Error.InnerException, "#10");
694                         }
695                 }
696
697                 [Test]
698                 public void CanShutdown_Service_Disabled ()
699                 {
700                         if (RunningOnUnix)
701                                 Assert.Ignore ("Running on Unix.");
702
703                         ServiceController sc1 = new ServiceController (DISABLED_SERVICE.ServiceName, ".");
704                         Assert.IsFalse (sc1.CanShutdown);
705                 }
706
707                 [Test]
708                 public void CanShutdown_Service_DoesNotExist ()
709                 {
710                         if (RunningOnUnix)
711                                 Assert.Ignore ("Running on Unix.");
712
713                         ServiceController sc = new ServiceController ("doesnotexist", ".");
714                         try {
715                                 bool value = sc.CanShutdown;
716                                 Assert.Fail ("#1: " + value.ToString ());
717                         } catch (InvalidOperationException ex) {
718                                 // Cannot open doesnotexist service on computer '.'
719                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
720                                 Assert.IsNotNull (ex.Message, "#3");
721                                 Assert.IsTrue (ex.Message.IndexOf ("doesnotexist") != -1, "#4");
722                                 Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#5");
723                                 Assert.IsNotNull (ex.InnerException, "#6");
724
725                                 // The filename, directory name, or volume label is incorrect
726                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#7");
727                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
728                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#8");
729                                 Assert.IsNotNull (win32Error.Message, "#9");
730                                 Assert.AreEqual (1060, win32Error.NativeErrorCode, "#10");
731                                 Assert.IsNull (win32Error.InnerException, "#11");
732                         }
733                 }
734
735                 [Test]
736                 public void CanShutdown_Service_OperationNotValid ()
737                 {
738                         if (RunningOnUnix)
739                                 Assert.Ignore ("Running on Unix.");
740
741                         ServiceController sc1 = new ServiceController (UNCONTROLLABLE_SERVICE.ServiceName, ".");
742                         Assert.IsFalse (sc1.CanShutdown);
743                 }
744
745                 [Test]
746                 public void CanShutdown_Service_Running ()
747                 {
748                         if (RunningOnUnix)
749                                 Assert.Ignore ("Running on Unix.");
750
751                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
752                         Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#1");
753                         Assert.IsTrue (sc.CanShutdown, "#2");
754                 }
755
756                 [Test]
757                 public void CanShutdown_Service_Paused ()
758                 {
759                         if (RunningOnUnix)
760                                 Assert.Ignore ("Running on Unix.");
761
762                         ServiceController sc1 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
763                         ServiceController sc2 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
764
765                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#A1");
766                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#A2");
767
768                         Assert.IsTrue (sc1.CanShutdown, "#B1");
769                         Assert.IsTrue (sc2.CanShutdown, "#B2");
770
771                         sc1.Pause ();
772
773                         try {
774                                 Assert.IsTrue (sc1.CanShutdown, "#C1");
775                                 Assert.IsTrue (sc2.CanShutdown, "#C2");
776
777                                 sc1.WaitForStatus (ServiceControllerStatus.Paused, new TimeSpan (0, 0, 5));
778
779                                 Assert.IsTrue (sc1.CanShutdown, "#D1");
780                                 Assert.IsTrue (sc2.CanShutdown, "#D2");
781                         } finally {
782                                 EnsureServiceIsRunning (sc1);
783                                 sc2.Refresh ();
784                         }
785
786                         Assert.IsTrue (sc1.CanShutdown, "#E1");
787                         Assert.IsTrue (sc2.CanShutdown, "#E2");
788
789                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#F1");
790                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#F2");
791                 }
792
793                 [Test]
794                 public void CanShutdown_Service_Stopped ()
795                 {
796                         if (RunningOnUnix)
797                                 Assert.Ignore ("Running on Unix.");
798
799                         ServiceController sc1 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
800                         ServiceController sc2 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
801
802                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#A1");
803                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#A2");
804
805                         Assert.IsTrue (sc1.CanShutdown, "#B1");
806                         Assert.IsTrue (sc2.CanShutdown, "#B2");
807
808                         sc1.Stop ();
809
810                         try {
811                                 Assert.IsTrue (sc1.CanShutdown, "#C1");
812                                 Assert.IsTrue (sc2.CanShutdown, "#C2");
813
814                                 sc1.WaitForStatus (ServiceControllerStatus.Stopped, new TimeSpan (0, 0, 5));
815
816                                 Assert.IsFalse (sc1.CanShutdown, "#D1");
817                                 Assert.IsTrue (sc2.CanShutdown, "#D2");
818                         } finally {
819                                 EnsureServiceIsRunning (sc1);
820                                 sc2.Refresh ();
821                         }
822
823                         Assert.IsTrue (sc1.CanShutdown, "#E1");
824                         Assert.IsTrue (sc2.CanShutdown, "#E2");
825
826                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#F1");
827                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#F2");
828                 }
829
830                 [Test]
831                 public void CanShutdown_ServiceName_Empty ()
832                 {
833                         ServiceController sc = new ServiceController ();
834                         try {
835                                 bool canShutdown = sc.CanShutdown;
836                                 Assert.Fail ("#1: " + canShutdown);
837                         } catch (ArgumentException ex) {
838                                 // Service name  contains invalid characters, is empty or is
839                                 // too long (max length = 80)
840                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
841                                 Assert.IsNotNull (ex.Message, "#3");
842                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#4");
843                                 Assert.IsTrue (ex.Message.IndexOf ("80") != -1, "#5");
844                                 Assert.IsNull (ex.ParamName, "#6");
845                                 Assert.IsNull (ex.InnerException, "#7");
846                         }
847                 }
848
849                 [Test]
850                 public void CanStop ()
851                 {
852                         if (RunningOnUnix)
853                                 Assert.Ignore ("Running on Unix.");
854
855                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
856                         Assert.IsTrue (sc.CanStop, "#1");
857                         sc.ServiceName = UNCONTROLLABLE_SERVICE.ServiceName;
858                         Assert.IsFalse (sc.CanStop, "#2");
859                         sc.DisplayName = CONTROLLABLE_SERVICE.DisplayName;
860                         Assert.IsTrue (sc.CanStop, "#3");
861                         sc.MachineName = "doesnotexist";
862                         try {
863                                 bool value = sc.CanStop;
864                                 Assert.Fail ("#4: " + value.ToString ());
865                         } catch (InvalidOperationException ex) {
866                                 // Cannot open Service Control Manager on computer 'doesnotexist'.
867                                 // This operation might require other priviliges
868                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#5");
869                                 Assert.IsNotNull (ex.Message, "#6");
870                                 Assert.IsTrue (ex.Message.IndexOf ("'doesnotexist'") != -1, "#7");
871                                 Assert.IsNotNull (ex.InnerException, "#8");
872
873                                 // The RPC server is unavailable
874                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#9");
875                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
876                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#10");
877                                 Assert.IsNotNull (win32Error.Message, "#11");
878                                 Assert.AreEqual (1722, win32Error.NativeErrorCode, "#12");
879                                 Assert.IsNull (win32Error.InnerException, "#13");
880                         }
881                 }
882
883                 [Test]
884                 public void CanStop_Machine_DoesNotExist ()
885                 {
886                         if (RunningOnUnix)
887                                 Assert.Ignore ("Running on Unix.");
888
889                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName,
890                                 "doesnotexist");
891                         try {
892                                 bool canStop = sc.CanStop;
893                                 Assert.Fail ("#1: " + canStop);
894                         } catch (InvalidOperationException ex) {
895                                 // Cannot open Service Control Manager on computer 'doesnotexist'.
896                                 // This operation might require other priviliges
897                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
898                                 Assert.IsNotNull (ex.Message, "#3");
899                                 Assert.IsTrue (ex.Message.IndexOf ("'doesnotexist'") != -1, "#4");
900                                 Assert.IsNotNull (ex.InnerException, "#5");
901
902                                 // The RPC server is unavailable
903                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#6");
904                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
905                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#7");
906                                 Assert.IsNotNull (win32Error.Message, "#8");
907                                 Assert.AreEqual (1722, win32Error.NativeErrorCode, "#9");
908                                 Assert.IsNull (win32Error.InnerException, "#10");
909                         }
910                 }
911
912                 [Test]
913                 public void CanStop_Service_Disabled ()
914                 {
915                         if (RunningOnUnix)
916                                 Assert.Ignore ("Running on Unix.");
917
918                         ServiceController sc1 = new ServiceController (DISABLED_SERVICE.ServiceName, ".");
919                         Assert.IsFalse (sc1.CanStop);
920                 }
921
922                 [Test]
923                 public void CanStop_Service_DoesNotExist ()
924                 {
925                         if (RunningOnUnix)
926                                 Assert.Ignore ("Running on Unix.");
927
928                         ServiceController sc = new ServiceController ("doesnotexist", ".");
929                         try {
930                                 bool canStop = sc.CanStop;
931                                 Assert.Fail ("#1: " + canStop);
932                         } catch (InvalidOperationException ex) {
933                                 // Cannot open doesnotexist service on computer '.'
934                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
935                                 Assert.IsNotNull (ex.Message, "#3");
936                                 Assert.IsTrue (ex.Message.IndexOf ("doesnotexist") != -1, "#4");
937                                 Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#5");
938                                 Assert.IsNotNull (ex.InnerException, "#6");
939
940                                 // The filename, directory name, or volume label is incorrect
941                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#7");
942                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
943                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#8");
944                                 Assert.IsNotNull (win32Error.Message, "#9");
945                                 Assert.AreEqual (1060, win32Error.NativeErrorCode, "#10");
946                                 Assert.IsNull (win32Error.InnerException, "#11");
947                         }
948                 }
949
950                 [Test]
951                 public void CanStop_Service_OperationNotValid ()
952                 {
953                         if (RunningOnUnix)
954                                 Assert.Ignore ("Running on Unix.");
955
956                         ServiceController sc1 = new ServiceController (UNCONTROLLABLE_SERVICE.ServiceName, ".");
957                         Assert.IsFalse (sc1.CanStop);
958                 }
959
960                 [Test]
961                 public void CanStop_Service_Running ()
962                 {
963                         if (RunningOnUnix)
964                                 Assert.Ignore ("Running on Unix.");
965
966                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
967                         Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#1");
968                         Assert.IsTrue (sc.CanStop, "#2");
969                 }
970
971                 [Test]
972                 public void CanStop_Service_Paused ()
973                 {
974                         if (RunningOnUnix)
975                                 Assert.Ignore ("Running on Unix.");
976
977                         ServiceController sc1 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
978                         ServiceController sc2 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
979
980                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#A1");
981                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#A2");
982
983                         Assert.IsTrue (sc1.CanStop, "#B1");
984                         Assert.IsTrue (sc2.CanStop, "#B2");
985
986                         sc1.Pause ();
987
988                         try {
989                                 Assert.IsTrue (sc1.CanStop, "#C1");
990                                 Assert.IsTrue (sc2.CanStop, "#C2");
991
992                                 sc1.WaitForStatus (ServiceControllerStatus.Paused, new TimeSpan (0, 0, 5));
993
994                                 Assert.IsTrue (sc1.CanStop, "#D1");
995                                 Assert.IsTrue (sc2.CanStop, "#D2");
996                         } finally {
997                                 EnsureServiceIsRunning (sc1);
998                                 sc2.Refresh ();
999                         }
1000
1001                         Assert.IsTrue (sc1.CanStop, "#E1");
1002                         Assert.IsTrue (sc2.CanStop, "#E2");
1003
1004                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#F1");
1005                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#F2");
1006                 }
1007
1008                 [Test]
1009                 public void CanStop_Service_Stopped ()
1010                 {
1011                         if (RunningOnUnix)
1012                                 Assert.Ignore ("Running on Unix.");
1013
1014                         ServiceController sc1 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
1015                         ServiceController sc2 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
1016
1017                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#A1");
1018                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#A2");
1019
1020                         Assert.IsTrue (sc1.CanStop, "#B1");
1021                         Assert.IsTrue (sc2.CanStop, "#B2");
1022
1023                         sc1.Stop ();
1024
1025                         try {
1026                                 Assert.IsTrue (sc1.CanStop, "#C1");
1027                                 Assert.IsTrue (sc2.CanStop, "#C2");
1028
1029                                 sc1.WaitForStatus (ServiceControllerStatus.Stopped, new TimeSpan (0, 0, 5));
1030
1031                                 Assert.IsFalse (sc1.CanStop, "#D1");
1032                                 Assert.IsTrue (sc2.CanStop, "#D2");
1033                         } finally {
1034                                 EnsureServiceIsRunning (sc1);
1035                                 sc2.Refresh ();
1036                         }
1037
1038                         Assert.IsTrue (sc1.CanShutdown, "#E1");
1039                         Assert.IsTrue (sc2.CanShutdown, "#E2");
1040
1041                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#F1");
1042                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#F2");
1043                 }
1044
1045                 [Test]
1046                 public void CanStop_ServiceName_Empty ()
1047                 {
1048                         ServiceController sc = new ServiceController ();
1049                         try {
1050                                 bool canStop = sc.CanStop;
1051                                 Assert.Fail ("#1: " + canStop);
1052                         } catch (ArgumentException ex) {
1053                                 // Service name  contains invalid characters, is empty or is
1054                                 // too long (max length = 80)
1055                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1056                                 Assert.IsNotNull (ex.Message, "#3");
1057                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#4");
1058                                 Assert.IsTrue (ex.Message.IndexOf ("80") != -1, "#5");
1059                                 Assert.IsNull (ex.ParamName, "#6");
1060                                 Assert.IsNull (ex.InnerException, "#7");
1061                         }
1062                 }
1063
1064                 [Test]
1065                 public void Continue ()
1066                 {
1067                         if (RunningOnUnix)
1068                                 Assert.Ignore ("Running on Unix.");
1069
1070                         ServiceController sc1 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
1071                         ServiceController sc2 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
1072
1073                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#A1");
1074                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#A2");
1075
1076                         sc1.Pause ();
1077
1078                         try {
1079                                 Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#B1");
1080                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#B2");
1081
1082                                 sc1.WaitForStatus (ServiceControllerStatus.Paused, new TimeSpan (0, 0, 5));
1083
1084                                 Assert.AreEqual (ServiceControllerStatus.Paused, sc1.Status, "#C1");
1085                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#C2");
1086
1087                                 sc1.Continue ();
1088
1089                                 Assert.AreEqual (ServiceControllerStatus.Paused, sc1.Status, "#D1");
1090                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#D2");
1091
1092                                 sc1.WaitForStatus (ServiceControllerStatus.Running, new TimeSpan (0, 0, 5));
1093
1094                                 Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#E1");
1095                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#E2");
1096                         } finally {
1097                                 EnsureServiceIsRunning (sc1);
1098                                 sc2.Refresh ();
1099                         }
1100                 }
1101
1102                 [Test]
1103                 public void Continue_Machine_DoesNotExist ()
1104                 {
1105                         if (RunningOnUnix)
1106                                 Assert.Ignore ("Running on Unix.");
1107
1108                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName,
1109                                 "doesnotexist");
1110                         try {
1111                                 sc.Continue ();
1112                                 Assert.Fail ("#1");
1113                         } catch (InvalidOperationException ex) {
1114                                 // Cannot open Service Control Manager on computer 'doesnotexist'.
1115                                 // This operation might require other priviliges
1116                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
1117                                 Assert.IsNotNull (ex.Message, "#3");
1118                                 Assert.IsTrue (ex.Message.IndexOf ("'doesnotexist'") != -1, "#4");
1119                                 Assert.IsNotNull (ex.InnerException, "#5");
1120
1121                                 // The RPC server is unavailable
1122                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#6");
1123                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
1124                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#7");
1125                                 Assert.IsNotNull (win32Error.Message, "#8");
1126                                 Assert.AreEqual (1722, win32Error.NativeErrorCode, "#9");
1127                                 Assert.IsNull (win32Error.InnerException, "#10");
1128                         }
1129                 }
1130
1131                 [Test]
1132                 public void Continue_Service_Disabled ()
1133                 {
1134                         if (RunningOnUnix)
1135                                 Assert.Ignore ("Running on Unix.");
1136
1137                         ServiceController sc1 = new ServiceController (DISABLED_SERVICE.ServiceName, ".");
1138                         ServiceController sc2 = new ServiceController (DISABLED_SERVICE.ServiceName, ".");
1139
1140                         Assert.AreEqual (ServiceControllerStatus.Stopped, sc1.Status, "#A1");
1141                         Assert.AreEqual (ServiceControllerStatus.Stopped, sc2.Status, "#A2");
1142
1143                         try {
1144                                 sc1.Continue ();
1145                                 Assert.Fail ("#B1");
1146                         } catch (InvalidOperationException ex) {
1147                                 // Cannot resume NetDDE service on computer '.'
1148                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
1149                                 Assert.IsNotNull (ex.Message, "#B3");
1150                                 Assert.IsTrue (ex.Message.IndexOf (DISABLED_SERVICE.ServiceName) != -1, "#B4");
1151                                 Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#B5");
1152                                 Assert.IsNotNull (ex.InnerException, "#B6");
1153
1154                                 // The service has not been started
1155                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#B7");
1156                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
1157                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#B8");
1158                                 Assert.IsNotNull (win32Error.Message, "#B9");
1159                                 Assert.AreEqual (1062, win32Error.NativeErrorCode, "#B10");
1160                                 Assert.IsNull (win32Error.InnerException, "#B11");
1161                         }
1162
1163                         Assert.AreEqual (ServiceControllerStatus.Stopped, sc1.Status, "#C1");
1164                         Assert.AreEqual (ServiceControllerStatus.Stopped, sc2.Status, "#C2");
1165                 }
1166
1167                 [Test]
1168                 public void Continue_Service_DoesNotExist ()
1169                 {
1170                         if (RunningOnUnix)
1171                                 Assert.Ignore ("Running on Unix.");
1172
1173                         ServiceController sc = new ServiceController ("doesnotexist", ".");
1174                         try {
1175                                 sc.Continue ();
1176                                 Assert.Fail ("#1");
1177                         } catch (InvalidOperationException ex) {
1178                                 // Cannot open doesnotexist service on computer '.'
1179                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
1180                                 Assert.IsNotNull (ex.Message, "#3");
1181                                 Assert.IsTrue (ex.Message.IndexOf ("doesnotexist") != -1, "#4");
1182                                 Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#5");
1183                                 Assert.IsNotNull (ex.InnerException, "#6");
1184
1185                                 // The filename, directory name, or volume label is incorrect
1186                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#7");
1187                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
1188                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#8");
1189                                 Assert.IsNotNull (win32Error.Message, "#9");
1190                                 Assert.AreEqual (1060, win32Error.NativeErrorCode, "#10");
1191                                 Assert.IsNull (win32Error.InnerException, "#11");
1192                         }
1193                 }
1194
1195                 [Test]
1196                 public void Continue_Service_OperationNotValid ()
1197                 {
1198                         if (RunningOnUnix)
1199                                 Assert.Ignore ("Running on Unix.");
1200
1201                         ServiceController sc1 = new ServiceController (UNCONTROLLABLE_SERVICE.ServiceName, ".");
1202                         ServiceController sc2 = new ServiceController (UNCONTROLLABLE_SERVICE.ServiceName, ".");
1203
1204                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#A1");
1205                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#A2");
1206
1207                         try {
1208                                 sc1.Continue ();
1209                                 Assert.Fail ("#B1");
1210                         } catch (InvalidOperationException ex) {
1211                                 // Cannot resume service on computer '.'
1212                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
1213                                 Assert.IsNotNull (ex.Message, "#B3");
1214                                 Assert.IsTrue (ex.Message.IndexOf (UNCONTROLLABLE_SERVICE.ServiceName) != -1, "#B4");
1215                                 Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#B5");
1216                                 Assert.IsNotNull (ex.InnerException, "#B6");
1217
1218                                 // The requested control is not valid for this service
1219                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#B7");
1220                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
1221                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#B8");
1222                                 Assert.IsNotNull (win32Error.Message, "#B9");
1223                                 Assert.AreEqual (1052, win32Error.NativeErrorCode, "#B10");
1224                                 Assert.IsNull (win32Error.InnerException, "#B11");
1225                         }
1226
1227                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#C1");
1228                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#C2");
1229                 }
1230
1231                 [Test]
1232                 public void Continue_Service_Running ()
1233                 {
1234                         if (RunningOnUnix)
1235                                 Assert.Ignore ("Running on Unix.");
1236
1237                         ServiceController sc1 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
1238                         ServiceController sc2 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
1239
1240                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#A1");
1241                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#A2");
1242
1243                         sc1.Continue ();
1244
1245                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#B1");
1246                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#B2");
1247                 }
1248
1249                 [Test]
1250                 public void Continue_Service_Stopped ()
1251                 {
1252                         if (RunningOnUnix)
1253                                 Assert.Ignore ("Running on Unix.");
1254
1255                         ServiceController sc1 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
1256                         ServiceController sc2 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
1257
1258                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#A1");
1259                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#A2");
1260
1261                         sc1.Stop ();
1262
1263                         try {
1264                                 Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#B1");
1265                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#B2");
1266
1267                                 sc1.WaitForStatus (ServiceControllerStatus.Stopped, new TimeSpan (0, 0, 5));
1268
1269                                 Assert.AreEqual (ServiceControllerStatus.Stopped, sc1.Status, "#C1");
1270                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#C2");
1271
1272                                 sc1.Continue ();
1273                                 Assert.Fail ("#D1");
1274                         } catch (InvalidOperationException ex) {
1275                                 // Cannot resume the service on computer '.'
1276                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
1277                                 Assert.IsNotNull (ex.Message, "#D3");
1278                                 Assert.IsTrue (ex.Message.IndexOf (CONTROLLABLE_SERVICE.ServiceName) != -1, "#D4");
1279                                 Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#D5");
1280                                 Assert.IsNotNull (ex.InnerException, "#D6");
1281
1282                                 // The service has not been started
1283                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#D7");
1284                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
1285                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#D8");
1286                                 Assert.IsNotNull (win32Error.Message, "#D9");
1287                                 Assert.AreEqual (1062, win32Error.NativeErrorCode, "#D10");
1288                                 Assert.IsNull (win32Error.InnerException, "#D11");
1289                         } finally {
1290                                 EnsureServiceIsRunning (sc1);
1291                                 sc2.Refresh ();
1292                         }
1293
1294                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#E1");
1295                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#E2");
1296                 }
1297
1298                 [Test]
1299                 public void Continue_ServiceName_Empty ()
1300                 {
1301                         ServiceController sc = new ServiceController ();
1302                         try {
1303                                 sc.Continue ();
1304                                 Assert.Fail ("#1");
1305                         } catch (ArgumentException ex) {
1306                                 // Service name  contains invalid characters, is empty or is
1307                                 // too long (max length = 80)
1308                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1309                                 Assert.IsNotNull (ex.Message, "#3");
1310                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#4");
1311                                 Assert.IsTrue (ex.Message.IndexOf ("80") != -1, "#5");
1312                                 Assert.IsNull (ex.ParamName, "#6");
1313                                 Assert.IsNull (ex.InnerException, "#7");
1314                         }
1315                 }
1316
1317                 [Test]
1318                 public void DependentServices ()
1319                 {
1320                         if (RunningOnUnix)
1321                                 Assert.Ignore ("Running on Unix.");
1322
1323                         ServiceController sc = null;
1324                         ServiceController [] dependentServices = null;
1325
1326                         // single dependent service
1327                         sc = new ServiceController (SERVICE_WITH_ONE_DEPENDENT.ServiceName, ".");
1328                         dependentServices = sc.DependentServices;
1329                         Assert.IsNotNull (dependentServices, "#A1");
1330                         Assert.AreEqual (1, dependentServices.Length, "#A2");
1331                         Assert.AreEqual (SERVICE_WITH_ONE_DEPENDENT.Dependents, ServiceNames (dependentServices), "#A3");
1332
1333                         // modifying ServiceName does not cause cache to be cleared:
1334                         // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=201762
1335                         sc.ServiceName = SERVICE_WITH_NO_DEPENDENTS.ServiceName;
1336                         dependentServices = sc.DependentServices;
1337                         Assert.IsNotNull (dependentServices, "#B1");
1338                         Assert.AreEqual (1, dependentServices.Length, "#B2");
1339                         Assert.AreEqual (SERVICE_WITH_ONE_DEPENDENT.Dependents, ServiceNames (dependentServices), "#B3");
1340
1341                         // modifying DisplayName does not cause cache to be cleared:
1342                         // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=201762
1343                         sc.DisplayName = SERVICE_WITH_MANY_DEPENDENTS.DisplayName;
1344                         dependentServices = sc.DependentServices;
1345                         Assert.IsNotNull (dependentServices, "#C1");
1346                         Assert.AreEqual (1, dependentServices.Length, "#C2");
1347                         Assert.AreEqual (SERVICE_WITH_ONE_DEPENDENT.Dependents, ServiceNames (dependentServices), "#C3");
1348
1349                         // modifying MachineName does not cause cache to be cleared:
1350                         // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=201762
1351                         sc.MachineName = "doesnotexist";
1352                         dependentServices = sc.DependentServices;
1353                         Assert.IsNotNull (dependentServices, "#D1");
1354                         Assert.AreEqual (1, dependentServices.Length, "#D");
1355                         Assert.AreEqual (SERVICE_WITH_ONE_DEPENDENT.Dependents, ServiceNames (dependentServices), "#D3");
1356
1357                         // no dependent services
1358                         sc = new ServiceController (SERVICE_WITH_NO_DEPENDENTS.ServiceName, ".");
1359                         dependentServices = sc.DependentServices;
1360                         Assert.IsNotNull (dependentServices, "#E1");
1361                         Assert.AreEqual (0, dependentServices.Length, "#E2");
1362
1363                         // modifying ServiceName does not cause cache to be cleared:
1364                         // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=201762
1365                         sc.ServiceName = SERVICE_WITH_MANY_DEPENDENTS.ServiceName;
1366                         dependentServices = sc.DependentServices;
1367                         Assert.IsNotNull (dependentServices, "#F1");
1368                         Assert.AreEqual (0, dependentServices.Length, "#F2");
1369
1370                         // modifying DisplayName does not cause cache to be cleared:
1371                         // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=201762
1372                         sc.DisplayName = SERVICE_WITH_ONE_DEPENDENT.DisplayName;
1373                         dependentServices = sc.DependentServices;
1374                         Assert.IsNotNull (dependentServices, "#G1");
1375                         Assert.AreEqual (0, dependentServices.Length, "#G2");
1376
1377                         // modifying MachineName does not cause cache to be cleared:
1378                         // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=201762
1379                         sc.MachineName = Environment.MachineName;
1380                         dependentServices = sc.DependentServices;
1381                         Assert.IsNotNull (dependentServices, "#H1");
1382                         Assert.AreEqual (0, dependentServices.Length, "#H2");
1383
1384                         // multiple dependent services
1385                         sc = new ServiceController (SERVICE_WITH_MANY_DEPENDENTS.ServiceName, ".");
1386                         dependentServices = sc.DependentServices;
1387                         Assert.IsNotNull (dependentServices, "#I1");
1388                         Assert.AreEqual (SERVICE_WITH_MANY_DEPENDENTS.Dependents.Length, dependentServices.Length, "#I2");
1389                         Assert.AreEqual (SERVICE_WITH_MANY_DEPENDENTS.Dependents, ServiceNames (dependentServices), "#I3");
1390
1391                         // modifying ServiceName does not cause cache to be cleared:
1392                         // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=201762
1393                         sc.ServiceName = SERVICE_WITH_NO_DEPENDENTS.ServiceName;
1394                         dependentServices = sc.DependentServices;
1395                         Assert.IsNotNull (dependentServices, "#J1");
1396                         Assert.AreEqual (SERVICE_WITH_MANY_DEPENDENTS.Dependents.Length, dependentServices.Length, "#J3");
1397                         Assert.AreEqual (SERVICE_WITH_MANY_DEPENDENTS.Dependents, ServiceNames (dependentServices), "#J3");
1398
1399                         // modifying DisplayName does not cause cache to be cleared:
1400                         // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=201762
1401                         sc.DisplayName = SERVICE_WITH_ONE_DEPENDENT.DisplayName;
1402                         dependentServices = sc.DependentServices;
1403                         Assert.IsNotNull (dependentServices, "#K1");
1404                         Assert.AreEqual (SERVICE_WITH_MANY_DEPENDENTS.Dependents.Length, dependentServices.Length, "#K2");
1405                         Assert.AreEqual (SERVICE_WITH_MANY_DEPENDENTS.Dependents, ServiceNames (dependentServices), "#K3");
1406
1407                         // modifying MachineName does not cause cache to be cleared:
1408                         // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=201762
1409                         sc.MachineName = Environment.MachineName;
1410                         dependentServices = sc.DependentServices;
1411                         Assert.IsNotNull (dependentServices, "#L1");
1412                         Assert.AreEqual (SERVICE_WITH_MANY_DEPENDENTS.Dependents.Length, dependentServices.Length, "#L2");
1413                         Assert.AreEqual (SERVICE_WITH_MANY_DEPENDENTS.Dependents, ServiceNames (dependentServices), "#L3");
1414                 }
1415
1416                 [Test]
1417                 public void DependentServices_Machine_DoesNotExist ()
1418                 {
1419                         if (RunningOnUnix)
1420                                 Assert.Ignore ("Running on Unix.");
1421
1422                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName,
1423                                 "doesnotexist");
1424                         try {
1425                                 ServiceController [] dependenServices = sc.DependentServices;
1426                                 Assert.Fail ("#1: " + dependenServices.Length);
1427                         } catch (InvalidOperationException ex) {
1428                                 // Cannot open Service Control Manager on computer 'doesnotexist'.
1429                                 // This operation might require other priviliges
1430                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
1431                                 Assert.IsNotNull (ex.Message, "#3");
1432                                 Assert.IsTrue (ex.Message.IndexOf ("'doesnotexist'") != -1, "#4");
1433                                 Assert.IsNotNull (ex.InnerException, "#5");
1434
1435                                 // The RPC server is unavailable
1436                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#6");
1437                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
1438                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#7");
1439                                 Assert.IsNotNull (win32Error.Message, "#8");
1440                                 Assert.AreEqual (1722, win32Error.NativeErrorCode, "#9");
1441                                 Assert.IsNull (win32Error.InnerException, "#10");
1442                         }
1443                 }
1444
1445                 [Test]
1446                 public void DependentServices_Service_Disabled ()
1447                 {
1448                         if (RunningOnUnix)
1449                                 Assert.Ignore ("Running on Unix.");
1450
1451                         ServiceController sc = new ServiceController (DISABLED_SERVICE.ServiceName, ".");
1452                         ServiceController [] dependentServices = sc.DependentServices;
1453                         Assert.IsNotNull (dependentServices, "#1");
1454                         Assert.AreEqual (DISABLED_SERVICE.Dependents, ServiceNames (dependentServices), "#2");
1455                 }
1456
1457                 [Test]
1458                 public void DependentServices_Service_DoesNotExist ()
1459                 {
1460                         if (RunningOnUnix)
1461                                 Assert.Ignore ("Running on Unix.");
1462
1463                         ServiceController sc = new ServiceController ("doesnotexist", ".");
1464                         try {
1465                                 ServiceController [] dependenServices = sc.DependentServices;
1466                                 Assert.Fail ("#1: " + dependenServices.Length);
1467                         } catch (InvalidOperationException ex) {
1468                                 // Cannot open doesnotexist service on computer '.'
1469                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
1470                                 Assert.IsNotNull (ex.Message, "#3");
1471                                 Assert.IsTrue (ex.Message.IndexOf ("doesnotexist") != -1, "#4");
1472                                 Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#5");
1473                                 Assert.IsNotNull (ex.InnerException, "#6");
1474
1475                                 // The filename, directory name, or volume label is incorrect
1476                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#7");
1477                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
1478                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#8");
1479                                 Assert.IsNotNull (win32Error.Message, "#9");
1480                                 Assert.AreEqual (1060, win32Error.NativeErrorCode, "#10");
1481                                 Assert.IsNull (win32Error.InnerException, "#11");
1482                         }
1483                 }
1484
1485                 [Test]
1486                 public void DependentServices_ServiceName_Empty ()
1487                 {
1488                         ServiceController sc = new ServiceController ();
1489                         try {
1490                                 ServiceController [] dependenServices = sc.DependentServices;
1491                                 Assert.Fail ("#1: " + dependenServices.Length);
1492                         } catch (ArgumentException ex) {
1493                                 // Service name  contains invalid characters, is empty or is
1494                                 // too long (max length = 80)
1495                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1496                                 Assert.IsNotNull (ex.Message, "#3");
1497                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#4");
1498                                 Assert.IsTrue (ex.Message.IndexOf ("80") != -1, "#5");
1499                                 Assert.IsNull (ex.ParamName, "#6");
1500                                 Assert.IsNull (ex.InnerException, "#7");
1501                         }
1502                 }
1503
1504                 [Test]
1505                 public void DisplayName ()
1506                 {
1507                         if (RunningOnUnix)
1508                                 Assert.Ignore ("Running on Unix.");
1509
1510                         var s1 = WORKSTATION_SERVICE;
1511                         var s2 = WINDOWS_SEARCH_SERVICE;
1512
1513                         ServiceController sc = new ServiceController ();
1514                         sc.DisplayName = s1.DisplayName.ToLower ();
1515                         Assert.AreEqual (s1.DisplayName.ToLower (), sc.DisplayName, "#A1");
1516                         Assert.AreEqual (s1.ServiceName, sc.ServiceName, "#A2");
1517
1518                         sc.DisplayName = s2.DisplayName.ToLower ();
1519                         Assert.AreEqual (s2.DisplayName.ToLower (), sc.DisplayName, "#B1");
1520                         Assert.AreEqual (s2.ServiceName, sc.ServiceName, "#B2");
1521
1522                         sc = new ServiceController (s1.DisplayName.ToLower ());
1523                         sc.DisplayName = s2.DisplayName.ToLower ();
1524                         Assert.AreEqual (s2.DisplayName.ToLower (), sc.DisplayName, "#C1");
1525                         Assert.AreEqual (s1.ServiceName, sc.ServiceName, "#C2");
1526                         Assert.AreEqual (s1.DisplayName.ToLower (), sc.DisplayName, "#C3");
1527
1528                         sc.DisplayName = s2.DisplayName.ToLower ();
1529                         Assert.AreEqual (s2.DisplayName.ToLower (), sc.DisplayName, "#D1");
1530                         Assert.AreEqual (s2.ServiceName, sc.ServiceName, "#D2");
1531
1532                         sc.DisplayName = s1.DisplayName;
1533                         Assert.AreEqual (s1.DisplayName, sc.DisplayName, "#E1");
1534                         Assert.AreEqual (s1.ServiceName, sc.ServiceName, "#E2");
1535
1536                         sc = new ServiceController (s1.DisplayName.ToLower ());
1537                         Assert.AreEqual (s1.DisplayName.ToLower (), sc.DisplayName, "#F1");
1538                         Assert.AreEqual (s1.ServiceName, sc.ServiceName, "#F2");
1539
1540                         sc.DisplayName = s1.DisplayName;
1541                         Assert.AreEqual (s1.DisplayName, sc.DisplayName, "#G1");
1542                         Assert.AreEqual (s1.ServiceName, sc.ServiceName, "#G2");
1543                 }
1544
1545                 [Test]
1546                 public void DisplayName_Machine_DoesNotExist ()
1547                 {
1548                         if (RunningOnUnix)
1549                                 Assert.Ignore ("Running on Unix.");
1550
1551                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName,
1552                                 "doesnotexist");
1553                         try {
1554                                 string displayName = sc.DisplayName;
1555                                 Assert.Fail ("#1: " + displayName);
1556                         } catch (InvalidOperationException ex) {
1557                                 // Cannot open Service Control Manager on computer 'doesnotexist'.
1558                                 // This operation might require other priviliges
1559                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
1560                                 Assert.IsNotNull (ex.Message, "#3");
1561                                 Assert.IsTrue (ex.Message.IndexOf ("'doesnotexist'") != -1, "#4");
1562                                 Assert.IsNotNull (ex.InnerException, "#5");
1563
1564                                 // The RPC server is unavailable
1565                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#6");
1566                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
1567                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#7");
1568                                 Assert.IsNotNull (win32Error.Message, "#8");
1569                                 Assert.AreEqual (1722, win32Error.NativeErrorCode, "#9");
1570                                 Assert.IsNull (win32Error.InnerException, "#10");
1571                         }
1572                 }
1573
1574                 [Test]
1575                 public void DisplayName_Service_Disabled ()
1576                 {
1577                         if (RunningOnUnix)
1578                                 Assert.Ignore ("Running on Unix.");
1579
1580                         ServiceController sc = new ServiceController (DISABLED_SERVICE.ServiceName, ".");
1581                         Assert.AreEqual (DISABLED_SERVICE.DisplayName, sc.DisplayName);
1582                 }
1583
1584                 [Test]
1585                 public void DisplayName_Service_DoesNotExist ()
1586                 {
1587                         if (RunningOnUnix)
1588                                 Assert.Ignore ("Running on Unix.");
1589
1590                         ServiceController sc = new ServiceController ("doesnotexist", ".");
1591                         try {
1592                                 string displayName = sc.DisplayName;
1593                                 Assert.Fail ("#1: " + displayName);
1594                         } catch (InvalidOperationException ex) {
1595                                 // Cannot open doesnotexist service on computer '.'
1596                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
1597                                 Assert.IsNotNull (ex.Message, "#3");
1598                                 Assert.IsTrue (ex.Message.IndexOf ("doesnotexist") != -1, "#4");
1599                                 Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#5");
1600                                 Assert.IsNotNull (ex.InnerException, "#6");
1601
1602                                 // The filename, directory name, or volume label is incorrect
1603                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#7");
1604                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
1605                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#8");
1606                                 Assert.IsNotNull (win32Error.Message, "#9");
1607                                 Assert.AreEqual (1060, win32Error.NativeErrorCode, "#10");
1608                                 Assert.IsNull (win32Error.InnerException, "#11");
1609                         }
1610                 }
1611
1612                 [Test]
1613                 public void DisplayName_ServiceName_Empty ()
1614                 {
1615                         if (RunningOnUnix)
1616                                 Assert.Ignore ("Running on Unix.");
1617
1618                         ServiceController sc = new ServiceController ();
1619                         sc.DisplayName = WORKSTATION_SERVICE.DisplayName.ToLower ();
1620                         Assert.AreEqual (WORKSTATION_SERVICE.DisplayName.ToLower (), sc.DisplayName, "#A1");
1621                         Assert.AreEqual (WORKSTATION_SERVICE.ServiceName, sc.ServiceName, "#A2");
1622                 }
1623
1624                 [Test]
1625                 public void DisplayName_Value_DoesNotExist ()
1626                 {
1627                         if (RunningOnUnix)
1628                                 Assert.Ignore ("Running on Unix.");
1629
1630                         ServiceController sc = new ServiceController ();
1631                         sc.DisplayName = "doesnotexist";
1632                         Assert.AreEqual ("doesnotexist", sc.DisplayName, "#1");
1633                         try {
1634                                 string serviceName = sc.ServiceName;
1635                                 Assert.Fail ("#2: " + serviceName);
1636                         } catch (InvalidOperationException ex) {
1637                                 // Service doesnotexist was not found on computer '.'
1638                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#3");
1639                                 Assert.IsNotNull (ex.Message, "#4");
1640                                 Assert.IsTrue (ex.Message.IndexOf ("doesnotexist") != -1, "#5");
1641                                 Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#6");
1642                                 Assert.IsNotNull (ex.InnerException, "#7");
1643
1644                                 // The specified service does not exist as an installed service
1645                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#8");
1646                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
1647                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#9");
1648                                 Assert.IsNotNull (win32Error.Message, "#10");
1649                                 Assert.AreEqual (1060, win32Error.NativeErrorCode, "#11");
1650                                 Assert.IsNull (win32Error.InnerException, "#12");
1651                         }
1652                         Assert.AreEqual ("doesnotexist", sc.DisplayName, "#13");
1653                 }
1654
1655                 [Test]
1656                 public void DisplayName_Value_Empty ()
1657                 {
1658                         if (RunningOnUnix)
1659                                 Assert.Ignore ("Running on Unix.");
1660
1661                         ServiceController sc = new ServiceController ();
1662                         Assert.AreEqual (string.Empty, sc.DisplayName, "#A1");
1663                         Assert.AreEqual (string.Empty, sc.ServiceName, "#A2");
1664
1665                         sc.DisplayName = WORKSTATION_SERVICE.DisplayName;
1666
1667                         Assert.AreEqual (WORKSTATION_SERVICE.DisplayName, sc.DisplayName, "#B1");
1668                         Assert.AreEqual (WORKSTATION_SERVICE.ServiceName, sc.ServiceName, "#B2");
1669
1670                         sc.DisplayName = string.Empty;
1671
1672                         Assert.AreEqual (string.Empty, sc.DisplayName, "#C1");
1673                         Assert.AreEqual (string.Empty, sc.ServiceName, "#C2");
1674                 }
1675
1676                 [Test]
1677                 public void DisplayName_Value_Null ()
1678                 {
1679                         if (RunningOnUnix)
1680                                 Assert.Ignore ("Running on Unix.");
1681
1682                         ServiceController sc = new ServiceController ();
1683                         sc.DisplayName = CONTROLLABLE_SERVICE.DisplayName;
1684                         try {
1685                                 sc.DisplayName = null;
1686                                 Assert.Fail ("#1");
1687                         } catch (ArgumentNullException ex) {
1688                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1689                                 Assert.IsNotNull (ex.Message, "#3");
1690                                 Assert.IsNotNull (ex.ParamName, "#4");
1691                                 Assert.AreEqual ("value", ex.ParamName, "#5");
1692                                 Assert.IsNull (ex.InnerException, "#6");
1693                         }
1694                         Assert.AreEqual (CONTROLLABLE_SERVICE.DisplayName, sc.DisplayName, "#7");
1695                 }
1696
1697                 [Test]
1698                 public void DisplayName_Value_ServiceName ()
1699                 {
1700                         if (RunningOnUnix)
1701                                 Assert.Ignore ("Running on Unix.");
1702
1703                         ServiceController sc = new ServiceController ();
1704                         sc.DisplayName = WORKSTATION_SERVICE.ServiceName;
1705                         Assert.AreEqual (WORKSTATION_SERVICE.ServiceName, sc.DisplayName, "#A1");
1706                         Assert.AreEqual (WORKSTATION_SERVICE.ServiceName, sc.ServiceName, "#A2");
1707                         Assert.AreEqual (WORKSTATION_SERVICE.DisplayName, sc.DisplayName, "#A3");
1708                 }
1709
1710                 [Test]
1711                 public void ExecuteCommand_Device_ControlCodes ()
1712                 {
1713                         if (RunningOnUnix)
1714                                 Assert.Ignore ("Running on Unix.");
1715
1716                         ServiceController sc = new ServiceController (DISK_DRIVER_SERVICE.ServiceName, ".");
1717                         Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#A");
1718
1719                         try {
1720                                 try {
1721                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_CONTINUE);
1722                                         Assert.Fail ("#B1");
1723                                 } catch (InvalidOperationException ex) {
1724                                         // Cannot control XXX service on computer '.'
1725                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
1726                                         Assert.IsNotNull (ex.Message, "#B3");
1727                                         Assert.IsTrue (ex.Message.IndexOf (DISK_DRIVER_SERVICE.ServiceName) != -1, "#B4");
1728                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#B5");
1729                                         Assert.IsNotNull (ex.InnerException, "#B6");
1730
1731                                         // Access is denied
1732                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#B7");
1733                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
1734                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#B8");
1735                                         Assert.IsNotNull (win32Error.Message, "#B9");
1736                                         Assert.AreEqual (5, win32Error.NativeErrorCode, "#B10");
1737                                         Assert.IsNull (win32Error.InnerException, "#B11");
1738                                 }
1739
1740                                 try {
1741                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_DEVICEEVENT);
1742                                         Assert.Fail ("#C1");
1743                                 } catch (InvalidOperationException ex) {
1744                                         // Cannot control XXX service on computer '.'
1745                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
1746                                         Assert.IsNotNull (ex.Message, "#C3");
1747                                         Assert.IsTrue (ex.Message.IndexOf (DISK_DRIVER_SERVICE.ServiceName) != -1, "#C4");
1748                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#C5");
1749                                         Assert.IsNotNull (ex.InnerException, "#C6");
1750
1751                                         // The parameter is incorrect
1752                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#C7");
1753                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
1754                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#C8");
1755                                         Assert.IsNotNull (win32Error.Message, "#C9");
1756                                         Assert.AreEqual (87, win32Error.NativeErrorCode, "#C10");
1757                                         Assert.IsNull (win32Error.InnerException, "#C11");
1758                                 }
1759
1760                                 try {
1761                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_HARDWAREPROFILECHANGE);
1762                                         Assert.Fail ("#D1");
1763                                 } catch (InvalidOperationException ex) {
1764                                         // Cannot control XXX service on computer '.'
1765                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
1766                                         Assert.IsNotNull (ex.Message, "#D3");
1767                                         Assert.IsTrue (ex.Message.IndexOf (DISK_DRIVER_SERVICE.ServiceName) != -1, "#D4");
1768                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#D5");
1769                                         Assert.IsNotNull (ex.InnerException, "#D6");
1770
1771                                         // The parameter is incorrect
1772                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#D7");
1773                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
1774                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#D8");
1775                                         Assert.IsNotNull (win32Error.Message, "#D9");
1776                                         Assert.AreEqual (87, win32Error.NativeErrorCode, "#D10");
1777                                         Assert.IsNull (win32Error.InnerException, "#D11");
1778                                 }
1779
1780                                 try {
1781                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_INTERROGATE);
1782                                         Assert.Fail ("#E1");
1783                                 } catch (InvalidOperationException ex) {
1784                                         // Cannot control XXX service on computer '.'
1785                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#E2");
1786                                         Assert.IsNotNull (ex.Message, "#E3");
1787                                         Assert.IsTrue (ex.Message.IndexOf (DISK_DRIVER_SERVICE.ServiceName) != -1, "#E4");
1788                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#E5");
1789                                         Assert.IsNotNull (ex.InnerException, "#E6");
1790
1791                                         // Access is denied
1792                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#E7");
1793                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
1794                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#E8");
1795                                         Assert.IsNotNull (win32Error.Message, "#E9");
1796                                         Assert.AreEqual (5, win32Error.NativeErrorCode, "#E10");
1797                                         Assert.IsNull (win32Error.InnerException, "#E11");
1798                                 }
1799
1800                                 try {
1801                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_NETBINDADD);
1802                                         Assert.Fail ("#F1");
1803                                 } catch (InvalidOperationException ex) {
1804                                         // Cannot control XXX service on computer '.'
1805                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#F2");
1806                                         Assert.IsNotNull (ex.Message, "#F3");
1807                                         Assert.IsTrue (ex.Message.IndexOf (DISK_DRIVER_SERVICE.ServiceName) != -1, "#F4");
1808                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#F5");
1809                                         Assert.IsNotNull (ex.InnerException, "#F6");
1810
1811                                         // Access is denied
1812                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#F7");
1813                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
1814                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#F8");
1815                                         Assert.IsNotNull (win32Error.Message, "#F9");
1816                                         Assert.AreEqual (5, win32Error.NativeErrorCode, "#F10");
1817                                         Assert.IsNull (win32Error.InnerException, "#F11");
1818                                 }
1819
1820                                 try {
1821                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_NETBINDDISABLE);
1822                                         Assert.Fail ("#G1");
1823                                 } catch (InvalidOperationException ex) {
1824                                         // Cannot control XXX service on computer '.'
1825                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#G2");
1826                                         Assert.IsNotNull (ex.Message, "#G3");
1827                                         Assert.IsTrue (ex.Message.IndexOf (DISK_DRIVER_SERVICE.ServiceName) != -1, "#G4");
1828                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#G5");
1829                                         Assert.IsNotNull (ex.InnerException, "#G6");
1830
1831                                         // Access is denied
1832                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#G7");
1833                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
1834                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#G8");
1835                                         Assert.IsNotNull (win32Error.Message, "#G9");
1836                                         Assert.AreEqual (5, win32Error.NativeErrorCode, "#G10");
1837                                         Assert.IsNull (win32Error.InnerException, "#G11");
1838                                 }
1839
1840                                 try {
1841                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_NETBINDENABLE);
1842                                         Assert.Fail ("#H1");
1843                                 } catch (InvalidOperationException ex) {
1844                                         // Cannot control XXX service on computer '.'
1845                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#H2");
1846                                         Assert.IsNotNull (ex.Message, "#H3");
1847                                         Assert.IsTrue (ex.Message.IndexOf (DISK_DRIVER_SERVICE.ServiceName) != -1, "#H4");
1848                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#H5");
1849                                         Assert.IsNotNull (ex.InnerException, "#H6");
1850
1851                                         // Access is denied
1852                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#H7");
1853                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
1854                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#H8");
1855                                         Assert.IsNotNull (win32Error.Message, "#H9");
1856                                         Assert.AreEqual (5, win32Error.NativeErrorCode, "#H10");
1857                                         Assert.IsNull (win32Error.InnerException, "#H11");
1858                                 }
1859
1860                                 try {
1861                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_NETBINDREMOVE);
1862                                         Assert.Fail ("#I1");
1863                                 } catch (InvalidOperationException ex) {
1864                                         // Cannot control XXX service on computer '.'
1865                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#I2");
1866                                         Assert.IsNotNull (ex.Message, "#I3");
1867                                         Assert.IsTrue (ex.Message.IndexOf (DISK_DRIVER_SERVICE.ServiceName) != -1, "#I4");
1868                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#I5");
1869                                         Assert.IsNotNull (ex.InnerException, "#I6");
1870
1871                                         // Access is denied
1872                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#I7");
1873                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
1874                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#I8");
1875                                         Assert.IsNotNull (win32Error.Message, "#I9");
1876                                         Assert.AreEqual (5, win32Error.NativeErrorCode, "#I10");
1877                                         Assert.IsNull (win32Error.InnerException, "#I11");
1878                                 }
1879
1880                                 try {
1881                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_PARAMCHANGE);
1882                                         Assert.Fail ("#J1");
1883                                 } catch (InvalidOperationException ex) {
1884                                         // Cannot control XXX service on computer '.'
1885                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#J2");
1886                                         Assert.IsNotNull (ex.Message, "#J3");
1887                                         Assert.IsTrue (ex.Message.IndexOf (DISK_DRIVER_SERVICE.ServiceName) != -1, "#J4");
1888                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#J5");
1889                                         Assert.IsNotNull (ex.InnerException, "#J6");
1890
1891                                         // Access is denied
1892                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#J7");
1893                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
1894                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#J8");
1895                                         Assert.IsNotNull (win32Error.Message, "#J9");
1896                                         Assert.AreEqual (5, win32Error.NativeErrorCode, "#J10");
1897                                         Assert.IsNull (win32Error.InnerException, "#J11");
1898                                 }
1899
1900                                 try {
1901                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_PAUSE);
1902                                         Assert.Fail ("#K1");
1903                                 } catch (InvalidOperationException ex) {
1904                                         // Cannot control XXX service on computer '.'
1905                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#K2");
1906                                         Assert.IsNotNull (ex.Message, "#K3");
1907                                         Assert.IsTrue (ex.Message.IndexOf (DISK_DRIVER_SERVICE.ServiceName) != -1, "#K4");
1908                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#K5");
1909                                         Assert.IsNotNull (ex.InnerException, "#K6");
1910
1911                                         // Access is denied
1912                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#K7");
1913                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
1914                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#K8");
1915                                         Assert.IsNotNull (win32Error.Message, "#K9");
1916                                         Assert.AreEqual (5, win32Error.NativeErrorCode, "#K10");
1917                                         Assert.IsNull (win32Error.InnerException, "#K11");
1918                                 }
1919
1920                                 try {
1921                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_POWEREVENT);
1922                                         Assert.Fail ("#L1");
1923                                 } catch (InvalidOperationException ex) {
1924                                         // Cannot control XXX service on computer '.'
1925                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#L2");
1926                                         Assert.IsNotNull (ex.Message, "#L3");
1927                                         Assert.IsTrue (ex.Message.IndexOf (DISK_DRIVER_SERVICE.ServiceName) != -1, "#L4");
1928                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#L5");
1929                                         Assert.IsNotNull (ex.InnerException, "#L6");
1930
1931                                         // Access is denied
1932                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#L7");
1933                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
1934                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#L8");
1935                                         Assert.IsNotNull (win32Error.Message, "#L9");
1936                                         Assert.AreEqual (87, win32Error.NativeErrorCode, "#L10");
1937                                         Assert.IsNull (win32Error.InnerException, "#L11");
1938                                 }
1939
1940                                 try {
1941                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_SESSIONCHANGE);
1942                                         Assert.Fail ("#M1");
1943                                 } catch (InvalidOperationException ex) {
1944                                         // Cannot control XXX service on computer '.'
1945                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#M2");
1946                                         Assert.IsNotNull (ex.Message, "#M3");
1947                                         Assert.IsTrue (ex.Message.IndexOf (DISK_DRIVER_SERVICE.ServiceName) != -1, "#M4");
1948                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#M5");
1949                                         Assert.IsNotNull (ex.InnerException, "#M6");
1950
1951                                         // The parameter is incorrect
1952                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#M7");
1953                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
1954                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#M8");
1955                                         Assert.IsNotNull (win32Error.Message, "#M9");
1956                                         Assert.AreEqual (87, win32Error.NativeErrorCode, "#M10");
1957                                         Assert.IsNull (win32Error.InnerException, "#M11");
1958                                 }
1959
1960                                 try {
1961                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_SHUTDOWN);
1962                                         Assert.Fail ("#N1");
1963                                 } catch (InvalidOperationException ex) {
1964                                         // Cannot control XXX service on computer '.'
1965                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#N2");
1966                                         Assert.IsNotNull (ex.Message, "#N3");
1967                                         Assert.IsTrue (ex.Message.IndexOf (DISK_DRIVER_SERVICE.ServiceName) != -1, "#N4");
1968                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#N5");
1969                                         Assert.IsNotNull (ex.InnerException, "#N6");
1970
1971                                         // The parameter is incorrect
1972                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#N7");
1973                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
1974                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#N8");
1975                                         Assert.IsNotNull (win32Error.Message, "#N9");
1976                                         Assert.AreEqual (87, win32Error.NativeErrorCode, "#N10");
1977                                         Assert.IsNull (win32Error.InnerException, "#N11");
1978                                 }
1979
1980                                 try {
1981                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_STOP);
1982                                         Assert.Fail ("#O1");
1983                                 } catch (InvalidOperationException ex) {
1984                                         // Cannot control XXX service on computer '.'
1985                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#O2");
1986                                         Assert.IsNotNull (ex.Message, "#O3");
1987                                         Assert.IsTrue (ex.Message.IndexOf (DISK_DRIVER_SERVICE.ServiceName) != -1, "#O4");
1988                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#O5");
1989                                         Assert.IsNotNull (ex.InnerException, "#O6");
1990
1991                                         // Access is denied
1992                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#O7");
1993                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
1994                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#O8");
1995                                         Assert.IsNotNull (win32Error.Message, "#O9");
1996                                         Assert.AreEqual (5, win32Error.NativeErrorCode, "#O10");
1997                                         Assert.IsNull (win32Error.InnerException, "#O11");
1998                                 }
1999                         } finally {
2000                                 EnsureServiceIsRunning (sc);
2001                         }
2002
2003                         Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#P");
2004                 }
2005
2006                 [Test]
2007                 public void ExecuteCommand_Machine_DoesNotExist ()
2008                 {
2009                         if (RunningOnUnix)
2010                                 Assert.Ignore ("Running on Unix.");
2011
2012                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName,
2013                                 "doesnotexist");
2014                         try {
2015                                 sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_PAUSE);
2016                                 Assert.Fail ("#1");
2017                         } catch (InvalidOperationException ex) {
2018                                 // Cannot open Service Control Manager on computer 'doesnotexist'.
2019                                 // This operation might require other priviliges
2020                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
2021                                 Assert.IsNotNull (ex.Message, "#3");
2022                                 Assert.IsTrue (ex.Message.IndexOf ("'doesnotexist'") != -1, "#4");
2023                                 Assert.IsNotNull (ex.InnerException, "#5");
2024
2025                                 // The RPC server is unavailable
2026                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#6");
2027                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
2028                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#7");
2029                                 Assert.IsNotNull (win32Error.Message, "#8");
2030                                 Assert.AreEqual (1722, win32Error.NativeErrorCode, "#9");
2031                                 Assert.IsNull (win32Error.InnerException, "#10");
2032                         }
2033                 }
2034
2035                 [Test]
2036                 public void ExecuteCommand_Parameter_Incorrect ()
2037                 {
2038                         if (RunningOnUnix)
2039                                 Assert.Ignore ("Running on Unix.");
2040
2041                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
2042                         Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#A");
2043
2044                         try {
2045                                 try {
2046                                         sc.ExecuteCommand (127);
2047                                         Assert.Fail ("#B1");
2048                                 } catch (InvalidOperationException ex) {
2049                                         // Cannot control XXX service on computer '.'
2050                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
2051                                         Assert.IsNotNull (ex.Message, "#B3");
2052                                         Assert.IsTrue (ex.Message.IndexOf (CONTROLLABLE_SERVICE.ServiceName) != -1, "#B4");
2053                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#B5");
2054                                         Assert.IsNotNull (ex.InnerException, "#B6");
2055
2056                                         // The parameter is incorrect
2057                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#B7");
2058                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
2059                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#B8");
2060                                         Assert.IsNotNull (win32Error.Message, "#B9");
2061                                         Assert.AreEqual (87, win32Error.NativeErrorCode, "#B10");
2062                                         Assert.IsNull (win32Error.InnerException, "#B11");
2063                                 }
2064
2065                                 sc.ExecuteCommand (128);
2066                                 sc.ExecuteCommand (255);
2067
2068                                 try {
2069                                         sc.ExecuteCommand (256);
2070                                         Assert.Fail ("#C1");
2071                                 } catch (InvalidOperationException ex) {
2072                                         // Cannot control XXX service on computer '.'
2073                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
2074                                         Assert.IsNotNull (ex.Message, "#C3");
2075                                         Assert.IsTrue (ex.Message.IndexOf (CONTROLLABLE_SERVICE.ServiceName) != -1, "#C4");
2076                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#C5");
2077                                         Assert.IsNotNull (ex.InnerException, "#C6");
2078
2079                                         // The parameter is incorrect
2080                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#C7");
2081                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
2082                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#C8");
2083                                         Assert.IsNotNull (win32Error.Message, "#C9");
2084                                         Assert.AreEqual (87, win32Error.NativeErrorCode, "#C10");
2085                                         Assert.IsNull (win32Error.InnerException, "#C11");
2086                                 }
2087                         } finally {
2088                                 EnsureServiceIsRunning (sc);
2089                         }
2090
2091                         Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#D");
2092                 }
2093
2094                 [Test]
2095                 public void ExecuteCommand_Service_ContinuePending ()
2096                 {
2097                         if (RunningOnUnix)
2098                                 Assert.Ignore ("Running on Unix.");
2099
2100                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
2101                         Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#A");
2102
2103                         sc.Pause ();
2104
2105                         try {
2106                                 sc.WaitForStatus (ServiceControllerStatus.Paused, new TimeSpan (0, 0, 5));
2107                                 Assert.AreEqual (ServiceControllerStatus.Paused, sc.Status, "#B");
2108
2109                                 sc.Continue ();
2110
2111                                 sc.ExecuteCommand (128);
2112
2113                                 try {
2114                                         sc.ExecuteCommand (127);
2115                                         Assert.Fail ("#C1");
2116                                 } catch (InvalidOperationException ex) {
2117                                         // Cannot control XXX service on computer '.'
2118                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
2119                                         Assert.IsNotNull (ex.Message, "#C3");
2120                                         Assert.IsTrue (ex.Message.IndexOf (CONTROLLABLE_SERVICE.ServiceName) != -1, "#C4");
2121                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#C5");
2122                                         Assert.IsNotNull (ex.InnerException, "#C6");
2123
2124                                         // The parameter is incorrect
2125                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#C7");
2126                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
2127                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#C8");
2128                                         Assert.IsNotNull (win32Error.Message, "#C9");
2129                                         Assert.AreEqual (87, win32Error.NativeErrorCode, "#C10");
2130                                         Assert.IsNull (win32Error.InnerException, "#C11");
2131                                 }
2132                         } finally {
2133                                 EnsureServiceIsRunning (sc);
2134                         }
2135
2136                         Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#E");
2137                 }
2138
2139                 [Test]
2140                 public void ExecuteCommand_Service_ControlCodes ()
2141                 {
2142                         if (RunningOnUnix)
2143                                 Assert.Ignore ("Running on Unix.");
2144
2145                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
2146                         Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#A");
2147
2148                         try {
2149                                 try {
2150                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_CONTINUE);
2151                                         Assert.Fail ("#B1");
2152                                 } catch (InvalidOperationException ex) {
2153                                         // Cannot control XXX service on computer '.'
2154                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
2155                                         Assert.IsNotNull (ex.Message, "#B3");
2156                                         Assert.IsTrue (ex.Message.IndexOf (CONTROLLABLE_SERVICE.ServiceName) != -1, "#B4");
2157                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#B5");
2158                                         Assert.IsNotNull (ex.InnerException, "#B6");
2159
2160                                         // Access is denied
2161                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#B7");
2162                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
2163                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#B8");
2164                                         Assert.IsNotNull (win32Error.Message, "#B9");
2165                                         Assert.AreEqual (5, win32Error.NativeErrorCode, "#B10");
2166                                         Assert.IsNull (win32Error.InnerException, "#B11");
2167                                 }
2168
2169                                 try {
2170                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_DEVICEEVENT);
2171                                         Assert.Fail ("#C1");
2172                                 } catch (InvalidOperationException ex) {
2173                                         // Cannot control XXX service on computer '.'
2174                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
2175                                         Assert.IsNotNull (ex.Message, "#C3");
2176                                         Assert.IsTrue (ex.Message.IndexOf (CONTROLLABLE_SERVICE.ServiceName) != -1, "#C4");
2177                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#C5");
2178                                         Assert.IsNotNull (ex.InnerException, "#C6");
2179
2180                                         // The parameter is incorrect
2181                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#C7");
2182                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
2183                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#C8");
2184                                         Assert.IsNotNull (win32Error.Message, "#C9");
2185                                         Assert.AreEqual (87, win32Error.NativeErrorCode, "#C10");
2186                                         Assert.IsNull (win32Error.InnerException, "#C11");
2187                                 }
2188
2189                                 try {
2190                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_HARDWAREPROFILECHANGE);
2191                                         Assert.Fail ("#D1");
2192                                 } catch (InvalidOperationException ex) {
2193                                         // Cannot control XXX service on computer '.'
2194                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
2195                                         Assert.IsNotNull (ex.Message, "#D3");
2196                                         Assert.IsTrue (ex.Message.IndexOf (CONTROLLABLE_SERVICE.ServiceName) != -1, "#D4");
2197                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#D5");
2198                                         Assert.IsNotNull (ex.InnerException, "#D6");
2199
2200                                         // The parameter is incorrect
2201                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#D7");
2202                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
2203                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#D8");
2204                                         Assert.IsNotNull (win32Error.Message, "#D9");
2205                                         Assert.AreEqual (87, win32Error.NativeErrorCode, "#D10");
2206                                         Assert.IsNull (win32Error.InnerException, "#D11");
2207                                 }
2208
2209                                 try {
2210                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_INTERROGATE);
2211                                         Assert.Fail ("#E1");
2212                                 } catch (InvalidOperationException ex) {
2213                                         // Cannot control XXX service on computer '.'
2214                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#E2");
2215                                         Assert.IsNotNull (ex.Message, "#E3");
2216                                         Assert.IsTrue (ex.Message.IndexOf (CONTROLLABLE_SERVICE.ServiceName) != -1, "#E4");
2217                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#E5");
2218                                         Assert.IsNotNull (ex.InnerException, "#E6");
2219
2220                                         // Access is denied
2221                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#E7");
2222                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
2223                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#E8");
2224                                         Assert.IsNotNull (win32Error.Message, "#E9");
2225                                         Assert.AreEqual (5, win32Error.NativeErrorCode, "#E10");
2226                                         Assert.IsNull (win32Error.InnerException, "#E11");
2227                                 }
2228
2229                                 try {
2230                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_NETBINDADD);
2231                                         Assert.Fail ("#F1");
2232                                 } catch (InvalidOperationException ex) {
2233                                         // Cannot control XXX service on computer '.'
2234                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#F2");
2235                                         Assert.IsNotNull (ex.Message, "#F3");
2236                                         Assert.IsTrue (ex.Message.IndexOf (CONTROLLABLE_SERVICE.ServiceName) != -1, "#F4");
2237                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#F5");
2238                                         Assert.IsNotNull (ex.InnerException, "#F6");
2239
2240                                         // Access is denied
2241                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#F7");
2242                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
2243                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#F8");
2244                                         Assert.IsNotNull (win32Error.Message, "#F9");
2245                                         Assert.AreEqual (5, win32Error.NativeErrorCode, "#F10");
2246                                         Assert.IsNull (win32Error.InnerException, "#F11");
2247                                 }
2248
2249                                 try {
2250                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_NETBINDDISABLE);
2251                                         Assert.Fail ("#G1");
2252                                 } catch (InvalidOperationException ex) {
2253                                         // Cannot control XXX service on computer '.'
2254                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#G2");
2255                                         Assert.IsNotNull (ex.Message, "#G3");
2256                                         Assert.IsTrue (ex.Message.IndexOf (CONTROLLABLE_SERVICE.ServiceName) != -1, "#G4");
2257                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#G5");
2258                                         Assert.IsNotNull (ex.InnerException, "#G6");
2259
2260                                         // Access is denied
2261                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#G7");
2262                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
2263                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#G8");
2264                                         Assert.IsNotNull (win32Error.Message, "#G9");
2265                                         Assert.AreEqual (5, win32Error.NativeErrorCode, "#G10");
2266                                         Assert.IsNull (win32Error.InnerException, "#G11");
2267                                 }
2268
2269                                 try {
2270                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_NETBINDENABLE);
2271                                         Assert.Fail ("#H1");
2272                                 } catch (InvalidOperationException ex) {
2273                                         // Cannot control XXX service on computer '.'
2274                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#H2");
2275                                         Assert.IsNotNull (ex.Message, "#H3");
2276                                         Assert.IsTrue (ex.Message.IndexOf (CONTROLLABLE_SERVICE.ServiceName) != -1, "#H4");
2277                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#H5");
2278                                         Assert.IsNotNull (ex.InnerException, "#H6");
2279
2280                                         // Access is denied
2281                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#H7");
2282                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
2283                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#H8");
2284                                         Assert.IsNotNull (win32Error.Message, "#H9");
2285                                         Assert.AreEqual (5, win32Error.NativeErrorCode, "#H10");
2286                                         Assert.IsNull (win32Error.InnerException, "#H11");
2287                                 }
2288
2289                                 try {
2290                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_NETBINDREMOVE);
2291                                         Assert.Fail ("#I1");
2292                                 } catch (InvalidOperationException ex) {
2293                                         // Cannot control XXX service on computer '.'
2294                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#I2");
2295                                         Assert.IsNotNull (ex.Message, "#I3");
2296                                         Assert.IsTrue (ex.Message.IndexOf (CONTROLLABLE_SERVICE.ServiceName) != -1, "#I4");
2297                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#I5");
2298                                         Assert.IsNotNull (ex.InnerException, "#I6");
2299
2300                                         // Access is denied
2301                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#I7");
2302                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
2303                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#I8");
2304                                         Assert.IsNotNull (win32Error.Message, "#I9");
2305                                         Assert.AreEqual (5, win32Error.NativeErrorCode, "#I10");
2306                                         Assert.IsNull (win32Error.InnerException, "#I11");
2307                                 }
2308
2309                                 try {
2310                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_PARAMCHANGE);
2311                                         Assert.Fail ("#J1");
2312                                 } catch (InvalidOperationException ex) {
2313                                         // Cannot control XXX service on computer '.'
2314                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#J2");
2315                                         Assert.IsNotNull (ex.Message, "#J3");
2316                                         Assert.IsTrue (ex.Message.IndexOf (CONTROLLABLE_SERVICE.ServiceName) != -1, "#J4");
2317                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#J5");
2318                                         Assert.IsNotNull (ex.InnerException, "#J6");
2319
2320                                         // Access is denied
2321                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#J7");
2322                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
2323                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#J8");
2324                                         Assert.IsNotNull (win32Error.Message, "#J9");
2325                                         Assert.AreEqual (5, win32Error.NativeErrorCode, "#J10");
2326                                         Assert.IsNull (win32Error.InnerException, "#J11");
2327                                 }
2328
2329                                 try {
2330                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_PAUSE);
2331                                         Assert.Fail ("#K1");
2332                                 } catch (InvalidOperationException ex) {
2333                                         // Cannot control XXX service on computer '.'
2334                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#K2");
2335                                         Assert.IsNotNull (ex.Message, "#K3");
2336                                         Assert.IsTrue (ex.Message.IndexOf (CONTROLLABLE_SERVICE.ServiceName) != -1, "#K4");
2337                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#K5");
2338                                         Assert.IsNotNull (ex.InnerException, "#K6");
2339
2340                                         // Access is denied
2341                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#K7");
2342                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
2343                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#K8");
2344                                         Assert.IsNotNull (win32Error.Message, "#K9");
2345                                         Assert.AreEqual (5, win32Error.NativeErrorCode, "#K10");
2346                                         Assert.IsNull (win32Error.InnerException, "#K11");
2347                                 }
2348
2349                                 try {
2350                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_POWEREVENT);
2351                                         Assert.Fail ("#L1");
2352                                 } catch (InvalidOperationException ex) {
2353                                         // Cannot control XXX service on computer '.'
2354                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#L2");
2355                                         Assert.IsNotNull (ex.Message, "#L3");
2356                                         Assert.IsTrue (ex.Message.IndexOf (CONTROLLABLE_SERVICE.ServiceName) != -1, "#L4");
2357                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#L5");
2358                                         Assert.IsNotNull (ex.InnerException, "#L6");
2359
2360                                         // Access is denied
2361                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#L7");
2362                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
2363                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#L8");
2364                                         Assert.IsNotNull (win32Error.Message, "#L9");
2365                                         Assert.AreEqual (87, win32Error.NativeErrorCode, "#L10");
2366                                         Assert.IsNull (win32Error.InnerException, "#L11");
2367                                 }
2368
2369                                 try {
2370                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_SESSIONCHANGE);
2371                                         Assert.Fail ("#M1");
2372                                 } catch (InvalidOperationException ex) {
2373                                         // Cannot control XXX service on computer '.'
2374                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#M2");
2375                                         Assert.IsNotNull (ex.Message, "#M3");
2376                                         Assert.IsTrue (ex.Message.IndexOf (CONTROLLABLE_SERVICE.ServiceName) != -1, "#M4");
2377                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#M5");
2378                                         Assert.IsNotNull (ex.InnerException, "#M6");
2379
2380                                         // The parameter is incorrect
2381                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#M7");
2382                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
2383                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#M8");
2384                                         Assert.IsNotNull (win32Error.Message, "#M9");
2385                                         Assert.AreEqual (87, win32Error.NativeErrorCode, "#M10");
2386                                         Assert.IsNull (win32Error.InnerException, "#M11");
2387                                 }
2388
2389                                 try {
2390                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_SHUTDOWN);
2391                                         Assert.Fail ("#N1");
2392                                 } catch (InvalidOperationException ex) {
2393                                         // Cannot control XXX service on computer '.'
2394                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#N2");
2395                                         Assert.IsNotNull (ex.Message, "#N3");
2396                                         Assert.IsTrue (ex.Message.IndexOf (CONTROLLABLE_SERVICE.ServiceName) != -1, "#N4");
2397                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#N5");
2398                                         Assert.IsNotNull (ex.InnerException, "#N6");
2399
2400                                         // The parameter is incorrect
2401                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#N7");
2402                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
2403                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#N8");
2404                                         Assert.IsNotNull (win32Error.Message, "#N9");
2405                                         Assert.AreEqual (87, win32Error.NativeErrorCode, "#N10");
2406                                         Assert.IsNull (win32Error.InnerException, "#N11");
2407                                 }
2408
2409                                 try {
2410                                         sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_STOP);
2411                                         Assert.Fail ("#O1");
2412                                 } catch (InvalidOperationException ex) {
2413                                         // Cannot control XXX service on computer '.'
2414                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#O2");
2415                                         Assert.IsNotNull (ex.Message, "#O3");
2416                                         Assert.IsTrue (ex.Message.IndexOf (CONTROLLABLE_SERVICE.ServiceName) != -1, "#O4");
2417                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#O5");
2418                                         Assert.IsNotNull (ex.InnerException, "#O6");
2419
2420                                         // Access is denied
2421                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#O7");
2422                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
2423                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#O8");
2424                                         Assert.IsNotNull (win32Error.Message, "#O9");
2425                                         Assert.AreEqual (5, win32Error.NativeErrorCode, "#O10");
2426                                         Assert.IsNull (win32Error.InnerException, "#O11");
2427                                 }
2428                         } finally {
2429                                 EnsureServiceIsRunning (sc);
2430                         }
2431
2432                         Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#P");
2433                 }
2434
2435                 [Test]
2436                 public void ExecuteCommand_Service_DoesNotExist ()
2437                 {
2438                         if (RunningOnUnix)
2439                                 Assert.Ignore ("Running on Unix.");
2440
2441                         ServiceController sc = new ServiceController ("doesnotexist", ".");
2442                         try {
2443                                 sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_INTERROGATE);
2444                                 Assert.Fail ("#1");
2445                         } catch (InvalidOperationException ex) {
2446                                 // Cannot open doesnotexist service on computer '.'
2447                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
2448                                 Assert.IsNotNull (ex.Message, "#3");
2449                                 Assert.IsTrue (ex.Message.IndexOf ("doesnotexist") != -1, "#4");
2450                                 Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#5");
2451                                 Assert.IsNotNull (ex.InnerException, "#6");
2452
2453                                 // The filename, directory name, or volume label is incorrect
2454                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#7");
2455                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
2456                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#8");
2457                                 Assert.IsNotNull (win32Error.Message, "#9");
2458                                 Assert.AreEqual (1060, win32Error.NativeErrorCode, "#10");
2459                                 Assert.IsNull (win32Error.InnerException, "#11");
2460                         }
2461                 }
2462
2463                 [Test]
2464                 public void ExecuteCommand_Service_Paused ()
2465                 {
2466                         if (RunningOnUnix)
2467                                 Assert.Ignore ("Running on Unix.");
2468
2469                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
2470                         Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#A");
2471
2472                         sc.Pause ();
2473
2474                         try {
2475                                 sc.WaitForStatus (ServiceControllerStatus.Paused, new TimeSpan (0, 0, 5));
2476                                 Assert.AreEqual (ServiceControllerStatus.Paused, sc.Status, "#B");
2477                                 sc.ExecuteCommand (154);
2478                                 sc.Refresh ();
2479                                 Assert.AreEqual (ServiceControllerStatus.Paused, sc.Status, "#C");
2480                                 //sc.ExecuteCommand (127);
2481                         } finally {
2482                                 EnsureServiceIsRunning (sc);
2483                         }
2484
2485                         Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#D");
2486                 }
2487
2488                 [Test]
2489                 public void ExecuteCommand_Service_PausePending ()
2490                 {
2491                         if (RunningOnUnix)
2492                                 Assert.Ignore ("Running on Unix.");
2493
2494                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
2495                         Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#A");
2496
2497                         sc.Pause ();
2498
2499                         try {
2500                                 sc.ExecuteCommand (128);
2501
2502                                 try {
2503                                         sc.ExecuteCommand (127);
2504                                         Assert.Fail ("#B1");
2505                                 } catch (InvalidOperationException ex) {
2506                                         // Cannot control XXX service on computer '.'
2507                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
2508                                         Assert.IsNotNull (ex.Message, "#B3");
2509                                         Assert.IsTrue (ex.Message.IndexOf (CONTROLLABLE_SERVICE.ServiceName) != -1, "#B4");
2510                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#B5");
2511                                         Assert.IsNotNull (ex.InnerException, "#B6");
2512
2513                                         // The parameter is incorrect
2514                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#B7");
2515                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
2516                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#B8");
2517                                         Assert.IsNotNull (win32Error.Message, "#B9");
2518                                         Assert.AreEqual (87, win32Error.NativeErrorCode, "#B10");
2519                                         Assert.IsNull (win32Error.InnerException, "#B11");
2520                                 }
2521                         } finally {
2522                                 EnsureServiceIsRunning (sc);
2523                         }
2524
2525                         Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#C");
2526                 }
2527
2528                 [Test]
2529                 public void ExecuteCommand_Service_StartPending ()
2530                 {
2531                         if (RunningOnUnix)
2532                                 Assert.Ignore ("Running on Unix.");
2533
2534                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
2535                         Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#A");
2536
2537                         sc.Stop ();
2538
2539                         try {
2540                                 sc.WaitForStatus (ServiceControllerStatus.Stopped, new TimeSpan (0, 0, 5));
2541                                 Assert.AreEqual (ServiceControllerStatus.Stopped, sc.Status, "#B");
2542
2543                                 sc.Start ();
2544
2545                                 try {
2546                                         sc.ExecuteCommand (127);
2547                                         Assert.Fail ("#C1");
2548                                 } catch (InvalidOperationException ex) {
2549                                         // Cannot control XXX service on computer '.'
2550                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
2551                                         Assert.IsNotNull (ex.Message, "#C3");
2552                                         Assert.IsTrue (ex.Message.IndexOf (CONTROLLABLE_SERVICE.ServiceName) != -1, "#C4");
2553                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#C5");
2554                                         Assert.IsNotNull (ex.InnerException, "#C6");
2555
2556                                         // The parameter is incorrect
2557                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#C7");
2558                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
2559                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#C8");
2560                                         Assert.IsNotNull (win32Error.Message, "#C9");
2561                                         Assert.AreEqual (87, win32Error.NativeErrorCode, "#C10");
2562                                         Assert.IsNull (win32Error.InnerException, "#C11");
2563                                 }
2564
2565                                 try {
2566                                         sc.ExecuteCommand (128);
2567                                         Assert.Fail ("#D1");
2568                                 } catch (InvalidOperationException ex) {
2569                                         // Cannot control XXX service on computer '.'
2570                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
2571                                         Assert.IsNotNull (ex.Message, "#D3");
2572                                         Assert.IsTrue (ex.Message.IndexOf (CONTROLLABLE_SERVICE.ServiceName) != -1, "#D4");
2573                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#D5");
2574                                         Assert.IsNotNull (ex.InnerException, "#D6");
2575
2576                                         // The service cannot accept control messages at this time
2577                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#D7");
2578                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
2579                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#D8");
2580                                         Assert.IsNotNull (win32Error.Message, "#D9");
2581                                         Assert.AreEqual (1061, win32Error.NativeErrorCode, "#D10");
2582                                         Assert.IsNull (win32Error.InnerException, "#D11");
2583                                 }
2584                         } finally {
2585                                 EnsureServiceIsRunning (sc);
2586                         }
2587
2588                         Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#D");
2589                 }
2590
2591                 [Test]
2592                 public void ExecuteCommand_Service_Stopped ()
2593                 {
2594                         if (RunningOnUnix)
2595                                 Assert.Ignore ("Running on Unix.");
2596
2597                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
2598                         Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#A");
2599
2600                         sc.Stop ();
2601
2602                         try {
2603                                 sc.WaitForStatus (ServiceControllerStatus.Stopped, new TimeSpan (0, 0, 5));
2604                                 Assert.AreEqual (ServiceControllerStatus.Stopped, sc.Status, "#B");
2605
2606                                 try {
2607                                         sc.ExecuteCommand (127);
2608                                         Assert.Fail ("#C1");
2609                                 } catch (InvalidOperationException ex) {
2610                                         // Cannot control XXX service on computer '.'
2611                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
2612                                         Assert.IsNotNull (ex.Message, "#C3");
2613                                         Assert.IsTrue (ex.Message.IndexOf (CONTROLLABLE_SERVICE.ServiceName) != -1, "#C4");
2614                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#C5");
2615                                         Assert.IsNotNull (ex.InnerException, "#C6");
2616
2617                                         // The parameter is incorrect
2618                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#C7");
2619                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
2620                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#C8");
2621                                         Assert.IsNotNull (win32Error.Message, "#C9");
2622                                         Assert.AreEqual (87, win32Error.NativeErrorCode, "#C10");
2623                                         Assert.IsNull (win32Error.InnerException, "#C11");
2624                                 }
2625
2626                                 try {
2627                                         sc.ExecuteCommand (128);
2628                                         Assert.Fail ("#D1");
2629                                 } catch (InvalidOperationException ex) {
2630                                         // Cannot control XXX service on computer '.'
2631                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
2632                                         Assert.IsNotNull (ex.Message, "#D3");
2633                                         Assert.IsTrue (ex.Message.IndexOf (CONTROLLABLE_SERVICE.ServiceName) != -1, "#D4");
2634                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#D5");
2635                                         Assert.IsNotNull (ex.InnerException, "#D6");
2636
2637                                         // The service has not been started
2638                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#D7");
2639                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
2640                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#D8");
2641                                         Assert.IsNotNull (win32Error.Message, "#D9");
2642                                         Assert.AreEqual (1062, win32Error.NativeErrorCode, "#D10");
2643                                         Assert.IsNull (win32Error.InnerException, "#D11");
2644                                 }
2645                         } finally {
2646                                 EnsureServiceIsRunning (sc);
2647                         }
2648
2649                         Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#E");
2650                 }
2651
2652                 [Test]
2653                 public void ExecuteCommand_Service_StopPending ()
2654                 {
2655                         if (RunningOnUnix)
2656                                 Assert.Ignore ("Running on Unix.");
2657
2658                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
2659                         Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#A");
2660
2661                         sc.Stop ();
2662
2663                         try {
2664                                 try {
2665                                         sc.ExecuteCommand (127);
2666                                         Assert.Fail ("#B1");
2667                                 } catch (InvalidOperationException ex) {
2668                                         // Cannot control XXX service on computer '.'
2669                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
2670                                         Assert.IsNotNull (ex.Message, "#B3");
2671                                         Assert.IsTrue (ex.Message.IndexOf (CONTROLLABLE_SERVICE.ServiceName) != -1, "#B4");
2672                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#B5");
2673                                         Assert.IsNotNull (ex.InnerException, "#B6");
2674
2675                                         // The parameter is incorrect
2676                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#B7");
2677                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
2678                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#B8");
2679                                         Assert.IsNotNull (win32Error.Message, "#B9");
2680                                         Assert.AreEqual (87, win32Error.NativeErrorCode, "#B10");
2681                                         Assert.IsNull (win32Error.InnerException, "#B11");
2682                                 }
2683
2684                                 try {
2685                                         sc.ExecuteCommand (128);
2686                                         Assert.Fail ("#C1");
2687                                 } catch (InvalidOperationException ex) {
2688                                         // Cannot control XXX service on computer '.'
2689                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
2690                                         Assert.IsNotNull (ex.Message, "#C3");
2691                                         Assert.IsTrue (ex.Message.IndexOf (CONTROLLABLE_SERVICE.ServiceName) != -1, "#C4");
2692                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#C5");
2693                                         Assert.IsNotNull (ex.InnerException, "#C6");
2694
2695                                         // The service cannot accept control messages at this time
2696                                         Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#C7");
2697                                         Win32Exception win32Error = (Win32Exception) ex.InnerException;
2698                                         //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#C8");
2699                                         Assert.IsNotNull (win32Error.Message, "#C9");
2700                                         Assert.AreEqual (1061, win32Error.NativeErrorCode, "#C10");
2701                                         Assert.IsNull (win32Error.InnerException, "#C11");
2702                                 }
2703                         } finally {
2704                                 EnsureServiceIsRunning (sc);
2705                         }
2706
2707                         Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#D");
2708                 }
2709
2710                 [Test]
2711                 public void ExecuteCommand_ServiceName_Empty ()
2712                 {
2713                         ServiceController sc = new ServiceController ();
2714                         try {
2715                                 sc.ExecuteCommand ((int) SERVICE_CONTROL_TYPE.SERVICE_CONTROL_INTERROGATE);
2716                                 Assert.Fail ("#1");
2717                         } catch (ArgumentException ex) {
2718                                 // Service name  contains invalid characters, is empty or is
2719                                 // too long (max length = 80)
2720                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2721                                 Assert.IsNotNull (ex.Message, "#3");
2722                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#4");
2723                                 Assert.IsTrue (ex.Message.IndexOf ("80") != -1, "#5");
2724                                 Assert.IsNull (ex.ParamName, "#6");
2725                                 Assert.IsNull (ex.InnerException, "#7");
2726                         }
2727                 }
2728
2729                 [Test]
2730                 public void GetDevices ()
2731                 {
2732                         if (RunningOnUnix)
2733                                 Assert.Ignore ("Running on Unix.");
2734
2735                         ServiceController [] devices = null;
2736
2737                         devices = ServiceController.GetDevices ();
2738                         Assert.IsNotNull (devices, "#A1");
2739
2740                         bool foundKernelService = false;
2741                         bool foundShareProcessService = false;
2742
2743                         foreach (ServiceController sc in devices) {
2744                                 if (sc.ServiceName.ToLower () == KERNEL_SERVICE.ServiceName.ToLower ()) {
2745                                         Assert.AreEqual (KERNEL_SERVICE.DisplayName, sc.DisplayName, "#A2");
2746                                         Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#A3");
2747                                         foundKernelService = true;
2748                                 } else if (sc.ServiceName.ToLower () == SHARE_PROCESS_SERVICE.ServiceName.ToLower ()) {
2749                                         foundShareProcessService = true;
2750                                 }
2751                         }
2752
2753                         Assert.IsTrue (foundKernelService, "#A4");
2754                         Assert.IsFalse (foundShareProcessService, "#A5");
2755
2756                         devices = ServiceController.GetDevices (Environment.MachineName);
2757                         Assert.IsNotNull (devices, "#B1");
2758
2759                         foundKernelService = false;
2760                         foundShareProcessService = false;
2761
2762                         foreach (ServiceController sc in devices) {
2763                                 if (sc.ServiceName.ToLower () == KERNEL_SERVICE.ServiceName.ToLower ()) {
2764                                         Assert.AreEqual (KERNEL_SERVICE.DisplayName, sc.DisplayName, "#B2");
2765                                         Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#B3");
2766                                         foundKernelService = true;
2767                                 } else if (sc.ServiceName.ToLower () == SHARE_PROCESS_SERVICE.ServiceName.ToLower ()) {
2768                                         foundShareProcessService = true;
2769                                 }
2770                         }
2771
2772                         Assert.IsTrue (foundKernelService, "#B4");
2773                         Assert.IsFalse (foundShareProcessService, "#B5");
2774                 }
2775
2776                 [Test]
2777                 public void GetDevices_Machine_DoesNotExist ()
2778                 {
2779                         if (RunningOnUnix)
2780                                 Assert.Ignore ("Running on Unix.");
2781
2782                         try {
2783                                 ServiceController [] devices = ServiceController.GetDevices ("doesnotexist");
2784                                 Assert.Fail ("#1: " + devices.Length);
2785                         } catch (InvalidOperationException ex) {
2786                                 // Cannot open Service Control Manager on computer 'doesnotexist'.
2787                                 // This operation might require other priviliges
2788                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
2789                                 Assert.IsNotNull (ex.Message, "#3");
2790                                 Assert.IsTrue (ex.Message.IndexOf ("'doesnotexist'") != -1, "#4");
2791                                 Assert.IsNotNull (ex.InnerException, "#5");
2792
2793                                 // The RPC server is unavailable
2794                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#6");
2795                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
2796                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#7");
2797                                 Assert.IsNotNull (win32Error.Message, "#8");
2798                                 Assert.AreEqual (1722, win32Error.NativeErrorCode, "#9");
2799                                 Assert.IsNull (win32Error.InnerException, "#10");
2800                         }
2801                 }
2802
2803                 [Test]
2804                 public void GetDevices_MachineName_Empty ()
2805                 {
2806                         try {
2807                                 ServiceController.GetDevices (string.Empty);
2808                                 Assert.Fail ("#1");
2809                         } catch (ArgumentException ex) {
2810                                 // MachineName value  is invalid
2811                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2812                                 Assert.IsNotNull (ex.Message, "#3");
2813                                 Assert.IsTrue (ex.Message.IndexOf ("MachineName") != -1, "#4");
2814                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#5");
2815                                 Assert.IsNull (ex.ParamName, "#6");
2816                                 Assert.IsNull (ex.InnerException, "#7");
2817                         }
2818                 }
2819
2820                 [Test]
2821                 public void GetDevices_MachineName_Null ()
2822                 {
2823                         try {
2824                                 ServiceController.GetDevices (null);
2825                                 Assert.Fail ("#1");
2826                         } catch (ArgumentException ex) {
2827                                 // MachineName value  is invalid
2828                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2829                                 Assert.IsNotNull (ex.Message, "#3");
2830                                 Assert.IsTrue (ex.Message.IndexOf ("MachineName") != -1, "#4");
2831                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#5");
2832                                 Assert.IsNull (ex.ParamName, "#6");
2833                                 Assert.IsNull (ex.InnerException, "#7");
2834                         }
2835                 }
2836
2837                 [Test]
2838                 public void GetServices ()
2839                 {
2840                         if (RunningOnUnix)
2841                                 Assert.Ignore ("Running on Unix.");
2842
2843                         ServiceController [] services = null;
2844
2845                         services = ServiceController.GetServices ();
2846                         Assert.IsNotNull (services, "#A1");
2847
2848                         bool foundKernelService = false;
2849                         bool foundShareProcessService = false;
2850
2851                         foreach (ServiceController sc in services) {
2852                                 if (sc.ServiceName.ToLower () == KERNEL_SERVICE.ServiceName.ToLower ()) {
2853                                         foundKernelService = true;
2854                                 } else if (sc.ServiceName.ToLower () == SHARE_PROCESS_SERVICE.ServiceName.ToLower ()) {
2855                                         foundShareProcessService = true;
2856                                 }
2857                         }
2858
2859                         Assert.IsFalse (foundKernelService, "#A4");
2860                         Assert.IsTrue (foundShareProcessService, "#A5");
2861
2862                         services = ServiceController.GetServices (Environment.MachineName);
2863                         Assert.IsNotNull (services, "#B1");
2864
2865                         foundKernelService = false;
2866                         foundShareProcessService = false;
2867
2868                         foreach (ServiceController sc in services) {
2869                                 if (sc.ServiceName.ToLower () == KERNEL_SERVICE.ServiceName.ToLower ()) {
2870                                         foundKernelService = true;
2871                                 } else if (sc.ServiceName.ToLower () == SHARE_PROCESS_SERVICE.ServiceName.ToLower ()) {
2872                                         Assert.AreEqual (SHARE_PROCESS_SERVICE.DisplayName, sc.DisplayName, "#B2");
2873                                         Assert.AreEqual (ServiceControllerStatus.Running, sc.Status, "#B3");
2874                                         foundShareProcessService = true;
2875                                 }
2876                         }
2877
2878                         Assert.IsFalse (foundKernelService, "#B4");
2879                         Assert.IsTrue (foundShareProcessService, "#B5");
2880                 }
2881
2882                 [Test]
2883                 public void GetServices_Machine_DoesNotExist ()
2884                 {
2885                         if (RunningOnUnix)
2886                                 Assert.Ignore ("Running on Unix.");
2887
2888                         try {
2889                                 ServiceController [] services = ServiceController.GetServices ("doesnotexist");
2890                                 Assert.Fail ("#1: " + services.Length);
2891                         } catch (InvalidOperationException ex) {
2892                                 // Cannot open Service Control Manager on computer 'doesnotexist'.
2893                                 // This operation might require other priviliges
2894                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
2895                                 Assert.IsNotNull (ex.Message, "#3");
2896                                 Assert.IsTrue (ex.Message.IndexOf ("'doesnotexist'") != -1, "#4");
2897                                 Assert.IsNotNull (ex.InnerException, "#5");
2898
2899                                 // The RPC server is unavailable
2900                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#6");
2901                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
2902                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#7");
2903                                 Assert.IsNotNull (win32Error.Message, "#8");
2904                                 Assert.AreEqual (1722, win32Error.NativeErrorCode, "#9");
2905                                 Assert.IsNull (win32Error.InnerException, "#10");
2906                         }
2907                 }
2908
2909                 [Test]
2910                 public void GetServices_MachineName_Empty ()
2911                 {
2912                         try {
2913                                 ServiceController.GetServices (string.Empty);
2914                                 Assert.Fail ("#1");
2915                         } catch (ArgumentException ex) {
2916                                 // MachineName value  is invalid
2917                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2918                                 Assert.IsNotNull (ex.Message, "#3");
2919                                 Assert.IsTrue (ex.Message.IndexOf ("MachineName") != -1, "#4");
2920                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#5");
2921                                 Assert.IsNull (ex.ParamName, "#6");
2922                                 Assert.IsNull (ex.InnerException, "#7");
2923                         }
2924                 }
2925
2926                 [Test]
2927                 public void GetServices_MachineName_Null ()
2928                 {
2929                         try {
2930                                 ServiceController.GetServices (null);
2931                                 Assert.Fail ("#1");
2932                         } catch (ArgumentException ex) {
2933                                 // MachineName value  is invalid
2934                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2935                                 Assert.IsNotNull (ex.Message, "#3");
2936                                 Assert.IsTrue (ex.Message.IndexOf ("MachineName") != -1, "#4");
2937                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#5");
2938                                 Assert.IsNull (ex.ParamName, "#6");
2939                                 Assert.IsNull (ex.InnerException, "#7");
2940                         }
2941                 }
2942
2943                 [Test]
2944                 public void MachineName ()
2945                 {
2946                         if (RunningOnUnix)
2947                                 Assert.Ignore ("Running on Unix.");
2948
2949                         ServiceController sc = new ServiceController ();
2950                         sc.ServiceName = WORKSTATION_SERVICE.ServiceName;
2951                         Assert.AreEqual (WORKSTATION_SERVICE.DisplayName, sc.DisplayName, "#A1");
2952                         Assert.AreEqual (".", sc.MachineName, "#A2");
2953                         Assert.AreEqual (WORKSTATION_SERVICE.ServiceName, sc.ServiceName, "#A3");
2954
2955                         sc.MachineName = Environment.MachineName;
2956                         Assert.AreEqual (WORKSTATION_SERVICE.DisplayName, sc.DisplayName, "#B1");
2957                         Assert.AreEqual (Environment.MachineName, sc.MachineName, "#B2");
2958                         Assert.AreEqual (WORKSTATION_SERVICE.ServiceName, sc.ServiceName, "#B3");
2959
2960                         sc.MachineName = "doesnotexist";
2961                         Assert.AreEqual (WORKSTATION_SERVICE.DisplayName, sc.DisplayName, "#C1");
2962                         Assert.AreEqual ("doesnotexist", sc.MachineName, "#C2");
2963                         Assert.AreEqual (WORKSTATION_SERVICE.ServiceName, sc.ServiceName, "#C3");
2964
2965                         sc.MachineName = "DoesNotExist";
2966                         Assert.AreEqual (WORKSTATION_SERVICE.DisplayName, sc.DisplayName, "#D1");
2967                         Assert.AreEqual ("DoesNotExist", sc.MachineName, "#D2");
2968                         Assert.AreEqual (WORKSTATION_SERVICE.ServiceName, sc.ServiceName, "#D3");
2969                 }
2970
2971                 [Test]
2972                 public void MachineName_Empty ()
2973                 {
2974                         if (RunningOnUnix)
2975                                 Assert.Ignore ("Running on Unix.");
2976
2977                         ServiceController sc = new ServiceController ();
2978                         sc.MachineName = Environment.MachineName;
2979                         try {
2980                                 sc.MachineName = string.Empty;
2981                                 Assert.Fail ("#1");
2982                         } catch (ArgumentException ex) {
2983                                 // MachineName value  is invalid
2984                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2985                                 Assert.IsNotNull (ex.Message, "#3");
2986                                 Assert.IsTrue (ex.Message.IndexOf ("MachineName") != -1, "#4");
2987                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#5");
2988                                 Assert.IsNull (ex.ParamName, "#6");
2989                                 Assert.IsNull (ex.InnerException, "#7");
2990                         }
2991                         Assert.AreEqual (Environment.MachineName, sc.MachineName, "#8");
2992                 }
2993
2994                 [Test]
2995                 public void MachineName_Null ()
2996                 {
2997                         if (RunningOnUnix)
2998                                 Assert.Ignore ("Running on Unix.");
2999
3000                         ServiceController sc = new ServiceController ();
3001                         sc.MachineName = Environment.MachineName;
3002                         try {
3003                                 sc.MachineName = null;
3004                                 Assert.Fail ("#1");
3005                         } catch (ArgumentException ex) {
3006                                 // MachineName value  is invalid
3007                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
3008                                 Assert.IsNotNull (ex.Message, "#3");
3009                                 Assert.IsTrue (ex.Message.IndexOf ("MachineName") != -1, "#4");
3010                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#5");
3011                                 Assert.IsNull (ex.ParamName, "#6");
3012                                 Assert.IsNull (ex.InnerException, "#7");
3013                         }
3014                         Assert.AreEqual (Environment.MachineName, sc.MachineName, "#8");
3015                 }
3016
3017                 [Test]
3018                 public void Pause ()
3019                 {
3020                         if (RunningOnUnix)
3021                                 Assert.Ignore ("Running on Unix.");
3022
3023                         ServiceController sc1 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
3024                         ServiceController sc2 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
3025
3026                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#A1");
3027                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#A2");
3028
3029                         sc1.Pause ();
3030
3031                         try {
3032                                 Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#B1");
3033                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#B2");
3034
3035                                 sc1.WaitForStatus (ServiceControllerStatus.Paused, new TimeSpan (0, 0, 5));
3036
3037                                 Assert.AreEqual (ServiceControllerStatus.Paused, sc1.Status, "#C1");
3038                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#C2");
3039                         } finally {
3040                                 EnsureServiceIsRunning (sc1);
3041                                 sc2.Refresh ();
3042                         }
3043
3044                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#D1");
3045                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#D2");
3046                 }
3047
3048                 [Test]
3049                 public void Pause_Machine_DoesNotExist ()
3050                 {
3051                         if (RunningOnUnix)
3052                                 Assert.Ignore ("Running on Unix.");
3053
3054                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName,
3055                                 "doesnotexist");
3056                         try {
3057                                 sc.Pause ();
3058                                 Assert.Fail ("#1");
3059                         } catch (InvalidOperationException ex) {
3060                                 // Cannot open Service Control Manager on computer 'doesnotexist'.
3061                                 // This operation might require other priviliges
3062                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
3063                                 Assert.IsNotNull (ex.Message, "#3");
3064                                 Assert.IsTrue (ex.Message.IndexOf ("'doesnotexist'") != -1, "#4");
3065                                 Assert.IsNotNull (ex.InnerException, "#5");
3066
3067                                 // The RPC server is unavailable
3068                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#6");
3069                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
3070                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#7");
3071                                 Assert.IsNotNull (win32Error.Message, "#8");
3072                                 Assert.AreEqual (1722, win32Error.NativeErrorCode, "#9");
3073                                 Assert.IsNull (win32Error.InnerException, "#10");
3074                         }
3075                 }
3076
3077                 [Test]
3078                 public void Pause_Service_Disabled ()
3079                 {
3080                         if (RunningOnUnix)
3081                                 Assert.Ignore ("Running on Unix.");
3082
3083                         ServiceController sc1 = new ServiceController (DISABLED_SERVICE.ServiceName, ".");
3084                         ServiceController sc2 = new ServiceController (DISABLED_SERVICE.ServiceName, ".");
3085
3086                         Assert.AreEqual (ServiceControllerStatus.Stopped, sc1.Status, "#A1");
3087                         Assert.AreEqual (ServiceControllerStatus.Stopped, sc2.Status, "#A2");
3088
3089                         try {
3090                                 sc1.Pause ();
3091                                 Assert.Fail ("#B1");
3092                         } catch (InvalidOperationException ex) {
3093                                 // Cannot pause NetDDE service on computer '.'
3094                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
3095                                 Assert.IsNotNull (ex.Message, "#B3");
3096                                 Assert.IsTrue (ex.Message.IndexOf (DISABLED_SERVICE.ServiceName) != -1, "#B4");
3097                                 Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#B5");
3098                                 Assert.IsNotNull (ex.InnerException, "#B6");
3099
3100                                 // The service has not been started
3101                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#B7");
3102                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
3103                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#B8");
3104                                 Assert.IsNotNull (win32Error.Message, "#B9");
3105                                 Assert.AreEqual (1062, win32Error.NativeErrorCode, "#B10");
3106                                 Assert.IsNull (win32Error.InnerException, "#B11");
3107                         }
3108
3109                         Assert.AreEqual (ServiceControllerStatus.Stopped, sc1.Status, "#C1");
3110                         Assert.AreEqual (ServiceControllerStatus.Stopped, sc2.Status, "#C2");
3111                 }
3112
3113                 [Test]
3114                 public void Pause_Service_DoesNotExist ()
3115                 {
3116                         if (RunningOnUnix)
3117                                 Assert.Ignore ("Running on Unix.");
3118
3119                         ServiceController sc = new ServiceController ("doesnotexist", ".");
3120                         try {
3121                                 sc.Pause ();
3122                                 Assert.Fail ("#1");
3123                         } catch (InvalidOperationException ex) {
3124                                 // Cannot open doesnotexist service on computer '.'
3125                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
3126                                 Assert.IsNotNull (ex.Message, "#3");
3127                                 Assert.IsTrue (ex.Message.IndexOf ("doesnotexist") != -1, "#4");
3128                                 Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#5");
3129                                 Assert.IsNotNull (ex.InnerException, "#6");
3130
3131                                 // The filename, directory name, or volume label is incorrect
3132                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#7");
3133                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
3134                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#8");
3135                                 Assert.IsNotNull (win32Error.Message, "#9");
3136                                 Assert.AreEqual (1060, win32Error.NativeErrorCode, "#10");
3137                                 Assert.IsNull (win32Error.InnerException, "#11");
3138                         }
3139                 }
3140
3141                 [Test]
3142                 public void Pause_Service_OperationNotValid ()
3143                 {
3144                         if (RunningOnUnix)
3145                                 Assert.Ignore ("Running on Unix.");
3146
3147                         ServiceController sc1 = new ServiceController (UNCONTROLLABLE_SERVICE.ServiceName, ".");
3148                         ServiceController sc2 = new ServiceController (UNCONTROLLABLE_SERVICE.ServiceName, ".");
3149
3150                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#A1");
3151                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#A2");
3152
3153                         try {
3154                                 sc1.Pause ();
3155                                 Assert.Fail ("#B1");
3156                         } catch (InvalidOperationException ex) {
3157                                 // Cannot pause XXX service on computer '.'
3158                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
3159                                 Assert.IsNotNull (ex.Message, "#B3");
3160                                 Assert.IsTrue (ex.Message.IndexOf (UNCONTROLLABLE_SERVICE.ServiceName) != -1, "#B4");
3161                                 Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#B5");
3162                                 Assert.IsNotNull (ex.InnerException, "#B6");
3163
3164                                 // The requested control is not valid for this service
3165                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#B7");
3166                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
3167                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#B8");
3168                                 Assert.IsNotNull (win32Error.Message, "#B9");
3169                                 Assert.AreEqual (1052, win32Error.NativeErrorCode, "#B10");
3170                                 Assert.IsNull (win32Error.InnerException, "#B11");
3171                         }
3172
3173                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#C1");
3174                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#C2");
3175                 }
3176
3177                 [Test]
3178                 public void Pause_Service_Paused ()
3179                 {
3180                         if (RunningOnUnix)
3181                                 Assert.Ignore ("Running on Unix.");
3182
3183                         ServiceController sc1 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
3184                         ServiceController sc2 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
3185
3186                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#A1");
3187                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#A2");
3188
3189                         sc1.Pause ();
3190
3191                         try {
3192                                 Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#B1");
3193                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#B2");
3194
3195                                 sc1.WaitForStatus (ServiceControllerStatus.Paused, new TimeSpan (0, 0, 5));
3196
3197                                 Assert.AreEqual (ServiceControllerStatus.Paused, sc1.Status, "#C1");
3198                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#C2");
3199
3200                                 sc1.Pause ();
3201
3202                                 Assert.AreEqual (ServiceControllerStatus.Paused, sc1.Status, "#D1");
3203                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#D2");
3204                         } finally {
3205                                 EnsureServiceIsRunning (sc1);
3206                                 sc2.Refresh ();
3207                         }
3208
3209                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#E1");
3210                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#E2");
3211                 }
3212
3213                 [Test]
3214                 public void Pause_Service_Stopped ()
3215                 {
3216                         if (RunningOnUnix)
3217                                 Assert.Ignore ("Running on Unix.");
3218
3219                         ServiceController sc1 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
3220                         ServiceController sc2 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
3221
3222                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#A1");
3223                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#A2");
3224
3225                         sc1.Stop ();
3226
3227                         try {
3228                                 Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#B1");
3229                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#B2");
3230
3231                                 sc1.WaitForStatus (ServiceControllerStatus.Stopped, new TimeSpan (0, 0, 5));
3232
3233                                 Assert.AreEqual (ServiceControllerStatus.Stopped, sc1.Status, "#C1");
3234                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#C2");
3235
3236                                 sc1.Pause ();
3237                                 Assert.Fail ("#D1");
3238                         } catch (InvalidOperationException ex) {
3239                                 // Cannot pause XXX service on computer '.'
3240                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
3241                                 Assert.IsNotNull (ex.Message, "#D3");
3242                                 Assert.IsTrue (ex.Message.IndexOf (CONTROLLABLE_SERVICE.ServiceName) != -1, "#D4");
3243                                 Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#D5");
3244                                 Assert.IsNotNull (ex.InnerException, "#D6");
3245
3246                                 // The service has not been started
3247                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#D7");
3248                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
3249                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#D8");
3250                                 Assert.IsNotNull (win32Error.Message, "#D9");
3251                                 Assert.AreEqual (1062, win32Error.NativeErrorCode, "#D10");
3252                                 Assert.IsNull (win32Error.InnerException, "#D11");
3253                         } finally {
3254                                 EnsureServiceIsRunning (sc1);
3255                                 sc2.Refresh ();
3256                         }
3257
3258                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#E1");
3259                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#E2");
3260                 }
3261
3262                 [Test]
3263                 public void Pause_ServiceName_Empty ()
3264                 {
3265                         ServiceController sc = new ServiceController ();
3266                         try {
3267                                 sc.Pause ();
3268                                 Assert.Fail ("#1");
3269                         } catch (ArgumentException ex) {
3270                                 // Service name  contains invalid characters, is empty or is
3271                                 // too long (max length = 80)
3272                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
3273                                 Assert.IsNotNull (ex.Message, "#3");
3274                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#4");
3275                                 Assert.IsTrue (ex.Message.IndexOf ("80") != -1, "#5");
3276                                 Assert.IsNull (ex.ParamName, "#6");
3277                                 Assert.IsNull (ex.InnerException, "#7");
3278                         }
3279                 }
3280
3281                 [Test]
3282                 public void Refresh ()
3283                 {
3284                         if (RunningOnUnix)
3285                                 Assert.Ignore ("Running on Unix.");
3286
3287                         ServiceController sc = null;
3288                         ServiceController [] dependentServices = null;
3289                         ServiceController [] servicesDependedOn = null;
3290
3291                         sc = new ServiceController (SERVICE_1_WITH_DEPENDENTS_AND_DEPENDED_ON.ServiceName, ".");
3292                         dependentServices = sc.DependentServices;
3293                         Assert.IsNotNull (dependentServices, "#A1");
3294                         Assert.AreEqual (SERVICE_1_WITH_DEPENDENTS_AND_DEPENDED_ON.Dependents, ServiceNames (dependentServices), "#A2");
3295                         servicesDependedOn = sc.ServicesDependedOn;
3296                         Assert.AreEqual (SERVICE_1_WITH_DEPENDENTS_AND_DEPENDED_ON.DependedOn, ServiceNames (servicesDependedOn), "#A3");
3297
3298                         sc.ServiceName = SERVICE_2_WITH_DEPENDENTS_AND_DEPENDED_ON.ServiceName;
3299                         sc.Refresh ();
3300
3301                         dependentServices = sc.DependentServices;
3302                         Assert.IsNotNull (dependentServices, "#B1");
3303                         Assert.AreEqual (SERVICE_2_WITH_DEPENDENTS_AND_DEPENDED_ON.Dependents, ServiceNames (dependentServices), "#B2");
3304                         servicesDependedOn = sc.ServicesDependedOn;
3305                         Assert.IsNotNull (servicesDependedOn, "#B3");
3306                         Assert.AreEqual (SERVICE_2_WITH_DEPENDENTS_AND_DEPENDED_ON.DependedOn, ServiceNames (servicesDependedOn), "#A4");
3307
3308                         sc.DisplayName = SERVICE_3_WITH_DEPENDENTS_AND_DEPENDED_ON.ServiceName;
3309                         sc.Refresh ();
3310
3311                         dependentServices = sc.DependentServices;
3312                         Assert.IsNotNull (dependentServices, "#C1");
3313                         Assert.AreEqual (SERVICE_3_WITH_DEPENDENTS_AND_DEPENDED_ON.Dependents, ServiceNames (dependentServices), "#C2");
3314                         servicesDependedOn = sc.ServicesDependedOn;
3315                         Assert.IsNotNull (servicesDependedOn, "#C3");
3316                         Assert.AreEqual (SERVICE_3_WITH_DEPENDENTS_AND_DEPENDED_ON.DependedOn, ServiceNames (servicesDependedOn), "#C4");
3317                 }
3318
3319                 [Test]
3320                 public void Refresh_Machine_DoesNotExist ()
3321                 {
3322                         if (RunningOnUnix)
3323                                 Assert.Ignore ("Running on Unix.");
3324
3325                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName,
3326                                 "doesnotexist");
3327                         sc.Refresh ();
3328                 }
3329
3330                 [Test]
3331                 public void Refresh_Service_DoesNotExist ()
3332                 {
3333                         if (RunningOnUnix)
3334                                 Assert.Ignore ("Running on Unix.");
3335
3336                         ServiceController sc = new ServiceController ("doesnotexist", ".");
3337                         sc.Refresh ();
3338                 }
3339
3340                 [Test]
3341                 public void Refresh_Service_Paused ()
3342                 {
3343                         if (RunningOnUnix)
3344                                 Assert.Ignore ("Running on Unix.");
3345
3346                         ServiceController sc1 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
3347                         ServiceController sc2 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
3348
3349                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#A1");
3350                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#A2");
3351
3352                         sc1.Pause ();
3353
3354                         try {
3355                                 Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#B1");
3356                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#B2");
3357
3358                                 sc1.Refresh ();
3359
3360                                 Assert.AreEqual (ServiceControllerStatus.Paused, sc1.Status, "#C1");
3361                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#C2");
3362
3363                                 sc1.WaitForStatus (ServiceControllerStatus.Paused, new TimeSpan (0, 0, 5));
3364
3365                                 Assert.AreEqual (ServiceControllerStatus.Paused, sc1.Status, "#D1");
3366                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#D2");
3367
3368                                 sc2.Refresh ();
3369
3370                                 Assert.AreEqual (ServiceControllerStatus.Paused, sc1.Status, "#E1");
3371                                 Assert.AreEqual (ServiceControllerStatus.Paused, sc2.Status, "#E2");
3372                         } finally {
3373                                 EnsureServiceIsRunning (sc1);
3374                                 sc2.Refresh ();
3375                         }
3376
3377                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#F1");
3378                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#F2");
3379                 }
3380
3381                 [Test]
3382                 public void Refresh_Service_Running ()
3383                 {
3384                         if (RunningOnUnix)
3385                                 Assert.Ignore ("Running on Unix.");
3386
3387                         ServiceController sc1 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
3388                         ServiceController sc2 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
3389
3390                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#A1");
3391                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#A2");
3392
3393                         sc1.Stop ();
3394
3395                         try {
3396                                 Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#B1");
3397                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#B2");
3398
3399                                 sc1.WaitForStatus (ServiceControllerStatus.Stopped, new TimeSpan (0, 0, 5));
3400                                 sc2.WaitForStatus (ServiceControllerStatus.Stopped, new TimeSpan (0, 0, 5));
3401
3402                                 Assert.AreEqual (ServiceControllerStatus.Stopped, sc1.Status, "#C1");
3403                                 Assert.AreEqual (ServiceControllerStatus.Stopped, sc2.Status, "#C2");
3404
3405                                 sc1.Start ();
3406
3407                                 Assert.AreEqual (ServiceControllerStatus.Stopped, sc1.Status, "#D1");
3408                                 Assert.AreEqual (ServiceControllerStatus.Stopped, sc2.Status, "#D2");
3409
3410                                 sc1.Refresh ();
3411
3412                                 Assert.AreEqual (ServiceControllerStatus.StartPending, sc1.Status, "#E1");
3413                                 Assert.AreEqual (ServiceControllerStatus.Stopped, sc2.Status, "#E2");
3414
3415                                 sc1.WaitForStatus (ServiceControllerStatus.Running, new TimeSpan (0, 0, 5));
3416
3417                                 Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#F1");
3418                                 Assert.AreEqual (ServiceControllerStatus.Stopped, sc2.Status, "#F2");
3419
3420                                 sc2.Refresh ();
3421
3422                                 Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#G1");
3423                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#G2");
3424                         } finally {
3425                                 EnsureServiceIsRunning (sc1);
3426                                 sc2.Refresh ();
3427                         }
3428
3429                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#H1");
3430                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#H2");
3431                 }
3432
3433                 [Test]
3434                 public void Refresh_Service_Stopped ()
3435                 {
3436                         if (RunningOnUnix)
3437                                 Assert.Ignore ("Running on Unix.");
3438
3439                         ServiceController sc1 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
3440                         ServiceController sc2 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
3441
3442                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#A1");
3443                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#A2");
3444
3445                         sc1.Stop ();
3446
3447                         try {
3448                                 Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#B1");
3449                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#B2");
3450
3451                                 sc1.WaitForStatus (ServiceControllerStatus.Stopped, new TimeSpan (0, 0, 5));
3452
3453                                 Assert.AreEqual (ServiceControllerStatus.Stopped, sc1.Status, "#D1");
3454                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#D2");
3455
3456                                 sc2.Refresh ();
3457
3458                                 Assert.AreEqual (ServiceControllerStatus.Stopped, sc1.Status, "#E1");
3459                                 Assert.AreEqual (ServiceControllerStatus.Stopped, sc2.Status, "#E2");
3460                         } finally {
3461                                 EnsureServiceIsRunning (sc1);
3462                                 sc2.Refresh ();
3463                         }
3464
3465                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#F1");
3466                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#F2");
3467                 }
3468
3469                 [Test]
3470                 public void Refresh_ServiceName_Empty ()
3471                 {
3472                         if (RunningOnUnix)
3473                                 Assert.Ignore ("Running on Unix.");
3474
3475                         ServiceController sc = new ServiceController ();
3476                         sc.Refresh ();
3477                 }
3478
3479                 [Test]
3480                 public void ServiceName ()
3481                 {
3482                         if (RunningOnUnix)
3483                                 Assert.Ignore ("Running on Unix.");
3484
3485                         var s1 = WORKSTATION_SERVICE;
3486                         var s2 = WINDOWS_SEARCH_SERVICE;
3487
3488                         ServiceController sc = new ServiceController ();
3489                         sc.ServiceName = s1.ServiceName;
3490                         Assert.AreEqual (s1.DisplayName, sc.DisplayName, "#A1");
3491                         Assert.AreEqual (s1.ServiceName, sc.ServiceName, "#A2");
3492
3493                         sc.ServiceName = s2.ServiceName;
3494                         Assert.AreEqual (s2.DisplayName, sc.DisplayName, "#B1");
3495                         Assert.AreEqual (s2.ServiceName, sc.ServiceName, "#B2");
3496
3497                         sc = new ServiceController (s1.ServiceName);
3498                         sc.ServiceName = s2.ServiceName;
3499                         Assert.AreEqual (s2.ServiceName, sc.ServiceName, "#C1");
3500                         Assert.AreEqual (s2.DisplayName, sc.DisplayName, "#C2");
3501                         Assert.AreEqual (s2.DisplayName, sc.DisplayName, "#C3");
3502
3503                         sc.ServiceName = s2.ServiceName;
3504                         Assert.AreEqual (s2.ServiceName, sc.ServiceName, "#D1");
3505                         Assert.AreEqual (s2.DisplayName, sc.DisplayName, "#D2");
3506
3507                         sc.ServiceName = s1.ServiceName;
3508                         Assert.AreEqual (s1.ServiceName, sc.ServiceName, "#E1");
3509                         Assert.AreEqual (s1.DisplayName, sc.DisplayName, "#E2");
3510
3511                         sc = new ServiceController (s1.ServiceName.ToUpper ());
3512                         Assert.AreEqual (s1.ServiceName.ToUpper (), sc.ServiceName, "#F1");
3513                         Assert.AreEqual (s1.DisplayName, sc.DisplayName, "#F2");
3514
3515                         var serviceName = s1.ServiceName [0].ToString ().ToUpper () + s1.ServiceName.Substring (1).ToLower ();
3516                         sc.ServiceName = serviceName;
3517                         Assert.AreEqual (serviceName, sc.ServiceName, "#G1");
3518                         Assert.AreEqual (s1.DisplayName, sc.DisplayName, "#G2");
3519                 }
3520
3521                 [Test]
3522                 public void ServiceName_Machine_DoesNotExist ()
3523                 {
3524                         if (RunningOnUnix)
3525                                 Assert.Ignore ("Running on Unix.");
3526
3527                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName,
3528                                 "doesnotexist");
3529                         try {
3530                                 string serviceName = sc.ServiceName;
3531                                 Assert.Fail ("#1: " + serviceName);
3532                         } catch (InvalidOperationException ex) {
3533                                 // Cannot open Service Control Manager on computer 'doesnotexist'.
3534                                 // This operation might require other priviliges
3535                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
3536                                 Assert.IsNotNull (ex.Message, "#3");
3537                                 Assert.IsTrue (ex.Message.IndexOf ("'doesnotexist'") != -1, "#4");
3538                                 Assert.IsNotNull (ex.InnerException, "#5");
3539
3540                                 // The RPC server is unavailable
3541                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#6");
3542                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
3543                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#7");
3544                                 Assert.IsNotNull (win32Error.Message, "#8");
3545                                 Assert.AreEqual (1722, win32Error.NativeErrorCode, "#9");
3546                                 Assert.IsNull (win32Error.InnerException, "#10");
3547                         }
3548                 }
3549
3550                 [Test]
3551                 public void ServiceName_Service_Disabled ()
3552                 {
3553                         if (RunningOnUnix)
3554                                 Assert.Ignore ("Running on Unix.");
3555
3556                         ServiceController sc = new ServiceController (DISABLED_SERVICE.ServiceName, ".");
3557                         Assert.AreEqual (DISABLED_SERVICE.ServiceName, sc.ServiceName);
3558                 }
3559
3560                 [Test]
3561                 public void ServiceName_Service_DoesNotExist ()
3562                 {
3563                         if (RunningOnUnix)
3564                                 Assert.Ignore ("Running on Unix.");
3565
3566                         ServiceController sc = new ServiceController ("doesnotexist", ".");
3567                         try {
3568                                 string serviceName = sc.ServiceName;
3569                                 Assert.Fail ("#1: " + serviceName);
3570                         } catch (InvalidOperationException ex) {
3571                                 // Cannot open doesnotexist service on computer '.'
3572                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
3573                                 Assert.IsNotNull (ex.Message, "#3");
3574                                 Assert.IsTrue (ex.Message.IndexOf ("doesnotexist") != -1, "#4");
3575                                 Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#5");
3576                                 Assert.IsNotNull (ex.InnerException, "#6");
3577
3578                                 // The filename, directory name, or volume label is incorrect
3579                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#7");
3580                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
3581                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#8");
3582                                 Assert.IsNotNull (win32Error.Message, "#9");
3583                                 Assert.AreEqual (1060, win32Error.NativeErrorCode, "#10");
3584                                 Assert.IsNull (win32Error.InnerException, "#11");
3585                         }
3586                 }
3587
3588                 [Test]
3589                 public void ServiceName_DisplayName_Empty ()
3590                 {
3591                         if (RunningOnUnix)
3592                                 Assert.Ignore ("Running on Unix.");
3593
3594                         ServiceController sc = new ServiceController ();
3595                         sc.ServiceName = WORKSTATION_SERVICE.ServiceName;
3596                         Assert.AreEqual (WORKSTATION_SERVICE.DisplayName, sc.DisplayName, "#1");
3597                         Assert.AreEqual (WORKSTATION_SERVICE.ServiceName, sc.ServiceName, "#2");
3598                 }
3599
3600                 [Test]
3601                 public void ServiceName_Value_DoesNotExist ()
3602                 {
3603                         if (RunningOnUnix)
3604                                 Assert.Ignore ("Running on Unix.");
3605
3606                         ServiceController sc = new ServiceController ();
3607                         sc.ServiceName = "doesnotexist";
3608                         try {
3609                                 string displayName = sc.DisplayName;
3610                                 Assert.Fail ("#1: " + displayName);
3611                         } catch (InvalidOperationException ex) {
3612                                 // Service doesnotexist was not found on computer '.'
3613                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
3614                                 Assert.IsNotNull (ex.Message, "#3");
3615                                 Assert.IsTrue (ex.Message.IndexOf ("doesnotexist") != -1, "#4");
3616                                 Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#5");
3617                                 Assert.IsNotNull (ex.InnerException, "#6");
3618
3619                                 // The specified service does not exist as an installed service
3620                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#7");
3621                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
3622                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#8");
3623                                 Assert.IsNotNull (win32Error.Message, "#9");
3624                                 Assert.AreEqual (1060, win32Error.NativeErrorCode, "#10");
3625                                 Assert.IsNull (win32Error.InnerException, "#11");
3626                         }
3627                         Assert.AreEqual ("doesnotexist", sc.ServiceName, "#12");
3628                 }
3629
3630                 [Test]
3631                 public void ServiceName_Value_Empty ()
3632                 {
3633                         if (RunningOnUnix)
3634                                 Assert.Ignore ("Running on Unix.");
3635
3636                         ServiceController sc = new ServiceController ();
3637                         Assert.AreEqual (string.Empty, sc.DisplayName, "#A1");
3638                         Assert.AreEqual (string.Empty, sc.ServiceName, "#A2");
3639
3640                         sc.ServiceName = WORKSTATION_SERVICE.ServiceName;
3641
3642                         Assert.AreEqual (WORKSTATION_SERVICE.DisplayName, sc.DisplayName, "#B1");
3643                         Assert.AreEqual (WORKSTATION_SERVICE.ServiceName, sc.ServiceName, "#B2");
3644
3645                         try {
3646                                 sc.ServiceName = string.Empty;
3647                                 Assert.Fail ("#C1");
3648                         } catch (ArgumentException ex) {
3649                                 // Service name  contains invalid characters, is empty or is
3650                                 // too long (max length = 80)
3651                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
3652                                 Assert.IsNotNull (ex.Message, "#A3");
3653                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#A4");
3654                                 Assert.IsTrue (ex.Message.IndexOf ("80") != -1, "#A5");
3655                                 Assert.IsNull (ex.ParamName, "#A6");
3656                                 Assert.IsNull (ex.InnerException, "#A7");
3657                         }
3658                 }
3659
3660                 [Test]
3661                 public void ServiceName_Value_Null ()
3662                 {
3663                         if (RunningOnUnix)
3664                                 Assert.Ignore ("Running on Unix.");
3665
3666                         ServiceController sc = new ServiceController ();
3667                         sc.ServiceName = WORKSTATION_SERVICE.ServiceName;
3668                         try {
3669                                 sc.ServiceName = null;
3670                                 Assert.Fail ("#1");
3671                         } catch (ArgumentNullException ex) {
3672                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3673                                 Assert.IsNotNull (ex.Message, "#3");
3674                                 Assert.IsNotNull (ex.ParamName, "#4");
3675                                 Assert.AreEqual ("value", ex.ParamName, "#5");
3676                                 Assert.IsNull (ex.InnerException, "#6");
3677                         }
3678
3679                         Assert.AreEqual (WORKSTATION_SERVICE.ServiceName, sc.ServiceName, "#7");
3680                 }
3681
3682                 [Test]
3683                 public void ServiceName_Value_DisplayName ()
3684                 {
3685                         if (RunningOnUnix)
3686                                 Assert.Ignore ("Running on Unix.");
3687
3688                         ServiceController sc = new ServiceController ();
3689                         sc.ServiceName = WORKSTATION_SERVICE.DisplayName.ToLower ();
3690                         try {
3691                                 string displayName = sc.DisplayName;
3692                                 Assert.Fail ("#1: " + displayName);
3693                         } catch (InvalidOperationException ex) {
3694                                 // Display name could not be retrieved for service XXX
3695                                 // on computer '.'
3696                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
3697                                 Assert.IsNotNull (ex.Message, "#3");
3698                                 Assert.IsTrue (ex.Message.IndexOf (WORKSTATION_SERVICE.DisplayName.ToLower ()) != -1, "#4");
3699                                 Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#5");
3700                                 Assert.IsNotNull (ex.InnerException, "#6");
3701
3702                                 // The specified service does not exist as an installed service
3703                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#7");
3704                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
3705                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#8");
3706                                 Assert.IsNotNull (win32Error.Message, "#9");
3707                                 Assert.AreEqual (1060, win32Error.NativeErrorCode, "#10");
3708                                 Assert.IsNull (win32Error.InnerException, "#11");
3709                         }
3710                         Assert.AreEqual (WORKSTATION_SERVICE.DisplayName.ToLower (), sc.ServiceName, "#12");
3711                 }
3712
3713                 [Test]
3714                 public void ServicesDependedOn ()
3715                 {
3716                         if (RunningOnUnix)
3717                                 Assert.Ignore ("Running on Unix.");
3718
3719                         ServiceController sc = null;
3720                         ServiceController [] servicesDependedOn = null;
3721
3722                         // single depended service
3723                         sc = new ServiceController (SERVICE_WITH_ONE_DEPENDED_ON.ServiceName, ".");
3724                         servicesDependedOn = sc.ServicesDependedOn;
3725                         Assert.IsNotNull (servicesDependedOn, "#A1");
3726                         Assert.AreEqual (1, servicesDependedOn.Length, "#A2");
3727                         Assert.AreEqual (SERVICE_WITH_ONE_DEPENDED_ON.DependedOn, ServiceNames (servicesDependedOn), "#A3");
3728
3729                         // modifying ServiceName does not cause cache to be cleared:
3730                         // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=201762
3731                         sc.ServiceName = SERVICE_WITH_NO_DEPENDED_ON.ServiceName;
3732                         servicesDependedOn = sc.ServicesDependedOn;
3733                         Assert.IsNotNull (servicesDependedOn, "#B1");
3734                         Assert.AreEqual (1, servicesDependedOn.Length, "#B2");
3735                         Assert.AreEqual (SERVICE_WITH_ONE_DEPENDED_ON.DependedOn, ServiceNames (servicesDependedOn), "#B3");
3736
3737                         // modifying DisplayName does not cause cache to be cleared:
3738                         // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=201762
3739                         sc.DisplayName = SERVICE_WITH_MANY_DEPENDED_ON.DisplayName;
3740                         servicesDependedOn = sc.ServicesDependedOn;
3741                         Assert.IsNotNull (servicesDependedOn, "#C1");
3742                         Assert.AreEqual (1, servicesDependedOn.Length, "#C2");
3743                         Assert.AreEqual (SERVICE_WITH_ONE_DEPENDED_ON.DependedOn, ServiceNames (servicesDependedOn), "#C3");
3744
3745                         // modifying MachineName does not cause cache to be cleared:
3746                         // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=201762
3747                         sc.MachineName = "doesnotexist";
3748                         servicesDependedOn = sc.ServicesDependedOn;
3749                         Assert.IsNotNull (servicesDependedOn, "#D1");
3750                         Assert.AreEqual (1, servicesDependedOn.Length, "#D2");
3751                         Assert.AreEqual (SERVICE_WITH_ONE_DEPENDED_ON.DependedOn, ServiceNames (servicesDependedOn), "#D3");
3752
3753                         // no depended services
3754                         sc = new ServiceController (SERVICE_WITH_NO_DEPENDED_ON.ServiceName, ".");
3755                         servicesDependedOn = sc.ServicesDependedOn;
3756                         Assert.IsNotNull (servicesDependedOn, "#E1");
3757                         Assert.AreEqual (0, servicesDependedOn.Length, "#E2");
3758
3759                         // modifying ServiceName does not cause cache to be cleared:
3760                         // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=201762
3761                         sc.ServiceName = SERVICE_WITH_MANY_DEPENDED_ON.ServiceName;
3762                         servicesDependedOn = sc.ServicesDependedOn;
3763                         Assert.IsNotNull (servicesDependedOn, "#F1");
3764                         Assert.AreEqual (0, servicesDependedOn.Length, "#F2");
3765
3766                         // modifying DisplayName does not cause cache to be cleared:
3767                         // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=201762
3768                         sc.DisplayName = SERVICE_WITH_ONE_DEPENDED_ON.DisplayName;
3769                         servicesDependedOn = sc.ServicesDependedOn;
3770                         Assert.IsNotNull (servicesDependedOn, "#G1");
3771                         Assert.AreEqual (0, servicesDependedOn.Length, "#G2");
3772
3773                         // modifying MachineName does not cause cache to be cleared:
3774                         // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=201762
3775                         sc.MachineName = Environment.MachineName;
3776                         servicesDependedOn = sc.ServicesDependedOn;
3777                         Assert.IsNotNull (servicesDependedOn, "#H1");
3778                         Assert.AreEqual (0, servicesDependedOn.Length, "#H2");
3779
3780                         // multiple depended services
3781                         sc = new ServiceController (SERVICE_WITH_MANY_DEPENDED_ON.ServiceName, ".");
3782                         servicesDependedOn = sc.ServicesDependedOn;
3783                         Assert.IsNotNull (servicesDependedOn, "#I1");
3784                         Assert.AreEqual (SERVICE_WITH_MANY_DEPENDED_ON.DependedOn.Length, servicesDependedOn.Length, "#I2");
3785                         Assert.AreEqual (SERVICE_WITH_MANY_DEPENDED_ON.DependedOn, ServiceNames (servicesDependedOn), "#I3");
3786
3787                         // modifying ServiceName does not cause cache to be cleared:
3788                         // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=201762
3789                         sc.ServiceName = SERVICE_WITH_NO_DEPENDED_ON.ServiceName;
3790                         servicesDependedOn = sc.ServicesDependedOn;
3791                         Assert.IsNotNull (servicesDependedOn, "#J1");
3792                         Assert.AreEqual (SERVICE_WITH_MANY_DEPENDED_ON.DependedOn.Length, servicesDependedOn.Length, "#J2");
3793                         Assert.AreEqual (SERVICE_WITH_MANY_DEPENDED_ON.DependedOn, ServiceNames (servicesDependedOn), "#J3");
3794
3795                         // modifying DisplayName does not cause cache to be cleared:
3796                         // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=201762
3797                         sc.DisplayName = SERVICE_WITH_ONE_DEPENDED_ON.DisplayName;
3798                         servicesDependedOn = sc.ServicesDependedOn;
3799                         Assert.IsNotNull (servicesDependedOn, "#K1");
3800                         Assert.AreEqual (SERVICE_WITH_MANY_DEPENDED_ON.DependedOn.Length, servicesDependedOn.Length, "#K2");
3801                         Assert.AreEqual (SERVICE_WITH_MANY_DEPENDED_ON.DependedOn, ServiceNames (servicesDependedOn), "#K3");
3802
3803                         // modifying MachineName does not cause cache to be cleared:
3804                         // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=201762
3805                         sc.MachineName = Environment.MachineName;
3806                         servicesDependedOn = sc.ServicesDependedOn;
3807                         Assert.IsNotNull (servicesDependedOn, "#L1");
3808                         Assert.AreEqual (SERVICE_WITH_MANY_DEPENDED_ON.DependedOn.Length, servicesDependedOn.Length, "#L2");
3809                         Assert.AreEqual (SERVICE_WITH_MANY_DEPENDED_ON.DependedOn, ServiceNames (servicesDependedOn), "#L3");
3810                 }
3811
3812                 [Test]
3813                 public void ServicesDependedOn_Machine_DoesNotExist ()
3814                 {
3815                         if (RunningOnUnix)
3816                                 Assert.Ignore ("Running on Unix.");
3817
3818                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName,
3819                                 "doesnotexist");
3820                         try {
3821                                 ServiceController [] servicesDependedOn = sc.ServicesDependedOn;
3822                                 Assert.Fail ("#1: " + servicesDependedOn.Length);
3823                         } catch (InvalidOperationException ex) {
3824                                 // Cannot open Service Control Manager on computer 'doesnotexist'.
3825                                 // This operation might require other priviliges
3826                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
3827                                 Assert.IsNotNull (ex.Message, "#3");
3828                                 Assert.IsTrue (ex.Message.IndexOf ("'doesnotexist'") != -1, "#4");
3829                                 Assert.IsNotNull (ex.InnerException, "#5");
3830
3831                                 // The RPC server is unavailable
3832                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#6");
3833                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
3834                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#7");
3835                                 Assert.IsNotNull (win32Error.Message, "#8");
3836                                 Assert.AreEqual (1722, win32Error.NativeErrorCode, "#9");
3837                                 Assert.IsNull (win32Error.InnerException, "#10");
3838                         }
3839                 }
3840
3841                 [Test]
3842                 public void ServicesDependedOn_Service_Disabled ()
3843                 {
3844                         if (RunningOnUnix)
3845                                 Assert.Ignore ("Running on Unix.");
3846
3847                         ServiceController sc = new ServiceController (DISABLED_SERVICE.ServiceName, ".");
3848                         ServiceController [] servicesDependedOn = sc.ServicesDependedOn;
3849                         Assert.IsNotNull (servicesDependedOn, "#1");
3850                         Assert.AreEqual (DISABLED_SERVICE.DependedOn, ServiceNames (servicesDependedOn), "#2");
3851                 }
3852
3853                 [Test]
3854                 public void ServicesDependedOn_Service_DoesNotExist ()
3855                 {
3856                         if (RunningOnUnix)
3857                                 Assert.Ignore ("Running on Unix.");
3858
3859                         ServiceController sc = new ServiceController ("doesnotexist", ".");
3860                         try {
3861                                 ServiceController [] servicesDependedOn = sc.ServicesDependedOn;
3862                                 Assert.Fail ("#1: " + servicesDependedOn.Length);
3863                         } catch (InvalidOperationException ex) {
3864                                 // Cannot open doesnotexist service on computer '.'
3865                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
3866                                 Assert.IsNotNull (ex.Message, "#3");
3867                                 Assert.IsTrue (ex.Message.IndexOf ("doesnotexist") != -1, "#4");
3868                                 Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#5");
3869                                 Assert.IsNotNull (ex.InnerException, "#6");
3870
3871                                 // The filename, directory name, or volume label is incorrect
3872                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#7");
3873                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
3874                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#8");
3875                                 Assert.IsNotNull (win32Error.Message, "#9");
3876                                 Assert.AreEqual (1060, win32Error.NativeErrorCode, "#10");
3877                                 Assert.IsNull (win32Error.InnerException, "#11");
3878                         }
3879                 }
3880
3881                 [Test]
3882                 public void ServicesDependedOn_ServiceName_Empty ()
3883                 {
3884                         ServiceController sc = new ServiceController ();
3885                         try {
3886                                 ServiceController [] servicesDependedOn = sc.ServicesDependedOn;
3887                                 Assert.Fail ("#1: " + servicesDependedOn.Length);
3888                         } catch (ArgumentException ex) {
3889                                 // Service name  contains invalid characters, is empty or is
3890                                 // too long (max length = 80)
3891                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
3892                                 Assert.IsNotNull (ex.Message, "#3");
3893                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#4");
3894                                 Assert.IsTrue (ex.Message.IndexOf ("80") != -1, "#5");
3895                                 Assert.IsNull (ex.ParamName, "#6");
3896                                 Assert.IsNull (ex.InnerException, "#7");
3897                         }
3898                 }
3899
3900                 [Test]
3901                 public void ServiceTypeTest ()
3902                 {
3903                         if (RunningOnUnix)
3904                                 Assert.Ignore ("Running on Unix.");
3905
3906                         ServiceController sc = null;
3907                         
3908                         sc = new ServiceController (SHARE_PROCESS_SERVICE.ServiceName, ".");
3909                         Assert.AreEqual (ServiceType.Win32ShareProcess, sc.ServiceType, "#A1");
3910                         sc.ServiceName = KERNEL_SERVICE.ServiceName;
3911                         Assert.AreEqual (ServiceType.KernelDriver, sc.ServiceType, "#A2");
3912                         sc.DisplayName = SHARE_PROCESS_SERVICE.ServiceName;
3913                         Assert.AreEqual (ServiceType.Win32ShareProcess, sc.ServiceType, "#A3");
3914                         sc.MachineName = "doesnotexist";
3915                         try {
3916                                 ServiceType serviceType = sc.ServiceType;
3917                                 Assert.Fail ("#A4: " + serviceType);
3918                         } catch (InvalidOperationException ex) {
3919                                 // Cannot open Service Control Manager on computer 'doesnotexist'.
3920                                 // This operation might require other priviliges
3921                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A5");
3922                                 Assert.IsNotNull (ex.Message, "#A6");
3923                                 Assert.IsTrue (ex.Message.IndexOf ("'doesnotexist'") != -1, "#A7");
3924                                 Assert.IsNotNull (ex.InnerException, "#A8");
3925
3926                                 // The RPC server is unavailable
3927                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#A9");
3928                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
3929                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#A10");
3930                                 Assert.IsNotNull (win32Error.Message, "#A11");
3931                                 Assert.AreEqual (1722, win32Error.NativeErrorCode, "#A12");
3932                                 Assert.IsNull (win32Error.InnerException, "#A13");
3933                         }
3934
3935                         sc = new ServiceController (KERNEL_SERVICE.ServiceName, ".");
3936                         Assert.AreEqual (ServiceType.KernelDriver, sc.ServiceType, "#B1");
3937                         sc.DisplayName = SHARE_PROCESS_SERVICE.DisplayName;
3938                         Assert.AreEqual (ServiceType.Win32ShareProcess, sc.ServiceType, "#B2");
3939                         sc.MachineName = Environment.MachineName;
3940                         Assert.AreEqual (ServiceType.Win32ShareProcess, sc.ServiceType, "#B3");
3941                         sc.ServiceName = KERNEL_SERVICE.ServiceName;
3942                         Assert.AreEqual (ServiceType.KernelDriver, sc.ServiceType, "#B4");
3943                 }
3944
3945                 [Test]
3946                 public void ServiceType_Machine_DoesNotExist ()
3947                 {
3948                         if (RunningOnUnix)
3949                                 Assert.Ignore ("Running on Unix.");
3950
3951                         ServiceController sc = new ServiceController (SHARE_PROCESS_SERVICE.ServiceName,
3952                                 "doesnotexist");
3953                         try {
3954                                 ServiceType serviceType = sc.ServiceType;
3955                                 Assert.Fail ("#1: " + serviceType);
3956                         } catch (InvalidOperationException ex) {
3957                                 // Cannot open Service Control Manager on computer 'doesnotexist'.
3958                                 // This operation might require other priviliges
3959                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
3960                                 Assert.IsNotNull (ex.Message, "#3");
3961                                 Assert.IsTrue (ex.Message.IndexOf ("'doesnotexist'") != -1, "#4");
3962                                 Assert.IsNotNull (ex.InnerException, "#5");
3963
3964                                 // The RPC server is unavailable
3965                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#6");
3966                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
3967                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#7");
3968                                 Assert.IsNotNull (win32Error.Message, "#8");
3969                                 Assert.AreEqual (1722, win32Error.NativeErrorCode, "#9");
3970                                 Assert.IsNull (win32Error.InnerException, "#10");
3971                         }
3972                 }
3973
3974                 [Test]
3975                 public void ServiceType_Service_Disabled ()
3976                 {
3977                         if (RunningOnUnix)
3978                                 Assert.Ignore ("Running on Unix.");
3979
3980                         ServiceController sc = new ServiceController (DISABLED_SERVICE.ServiceName, ".");
3981                         Assert.AreEqual (ServiceType.Win32ShareProcess, sc.ServiceType);
3982                 }
3983
3984                 [Test]
3985                 public void ServiceType_Service_DoesNotExist ()
3986                 {
3987                         if (RunningOnUnix)
3988                                 Assert.Ignore ("Running on Unix.");
3989
3990                         ServiceController sc = new ServiceController ("doesnotexist", ".");
3991                         try {
3992                                 ServiceType serviceType = sc.ServiceType;
3993                                 Assert.Fail ("#1: " + serviceType);
3994                         } catch (InvalidOperationException ex) {
3995                                 // Cannot open doesnotexist service on computer '.'
3996                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
3997                                 Assert.IsNotNull (ex.Message, "#3");
3998                                 Assert.IsTrue (ex.Message.IndexOf ("doesnotexist") != -1, "#4");
3999                                 Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#5");
4000                                 Assert.IsNotNull (ex.InnerException, "#6");
4001
4002                                 // The filename, directory name, or volume label is incorrect
4003                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#7");
4004                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
4005                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#8");
4006                                 Assert.IsNotNull (win32Error.Message, "#9");
4007                                 Assert.AreEqual (1060, win32Error.NativeErrorCode, "#10");
4008                                 Assert.IsNull (win32Error.InnerException, "#11");
4009                         }
4010                 }
4011
4012                 [Test]
4013                 public void ServiceType_ServiceName_Empty ()
4014                 {
4015                         if (RunningOnUnix)
4016                                 Assert.Ignore ("Running on Unix.");
4017
4018                         ServiceController sc = null;
4019
4020                         sc = new ServiceController ();
4021                         sc.DisplayName = SHARE_PROCESS_SERVICE.DisplayName.ToLower ();
4022                         Assert.AreEqual (ServiceType.Win32ShareProcess, sc.ServiceType, "#1");
4023
4024                         sc = new ServiceController ();
4025                         sc.DisplayName = KERNEL_SERVICE.DisplayName.ToLower ();
4026                         Assert.AreEqual (ServiceType.KernelDriver, sc.ServiceType, "#2");
4027                 }
4028
4029                 [Test]
4030                 public void Start ()
4031                 {
4032                         // not sure if we need additional tests for this, as it's actually
4033                         // tested as part of the other unit tests
4034                 }
4035
4036                 [Test]
4037                 public void Status ()
4038                 {
4039                         // not sure if we need additional tests for this, as it's actually
4040                         // tested as part of the other unit tests
4041                 }
4042
4043                 [Test]
4044                 public void Stop ()
4045                 {
4046                         if (RunningOnUnix)
4047                                 Assert.Ignore ("Running on Unix.");
4048
4049                         ServiceController sc1 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
4050                         ServiceController sc2 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
4051
4052                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#A1");
4053                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#A2");
4054
4055                         sc1.Stop ();
4056
4057                         try {
4058                                 Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#B1");
4059                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#B2");
4060
4061                                 sc1.WaitForStatus (ServiceControllerStatus.Stopped, new TimeSpan (0, 0, 5));
4062
4063                                 Assert.AreEqual (ServiceControllerStatus.Stopped, sc1.Status, "#C1");
4064                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#C2");
4065                         } finally {
4066                                 EnsureServiceIsRunning (sc1);
4067                                 sc2.Refresh ();
4068                         }
4069
4070                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#D1");
4071                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#D2");
4072                 }
4073
4074                 [Test]
4075                 public void Stop_Machine_DoesNotExist ()
4076                 {
4077                         if (RunningOnUnix)
4078                                 Assert.Ignore ("Running on Unix.");
4079
4080                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName,
4081                                 "doesnotexist");
4082                         try {
4083                                 sc.Stop ();
4084                                 Assert.Fail ("#1");
4085                         } catch (InvalidOperationException ex) {
4086                                 // Cannot open Service Control Manager on computer 'doesnotexist'.
4087                                 // This operation might require other priviliges
4088                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
4089                                 Assert.IsNotNull (ex.Message, "#3");
4090                                 Assert.IsTrue (ex.Message.IndexOf ("'doesnotexist'") != -1, "#4");
4091                                 Assert.IsNotNull (ex.InnerException, "#5");
4092
4093                                 // The RPC server is unavailable
4094                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#6");
4095                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
4096                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#7");
4097                                 Assert.IsNotNull (win32Error.Message, "#8");
4098                                 Assert.AreEqual (1722, win32Error.NativeErrorCode, "#9");
4099                                 Assert.IsNull (win32Error.InnerException, "#10");
4100                         }
4101                 }
4102
4103                 [Test]
4104                 public void Stop_Service_Disabled ()
4105                 {
4106                         if (RunningOnUnix)
4107                                 Assert.Ignore ("Running on Unix.");
4108
4109                         ServiceController sc1 = new ServiceController (DISABLED_SERVICE.ServiceName, ".");
4110                         ServiceController sc2 = new ServiceController (DISABLED_SERVICE.ServiceName, ".");
4111
4112                         Assert.AreEqual (ServiceControllerStatus.Stopped, sc1.Status, "#A1");
4113                         Assert.AreEqual (ServiceControllerStatus.Stopped, sc2.Status, "#A2");
4114
4115                         try {
4116                                 sc1.Stop ();
4117                                 Assert.Fail ("#B1");
4118                         } catch (InvalidOperationException ex) {
4119                                 // Cannot stop NetDDE service on computer '.'
4120                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
4121                                 Assert.IsNotNull (ex.Message, "#B3");
4122                                 Assert.IsTrue (ex.Message.IndexOf (DISABLED_SERVICE.ServiceName) != -1, "#B4");
4123                                 Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#B5");
4124                                 Assert.IsNotNull (ex.InnerException, "#B6");
4125
4126                                 // The service has not been started
4127                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#B7");
4128                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
4129                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#B8");
4130                                 Assert.IsNotNull (win32Error.Message, "#B9");
4131                                 Assert.AreEqual (1062, win32Error.NativeErrorCode, "#B10");
4132                                 Assert.IsNull (win32Error.InnerException, "#B11");
4133                         }
4134
4135                         Assert.AreEqual (ServiceControllerStatus.Stopped, sc1.Status, "#C1");
4136                         Assert.AreEqual (ServiceControllerStatus.Stopped, sc2.Status, "#C2");
4137                 }
4138
4139                 [Test]
4140                 public void Stop_Service_DoesNotExist ()
4141                 {
4142                         if (RunningOnUnix)
4143                                 Assert.Ignore ("Running on Unix.");
4144
4145                         ServiceController sc = new ServiceController ("doesnotexist", ".");
4146                         try {
4147                                 sc.Stop ();
4148                                 Assert.Fail ("#1");
4149                         } catch (InvalidOperationException ex) {
4150                                 // Cannot open doesnotexist service on computer '.'
4151                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
4152                                 Assert.IsNotNull (ex.Message, "#3");
4153                                 Assert.IsTrue (ex.Message.IndexOf ("doesnotexist") != -1, "#4");
4154                                 Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#5");
4155                                 Assert.IsNotNull (ex.InnerException, "#6");
4156
4157                                 // The filename, directory name, or volume label is incorrect
4158                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#7");
4159                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
4160                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#8");
4161                                 Assert.IsNotNull (win32Error.Message, "#9");
4162                                 Assert.AreEqual (1060, win32Error.NativeErrorCode, "#10");
4163                                 Assert.IsNull (win32Error.InnerException, "#11");
4164                         }
4165                 }
4166
4167                 [Test]
4168                 public void Stop_Service_OperationNotValid ()
4169                 {
4170                         if (RunningOnUnix)
4171                                 Assert.Ignore ("Running on Unix.");
4172
4173                         ServiceController sc1 = new ServiceController (UNCONTROLLABLE_SERVICE.ServiceName, ".");
4174                         ServiceController sc2 = new ServiceController (UNCONTROLLABLE_SERVICE.ServiceName, ".");
4175
4176                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#A1");
4177                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#A2");
4178
4179                         try {
4180                                 sc1.Stop ();
4181                                 Assert.Fail ("#B1");
4182                         } catch (InvalidOperationException ex) {
4183                                 // Cannot stop XXX service on computer '.'
4184                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
4185                                 Assert.IsNotNull (ex.Message, "#B3");
4186                                 Assert.IsTrue (ex.Message.IndexOf (UNCONTROLLABLE_SERVICE.ServiceName) != -1, "#B4");
4187                                 Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#B5");
4188                                 Assert.IsNotNull (ex.InnerException, "#B6");
4189
4190                                 // The requested control is not valid for this service
4191                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#B7");
4192                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
4193                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#B8");
4194                                 Assert.IsNotNull (win32Error.Message, "#B9");
4195                                 Assert.AreEqual (1052, win32Error.NativeErrorCode, "#B10");
4196                                 Assert.IsNull (win32Error.InnerException, "#B11");
4197                         }
4198
4199                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#C1");
4200                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#C2");
4201                 }
4202
4203                 [Test]
4204                 public void Stop_Service_Paused ()
4205                 {
4206                         if (RunningOnUnix)
4207                                 Assert.Ignore ("Running on Unix.");
4208
4209                         ServiceController sc1 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
4210                         ServiceController sc2 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
4211
4212                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#A1");
4213                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#A2");
4214
4215                         sc1.Pause ();
4216
4217                         try {
4218                                 Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#B1");
4219                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#B2");
4220
4221                                 sc1.WaitForStatus (ServiceControllerStatus.Paused, new TimeSpan (0, 0, 5));
4222
4223                                 Assert.AreEqual (ServiceControllerStatus.Paused, sc1.Status, "#C1");
4224                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#C2");
4225
4226                                 sc1.Stop ();
4227
4228                                 Assert.AreEqual (ServiceControllerStatus.Paused, sc1.Status, "#D1");
4229                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#D2");
4230
4231                                 sc1.WaitForStatus (ServiceControllerStatus.Stopped, new TimeSpan (0, 0, 5));
4232
4233                                 Assert.AreEqual (ServiceControllerStatus.Stopped, sc1.Status, "#E1");
4234                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#E2");
4235                         } finally {
4236                                 EnsureServiceIsRunning (sc1);
4237                                 sc2.Refresh ();
4238                         }
4239
4240                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#F1");
4241                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#F2");
4242                 }
4243
4244                 [Test]
4245                 public void Stop_Service_Stopped ()
4246                 {
4247                         if (RunningOnUnix)
4248                                 Assert.Ignore ("Running on Unix.");
4249
4250                         ServiceController sc1 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
4251                         ServiceController sc2 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
4252
4253                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#A1");
4254                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#A2");
4255
4256                         sc1.Stop ();
4257
4258                         try {
4259                                 Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#B1");
4260                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#B2");
4261
4262                                 sc1.WaitForStatus (ServiceControllerStatus.Stopped, new TimeSpan (0, 0, 5));
4263
4264                                 Assert.AreEqual (ServiceControllerStatus.Stopped, sc1.Status, "#C1");
4265                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#C2");
4266
4267                                 sc1.Stop ();
4268                                 Assert.Fail ("#D1");
4269                         } catch (InvalidOperationException ex) {
4270                                 // Cannot stop XXX service on computer '.'
4271                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
4272                                 Assert.IsNotNull (ex.Message, "#D3");
4273                                 Assert.IsTrue (ex.Message.IndexOf (CONTROLLABLE_SERVICE.ServiceName) != -1, "#D4");
4274                                 Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#D5");
4275                                 Assert.IsNotNull (ex.InnerException, "#D6");
4276
4277                                 // The service has not been started
4278                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#D7");
4279                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
4280                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#D8");
4281                                 Assert.IsNotNull (win32Error.Message, "#D9");
4282                                 Assert.AreEqual (1062, win32Error.NativeErrorCode, "#D10");
4283                                 Assert.IsNull (win32Error.InnerException, "#D11");
4284                         } finally {
4285                                 EnsureServiceIsRunning (sc1);
4286                                 sc2.Refresh ();
4287                         }
4288
4289                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#E1");
4290                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#E2");
4291                 }
4292
4293                 [Test]
4294                 public void Stop_ServiceName_Empty ()
4295                 {
4296                         ServiceController sc = new ServiceController ();
4297                         try {
4298                                 sc.Stop ();
4299                                 Assert.Fail ("#1");
4300                         } catch (ArgumentException ex) {
4301                                 // Service name  contains invalid characters, is empty or is
4302                                 // too long (max length = 80)
4303                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4304                                 Assert.IsNotNull (ex.Message, "#3");
4305                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#4");
4306                                 Assert.IsTrue (ex.Message.IndexOf ("80") != -1, "#5");
4307                                 Assert.IsNull (ex.ParamName, "#6");
4308                                 Assert.IsNull (ex.InnerException, "#7");
4309                         }
4310                 }
4311
4312                 [Test]
4313                 public void WaitForStatus ()
4314                 {
4315                         if (RunningOnUnix)
4316                                 Assert.Ignore ("Running on Unix.");
4317
4318                         ServiceController sc1 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
4319                         ServiceController sc2 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
4320
4321                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#A1");
4322                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#A2");
4323
4324                         sc1.Stop ();
4325
4326                         try {
4327                                 Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#B1");
4328                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#B2");
4329
4330                                 sc1.WaitForStatus (ServiceControllerStatus.Stopped, new TimeSpan (0, 0, 5));
4331
4332                                 Assert.AreEqual (ServiceControllerStatus.Stopped, sc1.Status, "#C1");
4333                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#C2");
4334
4335                                 sc1.WaitForStatus (ServiceControllerStatus.Stopped);
4336
4337                                 Assert.AreEqual (ServiceControllerStatus.Stopped, sc1.Status, "#D1");
4338                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#D2");
4339
4340                                 sc1.Start ();
4341
4342                                 Assert.AreEqual (ServiceControllerStatus.Stopped, sc1.Status, "#D1");
4343                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#D2");
4344
4345                                 sc1.WaitForStatus (ServiceControllerStatus.Running);
4346
4347                                 Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#E1");
4348                                 Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#E2");
4349                         } finally {
4350                                 EnsureServiceIsRunning (sc1);
4351                                 sc2.Refresh ();
4352                         }
4353
4354                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#F1");
4355                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#F2");
4356                 }
4357
4358                 [Test]
4359                 public void WaitForStatus_Machine_DoesNotExist ()
4360                 {
4361                         if (RunningOnUnix)
4362                                 Assert.Ignore ("Running on Unix.");
4363
4364                         ServiceController sc = new ServiceController (CONTROLLABLE_SERVICE.ServiceName,
4365                                 "doesnotexist");
4366                         try {
4367                                 sc.WaitForStatus (ServiceControllerStatus.Stopped,
4368                                         new TimeSpan (0, 0, 1));
4369                                 Assert.Fail ("#1");
4370                         } catch (InvalidOperationException ex) {
4371                                 // Cannot open Service Control Manager on computer 'doesnotexist'.
4372                                 // This operation might require other priviliges
4373                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
4374                                 Assert.IsNotNull (ex.Message, "#3");
4375                                 Assert.IsTrue (ex.Message.IndexOf ("'doesnotexist'") != -1, "#4");
4376                                 Assert.IsNotNull (ex.InnerException, "#5");
4377
4378                                 // The RPC server is unavailable
4379                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#6");
4380                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
4381                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#7");
4382                                 Assert.IsNotNull (win32Error.Message, "#8");
4383                                 Assert.AreEqual (1722, win32Error.NativeErrorCode, "#9");
4384                                 Assert.IsNull (win32Error.InnerException, "#10");
4385                         }
4386                 }
4387
4388                 [Test]
4389                 public void WaitForStatus_Service_Disabled ()
4390                 {
4391                         if (RunningOnUnix)
4392                                 Assert.Ignore ("Running on Unix.");
4393
4394                         ServiceController sc1 = new ServiceController (DISABLED_SERVICE.ServiceName, ".");
4395                         ServiceController sc2 = new ServiceController (DISABLED_SERVICE.ServiceName, ".");
4396
4397                         Assert.AreEqual (ServiceControllerStatus.Stopped, sc1.Status, "#A1");
4398                         Assert.AreEqual (ServiceControllerStatus.Stopped, sc2.Status, "#A2");
4399
4400                         sc1.WaitForStatus (ServiceControllerStatus.Stopped,
4401                                 new TimeSpan (0, 0, 1));
4402
4403                         try {
4404                                 sc1.WaitForStatus (ServiceControllerStatus.Running,
4405                                         new TimeSpan (0, 0, 1));
4406                                 Assert.Fail ("#B1");
4407                         } catch (TimeoutException ex) {
4408                                 // Time out has expired and the operation has not been completed
4409                                 Assert.AreEqual (typeof (TimeoutException), ex.GetType (), "#B2");
4410                                 Assert.IsNotNull (ex.Data, "#B3");
4411                                 Assert.AreEqual (0, ex.Data.Count, "#B4");
4412                                 Assert.IsNotNull (ex.Message, "#B5");
4413                                 Assert.IsNull (ex.InnerException, "#B6");
4414                         }
4415
4416                         Assert.AreEqual (ServiceControllerStatus.Stopped, sc1.Status, "#C1");
4417                         Assert.AreEqual (ServiceControllerStatus.Stopped, sc2.Status, "#C2");
4418                 }
4419
4420                 [Test]
4421                 public void WaitForStatus_Service_DoesNotExist ()
4422                 {
4423                         if (RunningOnUnix)
4424                                 Assert.Ignore ("Running on Unix.");
4425
4426                         ServiceController sc = new ServiceController ("doesnotexist", ".");
4427                         try {
4428                                 sc.WaitForStatus (ServiceControllerStatus.Stopped);
4429                                 Assert.Fail ("#1");
4430                         } catch (InvalidOperationException ex) {
4431                                 // Cannot open doesnotexist service on computer '.'
4432                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
4433                                 Assert.IsNotNull (ex.Message, "#3");
4434                                 Assert.IsTrue (ex.Message.IndexOf ("doesnotexist") != -1, "#4");
4435                                 Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#5");
4436                                 Assert.IsNotNull (ex.InnerException, "#6");
4437
4438                                 // The filename, directory name, or volume label is incorrect
4439                                 Assert.AreEqual (typeof (Win32Exception), ex.InnerException.GetType (), "#7");
4440                                 Win32Exception win32Error = (Win32Exception) ex.InnerException;
4441                                 //Assert.AreEqual (-2147467259, win32Error.ErrorCode, "#8");
4442                                 Assert.IsNotNull (win32Error.Message, "#9");
4443                                 Assert.AreEqual (1060, win32Error.NativeErrorCode, "#10");
4444                                 Assert.IsNull (win32Error.InnerException, "#11");
4445                         }
4446                 }
4447
4448                 [Test]
4449                 public void WaitForStatus_Service_OperationNotValid ()
4450                 {
4451                         if (RunningOnUnix)
4452                                 Assert.Ignore ("Running on Unix.");
4453
4454                         ServiceController sc1 = new ServiceController (UNCONTROLLABLE_SERVICE.ServiceName, ".");
4455                         ServiceController sc2 = new ServiceController (UNCONTROLLABLE_SERVICE.ServiceName, ".");
4456
4457                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#A1");
4458                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#A2");
4459
4460                         sc1.WaitForStatus (ServiceControllerStatus.Running,
4461                                 new TimeSpan (0, 0, 1));
4462
4463                         try {
4464                                 sc1.WaitForStatus (ServiceControllerStatus.Stopped,
4465                                         new TimeSpan (0, 0, 1));
4466                                 Assert.Fail ("#B1");
4467                         } catch (TimeoutException ex) {
4468                                 // Time out has expired and the operation has not been completed
4469                                 Assert.AreEqual (typeof (TimeoutException), ex.GetType (), "#B2");
4470                                 Assert.IsNotNull (ex.Data, "#B3");
4471                                 Assert.AreEqual (0, ex.Data.Count, "#B4");
4472                                 Assert.IsNotNull (ex.Message, "#B5");
4473                                 Assert.IsNull (ex.InnerException, "#B6");
4474                         }
4475
4476                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#C1");
4477                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#C2");
4478                 }
4479
4480                 [Test]
4481                 public void WaitForStatus_ServiceName_Empty ()
4482                 {
4483                         ServiceController sc = new ServiceController ();
4484                         try {
4485                                 sc.WaitForStatus (ServiceControllerStatus.Stopped,
4486                                         new TimeSpan (0, 0, 1));
4487                                 Assert.Fail ("#1");
4488                         } catch (ArgumentException ex) {
4489                                 // Service name  contains invalid characters, is empty or is
4490                                 // too long (max length = 80)
4491                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4492                                 Assert.IsNotNull (ex.Message, "#3");
4493                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#4");
4494                                 Assert.IsTrue (ex.Message.IndexOf ("80") != -1, "#5");
4495                                 Assert.IsNull (ex.ParamName, "#6");
4496                                 Assert.IsNull (ex.InnerException, "#7");
4497                         }
4498                 }
4499
4500                 [Test]
4501                 public void WaitForStatus_Timeout ()
4502                 {
4503                         if (RunningOnUnix)
4504                                 Assert.Ignore ("Running on Unix.");
4505
4506                         ServiceController sc1 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
4507                         ServiceController sc2 = new ServiceController (CONTROLLABLE_SERVICE.ServiceName, ".");
4508
4509                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#A1");
4510                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#A2");
4511
4512                         try {
4513                                 sc1.WaitForStatus (ServiceControllerStatus.Stopped,
4514                                         new TimeSpan (0, 0, 1));
4515                                 Assert.Fail ("#B1");
4516                         } catch (TimeoutException ex) {
4517                                 // Time out has expired and the operation has not been completed
4518                                 Assert.AreEqual (typeof (TimeoutException), ex.GetType (), "#B2");
4519                                 Assert.IsNotNull (ex.Data, "#B3");
4520                                 Assert.AreEqual (0, ex.Data.Count, "#B4");
4521                                 Assert.IsNotNull (ex.Message, "#B5");
4522                                 Assert.IsNull (ex.InnerException, "#B6");
4523                         }
4524
4525                         Assert.AreEqual (ServiceControllerStatus.Running, sc1.Status, "#C1");
4526                         Assert.AreEqual (ServiceControllerStatus.Running, sc2.Status, "#C2");
4527                 }
4528
4529                 // Run this on .NET to generate ServiceInfo objects for services on the current machine
4530 //              [Test]
4531                 public static void DumpServices ()
4532                 {
4533                         foreach (ServiceController sc in ServiceController.GetServices ()) {
4534                                 try {
4535                                         var si = new ServiceInfo { ServiceName = sc.ServiceName, DisplayName = sc.DisplayName, ServiceType = sc.ServiceType, Dependents = ServiceNames (sc.DependentServices), DependedOn = ServiceNames (sc.ServicesDependedOn) };
4536                                         var l = new List<string> ();
4537                                         l.Add ($"ServiceName = \"{si.ServiceName}\"");
4538                                         l.Add ($"DisplayName = \"{si.DisplayName}\"");
4539                                         if (si.ServiceType != ServiceType.Win32ShareProcess)
4540                                                 l.Add ($"ServiceType = ServiceType.{si.ServiceType}");
4541                                         if (si.Dependents.Length > 0)
4542                                                 l.Add ("Dependents = new [] { \"" + String.Join ("\", \"", si.Dependents) + "\" }");
4543                                         if (si.DependedOn.Length > 0)
4544                                                 l.Add ("DependedOn = new [] { \"" + String.Join ("\", \"", si.DependedOn) + "\" }");
4545                                         Console.WriteLine ("static ServiceInfo " + si.DisplayName.ToUpper ().Replace (' ', '_').Replace ('-', '_') + "_SERVICE = new ServiceInfo { " + String.Join (", ", l) + " };");
4546                                 } catch {
4547                                 }
4548                         }
4549                 }
4550
4551                 private static void EnsureServiceIsRunning (ServiceController sc)
4552                 {
4553                         sc.Refresh ();
4554                         switch (sc.Status) {
4555                         case ServiceControllerStatus.ContinuePending:
4556                                 sc.WaitForStatus (ServiceControllerStatus.Running, new TimeSpan (0, 0, 5));
4557                                 break;
4558                         case ServiceControllerStatus.Paused:
4559                                 sc.Continue ();
4560                                 sc.WaitForStatus (ServiceControllerStatus.Running, new TimeSpan (0, 0, 5));
4561                                 break;
4562                         case ServiceControllerStatus.PausePending:
4563                                 sc.WaitForStatus (ServiceControllerStatus.Paused, new TimeSpan (0, 0, 5));
4564                                 sc.Continue ();
4565                                 sc.WaitForStatus (ServiceControllerStatus.Running, new TimeSpan (0, 0, 5));
4566                                 break;
4567                         case ServiceControllerStatus.StartPending:
4568                                 sc.WaitForStatus (ServiceControllerStatus.Running, new TimeSpan (0, 0, 5));
4569                                 break;
4570                         case ServiceControllerStatus.Stopped:
4571                                 sc.Start ();
4572                                 sc.WaitForStatus (ServiceControllerStatus.Running, new TimeSpan (0, 0, 5));
4573                                 break;
4574                         case ServiceControllerStatus.StopPending:
4575                                 sc.WaitForStatus (ServiceControllerStatus.Stopped, new TimeSpan (0, 0, 5));
4576                                 sc.Start ();
4577                                 sc.WaitForStatus (ServiceControllerStatus.Running, new TimeSpan (0, 0, 5));
4578                                 break;
4579                         }
4580                 }
4581
4582                 private static bool ContainsService (ServiceController [] services, string serviceName)
4583                 {
4584                         for (int i = 0; i < services.Length; i++) {
4585                                 if (services [i].ServiceName == serviceName)
4586                                         return true;
4587                         }
4588                         return false;
4589                 }
4590
4591                 private static string[] ServiceNames (ServiceController [] services)
4592                 {
4593                         var result = new string [services.Length];
4594                         for (var i = 0; i < result.Length; i++)
4595                                 result [i] = services [i].ServiceName.ToLower ();
4596                         Array.Sort<string> (result);
4597                         return result;
4598                 }
4599
4600                 private bool RunningOnUnix
4601                 {
4602                         get {
4603                                 int p = (int) Environment.OSVersion.Platform;
4604                                 return ((p == 4) || (p == 128) || (p == 6));
4605                         }
4606                 }
4607
4608                 private enum SERVICE_CONTROL_TYPE
4609                 {
4610                         SERVICE_CONTROL_STOP = 0x1,
4611                         SERVICE_CONTROL_PAUSE = 0x2,
4612                         SERVICE_CONTROL_CONTINUE = 0x3,
4613                         SERVICE_CONTROL_INTERROGATE = 0x4,
4614                         SERVICE_CONTROL_SHUTDOWN = 0x5,
4615                         SERVICE_CONTROL_PARAMCHANGE = 0x6,
4616                         SERVICE_CONTROL_NETBINDADD = 0x7,
4617                         SERVICE_CONTROL_NETBINDREMOVE = 0x8,
4618                         SERVICE_CONTROL_NETBINDENABLE = 0x9,
4619                         SERVICE_CONTROL_NETBINDDISABLE = 0xA,
4620                         SERVICE_CONTROL_DEVICEEVENT = 0xB,
4621                         SERVICE_CONTROL_HARDWAREPROFILECHANGE = 0xC,
4622                         SERVICE_CONTROL_POWEREVENT = 0xD,
4623                         SERVICE_CONTROL_SESSIONCHANGE = 0xE
4624                 }
4625         }
4626 }