From 368e54d4cff07efbb7f500f494b9f5f317e4e20e Mon Sep 17 00:00:00 2001
From: Philippe PITTOLI
Date: Thu, 8 Sep 2016 23:41:15 +0200
Subject: [PATCH] pubsubd: test programs
---
pubsub/msg-serialize.c | 48 +++++++++++++++++++++++++++++++++
pubsub/msg-unserialize.c | 43 +++++++++++++++++++++++++++++
pubsub/test-test-send-params.sh | 38 ++++++++++++++++++++++++++
3 files changed, 129 insertions(+)
create mode 100644 pubsub/msg-serialize.c
create mode 100644 pubsub/msg-unserialize.c
create mode 100755 pubsub/test-test-send-params.sh
diff --git a/pubsub/msg-serialize.c b/pubsub/msg-serialize.c
new file mode 100644
index 0000000..3138f57
--- /dev/null
+++ b/pubsub/msg-serialize.c
@@ -0,0 +1,48 @@
+#include "../lib/pubsubd.h"
+#include
+#include
+
+#define MESSAGE "coucou"
+#define CHAN "chan1"
+
+void
+ohshit(int rvalue, const char* str) {
+ fprintf (stderr, "\033[31merr: %s\033[00m\n", str);
+ exit (rvalue);
+}
+
+void usage (char **argv)
+{
+ printf ( "usage: %s\n", argv[0]);
+}
+
+int
+main(int argc, char **argv)
+{
+ if (argc != 1) {
+ usage (argv);
+ exit (1);
+ }
+
+ struct pubsub_msg msg;
+ memset (&msg, 0, sizeof (struct pubsub_msg));
+ msg.type = PUBSUB_TYPE_MESSAGE;
+ msg.chan = malloc (strlen (CHAN) + 1);
+ strncpy ((char *)msg.chan, CHAN, strlen (CHAN) + 1);
+ msg.chanlen = strlen (CHAN) + 1;
+
+ msg.data = malloc (strlen (MESSAGE) + 1);
+ strncpy ((char *)msg.data, MESSAGE, strlen (CHAN) + 1);
+ msg.datalen = strlen (MESSAGE) + 1;
+
+ char *data = NULL;
+ size_t len = 0;
+ pubsubd_msg_serialize (&msg, &data, &len);
+ pubsubd_msg_free (&msg);
+
+ if (len != write (1, data, len)) {
+ ohshit (1, "unable to write the data");
+ }
+
+ return EXIT_SUCCESS;
+}
diff --git a/pubsub/msg-unserialize.c b/pubsub/msg-unserialize.c
new file mode 100644
index 0000000..8596ebe
--- /dev/null
+++ b/pubsub/msg-unserialize.c
@@ -0,0 +1,43 @@
+#include "../lib/pubsubd.h"
+#include
+#include
+
+void
+ohshit(int rvalue, const char* str) {
+ fprintf (stderr, "\033[31merr: %s\033[00m\n", str);
+ exit (rvalue);
+}
+
+void usage (char **argv)
+{
+ printf ( "usage: cat msg | %s\n", argv[0]);
+}
+
+void msg_print (struct pubsub_msg *msg) {
+ printf ("msg: type=%d chan=%.*s, data=%.*s\n"
+ , msg->type
+ , msg->chanlen, msg->chan
+ , msg->datalen, msg->data);
+}
+
+int
+main(int argc, char **argv)
+{
+
+ if (argc != 1) {
+ usage (argv);
+ exit (1);
+ }
+
+ char data[BUFSIZ];
+ memset (data, 0, BUFSIZ);
+ size_t len = read (0, data, BUFSIZ);
+ printf ("msg len %ld\n", len);
+
+ struct pubsub_msg msg;
+ pubsubd_msg_unserialize (&msg, data, len);
+ msg_print (&msg);
+ pubsubd_msg_free (&msg);
+
+ return EXIT_SUCCESS;
+}
diff --git a/pubsub/test-test-send-params.sh b/pubsub/test-test-send-params.sh
new file mode 100755
index 0000000..dc0a980
--- /dev/null
+++ b/pubsub/test-test-send-params.sh
@@ -0,0 +1,38 @@
+#!/bin/bash
+
+# start pubsub-test-send alone, with some parameters
+# then test it with this script
+
+REP=/tmp/ipc/
+PID=10
+INDEX=1
+VERSION=1
+ACTION="pub"
+CHAN="chan1"
+
+if [ $# != 0 ] ; then
+ PID=$1
+ shift
+fi
+
+if [ $# != 0 ] ; then
+ INDEX=$1
+ shift
+fi
+
+if [ $# != 0 ] ; then
+ ACTION=$1
+ shift
+fi
+
+if [ $# != 0 ] ; then
+ CHAN=$1
+ shift
+fi
+
+echo "there should be a line in $REP/pubsub"
+cat $REP/pubsub
+
+echo ""
+echo "there should be something to read in $REP/${PID}-${INDEX}-out"
+cat $REP/${PID}-${INDEX}-out | xxd