# -*- 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 from time import sleep hd_simulator = HDSimulator() LOCATION = '/home/denali/Projects/unittests/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 = { 'MESSAGE_1' : "\n[0xBB00]\nHD_custom_data_verification_1\nU32=unsigned_32bit\nU32=unsigned_int\n", 'MESSAGE_2' : "\n[0xAA00]\nHD_custom_data_verification_2\nF32=float_argument\nS32=string\n", 'MESSAGE_3' : "\n[0xCC00]\nHD_custom_data_verification_3\nS32=string_argument\nS32=string\n", 'MESSAGE_4' : "\n[0xDD00]\nHD_custom_data_verification_4\nS16string_argument\nS16=string\n", 'MESSAGE_5' : "\n[0xEE00]\nHD_custom_data_verification_5\nU16=unsigned_int_argument\nu16=integer\n", 'MESSAGE_6' : "\n[0xFF00]\nHD_custom_data_verification_6\nS08=string_argument\nS08=integer\n", 'MESSAGE_7' : "\n[0xAF00]\nHD_custom_data_verification_7\nU08=unsigned_int_argument\nU08=integer\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'", "UNHANDLED_API" : "Unhandled Message ID (HD)", "DATA_LENGTH_MISMATCH": "N5Types3S32E", "SD_CARD_STORAGE_WARNING" : "SD-CARD space lower than 10%" } 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) parameter_index = 0 #loop to remove default arguments present in API, here it is '?' while('?' in message_extracted[parameter_index]): message_extracted[parameter_index].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))): print(row) # 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 row[1] != None and row[1] != ERROR_MESSAGE['SD_CARD_STORAGE_WARNING']: return row[1] 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 append_unhandled_configuration_file(): """ This function is capable to write custom API's on unhandled.conf folder. unhandled.conf location = '/home/denali/Projects/unittests/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 deappend_unhandled_configuration_file() at the end of the testcase. """ with open(LOCATION, "a") as filereader: filereader.write(UNHANDLED_MESSAGE['MESSAGE_1']) filereader.write(UNHANDLED_MESSAGE['MESSAGE_2']) filereader.write(UNHANDLED_MESSAGE['MESSAGE_3']) filereader.write(UNHANDLED_MESSAGE['MESSAGE_4']) filereader.write(UNHANDLED_MESSAGE['MESSAGE_5']) filereader.write(UNHANDLED_MESSAGE['MESSAGE_6']) filereader.write(UNHANDLED_MESSAGE['MESSAGE_7']) utils.waitForGUI(5) def remove_unhandled_configuration_file(): """ This function is capable to remove custom API's which is written during method "append_unhandled_configuration_file()". unhandled.conf location = '/home/denali/Projects/unittests/resources/settings/Messages/Unhandled.conf' NB: it is a single time callable method should use only after calling "append_unhandled_configuration_file()" method. """ count = 0 for value in UNHANDLED_MESSAGE['MESSAGE_7']: if value == '\n': count+=1 for value in UNHANDLED_MESSAGE['MESSAGE_6']: if value == '\n': count+=1 for value in UNHANDLED_MESSAGE['MESSAGE_5']: if value == '\n': count+=1 for value in UNHANDLED_MESSAGE['MESSAGE_4']: if value == '\n': count+=1 for value in UNHANDLED_MESSAGE['MESSAGE_3']: if value == '\n': count+=1 for value in UNHANDLED_MESSAGE['MESSAGE_2']: if value == '\n': count+=1 for value in UNHANDLED_MESSAGE['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(): append_unhandled_configuration_file() utils.tstStart(__file__) startApplication(config.AUT_NAME_ONLY+ " -d") #checking if the message is not in the unhandled (err) test.startSection("verify the message is not in unhandled (err)") hd_simulator.cmd_send_hd_general_response(message_id = 350, accepted = 1, reason = 1) unhandled_message = get_error_message_from_log() test.log(str(unhandled_message)) test.compare(unhandled_message, ERROR_MESSAGE["UNHANDLED_API"], "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 = 351, accepted = 1, reason = 1) unhandled_status = get_error_message_from_log() test.compare(unhandled_message, ERROR_MESSAGE["UNHANDLED_API"], "message is not in unhandled.conf file. verified error message from .err file") test.endSection() #checking the id exists and parameters passed is not correct regarding the definition in the conf. (err) test.startSection("verify 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") test.endSection() #checking the number of parameters are correct but the type is not correct. (err) test.startSection("verify 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.") test.endSection() #checking everything is passed correctly. (log) test.startSection("verify the log is passed correctly.") #0xBB00 messageID_index = 1 message_index = 2 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['MESSAGE_1'].split('\n') #unhandled_message[message_index] is the expected message in unhandled.conf after API call #unhandled_message[messageID_index] is the expected message id in unhandled.conf after API call log_data = verify_log(unhandled_message[message_index]) message_id = convert_message_id_from_unhandled_file_format_to_standared_format(unhandled_message[messageID_index]) test.verify(message_id.lower() in log_data, "message id should be "+str(unhandled_message[messageID_index])) 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['MESSAGE_2'].split('\n') #unhandled_message[2] is the expected message in unhandled.conf after API call #unhandled_message[messageID_index] is the expected message id in unhandled.conf after API call log_data = verify_log(unhandled_message[message_index]) message_id = convert_message_id_from_unhandled_file_format_to_standared_format(unhandled_message[messageID_index]) test.verify(message_id.lower() in log_data, "message id should be "+str(unhandled_message[messageID_index])) 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") #checking everything is passed correctly. (log) #0xCC00 payload = integer_to_bytearray(DUMMY_INT_VALUE_1) hd_simulator.cmd_send_hd_general_response(message_id = 204, accepted = 1, reason = 0, is_pure_data = False, has_parameters = True, parameters_payload = payload) unhandled_message =UNHANDLED_MESSAGE['MESSAGE_3'].split('\n') #unhandled_message[2] is the expected message in unhandled.conf after API call #unhandled_message[messageID_index] is the expected message id in unhandled.conf after API call log_data = verify_log(unhandled_message[message_index]) message_id = convert_message_id_from_unhandled_file_format_to_standared_format(unhandled_message[messageID_index]) test.verify(message_id.lower() in log_data, "message id should be "+str(unhandled_message[messageID_index])) test.verify([DUMMY_INT_VALUE_1] in log_data, "API arguments should be "+str([DUMMY_INT_VALUE_1, 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") test.endSection() remove_unhandled_configuration_file() utils.tstDone()