Skip to content

Commit

Permalink
test: add basic tests using hypothesis
Browse files Browse the repository at this point in the history
It is segregated to its own file.

I am skipping the entire file using importorskip().

Mixing hypothesis tests with non-hypothesis tests is tricky.

Hypothesis uses decorators before test commands:

    @given(text())
    @settings(max_examples=_max_examples)

Pytest runs the decorators and the arguments as part of scanning the
file for tests. This means the decorator (given, settings ...) and the
strategies inside the decorators (e.g. text()) have to be defined
using a lambda or something. Only aborting at the top of the file
using importorskip prevents having to define all the symbols that
would be imported from hypothesis.
  • Loading branch information
rouilj committed Mar 24, 2024
1 parent 477fc66 commit f34d76d
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions test/test_hypothesis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import unittest

import pytest

pytest.importorskip("hypothesis")

# ruff: noqa: E402
from hypothesis import example, given, settings
from hypothesis.strategies import binary, none, one_of, sampled_from, text

from roundup.anypy.strings import b2s, s2b, s2u, u2s
# ruff: noqa: I001 - yes I know I am using \ to continue the line...
from roundup.password import PasswordValueError, encodePassword, \
h64decode, h64encode


def Identity(x):
return x


_max_examples = 1000


class HypoTestStrings(unittest.TestCase):

@given(text())
@settings(max_examples=_max_examples)
def test_b2s(self, utf8_bytes):
self.assertEqual(b2s(utf8_bytes.encode("utf-8")), utf8_bytes)

@given(text())
@settings(max_examples=_max_examples)
def test_s2b(self, s):
self.assertTrue(isinstance(s2b(s), bytes))

@given(text())
@settings(max_examples=_max_examples)
@example("\U0001F600 hi there") # smiley face emoji
def test_s2u_u2s_invertable(self, s):
self.assertEqual(u2s(s2u(s)), s)


class HypoTestPassword(unittest.TestCase):

@given(binary())
@example(b"")
@settings(max_examples=_max_examples)
def test_h64encode_h64decode(self, s):

self.assertEqual(h64decode(h64encode(s)), s)

@given(one_of(none(), text()),
sampled_from(("PBKDF2S5", "PBKDF2", "SSHA",
"SHA", "MD5", "crypt", "plaintext",
"zot")))
@example("asd\x00df", "crypt")
@settings(max_examples=100 * _max_examples)
def test_encodePassword(self, password, scheme):

if scheme == "crypt" and password and "\x00" in password:
with self.assertRaises(ValueError) as e:
encodePassword(password, scheme)
self.assertEqual(e.exception.args[0],
"embedded null character")
elif scheme == "plaintext":
if password is not None:
self.assertEqual(encodePassword(password, scheme), password)
else:
self.assertEqual(encodePassword(password, scheme), "")
elif scheme == "zot":
with self.assertRaises(PasswordValueError) as e:
encodePassword(password, scheme)
self.assertEqual(e.exception.args[0],
"Unknown encryption scheme 'zot'")
else:
# it shouldn't throw anything.
pw = encodePassword(password, scheme)

# verify format
if scheme in ["PBKDF2S5", "PBKDF2"]:
# 1000$XbSsijELEQbZZb1LlD7CFuotF/8$DdtssSlm.e
self.assertRegex(pw, r"^\d{4,8}\$.{27}\$.*")
elif scheme == "SSHA":
# vqDbjvs8rhrS1AJxHYEGGXQW3x7STAPgo7uCtnw4GYgU7FN5VYbZxccQYCC0eXOxSipLbtgBudH1vDRMNlG0uw==
self.assertRegex(pw, r"^[^=]*={0,3}$")
elif scheme == "SHA":
# da39a3ee5e6b4b0d3255bfef95601890afd80709'
self.assertRegex(pw, r"^[a-z0-9]{40}$")
elif scheme == "MD5":
# d41d8cd98f00b204e9800998ecf8427e'
self.assertRegex(pw, r"^[a-z0-9]{32}$")
elif scheme == "crypt":
# WqzFDzhi8MmoU
self.assertRegex(pw, r"^[A-z0-9./]{13}$")
else:
self.assertFalse("Unknown scheme: %s, val: %s" % (scheme, pw))

0 comments on commit f34d76d

Please sign in to comment.