From 84e2da8dd4a9f876809db718af1cb2c65e05cfb5 Mon Sep 17 00:00:00 2001 From: Colin Patrick McCabe Date: Wed, 3 Nov 2010 12:03:32 -0700 Subject: [PATCH] Create TestEncoding to test serialization code Signed-off-by: Colin McCabe --- src/Makefile.am | 4 ++ src/test/TestEncoding.cc | 79 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 src/test/TestEncoding.cc diff --git a/src/Makefile.am b/src/Makefile.am index f30b24f3a5bb2..c784e305e2fc8 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -188,6 +188,10 @@ testtimers_SOURCES = test/TestTimers.cc testtimers_LDADD = libceph.la libcrush.la -lpthread -lm -lcrypto bin_PROGRAMS += testtimers +testencoding_SOURCES = test/TestEncoding.cc +testencoding_LDADD = libceph.la libcrush.la -lpthread -lm -lcrypto +bin_PROGRAMS += testencoding + endif # librados diff --git a/src/test/TestEncoding.cc b/src/test/TestEncoding.cc new file mode 100644 index 0000000000000..6d9feed89cabc --- /dev/null +++ b/src/test/TestEncoding.cc @@ -0,0 +1,79 @@ +#include "config.h" +#include "include/encoding.h" +#include "common/common_init.h" + +#include +#include +#include + +using std::cout; +using std::cerr; +using std::string; +using std::vector; + +// Test string serialization +static int test_string_sz(const char *str) +{ + bufferlist bl(100000); + + try { + // source + string src(str); + encode(src, bl); + + // dest + string dst; + bufferlist::iterator i(bl.begin()); + decode(dst, i); + if (src != dst) { + cout << "src = " << src << ", but dst = " << dst << std::endl; + return 1; + } + } + catch (const ceph::buffer::malformed_input &i) { + cout << "got exception " << i.what() << std::endl; + return 1; + } + catch (const std::exception &e) { + cout << "got exception " << e.what() << std::endl; + return 1; + } + catch (...) { + cout << "unknown exception!" << std::endl; + return 1; + } + return 0; +} + +int main(int argc, const char **argv) +{ + int ret = 0; + + vector args; + argv_to_vec(argc, argv, args); + env_to_vec(args); + + ceph_set_default_id("admin"); + common_set_defaults(false); + common_init(args, "ceph", true); + + ret = test_string_sz("I am the very model of a modern major general"); + if (ret) + goto done; + + ret = test_string_sz(""); + if (ret) + goto done; + + ret = test_string_sz("foo bar baz\n"); + if (ret) + goto done; + +done: + if (ret) { + cout << "FAILURE" << std::endl; + return(EXIT_FAILURE); + } + cout << "SUCCESS" << std::endl; + return(EXIT_SUCCESS); +} -- 2.39.5