aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/__init__.py0
-rw-r--r--tests/context.py53
-rw-r--r--tests/test_process.py124
3 files changed, 177 insertions, 0 deletions
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/__init__.py
diff --git a/tests/context.py b/tests/context.py
new file mode 100644
index 0000000..d859c2f
--- /dev/null
+++ b/tests/context.py
@@ -0,0 +1,53 @@
+# this context.py should be included by all tests
+# idea from http://kennethreitz.com/repository-structure-and-python.html
+
+import os
+import shutil
+import sys
+import tempfile
+
+import whatmaps
+
+sys.path.insert(0, os.path.abspath('..'))
+
+# the top or root dir of the git-buildpackage source tree to be used by tests
+projectdir = os.path.dirname(os.path.dirname(os.path.abspath(whatmaps.__file__)))
+
+_chdir_backup = None
+_tmpdirs = []
+
+def chdir(dir):
+ global _chdir_backup
+ if not _chdir_backup:
+ _chdir_backup = os.path.abspath(os.curdir)
+ os.chdir(str(dir))
+
+def new_tmpdir(name):
+ global _tmpdirs
+ prefix='whatmaps_%s_' % name
+ tmpdir = TmpDir(prefix)
+ _tmpdirs.append(tmpdir)
+ return tmpdir
+
+def teardown():
+ if _chdir_backup:
+ os.chdir(_chdir_backup)
+ for tmpdir in _tmpdirs:
+ tmpdir.rmdir()
+ del _tmpdirs[:]
+
+class TmpDir(object):
+
+ def __init__(self, suffix='', prefix='tmp'):
+ self.path = tempfile.mkdtemp(suffix=suffix, prefix=prefix)
+
+ def rmdir(self):
+ if self.path and not os.getenv("WHATMAPS_TESTS_NOCLEAN"):
+ shutil.rmtree(self.path)
+ self.path = None
+
+ def __repr__(self):
+ return self.path
+
+ def join(self, *args):
+ return os.path.join(self.path, *args)
diff --git a/tests/test_process.py b/tests/test_process.py
new file mode 100644
index 0000000..da47ebd
--- /dev/null
+++ b/tests/test_process.py
@@ -0,0 +1,124 @@
+# vim: set fileencoding=utf-8 :
+# (C) 2014 Guido G√ľnther <agx@sigxcpu.org>
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+"""Test L{whatmaps.process} config"""
+
+import os
+import sys
+import unittest
+import random
+
+from whatmaps.process import Process
+
+from . import context
+
+class TestWhatmapsProcess(unittest.TestCase):
+ def setUp(self):
+ self.tmpdir = context.new_tmpdir(__name__)
+ self.procfs = str(self.tmpdir)
+ self.pid = random.randint(1, 65535)
+ self.piddir = os.path.join(self.procfs, str(self.pid))
+ os.mkdir(self.piddir)
+ self.exe = os.path.join(self.piddir, 'exe')
+ self.cmdline = os.path.join(self.piddir, 'cmdline')
+ self.maps = os.path.join(self.piddir, 'maps')
+ self._write_cmdline('doesnotmatter') # Write at least an empty cmdline
+ self._write_exe_symlink('acommand')
+ self._write_maps([['f32b43221000-7f32b4522000',
+ '---p',
+ '00020000',
+ 'fe:02',
+ '1704011',
+ '/lib/x86_64-linux-gnu/libselinux.so.1'],
+ ['7f32b4521000-7f32b4623000',
+ 'r--p',
+ '00020000',
+ 'fe:02',
+ '1704011',
+ '/lib/x86_64-linux-gnu/libselinux.so.1'],
+ ])
+
+ def _write_exe_symlink(self, name):
+ exe = os.path.join(str(self.tmpdir), name)
+ os.symlink(exe, self.exe)
+
+ def _write_cmdline(self, text=''):
+ f = open(self.cmdline, 'w')
+ f.write(text)
+ f.close()
+
+ def _write_maps(self, data):
+ f = open(self.maps, 'w')
+ f.write('\n'.join([' '.join(r) for r in data]))
+ f.close()
+
+ def test_nonexistent(self):
+ """No exe link should create an 'empty' object"""
+ os.unlink(self.exe)
+ p = Process(self.pid, self.procfs)
+ self.assertIsNone(p.exe)
+ self.assertIsNone(p.cmdline)
+
+ def test_deleted(self):
+ """Handle symlink to deleted binaries"""
+ exe = '/does/not/matter'
+ os.unlink(self.exe)
+ os.symlink(os.path.join(self.piddir, '%s (deleted)' % exe),
+ self.exe)
+
+ p = Process(self.pid, procfs=self.procfs)
+ self.assertEqual(p.exe, exe)
+ self.assertTrue(p.deleted, True)
+ self.assertEqual(p.cmdline, 'doesnotmatter')
+
+ def test_existing(self):
+ p = Process(self.pid, procfs=self.procfs)
+ exe = os.path.join(str(self.tmpdir), 'acommand')
+ self.assertEqual(p.exe, exe)
+ self.assertEqual(p.cmdline, 'doesnotmatter')
+ self.assertFalse(p.deleted)
+ self.assertEqual(str(p),
+ "<Process object pid:%d>" % self.pid)
+
+ def test_maps(self):
+ """Check whether the process maps a shared object at path"""
+ p = Process(self.pid, procfs=self.procfs)
+ self.assertFalse(p.maps('/does/not/exist'))
+ self.assertTrue(p.maps('/lib/x86_64-linux-gnu/libselinux.so.1'))
+
+ def test_no_maps(self):
+ """Check if we don't fail if the process went away"""
+ os.unlink(self.maps)
+ p = Process(self.pid, procfs=self.procfs)
+ self.assertFalse(p.maps('/does/not/exist'))
+ self.assertFalse(p.maps('/lib/x86_64-linux-gnu/libselinux.so.1'))
+
+ def test_broken_maps(self):
+ """Continue on unparseable map file"""
+ # Provoke index error by to few items in line
+ self._write_maps([['do', 'few', 'items']])
+ p = Process(self.pid, procfs=self.procfs)
+ self.assertFalse(p.maps('/does/not/exist'))
+ self.assertFalse(p.maps('/lib/x86_64-linux-gnu/libselinux.so.1'))
+
+ @unittest.skipIf(os.getuid() == 0, "Skip if root")
+ def test_broken_unreadable_map(self):
+ """Raise error if map file is unreadable"""
+ os.chmod(self.maps, 0)
+ p = Process(self.pid, procfs=self.procfs)
+ self.assertRaises(IOError, p.maps, '/does/not/exist')
+
+ def tearDown(self):
+ context.teardown()