8 our @ISA = qw(Exporter);
10 our @EXPORT_OK = qw(run_sip_tests no_tagged_fields
12 $login_test $sc_status_test
15 $instid $currency $server $username $password
16 $user_barcode $user_pin $user_fullname $user_homeaddr
17 $user_email $user_phone $user_birthday $user_ptype
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);
25 # The number of tests is set in run_sip_tests() below, based
26 # on the size of the array of tests.
31 use Sip::Checksum qw(verify_cksum);
32 use Sip::Constants qw(:all);
35 # Configuration parameters to run the test suite
37 our $instid = 'UWOLS';
38 our $currency = 'CAD';
39 our $server = 'localhost:6001'; # Address of the SIP server
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';
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';
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';
65 our $item2_barcode = '0440242746';
66 our $item2_title = 'The deep blue alibi';
67 our $item2_owner = 'UWOLS';
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';
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}';
80 # Pattern for a random text field (may be empty)
81 our $textpat = qr/^[^|]*$/;
84 (FID_SCREEN_MSG) => { field => FID_SCREEN_MSG,
87 (FID_PRINT_LINE) => { field => FID_PRINT_LINE,
90 (FID_INST_ID) => { field => FID_INST_ID,
91 pat => qr/^$instid$/o,
93 (FID_HOLD_ITEMS_LMT)=> { field => FID_HOLD_ITEMS_LMT,
96 (FID_OVERDUE_ITEMS_LMT)=> { field => FID_OVERDUE_ITEMS_LMT,
99 (FID_CHARGED_ITEMS_LMT)=> { field => FID_CHARGED_ITEMS_LMT,
102 (FID_VALID_PATRON) => { field => FID_VALID_PATRON,
105 (FID_VALID_PATRON_PWD)=> { field => FID_VALID_PATRON_PWD,
108 (FID_CURRENCY) => { field => FID_CURRENCY,
109 pat => qr/^$currency$/io,
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.
117 our $login_test = { id => 'login',
118 msg => "9300CN$username|CO$password|CPThe floor|",
122 our $sc_status_test = { id => 'SC status',
124 pat => qr/^98[YN]{6}\d{3}\d{3}$datepat(2\.00|1\.00)/,
126 $field_specs{(FID_SCREEN_MSG)},
127 $field_specs{(FID_PRINT_LINE)},
128 $field_specs{(FID_INST_ID)},
133 pat => qr/^[YN]{16}$/,
142 my ($sock, $test, $seqno) = @_;
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}");
156 if (!verify_cksum($resp)) {
157 fail("checksum $test->{id}");
160 if ($resp !~ $test->{pat}) {
161 fail("match leader $test->{id}");
162 diag("Response '$resp' doesn't match pattern '$test->{pat}'");
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;
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");
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};
181 if ($ftest->{required} && !exists($fields{$field})) {
182 fail("$test->{id} required field '$field' exists in '$resp'");
186 if (exists($fields{$field}) && ($fields{$field} !~ $ftest->{pat})) {
188 fail("$test->{id} field test $field");
189 diag("Field pattern '$ftest->{pat}' for '$field' doesn't match in '$resp'");
199 # _count_tests: Count the number of tests in a test array
207 $Sip::error_detection = 1;
210 $sock = new IO::Socket::INET(PeerAddr => $server,
211 Type => SOCK_STREAM);
213 BAIL_OUT('failed to create connection to server') unless $sock;
217 plan tests => _count_tests(@_);
219 foreach my $test (@_) {
220 one_msg($sock, $test, $seqno++);
221 $seqno %= 10; # sequence number is one digit