1 # $Id: 16-packages.t,v 1.14 2008-09-29 15:49:13 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 => 39;
19 BEGIN { use_ok('Net::Z3950::ZOOM') };
22 # We will create, and destroy, a new database with a random name
23 my $host = "z3950.indexdata.com:2100";
24 my $dbname = join("", map { chr(ord("a") + int(rand(26))) } 1..10);
26 # Connect anonymously, and expect this to fail
27 my $conn = makeconn($host, undef, undef, 1011);
29 # Connect as a user, but with incorrect password -- expect failure
30 Net::Z3950::ZOOM::connection_destroy($conn);
31 $conn = makeconn($host, "user", "badpw", 1011);
33 # Connect as a non-privileged user with correct password
34 Net::Z3950::ZOOM::connection_destroy($conn);
35 $conn = makeconn($host, "user", "frog", 0);
37 # Non-privileged user can't create database
38 makedb($conn, $dbname, 223);
40 # Connect as a privileged user with correct password, check DB is absent
41 Net::Z3950::ZOOM::connection_destroy($conn);
42 $conn = makeconn($host, "admin", "fish", 0);
43 Net::Z3950::ZOOM::connection_option_set($conn, databaseName => $dbname);
44 count_hits($conn, $dbname, "the", 109);
46 # Now create the database and check that it is present but empty
47 makedb($conn, $dbname, 0);
48 count_hits($conn, $dbname, "the", 114);
50 # Trying to create the same database again will fail EEXIST
51 makedb($conn, $dbname, 224);
53 # Add a single record, and check that it can be found
54 updaterec($conn, 1, content_of("samples/records/esdd0006.grs"), 0);
55 count_hits($conn, $dbname, "the", 0, 1);
57 # Add the same record with the same ID: overwrite => no change
58 updaterec($conn, 1, content_of("samples/records/esdd0006.grs"), 0);
59 count_hits($conn, $dbname, "the", 0, 1);
61 # Add it again record with different ID => new copy added
62 updaterec($conn, 2, content_of("samples/records/esdd0006.grs"), 0);
63 count_hits($conn, $dbname, "the", 0, 2);
65 # Now drop the newly-created database
66 dropdb($conn, $dbname, 0);
68 # A second dropping should fail, as the database is no longer there.
69 dropdb($conn, $dbname, 235);
73 my($host, $user, $password, $expected_error) = @_;
75 my $options = Net::Z3950::ZOOM::options_create();
76 Net::Z3950::ZOOM::options_set($options, user => $user)
78 Net::Z3950::ZOOM::options_set($options, password => $password)
81 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
82 my $conn = Net::Z3950::ZOOM::connection_create($options);
83 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
84 ok($errcode == 0, "unconnected connection object created");
86 Net::Z3950::ZOOM::connection_connect($conn, $host, 0);
87 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
88 ok($errcode == $expected_error,
89 "connection to '$host'" . ($errcode ? " refused ($errcode)" : ""));
96 my($conn, $dbname, $expected_error) = @_;
98 my $o = Net::Z3950::ZOOM::options_create();
99 my $p = Net::Z3950::ZOOM::connection_package($conn, $o);
100 # Inspection of the ZOOM-C code shows that this can never fail, in fact.
101 ok(defined $p, "created package");
103 Net::Z3950::ZOOM::package_option_set($p, databaseName => $dbname);
104 my $val = Net::Z3950::ZOOM::package_option_get($p, "databaseName");
105 ok($val eq $dbname, "package option retrieved as expected");
107 Net::Z3950::ZOOM::package_send($p, "create");
108 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
109 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
110 ok($errcode == $expected_error, "database creation '$dbname'" .
111 ($errcode ? " refused ($errcode)" : ""));
113 # Now we can inspect the package options to find out more about
114 # how the server dealt with the request. However, it seems that
115 # the "package database" described in the standard is not used,
116 # and that the only options we can inspect are the following:
117 $val = Net::Z3950::ZOOM::package_option_get($p, "targetReference");
118 $val = Net::Z3950::ZOOM::package_option_get($p, "xmlUpdateDoc");
119 # ... and we know nothing about expected or actual values.
121 Net::Z3950::ZOOM::package_destroy($p);
122 ok(1, "destroyed createdb package");
127 my($conn, $dbname, $expected_error) = @_;
129 my $o = Net::Z3950::ZOOM::options_create();
130 my $p = Net::Z3950::ZOOM::connection_package($conn, $o);
131 # No need to keep ok()ing this, or checking the option-setting
132 Net::Z3950::ZOOM::package_option_set($p, databaseName => $dbname);
133 Net::Z3950::ZOOM::package_send($p, "drop");
134 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
135 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
136 ok($errcode == $expected_error,
137 ("database drop '$dbname'" . ($errcode ? " refused $errcode" : "") .
138 ($expected_error ? " expected $expected_error but succeeded" : "")));
140 Net::Z3950::ZOOM::package_destroy($p);
141 ok(1, "destroyed dropdb package");
145 # We always use "specialUpdate", which adds a record or replaces it if
146 # it's already there. By contrast, "insert" fails if the record
147 # already exists, and "replace" fails if it does not.
150 my($conn, $id, $file, $expected_error) = @_;
152 my $o = Net::Z3950::ZOOM::options_create();
153 my $p = Net::Z3950::ZOOM::connection_package($conn, $o);
154 Net::Z3950::ZOOM::package_option_set($p, action => "specialUpdate");
155 Net::Z3950::ZOOM::package_option_set($p, recordIdOpaque => $id);
156 Net::Z3950::ZOOM::package_option_set($p, record => $file);
158 Net::Z3950::ZOOM::package_send($p, "update");
159 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
160 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
161 ok($errcode == $expected_error, "record update $id" .
162 ($errcode ? " failed $errcode '$errmsg' ($addinfo)" : ""));
164 Net::Z3950::ZOOM::package_destroy($p);
165 ok(1, "destroyed update package");
170 my($conn, $dbname, $query, $expected_error, $expected_count) = @_;
172 my $rs = Net::Z3950::ZOOM::connection_search_pqf($conn, $query);
173 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
174 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
175 ok($errcode == $expected_error, "database '$dbname' " .
176 ($errcode == 0 ? "can be searched" : "not searchable ($errcode)"));
177 return if $errcode != 0;
178 my $n = Net::Z3950::ZOOM::resultset_size($rs);
179 ok($n == $expected_count,
180 "database '$dbname' has $n records (expected $expected_count)");
188 my $f = new IO::File("<$filename")
189 or die "can't open file '$filename': $!";
190 my $text = join("", <$f>);