| 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 | |
| 24 | import os |
| 25 | import json |
| 26 | import unittest |
| 27 | |
| 28 | from src.cnfvar import string as cnfvar_old |
| 29 | |
| 30 | # |
| 31 | # test data |
| 32 | # |
| 33 | |
| 34 | # model cnf tree |
| 35 | demo_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 |
| 63 | demo_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 | |
| 87 | demo_nonascii = r""" |
| 88 | 1 USER,1: "admin" |
| 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" |
| 101 | """ |
| 102 | |
| 103 | demo_latin1crap = demo_nonascii.encode('latin1') |
| 104 | |
| 105 | demo_cnf_group = """ |
| 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" |
| 111 | """ |
| 112 | |
| 113 | demo_cnf_group_bytes = demo_cnf_group.encode("latin-1") |
| 114 | |
| 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" |
| 124 | """ |
| 125 | |
| 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" |
| 135 | """ |
| 136 | |
| 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" |
| 142 | """ |
| 143 | |
| 144 | demo_bad_quotes = '1 BAD_VALUE,0: "\""' |
| 145 | |
| 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}"' |
| 150 | ]) |
| 151 | |
| 152 | |
| 153 | # |
| 154 | # test class |
| 155 | # |
| 156 | |
| 157 | class 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 | |
| 183 | @unittest.skip("todo: this does not add anything to `test_print_cnf`") |
| 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) |
| 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) |
| 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 | |
| 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 |
| 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 | |
| 233 | if __name__ == '__main__': |
| 234 | unittest.main() |