1#!/usr/bin/env python
2#
3# Copyright (C) 2017 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
18import io
19import struct
20import unittest
21import sys
22import os
23
24import parser
25import sancov_parser
26
27
28class SancovParserTest(unittest.TestCase):
29    """Tests for sancov_parser in package vts.utils.python.coverage.
30
31    Ensures error handling, bitness detection, and correct
32    parsing of offsets in the file..
33    """
34
35    GOLDEN_SANCOV_PATH = os.path.join(sys.path[0], 'testdata/sample.sancov')
36    GOLDEN_EXPECTED_BITNESS = 64
37    GOLDEN_EXPECTED_OFFSETS = (
38        12115, 12219, 12463, 12527, 17123, 17311, 17507, 17771, 17975, 17987, 18107, 18167,
39        18299, 18503, 18571, 18743, 18755, 18791, 18903, 19127, 19715, 21027, 21123, 21223,
40        21335, 21407, 21455, 21611, 21643, 21683, 23227, 23303, 23343, 23503, 23767, 23779,
41        23791, 23819, 23867, 24615, 24651, 24743, 24775)
42
43    def testInvalidMagic(self):
44        """Asserts that an exception is raised when the magic is invalid.
45        """
46        stream = io.BytesIO(struct.pack('L', 0xC0BFFFFFFFFFFF10))
47        p = sancov_parser.SancovParser(stream)
48        with self.assertRaises(parser.FileFormatError) as context:
49            p.Parse()
50        self.assertTrue('Invalid magic' in str(context.exception))
51
52    def testMagic32(self):
53        """Asserts that values are correctly read in 32-bit sancov files.
54        """
55        stream = io.BytesIO(struct.pack('L', sancov_parser.MAGIC32))
56        stream.seek(8)
57        values = (1, 2, 3)
58        stream.write(struct.pack('III', *values))
59        stream.seek(0)
60        p = sancov_parser.SancovParser(stream)
61        s = p.Parse()
62        self.assertEqual(32, p._bitness)
63        self.assertEqual(values, s)
64
65    def testMagic64(self):
66        """Asserts that values are correctly read in 64-bit sancov files.
67        """
68        stream = io.BytesIO(struct.pack('L', sancov_parser.MAGIC64))
69        stream.seek(8)
70        values = (4, 5, 6)
71        stream.write(struct.pack('LLL', *values))
72        stream.seek(0)
73        p = sancov_parser.SancovParser(stream)
74        s = p.Parse()
75        self.assertEqual(64, p._bitness)
76        self.assertEqual(values, s)
77
78    def testGetBitness32(self):
79        """Asserts that bitness is correctly determined from a 32-bit sancov file.
80        """
81        stream = io.BytesIO(struct.pack('L', sancov_parser.MAGIC32))
82        p = sancov_parser.SancovParser(stream)
83        self.assertEqual(32, p.GetBitness())
84
85    def testGetBitness64(self):
86        """Asserts that bitness is correctly determined from a 64-bit sancov file.
87        """
88        stream = io.BytesIO(struct.pack('L', sancov_parser.MAGIC64))
89        p = sancov_parser.SancovParser(stream)
90        self.assertEqual(64, p.GetBitness())
91
92    def testGolden(self):
93        """Asserts that offsets are correctly parsed from the golden file.
94        """
95        bitness, offsets = sancov_parser.ParseSancovFile(self.GOLDEN_SANCOV_PATH)
96        self.assertEqual(self.GOLDEN_EXPECTED_BITNESS, bitness)
97        self.assertEqual(self.GOLDEN_EXPECTED_OFFSETS, offsets)
98
99
100
101if __name__ == "__main__":
102    unittest.main()
103