compression_wrapper: Support for filling cr_ContentStat object.
authorTomas Mlcoch <tmlcoch@redhat.com>
Wed, 5 Jun 2013 14:06:07 +0000 (16:06 +0200)
committerTomas Mlcoch <tmlcoch@redhat.com>
Wed, 5 Jun 2013 14:41:00 +0000 (16:41 +0200)
src/checksum.c
src/compression_wrapper.c
src/compression_wrapper.h
tests/run_gtester.sh.in
tests/test_compression_wrapper.c

index ab51234..2431230 100644 (file)
@@ -235,9 +235,9 @@ cr_checksum_update(cr_ChecksumCtx *ctx,
     if (len == 0)
         return CRE_OK;
 
-    if (EVP_DigestUpdate(ctx->ctx, buf, len)) {
+    if (!EVP_DigestUpdate(ctx->ctx, buf, len)) {
         g_set_error(err, CR_CHECKSUM_ERROR, CRE_OPENSSL,
-                    "EVP_DigestInit_ex() failed");
+                    "EVP_DigestUpdate_ex() failed");
         return CRE_OPENSSL;
     }
 
index b5962f4..3f6c567 100644 (file)
@@ -285,11 +285,11 @@ cr_gz_strerror(gzFile f)
 
 
 CR_FILE *
-cr_open_with_stats(const char *filename,
-                   cr_OpenMode mode,
-                   cr_CompressionType comtype,
-                   cr_ContentStat *stat,
-                   GError **err)
+cr_open_with_stat(const char *filename,
+                  cr_OpenMode mode,
+                  cr_CompressionType comtype,
+                  cr_ContentStat *stat,
+                  GError **err)
 {
     CR_FILE *file = NULL;
     cr_CompressionType type = comtype;
@@ -504,6 +504,8 @@ cr_open_with_stats(const char *filename,
         return NULL;
     }
 
+    file->stat = stat;
+
     assert(!err || (!file && *err != NULL) || (file && *err == NULL));
 
     return file;
@@ -888,6 +890,22 @@ cr_write(CR_FILE *cr_file, const void *buffer, unsigned int len, GError **err)
         return ret;
     }
 
+    if (cr_file->stat) {
+        // Do open content stat
+
+        cr_ContentStat *stat = cr_file->stat;
+        GError *tmp_err = NULL;
+
+        stat->size += len;
+        if (stat->checksum)
+            cr_checksum_update(stat->checksum, buffer, len, &tmp_err);
+
+        if (tmp_err) {
+            g_propagate_error(err, tmp_err);
+            return CR_CW_ERR;
+        }
+    }
+
     switch (cr_file->type) {
 
         case (CR_CW_NO_COMPRESSION): // ---------------------------------------
@@ -1035,30 +1053,12 @@ cr_puts(CR_FILE *cr_file, const char *str, GError **err)
     switch (cr_file->type) {
 
         case (CR_CW_NO_COMPRESSION): // ---------------------------------------
-            if (fputs(str, (FILE *) cr_file->FILE) != EOF) {
-                ret = 0;
-            } else {
-                g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_IO,
-                            "fputs(): %s", strerror(errno));
-            }
-            break;
-
         case (CR_CW_GZ_COMPRESSION): // ---------------------------------------
-            if (gzputs((gzFile) cr_file->FILE, str) != -1) {
-                ret = 0;
-            } else {
-                g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_GZ,
-                    "gzputs(): %s", cr_gz_strerror((gzFile) cr_file->FILE));
-            }
-            break;
-
         case (CR_CW_BZ2_COMPRESSION): // --------------------------------------
         case (CR_CW_XZ_COMPRESSION): // ---------------------------------------
             len = strlen(str);
             ret = cr_write(cr_file, str, len, err);
-            if (ret == (int) len)
-                ret = 0;
-            else
+            if (ret != (int) len)
                 ret = CR_CW_ERR;
             break;
 
@@ -1114,27 +1114,12 @@ cr_printf(GError **err, CR_FILE *cr_file, const char *format, ...)
     switch (cr_file->type) {
 
         case (CR_CW_NO_COMPRESSION): // ---------------------------------------
-            if ((ret = fwrite(buf, 1, ret, cr_file->FILE)) < 0) {
-                ret = CR_CW_ERR;
-                g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_IO,
-                            "fwrite(): %s", strerror(errno));
-            }
-            break;
-
         case (CR_CW_GZ_COMPRESSION): // ---------------------------------------
-            if (gzputs((gzFile) cr_file->FILE, buf) == -1) {
-                ret = CR_CW_ERR;
-                g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_GZ,
-                    "gzputs(): %s", cr_gz_strerror((gzFile) cr_file->FILE));
-            }
-            break;
-
         case (CR_CW_BZ2_COMPRESSION): // --------------------------------------
         case (CR_CW_XZ_COMPRESSION): // ---------------------------------------
             tmp_ret = cr_write(cr_file, buf, ret, err);
-            if (tmp_ret != (int) ret) {
+            if (tmp_ret != (int) ret)
                 ret = CR_CW_ERR;
-            }
             break;
 
         default: // -----------------------------------------------------------
index a574dd5..cbef901 100644 (file)
@@ -106,7 +106,7 @@ cr_CompressionType cr_detect_compression(const char* filename, GError **err);
  * @return              pointer to a CR_FILE or NULL
  */
 #define cr_open(FILENAME, MODE, COMPRESSION, ERR) \
-                    cr_open_with_stats(FILENAME, MODE, COMPRESSION, NULL, ERR)
+                    cr_open_with_stat(FILENAME, MODE, COMPRESSION, NULL, ERR)
 
 /** Open/Create the specified file. For writting is possible pass
  * a cr_ContentStat object and after cr_close() get stats of
@@ -118,7 +118,7 @@ cr_CompressionType cr_detect_compression(const char* filename, GError **err);
  * @param err           GError **
  * @return              pointer to a CR_FILE or NULL
  */
-CR_FILE *cr_open_with_stats(const char *filename,
+CR_FILE *cr_open_with_stat(const char *filename,
                             cr_OpenMode mode,
                             cr_CompressionType comtype,
                             cr_ContentStat *stat,
index 8394d04..d2ec0d4 100755 (executable)
@@ -4,7 +4,7 @@ BINDIR="${CMAKE_BINARY_DIR}/tests/"
 RET=0
 
 function runtest {
-    gtester --keep-going "$1"
+    gtester --verbose --keep-going "$1"
     if [ $? -ne 0 ]; then  RET=$(($RET+1)) ; fi
 }
 
index cdc6dd6..58bf445 100644 (file)
@@ -325,7 +325,7 @@ test_helper_cw_output(int type,
 
         case OUTPUT_TYPE_PUTS:
             ret = cr_puts(file, content, &tmp_err);
-            g_assert_cmpint(ret, ==, CRE_OK);
+            g_assert_cmpint(ret, ==, len);
             g_assert(!tmp_err);
             break;
 
@@ -555,6 +555,190 @@ test_cr_error_handling(void)
 }
 
 
+static void
+test_contentstating_singlewrite(Outputtest *outputtest, gconstpointer test_data)
+{
+    CR_FILE *f;
+    int ret;
+    cr_ContentStat *stat;
+    char *checksum;
+    GError *tmp_err = NULL;
+
+    CR_UNUSED(test_data);
+
+    const char *content = "sdlkjowykjnhsadyhfsoaf\nasoiuyseahlndsf\n";
+    const int content_len = 39;
+    const char *content_sha256 = "c9d112f052ab86270bfb484817a513d6ce188133ddc0"
+                                 "7c0fc1ac32018b6da6c7";
+
+    // No compression
+
+    stat = cr_contentstat_new(CR_CHECKSUM_SHA256, &tmp_err);
+    g_assert(stat);
+    g_assert(!tmp_err);
+
+    f = cr_open_with_stat(outputtest->tmp_filename,
+                          CR_CW_MODE_WRITE,
+                          CR_CW_NO_COMPRESSION,
+                          stat,
+                          &tmp_err);
+    g_assert(f);
+    g_assert(!tmp_err);
+
+    ret = cr_write(f, content, content_len, &tmp_err);
+    g_assert_cmpint(ret, ==, content_len);
+    g_assert(!tmp_err);
+
+    cr_close(f, &tmp_err);
+    g_assert(!tmp_err);
+
+    g_assert_cmpint(stat->size, ==, content_len);
+    checksum = cr_contentstat_free(stat, &tmp_err);
+    g_assert(checksum);
+    g_assert(!tmp_err);
+    g_assert_cmpstr(checksum, ==, content_sha256);
+    g_free(checksum);
+    checksum = NULL;
+
+    // Gz compression
+
+    stat = cr_contentstat_new(CR_CHECKSUM_SHA256, &tmp_err);
+    g_assert(stat);
+    g_assert(!tmp_err);
+
+    f = cr_open_with_stat(outputtest->tmp_filename,
+                          CR_CW_MODE_WRITE,
+                          CR_CW_GZ_COMPRESSION,
+                          stat,
+                          &tmp_err);
+    g_assert(f);
+    g_assert(!tmp_err);
+
+    ret = cr_write(f, content, content_len, &tmp_err);
+    g_assert_cmpint(ret, ==, content_len);
+    g_assert(!tmp_err);
+
+    cr_close(f, &tmp_err);
+    g_assert(!tmp_err);
+
+    g_assert_cmpint(stat->size, ==, content_len);
+    checksum = cr_contentstat_free(stat, &tmp_err);
+    g_assert(checksum);
+    g_assert(!tmp_err);
+    g_assert_cmpstr(checksum, ==, content_sha256);
+    g_free(checksum);
+    checksum = NULL;
+
+    // Bz2 compression
+
+    stat = cr_contentstat_new(CR_CHECKSUM_SHA256, &tmp_err);
+    g_assert(stat);
+    g_assert(!tmp_err);
+
+    f = cr_open_with_stat(outputtest->tmp_filename,
+                          CR_CW_MODE_WRITE,
+                          CR_CW_BZ2_COMPRESSION,
+                          stat,
+                          &tmp_err);
+    g_assert(f);
+    g_assert(!tmp_err);
+
+    ret = cr_write(f, content, content_len, &tmp_err);
+    g_assert_cmpint(ret, ==, content_len);
+    g_assert(!tmp_err);
+
+    cr_close(f, &tmp_err);
+    g_assert(!tmp_err);
+
+    g_assert_cmpint(stat->size, ==, content_len);
+    checksum = cr_contentstat_free(stat, &tmp_err);
+    g_assert(checksum);
+    g_assert(!tmp_err);
+    g_assert_cmpstr(checksum, ==, content_sha256);
+    g_free(checksum);
+    checksum = NULL;
+
+    // Xz compression
+
+    stat = cr_contentstat_new(CR_CHECKSUM_SHA256, &tmp_err);
+    g_assert(stat);
+    g_assert(!tmp_err);
+
+    f = cr_open_with_stat(outputtest->tmp_filename,
+                          CR_CW_MODE_WRITE,
+                          CR_CW_XZ_COMPRESSION,
+                          stat,
+                          &tmp_err);
+    g_assert(f);
+    g_assert(!tmp_err);
+
+    ret = cr_write(f, content, content_len, &tmp_err);
+    g_assert_cmpint(ret, ==, content_len);
+    g_assert(!tmp_err);
+
+    cr_close(f, &tmp_err);
+    g_assert(!tmp_err);
+
+    g_assert_cmpint(stat->size, ==, content_len);
+    checksum = cr_contentstat_free(stat, &tmp_err);
+    g_assert(checksum);
+    g_assert(!tmp_err);
+    g_assert_cmpstr(checksum, ==, content_sha256);
+    g_free(checksum);
+    checksum = NULL;
+}
+
+static void
+test_contentstating_multiwrite(Outputtest *outputtest, gconstpointer test_data)
+{
+    CR_FILE *f;
+    int ret;
+    cr_ContentStat *stat;
+    char *checksum;
+    GError *tmp_err = NULL;
+
+    CR_UNUSED(test_data);
+
+    const char *content = "sdlkjowykjnhsadyhfsoaf\nasoiuyseahlndsf\n";
+    const int content_len = 39;
+    const char *content_sha256 = "c9d112f052ab86270bfb484817a513d6ce188133ddc0"
+                                 "7c0fc1ac32018b6da6c7";
+
+    // Gz compression
+
+    stat = cr_contentstat_new(CR_CHECKSUM_SHA256, &tmp_err);
+    g_assert(stat);
+    g_assert(!tmp_err);
+
+    f = cr_open_with_stat(outputtest->tmp_filename,
+                          CR_CW_MODE_WRITE,
+                          CR_CW_GZ_COMPRESSION,
+                          stat,
+                          &tmp_err);
+    g_assert(f);
+    g_assert(!tmp_err);
+
+    ret = cr_write(f, content, 10, &tmp_err);
+    g_assert_cmpint(ret, ==, 10);
+    g_assert(!tmp_err);
+
+    ret = cr_write(f, content+10, 29, &tmp_err);
+    g_assert_cmpint(ret, ==, 29);
+    g_assert(!tmp_err);
+
+    cr_close(f, &tmp_err);
+    g_assert(!tmp_err);
+
+    g_assert_cmpint(stat->size, ==, content_len);
+    checksum = cr_contentstat_free(stat, &tmp_err);
+    g_assert(checksum);
+    g_assert(!tmp_err);
+    g_assert_cmpstr(checksum, ==, content_sha256);
+    g_free(checksum);
+    checksum = NULL;
+}
+
+
 int
 main(int argc, char *argv[])
 {
@@ -574,6 +758,12 @@ main(int argc, char *argv[])
             outputtest_setup, outputtest_cw_output, outputtest_teardown);
     g_test_add_func("/compression_wrapper/test_cr_error_handling",
             test_cr_error_handling);
+    g_test_add("/compression_wrapper/test_contentstating_singlewrite",
+            Outputtest, NULL, outputtest_setup,
+            test_contentstating_singlewrite, outputtest_teardown);
+    g_test_add("/compression_wrapper/test_contentstating_multiwrite",
+            Outputtest, NULL, outputtest_setup,
+            test_contentstating_multiwrite, outputtest_teardown);
 
     return g_test_run();
 }