Skip to content

Commit da7d20e

Browse files
authored
Merge pull request #274 from StellarCN/patch-v2.2
Patch v2.2.2
2 parents 6a042bc + 561f3e7 commit da7d20e

File tree

4 files changed

+45
-39
lines changed

4 files changed

+45
-39
lines changed

stellar_sdk/memo.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ class HashMemo(Memo):
160160
def __init__(self, memo_hash: Union[bytes, str]) -> None:
161161
memo_hash = hex_to_bytes(memo_hash)
162162
length = len(memo_hash)
163-
if length > 32:
163+
if length != 32:
164164
raise MemoInvalidException(
165-
"HashMemo can contain 32 bytes at max, got {:d} bytes".format(length)
165+
"The length of HashMemo should be 32 bytes, got {:d} bytes.".format(length)
166166
)
167167

168168
self.memo_hash: bytes = memo_hash
@@ -197,9 +197,9 @@ class ReturnHashMemo(Memo):
197197
def __init__(self, memo_return: bytes) -> None:
198198
memo_return = hex_to_bytes(memo_return)
199199
length = len(memo_return)
200-
if length > 32:
200+
if length != 32:
201201
raise MemoInvalidException(
202-
"ReturnHashMemo can contain 32 bytes at max, got {:d} bytes".format(
202+
"The length of ReturnHashMemo should be 32 bytes, got {:d} bytes.".format(
203203
length
204204
)
205205
)

stellar_sdk/time_bounds.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ class TimeBounds:
2424
"""
2525

2626
def __init__(self, min_time: int, max_time: int) -> None:
27-
if 0 < max_time <= min_time:
27+
if min_time < 0:
28+
raise ValueError("min_time cannot be negative.")
29+
30+
if max_time < 0:
31+
raise ValueError("max_time cannot be negative.")
32+
33+
if 0 < max_time < min_time:
2834
raise ValueError("max_time must be >= min_time.")
2935

3036
self.min_time: int = min_time

tests/test_memo.py

+27-31
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,9 @@ def test_id_memo_invalid_raise(self, id):
7676
):
7777
IdMemo(id)
7878

79-
@pytest.mark.parametrize(
80-
"hex, xdr",
81-
[
82-
(
83-
"90d38f2d64949bb64f693ff231f9a3b3",
84-
"AAAAA5DTjy1klJu2T2k/8jH5o7MAAAAAAAAAAAAAAAAAAAAA",
85-
),
86-
(
87-
"573c10b148fc4bc7db97540ce49da22930f4bcd48a060dc7347be84ea9f52d9f",
88-
"AAAAA1c8ELFI/EvH25dUDOSdoikw9LzUigYNxzR76E6p9S2f",
89-
),
90-
],
91-
)
92-
def test_hash_memo(self, hex, xdr):
79+
def test_hash_memo(self):
80+
hex = "573c10b148fc4bc7db97540ce49da22930f4bcd48a060dc7347be84ea9f52d9f"
81+
xdr = "AAAAA1c8ELFI/EvH25dUDOSdoikw9LzUigYNxzR76E6p9S2f"
9382
hash = binascii.unhexlify(hex)
9483
memo = HashMemo(hash)
9584
assert memo.to_xdr_object().to_xdr() == xdr
@@ -102,24 +91,21 @@ def test_hash_memo_too_long_raise(self):
10291
length = 33
10392
with pytest.raises(
10493
MemoInvalidException,
105-
match="HashMemo can contain 32 bytes at max, got {:d} bytes".format(length),
94+
match="The length of HashMemo should be 32 bytes, got {:d} bytes.".format(length),
10695
):
10796
HashMemo(os.urandom(length))
10897

109-
@pytest.mark.parametrize(
110-
"hex, xdr",
111-
[
112-
(
113-
"90d38f2d64949bb64f693ff231f9a3b3",
114-
"AAAABJDTjy1klJu2T2k/8jH5o7MAAAAAAAAAAAAAAAAAAAAA",
115-
),
116-
(
117-
"573c10b148fc4bc7db97540ce49da22930f4bcd48a060dc7347be84ea9f52d9f",
118-
"AAAABFc8ELFI/EvH25dUDOSdoikw9LzUigYNxzR76E6p9S2f",
119-
),
120-
],
121-
)
122-
def test_return_hash_memo(self, hex, xdr):
98+
def test_hash_memo_too_short_raise(self):
99+
length = 16
100+
with pytest.raises(
101+
MemoInvalidException,
102+
match="The length of HashMemo should be 32 bytes, got {:d} bytes.".format(length),
103+
):
104+
HashMemo(os.urandom(length))
105+
106+
def test_return_hash_memo(self):
107+
hex = "573c10b148fc4bc7db97540ce49da22930f4bcd48a060dc7347be84ea9f52d9f"
108+
xdr = "AAAABFc8ELFI/EvH25dUDOSdoikw9LzUigYNxzR76E6p9S2f"
123109
return_hash = binascii.unhexlify(hex)
124110
memo = ReturnHashMemo(return_hash)
125111
assert memo.to_xdr_object().to_xdr() == xdr
@@ -129,10 +115,20 @@ def test_return_hash_memo(self, hex, xdr):
129115
assert base_memo.to_xdr_object().to_xdr() == xdr
130116

131117
def test_return_hash_memo_too_long_raise(self):
132-
length = 33
118+
length = 48
119+
with pytest.raises(
120+
MemoInvalidException,
121+
match="The length of ReturnHashMemo should be 32 bytes, got {:d} bytes.".format(
122+
length
123+
),
124+
):
125+
ReturnHashMemo(os.urandom(length))
126+
127+
def test_return_hash_memo_too_short_raise(self):
128+
length = 16
133129
with pytest.raises(
134130
MemoInvalidException,
135-
match="ReturnHashMemo can contain 32 bytes at max, got {:d} bytes".format(
131+
match="The length of ReturnHashMemo should be 32 bytes, got {:d} bytes.".format(
136132
length
137133
),
138134
):

tests/test_time_bounds.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class TestTimeBounds:
99
[
1010
(1560844454, 1560846000, "AAAAAF0ImKYAAAAAXQiesA=="),
1111
(1560844454, 0, "AAAAAF0ImKYAAAAAAAAAAA=="),
12+
(1583633108, 1583633108, "AAAAAF5kUtQAAAAAXmRS1A=="),
1213
],
1314
)
1415
def test_to_xdr(self, min_time, max_time, xdr):
@@ -20,10 +21,13 @@ def test_to_xdr(self, min_time, max_time, xdr):
2021
assert from_instance.min_time == min_time
2122

2223
@pytest.mark.parametrize(
23-
"min_time, max_time", [(1560844454, 1560844454), (1560844454, 1)]
24+
"min_time, max_time, message", [
25+
(-1, 1560844454, "min_time cannot be negative."),
26+
(1560844454, -1, "max_time cannot be negative."),
27+
(1560844455, 156084454, "max_time must be >= min_time.")]
2428
)
25-
def test_to_xdr_with_invalid_time_raise(self, min_time, max_time):
26-
with pytest.raises(ValueError, match="max_time must be >= min_time."):
29+
def test_to_xdr_with_invalid_time_raise(self, min_time, max_time, message):
30+
with pytest.raises(ValueError, match=message):
2731
TimeBounds(min_time, max_time)
2832

2933
def test_equals(self):

0 commit comments

Comments
 (0)