1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3 4# Copyright (c) 2023 Huawei Device Co., Ltd. 5# 6# HDF is dual licensed: you can use it either under the terms of 7# the GPL, or the BSD license, at your option. 8# See the LICENSE file in the root of this repository for complete details. 9 10import os 11import subprocess 12import time 13 14 15def get_time_stamp(): 16 return int(round(time.time() * 1000)) 17 18 19def print_success(info): 20 print("\033[32m{}\033[0m".format(info)) 21 22 23def print_failure(info): 24 print("\033[31m{}\033[0m".format(info)) 25 26 27def compare_file(first_file, second_file): 28 with open(first_file, 'r') as first_hash_file: 29 with open(second_file, 'r') as second_hash_file: 30 first_hash_info = first_hash_file.read() 31 second_hash_info = second_hash_file.read() 32 return first_hash_info == second_hash_info 33 34 35def exec_command(command): 36 return subprocess.getstatusoutput(command) 37 38 39def file_exists(file_path): 40 return os.path.isfile(file_path) 41 42 43class Test: 44 def __init__(self, name): 45 self.name = name 46 47 def run(self): 48 # please add test code here 49 return False 50 51 def test(self): 52 print_success("[ RUN ] {}".format(self.name)) 53 start_time = get_time_stamp() 54 result = self.run() 55 end_time = get_time_stamp() 56 57 if result: 58 print_success("[ OK ] {} ({}ms)".format(self.name, end_time - start_time)) 59 else: 60 print_failure("[ FAILED ] {} ({}ms)".format(self.name, end_time - start_time)) 61 return result 62 63 64# get hash key and print standard ouput 65class TestHashGood1(Test): 66 def run(self): 67 result_hash_file_path = "./good/hash.txt" 68 command = "../../hdi-gen -D ./good/ -r ohos.hdi:./good/ --hash" 69 status, exec_result = exec_command(command) 70 if status != 0: 71 print(exec_result) 72 return False 73 temp_hash_info = exec_result 74 75 with open(result_hash_file_path, 'r') as result_hash_file: 76 result_hash_info = result_hash_file.read().rstrip() 77 return temp_hash_info == result_hash_info 78 79 80# get hash key and print file 81class TestHashGood2(Test): 82 def run(self): 83 result_hash_file_path = "./good/hash.txt" 84 temp_hash_file_path = "./good/temp.txt" 85 command = "../../hdi-gen -D ./good/ -r ohos.hdi:./good/ --hash -o {}".format(temp_hash_file_path) 86 status, result = exec_command(command) 87 if status != 0: 88 print(result) 89 return False 90 91 result = False 92 if compare_file(temp_hash_file_path, result_hash_file_path): 93 result = True 94 exec_command("rm -f ./good/temp.txt") 95 return result 96 97 98# nothing idl files 99class TestBadHash01(Test): 100 def run(self): 101 command = "../../hdi-gen -D ./bad_01/ -r ohos.hdi:./bad_01/ --hash" 102 status, _ = exec_command(command) 103 return status != 0 104 105 106# empty idl file 107class TestBadHash02(Test): 108 def run(self): 109 command = "../../hdi-gen -D ./bad_02/ -r ohos.hdi:./bad_02/ --hash" 110 status, _ = exec_command(command) 111 return status != 0 112 113 114# the idl file has no package name 115class TestBadHash03(Test): 116 def run(self): 117 command = "../../hdi-gen -D ./bad_03/ -r ohos.hdi:./bad_03/ --hash" 118 status, _ = exec_command(command) 119 return status != 0 120 121 122# the idl file has error package name 123class TestBadHash04(Test): 124 def run(self): 125 command = "../../hdi-gen -D ./bad_04/ -r ohos.hdi:./bad_04/ --hash" 126 status, _ = exec_command(command) 127 return status != 0 128 129 130class Tests: 131 test_cases = [ 132 TestHashGood1("TestHashPrintStd"), 133 TestHashGood2("TestHashPrintFile"), 134 TestBadHash01("TestBadHashWithNoFile"), 135 TestBadHash02("TestBadHashWithEmptyFile"), 136 TestBadHash03("TestBadHashWithNoPackage"), 137 TestBadHash04("TestBadHashWithErrorPackage"), 138 ] 139 140 @staticmethod 141 def set_up_test_case(): 142 hdi_gen_file = "../../hdi-gen" 143 ret = file_exists(hdi_gen_file) 144 if not ret: 145 print_failure("[===========] no such file:{}".format(hdi_gen_file)) 146 return ret 147 148 @staticmethod 149 def set_down_test_case(): 150 pass 151 152 @staticmethod 153 def test(): 154 test_case_num = len(Tests.test_cases) 155 success_case_num = 0 156 print_success("[===========] start {} test".format(test_case_num)) 157 for test_case in Tests.test_cases: 158 if test_case.test(): 159 success_case_num += 1 160 print_success("[ PASSED ] {} test".format(success_case_num)) 161 failure_case_num = test_case_num - success_case_num 162 if failure_case_num > 0: 163 print_failure("[ FAILED ] {} test".format(failure_case_num)) 164 165 166if __name__ == "__main__": 167 if not Tests.set_up_test_case(): 168 exit(-1) 169 Tests.test() 170 Tests.set_down_test_case() 171