1516N/A#!/usr/bin/python
154N/A#
154N/A# CDDL HEADER START
154N/A#
154N/A# The contents of this file are subject to the terms of the
154N/A# Common Development and Distribution License (the "License").
154N/A# You may not use this file except in compliance with the License.
154N/A#
154N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
154N/A# or http://www.opensolaris.org/os/licensing.
154N/A# See the License for the specific language governing permissions
154N/A# and limitations under the License.
154N/A#
154N/A# When distributing Covered Code, include this CDDL HEADER in each
154N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
154N/A# If applicable, add the following below this CDDL HEADER, with the
409N/A# fields enclosed by brackets "[]" replaced with your own identifying
409N/A# information: Portions Copyright [yyyy] [name of copyright owner]
154N/A#
154N/A# CDDL HEADER END
154N/A#
154N/A
3339N/A# Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
154N/A
3339N/Afrom . import testutils
1715N/Aif __name__ == "__main__":
1715N/A testutils.setup_environment("../../../proto")
1715N/Aimport pkg5unittest
1715N/A
526N/Aimport unittest
154N/Aimport pkg.elf as elf
154N/Aimport os
2721N/Aimport re
2721N/Aimport pkg.portable
154N/A
430N/Aclass TestElf(pkg5unittest.Pkg5TestCase):
154N/A
2721N/A # If something in this list does not exist, the test_valid_elf
2721N/A # tests may fail. At some point if someone moves paths around in
2721N/A # ON, this might fail. Sorry!
2721N/A elf_paths = [
2983N/A "/usr/bin/mdb",
2983N/A "/usr/bin/__ARCH__/mdb",
2721N/A "/usr/lib/libc.so",
2721N/A "/usr/lib/__ARCH__/libc.so",
2721N/A "/usr/lib/crti.o",
2721N/A "/usr/lib/__ARCH__/crti.o",
2721N/A "/kernel/drv/__ARCH__/sd",
2721N/A "/kernel/fs/__ARCH__/zfs",
3405N/A "/usr/kernel/drv/__ARCH__/ksyms",
2721N/A ]
154N/A
2721N/A def test_non_elf(self):
2721N/A """Test that elf routines gracefully handle non-elf objects."""
158N/A
2721N/A p = "this-is-not-an-elf-file.so"
2721N/A self.make_misc_files({p: "this is only a test"})
2721N/A os.chdir(self.test_root)
2721N/A self.assertEqual(elf.is_elf_object(p), False)
2721N/A self.assertRaises(elf.ElfError, elf.get_dynamic, p)
3236N/A self.assertRaises(elf.ElfError, elf.get_hashes, p)
2721N/A self.assertRaises(elf.ElfError, elf.get_info, p)
1185N/A
2721N/A def test_non_existent(self):
2721N/A """Test that elf routines gracefully handle ENOENT."""
154N/A
2721N/A os.chdir(self.test_root)
2721N/A p = "does/not/exist"
2721N/A self.assertRaises(OSError, elf.is_elf_object, p)
2721N/A self.assertRaises(OSError, elf.get_dynamic, p)
3236N/A self.assertRaises(OSError, elf.get_hashes, p)
2721N/A self.assertRaises(OSError, elf.get_info, p)
158N/A
2721N/A def test_valid_elf(self):
2721N/A """Test that elf routines work on a small set of objects."""
2721N/A arch = pkg.portable.get_isainfo()[0]
2721N/A for p in self.elf_paths:
2721N/A p = re.sub("__ARCH__", arch, p)
3158N/A self.debug("testing elf file {0}".format(p))
3339N/A self.assertTrue(os.path.exists(p), "{0} does not exist".format(p))
2721N/A self.assertEqual(elf.is_elf_object(p), True)
2721N/A elf.get_dynamic(p)
3236N/A elf.get_hashes(p)
2721N/A elf.get_info(p)
154N/A
3236N/A def test_get_hashes_params(self):
3236N/A """Test that get_hashes(..) returns checksums according to the
2962N/A parameters passed to the method."""
2962N/A
2962N/A # Check that the hashes generated have the correct length
2962N/A # depending on the algorithm used to generated.
2962N/A sha1_len = 40
2962N/A sha256_len = 64
2962N/A
3402N/A # the default is to return both the SHA-1 elfhash and
3402N/A # the SHA-256 pkg.content-hash
3236N/A d = elf.get_hashes(self.elf_paths[0])
3339N/A self.assertTrue(len(d["elfhash"]) == sha1_len)
3402N/A self.assertTrue("pkg.content-hash" in d)
3402N/A self.assertTrue(len(d["pkg.content-hash"]) == 2)
3402N/A for h in range(2):
3402N/A v = d["pkg.content-hash"][h].split(":")
3402N/A self.assertTrue(len(v) == 3)
3402N/A self.assertTrue(v[1] == "sha256")
3402N/A self.assertTrue(len(v[2]) == sha256_len)
2962N/A
3402N/A d = elf.get_hashes(self.elf_paths[0],
3402N/A elfhash=False, sha512t_256=True)
3339N/A self.assertTrue("elfhash" not in d)
3402N/A self.assertTrue("pkg.content-hash" in d)
3402N/A self.assertTrue(len(d["pkg.content-hash"]) == 4)
3402N/A sha256_count = 0
3402N/A sha512t_256_count = 0
3402N/A unsigned_count = 0
3402N/A for h in range(4):
3402N/A v = d["pkg.content-hash"][h].split(":")
3402N/A self.assertTrue(len(v) == 3)
3402N/A self.assertTrue(len(v[2]) == sha256_len)
3402N/A if v[0].endswith(".unsigned"):
3402N/A unsigned_count += 1
3402N/A if v[1] == "sha256":
3402N/A sha256_count += 1
3402N/A elif v[1] == "sha512t_256":
3402N/A sha512t_256_count += 1
3402N/A self.assertTrue(sha256_count == 2)
3402N/A self.assertTrue(sha512t_256_count == 2)
3402N/A self.assertTrue(unsigned_count == 2)
2962N/A
3402N/A d = elf.get_hashes(self.elf_paths[0], elfhash=False,
3402N/A sha256=False)
3402N/A self.assertTrue(len(d) == 0)
2962N/A
2962N/A
154N/Aif __name__ == "__main__":
2721N/A unittest.main()