commit:4a1ef10deb4ba66c2008a21865b9ed07a7e88def
author:Chip Black
committer:Chip Black
date:Sat Nov 23 16:22:47 2013 -0600
parents:0633c5ed9ca5302ba9ff0eb9050bc23f1173e38a
Add auth bits to Blerg::Database

Also, reorganized tests into database, subscription, and auth portions.
diff --git a/lib/perl/Blerg-Database/Database.xs b/lib/perl/Blerg-Database/Database.xs
line changes: +21/-8
index 69d2442..a3f87a5
--- a/lib/perl/Blerg-Database/Database.xs
+++ b/lib/perl/Blerg-Database/Database.xs
@@ -10,6 +10,7 @@
 #include "database/tags.h"
 #include "database/subscription.h"
 #include "database/util.h"
+#include "common/auth.h"
 
 #include "const-c.inc"
 
@@ -144,16 +145,8 @@ void tag_list(const char *tag, const char *str_offset, int direction)
         free(list);
 
 int subscription_add(const char *from, const char *to)
-    CODE:
-        RETVAL = subscription_add(from, to);
-    OUTPUT:
-        RETVAL
 
 int subscription_remove(const char *from, const char *to)
-    CODE:
-        RETVAL = subscription_remove(from, to);
-    OUTPUT:
-        RETVAL
 
 void _subscription_list(const char *author, const char *str_offset, int direction)
     INIT:
@@ -179,3 +172,23 @@ void _subscription_list(const char *author, const char *str_offset, int directio
 int valid_tag_name(const char *name)
 
 int valid_name(const char *name)
+
+int auth_set_password(const char *username, const char *password)
+
+int auth_check_password(const char *username, const char *password)
+
+char * auth_login(const char *username, const char *password)
+    INIT:
+        char *token;
+    PPCODE:
+        token = auth_login(username, password);
+        if (token != NULL) {
+            XPUSHs(sv_2mortal(newSVpv(token, TOKEN_SIZE * 2)));
+        } else {
+            XSRETURN_UNDEF;
+        }
+        free(token);
+
+int auth_logout(const char *username, const char *token)
+
+int auth_check_token(const char *username, const char *given_token)

diff --git a/lib/perl/Blerg-Database/Makefile.PL b/lib/perl/Blerg-Database/Makefile.PL
line changes: +1/-1
index c8810f0..6003b64
--- a/lib/perl/Blerg-Database/Makefile.PL
+++ b/lib/perl/Blerg-Database/Makefile.PL
@@ -10,7 +10,7 @@ WriteMakefile(
       (ABSTRACT_FROM  => 'lib/Blerg/Database.pm', # retrieve abstract from module
        AUTHOR         => 'Chip Black <bytex64@bytex64.net>') : ()),
     LIBS              => [''], # e.g., '-lm'
-    OBJECT            => '$(BASEEXT)$(OBJ_EXT) ../../../blerg.a',
+    OBJECT            => '$(BASEEXT)$(OBJ_EXT) ../../../blerg.a ../../../blerg_auth.a ../../../builddeps/scrypt.a',
     DEFINE            => '', # e.g., '-DHAVE_SOMETHING'
     INC               => '-I../../..', # e.g., '-I. -I/usr/include/other'
 	# Un-comment this if you add C files to link with later:

diff --git a/lib/perl/Blerg-Database/t/Blerg-Database.t b/lib/perl/Blerg-Database/t/Blerg-Database.t
line changes: +0/-128
index 4208375..0000000
--- a/lib/perl/Blerg-Database/t/Blerg-Database.t
+++ /dev/null
@@ -1,128 +0,0 @@
-# Before 'make install' is performed this script should be runnable with
-# 'make test'. After 'make install' it should work as 'perl Blerg-Database.t'
-
-#########################
-
-# change 'tests => 1' to 'tests => last_test_to_print';
-
-use File::Path qw/remove_tree/;
-use strict;
-use warnings;
-
-use Test::More tests => 34;
-BEGIN { use_ok('Blerg::Database') };
-
-#########################
-
-# Insert your test code below, the Test::More module is use()ed here so read
-# its man page ( perldoc Test::More ) for help writing this test script.
-
-# Setup
-my $test_user = 'barfy';
-my $test_dir = "/tmp/blerg_test_$$";
-mkdir $test_dir;
-chdir $test_dir;
-mkdir 'data';
-mkdir 'hash_tags';
-mkdir 'ref_tags';
-my ($ptr, @list, $n);
-$n = 0;
-
-### C interfaces
-# Open a database
-$ptr = Blerg::Database::_open($test_user);
-ok( ref $ptr eq 'struct blergPtr' );
-
-# Store
-my $data = "Hello!";
-my $t0 = time;
-ok( Blerg::Database::_store($ptr, $data) == $n );
-
-# Check internal database
-open DATA, "data/$test_user/data0";
-my $check_data = <DATA>;
-close DATA;
-ok( $check_data eq $data );
-
-# Fetch
-ok( Blerg::Database::_fetch($ptr, $n) eq $data );
-ok( not defined Blerg::Database::_fetch($ptr, $n + 1) );
-
-# Get time
-my $t = Blerg::Database::_get_timestamp($ptr, $n);
-ok( $t0 - $t <= 1.0 );
-
-# Subscription
-my ($ptra, $ptrb);
-$ptra = Blerg::Database::_open("a");
-$ptrb = Blerg::Database::_open("b");
-ok( Blerg::Database::subscription_add("a", "b") == 1 );
-Blerg::Database::_store($ptrb, "Hello, A!");
-@list = Blerg::Database::_subscription_list("a", 0, 1);
-ok( @list == 1 );
-ok( $list[0]->{author} eq 'b' );
-ok( $list[0]->{record} == 0 );
-ok( Blerg::Database::_fetch($ptrb, $list[0]->{record}) eq 'Hello, A!' );
-
-# Test tags
-Blerg::Database::_store($ptr, "This is data A #data");
-Blerg::Database::_store($ptr, "This is data B #data");
-Blerg::Database::_store($ptr, "This is data C #data");
-$n += 3;
-
-@list = Blerg::Database::tag_list('#data', 0, 1);
-ok( @list == 3 );
-ok( $list[0]->{author} eq $test_user );
-ok( $list[0]->{record} == $n );
-
-# Test mute
-ok( Blerg::Database::_set_mute($ptr, 1) );
-ok( Blerg::Database::_get_mute($ptr) == 1 );
-Blerg::Database::_store($ptr, "Testing more #data");
-@list = Blerg::Database::tag_list('#data', 0, 1);
-ok( @list == 3 );
-ok( Blerg::Database::_set_mute($ptr, 0) );
-ok( Blerg::Database::_get_mute($ptr) == 0 );
-Blerg::Database::_store($ptr, "Testing more #data");
-@list = Blerg::Database::tag_list('#data', 0, 1);
-ok( @list == 4 );
-$n += 2;
-
-# Count
-my $count = Blerg::Database::_get_record_count($ptr);
-ok( $count == $n + 1 );
-
-# Close database
-ok( Blerg::Database::_close($ptr) );
-
-# Test existence
-ok( Blerg::Database::exists($test_user) );
-
-
-# Perl OO interface
-my $blerg = Blerg::Database->open_existing('nonexistent');
-ok( not defined $blerg );
-$blerg = Blerg::Database->open($test_user);
-ok( defined $blerg );
-ok( ref $blerg->{ptr} eq 'struct blergPtr');
-ok( $blerg->fetch(0) eq $data );
-ok( $blerg->store($data) == $n + 1 );
-$n++;
-ok( $blerg->fetch($n) eq $data );
-$blerg->close;
-
-# Subscription OO-style
-$ptra = Blerg::Database->open_existing('a');
-$ptrb = Blerg::Database->open_existing('b');
-@list = $ptra->subscription_list;
-ok( @list == 1 );
-ok( $list[0]->{author} eq 'b' );
-ok( $list[0]->{record} == 0 );
-ok( $ptrb->fetch($list[0]->{record}) eq 'Hello, A!' );
-$ptrb->close;
-$ptra->close;
-
-END {
-	chdir;
-	remove_tree "/tmp/blerg_test_$$";
-}

diff --git a/lib/perl/Blerg-Database/t/auth.t b/lib/perl/Blerg-Database/t/auth.t
line changes: +54/-0
index 0000000..2762480
--- /dev/null
+++ b/lib/perl/Blerg-Database/t/auth.t
@@ -0,0 +1,54 @@
+use File::Path qw/remove_tree/;
+use strict;
+use warnings;
+
+use Test::More tests => 17;
+BEGIN { use_ok('Blerg::Database') };
+
+### Setup
+my $test_user = 'barfy';
+my $test_dir = "/tmp/blerg_test_$$";
+mkdir $test_dir;
+chdir $test_dir;
+mkdir 'data';
+mkdir 'hash_tags';
+mkdir 'ref_tags';
+my ($ptr, $password, $n);
+$password = 'blargh';
+$n = 0;
+
+$ptr = Blerg::Database->open($test_user);
+$ptr->close;
+
+### Auth (C interface)
+is(   Blerg::Database::auth_set_password($test_user, $password), 1 );
+isnt( Blerg::Database::auth_set_password('fakeuser', $password), 1 );
+
+is(   Blerg::Database::auth_check_password($test_user, $password), 1 );
+isnt( Blerg::Database::auth_check_password('fakeuser', $password), 1 );
+isnt( Blerg::Database::auth_check_password($test_user, 'wrongpass'), 1 );
+
+my $token = Blerg::Database::auth_login($test_user, $password);
+ok( defined $token );
+ok( length($token) == 32 );
+ok( Blerg::Database::auth_check_token($test_user, $token) );
+isnt( Blerg::Database::auth_check_token($test_user, 'badtoken'), 1 );
+isnt( Blerg::Database::auth_check_token('fakeuser', $token), 1 );
+ok( Blerg::Database::auth_logout($test_user, $token) );
+
+$token = Blerg::Database::auth_login($test_user, 'wrongpass');
+ok( not defined $token );
+
+$token = Blerg::Database::auth_login('wronguser', $password);
+ok( not defined $token );
+
+isnt( Blerg::Database::auth_logout($test_user, 'badtoken'), 1 );
+isnt( Blerg::Database::auth_logout('fakeuser', 'badtoken'), 1 );
+
+Blerg::Database::auth_set_password($test_user, $password . 'X');
+ok( Blerg::Database::auth_check_password($test_user, $password . 'X') );
+
+END {
+	chdir;
+	remove_tree "/tmp/blerg_test_$$";
+}

diff --git a/lib/perl/Blerg-Database/t/database.t b/lib/perl/Blerg-Database/t/database.t
line changes: +114/-0
index 0000000..6faed8e
--- /dev/null
+++ b/lib/perl/Blerg-Database/t/database.t
@@ -0,0 +1,114 @@
+# Before 'make install' is performed this script should be runnable with
+# 'make test'. After 'make install' it should work as 'perl Blerg-Database.t'
+
+#########################
+
+# change 'tests => 1' to 'tests => last_test_to_print';
+
+use File::Path qw/remove_tree/;
+use strict;
+use warnings;
+
+use Test::More tests => 25;
+BEGIN { use_ok('Blerg::Database') };
+
+#########################
+
+# Insert your test code below, the Test::More module is use()ed here so read
+# its man page ( perldoc Test::More ) for help writing this test script.
+
+# Setup
+my $test_user = 'barfy';
+my $test_dir = "/tmp/blerg_test_$$";
+mkdir $test_dir;
+chdir $test_dir;
+mkdir 'data';
+mkdir 'hash_tags';
+mkdir 'ref_tags';
+my ($ptr, @list, $n);
+$n = 0;
+
+### C interface
+
+# Open a database
+$ptr = Blerg::Database::_open($test_user);
+ok( ref $ptr eq 'struct blergPtr' );
+
+# Store
+my $data = "Hello!";
+my $t0 = time;
+ok( Blerg::Database::_store($ptr, $data) == $n );
+
+# Check internal database
+open DATA, "data/$test_user/data0";
+my $check_data = <DATA>;
+close DATA;
+ok( $check_data eq $data );
+
+# Fetch
+ok( Blerg::Database::_fetch($ptr, $n) eq $data );
+ok( not defined Blerg::Database::_fetch($ptr, $n + 1) );
+
+# Get time
+my $t = Blerg::Database::_get_timestamp($ptr, $n);
+ok( $t0 - $t <= 1.0 );
+
+# Test tags
+Blerg::Database::_store($ptr, "This is data A #data");
+Blerg::Database::_store($ptr, "This is data B #data");
+Blerg::Database::_store($ptr, "This is data C #data");
+$n += 3;
+
+@list = Blerg::Database::tag_list('#data', 0, 1);
+ok( @list == 3 );
+ok( $list[0]->{author} eq $test_user );
+ok( $list[0]->{record} == $n );
+
+# Test mute
+ok( Blerg::Database::_set_mute($ptr, 1) );
+ok( Blerg::Database::_get_mute($ptr) == 1 );
+Blerg::Database::_store($ptr, "Testing more #data");
+@list = Blerg::Database::tag_list('#data', 0, 1);
+ok( @list == 3 );
+ok( Blerg::Database::_set_mute($ptr, 0) );
+ok( Blerg::Database::_get_mute($ptr) == 0 );
+Blerg::Database::_store($ptr, "Testing more #data");
+@list = Blerg::Database::tag_list('#data', 0, 1);
+ok( @list == 4 );
+$n += 2;
+
+# Count
+my $count = Blerg::Database::_get_record_count($ptr);
+ok( $count == $n + 1 );
+
+# Close database
+ok( Blerg::Database::_close($ptr) );
+
+# Test existence
+ok( Blerg::Database::exists($test_user) );
+
+
+### Perl OO interface
+
+# Open a nonexistent database
+my $blerg = Blerg::Database->open_existing('nonexistent');
+ok( not defined $blerg );
+
+# Open a database
+$blerg = Blerg::Database->open($test_user);
+ok( defined $blerg );
+ok( ref $blerg->{ptr} eq 'struct blergPtr');
+
+# Fetch/Store
+ok( $blerg->fetch(0) eq $data );
+ok( $blerg->store($data) == $n + 1 );
+$n++;
+ok( $blerg->fetch($n) eq $data );
+
+# Close database
+$blerg->close;
+
+END {
+	chdir;
+	remove_tree "/tmp/blerg_test_$$";
+}

diff --git a/lib/perl/Blerg-Database/t/subscription.t b/lib/perl/Blerg-Database/t/subscription.t
line changes: +42/-0
index 0000000..96fa074
--- /dev/null
+++ b/lib/perl/Blerg-Database/t/subscription.t
@@ -0,0 +1,42 @@
+use File::Path qw/remove_tree/;
+use strict;
+use warnings;
+
+use Test::More tests => 10;
+BEGIN { use_ok('Blerg::Database') };
+
+### Setup
+my $test_dir = "/tmp/blerg_test_$$";
+mkdir $test_dir;
+chdir $test_dir;
+mkdir 'data';
+mkdir 'hash_tags';
+mkdir 'ref_tags';
+my ($ptra, $ptrb, @list);
+
+### Subscription
+$ptra = Blerg::Database::_open("a");
+$ptrb = Blerg::Database::_open("b");
+ok( Blerg::Database::subscription_add("a", "b") == 1 );
+Blerg::Database::_store($ptrb, "Hello, A!");
+@list = Blerg::Database::_subscription_list("a", 0, 1);
+ok( @list == 1 );
+ok( $list[0]->{author} eq 'b' );
+ok( $list[0]->{record} == 0 );
+ok( Blerg::Database::_fetch($ptrb, $list[0]->{record}) eq 'Hello, A!' );
+
+### Subscription OO-style
+$ptra = Blerg::Database->open_existing('a');
+$ptrb = Blerg::Database->open_existing('b');
+@list = $ptra->subscription_list;
+ok( @list == 1 );
+ok( $list[0]->{author} eq 'b' );
+ok( $list[0]->{record} == 0 );
+ok( $ptrb->fetch($list[0]->{record}) eq 'Hello, A!' );
+$ptrb->close;
+$ptra->close;
+
+END {
+	chdir;
+	remove_tree "/tmp/blerg_test_$$";
+}