From: Tomas Chvatal Date: Wed Feb 7 09:26:35 UTC 2018 Subject: Convert tests to run under python3 References: https://github.com/linux-audit/audit-userspace/pull/39 Patch-mainline: no; pending with maintainer Adjust auparse_test to run with python3 and python2 Index: audit-2.8.1/auparse/test/auparse_test.py =================================================================== --- audit-2.8.1.orig/auparse/test/auparse_test.py +++ audit-2.8.1/auparse/test/auparse_test.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +from __future__ import print_function + import os srcdir = os.getenv('srcdir') @@ -30,29 +32,29 @@ def walk_test(au): au.reset() while True: if not au.first_record(): - print "Error getting first record" + print("Error getting first record") sys.exit(1) - print "event %d has %d records" % (event_cnt, au.get_num_records()) + print("event %d has %d records" % (event_cnt, au.get_num_records())) record_cnt = 1 while True: - print " record %d of type %d(%s) has %d fields" % \ + print(" record %d of type %d(%s) has %d fields" % \ (record_cnt, au.get_type(), audit.audit_msg_type_to_name(au.get_type()), - au.get_num_fields()) - print " line=%d file=%s" % (au.get_line_number(), au.get_filename()) + au.get_num_fields())) + print(" line=%d file=%s" % (au.get_line_number(), au.get_filename())) event = au.get_timestamp() if event is None: - print "Error getting timestamp - aborting" + print("Error getting timestamp - aborting") sys.exit(1) - print " event time: %d.%d:%d, host=%s" % (event.sec, event.milli, event.serial, none_to_null(event.host)) + print(" event time: %d.%d:%d, host=%s" % (event.sec, event.milli, event.serial, none_to_null(event.host))) au.first_field() while True: - print " %s=%s (%s)" % (au.get_field_name(), au.get_field_str(), au.interpret_field()) + print(" %s=%s (%s)" % (au.get_field_name(), au.get_field_str(), au.interpret_field())) if not au.next_field(): break - print + print("") record_cnt += 1 if not au.next_record(): break event_cnt += 1 @@ -62,25 +64,25 @@ def walk_test(au): def light_test(au): while True: if not au.first_record(): - print "Error getting first record" + print("Error getting first record") sys.exit(1) - print "event has %d records" % (au.get_num_records()) + print("event has %d records" % (au.get_num_records())) record_cnt = 1 while True: - print " record %d of type %d(%s) has %d fields" % \ + print(" record %d of type %d(%s) has %d fields" % \ (record_cnt, au.get_type(), audit.audit_msg_type_to_name(au.get_type()), - au.get_num_fields()) - print " line=%d file=%s" % (au.get_line_number(), au.get_filename()) + au.get_num_fields())) + print(" line=%d file=%s" % (au.get_line_number(), au.get_filename())) event = au.get_timestamp() if event is None: - print "Error getting timestamp - aborting" + print("Error getting timestamp - aborting") sys.exit(1) - print " event time: %d.%d:%d, host=%s" % (event.sec, event.milli, event.serial, none_to_null(event.host)) - print + print(" event time: %d.%d:%d, host=%s" % (event.sec, event.milli, event.serial, none_to_null(event.host))) + print("") record_cnt += 1 if not au.next_record(): break if not au.parse_next_event(): break @@ -97,9 +99,9 @@ def simple_search(au, source, where): au.search_add_item("auid", "=", val, auparse.AUSEARCH_RULE_CLEAR) au.search_set_stop(where) if not au.search_next_event(): - print "Error searching for auid" + print("Error searching for auid") else: - print "Found %s = %s" % (au.get_field_name(), au.get_field_str()) + print("Found %s = %s" % (au.get_field_name(), au.get_field_str())) def compound_search(au, how): au = auparse.AuParser(auparse.AUSOURCE_FILE, srcdir + "/test.log"); @@ -115,119 +117,119 @@ def compound_search(au, how): au.search_set_stop(auparse.AUSEARCH_STOP_FIELD) if not au.search_next_event(): - print "Error searching for auid" + print("Error searching for auid") else: - print "Found %s = %s" % (au.get_field_name(), au.get_field_str()) + print("Found %s = %s" % (au.get_field_name(), au.get_field_str())) def feed_callback(au, cb_event_type, event_cnt): if cb_event_type == auparse.AUPARSE_CB_EVENT_READY: if not au.first_record(): - print "Error getting first record" + print("Error getting first record") sys.exit(1) - print "event %d has %d records" % (event_cnt[0], au.get_num_records()) + print("event %d has %d records" % (event_cnt[0], au.get_num_records())) record_cnt = 1 while True: - print " record %d of type %d(%s) has %d fields" % \ + print(" record %d of type %d(%s) has %d fields" % \ (record_cnt, au.get_type(), audit.audit_msg_type_to_name(au.get_type()), - au.get_num_fields()) - print " line=%d file=%s" % (au.get_line_number(), au.get_filename()) + au.get_num_fields())) + print(" line=%d file=%s" % (au.get_line_number(), au.get_filename())) event = au.get_timestamp() if event is None: - print "Error getting timestamp - aborting" + print("Error getting timestamp - aborting") sys.exit(1) - print " event time: %d.%d:%d, host=%s" % (event.sec, event.milli, event.serial, none_to_null(event.host)) + print(" event time: %d.%d:%d, host=%s" % (event.sec, event.milli, event.serial, none_to_null(event.host))) au.first_field() while True: - print " %s=%s (%s)" % (au.get_field_name(), au.get_field_str(), au.interpret_field()) + print(" %s=%s (%s)" % (au.get_field_name(), au.get_field_str(), au.interpret_field())) if not au.next_field(): break - print + print("") record_cnt += 1 if not au.next_record(): break event_cnt[0] += 1 au = auparse.AuParser(auparse.AUSOURCE_BUFFER_ARRAY, buf) -print "Starting Test 1, iterate..." +print("Starting Test 1, iterate...") while au.parse_next_event(): if au.find_field("auid"): - print "%s=%s" % (au.get_field_name(), au.get_field_str()) - print "interp auid=%s" % (au.interpret_field()) + print("%s=%s" % (au.get_field_name(), au.get_field_str())) + print("interp auid=%s" % (au.interpret_field())) else: - print "Error iterating to auid" -print "Test 1 Done\n" + print("Error iterating to auid") +print("Test 1 Done\n") # Reset, now lets go to beginning and walk the list manually */ -print "Starting Test 2, walk events, records, and fields..." +print("Starting Test 2, walk events, records, and fields...") au.reset() walk_test(au) -print "Test 2 Done\n" +print("Test 2 Done\n") # Reset, now lets go to beginning and walk the list manually */ -print "Starting Test 3, walk events, records of 1 buffer..." +print("Starting Test 3, walk events, records of 1 buffer...") au = auparse.AuParser(auparse.AUSOURCE_BUFFER, buf[1]) au.reset() light_test(au); -print "Test 3 Done\n" +print("Test 3 Done\n") -print "Starting Test 4, walk events, records of 1 file..." +print("Starting Test 4, walk events, records of 1 file...") au = auparse.AuParser(auparse.AUSOURCE_FILE, srcdir + "/test.log"); walk_test(au); -print "Test 4 Done\n" +print("Test 4 Done\n") -print "Starting Test 5, walk events, records of 2 files..." +print("Starting Test 5, walk events, records of 2 files...") au = auparse.AuParser(auparse.AUSOURCE_FILE_ARRAY, files); walk_test(au); -print "Test 5 Done\n" +print("Test 5 Done\n") -print "Starting Test 6, search..." +print("Starting Test 6, search...") au = auparse.AuParser(auparse.AUSOURCE_BUFFER_ARRAY, buf) au.search_add_item("auid", "=", "500", auparse.AUSEARCH_RULE_CLEAR) au.search_set_stop(auparse.AUSEARCH_STOP_EVENT) if au.search_next_event(): - print "Error search found something it shouldn't have" + print("Error search found something it shouldn't have") else: - print "auid = 500 not found...which is correct" + print("auid = 500 not found...which is correct") au.search_clear() au = auparse.AuParser(auparse.AUSOURCE_BUFFER_ARRAY, buf) #au.search_add_item("auid", "exists", None, auparse.AUSEARCH_RULE_CLEAR) au.search_add_item("auid", "exists", "", auparse.AUSEARCH_RULE_CLEAR) au.search_set_stop(auparse.AUSEARCH_STOP_EVENT) if not au.search_next_event(): - print "Error searching for existence of auid" -print "auid exists...which is correct" -print "Testing BUFFER_ARRAY, stop on field" + print("Error searching for existence of auid") +print("auid exists...which is correct") +print("Testing BUFFER_ARRAY, stop on field") simple_search(au, auparse.AUSOURCE_BUFFER_ARRAY, auparse.AUSEARCH_STOP_FIELD) -print "Testing BUFFER_ARRAY, stop on record" +print("Testing BUFFER_ARRAY, stop on record") simple_search(au, auparse.AUSOURCE_BUFFER_ARRAY, auparse.AUSEARCH_STOP_RECORD) -print "Testing BUFFER_ARRAY, stop on event" +print("Testing BUFFER_ARRAY, stop on event") simple_search(au, auparse.AUSOURCE_BUFFER_ARRAY, auparse.AUSEARCH_STOP_EVENT) -print "Testing test.log, stop on field" +print("Testing test.log, stop on field") simple_search(au, auparse.AUSOURCE_FILE, auparse.AUSEARCH_STOP_FIELD) -print "Testing test.log, stop on record" +print("Testing test.log, stop on record") simple_search(au, auparse.AUSOURCE_FILE, auparse.AUSEARCH_STOP_RECORD) -print "Testing test.log, stop on event" +print("Testing test.log, stop on event") simple_search(au, auparse.AUSOURCE_FILE, auparse.AUSEARCH_STOP_EVENT) -print "Test 6 Done\n" +print("Test 6 Done\n") -print "Starting Test 7, compound search..." +print("Starting Test 7, compound search...") au = auparse.AuParser(auparse.AUSOURCE_BUFFER_ARRAY, buf) compound_search(au, auparse.AUSEARCH_RULE_AND) compound_search(au, auparse.AUSEARCH_RULE_OR) -print "Test 7 Done\n" +print("Test 7 Done\n") -print "Starting Test 8, regex search..." +print("Starting Test 8, regex search...") au = auparse.AuParser(auparse.AUSOURCE_BUFFER_ARRAY, buf) -print "Doing regex match...\n" +print("Doing regex match...\n") au = auparse.AuParser(auparse.AUSOURCE_BUFFER_ARRAY, buf) -print "Test 8 Done\n" +print("Test 8 Done\n") # Note: this should match Test 2 exactly # Note: this should match Test 2 exactly -print "Starting Test 9, buffer feed..." +print("Starting Test 9, buffer feed...") au = auparse.AuParser(auparse.AUSOURCE_FEED); event_cnt = 1 au.add_callback(feed_callback, [event_cnt]) @@ -241,10 +243,10 @@ for s in buf: beg += chunk_len au.feed(data) au.flush_feed() -print "Test 9 Done\n" +print("Test 9 Done\n") # Note: this should match Test 4 exactly -print "Starting Test 10, file feed..." +print("Starting Test 10, file feed...") au = auparse.AuParser(auparse.AUSOURCE_FEED); event_cnt = 1 au.add_callback(feed_callback, [event_cnt]) @@ -254,9 +256,9 @@ while True: if not data: break au.feed(data) au.flush_feed() -print "Test 10 Done\n" +print("Test 10 Done\n") -print "Finished non-admin tests\n" +print("Finished non-admin tests\n") au = None sys.exit(0)