1 # $Id: 16-packages.t,v 1.6 2005-12-07 17:46:00 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 ### For some reason, this does not work
65 updaterec($conn, 2, content_of("samples/records/esdd0006.grs"), 0);
66 count_hits($conn, "the", 0, 2);
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, "database creation '$dbname'" .
115 ($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, "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>);