[maemo-commits] [maemo-commits] r15054 - in projects/haf/trunk/maemo-launcher: . launcher

From: subversion at stage.maemo.org subversion at stage.maemo.org
Date: Thu Jan 10 23:21:59 EET 2008
Author: guillem
Date: 2008-01-10 23:21:51 +0200 (Thu, 10 Jan 2008)
New Revision: 15054

Added:
   projects/haf/trunk/maemo-launcher/launcher/comm_msg.c
   projects/haf/trunk/maemo-launcher/launcher/comm_msg.h
   projects/haf/trunk/maemo-launcher/launcher/test_lib.c
   projects/haf/trunk/maemo-launcher/launcher/test_lib.h
   projects/haf/trunk/maemo-launcher/launcher/test_msg.c
Modified:
   projects/haf/trunk/maemo-launcher/ChangeLog
   projects/haf/trunk/maemo-launcher/launcher/Makefile.am
Log:
Add a tiny serializer and a test suite


Modified: projects/haf/trunk/maemo-launcher/ChangeLog
===================================================================
--- projects/haf/trunk/maemo-launcher/ChangeLog	2008-01-10 14:41:25 UTC (rev 15053)
+++ projects/haf/trunk/maemo-launcher/ChangeLog	2008-01-10 21:21:51 UTC (rev 15054)
@@ -1,3 +1,15 @@
+2008-01-10  Guillem Jover  <guillem.jover at nokia.com>
+
+	* launcher/comm_msg.h: New file.
+	* launcher/comm_msg.c: Likewise.
+	* launcher/test_lib.h: Likewise.
+	* launcher/test_lib.c: Likewise.
+	* launcher/test_msg.c: Likewise.
+	* launcher/Makefile.am (check_PROGRAMS): New variable.
+	(test_msg_SOURCES): Likewise.
+	(test_msg_CPPFLAGS): Likewise.
+	(TESTS): Likewise.
+
 2007-12-14  Guillem Jover  <guillem.jover at nokia.com>
 
 	* launcher/launcher.c (invoked_send_fake_exit): Move closing the file

Modified: projects/haf/trunk/maemo-launcher/launcher/Makefile.am
===================================================================
--- projects/haf/trunk/maemo-launcher/launcher/Makefile.am	2008-01-10 14:41:25 UTC (rev 15053)
+++ projects/haf/trunk/maemo-launcher/launcher/Makefile.am	2008-01-10 21:21:51 UTC (rev 15054)
@@ -65,6 +65,13 @@
 maemo_summoner_CPPFLAGS = -DPROG_NAME="\"maemo-summoner\""
 maemo_summoner_LDADD = -ldl
 
+check_PROGRAMS = test-msg
+
+test_msg_SOURCES = test_lib.h test_lib.c test_msg.c comm_msg.c report.c
+test_msg_CPPFLAGS = -DPROG_NAME="\"test-msg\""
+
+TESTS = $(check_PROGRAMS)
+
 man_MANS = \
 	maemo-launcher.1 \
 	maemo-invoker.1 \

Added: projects/haf/trunk/maemo-launcher/launcher/comm_msg.c
===================================================================
--- projects/haf/trunk/maemo-launcher/launcher/comm_msg.c	2008-01-10 14:41:25 UTC (rev 15053)
+++ projects/haf/trunk/maemo-launcher/launcher/comm_msg.c	2008-01-10 21:21:51 UTC (rev 15054)
@@ -0,0 +1,231 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2008 Nokia Corporation
+ *
+ * Author: Guillem Jover <guillem.jover at nokia.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ *
+ */
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "report.h"
+#include "comm_msg.h"
+
+#define WORD_SIZE	sizeof(uint32_t)
+#define WORD_MASK	(~(WORD_SIZE - 1))
+#define WORD_ALIGN(x)	(((x) + WORD_SIZE - 1) & WORD_MASK)
+
+comm_msg_t *
+comm_msg_new(uint32_t size)
+{
+  comm_msg_t *msg;
+
+  msg = malloc(sizeof(*msg));
+  if (!msg)
+  {
+    error("allocating comm message\n");
+    return NULL;
+  }
+
+  msg->used = msg->read = 0;
+  msg->size = WORD_ALIGN(size);
+  msg->buf = malloc(msg->size);
+  if (!msg->buf)
+  {
+    free(msg);
+    error("allocating comm message buffer\n");
+    return NULL;
+  }
+
+  return msg;
+}
+
+bool
+comm_msg_destroy(comm_msg_t *msg)
+{
+  if (msg->buf)
+  {
+    msg->size = msg->used = msg->read = 0;
+    free(msg->buf);
+    msg->buf = NULL;
+  }
+
+  return true;
+}
+
+bool
+comm_msg_grow(comm_msg_t *msg)
+{
+  uint32_t end_size;
+  void *p;
+
+  if (msg->used > msg->size)
+    /* Direct request for size growing. */
+    end_size = msg->used;
+  else
+    /* Automatic request for size growing. */
+    end_size = msg->size << 1;
+
+  p = realloc(msg->buf, end_size);
+  if (!p)
+    return false;
+
+  msg->buf = p;
+  memset(msg->buf + msg->used, 0, end_size - msg->used);
+
+  msg->size = end_size;
+
+  return true;
+}
+
+static bool
+comm_msg_pack_mem(comm_msg_t *msg, const void *buf, uint32_t size)
+{
+  void *cur = msg->buf + msg->used;
+  uint32_t aligned_size = WORD_ALIGN(size);
+  uint32_t pad_size = aligned_size - size;
+
+  msg->used += aligned_size + sizeof(uint32_t);
+
+  if (msg->used > msg->size)
+    comm_msg_grow(msg);
+
+  /* Pack the size. */
+  memcpy(cur, &aligned_size, sizeof(uint32_t));
+
+  /* Pack the data. */
+  cur += sizeof(uint32_t);
+  memcpy(cur, buf, size);
+
+  if (pad_size)
+  {
+    /* Add padding, if needed. */
+    cur += size;
+    memset(cur, 0, pad_size);
+  }
+
+  return true;
+}
+
+static const void *
+comm_msg_unpack_mem(comm_msg_t *msg, uint32_t *size)
+{
+  void *cur = msg->buf + msg->read;
+  int old_read = msg->read;
+  uint32_t new_size;
+
+  /* Unpack the size. */
+  memcpy(&new_size, cur, sizeof(uint32_t));
+
+  msg->read += new_size + sizeof(uint32_t);
+
+  if (msg->read > msg->used) {
+    error("trying to unpack more than available, unwinding action\n");
+    msg->read = old_read;
+    return NULL;
+  }
+
+  *size = new_size;
+
+  /* Return a pointer to the data. */
+  return cur + sizeof(uint32_t);
+}
+
+bool
+comm_msg_pack_int(comm_msg_t *msg, uint32_t i)
+{
+  comm_msg_pack_mem(msg, &i, sizeof(i));
+
+  return true;
+}
+
+bool
+comm_msg_unpack_int(comm_msg_t *msg, uint32_t *i)
+{
+  uint32_t size, *p;
+
+  p = (uint32_t *)comm_msg_unpack_mem(msg, &size);
+
+  if (size != sizeof(*i))
+    return false;
+
+  *i = *p;
+
+  return true;
+}
+
+bool
+comm_msg_pack_str(comm_msg_t *msg, const char *str)
+{
+  uint32_t size = strlen(str) + 1;
+
+  comm_msg_pack_mem(msg, str, size);
+
+  return true;
+}
+
+bool
+comm_msg_unpack_str(comm_msg_t *msg, const char **str_r)
+{
+  uint32_t size;
+  const char *str;
+
+  str = comm_msg_unpack_mem(msg, &size);
+  if (!str)
+  {
+    error("retrieving the string\n");
+    return false;
+  }
+
+  if ((size - strlen(str)) > WORD_SIZE)
+    return false;
+
+  *str_r = str;
+
+  return true;
+}
+
+bool
+comm_msg_send(int fd, comm_msg_t *msg)
+{
+  write(fd, &msg->used, sizeof(msg->size));
+  write(fd, msg->buf, msg->used);
+
+  debug("%s: %08x\n", __FUNCTION__, msg);
+
+  return true;
+}
+
+bool
+comm_msg_recv(int fd, comm_msg_t *msg)
+{
+  read(fd, &msg->used, sizeof(msg->used));
+
+  if (msg->used > msg->size)
+    comm_msg_grow(msg);
+
+  read(fd, msg->buf, msg->used);
+
+  debug("%s: %08x\n", __FUNCTION__, *msg);
+
+  return true;
+}
+


Property changes on: projects/haf/trunk/maemo-launcher/launcher/comm_msg.c
___________________________________________________________________
Name: svn:keywords
   + Id Revision

Added: projects/haf/trunk/maemo-launcher/launcher/comm_msg.h
===================================================================
--- projects/haf/trunk/maemo-launcher/launcher/comm_msg.h	2008-01-10 14:41:25 UTC (rev 15053)
+++ projects/haf/trunk/maemo-launcher/launcher/comm_msg.h	2008-01-10 21:21:51 UTC (rev 15054)
@@ -0,0 +1,50 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2008 Nokia Corporation
+ *
+ * Author: Guillem Jover <guillem.jover at nokia.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ *
+ */
+
+#ifndef COMM_MSG_H
+#define COMM_MSG_H
+
+#include <stdint.h>
+#include <stdbool.h>
+
+typedef struct comm_msg {
+  uint32_t size;
+  uint32_t used;
+  uint32_t read;
+  char *buf;
+} comm_msg_t;
+
+comm_msg_t *comm_msg_new(uint32_t size);
+bool comm_msg_destroy(comm_msg_t *msg);
+bool comm_msg_grow(comm_msg_t *msg);
+
+bool comm_msg_send(int fd, comm_msg_t *msg);
+bool comm_msg_recv(int fd, comm_msg_t *msg);
+
+bool comm_msg_pack_int(comm_msg_t *msg, uint32_t i);
+bool comm_msg_unpack_int(comm_msg_t *msg, uint32_t *i);
+
+bool comm_msg_pack_str(comm_msg_t *msg, const char *str);
+bool comm_msg_unpack_str(comm_msg_t *msg, const char **str);
+
+#endif
+


Property changes on: projects/haf/trunk/maemo-launcher/launcher/comm_msg.h
___________________________________________________________________
Name: svn:keywords
   + Id Revision

Added: projects/haf/trunk/maemo-launcher/launcher/test_lib.c
===================================================================
--- projects/haf/trunk/maemo-launcher/launcher/test_lib.c	2008-01-10 14:41:25 UTC (rev 15053)
+++ projects/haf/trunk/maemo-launcher/launcher/test_lib.c	2008-01-10 21:21:51 UTC (rev 15054)
@@ -0,0 +1,66 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2008 Nokia Corporation
+ *
+ * Authors: Guillem Jover <guillem.jover at nokia.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ *
+ */
+
+#include <string.h>
+
+#include "report.h"
+#include "test_lib.h"
+
+static int tests_ok, tests_total;
+
+void
+tests_init(int total)
+{
+	tests_ok = 0;
+	tests_total = total;
+}
+
+bool
+tests_summary(void)
+{
+	bool pass = tests_total == tests_ok;
+	const char *outcome = pass ? "ok" : "fail";
+
+	info("Tests pass (%d/%d): %s\n", tests_ok, tests_total, outcome);
+
+	return pass;
+}
+
+void
+test_cmp_int(uint32_t a, uint32_t b)
+{
+	if (a == b)
+		++tests_ok;
+	else
+		error("got %0#8x expected %0#8x\n", b, a);
+}
+
+void
+test_cmp_str(const char *a, const char *b)
+{
+	if (strcmp(a, b) == 0)
+		++tests_ok;
+	else
+		error("got %s expected %s\n", b, a);
+}
+


Property changes on: projects/haf/trunk/maemo-launcher/launcher/test_lib.c
___________________________________________________________________
Name: svn:keywords
   + Id Revision

Added: projects/haf/trunk/maemo-launcher/launcher/test_lib.h
===================================================================
--- projects/haf/trunk/maemo-launcher/launcher/test_lib.h	2008-01-10 14:41:25 UTC (rev 15053)
+++ projects/haf/trunk/maemo-launcher/launcher/test_lib.h	2008-01-10 21:21:51 UTC (rev 15054)
@@ -0,0 +1,36 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2008 Nokia Corporation
+ *
+ * Authors: Guillem Jover <guillem.jover at nokia.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ *
+ */
+
+#ifndef TEST_LIB_H
+#define TEST_LIB_H
+
+#include <stdbool.h>
+#include <stdint.h>
+
+void tests_init(int total);
+bool tests_summary(void);
+void test_cmp_int(uint32_t a, uint32_t b);
+void test_cmp_str(const char *a, const char *b);
+
+#endif
+


Property changes on: projects/haf/trunk/maemo-launcher/launcher/test_lib.h
___________________________________________________________________
Name: svn:keywords
   + Id Revision

Added: projects/haf/trunk/maemo-launcher/launcher/test_msg.c
===================================================================
--- projects/haf/trunk/maemo-launcher/launcher/test_msg.c	2008-01-10 14:41:25 UTC (rev 15053)
+++ projects/haf/trunk/maemo-launcher/launcher/test_msg.c	2008-01-10 21:21:51 UTC (rev 15054)
@@ -0,0 +1,98 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2008 Nokia Corporation
+ *
+ * Authors: Guillem Jover <guillem.jover at nokia.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ *
+ */
+
+#include <unistd.h>
+#include <sys/types.h>
+#include <fcntl.h>
+
+#include "comm_msg.h"
+#include "report.h"
+#include "test_lib.h"
+
+const char *test_file = "test_msg.out";
+
+int
+main()
+{
+	int fd;
+	uint32_t w1 = 0xcafef00d, w2 = 0xdeadbeef, w3 = 0xd00d, w4 = 0xbabe;
+	const char *s1 = "0123456789abcdef, end here.";
+	const char *s2 = "some other more reasonable string";
+	uint32_t w;
+	const char *s;
+	comm_msg_t *msg;
+
+	/* Serializing. */
+
+	msg = comm_msg_new(20);
+
+	comm_msg_pack_int(msg, w1);
+	comm_msg_pack_int(msg, w2);
+	comm_msg_pack_str(msg, s1);
+	comm_msg_pack_str(msg, s2);
+	comm_msg_pack_int(msg, w3);
+	comm_msg_pack_int(msg, w4);
+
+	fd = open(test_file, O_WRONLY | O_CREAT, 0644);
+	if (fd < 0)
+		die(1, "cannot create file");
+
+	comm_msg_send(fd, msg);
+	comm_msg_destroy(msg);
+
+	close(fd);
+
+	/* Deserializing. */
+
+	msg = comm_msg_new(5);
+
+	fd = open(test_file, O_RDONLY, 0644);
+	if (fd < 0)
+		die(2, "cannot open file");
+
+	comm_msg_recv(fd, msg);
+
+	tests_init(6);
+
+	if (comm_msg_unpack_int(msg, &w))
+		test_cmp_int(w1, w);
+	if (comm_msg_unpack_int(msg, &w))
+		test_cmp_int(w2, w);
+	if (comm_msg_unpack_str(msg, &s))
+		test_cmp_str(s1, s);
+	if (comm_msg_unpack_str(msg, &s))
+		test_cmp_str(s2, s);
+	if (comm_msg_unpack_int(msg, &w))
+		test_cmp_int(w3, w);
+	if (comm_msg_unpack_int(msg, &w))
+		test_cmp_int(w4, w);
+
+	comm_msg_destroy(msg);
+
+	close(fd);
+
+	unlink(test_file);
+
+	return tests_summary() ? 0 : 1;
+}
+


Property changes on: projects/haf/trunk/maemo-launcher/launcher/test_msg.c
___________________________________________________________________
Name: svn:keywords
   + Id Revision


More information about the maemo-commits mailing list