summaryrefslogtreecommitdiff
path: root/tests/test_viewstate.py
blob: 9cc928538115d3552006e508d8001432b31e502f (plain)
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
import pytest

from os import walk
from os.path import join
from viewstate import *


class TestViewState(object):

    def test_blank(self):
        vs = ViewState()
        assert not vs.is_valid()

    def test_is_valid(self):
        for root, dirs, files in walk('tests/samples'):
            for f in files:
                with open(join(root, f), 'r') as t:
                    vs = ViewState(t.read())
                    assert vs.is_valid() is True

    def test_invalid_base64(self):
        with pytest.raises(ViewStateException):
            vs = ViewState('hello')

    def test_invalid_decode(self):
        with pytest.raises(ViewStateException):
            vs = ViewState(raw=b'\x01\x02')
            vs.decode()

    def test_no_signature(self):
        vs = ViewState(raw=b'\xff\x01e')
        vs.decode()
        assert vs.mac is None
        assert vs.signature is None

    def test_macs(self):
        MAC_LENGTHS = {
            'hmac_sha1': 20,
            'hmac_sha256': 32,
            'unknown': 5  # could be any other value
        }

        for mac, n in MAC_LENGTHS.items():
            sig = b'\x55' * n  # not a real signature, just testing length
            vs = ViewState(raw=b'\xff\x01d' + sig)
            vs.decode()
            assert vs.mac == mac
            assert vs.signature == sig