From 3bbf013aa84d0b769c8c41c0abf46091427fc5e3 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Thu, 5 Dec 2019 15:34:06 +0000 Subject: [PATCH] perl: Make verify() a function on the database This makes the API similar to the Python version Signed-off-by: Michael Tremer --- src/perl/Location.xs | 38 ++++++++++++++++++++++---------------- src/perl/t/Location.t | 8 ++++++-- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/perl/Location.xs b/src/perl/Location.xs index 5071f85..02dff7d 100644 --- a/src/perl/Location.xs +++ b/src/perl/Location.xs @@ -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; diff --git a/src/perl/t/Location.t b/src/perl/t/Location.t index 71cdce8..3ba2d8f 100644 --- a/src/perl/t/Location.t +++ b/src/perl/t/Location.t @@ -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"); -- 2.47.3