1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
import os
import sys
import pytest
from werkzeug.security import check_password_hash
from werkzeug.security import generate_password_hash
from werkzeug.security import safe_join
def test_default_password_method():
value = generate_password_hash("secret")
assert value.startswith("scrypt:")
@pytest.mark.xfail(
sys.implementation.name == "pypy", reason="scrypt unavailable on pypy"
)
def test_scrypt():
value = generate_password_hash("secret", method="scrypt")
assert check_password_hash(value, "secret")
assert value.startswith("scrypt:32768:8:1$")
def test_pbkdf2():
value = generate_password_hash("secret", method="pbkdf2")
assert check_password_hash(value, "secret")
assert value.startswith("pbkdf2:sha256:1000000$")
def test_salted_hashes():
hash1 = generate_password_hash("secret")
hash2 = generate_password_hash("secret")
assert hash1 != hash2
assert check_password_hash(hash1, "secret")
assert check_password_hash(hash2, "secret")
def test_require_salt():
with pytest.raises(ValueError):
generate_password_hash("secret", salt_length=0)
def test_invalid_method():
with pytest.raises(ValueError, match="Invalid hash method"):
generate_password_hash("secret", "sha256")
@pytest.mark.parametrize(
("path", "expect"),
[
("b/c", "a/b/c"),
("../b/c", None),
("b\\c", None if os.name == "nt" else "a/b\\c"),
("//b/c", None),
],
)
def test_safe_join(path, expect):
assert safe_join("a", path) == expect
def test_safe_join_os_sep():
import werkzeug.security as sec
prev_value = sec._os_alt_seps
sec._os_alt_seps = "*"
assert safe_join("foo", "bar/baz*") is None
sec._os_alt_steps = prev_value
def test_safe_join_empty_trusted():
assert safe_join("", "c:test.txt") == "./c:test.txt"
def test_safe_join_windows_special(monkeypatch: pytest.MonkeyPatch) -> None:
"""Windows special device name is not allowed on Windows."""
monkeypatch.setattr("os.name", "nt")
assert safe_join("a", "CON") is None
monkeypatch.setattr("os.name", "posix")
assert safe_join("a", "CON") == "a/CON"
|