From 58acccd01506d020dc531a5ee4c738e73c3cff70 Mon Sep 17 00:00:00 2001 From: junkmd Date: Sat, 14 Dec 2024 10:12:51 +0900 Subject: [PATCH 1/3] Add frozen local server tests. --- comtypes/test/test_server_register.py | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/comtypes/test/test_server_register.py b/comtypes/test/test_server_register.py index 4c5fbfbb..d30b8860 100644 --- a/comtypes/test/test_server_register.py +++ b/comtypes/test/test_server_register.py @@ -233,3 +233,39 @@ class Cls: (HKCR, rf"{clsid_sub}\Typelib", "", libid), ] self.assertEqual(expected, list(RegistryEntries(Cls))) + + +class Test_RegistryEntries_Frozen(ut.TestCase): + @mock.patch.object(register, "sys") + def test_local_dll(self, _sys): + _sys.mock_add_spec(["executable", "frozen"]) + _sys.executable = sys.executable + _sys.frozen = "dll" + reg_clsid = GUID.create_new() + reg_clsctx = comtypes.CLSCTX_LOCAL_SERVER + + class Cls: + _reg_clsid_ = reg_clsid + _reg_clsctx_ = reg_clsctx + + clsid_sub = rf"CLSID\{reg_clsid}" + expected = [ + (HKCR, clsid_sub, "", ""), + (HKCR, rf"{clsid_sub}\LocalServer32", "", sys.executable), + ] + self.assertEqual(expected, list(RegistryEntries(Cls))) + + @mock.patch.object(register, "sys") + def test_local_frozendllhandle(self, _sys): + _sys.mock_add_spec(["frozen", "frozendllhandle"]) + _sys.frozen = "dll" + _sys.frozendllhandle = 1234 + reg_clsid = GUID.create_new() + reg_clsctx = comtypes.CLSCTX_LOCAL_SERVER + + class Cls: + _reg_clsid_ = reg_clsid + _reg_clsctx_ = reg_clsctx + + expected = [(HKCR, rf"CLSID\{reg_clsid}", "", "")] + self.assertEqual(expected, list(RegistryEntries(Cls))) From 88df5bc51ac038ac319ca0fa37acee646530c93c Mon Sep 17 00:00:00 2001 From: junkmd Date: Sat, 14 Dec 2024 10:12:51 +0900 Subject: [PATCH 2/3] Add frozen inproc server tests. --- comtypes/test/test_server_register.py | 90 +++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/comtypes/test/test_server_register.py b/comtypes/test/test_server_register.py index d30b8860..20cf3173 100644 --- a/comtypes/test/test_server_register.py +++ b/comtypes/test/test_server_register.py @@ -7,6 +7,7 @@ from unittest import mock import comtypes +import comtypes.server.inprocserver from comtypes import GUID from comtypes.server import register from comtypes.server.register import RegistryEntries, _get_serverdll @@ -269,3 +270,92 @@ class Cls: expected = [(HKCR, rf"CLSID\{reg_clsid}", "", "")] self.assertEqual(expected, list(RegistryEntries(Cls))) + + @mock.patch.object(register, "sys") + def test_inproc_windows_exe(self, _sys): + _sys.mock_add_spec(["frozen"]) + _sys.frozen = "windows_exe" + reg_clsid = GUID.create_new() + reg_clsctx = comtypes.CLSCTX_INPROC_SERVER + + class Cls: + _reg_clsid_ = reg_clsid + _reg_clsctx_ = reg_clsctx + + expected = [(HKCR, rf"CLSID\{reg_clsid}", "", "")] + self.assertEqual(expected, list(RegistryEntries(Cls))) + + @mock.patch.object(register, "_get_serverdll", lambda: r"my\target\server.dll") + @mock.patch.object(register, "sys") + def test_inproc_dll_frozendllhandle_clsid_to_class(self, _sys): + _sys.mock_add_spec(["frozen", "frozendllhandle"]) + _sys.frozen = "dll" + _sys.frozendllhandle = 1234 + reg_clsid = GUID.create_new() + reg_clsctx = comtypes.CLSCTX_INPROC_SERVER + + class Cls: + _reg_clsid_ = reg_clsid + _reg_clsctx_ = reg_clsctx + + clsid_sub = rf"CLSID\{reg_clsid}" + inproc_srv_sub = rf"{clsid_sub}\InprocServer32" + expected = [ + (HKCR, clsid_sub, "", ""), + (HKCR, inproc_srv_sub, "", r"my\target\server.dll"), + ] + + with mock.patch.dict(comtypes.server.inprocserver._clsid_to_class): + comtypes.server.inprocserver._clsid_to_class.update({5678: Cls}) + self.assertEqual(expected, list(RegistryEntries(Cls))) + + @mock.patch.object(register, "_get_serverdll", lambda: r"my\target\server.dll") + @mock.patch.object(register, "sys") + def test_inproc_dll(self, _sys): + _sys.mock_add_spec(["frozen", "modules"]) + _sys.frozen = "dll" + _sys.modules = sys.modules + reg_clsid = GUID.create_new() + reg_clsctx = comtypes.CLSCTX_INPROC_SERVER + + class Cls: + _reg_clsid_ = reg_clsid + _reg_clsctx_ = reg_clsctx + + clsid_sub = rf"CLSID\{reg_clsid}" + inproc_srv_sub = rf"{clsid_sub}\InprocServer32" + full_classname = f"{__name__}.Cls" + expected = [ + (HKCR, clsid_sub, "", ""), + (HKCR, inproc_srv_sub, "", r"my\target\server.dll"), + (HKCR, inproc_srv_sub, "PythonClass", full_classname), + (HKCR, inproc_srv_sub, "PythonPath", os.path.dirname(__file__)), + ] + self.assertEqual(expected, list(RegistryEntries(Cls))) + + @mock.patch.object(register, "_get_serverdll", lambda: r"my\target\server.dll") + @mock.patch.object(register, "sys") + def test_inproc_dll_reg_threading(self, _sys): + _sys.mock_add_spec(["frozen", "modules"]) + _sys.frozen = "dll" + _sys.modules = sys.modules + reg_clsid = GUID.create_new() + reg_threading = "Both" + reg_clsctx = comtypes.CLSCTX_INPROC_SERVER + + class Cls: + _reg_clsid_ = reg_clsid + _reg_threading_ = reg_threading + _reg_clsctx_ = reg_clsctx + + clsid_sub = rf"CLSID\{reg_clsid}" + inproc_srv_sub = rf"{clsid_sub}\InprocServer32" + full_classname = f"{__name__}.Cls" + expected = [ + (HKCR, clsid_sub, "", ""), + (HKCR, inproc_srv_sub, "", r"my\target\server.dll"), + (HKCR, inproc_srv_sub, "PythonClass", full_classname), + (HKCR, inproc_srv_sub, "PythonPath", os.path.dirname(__file__)), + (HKCR, inproc_srv_sub, "ThreadingModel", reg_threading), + ] + self.assertEqual(expected, list(RegistryEntries(Cls))) From 203cb562110cf52b8195f19d0a0947d4bfe2d7b0 Mon Sep 17 00:00:00 2001 From: junkmd Date: Sat, 14 Dec 2024 10:14:40 +0900 Subject: [PATCH 3/3] Rename testcases. --- comtypes/test/test_server_register.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/comtypes/test/test_server_register.py b/comtypes/test/test_server_register.py index 20cf3173..d72f87c4 100644 --- a/comtypes/test/test_server_register.py +++ b/comtypes/test/test_server_register.py @@ -33,7 +33,7 @@ def test_frozen(self): self.assertEqual(260, nSize) -class Test_RegistryEntries_NonFrozen(ut.TestCase): +class Test_NonFrozen_RegistryEntries(ut.TestCase): def test_reg_clsid(self): reg_clsid = GUID.create_new() @@ -236,7 +236,7 @@ class Cls: self.assertEqual(expected, list(RegistryEntries(Cls))) -class Test_RegistryEntries_Frozen(ut.TestCase): +class Test_Frozen_RegistryEntries(ut.TestCase): @mock.patch.object(register, "sys") def test_local_dll(self, _sys): _sys.mock_add_spec(["executable", "frozen"])