Client can now connect to servers interactively

This commit is contained in:
2024-02-22 17:30:24 -05:00
parent e68d63915d
commit 7f7bd5a781
8 changed files with 129 additions and 29 deletions

View File

@@ -55,6 +55,30 @@ bool valid_double(string_view value) {
return true;
}
bool valid_hostname(string_view value) {
bool start_word = true;
bool last_dash = false;
for (char c : value) {
if (c == '.') {
if (start_word) return false;
if (last_dash) return false;
start_word = true;
last_dash = false;
} else if (c == '-') {
if (start_word) return false;
start_word = false;
last_dash = true;
} else if (ascii_isalnum(c)) {
start_word = false;
last_dash = false;
} else {
return false;
}
}
if (start_word || last_dash) return false;
return true;
}
int64_t to_int64(string_view value, int64_t errval) {
int64_t result;
const char *p = value.data();
@@ -810,7 +834,18 @@ static std::string_view read_number_x(const char *p, bool plus, bool minus, bool
}
LuaDefine(unittests_util, "", "some unit tests") {
// test str_to_int64, str_to_double
// Test valid_hostname
LuaAssert(L, sv::valid_hostname("foo123"));
LuaAssert(L, !sv::valid_hostname("foo%123"));
LuaAssert(L, sv::valid_hostname("foo-bar"));
LuaAssert(L, !sv::valid_hostname("-foo"));
LuaAssert(L, !sv::valid_hostname("foo-"));
LuaAssert(L, sv::valid_hostname("foo.bar.baz"));
LuaAssert(L, sv::valid_hostname("foo-bar.baz"));
LuaAssert(L, !sv::valid_hostname("foo-bar-.baz"));
LuaAssert(L, !sv::valid_hostname("foo.-bar-baz"));
// test str_to_int64, str_to_double
LuaAssert(L, sv::to_int64("123") == 123);
LuaAssert(L, sv::to_int64("123.4") == INT64_MAX);
LuaAssert(L, sv::to_int64("12ab") == INT64_MAX);