Many improvements -- now does good database-creation stuff.
[ZOOM-Perl-moved-to-github.git] / t / 16-packages.t
1 # $Id: 16-packages.t,v 1.5 2005-12-07 15:28:07 mike Exp $
2
3 # Before `make install' is performed this script should be runnable with
4 # `make test'. After `make install' it should work as `perl 16-packages.t'
5
6 # Tests:
7 #       connect anonymously => refused
8 #       connect as "user" with incorrect password => refused
9 #       connect as "user" with correct password
10 #               try to create tmpdb => EPERM
11 #       connect as admin with correct password
12 #               try to create tmpdb => OK
13 #               try to create tmpdb => EFAIL
14
15 use strict;
16 use warnings;
17 use Test::More tests => 32;
18
19 BEGIN { use_ok('Net::Z3950::ZOOM') };
20
21
22 # For now, use a local database: later establish a public one for this.
23 # We will create, and destroy, a new database with a random name
24 my $host = "localhost:9999";
25 #my $host = "indexdata.com/gils";
26 my $dbname = join("", map { chr(ord("a") + int(rand(26))) } 1..10);
27
28 # Connect anonymously, and expect this to fail
29 my $conn = makeconn($host, undef, undef, 1011);
30
31 # Connect as a user, but with incorrect password -- expect failure
32 Net::Z3950::ZOOM::connection_destroy($conn);
33 $conn = makeconn($host, "user", "badpw", 1011);
34
35 # Connect as a non-privileged user with correct password
36 Net::Z3950::ZOOM::connection_destroy($conn);
37 $conn = makeconn($host, "user", "frog", 0);
38
39 # Non-privileged user can't create database
40 makedb($conn, $dbname, 223);
41
42 # Connect as a privileged user with correct password, check DB is absent
43 Net::Z3950::ZOOM::connection_destroy($conn);
44 $conn = makeconn($host, "admin", "fish", 0);
45 Net::Z3950::ZOOM::connection_option_set($conn, databaseName => $dbname);
46 my $rs = Net::Z3950::ZOOM::connection_search_pqf($conn, "the");
47 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
48 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
49 ok($errcode == 109, "database '$dbname' does not yet exist");
50
51 # Now create the database and check that it is present but empty
52 makedb($conn, $dbname, 0);
53 $rs = Net::Z3950::ZOOM::connection_search_pqf($conn, "the");
54 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
55 ok($errcode == 0, "database '$dbname' does now exists");
56 my $n = Net::Z3950::ZOOM::resultset_size($rs);
57 ok($n == 0, "database '$dbname' is empty");
58
59 # Trying to create the same database again will fail EEXIST
60 makedb($conn, $dbname, 224);
61
62 # Try to add a non-existent record
63 updaterec($conn, 465, "samples/records/notthere.grs", 224);
64
65 # Add a single record, and check that it can be found
66 updaterec($conn, 465, "samples/records/esdd0006.grs", 0);
67
68 # Now drop the newly-created database
69 dropdb($conn, $dbname, 0);
70
71 # A second dropping should fail, but does not do so -- I think that
72 # "drop" is an always-"successful" no-op.  Yuck.
73 dropdb($conn, $dbname, 0);
74
75
76 sub makeconn {
77     my($host, $user, $password, $expected_error) = @_;
78
79     my $options = Net::Z3950::ZOOM::options_create();
80     Net::Z3950::ZOOM::options_set($options, user => $user)
81         if defined $user;
82     Net::Z3950::ZOOM::options_set($options, password => $password)
83         if defined $password;
84
85     my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
86     my $conn = Net::Z3950::ZOOM::connection_create($options);
87     $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
88     ok($errcode == 0, "unconnected connection object created");
89
90     Net::Z3950::ZOOM::connection_connect($conn, $host, 0);
91     $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
92     ok($errcode == $expected_error,
93        "connection to '$host'" . ($errcode ? " refused $errcode" : ""));
94
95     return $conn;
96 }
97
98
99 sub makedb {
100     my($conn, $dbname, $expected_error) = @_;
101
102     my $o = Net::Z3950::ZOOM::options_create();
103     my $p = Net::Z3950::ZOOM::connection_package($conn, $o);
104     # Inspection of the ZOOM-C code shows that this can never fail, in fact.
105     ok(defined $p, "created package");
106
107     Net::Z3950::ZOOM::package_option_set($p, databaseName => $dbname);
108     my $val = Net::Z3950::ZOOM::package_option_get($p, "databaseName");
109     ok($val eq $dbname, "package option retrieved as expected");
110
111     Net::Z3950::ZOOM::package_send($p, "create");
112     my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
113     $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
114     ok($errcode == $expected_error,
115        "database creation '$dbname'"  . ($errcode ? " refused $errcode" : ""));
116
117     # Now we can inspect the package options to find out more about
118     # how the server dealt with the request.  However, it seems that
119     # the "package database" described in the standard is not used,
120     # and that the only options we can inspect are the following:
121     $val = Net::Z3950::ZOOM::package_option_get($p, "targetReference");
122     $val = Net::Z3950::ZOOM::package_option_get($p, "xmlUpdateDoc");
123     # ... and we know nothing about expected or actual values.
124
125     Net::Z3950::ZOOM::package_destroy($p);
126     ok(1, "destroyed createdb package");
127 }
128
129
130 sub dropdb {
131     my($conn, $dbname, $expected_error) = @_;
132
133     my $o = Net::Z3950::ZOOM::options_create();
134     my $p = Net::Z3950::ZOOM::connection_package($conn, $o);
135     # No need to keep ok()ing this, or checking the option-setting
136     Net::Z3950::ZOOM::package_option_set($p, databaseName => $dbname);
137
138     Net::Z3950::ZOOM::package_send($p, "drop");
139     my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
140     $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
141     ok($errcode == $expected_error,
142        "database drop '$dbname'"  . ($errcode ? " refused $errcode" : ""));
143
144     Net::Z3950::ZOOM::package_destroy($p);
145     ok(1, "destroyed dropdb package");
146 }
147
148
149 # We always use "specialUpdate", which adds a record or replaces it if
150 # it's already there.  By contrast, "insert" fails if the record
151 # already exists, and "replace" fails if it does not.
152 #
153 sub updaterec {
154     my($conn, $id, $file, $expected_error) = @_;
155
156     my $o = Net::Z3950::ZOOM::options_create();
157     my $p = Net::Z3950::ZOOM::connection_package($conn, $o);
158     Net::Z3950::ZOOM::package_option_set($p, action => "specialUpdate");
159     Net::Z3950::ZOOM::package_option_set($p, recordIdOpaque => $id);
160     Net::Z3950::ZOOM::package_option_set($p, record => $file);
161
162     Net::Z3950::ZOOM::package_send($p, "update");
163     my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
164     $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
165     ok($errcode == $expected_error,
166        "record update $id '$file'"  . ($errcode ? " failed $errcode $errmsg $addinfo" : ""));
167
168     Net::Z3950::ZOOM::package_destroy($p);
169     ok(1, "destroyed update package");
170 }