Add unittest for compression_wrapper
authorTomas Mlcoch <tmlcoch@redhat.com>
Tue, 17 Apr 2012 11:49:34 +0000 (13:49 +0200)
committerTomas Mlcoch <tmlcoch@redhat.com>
Tue, 17 Apr 2012 11:49:34 +0000 (13:49 +0200)
17 files changed:
CMakeLists.txt
src/CMakeLists.txt
src/compression_wrapper.c
src/compression_wrapper.h
tests/test_data/compressed_files/00_plain.foo0 [new file with mode: 0644]
tests/test_data/compressed_files/00_plain.foo1 [new file with mode: 0644]
tests/test_data/compressed_files/00_plain.foo2 [new file with mode: 0644]
tests/test_data/compressed_files/00_plain.txt [new file with mode: 0644]
tests/test_data/compressed_files/00_plain.txt.bz2 [new file with mode: 0644]
tests/test_data/compressed_files/00_plain.txt.gz [new file with mode: 0644]
tests/test_data/compressed_files/01_plain.foo0 [new file with mode: 0644]
tests/test_data/compressed_files/01_plain.foo1 [new file with mode: 0644]
tests/test_data/compressed_files/01_plain.foo2 [new file with mode: 0644]
tests/test_data/compressed_files/01_plain.txt [new file with mode: 0644]
tests/test_data/compressed_files/01_plain.txt.bz2 [new file with mode: 0644]
tests/test_data/compressed_files/01_plain.txt.gz [new file with mode: 0644]
tests/testcompression_wrapper.c [new file with mode: 0644]

index 55f122e..5211d0f 100644 (file)
@@ -136,3 +136,4 @@ INSTALL_FILES(/usr/share/doc/createrepo_c-${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK
 
 ADD_SUBDIRECTORY (src)
 ADD_SUBDIRECTORY (doc)
+ADD_SUBDIRECTORY (tests)
index 359998e..a24a0f1 100644 (file)
@@ -3,8 +3,7 @@ SET (createrepo_c_SRCS
     repomd.c xml_dump.c xml_dump_filelists.c xml_dump_other.c xml_dump_primary.c
     locate_metadata.c)
 
-#ADD_LIBRARY(libcreaterepo_c SHARED ${createrepo_c_SRCS})
-ADD_LIBRARY(libcreaterepo_c ${createrepo_c_SRCS})
+ADD_LIBRARY(libcreaterepo_c SHARED ${createrepo_c_SRCS})
 TARGET_LINK_LIBRARIES(libcreaterepo_c ${ZLIB_LIBRARY})
 TARGET_LINK_LIBRARIES(libcreaterepo_c ${RPMDB_LIBRARY})
 TARGET_LINK_LIBRARIES(libcreaterepo_c ${BZIP2_LIBRARIES})
index d5c42cf..ec1bc6c 100644 (file)
@@ -46,10 +46,10 @@ CompressionType detect_compression(const char *filename)
     CompressionType type = UNKNOWN_COMPRESSION;
 
     if (!g_file_test(filename, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
+        g_debug(MODULE"%s: File %s doesn't exists or it's not a regular file", __func__, filename);
         return type;
     }
 
-
     // Try determine compression type via filename suffix
 
     if (g_str_has_suffix(filename, ".gz") ||
@@ -80,7 +80,7 @@ CompressionType detect_compression(const char *filename)
     const char *mime_type = magic_file(myt, filename);
 
     if (mime_type) {
-        g_debug(MODULE"%s: Detected mime type: %s", __func__, mime_type);
+        g_debug(MODULE"%s: Detected mime type: %s (%s)", __func__, mime_type, filename);
 
 //        if (g_str_has_suffix(mime_type, "gzip") ||
 //            g_str_has_suffix(mime_type, "gunzip"))
@@ -107,14 +107,16 @@ CompressionType detect_compression(const char *filename)
         }
 
 //        else if (!g_strcmp0(mime_type, "text/plain")) {
-        else if (g_str_has_prefix(mime_type, "application/xml") ||
+        else if (g_str_has_prefix(mime_type, "text/plain") ||
+                 g_str_has_prefix(mime_type, "text/xml") ||
+                 g_str_has_prefix(mime_type, "application/xml") ||
                  g_str_has_prefix(mime_type, "application/x-xml") ||
-                 g_str_has_prefix(mime_type, "text/xml"))
+                 g_str_has_prefix(mime_type, "application/x-empty"))
         {
             type = NO_COMPRESSION;
         }
     } else {
-        g_debug(MODULE"%s: Mime type not detected!", __func__);
+        g_debug(MODULE"%s: Mime type not detected! (%s)", __func__, filename);
     }
 
 
@@ -153,12 +155,26 @@ CW_FILE *cw_open(const char *filename, int mode, CompressionType comtype)
     CompressionType type;
 
     if (!filename || (mode != CW_MODE_READ && mode != CW_MODE_WRITE)) {
+        g_debug(MODULE"%s: Filename is NULL or bad mode value", __func__);
         return NULL;
     }
 
 
     // Compression type detection
 
+    if (mode == CW_MODE_WRITE) {
+        if (comtype == AUTO_DETECT_COMPRESSION) {
+            g_debug(MODULE"%s: AUTO_DETECT_COMPRESSION cannot be used if mode is CW_MODE_WRITE", __func__);
+            return NULL;
+        }
+
+        if (comtype == UNKNOWN_COMPRESSION) {
+            g_debug(MODULE"%s: UNKNOWN_COMPRESSION cannot be used if mode is CW_MODE_WRITE", __func__);
+            return NULL;
+        }
+    }
+
+
     if (comtype != AUTO_DETECT_COMPRESSION) {
         type = comtype;
     } else {
@@ -166,6 +182,7 @@ CW_FILE *cw_open(const char *filename, int mode, CompressionType comtype)
     }
 
     if (type == UNKNOWN_COMPRESSION) {
+        g_debug(MODULE"%s: Cannot detect compression type", __func__);
         return NULL;
     }
 
@@ -321,7 +338,7 @@ int cw_read(CW_FILE *cw_file, void *buffer, unsigned int len)
 
 
 
-int cw_write(CW_FILE *cw_file, void *buffer, unsigned int len)
+int cw_write(CW_FILE *cw_file, const void *buffer, unsigned int len)
 {
     if (!cw_file || !buffer || cw_file->mode != CW_MODE_WRITE) {
         return CW_ERR;
@@ -340,13 +357,17 @@ int cw_write(CW_FILE *cw_file, void *buffer, unsigned int len)
             break;
 
         case (GZ_COMPRESSION): // ----------------------------------------------
+            if (len == 0) {
+                ret = 0;
+                break;
+            }
             if ((ret = gzwrite((gzFile) cw_file->FILE, buffer, len)) == 0) {
                 ret = CW_ERR;
             }
             break;
 
         case (BZ2_COMPRESSION): // ---------------------------------------------
-            BZ2_bzWrite(&bzerror, (BZFILE *) cw_file->FILE, buffer, len);
+            BZ2_bzWrite(&bzerror, (BZFILE *) cw_file->FILE, (void *) buffer, len);
             if (bzerror == BZ_OK) {
                 ret = len;
             } else {
index 0d9203d..8b79756 100644 (file)
@@ -32,7 +32,7 @@ CompressionType detect_compression(const char* filename);
 CW_FILE *cw_open(const char *filename, int mode, CompressionType comtype);
 
 int cw_read(CW_FILE *cw_file, void *buffer, unsigned int len);
-int cw_write(CW_FILE *cw_file, void *buffer, unsigned int len);
+int cw_write(CW_FILE *cw_file, const void *buffer, unsigned int len);
 int cw_puts(CW_FILE *cw_file, const char *str);
 int cw_printf(CW_FILE *cw_file, const char *format, ...);
 
diff --git a/tests/test_data/compressed_files/00_plain.foo0 b/tests/test_data/compressed_files/00_plain.foo0
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/test_data/compressed_files/00_plain.foo1 b/tests/test_data/compressed_files/00_plain.foo1
new file mode 100644 (file)
index 0000000..5108b2a
Binary files /dev/null and b/tests/test_data/compressed_files/00_plain.foo1 differ
diff --git a/tests/test_data/compressed_files/00_plain.foo2 b/tests/test_data/compressed_files/00_plain.foo2
new file mode 100644 (file)
index 0000000..b56f3b9
Binary files /dev/null and b/tests/test_data/compressed_files/00_plain.foo2 differ
diff --git a/tests/test_data/compressed_files/00_plain.txt b/tests/test_data/compressed_files/00_plain.txt
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/test_data/compressed_files/00_plain.txt.bz2 b/tests/test_data/compressed_files/00_plain.txt.bz2
new file mode 100644 (file)
index 0000000..b56f3b9
Binary files /dev/null and b/tests/test_data/compressed_files/00_plain.txt.bz2 differ
diff --git a/tests/test_data/compressed_files/00_plain.txt.gz b/tests/test_data/compressed_files/00_plain.txt.gz
new file mode 100644 (file)
index 0000000..5108b2a
Binary files /dev/null and b/tests/test_data/compressed_files/00_plain.txt.gz differ
diff --git a/tests/test_data/compressed_files/01_plain.foo0 b/tests/test_data/compressed_files/01_plain.foo0
new file mode 100644 (file)
index 0000000..9b02f7e
--- /dev/null
@@ -0,0 +1,2 @@
+foobar foobar foobar foobar test test
+folkjsaflkjsadokf
diff --git a/tests/test_data/compressed_files/01_plain.foo1 b/tests/test_data/compressed_files/01_plain.foo1
new file mode 100644 (file)
index 0000000..2ee9adc
Binary files /dev/null and b/tests/test_data/compressed_files/01_plain.foo1 differ
diff --git a/tests/test_data/compressed_files/01_plain.foo2 b/tests/test_data/compressed_files/01_plain.foo2
new file mode 100644 (file)
index 0000000..3c7a1dd
Binary files /dev/null and b/tests/test_data/compressed_files/01_plain.foo2 differ
diff --git a/tests/test_data/compressed_files/01_plain.txt b/tests/test_data/compressed_files/01_plain.txt
new file mode 100644 (file)
index 0000000..9b02f7e
--- /dev/null
@@ -0,0 +1,2 @@
+foobar foobar foobar foobar test test
+folkjsaflkjsadokf
diff --git a/tests/test_data/compressed_files/01_plain.txt.bz2 b/tests/test_data/compressed_files/01_plain.txt.bz2
new file mode 100644 (file)
index 0000000..3c7a1dd
Binary files /dev/null and b/tests/test_data/compressed_files/01_plain.txt.bz2 differ
diff --git a/tests/test_data/compressed_files/01_plain.txt.gz b/tests/test_data/compressed_files/01_plain.txt.gz
new file mode 100644 (file)
index 0000000..2ee9adc
Binary files /dev/null and b/tests/test_data/compressed_files/01_plain.txt.gz differ
diff --git a/tests/testcompression_wrapper.c b/tests/testcompression_wrapper.c
new file mode 100644 (file)
index 0000000..5892f22
--- /dev/null
@@ -0,0 +1,257 @@
+#include <glib.h>
+#include <glib/gstdio.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include "createrepo/misc.h"
+#include "createrepo/compression_wrapper.h"
+
+#define TMP_FILE_PATTERN                        "test_XXXXXX.txt"
+
+#define COMPRESSED_BUFFER_LEN                   512
+
+#define FILE_COMPRESSED_0_CONTENT               ""
+#define FILE_COMPRESSED_0_CONTENT_LEN           0
+#define FILE_COMPRESSED_0_PLAIN                 "test_data/compressed_files/00_plain.txt"
+#define FILE_COMPRESSED_0_GZ                    "test_data/compressed_files/00_plain.txt.gz"
+#define FILE_COMPRESSED_0_BZ2                   "test_data/compressed_files/00_plain.txt.bz2"
+#define FILE_COMPRESSED_0_PLAIN_BAD_SUFFIX      "test_data/compressed_files/00_plain.foo0"
+#define FILE_COMPRESSED_0_GZ_BAD_SUFFIX         "test_data/compressed_files/00_plain.foo1"
+#define FILE_COMPRESSED_0_BZ2_BAD_SUFFIX        "test_data/compressed_files/00_plain.foo2"
+
+#define FILE_COMPRESSED_1_CONTENT               "foobar foobar foobar foobar test test\nfolkjsaflkjsadokf\n"
+#define FILE_COMPRESSED_1_CONTENT_LEN           56
+#define FILE_COMPRESSED_1_PLAIN                 "test_data/compressed_files/01_plain.txt"
+#define FILE_COMPRESSED_1_GZ                    "test_data/compressed_files/01_plain.txt.gz"
+#define FILE_COMPRESSED_1_BZ2                   "test_data/compressed_files/01_plain.txt.bz2"
+#define FILE_COMPRESSED_1_PLAIN_BAD_SUFFIX      "test_data/compressed_files/01_plain.foo0"
+#define FILE_COMPRESSED_1_GZ_BAD_SUFFIX         "test_data/compressed_files/01_plain.foo1"
+#define FILE_COMPRESSED_1_BZ2_BAD_SUFFIX        "test_data/compressed_files/01_plain.foo2"
+
+
+
+static void test_get_suffix(void)
+{
+    const char *suffix;
+
+    suffix = get_suffix(AUTO_DETECT_COMPRESSION);
+    g_assert(!suffix);
+
+    suffix = get_suffix(UNKNOWN_COMPRESSION);
+    g_assert(!suffix);
+
+    suffix = get_suffix(NO_COMPRESSION);
+    g_assert(!suffix);
+
+    suffix = get_suffix(GZ_COMPRESSION);
+    g_assert_cmpstr(suffix, ==, ".gz");
+
+    suffix = get_suffix(BZ2_COMPRESSION);
+    g_assert_cmpstr(suffix, ==, ".bz2");
+}
+
+
+static void test_detect_compression(void)
+{
+    CompressionType ret;
+
+    // Plain
+
+    ret = detect_compression(FILE_COMPRESSED_0_PLAIN);
+    g_assert_cmpint(ret, ==, NO_COMPRESSION);
+    ret = detect_compression(FILE_COMPRESSED_1_PLAIN);
+    g_assert_cmpint(ret, ==, NO_COMPRESSION);
+
+    // Gz
+
+    ret = detect_compression(FILE_COMPRESSED_0_GZ);
+    g_assert_cmpint(ret, ==, GZ_COMPRESSION);
+    ret = detect_compression(FILE_COMPRESSED_1_GZ);
+    g_assert_cmpint(ret, ==, GZ_COMPRESSION);
+
+    // Bz2
+
+    ret = detect_compression(FILE_COMPRESSED_0_BZ2);
+    g_assert_cmpint(ret, ==, BZ2_COMPRESSION);
+    ret = detect_compression(FILE_COMPRESSED_1_BZ2);
+    g_assert_cmpint(ret, ==, BZ2_COMPRESSION);
+}
+
+
+static void test_detect_compression_bad_suffix(void)
+{
+    CompressionType ret;
+
+    // Plain
+
+    ret = detect_compression(FILE_COMPRESSED_0_PLAIN_BAD_SUFFIX);
+    printf("%s: (%d)\n", FILE_COMPRESSED_0_PLAIN_BAD_SUFFIX, ret);
+    g_assert_cmpint(ret, ==, NO_COMPRESSION);
+    ret = detect_compression(FILE_COMPRESSED_1_PLAIN_BAD_SUFFIX);
+    g_assert_cmpint(ret, ==, NO_COMPRESSION);
+
+    // Gz
+
+    ret = detect_compression(FILE_COMPRESSED_0_GZ_BAD_SUFFIX);
+    g_assert_cmpint(ret, ==, GZ_COMPRESSION);
+    ret = detect_compression(FILE_COMPRESSED_1_GZ_BAD_SUFFIX);
+    g_assert_cmpint(ret, ==, GZ_COMPRESSION);
+
+    // Bz2
+
+    ret = detect_compression(FILE_COMPRESSED_0_BZ2_BAD_SUFFIX);
+    g_assert_cmpint(ret, ==, BZ2_COMPRESSION);
+    ret = detect_compression(FILE_COMPRESSED_1_BZ2_BAD_SUFFIX);
+    g_assert_cmpint(ret, ==, BZ2_COMPRESSION);
+}
+
+
+void test_helper_cw_input(const char *filename, CompressionType ctype, const char *content, int len)
+{
+    int ret;
+    CW_FILE *file;
+    char buffer[COMPRESSED_BUFFER_LEN+1];
+
+    file = cw_open(filename, CW_MODE_READ, ctype);
+    g_assert(file != NULL);
+    ret = cw_read(file, buffer, COMPRESSED_BUFFER_LEN);
+    g_assert_cmpint(ret, ==, len);
+    buffer[ret] = '\0';
+    g_assert_cmpstr(buffer, ==, content);
+    ret = cw_close(file);
+    g_assert_cmpint(ret, ==, CW_OK);
+}
+
+
+static void test_cw_read_with_autodetection(void)
+{
+    // Plain
+
+    test_helper_cw_input(FILE_COMPRESSED_0_PLAIN, AUTO_DETECT_COMPRESSION, FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN);
+    test_helper_cw_input(FILE_COMPRESSED_1_PLAIN, AUTO_DETECT_COMPRESSION, FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN);
+
+    // Gz
+
+    test_helper_cw_input(FILE_COMPRESSED_0_GZ, AUTO_DETECT_COMPRESSION, FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN);
+    test_helper_cw_input(FILE_COMPRESSED_1_GZ, AUTO_DETECT_COMPRESSION, FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN);
+
+    // Bzip2
+
+    test_helper_cw_input(FILE_COMPRESSED_0_BZ2, AUTO_DETECT_COMPRESSION, FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN);
+    test_helper_cw_input(FILE_COMPRESSED_1_BZ2, AUTO_DETECT_COMPRESSION, FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN);
+}
+
+
+typedef struct {
+    gchar *tmp_filename;
+} Outputtest;
+
+
+static void outputtest_setup(Outputtest *outputtest, gconstpointer test_data)
+{
+    UNUSED(test_data);
+    int fd;
+
+    fd = g_file_open_tmp(TMP_FILE_PATTERN, &(outputtest->tmp_filename), NULL);
+    close(fd);
+}
+
+
+static void outputtest_teardown(Outputtest *outputtest, gconstpointer test_data)
+{
+    UNUSED(test_data);
+
+    if (outputtest->tmp_filename) {
+        remove(outputtest->tmp_filename);
+        g_free(outputtest->tmp_filename);
+    }
+}
+
+
+#define OUTPUT_TYPE_WRITE       0
+#define OUTPUT_TYPE_PUTS        1
+#define OUTPUT_TYPE_PRINTF      2
+
+void test_helper_cw_output(int type, const char *filename, CompressionType ctype, const char *content, int len) {
+    int ret;
+    CW_FILE *file;
+
+    file = cw_open(filename, CW_MODE_WRITE, ctype);
+    g_assert(file);
+
+    switch(type) {
+        case OUTPUT_TYPE_WRITE:
+            ret = cw_write(file, content, len);
+            g_assert_cmpint(ret, ==, len);
+            break;
+
+        case OUTPUT_TYPE_PUTS:
+            ret = cw_puts(file, content);
+            g_assert_cmpint(ret, ==, CW_OK);
+            break;
+
+        case OUTPUT_TYPE_PRINTF:
+            ret = cw_printf(file, "%s", content);
+            g_assert_cmpint(ret, ==, len);
+            break;
+
+        default:
+            break;
+    }
+
+    ret = cw_close(file);
+    g_assert_cmpint(ret, ==, CW_OK);
+
+    // Read and compare
+
+    test_helper_cw_input(filename, ctype, content, len);
+}
+
+
+static void outputtest_cw_output(Outputtest *outputtest, gconstpointer test_data)
+{
+    UNUSED(test_data);
+
+    CW_FILE *file;
+
+    file = cw_open(outputtest->tmp_filename, CW_MODE_WRITE, AUTO_DETECT_COMPRESSION);
+    g_assert(!file);
+
+    file = cw_open(outputtest->tmp_filename, CW_MODE_WRITE, UNKNOWN_COMPRESSION);
+    g_assert(!file);
+
+    // Plain
+
+    test_helper_cw_output(OUTPUT_TYPE_WRITE, outputtest->tmp_filename, NO_COMPRESSION, FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN);
+    test_helper_cw_output(OUTPUT_TYPE_WRITE, outputtest->tmp_filename, NO_COMPRESSION, FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN);
+    test_helper_cw_output(OUTPUT_TYPE_PUTS, outputtest->tmp_filename, NO_COMPRESSION, FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN);
+    test_helper_cw_output(OUTPUT_TYPE_PUTS, outputtest->tmp_filename, NO_COMPRESSION, FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN);
+
+    // Gz
+
+    test_helper_cw_output(OUTPUT_TYPE_WRITE, outputtest->tmp_filename, GZ_COMPRESSION, FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN);
+    test_helper_cw_output(OUTPUT_TYPE_WRITE, outputtest->tmp_filename, GZ_COMPRESSION, FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN);
+    test_helper_cw_output(OUTPUT_TYPE_PUTS, outputtest->tmp_filename, GZ_COMPRESSION, FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN);
+    test_helper_cw_output(OUTPUT_TYPE_PUTS, outputtest->tmp_filename, GZ_COMPRESSION, FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN);
+
+    // Bz2
+
+    test_helper_cw_output(OUTPUT_TYPE_WRITE, outputtest->tmp_filename, BZ2_COMPRESSION, FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN);
+    test_helper_cw_output(OUTPUT_TYPE_WRITE, outputtest->tmp_filename, BZ2_COMPRESSION, FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN);
+    test_helper_cw_output(OUTPUT_TYPE_PRINTF, outputtest->tmp_filename, BZ2_COMPRESSION, FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN);
+    test_helper_cw_output(OUTPUT_TYPE_PRINTF, outputtest->tmp_filename, BZ2_COMPRESSION, FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN);
+}
+
+
+
+int main(int argc, char *argv[])
+{
+    g_test_init(&argc, &argv, NULL);
+
+    g_test_add_func("/compression_wrapper/test_get_suffix", test_get_suffix);
+    g_test_add_func("/compression_wrapper/test_detect_compression", test_detect_compression);
+    g_test_add_func("/compression_wrapper/test_detect_compression_bad_suffix", test_detect_compression_bad_suffix);
+    g_test_add_func("/compression_wrapper/test_cw_read_with_autodetection", test_cw_read_with_autodetection);
+    g_test_add("/compression_wrapper/outputtest_cw_output", Outputtest, NULL, outputtest_setup, outputtest_cw_output, outputtest_teardown);
+
+    return g_test_run();
+}