From dadd20bd0db0289ac4ad9f322f03de686b24c5a7 Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Wed, 25 Jan 2017 15:42:08 +0100 Subject: [PATCH] Fix leading dots on sanitized hostname Add unit test Fix typo --- salt/utils/sanitizers.py | 2 +- tests/unit/utils/sanitizers_test.py | 57 +++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 tests/unit/utils/sanitizers_test.py diff --git a/salt/utils/sanitizers.py b/salt/utils/sanitizers.py index d05d39955d..83f621151a 100644 --- a/salt/utils/sanitizers.py +++ b/salt/utils/sanitizers.py @@ -56,7 +56,7 @@ class InputSanitizer(object): :param value: :return: ''' - return re.sub(r'[^a-zA-Z0-9.-]', '', InputSanitizer.trim(value)) + return re.sub(r'[^a-zA-Z0-9.-]', '', InputSanitizer.trim(value)).strip('.') id = hostname diff --git a/tests/unit/utils/sanitizers_test.py b/tests/unit/utils/sanitizers_test.py new file mode 100644 index 0000000000..e9c333149c --- /dev/null +++ b/tests/unit/utils/sanitizers_test.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- + +# Import python libs +from __future__ import absolute_import +from salt.ext.six import text_type as text + +# Import Salt Libs +from salt.utils.sanitizers import clean + +# Import Salt Testing Libs +from salttesting import TestCase, skipIf +from salttesting.mock import NO_MOCK, NO_MOCK_REASON +from salttesting.helpers import ensure_in_syspath + +ensure_in_syspath('../../') + + +@skipIf(NO_MOCK, NO_MOCK_REASON) +class SanitizersTestCase(TestCase): + ''' + TestCase for sanitizers + ''' + def test_sanitized_trim(self): + ''' + Test sanitized input for trimming + ''' + value = u' sample ' + response = clean.trim(value) + assert response == 'sample' + assert type(response) == text + + def test_sanitized_filename(self): + ''' + Test sanitized input for filename + ''' + value = '/absolute/path/to/the/file.txt' + response = clean.filename(value) + assert response == 'file.txt' + + value = '../relative/path/to/the/file.txt' + response = clean.filename(value) + assert response == 'file.txt' + + def test_sanitized_hostname(self): + ''' + Test sanitized input for hostname (id) + ''' + value = ' ../ ../some/dubious/hostname ' + response = clean.hostname(value) + assert response == 'somedubioushostname' + + test_sanitized_id = test_sanitized_hostname + + +if __name__ == '__main__': + from integration import run_tests + run_tests(SanitizersTestCase, needs_daemon=False) -- 2.11.0