sediff/PolicyDifferenceTest: use explicit sorting key on tuples

Guarantee stable output.
This commit is contained in:
Chris PeBenito 2016-02-02 08:52:30 -05:00
parent 964a3ea3f5
commit 742255643f
2 changed files with 41 additions and 32 deletions

43
sediff
View File

@ -111,7 +111,7 @@ try:
print("Policy Properties ({0} Modified)".format(len(diff.modified_properties))) print("Policy Properties ({0} Modified)".format(len(diff.modified_properties)))
if diff.modified_properties and not args.stats: if diff.modified_properties and not args.stats:
for name, added, removed in sorted(diff.modified_properties): for name, added, removed in sorted(diff.modified_properties, key=lambda x: x.property):
print(" * {0} +{1} -{2}".format(name, added, removed)) print(" * {0} +{1} -{2}".format(name, added, removed))
print() print()
@ -427,7 +427,8 @@ try:
print(" - {0}".format(l)) print(" - {0}".format(l))
if diff.modified_levels and not args.stats: if diff.modified_levels and not args.stats:
print(" Modified Levels: {0}".format(len(diff.modified_levels))) print(" Modified Levels: {0}".format(len(diff.modified_levels)))
for level, added_categories, removed_categories, _ in sorted(diff.modified_levels): for level, added_categories, removed_categories, _ in sorted(diff.modified_levels,
key=lambda x: x.level):
change = [] change = []
if added_categories: if added_categories:
change.append("{0} Added Categories".format(len(added_categories))) change.append("{0} Added Categories".format(len(added_categories)))
@ -459,7 +460,8 @@ try:
if diff.modified_allows and not args.stats: if diff.modified_allows and not args.stats:
print(" Modified Allow Rules: {0}".format(len(diff.modified_allows))) print(" Modified Allow Rules: {0}".format(len(diff.modified_allows)))
for rule, added_perms, removed_perms, matched_perms in sorted(diff.modified_allows): for rule, added_perms, removed_perms, matched_perms in sorted(diff.modified_allows,
key=lambda x: x.rule):
perms = " ".join(chain((p for p in matched_perms), perms = " ".join(chain((p for p in matched_perms),
("+" + p for p in added_perms), ("+" + p for p in added_perms),
("-" + p for p in removed_perms))) ("-" + p for p in removed_perms)))
@ -495,7 +497,7 @@ try:
print(" Modified Neverallow Rules: {0}".format(len(diff.modified_neverallows))) print(" Modified Neverallow Rules: {0}".format(len(diff.modified_neverallows)))
for rule, added_perms, removed_perms, matched_perms in sorted( for rule, added_perms, removed_perms, matched_perms in sorted(
diff.modified_neverallows): diff.modified_neverallows, key=lambda x: x.rule):
perms = " ".join(chain((p for p in matched_perms), perms = " ".join(chain((p for p in matched_perms),
("+" + p for p in added_perms), ("+" + p for p in added_perms),
("-" + p for p in removed_perms))) ("-" + p for p in removed_perms)))
@ -531,7 +533,7 @@ try:
print(" Modified Auditallow Rules: {0}".format(len(diff.modified_auditallows))) print(" Modified Auditallow Rules: {0}".format(len(diff.modified_auditallows)))
for rule, added_perms, removed_perms, matched_perms in sorted( for rule, added_perms, removed_perms, matched_perms in sorted(
diff.modified_auditallows): diff.modified_auditallows, key=lambda x: x.rule):
perms = " ".join(chain((p for p in matched_perms), perms = " ".join(chain((p for p in matched_perms),
("+" + p for p in added_perms), ("+" + p for p in added_perms),
("-" + p for p in removed_perms))) ("-" + p for p in removed_perms)))
@ -567,7 +569,7 @@ try:
print(" Modified Dontaudit Rules: {0}".format(len(diff.modified_dontaudits))) print(" Modified Dontaudit Rules: {0}".format(len(diff.modified_dontaudits)))
for rule, added_perms, removed_perms, matched_perms in sorted( for rule, added_perms, removed_perms, matched_perms in sorted(
diff.modified_dontaudits): diff.modified_dontaudits, key=lambda x: x.rule):
perms = " ".join(chain((p for p in matched_perms), perms = " ".join(chain((p for p in matched_perms),
("+" + p for p in added_perms), ("+" + p for p in added_perms),
("-" + p for p in removed_perms))) ("-" + p for p in removed_perms)))
@ -605,7 +607,8 @@ try:
print(" Modified Type_transition Rules: {0}".format( print(" Modified Type_transition Rules: {0}".format(
len(diff.modified_type_transitions))) len(diff.modified_type_transitions)))
for rule, added_default, removed_default in sorted(diff.modified_type_transitions): for rule, added_default, removed_default in sorted(diff.modified_type_transitions,
key=lambda x: x.rule):
rule_string = "{0.ruletype} {0.source} {0.target}:{0.tclass} +{1} -{2}".format( rule_string = "{0.ruletype} {0.source} {0.target}:{0.tclass} +{1} -{2}".format(
rule, added_default, removed_default) rule, added_default, removed_default)
@ -645,7 +648,8 @@ try:
if diff.modified_type_changes and not args.stats: if diff.modified_type_changes and not args.stats:
print(" Modified Type_change Rules: {0}".format(len(diff.modified_type_changes))) print(" Modified Type_change Rules: {0}".format(len(diff.modified_type_changes)))
for rule, added_default, removed_default in sorted(diff.modified_type_changes): for rule, added_default, removed_default in sorted(diff.modified_type_changes,
key=lambda x: x.rule):
rule_string = "{0.ruletype} {0.source} {0.target}:{0.tclass} +{1} -{2}".format( rule_string = "{0.ruletype} {0.source} {0.target}:{0.tclass} +{1} -{2}".format(
rule, added_default, removed_default) rule, added_default, removed_default)
@ -685,7 +689,8 @@ try:
if diff.modified_type_members and not args.stats: if diff.modified_type_members and not args.stats:
print(" Modified Type_member Rules: {0}".format(len(diff.modified_type_members))) print(" Modified Type_member Rules: {0}".format(len(diff.modified_type_members)))
for rule, added_default, removed_default in sorted(diff.modified_type_members): for rule, added_default, removed_default in sorted(diff.modified_type_members,
key=lambda x: x.rule):
rule_string = "{0.ruletype} {0.source} {0.target}:{0.tclass} +{1} -{2}".format( rule_string = "{0.ruletype} {0.source} {0.target}:{0.tclass} +{1} -{2}".format(
rule, added_default, removed_default) rule, added_default, removed_default)
@ -747,7 +752,8 @@ try:
print(" Modified Role_transition Rules: {0}".format( print(" Modified Role_transition Rules: {0}".format(
len(diff.modified_role_transitions))) len(diff.modified_role_transitions)))
for rule, added_default, removed_default in sorted(diff.modified_role_transitions): for rule, added_default, removed_default in sorted(diff.modified_role_transitions,
key=lambda x: x.rule):
rule_string = \ rule_string = \
"{0.ruletype} {0.source} {0.target}:{0.tclass} +{1} -{2}".format( "{0.ruletype} {0.source} {0.target}:{0.tclass} +{1} -{2}".format(
rule, added_default, removed_default) rule, added_default, removed_default)
@ -779,7 +785,8 @@ try:
print(" Modified Range_transition Rules: {0}".format( print(" Modified Range_transition Rules: {0}".format(
len(diff.modified_range_transitions))) len(diff.modified_range_transitions)))
for rule, added_default, removed_default in sorted(diff.modified_range_transitions): for rule, added_default, removed_default in sorted(diff.modified_range_transitions,
key=lambda x: x.rule):
# added brackets around range change for clarity since ranges # added brackets around range change for clarity since ranges
# can have '-' and spaces. # can have '-' and spaces.
rule_string = \ rule_string = \
@ -828,7 +835,7 @@ try:
print(" - {0}".format(s)) print(" - {0}".format(s))
if diff.modified_fs_uses and not args.stats: if diff.modified_fs_uses and not args.stats:
print(" Modified Fs_use: {0}".format(len(diff.modified_fs_uses))) print(" Modified Fs_use: {0}".format(len(diff.modified_fs_uses)))
for entry in sorted(diff.modified_fs_uses): for entry in sorted(diff.modified_fs_uses, key=lambda x: x.rule):
print(" * {0.ruletype} {0.fs} +[{1}] -[{2}];".format( print(" * {0.ruletype} {0.fs} +[{1}] -[{2}];".format(
entry.rule, entry.added_context, entry.removed_context)) entry.rule, entry.added_context, entry.removed_context))
@ -850,7 +857,7 @@ try:
print(" - {0}".format(s)) print(" - {0}".format(s))
if diff.modified_genfscons and not args.stats: if diff.modified_genfscons and not args.stats:
print(" Modified Genfscons: {0}".format(len(diff.modified_genfscons))) print(" Modified Genfscons: {0}".format(len(diff.modified_genfscons)))
for entry in sorted(diff.modified_genfscons): for entry in sorted(diff.modified_genfscons, key=lambda x: x.rule):
print(" * genfscon {0.fs} {0.path} {0.filetype} +[{1}] -[{2}];".format( print(" * genfscon {0.fs} {0.path} {0.filetype} +[{1}] -[{2}];".format(
entry.rule, entry.added_context, entry.removed_context)) entry.rule, entry.added_context, entry.removed_context))
@ -872,7 +879,7 @@ try:
print(" - {0}".format(n)) print(" - {0}".format(n))
if diff.modified_netifcons and not args.stats: if diff.modified_netifcons and not args.stats:
print(" Modified Netifcons: {0}".format(len(diff.modified_netifcons))) print(" Modified Netifcons: {0}".format(len(diff.modified_netifcons)))
for entry in sorted(diff.modified_netifcons): for entry in sorted(diff.modified_netifcons, key=lambda x: x.rule):
# This output is different than other statements because # This output is different than other statements because
# it becomes difficult to read if this was condensed # it becomes difficult to read if this was condensed
# into a single line, especially if both contexts # into a single line, especially if both contexts
@ -912,7 +919,8 @@ try:
print(" - {0}".format(n)) print(" - {0}".format(n))
if diff.modified_nodecons and not args.stats: if diff.modified_nodecons and not args.stats:
print(" Modified Nodecons: {0}".format(len(diff.modified_nodecons))) print(" Modified Nodecons: {0}".format(len(diff.modified_nodecons)))
for con, added_context, removed_context in sorted(diff.modified_nodecons): for con, added_context, removed_context in sorted(diff.modified_nodecons,
key=lambda x: x.rule):
print(" * nodecon {0.address} {0.netmask} +[{1}] -[{2}];".format( print(" * nodecon {0.address} {0.netmask} +[{1}] -[{2}];".format(
con, added_context, removed_context)) con, added_context, removed_context))
@ -934,7 +942,8 @@ try:
print(" - {0}".format(n)) print(" - {0}".format(n))
if diff.modified_portcons and not args.stats: if diff.modified_portcons and not args.stats:
print(" Modified Portcons: {0}".format(len(diff.modified_portcons))) print(" Modified Portcons: {0}".format(len(diff.modified_portcons)))
for con, added_context, removed_context in sorted(diff.modified_portcons): for con, added_context, removed_context in sorted(diff.modified_portcons,
key=lambda x: x.rule):
low, high = con.ports low, high = con.ports
if low == high: if low == high:
print(" * portcon {0.protocol} {1} +[{2}] -[{3}];".format( print(" * portcon {0.protocol} {1} +[{2}] -[{3}];".format(
@ -975,7 +984,7 @@ try:
if diff.modified_defaults and not args.stats: if diff.modified_defaults and not args.stats:
print(" Modified Defaults: {0}".format(len(diff.modified_defaults))) print(" Modified Defaults: {0}".format(len(diff.modified_defaults)))
for default, added_default, removed_default, added_range, removed_range in sorted( for default, added_default, removed_default, added_range, removed_range in sorted(
diff.modified_defaults): diff.modified_defaults, key=lambda x: x.rule):
line = " * {0.ruletype} {0.tclass} ".format(default) line = " * {0.ruletype} {0.tclass} ".format(default)
if removed_default: if removed_default:
line += "+{0} -{1}".format(added_default, removed_default) line += "+{0} -{1}".format(added_default, removed_default)

View File

@ -266,7 +266,7 @@ class PolicyDifferenceTest(ValidateRule, unittest.TestCase):
def test_modified_allow_rules(self): def test_modified_allow_rules(self):
"""Diff: modified allow rules.""" """Diff: modified allow rules."""
l = sorted(self.diff.modified_allows) l = sorted(self.diff.modified_allows, key=lambda x: x.rule)
self.assertEqual(3, len(l)) self.assertEqual(3, len(l))
# add permissions # add permissions
@ -354,7 +354,7 @@ class PolicyDifferenceTest(ValidateRule, unittest.TestCase):
def test_modified_auditallow_rules(self): def test_modified_auditallow_rules(self):
"""Diff: modified auditallow rules.""" """Diff: modified auditallow rules."""
l = sorted(self.diff.modified_auditallows) l = sorted(self.diff.modified_auditallows, key=lambda x: x.rule)
self.assertEqual(3, len(l)) self.assertEqual(3, len(l))
# add permissions # add permissions
@ -442,7 +442,7 @@ class PolicyDifferenceTest(ValidateRule, unittest.TestCase):
def test_modified_dontaudit_rules(self): def test_modified_dontaudit_rules(self):
"""Diff: modified dontaudit rules.""" """Diff: modified dontaudit rules."""
l = sorted(self.diff.modified_dontaudits) l = sorted(self.diff.modified_dontaudits, key=lambda x: x.rule)
self.assertEqual(3, len(l)) self.assertEqual(3, len(l))
# add permissions # add permissions
@ -506,7 +506,7 @@ class PolicyDifferenceTest(ValidateRule, unittest.TestCase):
def test_modified_neverallow_rules(self): def test_modified_neverallow_rules(self):
"""Diff: modified neverallow rules.""" """Diff: modified neverallow rules."""
l = sorted(self.diff.modified_neverallows) l = sorted(self.diff.modified_neverallows, key=lambda x: x.rule)
self.assertEqual(3, len(l)) self.assertEqual(3, len(l))
# add permissions # add permissions
@ -594,7 +594,7 @@ class PolicyDifferenceTest(ValidateRule, unittest.TestCase):
def test_modified_type_transition_rules(self): def test_modified_type_transition_rules(self):
"""Diff: modified type_transition rules.""" """Diff: modified type_transition rules."""
l = sorted(self.diff.modified_type_transitions) l = sorted(self.diff.modified_type_transitions, key=lambda x: x.rule)
self.assertEqual(1, len(l)) self.assertEqual(1, len(l))
rule, added_default, removed_default = l[0] rule, added_default, removed_default = l[0]
@ -660,7 +660,7 @@ class PolicyDifferenceTest(ValidateRule, unittest.TestCase):
def test_modified_type_change_rules(self): def test_modified_type_change_rules(self):
"""Diff: modified type_change rules.""" """Diff: modified type_change rules."""
l = sorted(self.diff.modified_type_changes) l = sorted(self.diff.modified_type_changes, key=lambda x: x.rule)
self.assertEqual(1, len(l)) self.assertEqual(1, len(l))
rule, added_default, removed_default = l[0] rule, added_default, removed_default = l[0]
@ -726,7 +726,7 @@ class PolicyDifferenceTest(ValidateRule, unittest.TestCase):
def test_modified_type_member_rules(self): def test_modified_type_member_rules(self):
"""Diff: modified type_member rules.""" """Diff: modified type_member rules."""
l = sorted(self.diff.modified_type_members) l = sorted(self.diff.modified_type_members, key=lambda x: x.rule)
self.assertEqual(1, len(l)) self.assertEqual(1, len(l))
rule, added_default, removed_default = l[0] rule, added_default, removed_default = l[0]
@ -768,7 +768,7 @@ class PolicyDifferenceTest(ValidateRule, unittest.TestCase):
def test_modified_range_transition_rules(self): def test_modified_range_transition_rules(self):
"""Diff: modified range_transition rules.""" """Diff: modified range_transition rules."""
l = sorted(self.diff.modified_range_transitions) l = sorted(self.diff.modified_range_transitions, key=lambda x: x.rule)
self.assertEqual(1, len(l)) self.assertEqual(1, len(l))
rule, added_default, removed_default = l[0] rule, added_default, removed_default = l[0]
@ -843,7 +843,7 @@ class PolicyDifferenceTest(ValidateRule, unittest.TestCase):
def test_modified_role_transition_rules(self): def test_modified_role_transition_rules(self):
"""Diff: modified role_transition rules.""" """Diff: modified role_transition rules."""
l = sorted(self.diff.modified_role_transitions) l = sorted(self.diff.modified_role_transitions, key=lambda x: x.rule)
self.assertEqual(1, len(l)) self.assertEqual(1, len(l))
rule, added_default, removed_default = l[0] rule, added_default, removed_default = l[0]
@ -1019,7 +1019,7 @@ class PolicyDifferenceTest(ValidateRule, unittest.TestCase):
def test_modified_fs_uses(self): def test_modified_fs_uses(self):
"""Diff: modified fs_uses.""" """Diff: modified fs_uses."""
l = sorted(self.diff.modified_fs_uses) l = sorted(self.diff.modified_fs_uses, key=lambda x: x.rule)
self.assertEqual(1, len(l)) self.assertEqual(1, len(l))
rule, added_context, removed_context = l[0] rule, added_context, removed_context = l[0]
@ -1063,7 +1063,7 @@ class PolicyDifferenceTest(ValidateRule, unittest.TestCase):
def test_modified_genfscons(self): def test_modified_genfscons(self):
"""Diff: modified genfscons.""" """Diff: modified genfscons."""
l = sorted(self.diff.modified_genfscons) l = sorted(self.diff.modified_genfscons, key=lambda x: x.rule)
self.assertEqual(1, len(l)) self.assertEqual(1, len(l))
rule, added_context, removed_context = l[0] rule, added_context, removed_context = l[0]
@ -1127,7 +1127,7 @@ class PolicyDifferenceTest(ValidateRule, unittest.TestCase):
def test_modified_netifcons(self): def test_modified_netifcons(self):
"""Diff: modified netifcons.""" """Diff: modified netifcons."""
l = sorted(self.diff.modified_netifcons) l = sorted(self.diff.modified_netifcons, key=lambda x: x.rule)
self.assertEqual(3, len(l)) self.assertEqual(3, len(l))
# modified both contexts # modified both contexts
@ -1209,7 +1209,7 @@ class PolicyDifferenceTest(ValidateRule, unittest.TestCase):
def test_modified_nodecons(self): def test_modified_nodecons(self):
"""Diff: modified nodecons.""" """Diff: modified nodecons."""
l = sorted(self.diff.modified_nodecons) l = sorted(self.diff.modified_nodecons, key=lambda x: x.rule)
self.assertEqual(2, len(l)) self.assertEqual(2, len(l))
# changed IPv4 # changed IPv4
@ -1268,7 +1268,7 @@ class PolicyDifferenceTest(ValidateRule, unittest.TestCase):
def test_modified_portcons(self): def test_modified_portcons(self):
"""Diff: modified portcons.""" """Diff: modified portcons."""
l = sorted(self.diff.modified_portcons) l = sorted(self.diff.modified_portcons, key=lambda x: x.rule)
self.assertEqual(2, len(l)) self.assertEqual(2, len(l))
portcon, added_context, removed_context = l[0] portcon, added_context, removed_context = l[0]
@ -1314,7 +1314,7 @@ class PolicyDifferenceTest(ValidateRule, unittest.TestCase):
def test_modified_defaults(self): def test_modified_defaults(self):
"""Diff: modified defaults.""" """Diff: modified defaults."""
l = sorted(self.diff.modified_defaults) l = sorted(self.diff.modified_defaults, key=lambda x: x.rule)
self.assertEqual(4, len(l)) self.assertEqual(4, len(l))
default, added_default, removed_default, added_range, removed_range = l[0] default, added_default, removed_default, added_range, removed_range = l[0]