# -*- coding: utf-8 -*- ## # Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. # copyright # THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, # IN PART OR IN WHOLE, # WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. # # file tst_inhandled_message_log # date 2022/04/09 # author sai chaithyna ela, # joseph varghese # # NOTE: # This test verifies the unhandled logs for the messages provided. # Scenarios are covered based on equivalence partitioning method of testing. import csv from dialin.ui.hd_simulator import HDSimulator from dialin.ui import utils from configuration import utility from configuration import config from dialin.utils.conversions import float_to_bytearray, integer_to_bytearray,unsigned_integer_to_bytearray hd_simulator = HDSimulator() LOCATION = '/home/denali/Projects/application/resources/settings/Messages/Unhandled.conf' DUMMY_FLOAT_VALUE_1 = 1.250 DUMMY_FLOAT_VALUE_2 = 5.855 DUMMY_INT_VALUE_1 = 25 DUMMY_INT_VALUE_2 = 32 DUMMY_UNSIGNED_INT_VALUE = 5 UNHANDLED_MESSAGE_1 = "\n[0xBB00]\nHD_custom_data_verification_1\nU32=unsigned_32bit\nU32=argument_2\n" UNHANDLED_MESSAGE_2 = "\n[0xAA00]\nHD_custom_data_verification_2\nF32=float_argument\nS32=argument_2\n" ERROR_MESSAGE = { "0x1300" : "Incorrect data length (9 of 32) for received Message with ID '0x1300'", "0x0900" : "Incorrect data length (9 of 20) for received Message with ID '0x0900'" } def verify_log(api_content): """ This function is capable to verify the log data from sd-card @param api_content - (string) combination of message id, message text and arguments separated with '\n' @param message_extracted - (list) list contains extracted data (ack_req, ack_bak, message, message_id). """ utils.waitForGUI(5) test.log(str(api_content)) message_extracted = utility.get_current_log_details(message_ack = True, message_text = '~'+api_content) test.log(str(message_extracted)) #loop to remove default arguments present in API, here it is '?' while('?' in message_extracted[0]): message_extracted[0].remove('?') return message_extracted def convert_message_id_from_unhandled_file_format_to_standared_format(message_id): """ This function is capable to remove square bracket and to convert message id into firmware support format. @param message_id - (string) message id from unhandled.conf file. @return expected_meassage_id - (string) message id in the standared format. """ formated_meassage_id = message_id.replace('[', "") formated_meassage_id = formated_meassage_id.replace(']', "") if formated_meassage_id[-2:] == '00' and len(formated_meassage_id) == 6: expected_meassage_id = formated_meassage_id.replace("00", "") return expected_meassage_id else: return formated_meassage_id def get_error_message_from_log(): """ This function is capable to verify the error logs from sd-card @param api_content - (string) combination of message id, message text and arguments separated with '\n' @param expected_api_argument - (list) expected arguments for API. """ utils.waitForGUI(5) count = 0 file_name = utility.get_extracted_error_file() try: with open(file_name, 'r') as csv_file: try: for row in reversed(list(csv.reader(csv_file))): if row[0].isalpha(): pass else: if count == 30: test.fail("handler unable to find message text from error file.") break row_length = sum(1 for values in row) try: if "'" in row[1]: return row[1] if "Unhandled Message ID (HD)" in row[1]: return True except: pass count+=1 except: test.fail("application error log data is corrupted") except: test.fail("err log file is not created or log file is not created based on standard log naming format.") def modify_unhandled_configuration_file(): """ This function is capable to write custom API's on unhandled.conf folder. unhandled.conf location = '/home/denali/Projects/application/resources/settings/Messages/Unhandled.conf' Through this method filereader act as a handler and it will write custom unhandled messages on Unhandled.conf NB: it is a single time callable method should only use before starting the application. it is mandatory to use demodify_unhandled_configuration_file() at the end of the testcase. """ with open(LOCATION, "a") as filereader: filereader.write(UNHANDLED_MESSAGE_1) filereader.write(UNHANDLED_MESSAGE_2) utils.waitForGUI(5) def demodify_unhandled_configuration_file(): """ This function is capable to remove custom API's which is written during method "modify_unhandled_configuration_file()". unhandled.conf location = '/home/denali/Projects/application/resources/settings/Messages/Unhandled.conf' NB: it is a single time callable method should use only after calling "modify_unhandled_configuration_file()" method. """ count = 0 for value in UNHANDLED_MESSAGE_2: if value == '\n': count+=1 for value in UNHANDLED_MESSAGE_1: if value == '\n': count+=1 with open(LOCATION, "r") as filereader: lines = filereader.readlines() with open(LOCATION, "w") as filereader: filereader.writelines(lines[:-count]) def main(): modify_unhandled_configuration_file() utils.tstStart(__file__) startApplication(config.AUT_NAME) #checking if the message is not in the unhandled (err) hd_simulator.cmd_send_hd_general_response(message_id = 275, accepted = 1, reason = 1) unhandled_status = get_error_message_from_log() test.verify(unhandled_status == True, "message is not in unhandled.conf file. verified error message from .err file") #checking if the message is not in the unhandled (err) hd_simulator.cmd_send_hd_general_response(message_id = 276, accepted = 1, reason = 1) unhandled_status = get_error_message_from_log() test.verify(unhandled_status == True, "message is not in unhandled.conf file. verified error message from .err file") #checking the id exists and parameters passed is not correct regarding the definition in the conf. (err) #0x1300 hd_simulator.cmd_send_hd_general_response(message_id = 19, accepted = 1, reason = 1) error_message = get_error_message_from_log() test.compare(error_message, ERROR_MESSAGE["0x1300"], "error code should be verified in log file") #checking the id exists and parameters passed is not correct regarding the definition in the conf. (err) #0x0900 hd_simulator.cmd_send_hd_general_response(message_id = 9, accepted = 0, reason = 0) error_message = get_error_message_from_log() test.compare(error_message, ERROR_MESSAGE["0x0900"], "error code should be verified in log file") #checking the number of parameters are correct but the type is not correct. (err) #0x0A00 payload = float_to_bytearray(DUMMY_FLOAT_VALUE_1) #expected negative scenario verification hd_simulator.cmd_send_hd_general_response(message_id = 10, accepted = 1, reason = 1, is_pure_data = False, has_parameters = True, parameters_payload = payload) log_data = verify_log("HD_RTC_Epoch_Data") test.verify(DUMMY_FLOAT_VALUE_1 not in log_data, "parameters type should show deviation in negative scenario.") #checking the number of parameters are correct but the type is not correct. (err) #0x7D00 payload = integer_to_bytearray(DUMMY_INT_VALUE_1) payload += float_to_bytearray(DUMMY_FLOAT_VALUE_1) payload += float_to_bytearray(DUMMY_FLOAT_VALUE_2) payload += float_to_bytearray(DUMMY_FLOAT_VALUE_1) payload += float_to_bytearray(DUMMY_INT_VALUE_2) #expected negative scenario verification hd_simulator.cmd_send_hd_general_response(message_id = 125, accepted = 1, reason = 1, is_pure_data = False, has_parameters = True, parameters_payload = payload) log_data = verify_log("HD_Alarm_Information") test.verify([DUMMY_INT_VALUE_1, DUMMY_FLOAT_VALUE_1, DUMMY_FLOAT_VALUE_2, DUMMY_FLOAT_VALUE_1, DUMMY_INT_VALUE_1] not in log_data, "parameters type should show deviation in negative scenario.") #checking everything is passed correctly. (log) #0xBB00 payload = integer_to_bytearray(DUMMY_INT_VALUE_1) payload += integer_to_bytearray(DUMMY_INT_VALUE_2) hd_simulator.cmd_send_hd_general_response(message_id = 187, accepted = 1, reason = 1, is_pure_data = False, has_parameters = True, parameters_payload = payload) unhandled_message = UNHANDLED_MESSAGE_1.split('\n') #unhandled_message[2] is the expected message in unhandled.conf after API call #unhandled_message[1] is the expected message id in unhandled.conf after API call log_data = verify_log(unhandled_message[2]) message_id = convert_message_id_from_unhandled_file_format_to_standared_format(unhandled_message[1]) test.verify(message_id.lower() in log_data, "message id should be "+str(unhandled_message[1])) test.verify([DUMMY_INT_VALUE_1, DUMMY_INT_VALUE_2] in log_data, "API arguments should be "+str([DUMMY_INT_VALUE_1, DUMMY_INT_VALUE_2])) test.verify(config.ACK_REQ_STATUS in log_data, "ack request is verified") test.verify(config.ACK_BAK_STATUS in log_data, "ack back is verified") #checking everything is passed correctly. (log) #0xAA00 payload = float_to_bytearray(DUMMY_FLOAT_VALUE_1) payload += unsigned_integer_to_bytearray(DUMMY_UNSIGNED_INT_VALUE) hd_simulator.cmd_send_hd_general_response(message_id = 170, accepted = 1, reason = 0, is_pure_data = False, has_parameters = True, parameters_payload = payload) unhandled_message = UNHANDLED_MESSAGE_2.split('\n') #unhandled_message[2] is the expected message in unhandled.conf after API call #unhandled_message[1] is the expected message id in unhandled.conf after API call log_data = verify_log(unhandled_message[2]) message_id = convert_message_id_from_unhandled_file_format_to_standared_format(unhandled_message[1]) test.verify(message_id.lower() in log_data, "message id should be "+str(unhandled_message[1])) test.verify([DUMMY_FLOAT_VALUE_1, DUMMY_UNSIGNED_INT_VALUE] in log_data, "API arguments should be "+str([DUMMY_FLOAT_VALUE_1, DUMMY_UNSIGNED_INT_VALUE, 0])) test.verify(config.ACK_REQ_STATUS in log_data, "ack request is verified") test.verify(config.ACK_BAK_STATUS in log_data, "ack back is verified") demodify_unhandled_configuration_file() utils.tstDone()