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.
15import os
16import tempfile
17import unittest
18import zipfile
19
20from test.fake_data import PARTITION_FILE_CONVERSION_FAILED
21from test.fake_data import UPDATER_SPECIFIED_CONFIG_REPEATS
22from utils import unzip_package
23from utils import clear_resource
24from utils import OPTIONS_MANAGER
25from utils import parse_update_config
26from utils import get_update_info
27from utils import VERSION_MBN_PATH
28from utils import BOARD_LIST_PATH
29from utils import UPDATER_CONFIG
30from utils import parse_partition_file_xml
31from patch_package_process import PatchProcess
32from transfers_manager import ActionInfo
33from transfers_manager import ActionType
34from blocks_manager import BlocksManager
35
36
37class TestUtils(unittest.TestCase):
38
39    def setUp(self):
40        print("set up")
41
42    def tearDown(self):
43        print("tear down")
44
45    def test_unzip_package(self):
46        """
47        unzip_package, Missing updater_config dir.
48        :return:
49        """
50        target_package = "test.zip"
51        zip_obj = zipfile.ZipFile(target_package, "w")
52        os.makedirs('test_dir')
53        zip_obj.write('test_dir', 'test_dir')
54        zip_obj.close()
55        check_re = unzip_package(target_package, origin='target')
56        os.remove(target_package)
57        os.rmdir('test_dir')
58        self.assertEqual(check_re, (False, False))
59
60        target_package = "test.zip"
61        zip_obj = zipfile.ZipFile(target_package, "w")
62        os.makedirs(UPDATER_CONFIG)
63        with open('test.file', 'w') as w_f:
64            w_f.write('test')
65        zip_obj.write(UPDATER_CONFIG, UPDATER_CONFIG)
66        zip_obj.write('test.file', 'test.file')
67        zip_obj.close()
68        check_re = unzip_package(target_package, origin='target')
69        os.remove(target_package)
70        os.rmdir(UPDATER_CONFIG)
71        os.remove('test.file')
72        self.assertEqual((type(check_re[0]), os.path.exists(check_re[1])),
73                         (tempfile.TemporaryDirectory, True))
74
75    def test_parse_update_config(self):
76        """
77        parse_update_config, return False
78        :return:
79        """
80        with open("./updater_specified_config_repeats.xml", "wb") as w_f:
81            w_f.write(UPDATER_SPECIFIED_CONFIG_REPEATS.encode())
82        check_re = parse_update_config("test.xml")
83        clear_resource()
84        self.assertEqual(
85            check_re, [False, False, False, False, False, False, False])
86
87        OPTIONS_MANAGER.target_package_dir = "./"
88        check_re = parse_update_config(
89            "./updater_specified_config_repeats.xml")
90        clear_resource()
91        self.assertEqual(
92            check_re, [False, False, False, False, False, False, False])
93        if os.path.exists("./updater_specified_config_repeats.xml"):
94            os.remove("./updater_specified_config_repeats.xml")
95
96    def test_clear_resource(self):
97        """
98        clear_resource, Clean up resources,
99        OPTIONS_MANAGER.update_package_file_path
100        :return:
101        """
102        with open('test.file', 'w') as w_f:
103            w_f.write('test')
104        OPTIONS_MANAGER.update_package_file_path = 'test.file'
105        clear_resource(err_clear=True)
106        clear_re = os.path.exists('test.file')
107        clear_resource()
108        self.assertEqual(clear_re, False)
109
110    def test_get_update_info(self):
111        """
112        get_update_info, return False
113        :return:
114        """
115        OPTIONS_MANAGER.target_package_config_dir = ""
116        check_re = get_update_info()
117        clear_resource()
118        self.assertEqual(check_re, False)
119
120        with open(VERSION_MBN_PATH, 'w') as w_f:
121            w_f.write('test content')
122        OPTIONS_MANAGER.target_package_config_dir = ""
123        check_re = get_update_info()
124        clear_resource()
125        if os.path.exists(VERSION_MBN_PATH):
126            os.remove(VERSION_MBN_PATH)
127        self.assertEqual(check_re, False)
128
129    def test_get_update_info_2(self):
130        """
131        get_update_info,parse_update_config, return False
132        :return:
133        """
134        with open(VERSION_MBN_PATH, 'w') as w_f:
135            w_f.write('test content')
136        with open(BOARD_LIST_PATH, 'w') as w_f:
137            w_f.write('test content')
138        OPTIONS_MANAGER.target_package_config_dir = ""
139        check_re = get_update_info()
140        clear_resource()
141        if os.path.exists(VERSION_MBN_PATH):
142            os.remove(VERSION_MBN_PATH)
143        if os.path.exists(BOARD_LIST_PATH):
144            os.remove(BOARD_LIST_PATH)
145        self.assertEqual(check_re, False)
146
147    def test_parse_partition_file_xml(self):
148        """
149        parse_partition_file_xml, return False
150        :return:
151        """
152        with open("./partition_file_conversion_failed.xml", "wb") as w_f:
153            w_f.write(PARTITION_FILE_CONVERSION_FAILED)
154        check_re = parse_partition_file_xml(
155            "./partition_file_conversion_failed.xml")
156        clear_resource()
157        self.assertEqual(check_re, (False, False, False))
158        if os.path.exists("./partition_file_conversion_failed.xml"):
159            os.remove("./partition_file_conversion_failed.xml")
160
161    def test_apply_zero_type(self):
162        """
163        apply_zero_type
164        :return:
165        """
166        patch_process = PatchProcess("vendor", None, None, [])
167        action_obj = ActionInfo(
168                        ActionType.ZERO, "tgt_file_name", "__ZERO",
169                        BlocksManager("0-5"), BlocksManager("0-5"))
170        transfer_content = []
171        patch_process.apply_zero_type(action_obj, 0, transfer_content)
172        check_re = len(transfer_content) == 0
173        clear_resource()
174        self.assertEqual(check_re, True)
175