diff --git a/keylime/common/validators.py b/keylime/common/validators.py index acc67e0a9..980d638bb 100644 --- a/keylime/common/validators.py +++ b/keylime/common/validators.py @@ -32,3 +32,29 @@ def valid_hex(value): except Exception: return False return True + + +def valid_uuid(uuid: str) -> bool: + """Check if the string is a valid UUID.""" + valid = False + try: + valid = bool( + re.fullmatch( + r"[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}", + uuid, + re.I, + ) + ) + except Exception: + pass + return valid + + +def valid_agent_id(agent_id: str) -> bool: + """Check if agent_id is valid.""" + valid = False + try: + valid = bool(re.fullmatch(r"[\w.-]+", agent_id)) + except Exception: + pass + return valid diff --git a/test/test_validators.py b/test/test_validators.py index b925ec38c..eb6d6bd29 100644 --- a/test/test_validators.py +++ b/test/test_validators.py @@ -78,5 +78,49 @@ def test_invalid(self): self.assertFalse(validators.valid_hex("123xyz")) +class TestValidUUID(unittest.TestCase): + """Tests for valid_uuid.""" + + def test_none(self): + """Check that None is not valid.""" + self.assertFalse(validators.valid_uuid(None)) + + def test_empty(self): + """Check that the empty string is not valid.""" + self.assertFalse(validators.valid_uuid("")) + + def test_valid(self): + """Check a valid UUID that mix upper and lower case.""" + self.assertTrue(validators.valid_uuid("74a93e15-da24-4ff1-ABC0-55beed02a16a")) + + def test_invalid(self): + """Check an invalid UUID that mix upper and lower case.""" + self.assertFalse(validators.valid_uuid("some text")) + + +class TestValidAgentID(unittest.TestCase): + """Tests for valid_agent_id.""" + + def test_none(self): + """Check that None is not valid.""" + self.assertFalse(validators.valid_agent_id(None)) + + def test_empty(self): + """Check that the empty string is not valid.""" + self.assertFalse(validators.valid_agent_id("")) + + def test_valid_uuid(self): + """Check a valid UUID that mix upper and lower case.""" + self.assertTrue(validators.valid_agent_id("74a93e15-da24-4ff1-ABC0-55beed02a16a")) + + def test_valid_hostname(self): + """Check a valid hostname that mix upper and lower case.""" + self.assertTrue(validators.valid_agent_id("my-Hostname.example.com")) + + def test_invalid(self): + """Check an invalid user ID with non valid characters.""" + self.assertFalse(validators.valid_agent_id("rm -fr *")) + + if __name__ == "__main__": unittest.main()