31b7f4d30296d8f3e9c58e7b5e95643bc0927598
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Test / Ping.pm
1
2 # See the "Main" test package for documentation
3
4 package ZOOM::IRSpy::Test::Ping;
5
6 use 5.008;
7 use strict;
8 use warnings;
9
10 use ZOOM::IRSpy::Test;
11 our @ISA = qw(ZOOM::IRSpy::Test);
12
13 use ZOOM::IRSpy::Utils qw(isodate);
14
15 use Text::Iconv;
16 my $conv = new Text::Iconv("LATIN1", "UTF8");
17
18
19 sub start {
20     my $class = shift();
21     my($conn) = @_;
22
23     my %options = ();
24     my $xc = $conn->record()->xpath_context();
25     my $user = $xc->find("e:serverInfo/e:authentication/e:user");
26     my $password = $xc->find("e:serverInfo/e:authentication/e:password");
27     $options{"*user"} = $user if $user;
28     $options{"*password"} = $password if $password;
29
30     $conn->irspy_connect(undef, \%options,
31                          ZOOM::Event::ZEND, \&connected,
32                          exception => \&not_connected);
33 }
34
35
36 sub connected {
37     my($conn, $__UNUSED_task, $__UNUSED_udata, $__UNUSED_event) = @_;
38
39     $conn->log("irspy_test", "connected");
40     $conn->record()->store_result("probe", ok => 1);
41
42     foreach my $opt (qw(search present delSet resourceReport
43                         triggerResourceCtrl resourceCtrl
44                         accessCtrl scan sort extendedServices
45                         level_1Segmentation level_2Segmentation
46                         concurrentOperations namedResultSets
47                         encapsulation resultCount negotiationModel
48                         duplicationDetection queryType104
49                         pQESCorrection stringSchema)) {
50         #print STDERR "\$conn->option('init_opt_$opt') = '", $conn->option("init_opt_$opt"), "'\n";
51         $conn->record()->store_result('init_opt', option => $opt)
52             if $conn->option("init_opt_$opt");
53     }
54
55     foreach my $opt (qw(serverImplementationId
56                         serverImplementationName
57                         serverImplementationVersion)) {
58         my $val = $conn->option($opt);
59         next if !defined $val; # not defined for SRU, for example
60
61         # There doesn't seem to be a reliable way to tell what
62         # character set the server uses for these.  At least one
63         # server (z3950.bcl.jcyl.es:210/AbsysCCFL) returns an ISO
64         # 8859-1 string containing an o-acute, which breaks the XML
65         # parser if we just insert it naively.  It seems reasonable,
66         # though, to guess that the great majority of servers will use
67         # ASCII, Latin-1 or Unicode.  The first of these is a subset
68         # of the second, so that brings it to down to two.  The
69         # strategy is simply this: assume it's ASCII-Latin-1, and try
70         # to convert to UTF-8.  If that conversion works, fine; if
71         # not, assume it's because the string was already UTF-8, so
72         # use it as is.
73         Text::Iconv->raise_error(1);
74         my $maybe;
75         eval {
76             $maybe = $conv->convert($val);
77         }; if (!$@ && $maybe ne $val) {
78             $conn->log("irspy", "converted '$val' from Latin-1 to UTF-8");
79             $val = $maybe;
80         }
81         $conn->record()->store_result($opt, value => $val);
82     }
83
84     return ZOOM::IRSpy::Status::TEST_GOOD;
85 }
86
87
88 sub not_connected {
89     my($conn, $__UNUSED_task, $__UNUSED_udata, $exception) = @_;
90
91     $conn->log("irspy", "not connected: $exception");
92     $conn->record()->store_result("probe",
93                                   ok => 0,
94                                   errcode => $exception->code(),
95                                   errmsg => $exception->message(),
96                                   addinfo => $exception->addinfo(),
97                                   diagset => $exception->diagset());
98
99     return ZOOM::IRSpy::Status::TEST_BAD;
100 }
101
102
103 1;