From b111d50347076336b3e655178d967f8e5c8c9913 Mon Sep 17 00:00:00 2001 From: Prashanth Pai Date: Mon, 30 May 2016 15:08:48 +0530 Subject: Add validation decorators As glfs and glfd are pointers to memory locations, passing invalid values of glfs and glfd to the libgfapi C library can result in segfault. This patch introduces decorators that validate glfs and glfd before calling correspoding C APIs. Change-Id: I4e86bd8e436e23cd41f75f428d246939c820bb9c Signed-off-by: Prashanth Pai --- test/unit/gluster/test_gfapi.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'test/unit/gluster/test_gfapi.py') diff --git a/test/unit/gluster/test_gfapi.py b/test/unit/gluster/test_gfapi.py index 3934a6f..86fa621 100644 --- a/test/unit/gluster/test_gfapi.py +++ b/test/unit/gluster/test_gfapi.py @@ -11,6 +11,7 @@ import unittest import gluster +import inspect import os import stat import time @@ -70,6 +71,36 @@ class TestFile(unittest.TestCase): def tearDown(self): gluster.gfapi.api.glfs_close = self._saved_glfs_close + def test_validate_init(self): + self.assertRaises(ValueError, File, None) + self.assertRaises(ValueError, File, "not_int") + + try: + with File(None) as f: + pass + except ValueError: + pass + else: + self.fail("Expecting ValueError") + + try: + with File("not_int") as f: + pass + except ValueError: + pass + else: + self.fail("Expecting ValueError") + + def test_validate_glfd_decorator_applied(self): + for method_name, method_instance in \ + inspect.getmembers(File, predicate=inspect.ismethod): + if not method_name.startswith('_'): + try: + wrapper_attribute = method_instance.__wrapped__.__name__ + self.assertEqual(wrapper_attribute, method_name) + except AttributeError: + self.fail("Method File.%s isn't decorated" % (method_name)) + def test_fchmod_success(self): mock_glfs_fchmod = Mock() mock_glfs_fchmod.return_value = 0 -- cgit