1#!/usr/bin/env python 2# 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"""This file contains unit tests for utils.""" 18 19import unittest 20 21from vts.utils.python.library.elf import utils as elf_utils 22 23 24# SLEB input data are generated by: 25# $ llvm-mc -filetype=obj <<EOF | obj2yaml | grep 'Content:' | awk '{print $2}' 26# > .sleb128 15 27# > .sleb128 -15 28# > EOF 29 30# Test data: SLEB128 encoded byte stream 31_SLEB_INPUT_DATA = bytes(bytearray.fromhex('0F71FF00800180800280807EFFFFFFFF' 32 '0F8180808070FFFFFFFFFFFFFFFFFF00' 33 '8180808080808080807F808080808080' 34 '8080807F')) 35 36# Reference output: [(value, length)] 37_SLEB_OUTPUT_DATA = [ 38 (0xF, 1), 39 (-0xF, 1), 40 (0x7F, 2), 41 (0x80, 2), 42 (0x8000, 3), 43 (-0x8000, 3), 44 (0xFFFFFFFF, 5), 45 (-0xFFFFFFFF, 5), 46 (0x7FFFFFFFFFFFFFFF, 10), 47 (-0x7FFFFFFFFFFFFFFF, 10), 48 (-0x8000000000000000, 10), 49] 50 51 52class UtilsTest(unittest.TestCase): 53 """Unit tests for vts.utils.python.library.elf.utils.""" 54 55 def testDecodeSLEB128(self): 56 """Test if DecodeSLEB128 decodes correctly.""" 57 result = [] 58 cur = 0 59 while cur < len(_SLEB_INPUT_DATA): 60 val, num = elf_utils.DecodeSLEB128(_SLEB_INPUT_DATA, cur) 61 cur += num 62 result.append((val, num)) 63 self.assertEqual(result, _SLEB_OUTPUT_DATA) 64 65 66if __name__ == '__main__': 67 unittest.main() 68