1 # $Id: 16-packages.t,v 1.8 2005-12-13 13:21:49 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, as the database is no longer there.
71 ### But at present, it's "always successful" (though not really)
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 ### Don't send the package at the moment -- it corrupts Zebra
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, "record update $id" .
166 ($errcode ? " failed $errcode '$errmsg' ($addinfo)" : ""));
168 Net::Z3950::ZOOM::package_destroy($p);
169 ok(1, "destroyed update package");
174 my($conn, $query, $expected_error, $expected_count) = @_;
176 my $rs = Net::Z3950::ZOOM::connection_search_pqf($conn, $query);
177 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
178 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
179 ok($errcode == $expected_error, "database '$dbname' " .
180 ($errcode == 0 ? "can be searched" : "not searchable ($errcode)"));
181 return if $errcode != 0;
182 my $n = Net::Z3950::ZOOM::resultset_size($rs);
183 ok($n == $expected_count,
184 "database '$dbname' has $n records (expected $expected_count)");
192 my $f = new IO::File("<$filename")
193 or die "can't open file '$filename': $!";
194 my $text = join("", <$f>);