r2027 - in trunk/src/target/OM-2007/applications/openmoko-rssreader: . src tests

zecke at sita.openmoko.org zecke at sita.openmoko.org
Fri May 18 23:32:26 CEST 2007


Author: zecke
Date: 2007-05-18 23:32:25 +0200 (Fri, 18 May 2007)
New Revision: 2027

Added:
   trunk/src/target/OM-2007/applications/openmoko-rssreader/tests/cache_test.c
Modified:
   trunk/src/target/OM-2007/applications/openmoko-rssreader/ChangeLog
   trunk/src/target/OM-2007/applications/openmoko-rssreader/src/moko_cache.c
   trunk/src/target/OM-2007/applications/openmoko-rssreader/src/moko_cache.h
   trunk/src/target/OM-2007/applications/openmoko-rssreader/tests/
   trunk/src/target/OM-2007/applications/openmoko-rssreader/tests/Makefile.am
Log:
2007-05-18  Holger Freyther  <zecke at selfish.org>

        Test the cache implementation

        * src/moko_cache.c:
        (moko_cache_create_dirs): Fix the g_file_test logic
        (object_name_to_file_name): replace the tile as well (~)
        (moko_cache_write_object): use g_set_file_contents to gain atomic
        writes
        * src/moko_cache.h: Update write prototype to gain a GError**
        * tests/Makefile.am:
        * tests/cache_test.c: Added.



Modified: trunk/src/target/OM-2007/applications/openmoko-rssreader/ChangeLog
===================================================================
--- trunk/src/target/OM-2007/applications/openmoko-rssreader/ChangeLog	2007-05-18 20:52:47 UTC (rev 2026)
+++ trunk/src/target/OM-2007/applications/openmoko-rssreader/ChangeLog	2007-05-18 21:32:25 UTC (rev 2027)
@@ -1,6 +1,19 @@
+2007-05-18  Holger Freyther  <zecke at selfish.org>
+
+        Test the cache implementation
+
+        * src/moko_cache.c:
+        (moko_cache_create_dirs): Fix the g_file_test logic
+        (object_name_to_file_name): replace the tile as well (~)
+        (moko_cache_write_object): use g_set_file_contents to gain atomic
+        writes
+        * src/moko_cache.h: Update write prototype to gain a GError**
+        * tests/Makefile.am:
+        * tests/cache_test.c: Added.
+
 2007-05-03  Holger Freyther  <zecke at selfish.org>
 
-        Start implementing the test (untested)
+        Start implementing the cache (untested)
 
         * src/moko_cache.c:
         (moko_cache_create_dirs): Make sure to create the dir. Somehow there

Modified: trunk/src/target/OM-2007/applications/openmoko-rssreader/src/moko_cache.c
===================================================================
--- trunk/src/target/OM-2007/applications/openmoko-rssreader/src/moko_cache.c	2007-05-18 20:52:47 UTC (rev 2026)
+++ trunk/src/target/OM-2007/applications/openmoko-rssreader/src/moko_cache.c	2007-05-18 21:32:25 UTC (rev 2027)
@@ -55,13 +55,18 @@
 moko_cache_create_dirs (gchar *cache_name)
 {
     gchar *path = g_build_path (G_DIR_SEPARATOR_S, g_get_home_dir (), CACHE_NAME, NULL);
-    if (g_file_test (path, G_FILE_TEST_EXISTS))
+    if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
+        g_debug ("Trying to create dir '%s'\n", path);
         g_mkdir (path, 0700);
+    }
+
     g_free (path);
 
     path = g_build_path (G_DIR_SEPARATOR_S, g_get_home_dir (), CACHE_NAME, cache_name, NULL);
-    if (g_file_test (path, G_FILE_TEST_EXISTS))
+    if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
+        g_debug ("Trying to create app cache dir '%s'\n", path);
         g_mkdir (path, 0700);
+    }
     g_free (path);
     
 }
@@ -85,7 +90,7 @@
     gchar *result = g_strdup (file_name);
     const int l = strlen(result);
     for (int i = 0; i < l; ++i)
-        if ( result[i] == '/' || result[i] == ':' || result[i] == '.' )
+        if ( result[i] == '/' || result[i] == ':' || result[i] == '.' || result[i] == '~' )
             result[i] = '_';
 
     return result;
@@ -141,7 +146,7 @@
 }
 
 gint
-moko_cache_write_object (MokoCache *self, gchar *object_name, gchar *content, gsize size)
+moko_cache_write_object (MokoCache *self, gchar *object_name, gchar *content, gsize size, GError **g_error)
 {
     int error = MOKO_CACHE_WRITE_SUCCESS;
     size = size == -1 ? strlen(content) : size;
@@ -150,18 +155,10 @@
     gchar *file_name = object_name_to_file_name (object_name);
     gchar *path = moko_cache_create_path (self->cache_name, file_name);
 
-    int fd = g_open (path, O_WRONLY|O_TRUNC, 0700);
-    if ( fd < 0 ) {
+    gboolean result = g_file_set_contents (path, content, size, g_error);
+    if (!result )
         error = MOKO_CACHE_WRITE_UNKNOWN_ERROR;
-        goto error_path;
-    }
 
-    if ( write (fd, content, size) < 0 ) {
-        error = MOKO_CACHE_WRITE_UNKNOWN_ERROR;
-        goto error_path;
-    }
-
-error_path:
     g_free (path);
     g_free (file_name);
     return error;

Modified: trunk/src/target/OM-2007/applications/openmoko-rssreader/src/moko_cache.h
===================================================================
--- trunk/src/target/OM-2007/applications/openmoko-rssreader/src/moko_cache.h	2007-05-18 20:52:47 UTC (rev 2026)
+++ trunk/src/target/OM-2007/applications/openmoko-rssreader/src/moko_cache.h	2007-05-18 21:32:25 UTC (rev 2027)
@@ -71,7 +71,7 @@
 gint          moko_cache_get_allowed_size (MokoCache *self);
 gint          moko_cache_get_utilized_size(MokoCache *self);
 
-gint          moko_cache_write_object (MokoCache *self, gchar *object_name, gchar *content, gsize size);
+gint          moko_cache_write_object (MokoCache *self, gchar *object_name, gchar *content, gsize size, GError** error);
 gchar*        moko_cache_read_object  (MokoCache *self, gchar *object_name, gsize *size);
 
 /**
@@ -95,10 +95,12 @@
  */
 
 /**
- * \fn moko_cache_write_object (MokoCache *self, gchar *object_name, gchar *content, guint size);
+ * \fn moko_cache_write_object (MokoCache *self, gchar *object_name, gchar *content, guint size, GError** error);
  * @param object_name The name of the object. E.g. http://www.heise.de/atom.xml
  * @param content     The actual content to be written to the cache
  * @param size        The size of the content. If it is -1 strlen will be used to determine the size
+ * @param error       The error containing a nice string.
+ * @return one of the MokoCacheWriteResult
  */
 
 /**


Property changes on: trunk/src/target/OM-2007/applications/openmoko-rssreader/tests
___________________________________________________________________
Name: svn:ignore
   - Makefile
Makefile.in
.deps
.libs
datetest
*.swp

   + Makefile
Makefile.in
.deps
.libs
datetest
*.swp
*.swo
cachetest


Modified: trunk/src/target/OM-2007/applications/openmoko-rssreader/tests/Makefile.am
===================================================================
--- trunk/src/target/OM-2007/applications/openmoko-rssreader/tests/Makefile.am	2007-05-18 20:52:47 UTC (rev 2026)
+++ trunk/src/target/OM-2007/applications/openmoko-rssreader/tests/Makefile.am	2007-05-18 21:32:25 UTC (rev 2027)
@@ -2,11 +2,16 @@
 INCLUDES  = -I$(top_srcdir)/src
 
 if ENABLE_TESTING
-TESTS = datetest
-check_PROGRAMS = datetest
+TESTS = datetest cachetest
+check_PROGRAMS = datetest cachetest
 endif
 
 datatest_INCLUDES = @CHECK_CFLAGS@
 datetest_SOURCES  = date_test.c ../src/rfcdate.c
 datetest_LIBS     = @CHECK_LIBS@
 datetest_LDFLAGS  = -lcheck @GCOV_LDFLAGS@ @OPENMOKO_LIBS@
+
+cachetest_INCLUDES = @CHECK_CFLAGS@
+cachetest_SOURCES  = cache_test.c ../src/moko_cache.c
+cachetest_LIBS     = @CHECK_LIBS@
+cachetest_LDFLAGS  = -lcheck @GCOV_LDFLAGS@ @OPENMOKO_LIBS@

Added: trunk/src/target/OM-2007/applications/openmoko-rssreader/tests/cache_test.c
===================================================================
--- trunk/src/target/OM-2007/applications/openmoko-rssreader/tests/cache_test.c	2007-05-18 20:52:47 UTC (rev 2026)
+++ trunk/src/target/OM-2007/applications/openmoko-rssreader/tests/cache_test.c	2007-05-18 21:32:25 UTC (rev 2027)
@@ -0,0 +1,104 @@
+#include <check.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <glib.h>
+#include <string.h>
+#include "rfcdate.h"
+
+#include "moko_cache.h"
+
+START_TEST (test_create_cache)
+{
+    GObject *cache = moko_cache_new ("moko-cache-test");
+    g_object_unref (cache);
+}
+END_TEST
+
+/*
+ * test error handling
+ */
+START_TEST (test_read_empty_cache)
+{
+    GObject *cache = moko_cache_new ("moko-cache-test");
+    fail_unless (cache != NULL, "Creating failed?");
+
+    gsize size;
+    gchar *cache_data = moko_cache_read_object (MOKO_CACHE(cache), "http://www.openembedded.org/~zecke/does-not-exist", &size);
+    fail_unless (cache_data == NULL, "Failed to read it");
+    fail_unless (size == -1, "Size is wrong");
+    g_object_unref (cache);
+}
+END_TEST
+
+/*
+ * test writing and reading a file
+ */
+START_TEST (test_read_write_cache)
+{
+    GObject *cache = moko_cache_new ("moko-cache-test");
+
+    gchar *content = "Hey this is a cache test";
+    gint result = moko_cache_write_object (MOKO_CACHE(cache), "http://openembedded.org/~zecke/foo.withtext", content, strlen(content), NULL);
+    g_print ("Result was %d\n", result);
+    fail_unless (result == MOKO_CACHE_WRITE_SUCCESS, "Writing the cache failed");
+
+    /* now try to read it */
+    gsize size;
+    gchar *content_result = moko_cache_read_object (MOKO_CACHE(cache), "http://openembedded.org/~zecke/foo.withtext", &size);
+    g_print ("String: %p size: %d\n", content_result, (int)size);
+    fail_unless (content_result != NULL, "A valid string");
+    fail_unless (size == 24, "Right size");
+    fail_unless (strcmp(content_result,content) == 0, "Right text?");
+    g_free(content_result);
+    g_object_unref (cache);
+}
+END_TEST
+
+START_TEST (test_read_write_empty_cache)
+{
+    GObject *cache = moko_cache_new ("moko-cache-test");
+
+    gchar *content = "";
+    gint result = moko_cache_write_object (MOKO_CACHE(cache), "http://openembedded.org/~zecke/foo.empty", content, strlen(content), NULL);
+    g_print ("Result was %d\n", result);
+    fail_unless (result == MOKO_CACHE_WRITE_SUCCESS, "Writing the cache failed");
+
+    /* now try to read it */
+    gsize size;
+    content = moko_cache_read_object (MOKO_CACHE(cache), "http://openembedded.org/~zecke/foo.empty", &size);
+    g_print ("String: %p size: %d\n", content, (int)size);
+    fail_unless (content != NULL, "A valid string");
+    fail_unless (size == 0, "Right size");
+    g_free(content);
+    g_object_unref (cache);
+}
+END_TEST
+
+Suite*
+cache_suite (void)
+{
+    Suite *s = suite_create( "Cache" );
+    TCase *tc_core = tcase_create ("Core");
+    tcase_add_test (tc_core, test_create_cache);
+    tcase_add_test (tc_core, test_read_empty_cache);
+    tcase_add_test (tc_core, test_read_write_cache);
+    tcase_add_test (tc_core, test_read_write_empty_cache);
+    suite_add_tcase (s, tc_core);
+
+    return s;
+}
+
+int
+main (void)
+{
+    g_type_init ();
+    Suite *s = cache_suite ();
+    SRunner *sr = srunner_create (s);
+    srunner_run_all (sr, CK_NORMAL);
+    int number_failed = srunner_ntests_failed (sr);
+    srunner_free (sr);
+
+    return  (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
+}


Property changes on: trunk/src/target/OM-2007/applications/openmoko-rssreader/tests/cache_test.c
___________________________________________________________________
Name: svn:eol-style
   + native





More information about the commitlog mailing list