blob: ca127b1366713445ec8641580f2c9d63e6d4d6c7 [file] [log] [blame]
Tao Bao30e31142019-04-09 00:12:30 -07001#!/usr/bin/env python
Tao Bao04e1f012018-02-04 12:13:35 -08002#
3# Copyright (C) 2018 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"""
19Utils for running unittests.
20"""
21
Tao Bao898a9242018-10-18 14:51:27 -070022import logging
Tao Baofc7e0e02018-02-13 13:54:02 -080023import os
Tao Bao04e1f012018-02-04 12:13:35 -080024import os.path
Tao Baofc7e0e02018-02-13 13:54:02 -080025import struct
Tao Bao898a9242018-10-18 14:51:27 -070026import sys
Tao Bao65b94e92018-10-11 21:57:26 -070027import unittest
Tao Baofc7e0e02018-02-13 13:54:02 -080028
29import common
Tao Bao04e1f012018-02-04 12:13:35 -080030
Tao Bao898a9242018-10-18 14:51:27 -070031# Some test runner doesn't like outputs from stderr.
32logging.basicConfig(stream=sys.stdout)
33
Tao Bao04e1f012018-02-04 12:13:35 -080034
35def get_testdata_dir():
36 """Returns the testdata dir, in relative to the script dir."""
37 # The script dir is the one we want, which could be different from pwd.
38 current_dir = os.path.dirname(os.path.realpath(__file__))
39 return os.path.join(current_dir, 'testdata')
Tao Baofc7e0e02018-02-13 13:54:02 -080040
41
Tao Bao3bf8c652018-03-16 12:59:42 -070042def get_search_path():
43 """Returns the search path that has 'framework/signapk.jar' under."""
Tao Bao30e31142019-04-09 00:12:30 -070044
45 def signapk_exists(path):
46 signapk_path = os.path.realpath(
47 os.path.join(path, 'framework', 'signapk.jar'))
48 return os.path.exists(signapk_path)
49
50 # Try with ANDROID_BUILD_TOP first.
51 full_path = os.path.realpath(os.path.join(
52 os.environ.get('ANDROID_BUILD_TOP', ''), 'out', 'host', 'linux-x86'))
53 if signapk_exists(full_path):
54 return full_path
55
56 # Otherwise try going with relative pathes.
Tao Bao3bf8c652018-03-16 12:59:42 -070057 current_dir = os.path.dirname(os.path.realpath(__file__))
58 for path in (
59 # In relative to 'build/make/tools/releasetools' in the Android source.
60 ['..'] * 4 + ['out', 'host', 'linux-x86'],
61 # Or running the script unpacked from otatools.zip.
62 ['..']):
63 full_path = os.path.realpath(os.path.join(current_dir, *path))
Tao Bao30e31142019-04-09 00:12:30 -070064 if signapk_exists(full_path):
Tao Bao3bf8c652018-03-16 12:59:42 -070065 return full_path
66 return None
67
68
Tao Baofc7e0e02018-02-13 13:54:02 -080069def construct_sparse_image(chunks):
70 """Returns a sparse image file constructed from the given chunks.
71
72 From system/core/libsparse/sparse_format.h.
73 typedef struct sparse_header {
74 __le32 magic; // 0xed26ff3a
75 __le16 major_version; // (0x1) - reject images with higher major versions
76 __le16 minor_version; // (0x0) - allow images with higer minor versions
77 __le16 file_hdr_sz; // 28 bytes for first revision of the file format
78 __le16 chunk_hdr_sz; // 12 bytes for first revision of the file format
79 __le32 blk_sz; // block size in bytes, must be a multiple of 4 (4096)
80 __le32 total_blks; // total blocks in the non-sparse output image
81 __le32 total_chunks; // total chunks in the sparse input image
82 __le32 image_checksum; // CRC32 checksum of the original data, counting
83 // "don't care" as 0. Standard 802.3 polynomial,
84 // use a Public Domain table implementation
85 } sparse_header_t;
86
87 typedef struct chunk_header {
88 __le16 chunk_type; // 0xCAC1 -> raw; 0xCAC2 -> fill;
89 // 0xCAC3 -> don't care
90 __le16 reserved1;
91 __le32 chunk_sz; // in blocks in output image
92 __le32 total_sz; // in bytes of chunk input file including chunk header
93 // and data
94 } chunk_header_t;
95
96 Args:
97 chunks: A list of chunks to be written. Each entry should be a tuple of
98 (chunk_type, block_number).
99
100 Returns:
101 Filename of the created sparse image.
102 """
103 SPARSE_HEADER_MAGIC = 0xED26FF3A
104 SPARSE_HEADER_FORMAT = "<I4H4I"
105 CHUNK_HEADER_FORMAT = "<2H2I"
106
107 sparse_image = common.MakeTempFile(prefix='sparse-', suffix='.img')
108 with open(sparse_image, 'wb') as fp:
109 fp.write(struct.pack(
110 SPARSE_HEADER_FORMAT, SPARSE_HEADER_MAGIC, 1, 0, 28, 12, 4096,
111 sum(chunk[1] for chunk in chunks),
112 len(chunks), 0))
113
114 for chunk in chunks:
115 data_size = 0
116 if chunk[0] == 0xCAC1:
117 data_size = 4096 * chunk[1]
118 elif chunk[0] == 0xCAC2:
119 data_size = 4
120 elif chunk[0] == 0xCAC3:
121 pass
122 else:
123 assert False, "Unsupported chunk type: {}".format(chunk[0])
124
125 fp.write(struct.pack(
126 CHUNK_HEADER_FORMAT, chunk[0], 0, chunk[1], data_size + 12))
127 if data_size != 0:
128 fp.write(os.urandom(data_size))
129
130 return sparse_image
Tao Bao65b94e92018-10-11 21:57:26 -0700131
132
133class ReleaseToolsTestCase(unittest.TestCase):
134 """A common base class for all the releasetools unittests."""
135
136 def tearDown(self):
137 common.Cleanup()
Tao Bao30e31142019-04-09 00:12:30 -0700138
139
140if __name__ == '__main__':
141 testsuite = unittest.TestLoader().discover(
142 os.path.dirname(os.path.realpath(__file__)))
143 # atest needs a verbosity level of >= 2 to correctly parse the result.
144 unittest.TextTestRunner(verbosity=2).run(testsuite)