• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = '0F71FF00800180800280807EFFFFFFFF0F8180808070FFFFFFFFFFFFFFFFFF008180808080808080807F8080808080808080807F'.decode('hex')
32
33# Reference output: [(value, length)]
34_SLEB_OUTPUT_DATA = [
35    (0xF, 1),
36    (-0xF, 1),
37    (0x7F, 2),
38    (0x80, 2),
39    (0x8000, 3),
40    (-0x8000, 3),
41    (0xFFFFFFFF, 5),
42    (-0xFFFFFFFF, 5),
43    (0x7FFFFFFFFFFFFFFF, 10),
44    (-0x7FFFFFFFFFFFFFFF, 10),
45    (-0x8000000000000000, 10),
46]
47
48
49class UtilsTest(unittest.TestCase):
50    """Unit tests for vts.utils.python.library.elf.utils."""
51
52    def testDecodeSLEB128(self):
53        """Test if DecodeSLEB128 decodes correctly."""
54        result = []
55        cur = 0
56        while cur < len(_SLEB_INPUT_DATA):
57            val, num = elf_utils.DecodeSLEB128(_SLEB_INPUT_DATA, cur)
58            cur += num
59            result.append((val, num))
60        self.assertEqual(result, _SLEB_OUTPUT_DATA)
61
62
63if __name__ == '__main__':
64    unittest.main()
65