Exemple #1
0
        public void EnumDependentServices()
        {
            using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE | SCM_ACCESS.SC_MANAGER_ENUMERATE_SERVICE))
            {
                // Just pick the first service to be dependency
                var dependentServiceNames = scm.EnumServicesStatus(SERVICE_TYPE.SERVICE_WIN32, SERVICE_STATE_FLAGS.SERVICE_STATE_ALL).Select(ss => ss.ServiceName).Take(1).ToList();

                var path = typeof(DummyService).Assembly.Location;

                using (scm.CreateService(
                           DummyService.DisplayName,
                           DummyService.DisplayName,
                           SERVICE_ACCESS.SERVICE_ALL_ACCESS,
                           SERVICE_TYPE.SERVICE_WIN32_OWN_PROCESS,
                           SERVICE_START_TYPE.SERVICE_AUTO_START,
                           SERVICE_ERROR_CONTROL.SERVICE_ERROR_NORMAL,
                           path,
                           "",
                           IntPtr.Zero,
                           dependentServiceNames,
                           null,
                           null))
                {
                }

                using (var service = scm.OpenService(dependentServiceNames.First(), SERVICE_ACCESS.SERVICE_ENUMERATE_DEPENDENTS))
                {
                    var serviceName = service.EnumDependentServices(SERVICE_STATE_FLAGS.SERVICE_STATE_ALL).Select(ss => ss.ServiceName).First();

                    Assert.That(serviceName, Is.EqualTo(DummyService.DisplayName));
                }
            }
        }
Exemple #2
0
 public void OpenControlServiceManager()
 {
     // Just checking for a lack of exceptions at this stage
     using (ServiceControlManager.OpenServiceControlManager(null, 0))
     {
     }
 }
Exemple #3
0
        public void CreateServiceFailure()
        {
            // Create should CreateServiceFailure() WithOperator insufficient permissions
            var scm = ServiceControlManager.OpenServiceControlManager(null, 0);

            Assert.That(() => CreateDummyService(scm), Throws.TypeOf <Win32Exception>().With.Property("NativeErrorCode").EqualTo(ERROR_ACCESS_DENIED));
        }
Exemple #4
0
 public void DeleteService()
 {
     using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE))
         using (var service = ServiceControlManagerTests.CreateDummyService(scm))
         {
             service.Delete();
         }
 }
Exemple #5
0
 public void CreateService()
 {
     using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE))
         using (CreateDummyService(scm))
         {
             // Service is cleaned up in TearDown
         }
 }
Exemple #6
0
 public void EnumServicesStatus()
 {
     using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE | SCM_ACCESS.SC_MANAGER_ENUMERATE_SERVICE))
         using (CreateDummyService(scm))
         {
             var services = scm.EnumServicesStatus(SERVICE_TYPE.SERVICE_WIN32, SERVICE_STATE_FLAGS.SERVICE_STATE_ALL);
             Assert.That(services.Count(s => s.ServiceName == DummyService.SvcName), Is.EqualTo(1));
         }
 }
Exemple #7
0
 public void EnumServicesStatusEx()
 {
     using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE | SCM_ACCESS.SC_MANAGER_ENUMERATE_SERVICE))
         using (CreateDummyService(scm))
         {
             var service = scm.EnumServicesStatusEx(SERVICE_TYPE.SERVICE_WIN32, SERVICE_STATE_FLAGS.SERVICE_STATE_ALL).First(s => s.ServiceName == DummyService.SvcName);
             Assert.That(service.ServiceStatusProcess.currentState, Is.EqualTo(SERVICE_STATE.SERVICE_STOPPED));
         }
 }
Exemple #8
0
 public void GetServiceDisplayName()
 {
     using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE))
     {
         using (CreateDummyService(scm))
         {
         }
         Assert.That(scm.GetServiceDisplayName(DummyService.SvcName), Is.EqualTo(DummyService.DisplayName));
     }
 }
Exemple #9
0
        public void OpenControlServiceManagerFailure()
        {
            // ReSharper disable once InconsistentNaming
            const int RPC_S_SERVER_UNAVAILABLE = 1722;

            // Opening the service control manager on a server with an incorrect name (with spaces) throws quickly
            Assert.That(() => ServiceControlManager.OpenServiceControlManager("aa aa", 0),
                        Throws.TypeOf <Win32Exception>()
                        .With.Property("NativeErrorCode").EqualTo(RPC_S_SERVER_UNAVAILABLE));
        }
Exemple #10
0
 public void EnumServicesStatusFailure()
 {
     using (var scm = ServiceControlManager.OpenServiceControlManager(null, 0))
     {
         // ReSharper disable once AccessToDisposedClosure
         Assert.That(() => scm.EnumServicesStatus(SERVICE_TYPE.SERVICE_WIN32, SERVICE_STATE_FLAGS.SERVICE_STATE_ALL).ToList(),
                     Throws.TypeOf <Win32Exception>()
                     .With.Property("NativeErrorCode").EqualTo(ERROR_ACCESS_DENIED));
     }
 }
Exemple #11
0
        public void StartWithParameters()
        {
            using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE))
                using (var service = ServiceControlManagerTests.CreateDummyService(scm))
                {
                    service.Start(new[] { "Dummy Parameter" });
                    service.WaitForServiceToStart();

                    service.StopServiceAndWait();
                }
        }
Exemple #12
0
        public void QueryConfig()
        {
            using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE))
                using (var service = ServiceControlManagerTests.CreateDummyService(scm))
                {
                    var config = service.QueryConfig();

                    Assert.That(config.DisplayName, Is.EqualTo(DummyService.DisplayName));

                    // Service is cleaned up in TearDown
                }
        }
Exemple #13
0
        public static void DeleteDummyServiceIfItExists()
        {
            using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_ENUMERATE_SERVICE))
            {
                var services = scm.EnumServicesStatus(SERVICE_TYPE.SERVICE_WIN32, SERVICE_STATE_FLAGS.SERVICE_STATE_ALL);

                foreach (var serviceName in services.Select(serviceStatus => serviceStatus.ServiceName).Where(name => name.StartsWith(DummyService.DisplayName) || name.StartsWith(DummyService.SvcName)))
                {
                    DeleteService(scm, serviceName);
                }
            }
        }
Exemple #14
0
 public void OpenService()
 {
     // Again just checking for a lack of exceptions at this stage
     using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CONNECT | SCM_ACCESS.SC_MANAGER_ENUMERATE_SERVICE))
     {
         var serviceName = scm.EnumServicesStatus(SERVICE_TYPE.SERVICE_WIN32, SERVICE_STATE_FLAGS.SERVICE_STATE_ALL).Select(ss => ss.ServiceName).First();
         using (scm.OpenService(serviceName, SERVICE_ACCESS.SERVICE_QUERY_STATUS))
         {
             // Service is cleaned up in TearDown
         }
     }
 }
Exemple #15
0
        public void ChangeConfig2()
        {
            using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE | SCM_ACCESS.SC_MANAGER_ENUMERATE_SERVICE))
                using (var service = ServiceControlManagerTests.CreateDummyService(scm))
                {
                    var description = new SERVICE_DESCRIPTION
                    {
                        Description = "A dummy service"
                    };

                    service.ChangeConfig2(ref description);
                    Assert.That(service.QueryConfig2 <SERVICE_DESCRIPTION>().Description, Is.EqualTo("A dummy service"));
                }
        }
Exemple #16
0
        public void QueryStatusEx()
        {
            using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE))
                using (var service = ServiceControlManagerTests.CreateDummyService(scm))
                {
                    var status = service.QueryStatusEx();
                    Assert.That(status.currentState, Is.EqualTo(SERVICE_STATE.SERVICE_STOPPED));

                    service.Start();
                    service.WaitForServiceToStart();
                    Assert.That(service.QueryStatusEx().currentState, Is.EqualTo(SERVICE_STATE.SERVICE_RUNNING));

                    service.StopServiceAndWait();
                }
        }
Exemple #17
0
        public void OpenServiceFailure()
        {
            // Check we get an exception if we try and open a service that doesn't exist

            // ReSharper disable once InconsistentNaming
            const int ERROR_SERVICE_DOES_NOT_EXIST = 1060;

            using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CONNECT))
            {
                // ReSharper disable once AccessToDisposedClosure
                Assert.That(() => scm.OpenService("Non existant service name", SERVICE_ACCESS.SERVICE_QUERY_STATUS),
                            Throws.TypeOf <Win32Exception>()
                            .With.Property("NativeErrorCode").EqualTo(ERROR_SERVICE_DOES_NOT_EXIST));
            }
        }
Exemple #18
0
        public void ChangeConfigDependentServices()
        {
            using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE | SCM_ACCESS.SC_MANAGER_ENUMERATE_SERVICE))
                using (var service = ServiceControlManagerTests.CreateDummyService(scm))
                {
                    // Just pick the first service to be dependency
                    var dependentServiceNames = scm.EnumServicesStatus(SERVICE_TYPE.SERVICE_WIN32, SERVICE_STATE_FLAGS.SERVICE_STATE_ALL).Select(ss => ss.ServiceName).Take(1).ToList();
                    service.ChangeConfig(SERVICE_TYPE.SERVICE_NO_CHANGE, SERVICE_START_TYPE.SERVICE_NO_CHANGE, SERVICE_ERROR_CONTROL.SERVICE_NO_CHANGE, null, null, IntPtr.Zero, dependentServiceNames, null, null, null);

                    using (var dependentService = scm.OpenService(dependentServiceNames.First(), SERVICE_ACCESS.SERVICE_ENUMERATE_DEPENDENTS))
                    {
                        var serviceName = dependentService.EnumDependentServices(SERVICE_STATE_FLAGS.SERVICE_STATE_ALL).Select(ss => ss.ServiceName).First();

                        Assert.That(serviceName, Is.EqualTo(DummyService.SvcName));
                    }
                }
        }
Exemple #19
0
        public void Control()
        {
            using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE))
                using (var service = ServiceControlManagerTests.CreateDummyService(scm))
                {
                    service.Start();
                    service.WaitForServiceToStart();

                    service.Control(SERVICE_CONTROL.SERVICE_CONTROL_PAUSE);
                    service.WaitForServiceStatus(SERVICE_STATE.SERVICE_PAUSED);

                    service.Control(SERVICE_CONTROL.SERVICE_CONTROL_CONTINUE);
                    service.WaitForServiceStatus(SERVICE_STATE.SERVICE_RUNNING);


                    service.StopServiceAndWait();
                }
        }
Exemple #20
0
        public void ControlEx()
        {
            using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE))
                using (var service = ServiceControlManagerTests.CreateDummyService(scm))
                {
                    service.Start();
                    service.WaitForServiceToStart();

                    var parameters = new SERVICE_CONTROL_STATUS_REASON_PARAMS();
                    service.ControlEx(SERVICE_CONTROL.SERVICE_CONTROL_PAUSE, ref parameters);
                    Assert.That(parameters.serviceStatus.currentState, Is.Not.EqualTo(SERVICE_STATE.SERVICE_RUNNING));
                    service.WaitForServiceStatus(SERVICE_STATE.SERVICE_PAUSED);

                    service.ControlEx(SERVICE_CONTROL.SERVICE_CONTROL_CONTINUE, ref parameters);
                    service.WaitForServiceStatus(SERVICE_STATE.SERVICE_RUNNING);


                    service.StopServiceAndWait();
                }
        }
Exemple #21
0
        public void ChangeConfig()
        {
            using (var scm = ServiceControlManager.OpenServiceControlManager(null, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE))
                using (var service = ServiceControlManagerTests.CreateDummyService(scm))
                {
                    // No changes should not throw
                    service.ChangeConfig(
                        SERVICE_TYPE.SERVICE_NO_CHANGE,
                        SERVICE_START_TYPE.SERVICE_NO_CHANGE,
                        SERVICE_ERROR_CONTROL.SERVICE_NO_CHANGE,
                        null,
                        null,
                        IntPtr.Zero,
                        null,
                        null,
                        null,
                        null);

                    // Set service type to share process
                    service.ChangeConfig(
                        SERVICE_TYPE.SERVICE_WIN32_SHARE_PROCESS,
                        SERVICE_START_TYPE.SERVICE_NO_CHANGE,
                        SERVICE_ERROR_CONTROL.SERVICE_NO_CHANGE,
                        null,
                        null,
                        IntPtr.Zero,
                        null,
                        null,
                        null,
                        null);
                    Assert.That(service.QueryConfig().ServiceType, Is.EqualTo((uint)SERVICE_TYPE.SERVICE_WIN32_SHARE_PROCESS));

                    // Set start type to disabled
                    service.ChangeConfig(
                        SERVICE_TYPE.SERVICE_NO_CHANGE,
                        SERVICE_START_TYPE.SERVICE_DISABLED,
                        SERVICE_ERROR_CONTROL.SERVICE_NO_CHANGE,
                        null,
                        null,
                        IntPtr.Zero,
                        null,
                        null,
                        null,
                        null);
                    Assert.That(service.QueryConfig().StartType, Is.EqualTo((uint)SERVICE_START_TYPE.SERVICE_DISABLED));

                    // Set error control to critical
                    service.ChangeConfig(
                        SERVICE_TYPE.SERVICE_NO_CHANGE,
                        SERVICE_START_TYPE.SERVICE_NO_CHANGE,
                        SERVICE_ERROR_CONTROL.SERVICE_ERROR_CRITICAL,
                        null,
                        null,
                        IntPtr.Zero,
                        null,
                        null,
                        null,
                        null);
                    Assert.That(service.QueryConfig().ErrorControl,
                                Is.EqualTo((uint)SERVICE_ERROR_CONTROL.SERVICE_ERROR_CRITICAL));

                    service.ChangeConfig(
                        SERVICE_TYPE.SERVICE_NO_CHANGE,
                        SERVICE_START_TYPE.SERVICE_NO_CHANGE,
                        SERVICE_ERROR_CONTROL.SERVICE_NO_CHANGE,
                        null,
                        null,
                        IntPtr.Zero,
                        null,
                        null,
                        null,
                        "New Display Name");
                    Assert.That(service.QueryConfig().DisplayName, Is.EqualTo("New Display Name"));
                }
        }