This is an automated email from the ASF dual-hosted git repository.
rrm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git
The following commit(s) were added to refs/heads/master by this push:
new 32c7d8c Convert mgmt/utils unit tests to use Catch.
32c7d8c is described below
commit 32c7d8c29096dca124dc074950005601c9bfbf8d
Author: Walter Karas <[email protected]>
AuthorDate: Tue Mar 24 11:09:28 2020 -0500
Convert mgmt/utils unit tests to use Catch.
---
.gitignore | 2 +-
mgmt/utils/Makefile.am | 16 ++-
mgmt/utils/{ => unit_tests}/test_marshall.cc | 208 +++++++++++++++------------
mgmt/utils/unit_tests/unit_test_main.cc | 25 ++++
4 files changed, 153 insertions(+), 98 deletions(-)
diff --git a/.gitignore b/.gitignore
index 8ed1207..2b7e297 100644
--- a/.gitignore
+++ b/.gitignore
@@ -149,7 +149,7 @@ mgmt/tools/traffic_time_config
mgmt/tools/traffic_vip_config
mgmt/api/api_cli_remote
mgmt/tools/shmem_clean
-mgmt/utils/test_marshall
+mgmt/utils/test_mgmt_utils
libtool
m4/libtool.m4
diff --git a/mgmt/utils/Makefile.am b/mgmt/utils/Makefile.am
index 8c366b1..610d1fd 100644
--- a/mgmt/utils/Makefile.am
+++ b/mgmt/utils/Makefile.am
@@ -32,7 +32,7 @@ AM_CPPFLAGS += \
EXTRA_DIST = MgmtSocket.h
noinst_LTLIBRARIES = libutils_lm.la libutils_p.la
-check_PROGRAMS = test_marshall
+check_PROGRAMS = test_mgmt_utils
TESTS = $(check_PROGRAMS)
@@ -54,15 +54,19 @@ libutils_p_la_SOURCES = \
$(libutils_COMMON) \
MgmtProcessCleanup.cc
-test_marshall_LDFLAGS = \
+test_mgmt_utils_CPPFLAGS = $(AM_CPPFLAGS) -I$(abs_top_srcdir)/tests/include
+
+test_mgmt_utils_LDFLAGS = \
@AM_LDFLAGS@ \
@OPENSSL_LDFLAGS@
-test_marshall_SOURCES = test_marshall.cc
-test_marshall_LDADD = \
+test_mgmt_utils_SOURCES = \
+ unit_tests/unit_test_main.cc \
+ unit_tests/test_marshall.cc
+
+test_mgmt_utils_LDADD = \
libutils_p.la \
- $(top_builddir)/src/tscore/libtscore.la \
- $(top_builddir)/src/tscpp/util/libtscpputil.la
+ $(top_builddir)/src/tscore/libtscore.la
include $(top_srcdir)/build/tidy.mk
diff --git a/mgmt/utils/test_marshall.cc
b/mgmt/utils/unit_tests/test_marshall.cc
similarity index 61%
rename from mgmt/utils/test_marshall.cc
rename to mgmt/utils/unit_tests/test_marshall.cc
index e94ce25..b2bef2d 100644
--- a/mgmt/utils/test_marshall.cc
+++ b/mgmt/utils/unit_tests/test_marshall.cc
@@ -16,35 +16,72 @@
* limitations under the License.
*/
-#include "tscore/ink_defs.h"
-#include "tscore/ink_thread.h"
-#include "tscore/ink_inet.h"
+#include <iostream>
+#include <string_view>
+#include <cstdint>
+
+#include <tscore/ink_defs.h>
+#include <tscore/ink_thread.h>
+#include <tscore/ink_inet.h>
-#include "tscore/TestBox.h"
#include <MgmtMarshall.h>
#include <MgmtSocket.h>
-#define CHECK_EQ(expr, len)
\
- do {
\
- MgmtMarshallInt rcvd = static_cast<MgmtMarshallInt>(expr);
\
- box.check(rcvd == static_cast<MgmtMarshallInt>(len), "%s returned length
%d, expected %d", #expr, rcvd, \
- static_cast<MgmtMarshallInt>(len));
\
- } while (0)
-
-#define CHECK_VALUE(value, expect, fmt)
\
- do {
\
- box.check((value) == (expect), "received marshalled value " fmt ",
expected " fmt "", (value), (expect)); \
- } while (0)
-
-// The NULL string is marshalled the same as the empty string.
-#define CHECK_STRING(value, expect)
\
- do {
\
- if (value) {
\
- box.check(strcmp((value), (expect)) == 0, "received marshalled value
'%s', expected '%s'", (value), (expect)); \
- } else {
\
- box.check(strcmp((expect), "") == 0, "received marshalled value '%s',
expected ''", (expect)); \
- }
\
- } while (0)
+#include <catch.hpp>
+
+namespace
+{
+bool
+check_eq(char const *expr_as_str, MgmtMarshallInt rcvd, MgmtMarshallInt len)
+{
+ if (rcvd == len) {
+ return true;
+ }
+ std::cout << expr_as_str << " returned length " << rcvd << ", expected " <<
len << '\n';
+ return false;
+}
+
+#define CHECK_EQ(expr, len) CHECK(check_eq(#expr,
static_cast<MgmtMarshallInt>(expr), static_cast<MgmtMarshallInt>(len)))
+
+template <typename V_t, typename E_t>
+bool
+check_value(V_t value, E_t expect)
+{
+ if (sizeof(V_t) > sizeof(E_t) ? (value == static_cast<V_t>(expect)) :
(static_cast<E_t>(value) == expect)) {
+ return true;
+ }
+ std::cout << "received marshalled value " << value << ", expected " <<
expect << '\n';
+ return false;
+}
+
+bool
+check_value(void *value, void *expect)
+{
+ if (value == expect) {
+ return true;
+ }
+ std::cout << std::hex << "received marshalled value " <<
reinterpret_cast<std::uintptr_t>(value) << ", expected "
+ << reinterpret_cast<std::uintptr_t>(expect) << '\n'
+ << std::dec;
+ return false;
+}
+
+#define CHECK_VALUE(value, expect) CHECK(check_value((value), (expect)))
+
+bool
+check_str(char const *value, char const *expect)
+{
+ if (!value) {
+ value = "";
+ }
+ if (std::string_view(value) == expect) {
+ return true;
+ }
+ std::cout << "received marshalled value " << value << ", expected " <<
expect << '\n';
+ return false;
+}
+
+#define CHECK_STR(value, expect) CHECK(check_str((value), (expect)))
const MgmtMarshallType inval[] = {static_cast<MgmtMarshallType>(1568)};
@@ -65,14 +102,14 @@ const MgmtMarshallType afields[] = {
const char alpha[] = "abcdefghijklmnopqrstuvwxyz0123456789";
const char *stringvals[] = {nullptr, "", "randomstring"};
-static bool
+bool
errno_is_continue()
{
return errno == EALREADY || errno == EWOULDBLOCK || errno == EINPROGRESS ||
errno == EAGAIN || mgmt_transient_error();
}
-static int
-message_connect_channel(RegressionTest *t, int listenfd, int clientfd, int
serverport)
+int
+message_connect_channel(int listenfd, int clientfd, int serverport)
{
// bool need_connect = true;
bool need_accept = true;
@@ -93,7 +130,7 @@ message_connect_channel(RegressionTest *t, int listenfd, int
clientfd, int serve
while (need_accept) {
serverfd = accept(listenfd, nullptr, nullptr);
if (serverfd == -1) {
- rprintf(t, "accepting, %d %s\n", errno, strerror(errno));
+ std::cout << "accepting, " << errno << ' ' << strerror(errno) << '\n';
if (!errno_is_continue()) {
return -1;
}
@@ -106,7 +143,7 @@ message_connect_channel(RegressionTest *t, int listenfd,
int clientfd, int serve
return serverfd;
}
-static int
+int
message_listen(int &port)
{
IpEndpoint sa;
@@ -114,34 +151,27 @@ message_listen(int &port)
int fd;
fd = mgmt_socket(AF_INET, SOCK_STREAM, 0);
- if (fd == -1) {
- goto fail;
- }
+ if (fd >= 0) {
+ sa.setToAnyAddr(AF_INET);
+ if (bind(fd, &sa.sa, sizeof(sa.sin)) >= 0) {
+ slen = sizeof(sa);
+ if (getsockname(fd, &sa.sa, &slen) >= -1) {
+ port = ntohs(ats_ip_port_cast(&sa.sa));
- sa.setToAnyAddr(AF_INET);
- if (bind(fd, &sa.sa, sizeof(sa.sin)) == -1) {
- goto fail;
- }
+ listen(fd, 5);
- slen = sizeof(sa);
- if (getsockname(fd, &sa.sa, &slen) == -1) {
- goto fail;
+ return fd;
+ }
+ }
+ close(fd);
}
-
- port = ntohs(ats_ip_port_cast(&sa.sa));
-
- listen(fd, 5);
- return fd;
-
-fail:
- close(fd);
return -1;
}
-REGRESSION_TEST(MessageReadWriteA)(RegressionTest *t, int /* atype ATS_UNUSED
*/, int *pstatus)
-{
- TestBox box(t, pstatus, REGRESSION_TEST_PASSED);
+} // end anonymous namespace
+TEST_CASE("MessageReadWriteA", "[mgmt_utils][msg_rd_wr_a]")
+{
int listenfd = -1;
int serverfd = -1;
int clientfd = -1;
@@ -154,14 +184,14 @@ REGRESSION_TEST(MessageReadWriteA)(RegressionTest *t, int
/* atype ATS_UNUSED */
clientfd = mgmt_socket(AF_INET, SOCK_STREAM, 0);
listenfd = message_listen(serverport);
- serverfd = message_connect_channel(t, listenfd, clientfd, serverport);
- rprintf(t, "listenfd=%d clientfd=%d, serverfd=%d, port=%d\n", listenfd,
clientfd, serverfd, serverport);
+ serverfd = message_connect_channel(listenfd, clientfd, serverport);
+ std::cout << "listenfd=" << listenfd << " clientfd=" << clientfd << "
serverfd=" << serverfd << " port=" << serverport << '\n';
fcntl(clientfd, F_SETFL, O_NDELAY);
fcntl(serverfd, F_SETFL, O_NDELAY);
mint = 99;
- mlong = (MgmtMarshallLong)(&listenfd);
+ mlong = reinterpret_cast<MgmtMarshallLong>(&listenfd);
// Check invalid Fd write. ToDo: Commented out, see TS-3052.
// CHECK_EQ(mgmt_message_write(FD_SETSIZE - 1, ifields, countof(ifields),
&mint, &mlong), -1);
@@ -171,13 +201,13 @@ REGRESSION_TEST(MessageReadWriteA)(RegressionTest *t, int
/* atype ATS_UNUSED */
mint = 0;
mlong = 0;
CHECK_EQ(mgmt_message_read(serverfd, ifields, countof(ifields), &mint,
&mlong), 12);
- CHECK_VALUE(mint, 99, "%" PRId32);
- CHECK_VALUE(mlong, (MgmtMarshallLong)(&listenfd), "%" PRId64);
+ CHECK_VALUE(mint, 99);
+ CHECK_VALUE(mlong, reinterpret_cast<MgmtMarshallLong>(&listenfd));
// Marshall a string.
for (unsigned i = 0; i < countof(stringvals); ++i) {
- const char *s = stringvals[i];
- size_t len = 4 /* length */ + (s ? strlen(s) : 0) /* bytes */ + 1 /*
NULL */;
+ const char *s = stringvals[i];
+ std::size_t len = 4 /* length */ + (s ? std::strlen(s) : 0) /* bytes */ +
1 /* NULL */;
mstring = s ? ats_strdup(s) : nullptr;
CHECK_EQ(mgmt_message_write(clientfd, sfields, countof(sfields),
&mstring), len);
@@ -185,21 +215,24 @@ REGRESSION_TEST(MessageReadWriteA)(RegressionTest *t, int
/* atype ATS_UNUSED */
mstring = nullptr;
CHECK_EQ(mgmt_message_read(serverfd, sfields, countof(sfields), &mstring),
len);
- CHECK_STRING(s, mstring);
+ CHECK_STR(s, mstring);
ats_free(mstring);
mstring = nullptr;
}
// Marshall data.
mdata.ptr = ats_strdup(alpha);
- mdata.len = strlen(alpha);
- CHECK_EQ(mgmt_message_write(clientfd, dfields, countof(dfields), &mdata), 4
+ strlen(alpha));
+ mdata.len = std::strlen(alpha);
+ CHECK_EQ(mgmt_message_write(clientfd, dfields, countof(dfields), &mdata), 4
+ std::strlen(alpha));
ats_free(mdata.ptr);
ink_zero(mdata);
- CHECK_EQ(mgmt_message_read(serverfd, dfields, countof(dfields), &mdata), 4 +
strlen(alpha));
- CHECK_VALUE(mdata.len, strlen(alpha), "%zu");
- box.check(memcmp(mdata.ptr, alpha, strlen(alpha)) == 0, "unexpected mdata
contents");
+ CHECK_EQ(mgmt_message_read(serverfd, dfields, countof(dfields), &mdata), 4 +
std::strlen(alpha));
+ CHECK_VALUE(mdata.len, std::strlen(alpha));
+ if (std::memcmp(mdata.ptr, alpha, std::strlen(alpha)) != 0) {
+ std::cout << "unexpected mdata contents\n";
+ CHECK(false);
+ }
ats_free(mdata.ptr);
ink_zero(mdata);
@@ -208,10 +241,8 @@ REGRESSION_TEST(MessageReadWriteA)(RegressionTest *t, int
/* atype ATS_UNUSED */
close(serverfd);
}
-REGRESSION_TEST(MessageMarshall)(RegressionTest *t, int /* atype ATS_UNUSED
*/, int *pstatus)
+TEST_CASE("MessageMarshall", "[mgmt_utils][msg_marshall]")
{
- TestBox box(t, pstatus, REGRESSION_TEST_PASSED);
-
char msgbuf[4096];
MgmtMarshallInt mint = 0;
@@ -232,13 +263,13 @@ REGRESSION_TEST(MessageMarshall)(RegressionTest *t, int
/* atype ATS_UNUSED */,
CHECK_EQ(mgmt_message_marshall(msgbuf, sizeof(msgbuf), ifields,
countof(ifields), &mint, &mlong), 12);
CHECK_EQ(mgmt_message_parse(msgbuf, 1, ifields, countof(ifields), &mint,
&mlong), -1);
CHECK_EQ(mgmt_message_parse(msgbuf, sizeof(msgbuf), ifields,
countof(ifields), &mint, &mlong), 12);
- CHECK_VALUE(mint, -156, "%" PRId32);
- CHECK_VALUE(mlong, static_cast<MgmtMarshallLong>(UINT32_MAX), "%" PRId64);
+ CHECK_VALUE(mint, -156);
+ CHECK_VALUE(mlong, static_cast<MgmtMarshallLong>(UINT32_MAX));
// Marshall a string.
for (unsigned i = 0; i < countof(stringvals); ++i) {
const char *s = stringvals[i];
- size_t len = 4 /* length */ + (s ? strlen(s) : 0) /* bytes */ + 1 /*
NULL */;
+ size_t len = 4 /* length */ + (s ? std::strlen(s) : 0) /* bytes */ + 1
/* NULL */;
mstring = s ? ats_strdup(s) : nullptr;
CHECK_EQ(mgmt_message_marshall(msgbuf, 1, sfields, countof(sfields),
&mstring), -1);
@@ -248,40 +279,41 @@ REGRESSION_TEST(MessageMarshall)(RegressionTest *t, int
/* atype ATS_UNUSED */,
CHECK_EQ(mgmt_message_parse(msgbuf, 1, sfields, countof(sfields),
&mstring), -1);
CHECK_EQ(mgmt_message_parse(msgbuf, sizeof(msgbuf), sfields,
countof(sfields), &mstring), len);
- CHECK_STRING(s, mstring);
+ CHECK_STR(s, mstring);
ats_free(mstring);
mstring = nullptr;
}
// Marshall data.
mdata.ptr = ats_strdup(alpha);
- mdata.len = strlen(alpha);
+ mdata.len = std::strlen(alpha);
CHECK_EQ(mgmt_message_marshall(msgbuf, 10, dfields, countof(dfields),
&mdata), -1);
- CHECK_EQ(mgmt_message_marshall(msgbuf, sizeof(msgbuf), dfields,
countof(dfields), &mdata), 4 + strlen(alpha));
+ CHECK_EQ(mgmt_message_marshall(msgbuf, sizeof(msgbuf), dfields,
countof(dfields), &mdata), 4 + std::strlen(alpha));
ats_free(mdata.ptr);
ink_zero(mdata);
- CHECK_EQ(mgmt_message_parse(msgbuf, strlen(alpha), dfields,
countof(dfields), &mdata), -1);
- CHECK_EQ(mgmt_message_parse(msgbuf, strlen(alpha) + 4, dfields,
countof(dfields), &mdata), 4 + strlen(alpha));
- CHECK_VALUE(mdata.len, strlen(alpha), "%zu");
- box.check(memcmp(mdata.ptr, alpha, strlen(alpha)) == 0, "unexpected mdata
contents");
+ CHECK_EQ(mgmt_message_parse(msgbuf, std::strlen(alpha), dfields,
countof(dfields), &mdata), -1);
+ CHECK_EQ(mgmt_message_parse(msgbuf, std::strlen(alpha) + 4, dfields,
countof(dfields), &mdata), 4 + std::strlen(alpha));
+ CHECK_VALUE(mdata.len, std::strlen(alpha));
+ if (std::memcmp(mdata.ptr, alpha, std::strlen(alpha)) != 0) {
+ std::cout << "unexpected mdata contents\n";
+ CHECK(false);
+ }
ats_free(mdata.ptr);
ink_zero(mdata);
// Marshall empty data.
CHECK_EQ(mgmt_message_marshall(msgbuf, sizeof(msgbuf), dfields,
countof(dfields), &mdata), 4);
- mdata.ptr = (void *)99;
+ mdata.ptr = reinterpret_cast<void *>(99);
mdata.len = 1000;
CHECK_EQ(mgmt_message_parse(msgbuf, sizeof(msgbuf), dfields,
countof(dfields), &mdata), 4);
- CHECK_VALUE(mdata.ptr, (void *)nullptr, "%p");
- CHECK_VALUE(mdata.len, (size_t)0, "%zu");
+ CHECK_VALUE(mdata.ptr, static_cast<void *>(nullptr));
+ CHECK_VALUE(mdata.len, 0);
}
-REGRESSION_TEST(MessageLength)(RegressionTest *t, int /* atype ATS_UNUSED */,
int *pstatus)
+TEST_CASE("MessageLemgth", "[mgmt_utils][msg_len]")
{
- TestBox box(t, pstatus, REGRESSION_TEST_PASSED);
-
MgmtMarshallInt mint = 0;
MgmtMarshallLong mlong = 0;
MgmtMarshallString mstring = nullptr;
@@ -296,13 +328,13 @@ REGRESSION_TEST(MessageLength)(RegressionTest *t, int /*
atype ATS_UNUSED */, in
CHECK_EQ(mgmt_message_length(ifields, countof(ifields), &mint, &mlong), 12);
// string messages include a 4-byte length and the NULL
- mstring = (char *)"foo";
+ mstring = const_cast<char *>("foo");
CHECK_EQ(mgmt_message_length(sfields, countof(sfields), &mstring),
sizeof("foo") + 4);
// NULL strings are the same as empty strings ...
mstring = nullptr;
CHECK_EQ(mgmt_message_length(sfields, countof(sfields), &mstring), 4 + 1);
- mstring = (char *)"";
+ mstring = const_cast<char *>("");
CHECK_EQ(mgmt_message_length(sfields, countof(sfields), &mstring), 4 + 1);
// data fields include a 4-byte length. We don't go looking at the data in
this case.
@@ -319,9 +351,3 @@ REGRESSION_TEST(MessageLength)(RegressionTest *t, int /*
atype ATS_UNUSED */, in
mdata.len = 0;
CHECK_EQ(mgmt_message_length(dfields, countof(dfields), &mdata), 4);
}
-
-int
-main(int argc, const char **argv)
-{
- return RegressionTest::main(argc, argv, REGRESSION_TEST_QUICK);
-}
diff --git a/mgmt/utils/unit_tests/unit_test_main.cc
b/mgmt/utils/unit_tests/unit_test_main.cc
new file mode 100644
index 0000000..940d865
--- /dev/null
+++ b/mgmt/utils/unit_tests/unit_test_main.cc
@@ -0,0 +1,25 @@
+/** @file
+
+ This file used for catch based tests. It is the main() stub.
+
+ @section license License
+
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ */
+
+#define CATCH_CONFIG_MAIN
+#include <catch.hpp>