]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
perl: Make verify() a function on the database
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 5 Dec 2019 15:34:06 +0000 (15:34 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 5 Dec 2019 15:34:06 +0000 (15:34 +0000)
This makes the API similar to the Python version

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/perl/Location.xs
src/perl/t/Location.t

index 5071f85bcffd23236bf16027208be75de35a1b00..02dff7d33aa26117df5a3070e2dbeba649102a37 100644 (file)
@@ -15,9 +15,8 @@
 MODULE = Location              PACKAGE = Location
 
 struct loc_database *
-init(file, keyfile)
+init(file)
        char* file;
-       char* keyfile;
 
        CODE:
                struct loc_ctx* ctx = NULL;
@@ -50,12 +49,25 @@ init(file, keyfile)
                        croak("Could not read database: %s\n", file);
                }
 
+               // Cleanup
+               loc_unref(ctx);
+
+               RETVAL = db;
+       OUTPUT:
+               RETVAL
+
+#
+# Database functions
+#
+bool
+verify(db, keyfile)
+       struct loc_database* db;
+       char* keyfile;
+
+       CODE:
                // Try to open the keyfile
-               f = fopen(keyfile, "r");
+               FILE* f = fopen(keyfile, "r");
                if (!f) {
-                       loc_database_unref(db);
-                       loc_unref(ctx);
-
                        croak("Could not open keyfile %s: %s\n",
                                keyfile, strerror(errno));
                }
@@ -63,26 +75,20 @@ init(file, keyfile)
                // Verify the database
                int status = loc_database_verify(db, f);
                if (status) {
-                       loc_database_unref(db);
-                       loc_unref(ctx);
+                       RETVAL = false;
                        fclose(f);
 
                        croak("Could not verify the database signature\n");
                }
 
+               // Database was validated successfully
+               RETVAL = true;
+
                // Close the keyfile
                fclose(f);
-
-               // Cleanup
-               loc_unref(ctx);
-
-               RETVAL = db;
        OUTPUT:
                RETVAL
 
-#
-# Database functions
-#
 const char*
 get_vendor(db)
        struct loc_database* db;
index 71cdce8badb07d24991292de8a9d08d8b72acb9e..3ba2d8ff4c8d40f3b0751e0e34074744a84dfebd 100644 (file)
@@ -12,7 +12,7 @@ use warnings;
 my $testdb = $ENV{'database'};
 my $keyfile = $ENV{'keyfile'};
 
-use Test::More tests => 6;
+use Test::More tests => 7;
 BEGIN { use_ok('Location') };
 
 #########################
@@ -24,7 +24,11 @@ BEGIN { use_ok('Location') };
 my $address = "2a07:1c44:5800::1";
 
 # Connect to the database.
-my $db = &Location::init("$testdb", "$keyfile");
+my $db = &Location::init("$testdb");
+
+# Verify
+my $status = &Location::verify($db, $keyfile);
+ok($status, "This database is valid");
 
 my $vendor = &Location::get_vendor($db);
 ok($vendor eq "IPFire Project", "Test 1 - Get Database Vendor");