7 use vars qw(@ISA $VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
14 auth => [qw(&api_auth)],
15 basic => [qw($datepat $textpat $login_test $sc_status_test
16 $instid $currency $server $username $password)],
17 user1 => [qw($user_barcode $user_pin $user_fullname $user_homeaddr $user_email
18 $user_phone $user_birthday $user_ptype $user_inet)],
19 item1 => [qw($item_barcode $item_title $item_owner )],
20 diacritic => [qw($item_diacritic_barcode $item_diacritic_title $item_diacritic_owner)],
22 # duplicate user1 and item1 as user2 and item2
23 # w/ tags like $user2_pin instead of $user_pin
24 foreach my $tag (qw(user item)) {
25 my @tags = @{$EXPORT_TAGS{$tag.'1'}}; # fresh array avoids side affect in map
26 push @{$EXPORT_TAGS{$tag.'2'}}, map {s/($tag)\_/${1}2_/;$_} @tags;
28 # From perldoc Exporter
29 # Add all the other ":class" tags to the ":all" class, deleting duplicates
31 push @{$EXPORT_TAGS{all}},
32 grep {!$seen{$_}++} @{$EXPORT_TAGS{$_}} foreach keys %EXPORT_TAGS;
33 Exporter::export_ok_tags('all'); # Anything in a tag is in OK_EXPORT
34 # print Dumper(\%EXPORT_TAGS); # Uncomment if you want to see the results of these tricks.
37 # The number of tests is set in run_sip_tests() below, based
38 # on the size of the array of tests.
44 use Sip::Checksum qw(verify_cksum);
45 use Sip::Constants qw(:all);
47 use C4::Auth qw(&check_api_auth);
51 # Configuration parameters to run the test suite
53 our $instid = 'CPL'; # 'UWOLS';
54 our $currency = 'USD'; # 'CAD';
55 our $server = 'localhost:6001'; # Address of the SIP server
57 # SIP username and password to connect to the server. See the
58 # SIP config.xml for the correct values.
59 our $username = 'term1';
60 our $password = 'term1';
64 # NOTE: make sure to escape the data for use in RegExp.
65 # Valid user barcode and corresponding user password/pin and full name
66 our $user_barcode = '23529001000463';
67 our $user_pin = 'fn5zS';
68 our $user_fullname= 'Edna Acosta';
69 our $user_homeaddr= '7896 Library Rd\.';
70 our $user_email = 'patron\@liblime\.com';
71 our $user_phone = '\(212\) 555-1212';
72 our $user_birthday= '1980-04-24';
73 our $user_ptype = 'PT';
77 our $user2_barcode = '23529000240482';
78 our $user2_pin = 'jw937';
79 our $user2_fullname= 'Jamie White';
80 our $user2_homeaddr= '937 Library Rd\.';
81 our $user2_email = 'patron\@liblime\.com';
82 our $user2_phone = '\(212\) 555-1212';
83 our $user2_birthday= '1950-04-22';
84 our $user2_ptype = 'T';
85 our $user2_inet = 'Y';
87 # Valid item barcode and corresponding title
88 our $item_barcode = '502326000005';
89 our $item_title = 'How I became a pirate /';
90 our $item_owner = 'CPL';
93 our $item2_barcode = '502326000011';
94 our $item2_title = 'The biggest, smallest, fastest, tallest things you\'ve ever heard of /';
95 our $item2_owner = 'CPL';
97 # An item with a diacritical in the title
98 our $item_diacritic_barcode = '502326001030';
99 our $item_diacritic_titlea = 'Hari Poṭer u-geviʻa ha-esh /';
100 our $item_diacritic_owner = 'CPL';
104 # Pattern for a SIP datestamp, to be used by individual tests to
105 # match timestamp fields (duh).
106 our $datepat = '\d{8} {4}\d{6}';
108 # Pattern for a random text field (may be empty)
109 our $textpat = qr/^[^|]*$/;
112 (FID_SCREEN_MSG) => { field => FID_SCREEN_MSG,
115 (FID_PRINT_LINE) => { field => FID_PRINT_LINE,
118 (FID_INST_ID) => { field => FID_INST_ID,
119 pat => qr/^$instid$/o,
121 (FID_HOLD_ITEMS_LMT)=> { field => FID_HOLD_ITEMS_LMT,
124 (FID_OVERDUE_ITEMS_LMT)=> { field => FID_OVERDUE_ITEMS_LMT,
127 (FID_CHARGED_ITEMS_LMT)=> { field => FID_CHARGED_ITEMS_LMT,
130 (FID_VALID_PATRON) => { field => FID_VALID_PATRON,
133 (FID_VALID_PATRON_PWD)=> { field => FID_VALID_PATRON_PWD,
136 (FID_CURRENCY) => { field => FID_CURRENCY,
137 pat => qr/^$currency$/io,
141 # Login and SC Status are always the first two messages that
142 # the terminal sends to the server, so just create the test
143 # cases here and reference them in the individual test files.
145 our $login_test = { id => 'login',
146 msg => "9300CN$username|CO$password|CPThe floor|",
150 our $sc_status_test = { id => 'SC status',
152 pat => qr/^98[YN]{6}\d{3}\d{3}$datepat(2\.00|1\.00)/,
154 $field_specs{(FID_SCREEN_MSG)},
155 $field_specs{(FID_PRINT_LINE)},
156 $field_specs{(FID_INST_ID)},
161 pat => qr/^[YN]{16}$/,
170 my ($sock, $test, $seqno) = @_;
174 # If reading or writing fails, then the server's dead,
175 # so there's no point in continuing.
176 if (!write_msg({seqno => $seqno}, $test->{msg}, $sock)) {
177 BAIL_OUT("Write failure in $test->{id}");
178 } elsif (!($resp = <$sock>)) {
179 BAIL_OUT("Read failure in $test->{id}");
186 if (!verify_cksum($resp)) {
187 fail("$test->{id} checksum($resp)");
190 if ($resp !~ $test->{pat}) {
191 fail("match leader $test->{id}");
192 diag("Response '$resp' doesn't match pattern '$test->{pat}'");
196 # Split the tagged fields of the response into (name, value)
197 # pairs and stuff them into the hash.
198 $resp =~ $test->{pat};
199 %fields = substr($resp, $+[0]) =~ /(..)([^|]*)\|/go;
201 # print STDERR "one_msg ( test ) : " . Dumper($test) . "\n" .
202 # "one_msg (fields) : " . Dumper(\%fields);
203 if (!defined($test->{fields})) {
204 diag("TODO: $test->{id} field tests not written yet");
206 # If there are no tagged fields, then 'fields' should be an
207 # empty list which will automatically skip this loop
208 foreach my $ftest (@{$test->{fields}}) {
209 my $field = $ftest->{field};
211 if ($ftest->{required} && !exists($fields{$field})) {
212 fail("$test->{id}: required field '$field' not found in '$resp'");
216 if (exists($fields{$field}) && ($fields{$field} !~ $ftest->{pat})) {
217 fail("$test->{id} field test $field");
218 diag("Field '$field' pattern '$ftest->{pat}' fails to match value '$fields{$field}' in message '$resp'");
229 $ENV{REMOTE_USER} = $username;
230 my $query = CGI->new();
231 $query->param(userid => $username);
232 $query->param(password => $password);
233 my ($status, $cookie, $sessionID) = check_api_auth($query, {circulate=>1}, "intranet");
234 print STDERR "check_api_auth returns " . ($status || 'undef') . "\n";
235 # print STDERR "api_auth userenv = " . &dump_userenv;
240 my $userenv = C4::Context->userenv;
241 return "# userenv: EMPTY\n" unless ($userenv);
242 my $userbranch = $userenv->{branch};
243 return "# userenv: " . Dumper($userenv)
244 . ($userbranch ? "BRANCH FOUND: $userbranch\n" : "NO BRANCH FOUND\n");
250 $Sip::error_detection = 1;
253 $sock = new IO::Socket::INET(PeerAddr => $server,
254 Type => SOCK_STREAM);
256 BAIL_OUT('failed to create connection to server') unless $sock;
259 # print STDERR "Number of tests : ", scalar (@_), "\n";
260 plan tests => scalar(@_);
261 foreach my $test (@_) {
262 # print STDERR "Test $seqno:" . Dumper($test);
263 one_msg($sock, $test, $seqno++);
264 $seqno %= 10; # sequence number is one digit