1#!/usr/bin/python3 -E
2import sys
3import selinux
4
5
6verbose = 0
7errors = 0
8
9
10def untrans(trans, val):
11    global errors, verbose
12    (rc, raw) = selinux.selinux_trans_to_raw_context(trans)
13    if raw != val:
14        print("untrans: '%s' -> '%s' != '%s' FAILED" % (trans, raw, val))
15        errors += 1
16    else:
17        if verbose:
18            print("untrans: %s -> %s != %s SUCCESS" % (trans, raw, val))
19
20
21def trans(raw, val):
22    global errors, verbose
23    (rc, trans) = selinux.selinux_raw_to_trans_context(raw)
24    if trans != val:
25        print("trans: '%s' -> '%s' != '%s' FAILED" % (raw, trans, val))
26        errors += 1
27    else:
28        if verbose:
29            print("trans: %s -> %s != %s SUCCESS" % (raw, trans, val))
30
31
32if len(sys.argv) > 1 and sys.argv[1] == "-v":
33    verbose = 1
34
35for arg in sys.argv[1:]:
36    f = open(arg, 'r')
37    for line in f:
38        if line.startswith('#'):
39            continue
40        if not line.strip():
41            continue
42        line = line.rstrip('\n')
43        if line.find("==") != -1:
44            t, r = line.split("==")
45            untrans("a:b:c:" + t, "a:b:c:" + r)
46            trans("a:b:c:" + r, "a:b:c:" + t)
47        else:
48            t, r = line.split("=")
49            untrans("a:b:c:" + t, "a:b:c:" + r)
50    f.close()
51
52s = "s"
53if errors == 1:
54    s = ""
55print("mlstrans-test done with %d error%s" % (errors, s))
56
57sys.exit(errors)
58