From 316a68e9f75f8cd730c9545333f0d32b8d4f1a3b Mon Sep 17 00:00:00 2001 From: Kent Slaney Date: Tue, 28 Nov 2023 00:30:27 -0800 Subject: [PATCH] regression test for host parse --- libmc/_client.pyx | 1 - src/Common.cpp | 5 +++-- tests/test_unix.cpp | 11 +++++++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/libmc/_client.pyx b/libmc/_client.pyx index 9ad7c2d6..785a44a6 100644 --- a/libmc/_client.pyx +++ b/libmc/_client.pyx @@ -394,7 +394,6 @@ cdef class PyClient: Py_INCREF(servers_) for i in range(n): c_split = splitServerString(servers_[i]) - host, port, alias = c_split c_hosts[i] = c_split.host c_ports[i] = MC_DEFAULT_PORT if c_split.port == NULL else atoi(c_split.port) diff --git a/src/Common.cpp b/src/Common.cpp index be1a98b6..9826666b 100644 --- a/src/Common.cpp +++ b/src/Common.cpp @@ -60,12 +60,13 @@ ServerSpec splitServerString(char* input) { for (;;) { switch (*(++input)) { + case '\0': + return res; case ':': // invalid in a UNIX path *input = '\0'; res.port = input + 1; num = true; - case '\0': - return res; + continue; case ' ': if (!escaped) { *input = '\0'; diff --git a/tests/test_unix.cpp b/tests/test_unix.cpp index 5341aae8..0646f084 100644 --- a/tests/test_unix.cpp +++ b/tests/test_unix.cpp @@ -1,3 +1,4 @@ +#include "Common.h" #include "Client.h" #include "test_common.h" @@ -6,9 +7,19 @@ using douban::mc::Client; using douban::mc::tests::newUnixClient; +using douban::mc::splitServerString; +using douban::mc::ServerSpec; TEST(test_unix, establish_connection) { Client* client = newUnixClient(); EXPECT_TRUE(client != NULL); delete client; } + +TEST(test_unix, host_parse_regression) { + char test[] = "127.0.0.1:21211 testing"; + ServerSpec out = splitServerString(test); + ASSERT_STREQ(out.host, "127.0.0.1"); + ASSERT_STREQ(out.port, "21211"); + ASSERT_STREQ(out.alias, "testing"); +}