#
# Copyright (c) 2016-2018 Intra2net AG <info@intra2net.com>
+import os
import unittest
+
from src import cnfvar
#
}
"""
+demo_jsoncnf_bytes = demo_jsoncnf.encode ("latin-1")
+
demo_latin1crap = r"""
{ "cnf" : [
{
]}
"""
-demo_cnf_group = b"""
+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"
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"
8 (1) EMAILFILTER_BAN_FILTERLIST_PREDEFINED_ID,0: "1"
"""
+demo_cnf_escaped_quotes = """
+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_json_escaped_quotes = """
+{ "cnf": [ { "number" : 1,
+ "varname" : "HERE_BE_QUOTES",
+ "instance" : 0,
+ "data" : "\\"" },
+ { "number" : 2,
+ "varname" : "HERE_BE_QUOTES",
+ "instance" : 1,
+ "data" : "foo\\"bar\\"\\"\\"baz" },
+ { "number" : 3,
+ "varname" : "HERE_BE_QUOTES",
+ "instance" : 2,
+ "data" : "unquo\\\\\\"table" },
+ { "number" : 4,
+ "varname" : "HERE_BE_QUOTES",
+ "instance" : 3,
+ "data" : "unquo\\\\\\\\\\"\\"table" } ] }
+"""
#
# test class
#
-@unittest.skip('TODO Philipp: correct (maybe replace all bytes with str using '
- 'type_helpers?)')
class CnfVarUnittest(unittest.TestCase):
def test_print_cnf(self):
with open(os.devnull, "w") as devnull:
cnfvar.print_cnf(demo_cnfvar, out=devnull)
- def test_parse_cnf_simple(self):
+ def test_parse_cnf_simple_str(self):
cnf = cnfvar.read_cnf(demo_cnf_group)
with open(os.devnull, "w") as devnull:
cnfvar.print_cnf_json(cnf, out=devnull)
+ def test_parse_cnf_simple_bytes(self):
+ cnf = cnfvar.read_cnf(demo_cnf_group_bytes)
+ with open(os.devnull, "w") as devnull:
+ cnfvar.print_cnf_json(cnf, out=devnull)
+
def test_parse_cnf_nested(self):
cnf = cnfvar.read_cnf(demo_cnf_filter)
with open(os.devnull, "w") as devnull:
except cnfvar.InvalidCNF:
print ("Caught the duplicate line, bravo!")
- def test_read_json(self):
+ def test_read_json_str(self):
cnf = cnfvar.read_cnf_json(demo_jsoncnf)
with open(os.devnull, "w") as devnull:
cnfvar.print_cnf(cnf, out=devnull)
+ def test_read_json_bytes(self):
+ cnf = cnfvar.read_cnf_json(demo_jsoncnf_bytes)
+ with open(os.devnull, "w") as devnull:
+ cnfvar.print_cnf(cnf, out=devnull)
+
def test_read_json_nonascii(self):
cnf = cnfvar.read_cnf_json(demo_latin1crap)
with open(os.devnull, "w") as devnull:
cnfvar.print_cnf(cnf, out=devnull)
+ def test_parse_cnf_quotes(self):
+ cnf = cnfvar.read_cnf(demo_cnf_escaped_quotes)
+ with open(os.devnull, "w") as devnull:
+ cnfvar.print_cnf_json(cnf, out=devnull)
+
+ def test_parse_json_quotes(self):
+ cnf = cnfvar.read_cnf_json(demo_json_escaped_quotes)
+ with open(os.devnull, "w") as devnull:
+ cnfvar.print_cnf_json(cnf, out=devnull)
+
class CnfVarUnittestVarnameCase(unittest.TestCase):
"""Tests for verifying that uppercasing/lowercasing of varname works."""