2 # This Python file uses the following encoding: utf-8
4 # The software in this package is distributed under the GNU General
5 # Public License version 2 (with a special exception described below).
7 # A copy of GNU General Public License (GPL) is included in this distribution,
8 # in the file COPYING.GPL.
10 # As a special exception, if other files instantiate templates or use macros
11 # or inline functions from this file, or you compile this file and link it
12 # with other works to produce a work based on this file, this file
13 # does not by itself cause the resulting work to be covered
14 # by the GNU General Public License.
16 # However the source code for this file must still be made available
17 # in accordance with section (3) of the GNU General Public License.
19 # This exception does not invalidate any other reasons why a work based
20 # on this file might be covered by the GNU General Public License.
22 # Copyright (c) 2016-2018 Intra2net AG <info@intra2net.com>
27 from src.cnfvar import string as cnfvar_old
34 demo_cnfvar = {"cnf": [
36 "varname": "MY_FAVORITE_CNF_VAR",
39 "data": "string conf content",
43 "varname": "SOME_NESTED_CNF_VAR",
50 "varname": "SOME_CHILD_VAR",
61 # duplicate line number
62 demo_invalid_cnfvar = {"cnf": [
64 "varname": "SOME_PARTICULARLY_TASTY_CNF_VAR",
67 "data": "classic wingers",
71 "varname": "EXTRAORDINARILY_FANCY_CNF_VAR",
74 "data": "ab mentions",
78 "varname": "ANOTHER_POPULAR_CNF_VAR",
88 2 (1) USER_DISABLED,0: "0"
89 3 (1) USER_FULLNAME,0: "Administrator"
90 4 (1) USER_GROUPWARE_FOLDER_CALENDAR,0: "INBOX/Kalender"
91 5 (1) USER_GROUPWARE_FOLDER_CONTACTS,0: "INBOX/Kontakte"
92 6 (1) USER_GROUPWARE_FOLDER_DRAFTS,0: "INBOX/Entwürfe"
93 7 (1) USER_GROUPWARE_FOLDER_NOTES,0: "INBOX/Notizen"
94 8 (1) USER_GROUPWARE_FOLDER_OUTBOX,0: "INBOX/Gesendete Elemente"
95 9 (1) USER_GROUPWARE_FOLDER_TASKS,0: "INBOX/Aufgaben"
96 10 (1) USER_GROUPWARE_FOLDER_TRASH,0: "INBOX/Gelöschte Elemente"
97 11 (1) USER_GROUP_MEMBER_REF,0: "1"
98 12 (1) USER_GROUP_MEMBER_REF,1: "2"
99 13 (1) USER_PASSWORD,0: "test1234"
102 demo_latin1crap = demo_nonascii.encode('latin1')
105 1 GROUP,1: "Administratoren"
106 2 (1) GROUP_ACCESS_GO_ONLINE_ALLOWED,0: "1"
107 3 (1) GROUP_EMAILFILTER_BAN_FILTERLIST_REF,0: "-1"
108 4 (1) GROUP_EMAIL_RELAY_RIGHTS,0: "RELAY_FROM_INTRANET"
109 5 (1) GROUP_PROXY_PROFILE_REF,0: "1"
112 demo_cnf_group_bytes = demo_cnf_group.encode("latin-1")
114 demo_cnf_filter = b"""
115 1 EMAILFILTER_BAN_FILTERLIST,1: "Vordefiniert: Alles verboten"
116 2 (1) EMAILFILTER_BAN_FILTERLIST_ENCRYPTED,0: "BLOCK"
117 3 (1) EMAILFILTER_BAN_FILTERLIST_EXTENSIONS,0: ""
118 4 (1) EMAILFILTER_BAN_FILTERLIST_MIMETYPES,0: ""
119 5 (4) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,0: "text/plain"
120 6 (4) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,1: "text/html"
121 7 (1) EMAILFILTER_BAN_FILTERLIST_MODE,0: "ALLOW"
122 8 (1) EMAILFILTER_BAN_FILTERLIST_PREDEFINED_ID,0: "1"
125 demo_cnf_comments = b"""
126 1 EMAILFILTER_BAN_FILTERLIST,1: "Vordefiniert: Alles verboten"
127 2 (1) EMAILFILTER_BAN_FILTERLIST_ENCRYPTED,0: "BLOCK"
128 3 (1) EMAILFILTER_BAN_FILTERLIST_EXTENSIONS,0: ""
129 4 (1) EMAILFILTER_BAN_FILTERLIST_MIMETYPES,0: "" # foo
130 5 (4) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,0: "text/plain"#bar
131 6 (4) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,1: "text/html"
132 7 (1) EMAILFILTER_BAN_FILTERLIST_MODE,0: "ALLOW" # baz
133 8 (1) EMAILFILTER_BAN_FILTERLIST_PREDEFINED_ID,0: "1"
136 demo_cnf_escaped_quotes = """
137 1 HERE_BE_QUOTES,0: "\""
138 2 HERE_BE_QUOTES,1: "foo\"bar\"\"\"baz"
139 3 HERE_BE_QUOTES,2: "unquo\\\"table"
140 4 HERE_BE_QUOTES,3: "unquo\\\\\"\"table"
147 class CnfVarUnittest(unittest.TestCase):
149 def test_print_cnf(self):
150 with open(os.devnull, "w") as devnull:
151 print(demo_cnfvar, file=devnull)
153 def test_parse_cnf_simple_str(self):
154 cnf = cnfvar_old.read_cnf(demo_cnf_group)
155 with open(os.devnull, "w") as devnull:
156 print(cnf, file=devnull)
158 def test_parse_cnf_simple_bytes(self):
159 cnf = cnfvar_old.read_cnf(demo_cnf_group_bytes)
160 with open(os.devnull, "w") as devnull:
161 print(cnf, file=devnull)
163 def test_parse_cnf_nested(self):
164 cnf = cnfvar_old.read_cnf(demo_cnf_filter)
165 with open(os.devnull, "w") as devnull:
166 print(cnf, file=devnull)
168 def test_parse_cnf_comments(self):
169 cnf = cnfvar_old.read_cnf(demo_cnf_comments)
170 with open(os.devnull, "w") as devnull:
171 print(cnf, file=devnull)
173 def test_print_cnf_garbage(self):
175 with open(os.devnull, "w") as devnull:
176 print(demo_invalid_cnfvar, file=devnull)
177 except cnfvar_old.InvalidCNF:
178 print ("Caught the duplicate line, bravo!")
180 def test_parse_cnf_quotes(self):
181 cnf = cnfvar_old.read_cnf(demo_cnf_escaped_quotes)
182 with open(os.devnull, "w") as devnull:
183 print(demo_invalid_cnfvar, file=devnull)
185 def test_read_nonascii(self):
186 cnf = cnfvar_old.read_cnf(demo_nonascii)
187 with open(os.devnull, "w") as devnull:
188 print(cnf, file=devnull)
190 def test_read_latin1(self):
191 cnf = cnfvar_old.read_cnf(demo_latin1crap)
192 with open(os.devnull, "w") as devnull:
193 print(cnf, file=devnull)
196 class CnfVarUnittestVarnameCase(unittest.TestCase):
197 """Tests for verifying that uppercasing/lowercasing of varname works."""
198 # TODO: rethink whether this lower-casing is worth all the effort it causes
200 def test_read_cnf_lowercase(self):
201 """Test that after reading, varnames are lowercase."""
202 cnf = cnfvar_old.read_cnf(demo_cnf_group_bytes)
203 for parentvar in cnf['cnf']:
204 self.assertEqual(parentvar['varname'],
205 parentvar['varname'].lower())
206 if 'children' in parentvar:
207 for childvar in parentvar['children']:
208 self.assertEqual(parentvar['varname'],
209 parentvar['varname'].lower())
212 if __name__ == '__main__':