• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
36class TestUpdatePackage(unittest.TestCase):
37
38    def setUp(self):
39        print("set up")
40
41    def tearDown(self):
42        print("tear down")
43
44    def test_create_update_bin_failed(self):
45        """
46        create_update_bin, Failed to generate bin
47        :return:
48        """
49
50        OPTIONS_MANAGER.head_info_list = \
51            ["01", "123456", "Hi3516DV300-eng 10 QP1A.190711.020",
52             "2021-01-23", "12:30"]
53        OPTIONS_MANAGER.component_info_dict = \
54            {"version_list": ["1"] * 5, "board_list": ["1"] * 5,
55             'vendor': ["1"] * 5}
56        with open(VERSION_MBN_PATH, 'w') as w_f:
57            w_f.write('test content')
58        with open(BOARD_LIST_PATH, 'w') as w_f:
59            w_f.write('test content')
60        with open(VERSION_MBN_PATH) as om_first_f:
61            OPTIONS_MANAGER.full_image_file_obj_list = [om_first_f]
62        OPTIONS_MANAGER.full_img_list = ['vendor']
63        OPTIONS_MANAGER.incremental_img_list = []
64        OPTIONS_MANAGER.hash_algorithm = 'test_algo'
65        OPTIONS_MANAGER.target_package_config_dir = ""
66        OPTIONS_MANAGER.version_mbn_file_path = VERSION_MBN_PATH
67        OPTIONS_MANAGER.board_list_file_path = BOARD_LIST_PATH
68
69        get_component_list(
70            OPTIONS_MANAGER.full_image_file_obj_list,
71            OrderedDict([('version_list', ['1', '1', '1', '1', '1']),
72                         ('board_list', ['1', '1', '1', '1', '1'])]))
73
74        create_input_package(
75            "test_target_package", package_type="source")
76
77        OPTIONS_MANAGER.hash_algorithm = 'sha256'
78        OPTIONS_MANAGER.target_package_config_dir = \
79            "./test_target_package/updater_config"
80        OPTIONS_MANAGER.target_package_dir = \
81            "./test_target_package/"
82        with open(VERSION_MBN_PATH) as om_second_f:
83            OPTIONS_MANAGER.total_script_file_obj = om_second_f
84        OPTIONS_MANAGER.full_img_list = []
85        OPTIONS_MANAGER.opera_script_file_name_dic = {}
86        OPTIONS_MANAGER.private_key = "../"
87        OPTIONS_MANAGER.product = 'Hi3516'
88        prelude_script = PreludeScript()
89        verse_script = VerseScript()
90        refrain_script = RefrainScript()
91        ending_script = EndingScript()
92        check_re = build_update_package(
93            False, 'test_dir', prelude_script, verse_script,
94            refrain_script, ending_script)
95        self.assertEqual(check_re, False)
96
97        OPTIONS_MANAGER.hash_algorithm = 'sha256'
98        OPTIONS_MANAGER.target_package_config_dir = ""
99        OPTIONS_MANAGER.target_package_dir = \
100            "./test_target_package/"
101        with open(VERSION_MBN_PATH) as om_third_f:
102            OPTIONS_MANAGER.total_script_file_obj = om_third_f
103        OPTIONS_MANAGER.full_img_list = []
104        OPTIONS_MANAGER.opera_script_file_name_dic = {}
105        OPTIONS_MANAGER.private_key = "../"
106        OPTIONS_MANAGER.product = 'Hi3516'
107        verse_script = VerseScript()
108        check_re = build_update_package(
109            False, 'test_dir', prelude_script, verse_script,
110            refrain_script, ending_script)
111        self.assertEqual(check_re, False)
112
113        if os.path.exists(VERSION_MBN_PATH):
114            os.remove(VERSION_MBN_PATH)
115        if os.path.exists(BOARD_LIST_PATH):
116            os.remove(BOARD_LIST_PATH)
117        clear_resource()
118        clear_package("test_target_package")
119
120    def test_get_hash_content(self):
121        """
122        get_hash_content, Get hash.
123        :return:
124        """
125        with self.assertRaises(RuntimeError):
126            get_hash_content("non_existent.file", 'sha256')
127
128        re_str = get_hash_content("non_existent.file", 'test_sha')
129        check_re = re_str is False
130        self.assertEqual(check_re, True)
131        clear_resource()
132
133    def test_signing_package(self):
134        """
135        test signing_package
136        :return:
137        """
138        package_path = 'test.zip'
139        with self.assertRaises(OSError):
140            signing_package(package_path, "sha256",
141                            position=1, package_type='.bin')
142
143        with open(package_path, 'wb') as w_f:
144            w_f.write('test content'.encode())
145        check_re = signing_package(package_path, "sha256",
146                                   position=1, package_type='.bin')
147        self.assertEqual(check_re, True)
148        os.remove(package_path)
149