adding openncip / opensip SIP2 service
[koha.git] / C4 / SIP / t / SIPtest.pm
1 package SIPtest;
2
3 use strict;
4 use warnings;
5
6 use Exporter;
7
8 our @ISA = qw(Exporter);
9
10 our @EXPORT_OK = qw(run_sip_tests no_tagged_fields
11                     $datepat $textpat
12                     $login_test $sc_status_test
13                     %field_specs
14
15                     $instid $currency $server $username $password
16                     $user_barcode $user_pin $user_fullname $user_homeaddr
17                     $user_email $user_phone $user_birthday $user_ptype
18                     $user_inet
19                     $item_barcode $item_title $item_owner
20                     $item2_barcode $item2_title $item2_owner
21                     $item_diacritic_barcode $item_diacritic_title
22                     $item_diacritic_owner);
23 #use Data::Dumper;
24
25 # The number of tests is set in run_sip_tests() below, based
26 # on the size of the array of tests.
27 use Test::More;
28
29 use IO::Socket::INET;
30 use Sip qw(:all);
31 use Sip::Checksum qw(verify_cksum);
32 use Sip::Constants qw(:all);
33
34
35 # Configuration parameters to run the test suite
36 #
37 our $instid = 'UWOLS';
38 our $currency = 'CAD';
39 our $server   = 'localhost:6001'; # Address of the SIP server
40
41 # SIP username and password to connect to the server.  See the
42 # SIP config.xml for the correct values.
43 our $username = 'scclient';
44 our $password = 'clientpwd';
45
46 # ILS Information
47
48 # Valid user barcode and corresponding user password/pin and full name
49 our $user_barcode = 'djfiander';
50 our $user_pin     = '6789';
51 our $user_fullname= 'David J\. Fiander';
52 our $user_homeaddr= '2 Meadowvale Dr\. St Thomas, ON';
53 our $user_email   = 'djfiander\@hotmail\.com';
54 our $user_phone   = '\(519\) 555 1234';
55 our $user_birthday= '19640925';
56 our $user_ptype   = 'A';
57 our $user_inet    = 'Y';
58
59 # Valid item barcode and corresponding title
60 our $item_barcode = '1565921879';
61 our $item_title   = 'Perl 5 desktop reference';
62 our $item_owner   = 'UWOLS';
63
64 # Another valid item
65 our $item2_barcode = '0440242746';
66 our $item2_title   = 'The deep blue alibi';
67 our $item2_owner   = 'UWOLS';
68
69 # An item with a diacritical in the title
70 our $item_diacritic_barcode = '660';
71 our $item_diacritic_title = 'Harry Potter y el cáliz de fuego';
72 our $item_diacritic_owner = 'UWOLS';
73
74 # End configuration
75
76 # Pattern for a SIP datestamp, to be used by individual tests to
77 # match timestamp fields (duh).
78 our $datepat = '\d{8} {4}\d{6}';
79
80 # Pattern for a random text field (may be empty)
81 our $textpat = qr/^[^|]*$/;
82
83 our %field_specs = (
84                     (FID_SCREEN_MSG) => { field    => FID_SCREEN_MSG,
85                                           pat      => $textpat,
86                                           required => 0, },
87                     (FID_PRINT_LINE) => { field    => FID_PRINT_LINE,
88                                           pat      => $textpat,
89                                           required => 0, },
90                     (FID_INST_ID)    => { field    => FID_INST_ID,
91                                           pat      => qr/^$instid$/o,
92                                           required => 1, },
93                     (FID_HOLD_ITEMS_LMT)=> { field    => FID_HOLD_ITEMS_LMT,
94                                              pat      => qr/^\d{4}$/,
95                                              required => 0, },
96                     (FID_OVERDUE_ITEMS_LMT)=> { field    => FID_OVERDUE_ITEMS_LMT,
97                                                 pat      => qr/^\d{4}$/,
98                                                 required => 0, },
99                     (FID_CHARGED_ITEMS_LMT)=> { field    => FID_CHARGED_ITEMS_LMT,
100                                                 pat      => qr/^\d{4}$/,
101                                                 required => 0, },
102                     (FID_VALID_PATRON) => { field    => FID_VALID_PATRON,
103                                             pat      => qr/^[NY]$/,
104                                             required => 0, },
105                     (FID_VALID_PATRON_PWD)=> { field    => FID_VALID_PATRON_PWD,
106                                                pat      => qr/^[NY]$/,
107                                                required => 0, },
108                     (FID_CURRENCY)   => { field    => FID_CURRENCY,
109                                           pat      => qr/^$currency$/io,
110                                           required => 0, },
111                     );
112
113 # Login and SC Status are always the first two messages that
114 # the terminal sends to the server, so just create the test
115 # cases here and reference them in the individual test files.
116
117 our $login_test = { id => 'login',
118                     msg => "9300CN$username|CO$password|CPThe floor|",
119                     pat => qr/^941/,
120                     fields => [], };
121
122 our $sc_status_test = { id => 'SC status',
123                         msg => '9910302.00',
124                         pat => qr/^98[YN]{6}\d{3}\d{3}$datepat(2\.00|1\.00)/,
125                         fields => [
126                                    $field_specs{(FID_SCREEN_MSG)},
127                                    $field_specs{(FID_PRINT_LINE)},
128                                    $field_specs{(FID_INST_ID)},
129                                    { field    => 'AM',
130                                      pat      => $textpat,
131                                      required => 0, },
132                                    { field    => 'BX',
133                                      pat      => qr/^[YN]{16}$/,
134                                      required => 1, },
135                                    { field    => 'AN',
136                                      pat      => $textpat,
137                                      required => 0, },
138                                    ],
139                         };
140
141 sub one_msg {
142     my ($sock, $test, $seqno) = @_;
143     my $resp;
144     my %fields;
145
146     # If reading or writing fails, then the server's dead,
147     # so there's no point in continuing.
148     if (!write_msg({seqno => $seqno}, $test->{msg}, $sock)) {
149         BAIL_OUT("Write failure in $test->{id}");
150     } elsif (!($resp = <$sock>)) {
151         BAIL_OUT("Read failure in $test->{id}");
152     }
153
154     chomp($resp);
155
156     if (!verify_cksum($resp)) {
157         fail("checksum $test->{id}");
158         return;
159     }
160     if ($resp !~ $test->{pat}) {
161         fail("match leader $test->{id}");
162         diag("Response '$resp' doesn't match pattern '$test->{pat}'");
163         return;
164     }
165
166     # Split the tagged fields of the response into (name, value)
167     # pairs and stuff them into the hash.
168     $resp =~ $test->{pat};
169     %fields = substr($resp, $+[0]) =~ /(..)([^|]*)\|/go;
170
171 #    print STDERR Dumper($test);
172 #    print STDERR Dumper(\%fields);
173     if (!defined($test->{fields})) {
174         diag("TODO: $test->{id} field tests not written yet");
175     } else {
176         # If there are no tagged fields, then 'fields' should be an
177         # empty list which will automatically skip this loop
178         foreach my $ftest (@{$test->{fields}}) {
179             my $field = $ftest->{field};
180
181             if ($ftest->{required} && !exists($fields{$field})) {
182                 fail("$test->{id} required field '$field' exists in '$resp'");
183                 return;
184             }
185
186             if (exists($fields{$field}) && ($fields{$field} !~ $ftest->{pat})) {
187
188                 fail("$test->{id} field test $field");
189                 diag("Field pattern '$ftest->{pat}' for '$field' doesn't match in '$resp'");
190                 return;
191             }
192         }
193     }
194     pass("$test->{id}");
195     return;
196 }
197
198 #
199 # _count_tests: Count the number of tests in a test array
200 sub _count_tests {
201     return scalar @_;
202 }
203
204 sub run_sip_tests {
205     my ($sock, $seqno);
206
207     $Sip::error_detection = 1;
208     $/ = "\r";
209
210     $sock = new IO::Socket::INET(PeerAddr => $server,
211                                  Type     => SOCK_STREAM);
212
213     BAIL_OUT('failed to create connection to server') unless $sock;
214
215     $seqno = 1;
216
217     plan tests => _count_tests(@_);
218
219     foreach my $test (@_) {
220         one_msg($sock, $test, $seqno++);
221         $seqno %= 10;           # sequence number is one digit
222     }
223 }
224
225 1;