1 # $Id: 16-packages.t,v 1.5 2005-12-07 15:28:07 mike Exp $
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'
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
17 use Test::More tests => 32;
19 BEGIN { use_ok('Net::Z3950::ZOOM') };
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);
28 # Connect anonymously, and expect this to fail
29 my $conn = makeconn($host, undef, undef, 1011);
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);
35 # Connect as a non-privileged user with correct password
36 Net::Z3950::ZOOM::connection_destroy($conn);
37 $conn = makeconn($host, "user", "frog", 0);
39 # Non-privileged user can't create database
40 makedb($conn, $dbname, 223);
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");
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");
59 # Trying to create the same database again will fail EEXIST
60 makedb($conn, $dbname, 224);
62 # Try to add a non-existent record
63 updaterec($conn, 465, "samples/records/notthere.grs", 224);
65 # Add a single record, and check that it can be found
66 updaterec($conn, 465, "samples/records/esdd0006.grs", 0);
68 # Now drop the newly-created database
69 dropdb($conn, $dbname, 0);
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);
77 my($host, $user, $password, $expected_error) = @_;
79 my $options = Net::Z3950::ZOOM::options_create();
80 Net::Z3950::ZOOM::options_set($options, user => $user)
82 Net::Z3950::ZOOM::options_set($options, password => $password)
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");
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" : ""));
100 my($conn, $dbname, $expected_error) = @_;
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");
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");
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" : ""));
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.
125 Net::Z3950::ZOOM::package_destroy($p);
126 ok(1, "destroyed createdb package");
131 my($conn, $dbname, $expected_error) = @_;
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);
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" : ""));
144 Net::Z3950::ZOOM::package_destroy($p);
145 ok(1, "destroyed dropdb package");
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.
154 my($conn, $id, $file, $expected_error) = @_;
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);
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" : ""));
168 Net::Z3950::ZOOM::package_destroy($p);
169 ok(1, "destroyed update package");