1# Copyright (C) 2008 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15
16import os
17import tempfile
18import test_utils
19import merge_ota
20import update_payload
21from update_metadata_pb2 import DynamicPartitionGroup
22from update_metadata_pb2 import DynamicPartitionMetadata
23from test_utils import SkipIfExternalToolsUnavailable, ReleaseToolsTestCase
24
25
26class MergeOtaTest(ReleaseToolsTestCase):
27  def setUp(self) -> None:
28    self.testdata_dir = test_utils.get_testdata_dir()
29    return super().setUp()
30
31  @SkipIfExternalToolsUnavailable()
32  def test_MergeThreeOtas(self):
33    ota1 = os.path.join(self.testdata_dir, "tuna_vbmeta.zip")
34    ota2 = os.path.join(self.testdata_dir, "tuna_vbmeta_system.zip")
35    ota3 = os.path.join(self.testdata_dir, "tuna_vbmeta_vendor.zip")
36    payloads = [update_payload.Payload(ota) for ota in [ota1, ota2, ota3]]
37    with tempfile.NamedTemporaryFile() as output_file:
38      merge_ota.main(["merge_ota", "-v", ota1, ota2, ota3,
39                     "--output", output_file.name])
40      payload = update_payload.Payload(output_file.name)
41      partition_names = [
42          part.partition_name for part in payload.manifest.partitions]
43      self.assertEqual(partition_names, [
44                       "vbmeta", "vbmeta_system", "vbmeta_vendor"])
45      payload.CheckDataHash()
46      for i in range(3):
47        self.assertEqual(payload.manifest.partitions[i].old_partition_info,
48                         payloads[i].manifest.partitions[0].old_partition_info)
49        self.assertEqual(payload.manifest.partitions[i].new_partition_info,
50                         payloads[i].manifest.partitions[0].new_partition_info)
51
52  def test_MergeDAPSnapshotDisabled(self):
53    dap1 = DynamicPartitionMetadata()
54    dap2 = DynamicPartitionMetadata()
55    merged_dap = DynamicPartitionMetadata()
56    dap1.snapshot_enabled = True
57    dap2.snapshot_enabled = False
58    merge_ota.MergeDynamicPartitionMetadata(merged_dap, dap1)
59    merge_ota.MergeDynamicPartitionMetadata(merged_dap, dap2)
60    self.assertFalse(merged_dap.snapshot_enabled)
61
62  def test_MergeDAPSnapshotEnabled(self):
63    dap1 = DynamicPartitionMetadata()
64    dap2 = DynamicPartitionMetadata()
65    merged_dap = DynamicPartitionMetadata()
66    merged_dap.snapshot_enabled = True
67    dap1.snapshot_enabled = True
68    dap2.snapshot_enabled = True
69    merge_ota.MergeDynamicPartitionMetadata(merged_dap, dap1)
70    merge_ota.MergeDynamicPartitionMetadata(merged_dap, dap2)
71    self.assertTrue(merged_dap.snapshot_enabled)
72
73  def test_MergeDAPGroups(self):
74    dap1 = DynamicPartitionMetadata()
75    dap1.groups.append(DynamicPartitionGroup(
76        name="abc", partition_names=["a", "b", "c"]))
77    dap2 = DynamicPartitionMetadata()
78    dap2.groups.append(DynamicPartitionGroup(
79        name="abc", partition_names=["d", "e", "f"]))
80    merged_dap = DynamicPartitionMetadata()
81    merge_ota.MergeDynamicPartitionMetadata(merged_dap, dap1)
82    merge_ota.MergeDynamicPartitionMetadata(merged_dap, dap2)
83    self.assertEqual(len(merged_dap.groups), 1)
84    self.assertEqual(merged_dap.groups[0].name, "abc")
85    self.assertEqual(merged_dap.groups[0].partition_names, [
86                     "a", "b", "c", "d", "e", "f"])
87