From d769925c9819c012602595cc0f47c8a81444ca0e Mon Sep 17 00:00:00 2001 From: kali Date: Wed, 8 Aug 2012 19:29:14 +0900 Subject: bunch of tests for leap/util/fileutil --- src/leap/util/test_fileutil.py | 99 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/leap/util/test_fileutil.py (limited to 'src/leap/util') diff --git a/src/leap/util/test_fileutil.py b/src/leap/util/test_fileutil.py new file mode 100644 index 00000000..849decaf --- /dev/null +++ b/src/leap/util/test_fileutil.py @@ -0,0 +1,99 @@ +import os +import platform +import shutil +import stat +import tempfile +import unittest + +from leap.util import fileutil + + +class FileUtilTest(unittest.TestCase): + """ + test our file utils + """ + + def setUp(self): + self.system = platform.system() + self.create_temp_dir() + + def tearDown(self): + self.remove_temp_dir() + + # + # helpers + # + + def create_temp_dir(self): + self.tmpdir = tempfile.mkdtemp() + + def remove_temp_dir(self): + shutil.rmtree(self.tmpdir) + + def get_file_path(self, filename): + return os.path.join( + self.tmpdir, + filename) + + def touch_exec_file(self): + fp = self.get_file_path('testexec') + open(fp, 'w').close() + os.chmod( + fp, + stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR) + return fp + + def get_mode(self, fp): + return stat.S_IMODE(os.stat(fp).st_mode) + + # + # tests + # + + def test_is_user_executable(self): + """ + test that a 700 file + is an 700 file. kindda oximoronic, but... + """ + # XXX could check access X_OK + + fp = self.touch_exec_file() + mode = self.get_mode(fp) + self.assertEqual(mode, int('700', 8)) + + def test_which(self): + """ + not a very reliable test, + but I cannot think of anything smarter now + I guess it's highly improbable that copy + command is somewhere else..? + """ + # XXX yep, we can change the syspath + # for the test... ! + + if self.system == "Linux": + self.assertEqual( + fileutil.which('cp'), + '/bin/cp') + + def test_mkdir_p(self): + """ + test our mkdir -p implementation + """ + testdir = self.get_file_path( + os.path.join('test', 'foo', 'bar')) + self.assertEqual(os.path.isdir(testdir), False) + fileutil.mkdir_p(testdir) + self.assertEqual(os.path.isdir(testdir), True) + + def test_check_and_fix_urw_only(self): + """ + test function that fixes perms on + files that should be rw only for owner + """ + fp = self.touch_exec_file() + mode = self.get_mode(fp) + self.assertEqual(mode, int('700', 8)) + fileutil.check_and_fix_urw_only(fp) + mode = self.get_mode(fp) + self.assertEqual(mode, int('600', 8)) -- cgit v1.2.3 From e8c950c65ebd5bb4ba0dcbfac869e7b40b902b8c Mon Sep 17 00:00:00 2001 From: kali Date: Wed, 8 Aug 2012 19:29:56 +0900 Subject: fix bad permission check on check_and_fix_urw_only (was not testing the mode properly. gotcha!) --- src/leap/util/fileutil.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/leap/util') diff --git a/src/leap/util/fileutil.py b/src/leap/util/fileutil.py index cc3bf34b..429e4b12 100644 --- a/src/leap/util/fileutil.py +++ b/src/leap/util/fileutil.py @@ -96,7 +96,9 @@ def check_and_fix_urw_only(_file): test for 600 mode and try to set it if anything different found """ - mode = os.stat(_file).st_mode + mode = stat.S_IMODE( + os.stat(_file).st_mode) + if mode != int('600', 8): try: logger.warning( -- cgit v1.2.3 From 0ac0cbb9f6dd91a414747f2a59d5a9d1bbfee571 Mon Sep 17 00:00:00 2001 From: kali Date: Wed, 8 Aug 2012 19:36:17 +0900 Subject: stub test for leap_argparse --- src/leap/util/test_leap_argparse.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/leap/util/test_leap_argparse.py (limited to 'src/leap/util') diff --git a/src/leap/util/test_leap_argparse.py b/src/leap/util/test_leap_argparse.py new file mode 100644 index 00000000..1442e827 --- /dev/null +++ b/src/leap/util/test_leap_argparse.py @@ -0,0 +1,27 @@ +from argparse import Namespace +import unittest + +from leap.util import leap_argparse + + +class LeapArgParseTest(unittest.TestCase): + """ + Test argparse options for eip client + """ + + def setUp(self): + """ + get the parser + """ + self.parser = leap_argparse.build_parser() + + def test_debug_mode(self): + """ + test debug mode option + """ + opts = self.parser.parse_args( + ['--debug']) + self.assertEqual( + opts, + Namespace(config_file=None, + debug=True)) -- cgit v1.2.3