#!/usr/bin/env python # This Python file uses the following encoding: utf-8 # The software in this package is distributed under the GNU General # Public License version 2 (with a special exception described below). # # A copy of GNU General Public License (GPL) is included in this distribution, # in the file COPYING.GPL. # # As a special exception, if other files instantiate templates or use macros # or inline functions from this file, or you compile this file and link it # with other works to produce a work based on this file, this file # does not by itself cause the resulting work to be covered # by the GNU General Public License. # # However the source code for this file must still be made available # in accordance with section (3) of the GNU General Public License. # # This exception does not invalidate any other reasons why a work based # on this file might be covered by the GNU General Public License. # # Copyright (c) 2016-2018 Intra2net AG import os import json import unittest from src.cnfvar import string as cnfvar_old # # test data # # model cnf tree demo_cnfvar = {"cnf": [ { "varname": "MY_FAVORITE_CNF_VAR", "instance": 1337, "number": 42, "data": "string conf content", "comment": "" }, { "varname": "SOME_NESTED_CNF_VAR", "instance": 0, "number": 23, "data": "999", "comment": "", "children": [ { "varname": "SOME_CHILD_VAR", "instance": 1, "number": 24, "data": "2014", "parent": 23, "comment": "" } ] }, ]} # duplicate line number demo_invalid_cnfvar = {"cnf": [ { "varname": "SOME_PARTICULARLY_TASTY_CNF_VAR", "instance": 1337, "number": 23, "data": "classic wingers", "comment": "" }, { "varname": "EXTRAORDINARILY_FANCY_CNF_VAR", "instance": 1, "number": 42, "data": "ab mentions", "comment": "" }, { "varname": "ANOTHER_POPULAR_CNF_VAR", "instance": 0, "number": 42, "data": "notches", "comment": "" } ]} demo_nonascii = r""" 1 USER,1: "admin" 2 (1) USER_DISABLED,0: "0" 3 (1) USER_FULLNAME,0: "Administrator" 4 (1) USER_GROUPWARE_FOLDER_CALENDAR,0: "INBOX/Kalender" 5 (1) USER_GROUPWARE_FOLDER_CONTACTS,0: "INBOX/Kontakte" 6 (1) USER_GROUPWARE_FOLDER_DRAFTS,0: "INBOX/Entwürfe" 7 (1) USER_GROUPWARE_FOLDER_NOTES,0: "INBOX/Notizen" 8 (1) USER_GROUPWARE_FOLDER_OUTBOX,0: "INBOX/Gesendete Elemente" 9 (1) USER_GROUPWARE_FOLDER_TASKS,0: "INBOX/Aufgaben" 10 (1) USER_GROUPWARE_FOLDER_TRASH,0: "INBOX/Gelöschte Elemente" 11 (1) USER_GROUP_MEMBER_REF,0: "1" 12 (1) USER_GROUP_MEMBER_REF,1: "2" 13 (1) USER_PASSWORD,0: "test1234" """ demo_latin1crap = demo_nonascii.encode('latin1') demo_cnf_group = """ 1 GROUP,1: "Administratoren" 2 (1) GROUP_ACCESS_GO_ONLINE_ALLOWED,0: "1" 3 (1) GROUP_EMAILFILTER_BAN_FILTERLIST_REF,0: "-1" 4 (1) GROUP_EMAIL_RELAY_RIGHTS,0: "RELAY_FROM_INTRANET" 5 (1) GROUP_PROXY_PROFILE_REF,0: "1" """ demo_cnf_group_bytes = demo_cnf_group.encode("latin-1") demo_cnf_filter = b""" 1 EMAILFILTER_BAN_FILTERLIST,1: "Vordefiniert: Alles verboten" 2 (1) EMAILFILTER_BAN_FILTERLIST_ENCRYPTED,0: "BLOCK" 3 (1) EMAILFILTER_BAN_FILTERLIST_EXTENSIONS,0: "" 4 (1) EMAILFILTER_BAN_FILTERLIST_MIMETYPES,0: "" 5 (4) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,0: "text/plain" 6 (4) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,1: "text/html" 7 (1) EMAILFILTER_BAN_FILTERLIST_MODE,0: "ALLOW" 8 (1) EMAILFILTER_BAN_FILTERLIST_PREDEFINED_ID,0: "1" """ demo_cnf_comments = b""" 1 EMAILFILTER_BAN_FILTERLIST,1: "Vordefiniert: Alles verboten" 2 (1) EMAILFILTER_BAN_FILTERLIST_ENCRYPTED,0: "BLOCK" 3 (1) EMAILFILTER_BAN_FILTERLIST_EXTENSIONS,0: "" 4 (1) EMAILFILTER_BAN_FILTERLIST_MIMETYPES,0: "" # foo 5 (4) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,0: "text/plain"#bar 6 (4) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,1: "text/html" 7 (1) EMAILFILTER_BAN_FILTERLIST_MODE,0: "ALLOW" # baz 8 (1) EMAILFILTER_BAN_FILTERLIST_PREDEFINED_ID,0: "1" """ demo_cnf_escaped_quotes = r""" 1 HERE_BE_QUOTES,0: "\"" 2 HERE_BE_QUOTES,1: "foo\"bar\"\"\"baz" 3 HERE_BE_QUOTES,2: "unquo\\\"table" 4 HERE_BE_QUOTES,3: "unquo\\\\\"\"table" """ demo_bad_quotes = '1 BAD_VALUE,0: "\""' demo_cnf_quoted_json = r''.join([ r'1 AD_MACHINE_CREDENTIALS,0: "{\"Reserved Flags\":\"0\",', r'\"Join Time\":\"20241001071550.162557Z\",\"Computer Name\":\"MIS1\",', r'\"Account Name\":\"MIS1$\",\"Secure Channel Type\":2,\"Trust Flags\":26}"' ]) # # test class # class CnfVarUnittest(unittest.TestCase): def test_print_cnf(self): with open(os.devnull, "w") as devnull: print(demo_cnfvar, file=devnull) def test_parse_cnf_simple_str(self): cnf = cnfvar_old.read_cnf(demo_cnf_group) with open(os.devnull, "w") as devnull: print(cnf, file=devnull) def test_parse_cnf_simple_bytes(self): cnf = cnfvar_old.read_cnf(demo_cnf_group_bytes) with open(os.devnull, "w") as devnull: print(cnf, file=devnull) def test_parse_cnf_nested(self): cnf = cnfvar_old.read_cnf(demo_cnf_filter) with open(os.devnull, "w") as devnull: print(cnf, file=devnull) def test_parse_cnf_comments(self): cnf = cnfvar_old.read_cnf(demo_cnf_comments) with open(os.devnull, "w") as devnull: print(cnf, file=devnull) @unittest.skip("todo: this does not add anything to `test_print_cnf`") def test_print_cnf_garbage(self): try: with open(os.devnull, "w") as devnull: print(demo_invalid_cnfvar, file=devnull) except cnfvar_old.InvalidCNF: print ("Caught the duplicate line, bravo!") def test_parse_cnf_quotes(self): cnf = cnfvar_old.read_cnf(demo_cnf_escaped_quotes) self.assertEqual(len(cnf["cnf"]), 4) def test_parse_bad_cnf_quotes(self): self.assertRaises(cnfvar_old.MalformedCNF, cnfvar_old.read_cnf, demo_bad_quotes) def test_parse_quoted_json(self): cnf = cnfvar_old.read_cnf(demo_cnf_quoted_json) dat = json.loads(cnf["cnf"][0]["data"]) self.assertIn("Computer Name", dat) self.assertIn("Account Name", dat) def test_read_nonascii(self): cnf = cnfvar_old.read_cnf(demo_nonascii) with open(os.devnull, "w") as devnull: print(cnf, file=devnull) def test_read_latin1(self): cnf = cnfvar_old.read_cnf(demo_latin1crap) with open(os.devnull, "w") as devnull: print(cnf, file=devnull) class CnfVarUnittestVarnameCase(unittest.TestCase): """Tests for verifying that uppercasing/lowercasing of varname works.""" # TODO: rethink whether this lower-casing is worth all the effort it causes def test_read_cnf_lowercase(self): """Test that after reading, varnames are lowercase.""" cnf = cnfvar_old.read_cnf(demo_cnf_group_bytes) for parentvar in cnf['cnf']: self.assertEqual(parentvar['varname'], parentvar['varname'].lower()) if 'children' in parentvar: for childvar in parentvar['children']: self.assertEqual(parentvar['varname'], parentvar['varname'].lower()) if __name__ == '__main__': unittest.main()