Add simple c test.
authorTomas Mlcoch <tmlcoch@redhat.com>
Wed, 11 Jan 2012 11:05:59 +0000 (12:05 +0100)
committerTomas Mlcoch <tmlcoch@redhat.com>
Wed, 11 Jan 2012 11:05:59 +0000 (12:05 +0100)
Makefile
ctests/icedax-1.1.10-2.fc14.i686.rpm [new file with mode: 0644]
ctests/parsepkg_test_01.c [new file with mode: 0644]
parsepkg.c

index 91984a8..3db4d49 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,9 +1,14 @@
 SWIG=/usr/bin/swig
 
-CFLAGS=-DDEBUG -I/usr/include/python2.7/ `pkg-config --cflags glib-2.0` `xml2-config --cflags`
-LINKFLAGS=`pkg-config --libs glib-2.0` `xml2-config --libs` -lrpm
+# FIXME: There must be -O option in cflags or we get: _parsepkg.so: undefined symbol: stat
+#        link option -lc should solves this problem but it seems to be doesn't working
+
+CFLAGS=-O -DDEBUG -I/usr/include/python2.7/ `pkg-config --cflags glib-2.0` `xml2-config --cflags`
+LINKFLAGS=`pkg-config --libs glib-2.0` `xml2-config --libs` -lrpm -lrpmio -lc
 
 all: package.so xml_dump.so parsehdr.so parsepkg.so
+ctests: parsepkg_test_01
 
 test: main
 
@@ -25,7 +30,7 @@ parsehdr.o parsehdr_wrap.o: parsehdr.c parsehdr.h
        $(SWIG) -python -Wall parsehdr.i
        gcc $(CFLAGS) -c parsehdr.c parsehdr_wrap.c
 
-parsepkg.o parsepkg_wrap.o: parsepkg.c parsepkg.h
+parsepkg.o parsepkg_wrap.o: parsepkg.c parsepkg.h constants.h
        $(SWIG) -python -Wall parsepkg.i
        gcc $(CFLAGS) -c parsepkg.c parsepkg_wrap.c
 
@@ -55,6 +60,11 @@ parsehdr.so: parsehdr_wrap.o parsehdr.o package.o xml_dump.o misc.o
 parsepkg.so: parsepkg_wrap.o parsepkg.o parsehdr.o package.o xml_dump.o misc.o
        ld $(LINKFLAGS) -shared misc.o parsepkg_wrap.o parsepkg.o parsehdr.o package.o xml_dump.o xml_dump_primary.o xml_dump_filelists.o xml_dump_other.o -o _parsepkg.so
 
+# Tests
+
+parsepkg_test_01: parsepkg.o parsehdr.o package.o xml_dump.o xml_dump_primary.o xml_dump_filelists.o xml_dump_other.o misc.o
+       gcc $(LINKFLAGS) $(CFLAGS) parsepkg.o parsehdr.o package.o xml_dump.o xml_dump_primary.o xml_dump_filelists.o xml_dump_other.o misc.o ctests/parsepkg_test_01.c -o ctests/parsepkg_test_01
+
 # Main
 
 main: package.o xml_dump.o xml_dump_primary.o xml_dump_filelists.o xml_dump_other.o
diff --git a/ctests/icedax-1.1.10-2.fc14.i686.rpm b/ctests/icedax-1.1.10-2.fc14.i686.rpm
new file mode 100644 (file)
index 0000000..87800e1
Binary files /dev/null and b/ctests/icedax-1.1.10-2.fc14.i686.rpm differ
diff --git a/ctests/parsepkg_test_01.c b/ctests/parsepkg_test_01.c
new file mode 100644 (file)
index 0000000..5064160
--- /dev/null
@@ -0,0 +1,14 @@
+#include <stdio.h>
+#include "../xml_dump.h"
+#include "../package.h"
+#include "../parsepkg.h"
+
+int main() {
+    struct XmlStruct res;
+    res = xml_from_package_file("icedax-1.1.10-2.fc14.i686.rpm", "", "", "", 4, 10,12);
+
+    printf("Test - Start\n");
+    printf("%s\n\n%s\n\n%s\n", res.primary, res.filelists, res.other);
+    printf("Test - Done\n");
+    return 0;
+}
\ No newline at end of file
index 46ab0b7..236ce4e 100644 (file)
@@ -1,4 +1,7 @@
 #include <glib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
 #include <rpm/rpmfi.h>
 #include "parsehdr.h"
 #include "xml_dump.h"
@@ -49,28 +52,39 @@ struct XmlStruct xml_from_package_file(const char *filename, const char *checksu
         }
     }
 
+    // Cleanup
+    rpmtsFree(ts);
+    Fclose(fd);
+
     //
     // Get file stat
     //
 
-    gint64 mtime = 0;
-    gint64 size = 0;
+    gint64 mtime  = 0;
+    gint64 size   = 0;
+
+    struct stat stat_buf;
+    if (stat(filename, &stat_buf) == -1) {
+        perror("stat");
+        return result;
+    }
+    mtime  = stat_buf.st_mtime;
+    size   = stat_buf.st_size;
+
+    //
+    // Compute checksum
+    //
+
     char *checksum = "abcdefghijklmnop";
-    //char *location_href = NULL;
-    //char *location_base = NULL;
-    //int changelog_limit = 5;
-    //gint64 hdr_start = 10;
-    //gint64 hdr_end   = 22;
 
     //
     // Gen XML
     //
 
-    Package *pkg = parse_header(hdr, mtime, size, checksum, checksum_type, location_href,
+    result = xml_from_header(hdr, mtime, size, checksum, checksum_type, location_href,
                                 location_base, changelog_limit, hdr_start, hdr_end);
 
-    result.primary = xml_dump_primary(pkg, NULL);
-    result.filelists = xml_dump_filelists(pkg, NULL);
-    result.other = xml_dump_other(pkg, NULL);
+    headerFree(hdr);   // ???
+
     return result;
 }