summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHubert Kario <hkario@redhat.com>2023-02-28 20:22:47 +0100
committerGitHub <noreply@github.com>2023-02-28 20:22:47 +0100
commit31039b4950e1b6be2b41744ce452653ab62c0d10 (patch)
treee83681572a824235cb6a163b26ba59e47fec1a3d
parentcd5ac4fe5e83ac61c7531a99adfa8b025f91acfe (diff)
parent439e45271795b8c2ad4cec94c325258777ba0b41 (diff)
downloadecdsa-31039b4950e1b6be2b41744ce452653ab62c0d10.tar.gz
Merge pull request #315 from tlsfuzzer/codeql
Add CodeQL fixes
-rw-r--r--README.md3
-rw-r--r--docs/source/conf.py2
-rw-r--r--src/ecdsa/_rwlock.py86
-rw-r--r--src/ecdsa/test_eddsa.py4
-rw-r--r--src/ecdsa/test_keys.py4
-rw-r--r--src/ecdsa/test_malformed_sigs.py7
-rw-r--r--src/ecdsa/test_pyecdsa.py104
-rw-r--r--src/ecdsa/test_rw_lock.py180
8 files changed, 66 insertions, 324 deletions
diff --git a/README.md b/README.md
index 7cffeb1..fbb41a6 100644
--- a/README.md
+++ b/README.md
@@ -4,8 +4,7 @@
[![Documentation Status](https://readthedocs.org/projects/ecdsa/badge/?version=latest)](https://ecdsa.readthedocs.io/en/latest/?badge=latest)
[![Coverage Status](https://coveralls.io/repos/github/tlsfuzzer/python-ecdsa/badge.svg?branch=master)](https://coveralls.io/github/tlsfuzzer/python-ecdsa?branch=master)
![condition coverage](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/tomato42/9b6ca1f3410207fbeca785a178781651/raw/python-ecdsa-condition-coverage.json)
-[![Language grade: Python](https://img.shields.io/lgtm/grade/python/g/tlsfuzzer/python-ecdsa.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/tlsfuzzer/python-ecdsa/context:python)
-[![Total alerts](https://img.shields.io/lgtm/alerts/g/tlsfuzzer/python-ecdsa.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/tlsfuzzer/python-ecdsa/alerts/)
+[![CodeQL](https://github.com/tlsfuzzer/python-ecdsa/actions/workflows/codeql.yml/badge.svg)](https://github.com/tlsfuzzer/python-ecdsa/actions/workflows/codeql.yml)
[![Latest Version](https://img.shields.io/pypi/v/ecdsa.svg?style=flat)](https://pypi.python.org/pypi/ecdsa/)
![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg?style=flat)
diff --git a/docs/source/conf.py b/docs/source/conf.py
index 0eaea41..0581c1c 100644
--- a/docs/source/conf.py
+++ b/docs/source/conf.py
@@ -68,5 +68,3 @@ autodoc_default_options = {
"undoc-members": True,
"inherited-members": True,
}
-
-intersphinx_mapping = {"https://docs.python.org/": None}
diff --git a/src/ecdsa/_rwlock.py b/src/ecdsa/_rwlock.py
deleted file mode 100644
index 010e498..0000000
--- a/src/ecdsa/_rwlock.py
+++ /dev/null
@@ -1,86 +0,0 @@
-# Copyright Mateusz Kobos, (c) 2011
-# https://code.activestate.com/recipes/577803-reader-writer-lock-with-priority-for-writers/
-# released under the MIT licence
-
-import threading
-
-
-__author__ = "Mateusz Kobos"
-
-
-class RWLock:
- """
- Read-Write locking primitive
-
- Synchronization object used in a solution of so-called second
- readers-writers problem. In this problem, many readers can simultaneously
- access a share, and a writer has an exclusive access to this share.
- Additionally, the following constraints should be met:
- 1) no reader should be kept waiting if the share is currently opened for
- reading unless a writer is also waiting for the share,
- 2) no writer should be kept waiting for the share longer than absolutely
- necessary.
-
- The implementation is based on [1, secs. 4.2.2, 4.2.6, 4.2.7]
- with a modification -- adding an additional lock (C{self.__readers_queue})
- -- in accordance with [2].
-
- Sources:
- [1] A.B. Downey: "The little book of semaphores", Version 2.1.5, 2008
- [2] P.J. Courtois, F. Heymans, D.L. Parnas:
- "Concurrent Control with 'Readers' and 'Writers'",
- Communications of the ACM, 1971 (via [3])
- [3] http://en.wikipedia.org/wiki/Readers-writers_problem
- """
-
- def __init__(self):
- """
- A lock giving an even higher priority to the writer in certain
- cases (see [2] for a discussion).
- """
- self.__read_switch = _LightSwitch()
- self.__write_switch = _LightSwitch()
- self.__no_readers = threading.Lock()
- self.__no_writers = threading.Lock()
- self.__readers_queue = threading.Lock()
-
- def reader_acquire(self):
- self.__readers_queue.acquire()
- self.__no_readers.acquire()
- self.__read_switch.acquire(self.__no_writers)
- self.__no_readers.release()
- self.__readers_queue.release()
-
- def reader_release(self):
- self.__read_switch.release(self.__no_writers)
-
- def writer_acquire(self):
- self.__write_switch.acquire(self.__no_readers)
- self.__no_writers.acquire()
-
- def writer_release(self):
- self.__no_writers.release()
- self.__write_switch.release(self.__no_readers)
-
-
-class _LightSwitch:
- """An auxiliary "light switch"-like object. The first thread turns on the
- "switch", the last one turns it off (see [1, sec. 4.2.2] for details)."""
-
- def __init__(self):
- self.__counter = 0
- self.__mutex = threading.Lock()
-
- def acquire(self, lock):
- self.__mutex.acquire()
- self.__counter += 1
- if self.__counter == 1:
- lock.acquire()
- self.__mutex.release()
-
- def release(self, lock):
- self.__mutex.acquire()
- self.__counter -= 1
- if self.__counter == 0:
- lock.release()
- self.__mutex.release()
diff --git a/src/ecdsa/test_eddsa.py b/src/ecdsa/test_eddsa.py
index 7a09ad7..a125b94 100644
--- a/src/ecdsa/test_eddsa.py
+++ b/src/ecdsa/test_eddsa.py
@@ -500,6 +500,7 @@ class TestEdDSAEquality(unittest.TestCase):
key2 = PublicKey(generator_ed25519, b"\x01" * 32)
self.assertEqual(key1, key2)
+ # verify that `__ne__` works as expected
self.assertFalse(key1 != key2)
def test_unequal_public_points(self):
@@ -519,6 +520,7 @@ class TestEdDSAEquality(unittest.TestCase):
key2 = PublicKey(generator_ed448, b"\x03" * 56 + b"\x00")
self.assertNotEqual(key1, key2)
+ # verify that `__ne__` works as expected
self.assertTrue(key1 != key2)
def test_equal_private_keys(self):
@@ -526,6 +528,7 @@ class TestEdDSAEquality(unittest.TestCase):
key2 = PrivateKey(generator_ed25519, b"\x01" * 32)
self.assertEqual(key1, key2)
+ # verify that `__ne__` works as expected
self.assertFalse(key1 != key2)
def test_unequal_private_keys(self):
@@ -533,6 +536,7 @@ class TestEdDSAEquality(unittest.TestCase):
key2 = PrivateKey(generator_ed25519, b"\x02" * 32)
self.assertNotEqual(key1, key2)
+ # verify that `__ne__` works as expected
self.assertTrue(key1 != key2)
def test_unequal_privatekey_to_string(self):
diff --git a/src/ecdsa/test_keys.py b/src/ecdsa/test_keys.py
index 25386b1..f9dbcad 100644
--- a/src/ecdsa/test_keys.py
+++ b/src/ecdsa/test_keys.py
@@ -769,8 +769,8 @@ assert isinstance(sig_strings[0], bytes)
verifiers = []
for modifier, fun in [
("bytes", lambda x: x),
- ("bytes memoryview", lambda x: buffer(x)),
- ("bytearray", lambda x: bytearray(x)),
+ ("bytes memoryview", buffer),
+ ("bytearray", bytearray),
("bytearray memoryview", lambda x: buffer(bytearray(x))),
("array.array of bytes", lambda x: array.array("B", x)),
("array.array of bytes memoryview", lambda x: buffer(array.array("B", x))),
diff --git a/src/ecdsa/test_malformed_sigs.py b/src/ecdsa/test_malformed_sigs.py
index 8e1b611..6db0176 100644
--- a/src/ecdsa/test_malformed_sigs.py
+++ b/src/ecdsa/test_malformed_sigs.py
@@ -111,6 +111,7 @@ def st_fuzzed_sig(draw, keys_and_sigs):
note("Remove bytes: {0}".format(to_remove))
# decide which bytes of the original signature should be changed
+ xors = None
if sig: # pragma: no branch
xors = draw(
st.dictionaries(
@@ -288,15 +289,13 @@ def st_der():
| st_der_octet_string(max_size=1024**2)
| st_der_null()
| st_der_oid(),
- lambda children: st.builds(
- lambda x: encode_octet_string(x), st.one_of(children)
- )
+ lambda children: st.builds(encode_octet_string, st.one_of(children))
| st.builds(lambda x: encode_bitstring(x, 0), st.one_of(children))
| st.builds(
lambda x: encode_sequence(*x), st.lists(children, max_size=200)
)
| st.builds(
- lambda tag, x: encode_constructed(tag, x),
+ encode_constructed,
st.integers(min_value=0, max_value=0x3F),
st.one_of(children),
),
diff --git a/src/ecdsa/test_pyecdsa.py b/src/ecdsa/test_pyecdsa.py
index 59c876c..b250677 100644
--- a/src/ecdsa/test_pyecdsa.py
+++ b/src/ecdsa/test_pyecdsa.py
@@ -10,7 +10,6 @@ import shutil
import subprocess
import pytest
from binascii import hexlify, unhexlify
-from hashlib import sha1, sha256, sha384, sha512
import hashlib
from functools import partial
@@ -97,11 +96,16 @@ class ECDSA(unittest.TestCase):
data = b("blahblah")
secexp = int("9d0219792467d7d37b4d43298a7d0c05", 16)
- priv = SigningKey.from_secret_exponent(secexp, SECP256k1, sha256)
+ priv = SigningKey.from_secret_exponent(
+ secexp, SECP256k1, hashlib.sha256
+ )
pub = priv.get_verifying_key()
k = rfc6979.generate_k(
- SECP256k1.generator.order(), secexp, sha256, sha256(data).digest()
+ SECP256k1.generator.order(),
+ secexp,
+ hashlib.sha256,
+ hashlib.sha256(data).digest(),
)
sig1 = priv.sign(data, k=k)
@@ -110,7 +114,7 @@ class ECDSA(unittest.TestCase):
sig2 = priv.sign(data, k=k)
self.assertTrue(pub.verify(sig2, data))
- sig3 = priv.sign_deterministic(data, sha256)
+ sig3 = priv.sign_deterministic(data, hashlib.sha256)
self.assertTrue(pub.verify(sig3, data))
self.assertEqual(sig1, sig2)
@@ -601,27 +605,29 @@ class ECDSA(unittest.TestCase):
sk.sign_digest(b("\xff") * 64)
def test_hashfunc(self):
- sk = SigningKey.generate(curve=NIST256p, hashfunc=sha256)
+ sk = SigningKey.generate(curve=NIST256p, hashfunc=hashlib.sha256)
data = b("security level is 128 bits")
sig = sk.sign(data)
vk = VerifyingKey.from_string(
- sk.get_verifying_key().to_string(), curve=NIST256p, hashfunc=sha256
+ sk.get_verifying_key().to_string(),
+ curve=NIST256p,
+ hashfunc=hashlib.sha256,
)
self.assertTrue(vk.verify(sig, data))
sk2 = SigningKey.generate(curve=NIST256p)
- sig2 = sk2.sign(data, hashfunc=sha256)
+ sig2 = sk2.sign(data, hashfunc=hashlib.sha256)
vk2 = VerifyingKey.from_string(
sk2.get_verifying_key().to_string(),
curve=NIST256p,
- hashfunc=sha256,
+ hashfunc=hashlib.sha256,
)
self.assertTrue(vk2.verify(sig2, data))
vk3 = VerifyingKey.from_string(
sk.get_verifying_key().to_string(), curve=NIST256p
)
- self.assertTrue(vk3.verify(sig, data, hashfunc=sha256))
+ self.assertTrue(vk3.verify(sig, data, hashfunc=hashlib.sha256))
def test_public_key_recovery(self):
# Create keys
@@ -660,7 +666,7 @@ class ECDSA(unittest.TestCase):
# Create keys
curve = BRAINPOOLP160r1
- sk = SigningKey.generate(curve=curve, hashfunc=sha256)
+ sk = SigningKey.generate(curve=curve, hashfunc=hashlib.sha256)
vk = sk.get_verifying_key()
# Sign a message
@@ -669,7 +675,11 @@ class ECDSA(unittest.TestCase):
# Recover verifying keys
recovered_vks = VerifyingKey.from_public_key_recovery(
- signature, data, curve, hashfunc=sha256, allow_truncate=True
+ signature,
+ data,
+ curve,
+ hashfunc=hashlib.sha256,
+ allow_truncate=True,
)
# Test if each pk is valid
@@ -679,7 +689,7 @@ class ECDSA(unittest.TestCase):
# Test if properties are equal
self.assertEqual(vk.curve, recovered_vk.curve)
- self.assertEqual(sha256, recovered_vk.default_hashfunc)
+ self.assertEqual(hashlib.sha256, recovered_vk.default_hashfunc)
# Test if original vk is the list of recovered keys
self.assertIn(
@@ -1368,8 +1378,6 @@ class OpenSSL(unittest.TestCase):
pass
def do_eddsa_test_to_openssl(self, curve):
- curvename = curve.name.upper()
-
if os.path.isdir("t"):
shutil.rmtree("t")
os.mkdir("t")
@@ -1656,8 +1664,8 @@ class RFC6979(unittest.TestCase):
self._do(
generator=SECP256k1.generator,
secexp=int("9d0219792467d7d37b4d43298a7d0c05", 16),
- hsh=sha256(b("sample")).digest(),
- hash_func=sha256,
+ hsh=hashlib.sha256(b("sample")).digest(),
+ hash_func=hashlib.sha256,
expected=int(
"8fa1f95d514760e498f28957b824ee6ec39ed64826ff4fecc2b5739ec45b91cd",
16,
@@ -1671,8 +1679,8 @@ class RFC6979(unittest.TestCase):
"cca9fbcc1b41e5a95d369eaa6ddcff73b61a4efaa279cfc6567e8daa39cbaf50",
16,
),
- hsh=sha256(b("sample")).digest(),
- hash_func=sha256,
+ hsh=hashlib.sha256(b("sample")).digest(),
+ hash_func=hashlib.sha256,
expected=int(
"2df40ca70e639d89528a6b670d9d48d9165fdc0febc0974056bdce192b8e16a3",
16,
@@ -1683,8 +1691,8 @@ class RFC6979(unittest.TestCase):
self._do(
generator=SECP256k1.generator,
secexp=0x1,
- hsh=sha256(b("Satoshi Nakamoto")).digest(),
- hash_func=sha256,
+ hsh=hashlib.sha256(b("Satoshi Nakamoto")).digest(),
+ hash_func=hashlib.sha256,
expected=0x8F8A276C19F4149656B280621E358CCE24F5F52542772691EE69063B74F15D15,
)
@@ -1692,12 +1700,12 @@ class RFC6979(unittest.TestCase):
self._do(
generator=SECP256k1.generator,
secexp=0x1,
- hsh=sha256(
+ hsh=hashlib.sha256(
b(
"All those moments will be lost in time, like tears in rain. Time to die..."
)
).digest(),
- hash_func=sha256,
+ hash_func=hashlib.sha256,
expected=0x38AA22D72376B4DBC472E06C3BA403EE0A394DA63FC58D88686C611ABA98D6B3,
)
@@ -1705,8 +1713,8 @@ class RFC6979(unittest.TestCase):
self._do(
generator=SECP256k1.generator,
secexp=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364140,
- hsh=sha256(b("Satoshi Nakamoto")).digest(),
- hash_func=sha256,
+ hsh=hashlib.sha256(b("Satoshi Nakamoto")).digest(),
+ hash_func=hashlib.sha256,
expected=0x33A19B60E25FB6F4435AF53A3D42D493644827367E6453928554F43E49AA6F90,
)
@@ -1714,8 +1722,8 @@ class RFC6979(unittest.TestCase):
self._do(
generator=SECP256k1.generator,
secexp=0xF8B8AF8CE3C7CCA5E300D33939540C10D45CE001B8F252BFBC57BA0342904181,
- hsh=sha256(b("Alan Turing")).digest(),
- hash_func=sha256,
+ hsh=hashlib.sha256(b("Alan Turing")).digest(),
+ hash_func=hashlib.sha256,
expected=0x525A82B70E67874398067543FD84C83D30C175FDC45FDEEE082FE13B1D7CFDF1,
)
@@ -1734,7 +1742,7 @@ class RFC6979(unittest.TestCase):
"AF2BDBE1AA9B6EC1E2ADE1D694F41FC71A831D0268E9891562113D8A62ADD1BF"
)
),
- hash_func=sha256,
+ hash_func=hashlib.sha256,
expected=int("23AF4074C90A02B3FE61D286D5C87F425E6BDD81B", 16),
)
@@ -1742,8 +1750,8 @@ class RFC6979(unittest.TestCase):
self._do(
generator=NIST192p.generator,
secexp=int("6FAB034934E4C0FC9AE67F5B5659A9D7D1FEFD187EE09FD4", 16),
- hsh=sha1(b("sample")).digest(),
- hash_func=sha1,
+ hsh=hashlib.sha1(b("sample")).digest(),
+ hash_func=hashlib.sha1,
expected=int(
"37D7CA00D2C7B0E5E412AC03BD44BA837FDD5B28CD3B0021", 16
),
@@ -1753,8 +1761,8 @@ class RFC6979(unittest.TestCase):
self._do(
generator=NIST192p.generator,
secexp=int("6FAB034934E4C0FC9AE67F5B5659A9D7D1FEFD187EE09FD4", 16),
- hsh=sha256(b("sample")).digest(),
- hash_func=sha256,
+ hsh=hashlib.sha256(b("sample")).digest(),
+ hash_func=hashlib.sha256,
expected=int(
"32B1B6D7D42A05CB449065727A84804FB1A3E34D8F261496", 16
),
@@ -1764,8 +1772,8 @@ class RFC6979(unittest.TestCase):
self._do(
generator=NIST192p.generator,
secexp=int("6FAB034934E4C0FC9AE67F5B5659A9D7D1FEFD187EE09FD4", 16),
- hsh=sha512(b("sample")).digest(),
- hash_func=sha512,
+ hsh=hashlib.sha512(b("sample")).digest(),
+ hash_func=hashlib.sha512,
expected=int(
"A2AC7AB055E4F20692D49209544C203A7D1F2C0BFBC75DB1", 16
),
@@ -1775,8 +1783,8 @@ class RFC6979(unittest.TestCase):
self._do(
generator=NIST192p.generator,
secexp=int("6FAB034934E4C0FC9AE67F5B5659A9D7D1FEFD187EE09FD4", 16),
- hsh=sha1(b("test")).digest(),
- hash_func=sha1,
+ hsh=hashlib.sha1(b("test")).digest(),
+ hash_func=hashlib.sha1,
expected=int(
"D9CF9C3D3297D3260773A1DA7418DB5537AB8DD93DE7FA25", 16
),
@@ -1786,8 +1794,8 @@ class RFC6979(unittest.TestCase):
self._do(
generator=NIST192p.generator,
secexp=int("6FAB034934E4C0FC9AE67F5B5659A9D7D1FEFD187EE09FD4", 16),
- hsh=sha256(b("test")).digest(),
- hash_func=sha256,
+ hsh=hashlib.sha256(b("test")).digest(),
+ hash_func=hashlib.sha256,
expected=int(
"5C4CE89CF56D9E7C77C8585339B006B97B5F0680B4306C6C", 16
),
@@ -1797,8 +1805,8 @@ class RFC6979(unittest.TestCase):
self._do(
generator=NIST192p.generator,
secexp=int("6FAB034934E4C0FC9AE67F5B5659A9D7D1FEFD187EE09FD4", 16),
- hsh=sha512(b("test")).digest(),
- hash_func=sha512,
+ hsh=hashlib.sha512(b("test")).digest(),
+ hash_func=hashlib.sha512,
expected=int(
"0758753A5254759C7CFBAD2E2D9B0792EEE44136C9480527", 16
),
@@ -1811,8 +1819,8 @@ class RFC6979(unittest.TestCase):
"0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75CAA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83538",
16,
),
- hsh=sha1(b("sample")).digest(),
- hash_func=sha1,
+ hsh=hashlib.sha1(b("sample")).digest(),
+ hash_func=hashlib.sha1,
expected=int(
"089C071B419E1C2820962321787258469511958E80582E95D8378E0C2CCDB3CB42BEDE42F50E3FA3C71F5A76724281D31D9C89F0F91FC1BE4918DB1C03A5838D0F9",
16,
@@ -1826,8 +1834,8 @@ class RFC6979(unittest.TestCase):
"0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75CAA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83538",
16,
),
- hsh=sha256(b("sample")).digest(),
- hash_func=sha256,
+ hsh=hashlib.sha256(b("sample")).digest(),
+ hash_func=hashlib.sha256,
expected=int(
"0EDF38AFCAAECAB4383358B34D67C9F2216C8382AAEA44A3DAD5FDC9C32575761793FEF24EB0FC276DFC4F6E3EC476752F043CF01415387470BCBD8678ED2C7E1A0",
16,
@@ -1841,8 +1849,8 @@ class RFC6979(unittest.TestCase):
"0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75CAA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83538",
16,
),
- hsh=sha512(b("test")).digest(),
- hash_func=sha512,
+ hsh=hashlib.sha512(b("test")).digest(),
+ hash_func=hashlib.sha512,
expected=int(
"16200813020EC986863BEDFC1B121F605C1215645018AEA1A7B215A564DE9EB1B38A67AA1128B80CE391C4FB71187654AAA3431027BFC7F395766CA988C964DC56D",
16,
@@ -2206,7 +2214,7 @@ class RFC7027(ECDH):
"6FC98BD7E50211A4A27102FA3549DF79EBCB4BF246B80945CDDFE7D509BBFD7D",
"9E56F509196784D963D1C0A401510EE7ADA3DCC5DEE04B154BF61AF1D5A6DECE",
b"abc",
- sha256,
+ hashlib.sha256,
"CB28E0999B9C7715FD0A80D8E47A77079716CBBF917DD72E97566EA1C066957C",
"86FA3BB4E26CAD5BF90B7F81899256CE7594BB1EA0C89212748BFF3B3D5B0315",
NIST256p,
@@ -2222,7 +2230,7 @@ class RFC7027(ECDH):
"B4B74E44D71A13D568003D7489908D564C7761E229C58CBFA18950096EB7463B"
"854D7FA992F934D927376285E63414FA",
b"abc",
- sha384,
+ hashlib.sha384,
"FB017B914E29149432D8BAC29A514640B46F53DDAB2C69948084E2930F1C8F7E"
"08E07C9C63F2D21A07DCB56A6AF56EB3",
"B263A1305E057F984D38726A1B46874109F417BCA112674C528262A40A629AF1"
@@ -2244,7 +2252,7 @@ class RFC7027(ECDH):
"373778F9DE6B6497B1EF825FF24F42F9B4A4BD7382CFC3378A540B1B7F0C1B95"
"6C2F",
b"abc",
- sha512,
+ hashlib.sha512,
"0154FD3836AF92D0DCA57DD5341D3053988534FDE8318FC6AAAAB68E2E6F4339"
"B19F2F281A7E0B22C269D93CF8794A9278880ED7DBB8D9362CAEACEE54432055"
"2251",
diff --git a/src/ecdsa/test_rw_lock.py b/src/ecdsa/test_rw_lock.py
deleted file mode 100644
index 0a84b9c..0000000
--- a/src/ecdsa/test_rw_lock.py
+++ /dev/null
@@ -1,180 +0,0 @@
-# Copyright Mateusz Kobos, (c) 2011
-# https://code.activestate.com/recipes/577803-reader-writer-lock-with-priority-for-writers/
-# released under the MIT licence
-
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
-import threading
-import time
-import copy
-from ._rwlock import RWLock
-
-
-class Writer(threading.Thread):
- def __init__(
- self, buffer_, rw_lock, init_sleep_time, sleep_time, to_write
- ):
- """
- @param buffer_: common buffer_ shared by the readers and writers
- @type buffer_: list
- @type rw_lock: L{RWLock}
- @param init_sleep_time: sleep time before doing any action
- @type init_sleep_time: C{float}
- @param sleep_time: sleep time while in critical section
- @type sleep_time: C{float}
- @param to_write: data that will be appended to the buffer
- """
- threading.Thread.__init__(self)
- self.__buffer = buffer_
- self.__rw_lock = rw_lock
- self.__init_sleep_time = init_sleep_time
- self.__sleep_time = sleep_time
- self.__to_write = to_write
- self.entry_time = None
- """Time of entry to the critical section"""
- self.exit_time = None
- """Time of exit from the critical section"""
-
- def run(self):
- time.sleep(self.__init_sleep_time)
- self.__rw_lock.writer_acquire()
- self.entry_time = time.time()
- time.sleep(self.__sleep_time)
- self.__buffer.append(self.__to_write)
- self.exit_time = time.time()
- self.__rw_lock.writer_release()
-
-
-class Reader(threading.Thread):
- def __init__(self, buffer_, rw_lock, init_sleep_time, sleep_time):
- """
- @param buffer_: common buffer shared by the readers and writers
- @type buffer_: list
- @type rw_lock: L{RWLock}
- @param init_sleep_time: sleep time before doing any action
- @type init_sleep_time: C{float}
- @param sleep_time: sleep time while in critical section
- @type sleep_time: C{float}
- """
- threading.Thread.__init__(self)
- self.__buffer = buffer_
- self.__rw_lock = rw_lock
- self.__init_sleep_time = init_sleep_time
- self.__sleep_time = sleep_time
- self.buffer_read = None
- """a copy of a the buffer read while in critical section"""
- self.entry_time = None
- """Time of entry to the critical section"""
- self.exit_time = None
- """Time of exit from the critical section"""
-
- def run(self):
- time.sleep(self.__init_sleep_time)
- self.__rw_lock.reader_acquire()
- self.entry_time = time.time()
- time.sleep(self.__sleep_time)
- self.buffer_read = copy.deepcopy(self.__buffer)
- self.exit_time = time.time()
- self.__rw_lock.reader_release()
-
-
-class RWLockTestCase(unittest.TestCase):
- def test_readers_nonexclusive_access(self):
- (buffer_, rw_lock, threads) = self.__init_variables()
-
- threads.append(Reader(buffer_, rw_lock, 0, 0))
- threads.append(Writer(buffer_, rw_lock, 0.2, 0.4, 1))
- threads.append(Reader(buffer_, rw_lock, 0.3, 0.3))
- threads.append(Reader(buffer_, rw_lock, 0.5, 0))
-
- self.__start_and_join_threads(threads)
-
- ## The third reader should enter after the second one but it should
- ## exit before the second one exits
- ## (i.e. the readers should be in the critical section
- ## at the same time)
-
- self.assertEqual([], threads[0].buffer_read)
- self.assertEqual([1], threads[2].buffer_read)
- self.assertEqual([1], threads[3].buffer_read)
- self.assertTrue(threads[1].exit_time <= threads[2].entry_time)
- self.assertTrue(threads[2].entry_time <= threads[3].entry_time)
- self.assertTrue(threads[3].exit_time < threads[2].exit_time)
-
- def test_writers_exclusive_access(self):
- (buffer_, rw_lock, threads) = self.__init_variables()
-
- threads.append(Writer(buffer_, rw_lock, 0, 0.4, 1))
- threads.append(Writer(buffer_, rw_lock, 0.1, 0, 2))
- threads.append(Reader(buffer_, rw_lock, 0.2, 0))
-
- self.__start_and_join_threads(threads)
-
- ## The second writer should wait for the first one to exit
-
- self.assertEqual([1, 2], threads[2].buffer_read)
- self.assertTrue(threads[0].exit_time <= threads[1].entry_time)
- self.assertTrue(threads[1].exit_time <= threads[2].exit_time)
-
- def test_writer_priority(self):
- (buffer_, rw_lock, threads) = self.__init_variables()
-
- threads.append(Writer(buffer_, rw_lock, 0, 0, 1))
- threads.append(Reader(buffer_, rw_lock, 0.1, 0.4))
- threads.append(Writer(buffer_, rw_lock, 0.2, 0, 2))
- threads.append(Reader(buffer_, rw_lock, 0.3, 0))
- threads.append(Reader(buffer_, rw_lock, 0.3, 0))
-
- self.__start_and_join_threads(threads)
-
- ## The second writer should go before the second and the third reader
-
- self.assertEqual([1], threads[1].buffer_read)
- self.assertEqual([1, 2], threads[3].buffer_read)
- self.assertEqual([1, 2], threads[4].buffer_read)
- self.assertTrue(threads[0].exit_time < threads[1].entry_time)
- self.assertTrue(threads[1].exit_time <= threads[2].entry_time)
- self.assertTrue(threads[2].exit_time <= threads[3].entry_time)
- self.assertTrue(threads[2].exit_time <= threads[4].entry_time)
-
- def test_many_writers_priority(self):
- (buffer_, rw_lock, threads) = self.__init_variables()
-
- threads.append(Writer(buffer_, rw_lock, 0, 0, 1))
- threads.append(Reader(buffer_, rw_lock, 0.1, 0.6))
- threads.append(Writer(buffer_, rw_lock, 0.2, 0.1, 2))
- threads.append(Reader(buffer_, rw_lock, 0.3, 0))
- threads.append(Reader(buffer_, rw_lock, 0.4, 0))
- threads.append(Writer(buffer_, rw_lock, 0.5, 0.1, 3))
-
- self.__start_and_join_threads(threads)
-
- ## The two last writers should go first -- after the first reader and
- ## before the second and the third reader
-
- self.assertEqual([1], threads[1].buffer_read)
- self.assertEqual([1, 2, 3], threads[3].buffer_read)
- self.assertEqual([1, 2, 3], threads[4].buffer_read)
- self.assertTrue(threads[0].exit_time < threads[1].entry_time)
- self.assertTrue(threads[1].exit_time <= threads[2].entry_time)
- self.assertTrue(threads[1].exit_time <= threads[5].entry_time)
- self.assertTrue(threads[2].exit_time <= threads[3].entry_time)
- self.assertTrue(threads[2].exit_time <= threads[4].entry_time)
- self.assertTrue(threads[5].exit_time <= threads[3].entry_time)
- self.assertTrue(threads[5].exit_time <= threads[4].entry_time)
-
- @staticmethod
- def __init_variables():
- buffer_ = []
- rw_lock = RWLock()
- threads = []
- return (buffer_, rw_lock, threads)
-
- @staticmethod
- def __start_and_join_threads(threads):
- for t in threads:
- t.start()
- for t in threads:
- t.join()