Merge branch 'master' of ssh://git.indexdata.com:222/home/git/pub/irspy
[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", "UTF-8");
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     my $proxy = $xc->find("e:serverInfo/e:authentication/e:proxy");
28     $options{"*user"} = $user if $user;
29     $options{"*password"} = $password if $password;
30     $options{"*proxy"} = $proxy if $proxy;
31
32     $conn->irspy_connect(undef, \%options,
33                          ZOOM::Event::ZEND, \&connected,
34                          exception => \&not_connected);
35 }
36
37
38 sub connected {
39     my($conn, $__UNUSED_task, $__UNUSED_udata, $__UNUSED_event) = @_;
40
41     $conn->log("irspy_test", "connected");
42     $conn->record()->store_result("probe", ok => 1);
43
44     foreach my $opt (qw(search present delSet resourceReport
45                         triggerResourceCtrl resourceCtrl
46                         accessCtrl scan sort extendedServices
47                         level_1Segmentation level_2Segmentation
48                         concurrentOperations namedResultSets
49                         encapsulation resultCount negotiationModel
50                         duplicationDetection queryType104
51                         pQESCorrection stringSchema)) {
52         #print STDERR "\$conn->option('init_opt_$opt') = '", $conn->option("init_opt_$opt"), "'\n";
53         $conn->record()->store_result('init_opt', option => $opt)
54             if $conn->option("init_opt_$opt");
55     }
56
57     my %params = (serverImplementationId => "id",
58                   serverImplementationName => "name",
59                   serverImplementationVersion => "version",
60                  );
61     foreach my $opt (keys %params) {
62         my $val = $conn->option($opt);
63         next if !defined $val; # not defined for SRU, for example
64
65         # There doesn't seem to be a reliable way to tell what
66         # character set the server uses for these.  At least one
67         # server (z3950.bcl.jcyl.es:210/AbsysCCFL) returns an ISO
68         # 8859-1 string containing an o-acute, which breaks the XML
69         # parser if we just insert it naively.  It seems reasonable,
70         # though, to guess that the great majority of servers will use
71         # ASCII, Latin-1 or Unicode.  The first of these is a subset
72         # of the second, so that brings it to down to two.  The
73         # strategy is simply this: assume it's ASCII-Latin-1, and try
74         # to convert to UTF-8.  If that conversion works, fine; if
75         # not, assume it's because the string was already UTF-8, so
76         # use it as is.
77         Text::Iconv->raise_error(1);
78         my $maybe;
79         eval {
80             $maybe = $conv->convert($val);
81         }; if (!$@ && $maybe ne $val) {
82             $conn->log("irspy", "converted '$val' from Latin-1 to UTF-8");
83             $val = $maybe;
84         }
85         $conn->record()->store_result($opt, value => $val);
86         $conn->irspy()->var($params{$opt}, $val);
87     }
88
89     return ZOOM::IRSpy::Status::TEST_GOOD;
90 }
91
92
93 sub not_connected {
94     my($conn, $__UNUSED_task, $__UNUSED_udata, $exception) = @_;
95
96     $conn->log("irspy", "not connected: $exception");
97     $conn->record()->store_result("probe",
98                                   ok => 0,
99                                   errcode => $exception->code(),
100                                   errmsg => $exception->message(),
101                                   addinfo => $exception->addinfo(),
102                                   diagset => $exception->diagset());
103
104     return ZOOM::IRSpy::Status::TEST_BAD;
105 }
106
107
108 1;