github.com/westcoastroms/westcoastroms-build@v0.0.0-20190928114312-2350e5a73030/build/make/tools/releasetools/test_utils.py (about)

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