e8058322725ba050014777ee2484f7e833ab1e3aLukas Slebodnik#!/usr/bin/env python
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose# SSSD
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose#
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose# Unit tests for pysss_murmur
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose#
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose# Copyright (C) Sumit Bose <sbose@redhat.com> 2012
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose#
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose# This program is free software; you can redistribute it and/or modify
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose# it under the terms of the GNU General Public License as published by
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose# the Free Software Foundation; either version 3 of the License, or
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose# (at your option) any later version.
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose#
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose# This program is distributed in the hope that it will be useful,
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose# but WITHOUT ANY WARRANTY; without even the implied warranty of
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose# GNU General Public License for more details.
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose#
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose# You should have received a copy of the GNU General Public License
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose# along with this program. If not, see <http://www.gnu.org/licenses/>.
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrdafrom __future__ import print_function
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose
60e51fd2764291df2332f36ff478777627d92b57Sumit Boseimport unittest
60e51fd2764291df2332f36ff478777627d92b57Sumit Boseimport sys
60e51fd2764291df2332f36ff478777627d92b57Sumit Boseimport os
9e1de5c83371d91e200254cceef70852f5f94fd2Lukas Slebodnikimport tempfile
9e1de5c83371d91e200254cceef70852f5f94fd2Lukas Slebodnik
9e1de5c83371d91e200254cceef70852f5f94fd2Lukas SlebodnikBUILD_DIR = os.getenv('builddir') or "."
9e1de5c83371d91e200254cceef70852f5f94fd2Lukas SlebodnikTEST_DIR = os.getenv('SSS_TEST_DIR') or "."
9e1de5c83371d91e200254cceef70852f5f94fd2Lukas SlebodnikMODPATH = tempfile.mkdtemp(prefix="tp_pysss_murmur_", dir=TEST_DIR)
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose
60e51fd2764291df2332f36ff478777627d92b57Sumit Boseclass PySssMurmurImport(unittest.TestCase):
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose def setUp(self):
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose " Make sure we load the in-tree module "
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose self.system_path = sys.path[:]
7b9c3e69e5248940accc5e0c2ce08503bea87c75Fabiano Fidêncio sys.path = [MODPATH]
9dc4c15552c396a80add39d0da865793e5643237Fabiano Fidêncio print(os.getcwd())
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda print(MODPATH)
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose def tearDown(self):
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose " Restore the system path "
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose sys.path = self.system_path
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose def testImport(self):
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose " Import the module and assert it comes from tree "
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose try:
9e1de5c83371d91e200254cceef70852f5f94fd2Lukas Slebodnik dest_module_path = MODPATH + "/pysss_murmur.so"
4a5a18f489f4d19aa0571528a7f0c7a8d35ac83fLukas Slebodnik
4a5a18f489f4d19aa0571528a7f0c7a8d35ac83fLukas Slebodnik if sys.version_info[0] > 2:
9e1de5c83371d91e200254cceef70852f5f94fd2Lukas Slebodnik src_module_path = BUILD_DIR + "/.libs/_py3sss_murmur.so"
4a5a18f489f4d19aa0571528a7f0c7a8d35ac83fLukas Slebodnik else:
9e1de5c83371d91e200254cceef70852f5f94fd2Lukas Slebodnik src_module_path = BUILD_DIR + "/.libs/_py2sss_murmur.so"
9e1de5c83371d91e200254cceef70852f5f94fd2Lukas Slebodnik
9e1de5c83371d91e200254cceef70852f5f94fd2Lukas Slebodnik src_module_path = os.path.abspath(src_module_path)
9e1de5c83371d91e200254cceef70852f5f94fd2Lukas Slebodnik os.symlink(src_module_path, dest_module_path)
4a5a18f489f4d19aa0571528a7f0c7a8d35ac83fLukas Slebodnik
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose import pysss_murmur
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda except ImportError as e:
38cec21168b3b80c312e9efcfea4eba22a9648c8Fabiano Fidêncio print("Could not load the pysss_murmur module. "
38cec21168b3b80c312e9efcfea4eba22a9648c8Fabiano Fidêncio "Please check if it is compiled", file=sys.stderr)
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose raise e
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose self.assertEqual(pysss_murmur.__file__, MODPATH + "/pysss_murmur.so")
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnikclass PySssMurmurTestNeg(unittest.TestCase):
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik def test_invalid_arguments(self):
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik self.assertRaises(ValueError, pysss_murmur.murmurhash3, 1, 2, 3)
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik self.assertRaises(ValueError, pysss_murmur.murmurhash3, "test", 2)
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik self.assertRaises(ValueError, pysss_murmur.murmurhash3, "test")
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik self.assertRaises(ValueError, pysss_murmur.murmurhash3)
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik def test_invalid_length(self):
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik seed = 12345
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik self.assertRaises(ValueError, pysss_murmur.murmurhash3, "t", -1, seed)
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik # length is off by one
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik self.assertRaises(ValueError, pysss_murmur.murmurhash3, "test", 5,
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik seed)
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik self.assertRaises(ValueError, pysss_murmur.murmurhash3, "test",
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik 0xffffffffff, seed)
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnikclass PySssMurmurTestPos(unittest.TestCase):
9e1de5c83371d91e200254cceef70852f5f94fd2Lukas Slebodnik @classmethod
9e1de5c83371d91e200254cceef70852f5f94fd2Lukas Slebodnik def tearDownClass(cls):
9e1de5c83371d91e200254cceef70852f5f94fd2Lukas Slebodnik os.unlink(MODPATH + "/pysss_murmur.so")
9e1de5c83371d91e200254cceef70852f5f94fd2Lukas Slebodnik os.rmdir(MODPATH)
9e1de5c83371d91e200254cceef70852f5f94fd2Lukas Slebodnik
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose def testExpectedHash(self):
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik sid_str = "S-1-5-21-2153326666-2176343378-3404031434"
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik seed = 0xdeadbeef
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik hash_val = pysss_murmur.murmurhash3(sid_str, 0, seed)
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik self.assertEqual(hash_val, 233162409)
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik hash_val = pysss_murmur.murmurhash3(sid_str, len(sid_str), seed)
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik self.assertEqual(hash_val, 93103853)
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik def test_memory_cache_usage(self):
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik seed = 0xbeefdead
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik input_str = "test_user1"
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik input_len = len(input_str)
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik val_bin = pysss_murmur.murmurhash3(input_str + '\0',
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik input_len + 1, seed)
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik self.assertEqual(val_bin, 1198610880)
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik val_bin = pysss_murmur.murmurhash3(input_str + '\0' * 5,
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik input_len + 5, seed)
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik self.assertEqual(val_bin, 2917868047)
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose
60e51fd2764291df2332f36ff478777627d92b57Sumit Boseif __name__ == "__main__":
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose error = 0
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose suite = unittest.TestLoader().loadTestsFromTestCase(PySssMurmurImport)
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose res = unittest.TextTestRunner().run(suite)
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose if not res.wasSuccessful():
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose error |= 0x1
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose # need to bail out here because pysss_murmur could not be imported
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose sys.exit(error)
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose # import the pysss_murmur module into the global namespace, but make sure
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose # it's the one in tree
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose sys.path.insert(0, MODPATH)
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose import pysss_murmur
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik suite = unittest.TestLoader().loadTestsFromTestCase(PySssMurmurTestNeg)
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose res = unittest.TextTestRunner().run(suite)
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose if not res.wasSuccessful():
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose error |= 0x2
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik suite = unittest.TestLoader().loadTestsFromTestCase(PySssMurmurTestPos)
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik res = unittest.TextTestRunner().run(suite)
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik if not res.wasSuccessful():
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik error |= 0x4
8f83feea4586a8fcf4365e0d22e0a16740424cc2Lukas Slebodnik
60e51fd2764291df2332f36ff478777627d92b57Sumit Bose sys.exit(error)