1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# Copyright (c) 2021 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import os 17import unittest 18from collections import OrderedDict 19 20from test.create_package_data import create_input_package 21from test.create_package_data import clear_package 22from utils import OPTIONS_MANAGER 23from utils import clear_resource 24from utils import VERSION_MBN_PATH 25from utils import BOARD_LIST_PATH 26from update_package import get_hash_content 27from update_package import signing_package 28from script_generator import PreludeScript 29from script_generator import VerseScript 30from script_generator import RefrainScript 31from script_generator import EndingScript 32from update_package import build_update_package 33from update_package import get_component_list 34 35 36def fail_test_case_1(): 37 OPTIONS_MANAGER.hash_algorithm = 'sha256' 38 OPTIONS_MANAGER.target_package_config_dir = \ 39 "./test_target_package/updater_config" 40 OPTIONS_MANAGER.target_package_dir = \ 41 "./test_target_package/" 42 with open(VERSION_MBN_PATH) as om_second_f: 43 OPTIONS_MANAGER.total_script_file_obj = om_second_f 44 OPTIONS_MANAGER.full_img_list = [] 45 OPTIONS_MANAGER.opera_script_file_name_dic = {} 46 OPTIONS_MANAGER.private_key = "../" 47 OPTIONS_MANAGER.product = 'Hi3516' 48 prelude_script = PreludeScript() 49 verse_script = VerseScript() 50 refrain_script = RefrainScript() 51 ending_script = EndingScript() 52 check_re = build_update_package( 53 False, 'test_dir', prelude_script, verse_script, 54 refrain_script, ending_script) 55 return check_re 56 57 58def fail_test_case_2(): 59 OPTIONS_MANAGER.hash_algorithm = 'sha256' 60 OPTIONS_MANAGER.target_package_config_dir = "" 61 OPTIONS_MANAGER.target_package_dir = \ 62 "./test_target_package/" 63 with open(VERSION_MBN_PATH) as om_third_f: 64 OPTIONS_MANAGER.total_script_file_obj = om_third_f 65 OPTIONS_MANAGER.full_img_list = [] 66 OPTIONS_MANAGER.opera_script_file_name_dic = {} 67 OPTIONS_MANAGER.private_key = "../" 68 OPTIONS_MANAGER.product = 'Hi3516' 69 verse_script = VerseScript() 70 check_re = build_update_package( 71 False, 'test_dir', prelude_script, verse_script, 72 refrain_script, ending_script) 73 return check_re 74 75 76class TestUpdatePackage(unittest.TestCase): 77 78 def setUp(self): 79 print("set up") 80 81 def tearDown(self): 82 print("tear down") 83 84 def test_create_update_bin_failed(self): 85 """ 86 create_update_bin, Failed to generate bin 87 :return: 88 """ 89 90 OPTIONS_MANAGER.head_info_list = \ 91 ["01", "123456", "Hi3516DV300-eng 10 QP1A.190711.020", 92 "2021-01-23", "12:30"] 93 OPTIONS_MANAGER.component_info_dict = \ 94 {"version_list": ["1"] * 5, "board_list": ["1"] * 5, 95 'vendor': ["1"] * 5} 96 with open(VERSION_MBN_PATH, 'w') as w_f: 97 w_f.write('test content') 98 with open(BOARD_LIST_PATH, 'w') as w_f: 99 w_f.write('test content') 100 with open(VERSION_MBN_PATH) as om_first_f: 101 OPTIONS_MANAGER.full_image_file_obj_list = [om_first_f] 102 OPTIONS_MANAGER.full_img_list = ['vendor'] 103 OPTIONS_MANAGER.incremental_img_list = [] 104 OPTIONS_MANAGER.hash_algorithm = 'test_algo' 105 OPTIONS_MANAGER.target_package_config_dir = "" 106 OPTIONS_MANAGER.version_mbn_file_path = VERSION_MBN_PATH 107 OPTIONS_MANAGER.board_list_file_path = BOARD_LIST_PATH 108 109 get_component_list( 110 OPTIONS_MANAGER.full_image_file_obj_list, 111 OrderedDict([('version_list', ['1', '1', '1', '1', '1']), 112 ('board_list', ['1', '1', '1', '1', '1'])])) 113 114 create_input_package( 115 "test_target_package", package_type="source") 116 117 check_re = fail_test_case_1() 118 self.assertEqual(check_re, False) 119 120 check_re = fail_test_case_2() 121 self.assertEqual(check_re, False) 122 123 if os.path.exists(VERSION_MBN_PATH): 124 os.remove(VERSION_MBN_PATH) 125 if os.path.exists(BOARD_LIST_PATH): 126 os.remove(BOARD_LIST_PATH) 127 clear_resource() 128 clear_package("test_target_package") 129 130 def test_get_hash_content(self): 131 """ 132 get_hash_content, Get hash. 133 :return: 134 """ 135 with self.assertRaises(RuntimeError): 136 get_hash_content("non_existent.file", 'sha256') 137 138 re_str = get_hash_content("non_existent.file", 'test_sha') 139 check_re = re_str is False 140 self.assertEqual(check_re, True) 141 clear_resource() 142 143 def test_signing_package(self): 144 """ 145 test signing_package 146 :return: 147 """ 148 package_path = 'test.zip' 149 with self.assertRaises(OSError): 150 signing_package(package_path, "sha256", 151 position=1, package_type='.bin') 152 153 with open(package_path, 'wb') as w_f: 154 w_f.write('test content'.encode()) 155 check_re = signing_package(package_path, "sha256", 156 position=1, package_type='.bin') 157 self.assertEqual(check_re, True) 158 os.remove(package_path) 159