blob: e64355b186399f433cb63ce5d68325ba2614c89d [file] [log] [blame]
Tao Bao04e1f012018-02-04 12:13:35 -08001#
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"""
18Utils for running unittests.
19"""
20
Tao Baofc7e0e02018-02-13 13:54:02 -080021import os
Tao Bao04e1f012018-02-04 12:13:35 -080022import os.path
Tao Baofc7e0e02018-02-13 13:54:02 -080023import struct
24
25import common
Tao Bao04e1f012018-02-04 12:13:35 -080026
27
28def get_testdata_dir():
29 """Returns the testdata dir, in relative to the script dir."""
30 # The script dir is the one we want, which could be different from pwd.
31 current_dir = os.path.dirname(os.path.realpath(__file__))
32 return os.path.join(current_dir, 'testdata')
Tao Baofc7e0e02018-02-13 13:54:02 -080033
34
35def construct_sparse_image(chunks):
36 """Returns a sparse image file constructed from the given chunks.
37
38 From system/core/libsparse/sparse_format.h.
39 typedef struct sparse_header {
40 __le32 magic; // 0xed26ff3a
41 __le16 major_version; // (0x1) - reject images with higher major versions
42 __le16 minor_version; // (0x0) - allow images with higer minor versions
43 __le16 file_hdr_sz; // 28 bytes for first revision of the file format
44 __le16 chunk_hdr_sz; // 12 bytes for first revision of the file format
45 __le32 blk_sz; // block size in bytes, must be a multiple of 4 (4096)
46 __le32 total_blks; // total blocks in the non-sparse output image
47 __le32 total_chunks; // total chunks in the sparse input image
48 __le32 image_checksum; // CRC32 checksum of the original data, counting
49 // "don't care" as 0. Standard 802.3 polynomial,
50 // use a Public Domain table implementation
51 } sparse_header_t;
52
53 typedef struct chunk_header {
54 __le16 chunk_type; // 0xCAC1 -> raw; 0xCAC2 -> fill;
55 // 0xCAC3 -> don't care
56 __le16 reserved1;
57 __le32 chunk_sz; // in blocks in output image
58 __le32 total_sz; // in bytes of chunk input file including chunk header
59 // and data
60 } chunk_header_t;
61
62 Args:
63 chunks: A list of chunks to be written. Each entry should be a tuple of
64 (chunk_type, block_number).
65
66 Returns:
67 Filename of the created sparse image.
68 """
69 SPARSE_HEADER_MAGIC = 0xED26FF3A
70 SPARSE_HEADER_FORMAT = "<I4H4I"
71 CHUNK_HEADER_FORMAT = "<2H2I"
72
73 sparse_image = common.MakeTempFile(prefix='sparse-', suffix='.img')
74 with open(sparse_image, 'wb') as fp:
75 fp.write(struct.pack(
76 SPARSE_HEADER_FORMAT, SPARSE_HEADER_MAGIC, 1, 0, 28, 12, 4096,
77 sum(chunk[1] for chunk in chunks),
78 len(chunks), 0))
79
80 for chunk in chunks:
81 data_size = 0
82 if chunk[0] == 0xCAC1:
83 data_size = 4096 * chunk[1]
84 elif chunk[0] == 0xCAC2:
85 data_size = 4
86 elif chunk[0] == 0xCAC3:
87 pass
88 else:
89 assert False, "Unsupported chunk type: {}".format(chunk[0])
90
91 fp.write(struct.pack(
92 CHUNK_HEADER_FORMAT, chunk[0], 0, chunk[1], data_size + 12))
93 if data_size != 0:
94 fp.write(os.urandom(data_size))
95
96 return sparse_image