1 # Before `make install' is performed this script should be runnable with
2 # `make test'. After `make install' it should work as `perl 16-packages.t'
5 # connect anonymously => refused
6 # connect as "user" with incorrect password => refused
7 # connect as "user" with correct password
8 # try to create tmpdb => EPERM
9 # connect as admin with correct password
10 # try to create tmpdb => OK
11 # try to create tmpdb => EFAIL
15 use Test::More tests => 39;
17 BEGIN { use_ok('Net::Z3950::ZOOM') };
20 # We will create, and destroy, a new database with a random name
21 my $host = "z3950.indexdata.com:2100";
22 my $dbname = join("", map { chr(ord("a") + int(rand(26))) } 1..10);
24 # Connect anonymously, and expect this to fail
25 my $conn = makeconn($host, undef, undef, 1011);
27 # Connect as a user, but with incorrect password -- expect failure
28 Net::Z3950::ZOOM::connection_destroy($conn);
29 $conn = makeconn($host, "user", "badpw", 1011);
31 # Connect as a non-privileged user with correct password
32 Net::Z3950::ZOOM::connection_destroy($conn);
33 $conn = makeconn($host, "user", "frog", 0);
35 # Non-privileged user can't create database
36 makedb($conn, $dbname, 223);
38 # Connect as a privileged user with correct password, check DB is absent
39 Net::Z3950::ZOOM::connection_destroy($conn);
40 $conn = makeconn($host, "admin", "fish", 0);
41 Net::Z3950::ZOOM::connection_option_set($conn, databaseName => $dbname);
42 count_hits($conn, $dbname, "the", 109);
44 # Now create the database and check that it is present but empty
45 makedb($conn, $dbname, 0);
46 count_hits($conn, $dbname, "the", 114);
48 # Trying to create the same database again will fail EEXIST
49 makedb($conn, $dbname, 224);
51 # Add a single record, and check that it can be found
52 updaterec($conn, 1, content_of("samples/records/esdd0006.grs"), 0);
53 count_hits($conn, $dbname, "the", 0, 1);
55 # Add the same record with the same ID: overwrite => no change
56 updaterec($conn, 1, content_of("samples/records/esdd0006.grs"), 0);
57 count_hits($conn, $dbname, "the", 0, 1);
59 # Add it again record with different ID => new copy added
60 updaterec($conn, 2, content_of("samples/records/esdd0006.grs"), 0);
61 count_hits($conn, $dbname, "the", 0, 2);
63 # Now drop the newly-created database
64 dropdb($conn, $dbname, 0);
66 # A second dropping should fail, as the database is no longer there.
67 dropdb($conn, $dbname, 235);
71 my($host, $user, $password, $expected_error) = @_;
73 my $options = Net::Z3950::ZOOM::options_create();
74 Net::Z3950::ZOOM::options_set($options, user => $user)
76 Net::Z3950::ZOOM::options_set($options, password => $password)
79 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
80 my $conn = Net::Z3950::ZOOM::connection_create($options);
81 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
82 ok($errcode == 0, "unconnected connection object created");
84 Net::Z3950::ZOOM::connection_connect($conn, $host, 0);
85 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
86 ok($errcode == $expected_error,
87 "connection to '$host'" . ($errcode ? " refused ($errcode)" : ""));
94 my($conn, $dbname, $expected_error) = @_;
96 my $o = Net::Z3950::ZOOM::options_create();
97 my $p = Net::Z3950::ZOOM::connection_package($conn, $o);
98 # Inspection of the ZOOM-C code shows that this can never fail, in fact.
99 ok(defined $p, "created package");
101 Net::Z3950::ZOOM::package_option_set($p, databaseName => $dbname);
102 my $val = Net::Z3950::ZOOM::package_option_get($p, "databaseName");
103 ok($val eq $dbname, "package option retrieved as expected");
105 Net::Z3950::ZOOM::package_send($p, "create");
106 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
107 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
108 ok($errcode == $expected_error, "database creation '$dbname'" .
109 ($errcode ? " refused ($errcode)" : ""));
111 # Now we can inspect the package options to find out more about
112 # how the server dealt with the request. However, it seems that
113 # the "package database" described in the standard is not used,
114 # and that the only options we can inspect are the following:
115 $val = Net::Z3950::ZOOM::package_option_get($p, "targetReference");
116 $val = Net::Z3950::ZOOM::package_option_get($p, "xmlUpdateDoc");
117 # ... and we know nothing about expected or actual values.
119 Net::Z3950::ZOOM::package_destroy($p);
120 ok(1, "destroyed createdb package");
125 my($conn, $dbname, $expected_error) = @_;
127 my $o = Net::Z3950::ZOOM::options_create();
128 my $p = Net::Z3950::ZOOM::connection_package($conn, $o);
129 # No need to keep ok()ing this, or checking the option-setting
130 Net::Z3950::ZOOM::package_option_set($p, databaseName => $dbname);
131 Net::Z3950::ZOOM::package_send($p, "drop");
132 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
133 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
134 ok($errcode == $expected_error,
135 ("database drop '$dbname'" . ($errcode ? " refused $errcode" : "") .
136 ($expected_error ? " expected $expected_error but succeeded" : "")));
138 Net::Z3950::ZOOM::package_destroy($p);
139 ok(1, "destroyed dropdb package");
143 # We always use "specialUpdate", which adds a record or replaces it if
144 # it's already there. By contrast, "insert" fails if the record
145 # already exists, and "replace" fails if it does not.
148 my($conn, $id, $file, $expected_error) = @_;
150 my $o = Net::Z3950::ZOOM::options_create();
151 my $p = Net::Z3950::ZOOM::connection_package($conn, $o);
152 Net::Z3950::ZOOM::package_option_set($p, action => "specialUpdate");
153 Net::Z3950::ZOOM::package_option_set($p, recordIdOpaque => $id);
154 Net::Z3950::ZOOM::package_option_set($p, record => $file);
156 Net::Z3950::ZOOM::package_send($p, "update");
157 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
158 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
159 ok($errcode == $expected_error, "record update $id" .
160 ($errcode ? " failed $errcode '$errmsg' ($addinfo)" : ""));
162 Net::Z3950::ZOOM::package_destroy($p);
163 ok(1, "destroyed update package");
168 my($conn, $dbname, $query, $expected_error, $expected_count) = @_;
170 my $rs = Net::Z3950::ZOOM::connection_search_pqf($conn, $query);
171 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
172 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
173 ok($errcode == $expected_error, "database '$dbname' " .
174 ($errcode == 0 ? "can be searched" : "not searchable ($errcode)"));
175 return if $errcode != 0;
176 my $n = Net::Z3950::ZOOM::resultset_size($rs);
177 ok($n == $expected_count,
178 "database '$dbname' has $n records (expected $expected_count)");
186 my $f = new IO::File("<$filename")
187 or die "can't open file '$filename': $!";
188 my $text = join("", <$f>);