Revision 28f34048
b/lib/utils.py | ||
---|---|---|
81 | 81 |
HEX_CHAR_RE, HEX_CHAR_RE), |
82 | 82 |
re.S | re.I) |
83 | 83 |
|
84 |
_VALID_SERVICE_NAME_RE = re.compile("^[-_.a-zA-Z0-9]{1,128}$") |
|
85 |
|
|
84 | 86 |
# Structure definition for getsockopt(SOL_SOCKET, SO_PEERCRED, ...): |
85 | 87 |
# struct ucred { pid_t pid; uid_t uid; gid_t gid; }; |
86 | 88 |
# |
... | ... | |
1155 | 1157 |
return hostname |
1156 | 1158 |
|
1157 | 1159 |
|
1160 |
def ValidateServiceName(name): |
|
1161 |
"""Validate the given service name. |
|
1162 |
|
|
1163 |
@type name: number or string |
|
1164 |
@param name: Service name or port specification |
|
1165 |
|
|
1166 |
""" |
|
1167 |
try: |
|
1168 |
numport = int(name) |
|
1169 |
except (ValueError, TypeError): |
|
1170 |
# Non-numeric service name |
|
1171 |
valid = _VALID_SERVICE_NAME_RE.match(name) |
|
1172 |
else: |
|
1173 |
# Numeric port (protocols other than TCP or UDP might need adjustments |
|
1174 |
# here) |
|
1175 |
valid = (numport >= 0 and numport < (1 << 16)) |
|
1176 |
|
|
1177 |
if not valid: |
|
1178 |
raise errors.OpPrereqError("Invalid service name '%s'" % name, |
|
1179 |
errors.ECODE_INVAL) |
|
1180 |
|
|
1181 |
return name |
|
1182 |
|
|
1183 |
|
|
1158 | 1184 |
def GetHostInfo(name=None): |
1159 | 1185 |
"""Lookup host name and raise an OpPrereqError for failures""" |
1160 | 1186 |
|
b/test/ganeti.utils_unittest.py | ||
---|---|---|
1910 | 1910 |
HostInfo.NormalizeName(value) |
1911 | 1911 |
|
1912 | 1912 |
|
1913 |
class TestValidateServiceName(unittest.TestCase): |
|
1914 |
def testValid(self): |
|
1915 |
testnames = [ |
|
1916 |
0, 1, 2, 3, 1024, 65000, 65534, 65535, |
|
1917 |
"ganeti", |
|
1918 |
"gnt-masterd", |
|
1919 |
"HELLO_WORLD_SVC", |
|
1920 |
"hello.world.1", |
|
1921 |
"0", "80", "1111", "65535", |
|
1922 |
] |
|
1923 |
|
|
1924 |
for name in testnames: |
|
1925 |
self.assertEqual(utils.ValidateServiceName(name), name) |
|
1926 |
|
|
1927 |
def testInvalid(self): |
|
1928 |
testnames = [ |
|
1929 |
-15756, -1, 65536, 133428083, |
|
1930 |
"", "Hello World!", "!", "'", "\"", "\t", "\n", "`", |
|
1931 |
"-8546", "-1", "65536", |
|
1932 |
(129 * "A"), |
|
1933 |
] |
|
1934 |
|
|
1935 |
for name in testnames: |
|
1936 |
self.assertRaises(OpPrereqError, utils.ValidateServiceName, name) |
|
1937 |
|
|
1938 |
|
|
1913 | 1939 |
class TestParseAsn1Generalizedtime(unittest.TestCase): |
1914 | 1940 |
def test(self): |
1915 | 1941 |
# UTC |
Also available in: Unified diff