• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2# Copyright (C) 2020 The Android Open Source Project
3#
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#
16
17import copy
18import zipfile
19
20import common
21import test_utils
22
23from non_ab_ota import NonAbOtaPropertyFiles, WriteFingerprintAssertion
24from test_utils import PropertyFilesTestCase
25
26
27class NonAbOtaPropertyFilesTest(PropertyFilesTestCase):
28  """Additional validity checks specialized for NonAbOtaPropertyFiles."""
29  def setUp(self):
30     common.OPTIONS.no_signing = False
31  def test_init(self):
32    property_files = NonAbOtaPropertyFiles()
33    self.assertEqual('ota-property-files', property_files.name)
34    self.assertEqual((), property_files.required)
35    self.assertEqual((), property_files.optional)
36
37  def test_Compute(self):
38    entries = ()
39    zip_file = self.construct_zip_package(entries)
40    property_files = NonAbOtaPropertyFiles()
41    with zipfile.ZipFile(zip_file) as zip_fp:
42      property_files_string = property_files.Compute(zip_fp)
43
44    tokens = self._parse_property_files_string(property_files_string)
45    self.assertEqual(2, len(tokens))
46    self._verify_entries(zip_file, tokens, entries)
47
48  def test_Finalize(self):
49    entries = [
50        'META-INF/com/android/metadata',
51        'META-INF/com/android/metadata.pb',
52    ]
53    zip_file = self.construct_zip_package(entries)
54    property_files = NonAbOtaPropertyFiles()
55    with zipfile.ZipFile(zip_file) as zip_fp:
56      raw_metadata = property_files.GetPropertyFilesString(
57          zip_fp, reserve_space=False)
58      property_files_string = property_files.Finalize(zip_fp, len(raw_metadata))
59    tokens = self._parse_property_files_string(property_files_string)
60
61    self.assertEqual(2, len(tokens))
62    # 'META-INF/com/android/metadata' will be key'd as 'metadata'.
63    entries[0] = 'metadata'
64    entries[1] = 'metadata.pb'
65    self._verify_entries(zip_file, tokens, entries)
66
67  def test_Verify(self):
68    entries = (
69        'META-INF/com/android/metadata',
70        'META-INF/com/android/metadata.pb',
71    )
72    zip_file = self.construct_zip_package(entries)
73    property_files = NonAbOtaPropertyFiles()
74    with zipfile.ZipFile(zip_file) as zip_fp:
75      raw_metadata = property_files.GetPropertyFilesString(
76          zip_fp, reserve_space=False)
77
78      property_files.Verify(zip_fp, raw_metadata)
79
80class NonAbOTATest(test_utils.ReleaseToolsTestCase):
81  TEST_TARGET_INFO_DICT = {
82      'build.prop': common.PartitionBuildProps.FromDictionary(
83          'system', {
84              'ro.product.device': 'product-device',
85              'ro.build.fingerprint': 'build-fingerprint-target',
86              'ro.build.version.incremental': 'build-version-incremental-target',
87              'ro.build.version.sdk': '27',
88              'ro.build.version.security_patch': '2017-12-01',
89              'ro.build.date.utc': '1500000000'}
90      )
91  }
92  TEST_INFO_DICT_USES_OEM_PROPS = {
93      'build.prop': common.PartitionBuildProps.FromDictionary(
94          'system', {
95              'ro.product.name': 'product-name',
96              'ro.build.thumbprint': 'build-thumbprint',
97              'ro.build.bar': 'build-bar'}
98      ),
99      'vendor.build.prop': common.PartitionBuildProps.FromDictionary(
100          'vendor', {
101               'ro.vendor.build.fingerprint': 'vendor-build-fingerprint'}
102      ),
103      'property1': 'value1',
104      'property2': 4096,
105      'oem_fingerprint_properties': 'ro.product.device ro.product.brand',
106  }
107  TEST_OEM_DICTS = [
108      {
109          'ro.product.brand': 'brand1',
110          'ro.product.device': 'device1',
111      },
112      {
113          'ro.product.brand': 'brand2',
114          'ro.product.device': 'device2',
115      },
116      {
117          'ro.product.brand': 'brand3',
118          'ro.product.device': 'device3',
119      },
120  ]
121  def test_WriteFingerprintAssertion_without_oem_props(self):
122    target_info = common.BuildInfo(self.TEST_TARGET_INFO_DICT, None)
123    source_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
124    source_info_dict['build.prop'].build_props['ro.build.fingerprint'] = (
125        'source-build-fingerprint')
126    source_info = common.BuildInfo(source_info_dict, None)
127
128    script_writer = test_utils.MockScriptWriter()
129    WriteFingerprintAssertion(script_writer, target_info, source_info)
130    self.assertEqual(
131        [('AssertSomeFingerprint', 'source-build-fingerprint',
132          'build-fingerprint-target')],
133        script_writer.lines)
134
135  def test_WriteFingerprintAssertion_with_source_oem_props(self):
136    target_info = common.BuildInfo(self.TEST_TARGET_INFO_DICT, None)
137    source_info = common.BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
138                                   self.TEST_OEM_DICTS)
139
140    script_writer = test_utils.MockScriptWriter()
141    WriteFingerprintAssertion(script_writer, target_info, source_info)
142    self.assertEqual(
143        [('AssertFingerprintOrThumbprint', 'build-fingerprint-target',
144          'build-thumbprint')],
145        script_writer.lines)
146
147  def test_WriteFingerprintAssertion_with_target_oem_props(self):
148    target_info = common.BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
149                                   self.TEST_OEM_DICTS)
150    source_info = common.BuildInfo(self.TEST_TARGET_INFO_DICT, None)
151
152    script_writer = test_utils.MockScriptWriter()
153    WriteFingerprintAssertion(script_writer, target_info, source_info)
154    self.assertEqual(
155        [('AssertFingerprintOrThumbprint', 'build-fingerprint-target',
156          'build-thumbprint')],
157        script_writer.lines)
158
159  def test_WriteFingerprintAssertion_with_both_oem_props(self):
160    target_info = common.BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
161                                   self.TEST_OEM_DICTS)
162    source_info_dict = copy.deepcopy(self.TEST_INFO_DICT_USES_OEM_PROPS)
163    source_info_dict['build.prop'].build_props['ro.build.thumbprint'] = (
164        'source-build-thumbprint')
165    source_info = common.BuildInfo(source_info_dict, self.TEST_OEM_DICTS)
166
167    script_writer = test_utils.MockScriptWriter()
168    WriteFingerprintAssertion(script_writer, target_info, source_info)
169    self.assertEqual(
170        [('AssertSomeThumbprint', 'build-thumbprint',
171          'source-build-thumbprint')],
172        script_writer.lines)
173