Skip to content

Commit 8bd719c

Browse files
Add regression test.
1 parent f6c27bf commit 8bd719c

1 file changed

Lines changed: 185 additions & 0 deletions

File tree

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# test_delete_descriptor_binding.py
2+
#
3+
# Copyright (C) 2026 wolfSSL Inc.
4+
#
5+
# This file is part of wolfSSL. (formerly known as CyaSSL)
6+
#
7+
# wolfSSL is free software; you can redistribute it and/or modify
8+
# it under the terms of the GNU General Public License as published by
9+
# the Free Software Foundation; either version 2 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# wolfSSL is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU General Public License
18+
# along with this program; if not, write to the Free Software
19+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20+
21+
"""
22+
Regression tests guarding against the Python descriptor-binding bug on
23+
``_delete`` / ``_copy`` class attributes.
24+
25+
Historically these were written as bare references to ``_lib`` functions::
26+
27+
class Random:
28+
_delete = _lib.wc_FreeRng
29+
30+
def __del__(self):
31+
self._delete(self.native_object)
32+
33+
If the underlying callable is ever a plain Python function (e.g. a mock,
34+
wrapper, or future CFFI change), the descriptor protocol turns
35+
``self._delete`` into a *bound method*, and ``self._delete(native)`` then
36+
calls ``fn(self, native)`` - passing ``self`` as an extra C argument.
37+
38+
The fix wraps the callable in ``staticmethod(...)`` at the class level so
39+
that attribute lookup never binds ``self``. These tests assert the fix
40+
stays in place and document the Python semantics it relies on.
41+
"""
42+
43+
# pylint: disable=redefined-outer-name
44+
45+
import inspect
46+
47+
import pytest
48+
49+
from wolfcrypt._ffi import lib as _lib
50+
51+
52+
def _static_attrs():
53+
"""Yield (cls, attr_name) pairs that must be staticmethod-wrapped."""
54+
from wolfcrypt.random import Random
55+
yield Random, "_delete"
56+
57+
if _lib.SHA_ENABLED:
58+
from wolfcrypt.hashes import Sha
59+
yield Sha, "_delete"
60+
yield Sha, "_copy"
61+
if _lib.SHA256_ENABLED:
62+
from wolfcrypt.hashes import Sha256
63+
yield Sha256, "_delete"
64+
yield Sha256, "_copy"
65+
if _lib.SHA384_ENABLED:
66+
from wolfcrypt.hashes import Sha384
67+
yield Sha384, "_delete"
68+
yield Sha384, "_copy"
69+
if _lib.SHA512_ENABLED:
70+
from wolfcrypt.hashes import Sha512
71+
yield Sha512, "_delete"
72+
yield Sha512, "_copy"
73+
if _lib.HMAC_ENABLED:
74+
from wolfcrypt.hashes import _Hmac
75+
yield _Hmac, "_delete"
76+
77+
if _lib.AESGCM_STREAM_ENABLED:
78+
from wolfcrypt.ciphers import AesGcmStream
79+
yield AesGcmStream, "_delete"
80+
if _lib.RSA_ENABLED:
81+
from wolfcrypt.ciphers import _Rsa
82+
yield _Rsa, "_delete"
83+
if _lib.ECC_ENABLED:
84+
from wolfcrypt.ciphers import _Ecc
85+
yield _Ecc, "_delete"
86+
if _lib.ED25519_ENABLED:
87+
from wolfcrypt.ciphers import _Ed25519
88+
yield _Ed25519, "_delete"
89+
if _lib.ED448_ENABLED:
90+
from wolfcrypt.ciphers import _Ed448
91+
yield _Ed448, "_delete"
92+
93+
94+
@pytest.mark.parametrize(
95+
"cls,attr",
96+
list(_static_attrs()),
97+
ids=lambda v: v if isinstance(v, str) else v.__name__,
98+
)
99+
def test_lib_fn_class_attr_is_staticmethod(cls, attr):
100+
"""The class attribute must be a ``staticmethod`` so that attribute
101+
access via an instance never triggers descriptor binding.
102+
103+
``inspect.getattr_static`` walks the MRO without invoking descriptors,
104+
so it returns the raw object (the ``staticmethod`` wrapper itself).
105+
"""
106+
raw = inspect.getattr_static(cls, attr)
107+
assert isinstance(raw, staticmethod), (
108+
"%s.%s must be wrapped in staticmethod(...) to prevent Python's "
109+
"descriptor protocol from injecting `self` as an extra positional "
110+
"argument when the underlying callable is a plain Python function "
111+
"(e.g. a test mock). Got %r." % (cls.__name__, attr, type(raw))
112+
)
113+
114+
115+
def test_descriptor_binding_semantics_documentation():
116+
"""Document the exact Python behavior the fix relies on.
117+
118+
Without ``staticmethod``, a Python-function class attribute becomes a
119+
bound method and leaks ``self`` into the call. ``staticmethod`` makes
120+
the descriptor return the underlying callable unchanged.
121+
"""
122+
received = []
123+
124+
def recorder(*args, **kwargs):
125+
received.append((args, kwargs))
126+
127+
class Buggy:
128+
_delete = recorder
129+
130+
def run(self):
131+
self._delete("native")
132+
133+
class Fixed:
134+
_delete = staticmethod(recorder)
135+
136+
def run(self):
137+
self._delete("native")
138+
139+
Buggy().run()
140+
buggy_args, _ = received[-1]
141+
assert len(buggy_args) == 2 and buggy_args[1] == "native", (
142+
"Sanity check failed: plain class-attribute Python function "
143+
"should have been bound and passed self as the first arg."
144+
)
145+
146+
Fixed().run()
147+
fixed_args, _ = received[-1]
148+
assert fixed_args == ("native",), (
149+
"staticmethod-wrapping should prevent self from being bound, "
150+
"so the callable receives only the intended positional argument."
151+
)
152+
153+
154+
def test_random_delete_receives_only_native_object():
155+
"""End-to-end behavioral check on the real ``Random`` class.
156+
157+
We substitute a plain Python recorder in place of the CFFI free
158+
function (wrapped in staticmethod, mirroring how the class itself
159+
stores it) and trigger the code path that calls ``self._delete``.
160+
The recorder must see exactly one positional argument - the
161+
``native_object`` - and never ``self``.
162+
"""
163+
from wolfcrypt.random import Random
164+
165+
received = []
166+
167+
def recorder(*args, **kwargs):
168+
received.append((args, kwargs))
169+
170+
original = inspect.getattr_static(Random, "_delete")
171+
try:
172+
Random._delete = staticmethod(recorder)
173+
r = Random()
174+
native = r.native_object
175+
r.__del__()
176+
r.native_object = None # prevent real cleanup on the way out
177+
assert received, "recorder was never called"
178+
args, kwargs = received[-1]
179+
assert kwargs == {}
180+
assert args == (native,), (
181+
"Random.__del__ must call _delete with only native_object, "
182+
"but got args=%r" % (args,)
183+
)
184+
finally:
185+
Random._delete = original

0 commit comments

Comments
 (0)