t_elf.py revision 2721
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa#!/usr/bin/python
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa#
abc834c020080fe44a1ea4e34278327e99e3e12eEugen Kuksa# CDDL HEADER START
abc834c020080fe44a1ea4e34278327e99e3e12eEugen Kuksa#
abc834c020080fe44a1ea4e34278327e99e3e12eEugen Kuksa# The contents of this file are subject to the terms of the
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksa# Common Development and Distribution License (the "License").
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksa# You may not use this file except in compliance with the License.
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksa#
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksa# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksa# or http://www.opensolaris.org/os/licensing.
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksa# See the License for the specific language governing permissions
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksa# and limitations under the License.
0978722f6ca27f5d5f5ed4ec8400703dfe211184Eugen Kuksa#
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksa# When distributing Covered Code, include this CDDL HEADER in each
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksa# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksa# If applicable, add the following below this CDDL HEADER, with the
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksa# fields enclosed by brackets "[]" replaced with your own identifying
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksa# information: Portions Copyright [yyyy] [name of copyright owner]
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksa#
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksa# CDDL HEADER END
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksa#
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksa
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksa# Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksa
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksaimport testutils
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksaif __name__ == "__main__":
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksa testutils.setup_environment("../../../proto")
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksaimport pkg5unittest
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksa
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksaimport unittest
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksaimport pkg.elf as elf
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksaimport os
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksaimport re
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksaimport sys
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksaimport pkg.portable
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksa
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksaclass TestElf(pkg5unittest.Pkg5TestCase):
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksa
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksa # If something in this list does not exist, the test_valid_elf
f0264afd33a980b6584747fc8159ee950805d9e3Eugen Kuksa # tests may fail. At some point if someone moves paths around in
b51057b860560bf3ee454c03a121af3d5d34f482Eugen Kuksa # ON, this might fail. Sorry!
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa elf_paths = [
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa "/usr/bin/ls",
28001d576e67ba46ed481c5695f1e0827ff26007Eugen Kuksa "/usr/bin/__ARCH__/ls",
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa "/dev/ksyms",
b1fe9054ad7c7192fe4c474363247dad15963e99Eugen Kuksa "/usr/lib/libc.so",
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa "/usr/lib/__ARCH__/libc.so",
5efadb4662f2a63d5f5f1a5b303ab7c3371069a8Eugen Kuksa "/usr/lib/crti.o",
d89f470f7da0b9f8295d0ac0defff09884894b8bEugen Kuksa "/usr/lib/__ARCH__/crti.o",
d89f470f7da0b9f8295d0ac0defff09884894b8bEugen Kuksa "/kernel/drv/__ARCH__/sd",
d89f470f7da0b9f8295d0ac0defff09884894b8bEugen Kuksa "/kernel/fs/__ARCH__/zfs",
5efadb4662f2a63d5f5f1a5b303ab7c3371069a8Eugen Kuksa "/usr/kernel/drv/__ARCH__/ptm",
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa ]
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa def test_non_elf(self):
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa """Test that elf routines gracefully handle non-elf objects."""
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa
a8028fd2789e323040de08827a0fe1f7d36fde2bEugen Kuksa p = "this-is-not-an-elf-file.so"
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa self.make_misc_files({p: "this is only a test"})
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa os.chdir(self.test_root)
67daeb8066e4460cb820db60c45138dd48309bb9Eugen Kuksa self.assertEqual(elf.is_elf_object(p), False)
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa self.assertRaises(elf.ElfError, elf.get_dynamic, p)
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa self.assertRaises(elf.ElfError, elf.get_info, p)
57c81a32dff6182f040b4f852892144641a62fe5Eugen Kuksa
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa def test_non_existent(self):
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa """Test that elf routines gracefully handle ENOENT."""
a8028fd2789e323040de08827a0fe1f7d36fde2bEugen Kuksa
a8028fd2789e323040de08827a0fe1f7d36fde2bEugen Kuksa os.chdir(self.test_root)
a8028fd2789e323040de08827a0fe1f7d36fde2bEugen Kuksa p = "does/not/exist"
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa self.assertRaises(OSError, elf.is_elf_object, p)
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa self.assertRaises(OSError, elf.get_dynamic, p)
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa self.assertRaises(OSError, elf.get_info, p)
67daeb8066e4460cb820db60c45138dd48309bb9Eugen Kuksa
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa def test_valid_elf(self):
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa """Test that elf routines work on a small set of objects."""
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa arch = pkg.portable.get_isainfo()[0]
4ec9d8b62c3c1a001548eb0883b6f81e00c391a0Eugen Kuksa for p in self.elf_paths:
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa p = re.sub("__ARCH__", arch, p)
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa self.debug("testing elf file %s" % p)
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa self.assert_(os.path.exists(p))
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa self.assertEqual(elf.is_elf_object(p), True)
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa elf.get_dynamic(p)
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa elf.get_info(p)
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa
4ec9d8b62c3c1a001548eb0883b6f81e00c391a0Eugen Kuksaif __name__ == "__main__":
5efadb4662f2a63d5f5f1a5b303ab7c3371069a8Eugen Kuksa unittest.main()
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa