public void CloseServiceRequiresOpenServiceControlManager(Fake<IAdvApi32> advApi32)
        {
            var sut = new ServiceConnection(advApi32.FakedObject);

            var connectionHandle = new ConnectionHandle();

            Assert.Throws<MissingServiceManagerConnectionException>(() => sut.Close(connectionHandle));
        }
        public void ReleaseWriteLockCallsUnderlyingAPI(Fake<IAdvApi32> advApi32, int serviceControlManagerHandlerValue,
            int serviceDatabaseLockHandleValue)
        {
            var sut = new ServiceDatabaseConnection(advApi32.FakedObject);

            var connectionHandle = new ConnectionHandle
                {
                    ServiceManagerHandle = new IntPtr(serviceControlManagerHandlerValue),
                    ServiceDatabaseLockHandle = new IntPtr(serviceDatabaseLockHandleValue)
                };

            sut.ReleaseLock(connectionHandle);

            advApi32.CallsTo(_ => _.ReleaseServiceDatabaseLock(A<IntPtr>._)).MustHaveHappened();
        }
        public void WriteLock(ConnectionHandle connectionHandle)
        {
            if (connectionHandle == null)
            {
                throw new ArgumentNullException("connectionHandle");
            }

            if (!connectionHandle.IsServiceManagerOpen)
            {
                throw new ServiceConnectionException("Unable to lock service database without an open connection to the service manager");
            }

            var serviceDatabaseLockHandle = advApi32.AquireServiceDatabaseLock(connectionHandle.ServiceManagerHandle);

            connectionHandle.ServiceDatabaseLockHandle = serviceDatabaseLockHandle;
        }
        public void CloseServiceManagerResetsConnectionHandleToIntPtrZero(Fake<IAdvApi32> advApi32, int serviceManagerHandleValue)
        {
            advApi32.CallsTo(_ => _.CloseServiceControlManager(A<IntPtr>._))
                    .Returns(true);

            var sut = new ServiceDatabaseConnection(advApi32.FakedObject);

            var connectionHandle = new ConnectionHandle {ServiceManagerHandle = new IntPtr(serviceManagerHandleValue)};

            sut.Close(connectionHandle);

            var actual = connectionHandle.ServiceManagerHandle;
            var expected = IntPtr.Zero;

            Assert.Equal(expected, actual);
        }
        public void Close(ConnectionHandle connectionHandle)
        {
            if (connectionHandle == null)
            {
                throw new ArgumentNullException("connectionHandle");
            }

            if (!connectionHandle.IsServiceManagerOpen)
            {
                throw new ServiceConnectionException("Unable to close service manager connection when it is not open");
            }

            if (advApi32.CloseServiceControlManager(connectionHandle.ServiceManagerHandle))
            {
                connectionHandle.ServiceManagerHandle = IntPtr.Zero;
            }
        }
        public void CloseServiceCallsUnderlyingApi(Fake<IAdvApi32> advApi32, int serviceControlManagerHandleValue, int serviceHandleValue)
        {
            advApi32.CallsTo(_ => _.CloseService(A<IntPtr>._))
                    .Returns(true);

            var connectionHandle = new ConnectionHandle
                {
                    ServiceManagerHandle = new IntPtr(serviceControlManagerHandleValue),
                    ServiceHandle = new IntPtr(serviceHandleValue)
                };

            var sut = new ServiceConnection(advApi32.FakedObject);

            sut.Close(connectionHandle);

            advApi32.CallsTo(_ => _.CloseService(A<IntPtr>._)).MustHaveHappened();
        }
        public void Open(ConnectionHandle connectionHandle, string serviceName)
        {
            if (connectionHandle == null)
            {
                throw new ArgumentNullException("connectionHandle");
            }

            if (serviceName == null)
            {
                throw new ArgumentNullException("serviceName");
            }

            if (!connectionHandle.IsServiceManagerOpen)
            {
                throw new MissingServiceManagerConnectionException("Unable to open service connection without an open service manager connection");
            }

            var serviceHandle = advApi32.OpenService(connectionHandle.ServiceManagerHandle, serviceName, ScmAccess.ScManagerAllAccess);

            connectionHandle.ServiceHandle = serviceHandle;
        }
        public void ReleaseLock(ConnectionHandle connectionHandle)
        {
            if (connectionHandle == null)
            {
                throw new ArgumentNullException("connectionHandle");
            }

            if (!connectionHandle.IsServiceManagerOpen)
            {
                throw new ServiceConnectionException("Unable to unlock service database without an open connection to the service manager");
            }

            if (!connectionHandle.IsServiceDatabaseLocked)
            {
                throw new ServiceConnectionException("Unable to unlock service database when it is not locked");
            }

            if (advApi32.ReleaseServiceDatabaseLock(connectionHandle.ServiceDatabaseLockHandle))
            {
                connectionHandle.ServiceDatabaseLockHandle = IntPtr.Zero;
            }
        }
        public void Close(ConnectionHandle connectionHandle)
        {
            if (connectionHandle == null)
            {
                throw new ArgumentNullException("connectionHandle");
            }

            if (!connectionHandle.IsServiceManagerOpen)
            {
                throw new MissingServiceManagerConnectionException("Unable to close a service connection without an open service manager connection");
            }

            if (!connectionHandle.IsServiceOpen)
            {
                throw new ArgumentException("Unable to close a service that is not open yet");
            }

            if (advApi32.CloseService(connectionHandle.ServiceHandle))
            {
                connectionHandle.ServiceHandle = IntPtr.Zero;
            }
        }
        public void FailedCloseServiceDoesNotResetConnectionHandle(Fake<IAdvApi32> advApi32, int serviceControlManagerHandleValue,
            int serviceHandleValue)
        {
            advApi32.CallsTo(_ => _.CloseService(A<IntPtr>._))
                    .Returns(false);

            var connectionHandle = new ConnectionHandle
                {
                    ServiceManagerHandle = new IntPtr(serviceControlManagerHandleValue),
                    ServiceHandle = new IntPtr(serviceHandleValue)
                };

            var sut = new ServiceConnection(advApi32.FakedObject);

            sut.Close(connectionHandle);

            var actual = connectionHandle.ServiceHandle;
            var expected = new IntPtr(serviceHandleValue);

            Assert.Equal(expected, actual);
        }
        public void OpenServiceSetsConnectionHandleToNotIntPtrZero(Fake<IAdvApi32> advApi32, int serviceControlManagerHandleValue,
            int serviceHandleValue, string serviceName)
        {
            advApi32.CallsTo(_ => _.OpenService(A<IntPtr>._, A<string>._, A<ScmAccess>._))
                    .Returns(new IntPtr(serviceHandleValue));

            var sut = new ServiceConnection(advApi32.FakedObject);

            var connectionHandle = new ConnectionHandle {ServiceManagerHandle = new IntPtr(serviceControlManagerHandleValue)};

            sut.Open(connectionHandle, serviceName);

            var actual = connectionHandle.ServiceHandle;

            var expected = new IntPtr(serviceHandleValue);

            Assert.Equal(expected, actual);
        }
        public void OpenServiceRequiresServiceName(Fake<IAdvApi32> advApi32, int serviceControlManagerHandleValue)
        {
            var sut = new ServiceConnection(advApi32.FakedObject);

            var connectionHandle = new ConnectionHandle {ServiceManagerHandle = new IntPtr(serviceControlManagerHandleValue)};

            Assert.Throws<ArgumentNullException>(() => sut.Open(connectionHandle, null));
        }
        public void OpenServiceCallsUnderlyingApi(Fake<IAdvApi32> advApi32, int serviceControlManagerHandleValue, int serviceHandleValue,
            string serviceName)
        {
            advApi32.CallsTo(_ => _.OpenService(A<IntPtr>._, A<string>._, A<ScmAccess>._))
                    .Returns(new IntPtr(serviceHandleValue));

            var sut = new ServiceConnection(advApi32.FakedObject);

            var connectionHandle = new ConnectionHandle {ServiceManagerHandle = new IntPtr(serviceControlManagerHandleValue)};

            sut.Open(connectionHandle, serviceName);

            advApi32.CallsTo(_ => _.OpenService(A<IntPtr>._, A<string>._, A<ScmAccess>._)).MustHaveHappened();
        }
        public void ReleaseWriteLockResetsDatabaseLockHandle(Fake<IAdvApi32> advApi32, int serviceControlManagerHandlerValue,
            int serviceDatabaseLockHandleValue)
        {
            advApi32.CallsTo(_ => _.ReleaseServiceDatabaseLock(A<IntPtr>._))
                    .Returns(true);

            var sut = new ServiceDatabaseConnection(advApi32.FakedObject);

            var connectionHandle = new ConnectionHandle
                {
                    ServiceManagerHandle = new IntPtr(serviceControlManagerHandlerValue),
                    ServiceDatabaseLockHandle = new IntPtr(serviceDatabaseLockHandleValue)
                };

            sut.ReleaseLock(connectionHandle);

            var actual = connectionHandle.ServiceDatabaseLockHandle;
            var expected = IntPtr.Zero;

            Assert.Equal(expected, actual);
        }