Increase version to 1.7.3
[pyi2ncommon] / test / cnfvar / test_string.py
CommitLineData
b0075fa2
PD
1#!/usr/bin/env python
2# This Python file uses the following encoding: utf-8
3
4# The software in this package is distributed under the GNU General
5# Public License version 2 (with a special exception described below).
6#
7# A copy of GNU General Public License (GPL) is included in this distribution,
8# in the file COPYING.GPL.
9#
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.
15#
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.
18#
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.
21#
22# Copyright (c) 2016-2018 Intra2net AG <info@intra2net.com>
23
24import os
149c9239 25import json
b0075fa2
PD
26import unittest
27
28from src.cnfvar import string as cnfvar_old
29
30#
31# test data
32#
33
34# model cnf tree
35demo_cnfvar = {"cnf": [
36 {
37 "varname": "MY_FAVORITE_CNF_VAR",
38 "instance": 1337,
39 "number": 42,
40 "data": "string conf content",
41 "comment": ""
42 },
43 {
44 "varname": "SOME_NESTED_CNF_VAR",
45 "instance": 0,
46 "number": 23,
47 "data": "999",
48 "comment": "",
49 "children": [
50 {
51 "varname": "SOME_CHILD_VAR",
52 "instance": 1,
53 "number": 24,
54 "data": "2014",
55 "parent": 23,
56 "comment": ""
57 }
58 ]
59 },
60]}
61
62# duplicate line number
63demo_invalid_cnfvar = {"cnf": [
64 {
65 "varname": "SOME_PARTICULARLY_TASTY_CNF_VAR",
66 "instance": 1337,
67 "number": 23,
68 "data": "classic wingers",
69 "comment": ""
70 },
71 {
72 "varname": "EXTRAORDINARILY_FANCY_CNF_VAR",
73 "instance": 1,
74 "number": 42,
75 "data": "ab mentions",
76 "comment": ""
77 },
78 {
79 "varname": "ANOTHER_POPULAR_CNF_VAR",
80 "instance": 0,
81 "number": 42,
82 "data": "notches",
83 "comment": ""
84 }
85]}
86
87demo_nonascii = r"""
881 USER,1: "admin"
892 (1) USER_DISABLED,0: "0"
903 (1) USER_FULLNAME,0: "Administrator"
914 (1) USER_GROUPWARE_FOLDER_CALENDAR,0: "INBOX/Kalender"
925 (1) USER_GROUPWARE_FOLDER_CONTACTS,0: "INBOX/Kontakte"
936 (1) USER_GROUPWARE_FOLDER_DRAFTS,0: "INBOX/Entwürfe"
947 (1) USER_GROUPWARE_FOLDER_NOTES,0: "INBOX/Notizen"
958 (1) USER_GROUPWARE_FOLDER_OUTBOX,0: "INBOX/Gesendete Elemente"
969 (1) USER_GROUPWARE_FOLDER_TASKS,0: "INBOX/Aufgaben"
9710 (1) USER_GROUPWARE_FOLDER_TRASH,0: "INBOX/Gelöschte Elemente"
9811 (1) USER_GROUP_MEMBER_REF,0: "1"
9912 (1) USER_GROUP_MEMBER_REF,1: "2"
10013 (1) USER_PASSWORD,0: "test1234"
101"""
102
103demo_latin1crap = demo_nonascii.encode('latin1')
104
105demo_cnf_group = """
1061 GROUP,1: "Administratoren"
1072 (1) GROUP_ACCESS_GO_ONLINE_ALLOWED,0: "1"
1083 (1) GROUP_EMAILFILTER_BAN_FILTERLIST_REF,0: "-1"
1094 (1) GROUP_EMAIL_RELAY_RIGHTS,0: "RELAY_FROM_INTRANET"
1105 (1) GROUP_PROXY_PROFILE_REF,0: "1"
111"""
112
113demo_cnf_group_bytes = demo_cnf_group.encode("latin-1")
114
115demo_cnf_filter = b"""
1161 EMAILFILTER_BAN_FILTERLIST,1: "Vordefiniert: Alles verboten"
1172 (1) EMAILFILTER_BAN_FILTERLIST_ENCRYPTED,0: "BLOCK"
1183 (1) EMAILFILTER_BAN_FILTERLIST_EXTENSIONS,0: ""
1194 (1) EMAILFILTER_BAN_FILTERLIST_MIMETYPES,0: ""
1205 (4) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,0: "text/plain"
1216 (4) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,1: "text/html"
1227 (1) EMAILFILTER_BAN_FILTERLIST_MODE,0: "ALLOW"
1238 (1) EMAILFILTER_BAN_FILTERLIST_PREDEFINED_ID,0: "1"
124"""
125
126demo_cnf_comments = b"""
1271 EMAILFILTER_BAN_FILTERLIST,1: "Vordefiniert: Alles verboten"
1282 (1) EMAILFILTER_BAN_FILTERLIST_ENCRYPTED,0: "BLOCK"
1293 (1) EMAILFILTER_BAN_FILTERLIST_EXTENSIONS,0: ""
1304 (1) EMAILFILTER_BAN_FILTERLIST_MIMETYPES,0: "" # foo
1315 (4) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,0: "text/plain"#bar
1326 (4) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,1: "text/html"
1337 (1) EMAILFILTER_BAN_FILTERLIST_MODE,0: "ALLOW" # baz
1348 (1) EMAILFILTER_BAN_FILTERLIST_PREDEFINED_ID,0: "1"
135"""
136
ce076b05 137demo_cnf_escaped_quotes = r"""
b0075fa2
PD
1381 HERE_BE_QUOTES,0: "\""
1392 HERE_BE_QUOTES,1: "foo\"bar\"\"\"baz"
1403 HERE_BE_QUOTES,2: "unquo\\\"table"
1414 HERE_BE_QUOTES,3: "unquo\\\\\"\"table"
142"""
143
149c9239
CH
144demo_bad_quotes = '1 BAD_VALUE,0: "\""'
145
146demo_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}"'
150])
151
152
b0075fa2
PD
153#
154# test class
155#
156
157class CnfVarUnittest(unittest.TestCase):
158
159 def test_print_cnf(self):
160 with open(os.devnull, "w") as devnull:
161 print(demo_cnfvar, file=devnull)
162
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)
167
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)
172
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)
177
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)
182
149c9239 183 @unittest.skip("todo: this does not add anything to `test_print_cnf`")
b0075fa2
PD
184 def test_print_cnf_garbage(self):
185 try:
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!")
190
191 def test_parse_cnf_quotes(self):
192 cnf = cnfvar_old.read_cnf(demo_cnf_escaped_quotes)
149c9239
CH
193 self.assertEqual(len(cnf["cnf"]), 4)
194
195 def test_parse_bad_cnf_quotes(self):
196 self.assertRaises(cnfvar_old.MalformedCNF,
197 cnfvar_old.read_cnf,
198 demo_bad_quotes)
199
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)
b0075fa2
PD
205
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)
210
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)
215
216
217class 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
220
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())
231
232
233if __name__ == '__main__':
234 unittest.main()