1 # $Id: 16-packages.t,v 1.7 2005-12-09 10:17:35 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 => 40;
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 count_hits($conn, "the", 109);
48 # Now create the database and check that it is present but empty
49 makedb($conn, $dbname, 0);
50 count_hits($conn, "the", 0, 0);
52 # Trying to create the same database again will fail EEXIST
53 makedb($conn, $dbname, 224);
55 # Add a single record, and check that it can be found
56 updaterec($conn, 1, content_of("samples/records/esdd0006.grs"), 0);
57 count_hits($conn, "the", 0, 1);
59 # Add the same record with the same ID: overwrite => no change
60 updaterec($conn, 1, content_of("samples/records/esdd0006.grs"), 0);
61 count_hits($conn, "the", 0, 1);
63 # Add it again record with different ID => new copy added
64 updaterec($conn, 2, content_of("samples/records/esdd0006.grs"), 0);
65 count_hits($conn, "the", 0, 2);
67 # Now drop the newly-created database
68 dropdb($conn, $dbname, 0);
70 # A second dropping should fail, but does not do so -- I think that
71 # "drop" is an always-"successful" no-op. Yuck.
72 dropdb($conn, $dbname, 0);
76 my($host, $user, $password, $expected_error) = @_;
78 my $options = Net::Z3950::ZOOM::options_create();
79 Net::Z3950::ZOOM::options_set($options, user => $user)
81 Net::Z3950::ZOOM::options_set($options, password => $password)
84 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
85 my $conn = Net::Z3950::ZOOM::connection_create($options);
86 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
87 ok($errcode == 0, "unconnected connection object created");
89 Net::Z3950::ZOOM::connection_connect($conn, $host, 0);
90 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
91 ok($errcode == $expected_error,
92 "connection to '$host'" . ($errcode ? " refused ($errcode)" : ""));
99 my($conn, $dbname, $expected_error) = @_;
101 my $o = Net::Z3950::ZOOM::options_create();
102 my $p = Net::Z3950::ZOOM::connection_package($conn, $o);
103 # Inspection of the ZOOM-C code shows that this can never fail, in fact.
104 ok(defined $p, "created package");
106 Net::Z3950::ZOOM::package_option_set($p, databaseName => $dbname);
107 my $val = Net::Z3950::ZOOM::package_option_get($p, "databaseName");
108 ok($val eq $dbname, "package option retrieved as expected");
110 Net::Z3950::ZOOM::package_send($p, "create");
111 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
112 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
113 ok($errcode == $expected_error, "database creation '$dbname'" .
114 ($errcode ? " refused ($errcode)" : ""));
116 # Now we can inspect the package options to find out more about
117 # how the server dealt with the request. However, it seems that
118 # the "package database" described in the standard is not used,
119 # and that the only options we can inspect are the following:
120 $val = Net::Z3950::ZOOM::package_option_get($p, "targetReference");
121 $val = Net::Z3950::ZOOM::package_option_get($p, "xmlUpdateDoc");
122 # ... and we know nothing about expected or actual values.
124 Net::Z3950::ZOOM::package_destroy($p);
125 ok(1, "destroyed createdb package");
130 my($conn, $dbname, $expected_error) = @_;
132 my $o = Net::Z3950::ZOOM::options_create();
133 my $p = Net::Z3950::ZOOM::connection_package($conn, $o);
134 # No need to keep ok()ing this, or checking the option-setting
135 Net::Z3950::ZOOM::package_option_set($p, databaseName => $dbname);
137 Net::Z3950::ZOOM::package_send($p, "drop");
138 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
139 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
140 ok($errcode == $expected_error,
141 "database drop '$dbname'" . ($errcode ? " refused $errcode" : ""));
143 Net::Z3950::ZOOM::package_destroy($p);
144 ok(1, "destroyed dropdb package");
148 # We always use "specialUpdate", which adds a record or replaces it if
149 # it's already there. By contrast, "insert" fails if the record
150 # already exists, and "replace" fails if it does not.
153 my($conn, $id, $file, $expected_error) = @_;
155 my $o = Net::Z3950::ZOOM::options_create();
156 my $p = Net::Z3950::ZOOM::connection_package($conn, $o);
157 Net::Z3950::ZOOM::package_option_set($p, action => "specialUpdate");
158 Net::Z3950::ZOOM::package_option_set($p, recordIdOpaque => $id);
159 Net::Z3950::ZOOM::package_option_set($p, record => $file);
161 Net::Z3950::ZOOM::package_send($p, "update");
162 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
163 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
164 ok($errcode == $expected_error, "record update $id" .
165 ($errcode ? " failed $errcode '$errmsg' ($addinfo)" : ""));
167 Net::Z3950::ZOOM::package_destroy($p);
168 ok(1, "destroyed update package");
173 my($conn, $query, $expected_error, $expected_count) = @_;
175 my $rs = Net::Z3950::ZOOM::connection_search_pqf($conn, $query);
176 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
177 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
178 ok($errcode == $expected_error, "database '$dbname' " .
179 ($errcode == 0 ? "can be searched" : "not searchable ($errcode)"));
180 return if $errcode != 0;
181 my $n = Net::Z3950::ZOOM::resultset_size($rs);
182 ok($n == $expected_count,
183 "database '$dbname' has $n records (expected $expected_count)");
191 my $f = new IO::File("<$filename")
192 or die "can't open file '$filename': $!";
193 my $text = join("", <$f>);