1#!/usr/bin/env python
2# Copyright 2013 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import cStringIO
7import logging
8import os
9import sys
10import unittest
11
12ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
13sys.path.insert(0, ROOT_DIR)
14
15from procfs import ProcMaps
16
17
18class ProcMapsTest(unittest.TestCase):
19  _TEST_PROCMAPS = '\n'.join([
20      '00000000-00001000 r--p 00000000 fc:00 0',
21      '0080b000-0080c000 r-xp 0020b000 fc:00 2231329'
22          '                            /usr/bin/some',
23      '0080c000-0080f000 ---p 0020c000 fc:00 2231329'
24          '                            /usr/bin/some',
25      '0100a000-0100c000 r-xp 0120a000 fc:00 22381'
26          '                            /usr/bin/chrome',
27      '0100c000-0100f000 ---p 0120c000 fc:00 22381'
28          '                            /usr/bin/chrome',
29      '0237d000-02a9b000 rw-p 00000000 00:00 0'
30          '                                  [heap]',
31      '7fb920e6d000-7fb920e85000 r-xp 00000000 fc:00 263482'
32          '                     /lib/x86_64-linux-gnu/libpthread-2.15.so',
33      '7fb920e85000-7fb921084000 ---p 00018000 fc:00 263482'
34          '                     /lib/x86_64-linux-gnu/libpthread-2.15.so',
35      '7fb9225f4000-7fb922654000 rw-s 00000000 00:04 19660808'
36          '                   /SYSV00000000 (deleted)',
37      'ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0'
38          '                  [vsyscall]',
39      ])
40
41  _EXPECTED = [
42      (0x0, 0x1000, 'r', '-', '-', 'p', 0x0, 'fc', '00', 0, ''),
43      (0x80b000, 0x80c000, 'r', '-', 'x', 'p', 0x20b000,
44       'fc', '00', 2231329, '/usr/bin/some'),
45      (0x80c000, 0x80f000, '-', '-', '-', 'p', 0x20c000,
46       'fc', '00', 2231329, '/usr/bin/some'),
47      (0x100a000, 0x100c000, 'r', '-', 'x', 'p', 0x120a000,
48       'fc', '00', 22381, '/usr/bin/chrome'),
49      (0x100c000, 0x100f000, '-', '-', '-', 'p', 0x120c000,
50       'fc', '00', 22381, '/usr/bin/chrome'),
51      (0x237d000, 0x2a9b000, 'r', 'w', '-', 'p', 0x0,
52       '00', '00', 0, '[heap]'),
53      (0x7fb920e6d000, 0x7fb920e85000, 'r', '-', 'x', 'p', 0x0,
54       'fc', '00', 263482, '/lib/x86_64-linux-gnu/libpthread-2.15.so'),
55      (0x7fb920e85000, 0x7fb921084000, '-', '-', '-', 'p', 0x18000,
56       'fc', '00', 263482, '/lib/x86_64-linux-gnu/libpthread-2.15.so'),
57      (0x7fb9225f4000, 0x7fb922654000, 'r', 'w', '-', 's', 0x0,
58       '00', '04', 19660808, '/SYSV00000000 (deleted)'),
59      (0xffffffffff600000, 0xffffffffff601000, 'r', '-', 'x', 'p', 0x0,
60       '00', '00', 0, '[vsyscall]'),
61      ]
62
63  @staticmethod
64  def _expected_as_dict(index):
65    return {
66        'begin': ProcMapsTest._EXPECTED[index][0],
67        'end': ProcMapsTest._EXPECTED[index][1],
68        'readable': ProcMapsTest._EXPECTED[index][2],
69        'writable': ProcMapsTest._EXPECTED[index][3],
70        'executable': ProcMapsTest._EXPECTED[index][4],
71        'private': ProcMapsTest._EXPECTED[index][5],
72        'offset': ProcMapsTest._EXPECTED[index][6],
73        'major': ProcMapsTest._EXPECTED[index][7],
74        'minor': ProcMapsTest._EXPECTED[index][8],
75        'inode': ProcMapsTest._EXPECTED[index][9],
76        'name': ProcMapsTest._EXPECTED[index][10],
77        }
78
79  def test_load(self):
80    maps = ProcMaps.load_file(cStringIO.StringIO(self._TEST_PROCMAPS))
81    for index, entry in enumerate(maps):
82      self.assertEqual(entry.as_dict(), self._expected_as_dict(index))
83
84  def test_constants(self):
85    maps = ProcMaps.load_file(cStringIO.StringIO(self._TEST_PROCMAPS))
86    selected = [0, 2, 4, 7]
87    for index, entry in enumerate(maps.iter(ProcMaps.constants)):
88      self.assertEqual(entry.as_dict(),
89                       self._expected_as_dict(selected[index]))
90
91  def test_executable(self):
92    maps = ProcMaps.load_file(cStringIO.StringIO(self._TEST_PROCMAPS))
93    selected = [1, 3, 6, 9]
94    for index, entry in enumerate(maps.iter(ProcMaps.executable)):
95      self.assertEqual(entry.as_dict(),
96                       self._expected_as_dict(selected[index]))
97
98  def test_executable_and_constants(self):
99    maps = ProcMaps.load_file(cStringIO.StringIO(self._TEST_PROCMAPS))
100    selected = [0, 1, 2, 3, 4, 6, 7, 9]
101    for index, entry in enumerate(maps.iter(ProcMaps.executable_and_constants)):
102      self.assertEqual(entry.as_dict(),
103                       self._expected_as_dict(selected[index]))
104
105
106if __name__ == '__main__':
107  logging.basicConfig(
108      level=logging.DEBUG if '-v' in sys.argv else logging.ERROR,
109      format='%(levelname)5s %(filename)15s(%(lineno)3d): %(message)s')
110  unittest.main()
111