Accept short data
authorMatthias Clasen <matthiasc@src.gnome.org>
Fri, 24 Oct 2008 04:10:50 +0000 (04:10 +0000)
committerMatthias Clasen <matthiasc@src.gnome.org>
Fri, 24 Oct 2008 04:10:50 +0000 (04:10 +0000)
svn path=/trunk/; revision=7625

ChangeLog
glib/gchecksum.c
tests/checksum-test.c

index 4527b8069f746a6fd2a1d482bc1914ece2b58b9d..b339e8e38c59ab76de732680a94d07384c3a4188 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2008-10-23 Matthias Clasen <mclasen@redhat.com>
+
+       * glib/gchecksum.c (g_compute_checksum_for_data): Accept 
+       lengths < 2. Patch by Tommi Komulainen
+
+       * tests/checksum-test.c: Add a test for this
+
 2008-10-23 Matthias Clasen <mclasen@redhat.com>
 
        Bug 556921 – gpoll.h breaks hal compilation
index 41cc1f845453cb904c550dab24d9cb5f703e342f..529f79bc4ca26882fe1483980fa7b48a841684fa 100644 (file)
@@ -1389,7 +1389,6 @@ g_compute_checksum_for_data (GChecksumType  checksum_type,
 
   g_return_val_if_fail (IS_VALID_TYPE (checksum_type), NULL);
   g_return_val_if_fail (data != NULL, NULL);
-  g_return_val_if_fail (length > 1, NULL);
 
   checksum = g_checksum_new (checksum_type);
   if (!checksum)
index b5374b6a2110acaee6b5ae66231df78ba384978c..dc1a61e48f3a4800963c0ab2540f4822f96b00b4 100644 (file)
@@ -617,11 +617,45 @@ test_checksum (GChecksumType  checksum_type,
     }
 }
 
+static void
+test_checksum_string (GChecksumType   checksum_type,
+                      const gchar    *type,
+                      const gchar   **sums)
+{
+  int length;
+
+  for (length = 0; length <= FIXED_LEN; length++)
+    {
+      const char *expected = sums[length];
+      char *checksum;
+
+      checksum = g_compute_checksum_for_string (checksum_type,
+                                                FIXED_STR,
+                                                length);
+      if (strcmp (checksum, expected) != 0)
+        {
+          g_print ("Invalid %s checksum for `%.*s' (length %d):\n"
+                   "%s (expecting: %s)\n",
+                   type,
+                   length, FIXED_STR, length,
+                   checksum,
+                   expected);
+          exit (1);
+        }
+
+      g_free (checksum);
+    }
+}
+
 #define test(type, length) test_checksum (G_CHECKSUM_##type,   \
                                           #type, \
                                           type##_sums[length], \
                                           length)
 
+#define test_string(type) test_checksum_string (G_CHECKSUM_##type,     \
+                                                #type, \
+                                                type##_sums)
+
 int
 main (int argc, char *argv[])
 {
@@ -634,5 +668,9 @@ main (int argc, char *argv[])
       test (SHA256, length);
     }
 
+  test_string (MD5);
+  test_string (SHA1);
+  test_string (SHA256);
+
   return EXIT_SUCCESS;
 }