1#!/usr/bin/env python3
2#
3# Copyright (C) 2018 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""
18Tests gen_update_config.py.
19
20Example:
21    $ PYTHONPATH=$ANDROID_BUILD_TOP/build/make/tools/releasetools:$PYTHONPATH \\
22        python3 -m unittest test_gen_update_config
23"""
24
25import os.path
26import unittest
27from gen_update_config import GenUpdateConfig
28
29
30class GenUpdateConfigTest(unittest.TestCase): # pylint: disable=missing-docstring
31
32    def test_ab_install_type_streaming(self):
33        """tests if streaming property files' offset and size are generated properly"""
34        config, package = self._generate_config()
35        property_files = config['ab_config']['property_files']
36        self.assertEqual(len(property_files), 6)
37        with open(package, 'rb') as pkg_file:
38            for prop in property_files:
39                filename, offset, size = prop['filename'], prop['offset'], prop['size']
40                pkg_file.seek(offset)
41                raw_data = pkg_file.read(size)
42                if filename in ['payload.bin', 'payload_metadata.bin']:
43                    pass
44                elif filename == 'payload_properties.txt':
45                    pass
46                elif filename == 'metadata':
47                    self.assertEqual(raw_data.decode('ascii'), 'META-INF/COM/ANDROID/METADATA')
48                else:
49                    expected_data = filename.replace('.', '-').upper()
50                    self.assertEqual(raw_data.decode('ascii'), expected_data)
51
52    @staticmethod
53    def _generate_config():
54        """Generates JSON config from ota_002_package.zip."""
55        ota_package = os.path.join(os.path.dirname(__file__),
56                                   '../tests/res/raw/ota_002_package.zip')
57        gen = GenUpdateConfig(ota_package,
58                              'file:///foo.bar',
59                              GenUpdateConfig.AB_INSTALL_TYPE_STREAMING,
60                              True,  # ab_force_switch_slot
61                              True)  # ab_verify_payload_metadata
62        gen.run()
63        return gen.config, ota_package
64