aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_wlp.py
blob: 430c235e042944bff70916b83f76954bd6adf686 (plain) (blame)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/python
# -*- coding: utf-8 -*-
import unittest
import wlp_parser

from rply import Token
import whitelist
import wlp


# logging.basicConfig(level=logging.DEBUG)

EXPECTED_WL = {
    'alfarano@students.cs.unibo.it': {
        'From:': 'Cosimo Alfarano',
        'X-Firstname:': 'Cosimo'
    },
    'kame@innocent.com': {
        'From:': 'kame@inwind.it',
        'Reply-to': 'me',
        'Reply-to:': 'KA',
        'Sender:': 'Kalfa'
    }
}


class TestWLP(unittest.TestCase):
    maxDiff = None
    test_input = '''<kame@innocent.com> {
                From: = 'ME' Sender: = "Cosimo" Reply-to = "me"
            }

            <alfarano@students.cs.unibo.it> {
                From: = 'Cosimo Alfarano'
                X-Firstname: = 'Cosimo'
            }

            <kame@innocent.com> {
                From: = 'kame@inwind.it'
                Reply-to: = "KA"
                Sender: = "Kalfa"
            }'''

    def test_wlp_lexer(self):
        expected_stream = [
            Token('OWNER', '<kame@innocent.com>'),
            Token('VAR', 'From:'),
            Token('VAL', "'ME'"),
            Token('VAR', 'Sender:'),
            Token('VAL', '"Cosimo"'),
            Token('VAR', 'Reply-to'),
            Token('VAL', '"me"'),
            Token('OWNER', '<alfarano@students.cs.unibo.it>'),
            Token('VAR', 'From:'),
            Token('VAL', "'Cosimo Alfarano'"),
            Token('VAR', 'X-Firstname:'),
            Token('VAL', "'Cosimo'"),
            Token('OWNER', '<kame@innocent.com>'),
            Token('VAR', 'From:'),
            Token('VAL', "'kame@inwind.it'"),
            Token('VAR', 'Reply-to:'),
            Token('VAL', '"KA"'),
            Token('VAR', 'Sender:'),
            Token('VAL', '"Kalfa"')
        ]
        tokens = list(wlp_parser.lexer.lex(self.test_input))
        self.assertEqual(tokens, expected_stream)

    def test_wlp_parser(self):
        expected_tree = [
            [
                Token('OWNER', '<kame@innocent.com>'),
                [
                    [Token('VAR', 'From:'), Token('VAL', "'ME'")],
                    [Token('VAR', 'Sender:'), Token('VAL', '"Cosimo"')],
                    [Token('VAR', 'Reply-to'), Token('VAL', '"me"')]
                ]
            ],
            [
                Token('OWNER', '<alfarano@students.cs.unibo.it>'),
                [
                    [Token('VAR', 'From:'), Token('VAL', "'Cosimo Alfarano'")],
                    [Token('VAR', 'X-Firstname:'), Token('VAL', "'Cosimo'")]
                ]
            ],
            [
                Token('OWNER', '<kame@innocent.com>'),
                [
                    [Token('VAR', 'From:'), Token('VAL', "'kame@inwind.it'")],
                    [Token('VAR', 'Reply-to:'), Token('VAL', '"KA"')],
                    [Token('VAR', 'Sender:'), Token('VAL', '"Kalfa"')]
                ]
            ]
        ]
        lex_stream = wlp_parser.lexer.lex(self.test_input)
        tree = wlp_parser.parser.parse(lex_stream)
        self.assertEqual(tree, expected_tree)

    def test_wlp(self):
        wlp.setfilebyname('examples/whitelist.example')
        wl_dict = wlp.mkdict()
        self.assertEqual(wl_dict, EXPECTED_WL)


class TestWhitelist(unittest.TestCase):
    def setUp(self):
        self.wl = whitelist.whitelist('examples/whitelist.example')

    def test_init(self):
        self.assertEqual(self.wl.wl, EXPECTED_WL)

    def test_checkfrom(self):
        self.assertEqual(self.wl.checkfrom('kame@inwind.it'),
                         'kame@innocent.com')

if __name__ == "__main__":
    unittest.main()