1#!/usr/bin/env python
2#
3# Copyright (C) 2020 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
18"""Unit testing paycheck.py."""
19
20# This test requires new (Y) and old (X) images, as well as a full payload
21# from image Y and a delta payload from Y to X for each partition.
22# Payloads are from sample_images/generate_payloads.
23#
24# The test performs the following:
25#
26# - It statically applies the full and delta payloads.
27#
28# - It applies full_payload to yield a new kernel (kern.part) and rootfs
29#   (root.part) and compares them to the new image partitions.
30#
31# - It applies delta_payload to the old image to yield a new kernel and rootfs
32#   and compares them to the new image partitions.
33#
34# Previously test_paycheck.sh. Run with update_payload ebuild.
35
36# Disable check for function names to avoid errors based on old code
37# pylint: disable=invalid-name
38
39import filecmp
40import os
41import subprocess
42import unittest
43
44
45class PaycheckTest(unittest.TestCase):
46  """Test paycheck functions."""
47
48  def setUp(self):
49    self.tmpdir = os.getenv('T')
50
51    self._full_payload = os.path.join(self.tmpdir, 'full_payload.bin')
52    self._delta_payload = os.path.join(self.tmpdir, 'delta_payload.bin')
53
54    self._new_kernel = os.path.join(self.tmpdir, 'disk_ext2_4k.img')
55    self._new_root = os.path.join(self.tmpdir, 'disk_sqfs_default.img')
56    self._old_kernel = os.path.join(self.tmpdir,
57                                    'disk_ext2_4k_empty.img')
58    self._old_root = os.path.join(self.tmpdir, 'disk_sqfs_empty.img')
59
60    # Temp output files.
61    self._kernel_part = os.path.join(self.tmpdir, 'kern.part')
62    self._root_part = os.path.join(self.tmpdir, 'root.part')
63
64  def checkPayload(self, type_arg, payload):
65    """Checks Payload."""
66    self.assertEqual(0, subprocess.check_call(['./paycheck.py', '-t',
67                                               type_arg, payload]))
68
69  def testFullPayload(self):
70    """Checks the full payload statically."""
71    self.checkPayload('full', self._full_payload)
72
73  def testDeltaPayload(self):
74    """Checks the delta payload statically."""
75    self.checkPayload('delta', self._delta_payload)
76
77  def testApplyFullPayload(self):
78    """Applies full payloads and compares results to new sample images."""
79    self.assertEqual(0, subprocess.check_call(['./paycheck.py',
80                                               self._full_payload,
81                                               '--part_names', 'kernel', 'root',
82                                               '--out_dst_part_paths',
83                                               self._kernel_part,
84                                               self._root_part]))
85
86    # Check if generated full image is equal to sample image.
87    self.assertTrue(filecmp.cmp(self._kernel_part, self._new_kernel))
88    self.assertTrue(filecmp.cmp(self._root_part, self._new_root))
89
90  def testApplyDeltaPayload(self):
91    """Applies delta to old image and checks against new sample images."""
92    self.assertEqual(0, subprocess.check_call(['./paycheck.py',
93                                               self._delta_payload,
94                                               '--part_names', 'kernel', 'root',
95                                               '--src_part_paths',
96                                               self._old_kernel, self._old_root,
97                                               '--out_dst_part_paths',
98                                               self._kernel_part,
99                                               self._root_part]))
100
101    self.assertTrue(filecmp.cmp(self._kernel_part, self._new_kernel))
102    self.assertTrue(filecmp.cmp(self._root_part, self._new_root))
103
104if __name__ == '__main__':
105  unittest.main()
106