Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
validators: create validators module
Move from the config module some validators, and create the validators module. Add tests for them. Signed-off-by: Alberto Planas <aplanas@suse.com>
- Loading branch information
Showing
6 changed files
with
127 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"""Validators module.""" | ||
import re | ||
|
||
|
||
def valid_regex(regex): | ||
"""Check if string is a valid regular expression.""" | ||
if regex is None: | ||
return True, None, None | ||
|
||
try: | ||
compiled_regex = re.compile(regex) | ||
except re.error as regex_err: | ||
err = "Invalid regex: " + regex_err.msg + "." | ||
return False, None, err | ||
|
||
return True, compiled_regex, None | ||
|
||
|
||
def valid_exclude_list(exclude_list): | ||
"""Check if the list is composed of valid regex.""" | ||
if not exclude_list: | ||
return True, None, None | ||
|
||
combined_regex = "(" + ")|(".join(exclude_list) + ")" | ||
return valid_regex(combined_regex) | ||
|
||
|
||
def valid_hex(value): | ||
"""Check if the string is a valid hex number representation.""" | ||
try: | ||
int(value, 16) | ||
except Exception: | ||
return False | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import unittest | ||
|
||
from keylime.common import validators | ||
|
||
|
||
class TestValidRegex(unittest.TestCase): | ||
"""Tests for valid_regex.""" | ||
|
||
def test_none(self): | ||
"""Check that None is a valid regex.""" | ||
self.assertEqual(validators.valid_regex(None), (True, None, None)) | ||
|
||
def test_valid(self): | ||
"""Check a well formed regex.""" | ||
value = validators.valid_regex(r"a.*") | ||
self.assertTrue(value[0]) | ||
self.assertEqual(value[1].pattern, r"a.*") | ||
self.assertEqual(value[2], None) | ||
|
||
def test_invalid(self): | ||
"""Check a not valid regex.""" | ||
value = validators.valid_regex(r"a[") | ||
self.assertEqual( | ||
value, (False, None, "Invalid regex: unterminated character set.") | ||
) | ||
|
||
|
||
class TestValidExcludeList(unittest.TestCase): | ||
"""Tests for valid_exclude_list.""" | ||
|
||
def test_none(self): | ||
"""Check that the empty list is valid.""" | ||
self.assertEqual(validators.valid_exclude_list(None), (True, None, None)) | ||
|
||
def test_single(self): | ||
"""Check a single exclude list element.""" | ||
value = validators.valid_exclude_list([r"a.*"]) | ||
self.assertTrue(value[0]) | ||
self.assertEqual(value[1].pattern, r"(a.*)") | ||
self.assertEqual(value[2], None) | ||
|
||
def test_multi(self): | ||
"""Check a multiple elements exclude list.""" | ||
value = validators.valid_exclude_list([r"a.*", r"b.*"]) | ||
self.assertTrue(value[0]) | ||
self.assertEqual(value[1].pattern, r"(a.*)|(b.*)") | ||
self.assertEqual(value[2], None) | ||
|
||
def test_invalid(self): | ||
"""Check an invalid exclude list.""" | ||
value = validators.valid_exclude_list([r"a["]) | ||
self.assertEqual( | ||
value, (False, None, "Invalid regex: unterminated character set.") | ||
) | ||
|
||
|
||
class TestValidHex(unittest.TestCase): | ||
"""Tests for valid_hex.""" | ||
|
||
def test_none(self): | ||
"""Check that None is not valid.""" | ||
self.assertFalse(validators.valid_hex(None)) | ||
|
||
def test_empty(self): | ||
"""Check that the empty string is not valid.""" | ||
self.assertFalse(validators.valid_hex("")) | ||
|
||
def test_valid_lower(self): | ||
"""Check a valid lower case hexadecimal number.""" | ||
self.assertTrue(validators.valid_hex("123abc")) | ||
|
||
def test_valid_upper(self): | ||
"""Check a valid upper case hexadecimal number.""" | ||
self.assertTrue(validators.valid_hex("123ABC")) | ||
|
||
def test_invalid(self): | ||
"""Check and invalid hexadecimal number.""" | ||
self.assertFalse(validators.valid_hex("123xyz")) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |