6c09bcff5ea2191a8272e988edf4013a61474510
[ZOOM-Perl-moved-to-github.git] / t / 16-packages.t
1 # $Id: 16-packages.t,v 1.12 2006-04-12 12:30:09 mike Exp $
2
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'
5
6 # Tests:
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
14
15 use strict;
16 use warnings;
17 use Test::More tests => 40;
18
19 BEGIN { use_ok('Net::Z3950::ZOOM') };
20
21
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);
25
26 # Connect anonymously, and expect this to fail
27 my $conn = makeconn($host, undef, undef, 1011);
28
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);
32
33 # Connect as a non-privileged user with correct password
34 Net::Z3950::ZOOM::connection_destroy($conn);
35 $conn = makeconn($host, "user", "frog", 0);
36
37 # Non-privileged user can't create database
38 makedb($conn, $dbname, 223);
39
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);
45
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);
49
50 # Trying to create the same database again will fail EEXIST
51 makedb($conn, $dbname, 224);
52
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);
56
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);
60
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);
64
65 # Now drop the newly-created database
66 dropdb($conn, $dbname, 0);
67
68 # A second dropping should fail, as the database is no longer there.
69 dropdb($conn, $dbname, 10004);
70
71
72 sub makeconn {
73     my($host, $user, $password, $expected_error) = @_;
74
75     my $options = Net::Z3950::ZOOM::options_create();
76     Net::Z3950::ZOOM::options_set($options, user => $user)
77         if defined $user;
78     Net::Z3950::ZOOM::options_set($options, password => $password)
79         if defined $password;
80
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");
85
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)" : ""));
90
91     return $conn;
92 }
93
94
95 sub makedb {
96     my($conn, $dbname, $expected_error) = @_;
97
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");
102
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");
106
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)" : ""));
112
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.
120
121     Net::Z3950::ZOOM::package_destroy($p);
122     ok(1, "destroyed createdb package");
123 }
124
125
126 sub dropdb {
127     my($conn, $dbname, $expected_error) = @_;
128
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
139     Net::Z3950::ZOOM::package_destroy($p);
140     ok(1, "destroyed dropdb package");
141 }
142
143
144 # We always use "specialUpdate", which adds a record or replaces it if
145 # it's already there.  By contrast, "insert" fails if the record
146 # already exists, and "replace" fails if it does not.
147 #
148 sub updaterec {
149     my($conn, $id, $file, $expected_error) = @_;
150
151     my $o = Net::Z3950::ZOOM::options_create();
152     my $p = Net::Z3950::ZOOM::connection_package($conn, $o);
153     Net::Z3950::ZOOM::package_option_set($p, action => "specialUpdate");
154     Net::Z3950::ZOOM::package_option_set($p, recordIdOpaque => $id);
155     Net::Z3950::ZOOM::package_option_set($p, record => $file);
156
157     Net::Z3950::ZOOM::package_send($p, "update");
158     my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
159     $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
160     ok($errcode == $expected_error, "record update $id" .
161        ($errcode ? " failed $errcode '$errmsg' ($addinfo)" : ""));
162
163     Net::Z3950::ZOOM::package_destroy($p);
164     ok(1, "destroyed update package");
165 }
166
167
168 sub count_hits {
169     my($conn, $query, $expected_error, $expected_count) = @_;
170
171     my $rs = Net::Z3950::ZOOM::connection_search_pqf($conn, $query);
172     my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
173     $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
174     ok($errcode == $expected_error, "database '$dbname' " .
175        ($errcode == 0 ? "can be searched" : "not searchable ($errcode)"));
176     return if $errcode != 0;
177     my $n = Net::Z3950::ZOOM::resultset_size($rs);
178     ok($n == $expected_count,
179        "database '$dbname' has $n records (expected $expected_count)");
180 }
181
182
183 sub content_of {
184     my($filename) = @_;
185
186     use IO::File;
187     my $f = new IO::File("<$filename")
188         or die "can't open file '$filename': $!";
189     my $text = join("", <$f>);
190     $f->close();
191
192     return $text;
193 }