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>
28 from src.cnfvar import string as cnfvar_old
35 demo_cnfvar = {"cnf": [
37 "varname": "MY_FAVORITE_CNF_VAR",
40 "data": "string conf content",
44 "varname": "SOME_NESTED_CNF_VAR",
51 "varname": "SOME_CHILD_VAR",
62 # duplicate line number
63 demo_invalid_cnfvar = {"cnf": [
65 "varname": "SOME_PARTICULARLY_TASTY_CNF_VAR",
68 "data": "classic wingers",
72 "varname": "EXTRAORDINARILY_FANCY_CNF_VAR",
75 "data": "ab mentions",
79 "varname": "ANOTHER_POPULAR_CNF_VAR",
89 2 (1) USER_DISABLED,0: "0"
90 3 (1) USER_FULLNAME,0: "Administrator"
91 4 (1) USER_GROUPWARE_FOLDER_CALENDAR,0: "INBOX/Kalender"
92 5 (1) USER_GROUPWARE_FOLDER_CONTACTS,0: "INBOX/Kontakte"
93 6 (1) USER_GROUPWARE_FOLDER_DRAFTS,0: "INBOX/Entwürfe"
94 7 (1) USER_GROUPWARE_FOLDER_NOTES,0: "INBOX/Notizen"
95 8 (1) USER_GROUPWARE_FOLDER_OUTBOX,0: "INBOX/Gesendete Elemente"
96 9 (1) USER_GROUPWARE_FOLDER_TASKS,0: "INBOX/Aufgaben"
97 10 (1) USER_GROUPWARE_FOLDER_TRASH,0: "INBOX/Gelöschte Elemente"
98 11 (1) USER_GROUP_MEMBER_REF,0: "1"
99 12 (1) USER_GROUP_MEMBER_REF,1: "2"
100 13 (1) USER_PASSWORD,0: "test1234"
103 demo_latin1crap = demo_nonascii.encode('latin1')
106 1 GROUP,1: "Administratoren"
107 2 (1) GROUP_ACCESS_GO_ONLINE_ALLOWED,0: "1"
108 3 (1) GROUP_EMAILFILTER_BAN_FILTERLIST_REF,0: "-1"
109 4 (1) GROUP_EMAIL_RELAY_RIGHTS,0: "RELAY_FROM_INTRANET"
110 5 (1) GROUP_PROXY_PROFILE_REF,0: "1"
113 demo_cnf_group_bytes = demo_cnf_group.encode("latin-1")
115 demo_cnf_filter = b"""
116 1 EMAILFILTER_BAN_FILTERLIST,1: "Vordefiniert: Alles verboten"
117 2 (1) EMAILFILTER_BAN_FILTERLIST_ENCRYPTED,0: "BLOCK"
118 3 (1) EMAILFILTER_BAN_FILTERLIST_EXTENSIONS,0: ""
119 4 (1) EMAILFILTER_BAN_FILTERLIST_MIMETYPES,0: ""
120 5 (4) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,0: "text/plain"
121 6 (4) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,1: "text/html"
122 7 (1) EMAILFILTER_BAN_FILTERLIST_MODE,0: "ALLOW"
123 8 (1) EMAILFILTER_BAN_FILTERLIST_PREDEFINED_ID,0: "1"
126 demo_cnf_comments = b"""
127 1 EMAILFILTER_BAN_FILTERLIST,1: "Vordefiniert: Alles verboten"
128 2 (1) EMAILFILTER_BAN_FILTERLIST_ENCRYPTED,0: "BLOCK"
129 3 (1) EMAILFILTER_BAN_FILTERLIST_EXTENSIONS,0: ""
130 4 (1) EMAILFILTER_BAN_FILTERLIST_MIMETYPES,0: "" # foo
131 5 (4) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,0: "text/plain"#bar
132 6 (4) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,1: "text/html"
133 7 (1) EMAILFILTER_BAN_FILTERLIST_MODE,0: "ALLOW" # baz
134 8 (1) EMAILFILTER_BAN_FILTERLIST_PREDEFINED_ID,0: "1"
137 demo_cnf_escaped_quotes = r"""
138 1 HERE_BE_QUOTES,0: "\""
139 2 HERE_BE_QUOTES,1: "foo\"bar\"\"\"baz"
140 3 HERE_BE_QUOTES,2: "unquo\\\"table"
141 4 HERE_BE_QUOTES,3: "unquo\\\\\"\"table"
144 demo_bad_quotes = '1 BAD_VALUE,0: "\""'
146 demo_cnf_quoted_json = r''.join([
147 r'1 AD_MACHINE_CREDENTIALS,0: "{\"Reserved Flags\":\"0\",',
148 r'\"Join Time\":\"20241001071550.162557Z\",\"Computer Name\":\"MIS1\",',
149 r'\"Account Name\":\"MIS1$\",\"Secure Channel Type\":2,\"Trust Flags\":26}"'
157 class CnfVarUnittest(unittest.TestCase):
159 def test_print_cnf(self):
160 with open(os.devnull, "w") as devnull:
161 print(demo_cnfvar, file=devnull)
163 def test_parse_cnf_simple_str(self):
164 cnf = cnfvar_old.read_cnf(demo_cnf_group)
165 with open(os.devnull, "w") as devnull:
166 print(cnf, file=devnull)
168 def test_parse_cnf_simple_bytes(self):
169 cnf = cnfvar_old.read_cnf(demo_cnf_group_bytes)
170 with open(os.devnull, "w") as devnull:
171 print(cnf, file=devnull)
173 def test_parse_cnf_nested(self):
174 cnf = cnfvar_old.read_cnf(demo_cnf_filter)
175 with open(os.devnull, "w") as devnull:
176 print(cnf, file=devnull)
178 def test_parse_cnf_comments(self):
179 cnf = cnfvar_old.read_cnf(demo_cnf_comments)
180 with open(os.devnull, "w") as devnull:
181 print(cnf, file=devnull)
183 @unittest.skip("todo: this does not add anything to `test_print_cnf`")
184 def test_print_cnf_garbage(self):
186 with open(os.devnull, "w") as devnull:
187 print(demo_invalid_cnfvar, file=devnull)
188 except cnfvar_old.InvalidCNF:
189 print ("Caught the duplicate line, bravo!")
191 def test_parse_cnf_quotes(self):
192 cnf = cnfvar_old.read_cnf(demo_cnf_escaped_quotes)
193 self.assertEqual(len(cnf["cnf"]), 4)
195 def test_parse_bad_cnf_quotes(self):
196 self.assertRaises(cnfvar_old.MalformedCNF,
200 def test_parse_quoted_json(self):
201 cnf = cnfvar_old.read_cnf(demo_cnf_quoted_json)
202 dat = json.loads(cnf["cnf"][0]["data"])
203 self.assertIn("Computer Name", dat)
204 self.assertIn("Account Name", dat)
206 def test_read_nonascii(self):
207 cnf = cnfvar_old.read_cnf(demo_nonascii)
208 with open(os.devnull, "w") as devnull:
209 print(cnf, file=devnull)
211 def test_read_latin1(self):
212 cnf = cnfvar_old.read_cnf(demo_latin1crap)
213 with open(os.devnull, "w") as devnull:
214 print(cnf, file=devnull)
217 class CnfVarUnittestVarnameCase(unittest.TestCase):
218 """Tests for verifying that uppercasing/lowercasing of varname works."""
219 # TODO: rethink whether this lower-casing is worth all the effort it causes
221 def test_read_cnf_lowercase(self):
222 """Test that after reading, varnames are lowercase."""
223 cnf = cnfvar_old.read_cnf(demo_cnf_group_bytes)
224 for parentvar in cnf['cnf']:
225 self.assertEqual(parentvar['varname'],
226 parentvar['varname'].lower())
227 if 'children' in parentvar:
228 for childvar in parentvar['children']:
229 self.assertEqual(parentvar['varname'],
230 parentvar['varname'].lower())
233 if __name__ == '__main__':