Tao Bao | 04e1f01 | 2018-02-04 12:13:35 -0800 | [diff] [blame] | 1 | # |
| 2 | # Copyright (C) 2018 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 | |
| 17 | """ |
| 18 | Utils for running unittests. |
| 19 | """ |
| 20 | |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 21 | import os |
Tao Bao | 04e1f01 | 2018-02-04 12:13:35 -0800 | [diff] [blame] | 22 | import os.path |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 23 | import struct |
Tao Bao | 65b94e9 | 2018-10-11 21:57:26 -0700 | [diff] [blame] | 24 | import unittest |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 25 | |
| 26 | import common |
Tao Bao | 04e1f01 | 2018-02-04 12:13:35 -0800 | [diff] [blame] | 27 | |
| 28 | |
| 29 | def get_testdata_dir(): |
| 30 | """Returns the testdata dir, in relative to the script dir.""" |
| 31 | # The script dir is the one we want, which could be different from pwd. |
| 32 | current_dir = os.path.dirname(os.path.realpath(__file__)) |
| 33 | return os.path.join(current_dir, 'testdata') |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 34 | |
| 35 | |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 36 | def get_search_path(): |
| 37 | """Returns the search path that has 'framework/signapk.jar' under.""" |
| 38 | current_dir = os.path.dirname(os.path.realpath(__file__)) |
| 39 | for path in ( |
| 40 | # In relative to 'build/make/tools/releasetools' in the Android source. |
| 41 | ['..'] * 4 + ['out', 'host', 'linux-x86'], |
| 42 | # Or running the script unpacked from otatools.zip. |
| 43 | ['..']): |
| 44 | full_path = os.path.realpath(os.path.join(current_dir, *path)) |
| 45 | signapk_path = os.path.realpath( |
| 46 | os.path.join(full_path, 'framework', 'signapk.jar')) |
| 47 | if os.path.exists(signapk_path): |
| 48 | return full_path |
| 49 | return None |
| 50 | |
| 51 | |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 52 | def construct_sparse_image(chunks): |
| 53 | """Returns a sparse image file constructed from the given chunks. |
| 54 | |
| 55 | From system/core/libsparse/sparse_format.h. |
| 56 | typedef struct sparse_header { |
| 57 | __le32 magic; // 0xed26ff3a |
| 58 | __le16 major_version; // (0x1) - reject images with higher major versions |
| 59 | __le16 minor_version; // (0x0) - allow images with higer minor versions |
| 60 | __le16 file_hdr_sz; // 28 bytes for first revision of the file format |
| 61 | __le16 chunk_hdr_sz; // 12 bytes for first revision of the file format |
| 62 | __le32 blk_sz; // block size in bytes, must be a multiple of 4 (4096) |
| 63 | __le32 total_blks; // total blocks in the non-sparse output image |
| 64 | __le32 total_chunks; // total chunks in the sparse input image |
| 65 | __le32 image_checksum; // CRC32 checksum of the original data, counting |
| 66 | // "don't care" as 0. Standard 802.3 polynomial, |
| 67 | // use a Public Domain table implementation |
| 68 | } sparse_header_t; |
| 69 | |
| 70 | typedef struct chunk_header { |
| 71 | __le16 chunk_type; // 0xCAC1 -> raw; 0xCAC2 -> fill; |
| 72 | // 0xCAC3 -> don't care |
| 73 | __le16 reserved1; |
| 74 | __le32 chunk_sz; // in blocks in output image |
| 75 | __le32 total_sz; // in bytes of chunk input file including chunk header |
| 76 | // and data |
| 77 | } chunk_header_t; |
| 78 | |
| 79 | Args: |
| 80 | chunks: A list of chunks to be written. Each entry should be a tuple of |
| 81 | (chunk_type, block_number). |
| 82 | |
| 83 | Returns: |
| 84 | Filename of the created sparse image. |
| 85 | """ |
| 86 | SPARSE_HEADER_MAGIC = 0xED26FF3A |
| 87 | SPARSE_HEADER_FORMAT = "<I4H4I" |
| 88 | CHUNK_HEADER_FORMAT = "<2H2I" |
| 89 | |
| 90 | sparse_image = common.MakeTempFile(prefix='sparse-', suffix='.img') |
| 91 | with open(sparse_image, 'wb') as fp: |
| 92 | fp.write(struct.pack( |
| 93 | SPARSE_HEADER_FORMAT, SPARSE_HEADER_MAGIC, 1, 0, 28, 12, 4096, |
| 94 | sum(chunk[1] for chunk in chunks), |
| 95 | len(chunks), 0)) |
| 96 | |
| 97 | for chunk in chunks: |
| 98 | data_size = 0 |
| 99 | if chunk[0] == 0xCAC1: |
| 100 | data_size = 4096 * chunk[1] |
| 101 | elif chunk[0] == 0xCAC2: |
| 102 | data_size = 4 |
| 103 | elif chunk[0] == 0xCAC3: |
| 104 | pass |
| 105 | else: |
| 106 | assert False, "Unsupported chunk type: {}".format(chunk[0]) |
| 107 | |
| 108 | fp.write(struct.pack( |
| 109 | CHUNK_HEADER_FORMAT, chunk[0], 0, chunk[1], data_size + 12)) |
| 110 | if data_size != 0: |
| 111 | fp.write(os.urandom(data_size)) |
| 112 | |
| 113 | return sparse_image |
Tao Bao | 65b94e9 | 2018-10-11 21:57:26 -0700 | [diff] [blame] | 114 | |
| 115 | |
| 116 | class ReleaseToolsTestCase(unittest.TestCase): |
| 117 | """A common base class for all the releasetools unittests.""" |
| 118 | |
| 119 | def tearDown(self): |
| 120 | common.Cleanup() |