Fix stuff in service.py + update test

This commit is contained in:
hakril
2018-06-05 11:59:06 +02:00
parent 3d69ad3cb5
commit b223ed233e
4 changed files with 37 additions and 28 deletions
-2
View File
@@ -1,6 +1,4 @@
import pytest
import subprocess
import os.path
import windows
import windows.generated_def as gdef
+21
View File
@@ -0,0 +1,21 @@
import pytest
import windows
import windows.generated_def as gdef
def test_services_process():
services_with_process = [s for s in windows.system.services if s.ServiceStatusProcess.dwProcessId]
service = services_with_process[0]
proc = service.process
assert proc.pid == service.ServiceStatusProcess.dwProcessId
def test_service_appinfo():
appinfos = [x for x in windows.system.services if x.name == "Appinfo"]
assert len(appinfos) == 1
appinfo = appinfos[0]
assert appinfo.status.type & gdef.SERVICE_WIN32_OWN_PROCESS
# Check other fields
assert appinfo.name == "Appinfo"
assert appinfo.description == "Application Information"
-6
View File
@@ -17,12 +17,6 @@ class TestSystemWithCheckGarbage(object):
def test_services(self):
return windows.system.services
def test_services_process(self):
services_with_process = [s for s in windows.system.services if s.ServiceStatusProcess.dwProcessId]
service = services_with_process[0]
proc = service.process
assert proc.pid == service.ServiceStatusProcess.dwProcessId
def test_logicaldrives(self):
return windows.system.logicaldrives
+16 -20
View File
@@ -112,25 +112,21 @@ def scmanagera(access):
windows.winproxy.CloseServiceHandle(scmanager)
def enumerate_services():
# TODO: fix this so we don't have a scmanager leak..
scmanager = windows.winproxy.OpenSCManagerA(dwDesiredAccess=SC_MANAGER_ENUMERATE_SERVICE)
size_needed = DWORD()
nb_services = DWORD()
counter = DWORD()
try:
windows.winproxy.EnumServicesStatusExA(scmanager, SC_ENUM_PROCESS_INFO, SERVICE_TYPE_ALL, SERVICE_STATE_ALL, None, 0, ctypes.byref(size_needed), ctypes.byref(nb_services), byref(counter), None)
except WindowsError:
pass
while True:
size = size_needed.value
buffer = (BYTE * size)()
with scmanagera(SC_MANAGER_ENUMERATE_SERVICE) as scm:
size_needed = DWORD()
nb_services = DWORD()
counter = DWORD()
try:
windows.winproxy.EnumServicesStatusExA(scmanager, SC_ENUM_PROCESS_INFO, SERVICE_TYPE_ALL, SERVICE_STATE_ALL, buffer, size, ctypes.byref(size_needed), ctypes.byref(nb_services), byref(counter), None)
except WindowsError as e:
continue
windows.winproxy.EnumServicesStatusExA(scm, SC_ENUM_PROCESS_INFO, SERVICE_TYPE_ALL, SERVICE_STATE_ALL, None, 0, ctypes.byref(size_needed), ctypes.byref(nb_services), byref(counter), None)
except WindowsError:
pass
return_type = (ServiceA * nb_services.value)
return list(return_type.from_buffer(buffer))
while True:
size = size_needed.value
buffer = (BYTE * size)()
try:
windows.winproxy.EnumServicesStatusExA(scm, SC_ENUM_PROCESS_INFO, SERVICE_TYPE_ALL, SERVICE_STATE_ALL, buffer, size, ctypes.byref(size_needed), ctypes.byref(nb_services), byref(counter), None)
except WindowsError as e:
continue
return_type = (ServiceA * nb_services.value)
return list(return_type.from_buffer(buffer))