]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
Create TestEncoding to test serialization code
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Wed, 3 Nov 2010 19:03:32 +0000 (12:03 -0700)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Wed, 3 Nov 2010 19:18:56 +0000 (12:18 -0700)
Signed-off-by: Colin McCabe <colinm@hq.newdream.net>
src/Makefile.am
src/test/TestEncoding.cc [new file with mode: 0644]

index f30b24f3a5bb291c90a21bf58fec794e0cfa30ca..c784e305e2fc8779f2ec56617f54b705806ce15c 100644 (file)
@@ -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 (file)
index 0000000..6d9feed
--- /dev/null
@@ -0,0 +1,79 @@
+#include "config.h"
+#include "include/encoding.h"
+#include "common/common_init.h"
+
+#include <iostream>
+#include <stdlib.h>
+#include <vector>
+
+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<const char*> 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);
+}