1 # $Id: 16-packages.t,v 1.10 2005-12-14 09:30:58 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 # We will create, and destroy, a new database with a random name
23 my $host = "test.indexdata.com:2118";
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, "the", 109);
46 # Now create the database and check that it is present but empty
47 makedb($conn, $dbname, 0);
48 count_hits($conn, "the", 0, 0);
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, "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, "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, "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 ### But at present, it's "always successful" (though not really)
70 dropdb($conn, $dbname, 0);
74 my($host, $user, $password, $expected_error) = @_;
76 my $options = Net::Z3950::ZOOM::options_create();
77 Net::Z3950::ZOOM::options_set($options, user => $user)
79 Net::Z3950::ZOOM::options_set($options, password => $password)
82 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
83 my $conn = Net::Z3950::ZOOM::connection_create($options);
84 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
85 ok($errcode == 0, "unconnected connection object created");
87 Net::Z3950::ZOOM::connection_connect($conn, $host, 0);
88 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
89 ok($errcode == $expected_error,
90 "connection to '$host'" . ($errcode ? " refused ($errcode)" : ""));
97 my($conn, $dbname, $expected_error) = @_;
99 my $o = Net::Z3950::ZOOM::options_create();
100 my $p = Net::Z3950::ZOOM::connection_package($conn, $o);
101 # Inspection of the ZOOM-C code shows that this can never fail, in fact.
102 ok(defined $p, "created package");
104 Net::Z3950::ZOOM::package_option_set($p, databaseName => $dbname);
105 my $val = Net::Z3950::ZOOM::package_option_get($p, "databaseName");
106 ok($val eq $dbname, "package option retrieved as expected");
108 Net::Z3950::ZOOM::package_send($p, "create");
109 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
110 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
111 ok($errcode == $expected_error, "database creation '$dbname'" .
112 ($errcode ? " refused ($errcode)" : ""));
114 # Now we can inspect the package options to find out more about
115 # how the server dealt with the request. However, it seems that
116 # the "package database" described in the standard is not used,
117 # and that the only options we can inspect are the following:
118 $val = Net::Z3950::ZOOM::package_option_get($p, "targetReference");
119 $val = Net::Z3950::ZOOM::package_option_get($p, "xmlUpdateDoc");
120 # ... and we know nothing about expected or actual values.
122 Net::Z3950::ZOOM::package_destroy($p);
123 ok(1, "destroyed createdb package");
128 my($conn, $dbname, $expected_error) = @_;
130 my $o = Net::Z3950::ZOOM::options_create();
131 my $p = Net::Z3950::ZOOM::connection_package($conn, $o);
132 # No need to keep ok()ing this, or checking the option-setting
133 Net::Z3950::ZOOM::package_option_set($p, databaseName => $dbname);
135 ### Don't send the package at the moment -- it corrupts Zebra
136 #Net::Z3950::ZOOM::package_send($p, "drop");
137 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
138 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
139 ok($errcode == $expected_error,
140 "database drop '$dbname'" . ($errcode ? " refused $errcode" : ""));
142 Net::Z3950::ZOOM::package_destroy($p);
143 ok(1, "destroyed dropdb package");
147 # We always use "specialUpdate", which adds a record or replaces it if
148 # it's already there. By contrast, "insert" fails if the record
149 # already exists, and "replace" fails if it does not.
152 my($conn, $id, $file, $expected_error) = @_;
154 my $o = Net::Z3950::ZOOM::options_create();
155 my $p = Net::Z3950::ZOOM::connection_package($conn, $o);
156 Net::Z3950::ZOOM::package_option_set($p, action => "specialUpdate");
157 Net::Z3950::ZOOM::package_option_set($p, recordIdOpaque => $id);
158 Net::Z3950::ZOOM::package_option_set($p, record => $file);
160 Net::Z3950::ZOOM::package_send($p, "update");
161 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
162 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
163 ok($errcode == $expected_error, "record update $id" .
164 ($errcode ? " failed $errcode '$errmsg' ($addinfo)" : ""));
166 Net::Z3950::ZOOM::package_destroy($p);
167 ok(1, "destroyed update package");
172 my($conn, $query, $expected_error, $expected_count) = @_;
174 my $rs = Net::Z3950::ZOOM::connection_search_pqf($conn, $query);
175 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
176 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
177 ok($errcode == $expected_error, "database '$dbname' " .
178 ($errcode == 0 ? "can be searched" : "not searchable ($errcode)"));
179 return if $errcode != 0;
180 my $n = Net::Z3950::ZOOM::resultset_size($rs);
181 ok($n == $expected_count,
182 "database '$dbname' has $n records (expected $expected_count)");
190 my $f = new IO::File("<$filename")
191 or die "can't open file '$filename': $!";
192 my $text = join("", <$f>);