xref: /aosp_15_r20/build/make/tools/releasetools/test_ota_utils.py (revision 9e94795a3d4ef5c1d47486f9a02bb378756cea8a)
1*9e94795aSAndroid Build Coastguard Worker# Copyright (C) 2021 The Android Open Source Project
2*9e94795aSAndroid Build Coastguard Worker#
3*9e94795aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*9e94795aSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*9e94795aSAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*9e94795aSAndroid Build Coastguard Worker#
7*9e94795aSAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
8*9e94795aSAndroid Build Coastguard Worker#
9*9e94795aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*9e94795aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*9e94795aSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9e94795aSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*9e94795aSAndroid Build Coastguard Worker# limitations under the License.
14*9e94795aSAndroid Build Coastguard Worker
15*9e94795aSAndroid Build Coastguard Worker
16*9e94795aSAndroid Build Coastguard Workerimport unittest
17*9e94795aSAndroid Build Coastguard Workerimport io
18*9e94795aSAndroid Build Coastguard Workerimport ota_utils
19*9e94795aSAndroid Build Coastguard Workerimport zipfile
20*9e94795aSAndroid Build Coastguard Worker
21*9e94795aSAndroid Build Coastguard Worker
22*9e94795aSAndroid Build Coastguard Workerclass TestZipEntryOffset(unittest.TestCase):
23*9e94795aSAndroid Build Coastguard Worker  def test_extra_length_differ(self):
24*9e94795aSAndroid Build Coastguard Worker      # This is a magic zip file such that:
25*9e94795aSAndroid Build Coastguard Worker      # 1. It has 1 entry, `file.txt'`
26*9e94795aSAndroid Build Coastguard Worker      # 2. The central directory entry for the entry contains an extra field of#
27*9e94795aSAndroid Build Coastguard Worker      # length 24, while the local file header for the entry contains an extra#
28*9e94795aSAndroid Build Coastguard Worker      # field of length 28.
29*9e94795aSAndroid Build Coastguard Worker      # It is key that the entry contains extra field of different length.
30*9e94795aSAndroid Build Coastguard Worker      # The sole purpose of this test case is make sure our offset computing
31*9e94795aSAndroid Build Coastguard Worker      # logic works in this scenario.
32*9e94795aSAndroid Build Coastguard Worker
33*9e94795aSAndroid Build Coastguard Worker      # This is created by:
34*9e94795aSAndroid Build Coastguard Worker      # touch file.txt
35*9e94795aSAndroid Build Coastguard Worker      # zip -0 test.zip file.txt
36*9e94795aSAndroid Build Coastguard Worker      # Above command may or may not work on all platforms.
37*9e94795aSAndroid Build Coastguard Worker      # Some zip implementation will keep the extra field size consistent.
38*9e94795aSAndroid Build Coastguard Worker      # Some don't
39*9e94795aSAndroid Build Coastguard Worker    magic_zip = b'PK\x03\x04\n\x00\x00\x00\x00\x00nY\xfcR\x00\x00\x00\x00\x00\x00\x00' +\
40*9e94795aSAndroid Build Coastguard Worker        b'\x00\x00\x00\x00\x00\x08\x00\x1c\x00file.txtUT\t\x00\x03' +\
41*9e94795aSAndroid Build Coastguard Worker        b'\xa0s\x01a\xa0s\x01aux\x0b\x00\x01\x04\x88\xc4\t\x00\x04S_\x01\x00' +\
42*9e94795aSAndroid Build Coastguard Worker        b'PK\x01\x02\x1e\x03\n\x00\x00\x00\x00\x00nY\xfcR\x00\x00\x00\x00' +\
43*9e94795aSAndroid Build Coastguard Worker        b'\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00\x18\x00\x00\x00\x00\x00' +\
44*9e94795aSAndroid Build Coastguard Worker        b'\x00\x00\x00\x00\x80\x81\x00\x00\x00\x00file.txt' +\
45*9e94795aSAndroid Build Coastguard Worker        b'UT\x05\x00\x03\xa0s\x01aux\x0b\x00\x01\x04\x88\xc4\t\x00\x04' +\
46*9e94795aSAndroid Build Coastguard Worker        b'S_\x01\x00PK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00N\x00\x00' +\
47*9e94795aSAndroid Build Coastguard Worker        b'\x00B\x00\x00\x00\x00\x00'
48*9e94795aSAndroid Build Coastguard Worker    # Just making sure we concatenated the bytes correctly
49*9e94795aSAndroid Build Coastguard Worker    self.assertEqual(len(magic_zip), 166)
50*9e94795aSAndroid Build Coastguard Worker    fp = io.BytesIO(magic_zip)
51*9e94795aSAndroid Build Coastguard Worker    with zipfile.ZipFile(fp, 'r') as zfp:
52*9e94795aSAndroid Build Coastguard Worker      self.assertGreater(len(zfp.infolist()), 0)
53*9e94795aSAndroid Build Coastguard Worker      zinfo = zfp.getinfo("file.txt")
54*9e94795aSAndroid Build Coastguard Worker      (offset, size) = ota_utils.GetZipEntryOffset(zfp, zinfo)
55*9e94795aSAndroid Build Coastguard Worker      self.assertEqual(size, zinfo.file_size)
56*9e94795aSAndroid Build Coastguard Worker      self.assertEqual(offset, zipfile.sizeFileHeader+len(zinfo.filename) + 28)
57