File: test_helpers.py

package info (click to toggle)
python-pyxs 0.4.2~git20190115.97f14313-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 332 kB
  • sloc: python: 1,196; makefile: 93
file content (72 lines) | stat: -rw-r--r-- 1,902 bytes parent folder | download | duplicates (3)
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
import string

import pytest

from pyxs.exceptions import InvalidPath, InvalidPermission
from pyxs.helpers import check_path, check_watch_path, check_perms


def test_check_path():
    # a) max length is bounded by 3072 for absolute path and 2048 for
    #    relative ones.
    with pytest.raises(InvalidPath):
        check_path(b"/foo/bar" * 3072)

    with pytest.raises(InvalidPath):
        check_path(b"foo/bar" * 2048)

    # b) ASCII alphanumerics and -/_@ only!
    for char in string.punctuation:
        if char in "-/_@":
            continue

        with pytest.raises(InvalidPath):
            check_path(b"/foo" + char.encode())

    # c) no trailing / -- except for root path.
    with pytest.raises(InvalidPath):
        check_path(b"/foo/")

    # d) no //'s!.
    with pytest.raises(InvalidPath):
        check_path(b"/foo//bar")

    # e) OK-case.
    check_path(b"/")


def test_check_watch_path():
    # a) ordinary path should be checked with `check_path()`
    with pytest.raises(InvalidPath):
        check_watch_path(b"/foo/")

    with pytest.raises(InvalidPath):
        check_watch_path(b"/fo\x07o")

    with pytest.raises(InvalidPath):
        check_watch_path(b"/$/foo")

    # b) special path options are limited to `@introduceDomain` and
    #    `@releaseDomain`.
    with pytest.raises(InvalidPath):
        check_watch_path(b"@foo")

    # c) OK-case.
    check_watch_path(b"@introduceDomain")
    check_watch_path(b"@releaseDomain")


def test_check_perms():
    # A valid permission has a form `[wrbn]:digits:`.
    with pytest.raises(InvalidPermission):
        check_perms([b"foo"])

    with pytest.raises(InvalidPermission):
        check_perms([b"f20"])

    with pytest.raises(InvalidPermission):
        check_perms([b"r-20"])

    # OK-case
    check_perms(b"w0 r0 b0 n0".split())
    check_perms([b"w999999"])  # valid, even though it overflows int32.