Run against special zebra on test:8017
[ZOOM-Perl-moved-to-github.git] / t / 16-packages.t
1 # $Id: 16-packages.t,v 1.9 2005-12-13 17:35:51 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:8017";
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 ### But at present, it's "always successful" (though not really)
70 dropdb($conn, $dbname, 0);
71
72
73 sub makeconn {
74     my($host, $user, $password, $expected_error) = @_;
75
76     my $options = Net::Z3950::ZOOM::options_create();
77     Net::Z3950::ZOOM::options_set($options, user => $user)
78         if defined $user;
79     Net::Z3950::ZOOM::options_set($options, password => $password)
80         if defined $password;
81
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");
86
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)" : ""));
91
92     return $conn;
93 }
94
95
96 sub makedb {
97     my($conn, $dbname, $expected_error) = @_;
98
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");
103
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");
107
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)" : ""));
113
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.
121
122     Net::Z3950::ZOOM::package_destroy($p);
123     ok(1, "destroyed createdb package");
124 }
125
126
127 sub dropdb {
128     my($conn, $dbname, $expected_error) = @_;
129
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);
134
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" : ""));
141
142     Net::Z3950::ZOOM::package_destroy($p);
143     ok(1, "destroyed dropdb package");
144 }
145
146
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.
150 #
151 sub updaterec {
152     my($conn, $id, $file, $expected_error) = @_;
153
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);
159
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)" : ""));
165
166     Net::Z3950::ZOOM::package_destroy($p);
167     ok(1, "destroyed update package");
168 }
169
170
171 sub count_hits {
172     my($conn, $query, $expected_error, $expected_count) = @_;
173
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)");
183 }
184
185
186 sub content_of {
187     my($filename) = @_;
188
189     use IO::File;
190     my $f = new IO::File("<$filename")
191         or die "can't open file '$filename': $!";
192     my $text = join("", <$f>);
193     $f->close();
194
195     return $text;
196 }