# -*- 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 hd_simulator = HDSimulator() LOCATION = '/home/denali/Projects/application/resources/settings/Messages/Unhandled.conf' UNHANDLED_MESSAGE_1 = "\n[0x2A00]\nHD_custom_data_verification_1\nU32=unsigned_32bit\nU32=argument_2\n" UNHANDLED_MESSAGE_2 = "\n[0x8C00]\nHD_custom_data_verification_2\nF32=float_argument\nS16=argument_2\n" UNHANDLED_MESSAGE_3 = "\n[0x9F00]\nHD_custom_data_verification_3\nU16=unsigned_16bit\nS32=argument_2\n" PREDEFINED_UNHANDLED_MESSAGE_1 = "\n[0x3A00]\nHD_Valves_Data\nU32=ValveID\nU32=PosID\nS16=Position\nS16=Next Position\nF32=Current\nS16=PositionC\nS16=PositionA\nS16=PositionB\nU32=PWM DC\nU32=Air Trap Valve State\n" PREDEFINED_UNHANDLED_MESSAGE_2 = "\n[0x7A00]\nDG_Flush_Data\nU32=State\nU32=Time\nU32=State Time\n" PREDEFINED_UNHANDLED_MESSAGE_3 = "\n[0x4500]\nDG_Thermistors_Data\nU32=F32=Board Temp\nU32=F32=PS1 Temp\nU32=F32=PS2 Temp\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, expected_api_argument): """ 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 expected_api_argument - (list) expected arguments for API. """ utils.waitForGUI(2) api_content = api_content.split('\n') test.log(str(api_content[2])) message_extracted = utility.get_current_log_details(message_ack = True, message_text = '~'+api_content[2]) 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('?') #step to remove square bracket and padded zeros from standard UNHANDLED_MESSAGE format. formated_meassage_id = api_content[1].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", "") test.compare(expected_meassage_id.lower(), message_extracted[2], "message id should be "+str(api_content[1])) test.compare(str(expected_api_argument), str(message_extracted[0]), "API arguments should be "+str(expected_api_argument)) test.verify(config.ACK_REQ_STATUS in message_extracted, "ack request is verified") test.verify(config.ACK_BAK_STATUS in message_extracted, "ack back is verified") 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) if "'" in row[1]: return row[1] 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) filereader.write(UNHANDLED_MESSAGE_3) 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_3: if value == '\n': count+=1 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) #0x3A00 hd_simulator.cmd_send_hd_general_response(message_id = 58, accepted = 1, reason = 1) verify_log(PREDEFINED_UNHANDLED_MESSAGE_1, [1, 1]) #0x8600 hd_simulator.cmd_send_hd_general_response(message_id = 122, accepted = 0, reason = 1) verify_log(PREDEFINED_UNHANDLED_MESSAGE_2, [0, 1]) #0x4500 hd_simulator.cmd_send_hd_general_response(message_id = 69, accepted = 1, reason = 0) verify_log(PREDEFINED_UNHANDLED_MESSAGE_3, [1.4013e-45, 0]) #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") #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") #0x2A00 hd_simulator.cmd_send_hd_general_response(message_id = 42, accepted = 1, reason = 0) verify_log(UNHANDLED_MESSAGE_1, [1, 0]) #0x8C00 hd_simulator.cmd_send_hd_general_response(message_id = 140, accepted = 0, reason = 0) verify_log(UNHANDLED_MESSAGE_2, [0, 0]) #0x9F00 hd_simulator.cmd_send_hd_general_response(message_id = 159, accepted = 0, reason = 4) verify_log(UNHANDLED_MESSAGE_3, [0, 262144]) demodify_unhandled_configuration_file() utils.tstDone()