Lines 17-58
Link Here
|
17 |
|
17 |
|
18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
19 |
|
19 |
|
20 |
use Test::More tests => 9; |
20 |
use Test::More tests => 3; |
21 |
use Test::Warn; |
21 |
use Test::Warn; |
22 |
|
22 |
|
23 |
BEGIN { |
23 |
BEGIN { |
24 |
use_ok('C4::SIP::Sip'); |
24 |
use_ok('C4::SIP::Sip', qw|get_timeout| ); |
25 |
} |
25 |
} |
26 |
|
26 |
|
27 |
my $date_time = C4::SIP::Sip::timestamp(); |
27 |
subtest 'Timestamp' => sub { |
28 |
like( $date_time, qr/^\d{8} \d{6}$/, 'Timestamp format no param'); |
28 |
plan tests => 8; |
|
|
29 |
test_timestamp(); |
30 |
}; |
31 |
subtest 'Get_timeout' => sub { |
32 |
plan tests => 11; |
33 |
test_get_timeout(); |
34 |
}; |
29 |
|
35 |
|
30 |
my $t = time(); |
36 |
sub test_timestamp { |
|
|
37 |
my $date_time = C4::SIP::Sip::timestamp(); |
38 |
like( $date_time, qr/^\d{8} \d{6}$/, 'Timestamp format no param'); |
31 |
|
39 |
|
32 |
$date_time = C4::SIP::Sip::timestamp($t); |
40 |
my $t = time(); |
33 |
like( $date_time, qr/^\d{8} \d{6}$/, 'Timestamp format secs'); |
|
|
34 |
|
41 |
|
35 |
$date_time = C4::SIP::Sip::timestamp('2011-01-12'); |
42 |
$date_time = C4::SIP::Sip::timestamp($t); |
36 |
ok( $date_time eq '20110112 235900', 'Timestamp iso date string'); |
43 |
like( $date_time, qr/^\d{8} \d{6}$/, 'Timestamp format secs'); |
37 |
|
44 |
|
38 |
my $myChecksum = C4::SIP::Sip::Checksum::checksum("12345"); |
45 |
$date_time = C4::SIP::Sip::timestamp('2011-01-12'); |
39 |
my $checker = 65281; |
46 |
ok( $date_time eq '20110112 235900', 'Timestamp iso date string'); |
40 |
my $stringChecksum = C4::SIP::Sip::Checksum::checksum("teststring"); |
|
|
41 |
my $stringChecker = 64425; |
42 |
|
47 |
|
43 |
is( $myChecksum, $checker, "Checksum: $myChecksum matches expected output"); |
48 |
my $myChecksum = C4::SIP::Sip::Checksum::checksum("12345"); |
44 |
is( $stringChecksum, $stringChecker, "Checksum: $stringChecksum matches expected output"); |
49 |
my $checker = 65281; |
|
|
50 |
my $stringChecksum = C4::SIP::Sip::Checksum::checksum("teststring"); |
51 |
my $stringChecker = 64425; |
45 |
|
52 |
|
46 |
my $testdata = "abcdAZ"; |
53 |
is( $myChecksum, $checker, "Checksum: $myChecksum matches expected output"); |
47 |
my $something = C4::SIP::Sip::Checksum::checksum($testdata); |
54 |
is( $stringChecksum, $stringChecker, "Checksum: $stringChecksum matches expected output"); |
48 |
|
55 |
|
49 |
$something = sprintf("%4X", $something); |
56 |
my $testdata = "abcdAZ"; |
50 |
ok( C4::SIP::Sip::Checksum::verify_cksum($testdata.$something), "Checksum: $something is valid."); |
57 |
my $something = C4::SIP::Sip::Checksum::checksum($testdata); |
51 |
|
58 |
|
52 |
my $invalidTest; |
59 |
$something = sprintf("%4X", $something); |
53 |
warning_is { $invalidTest = C4::SIP::Sip::Checksum::verify_cksum("1234567") } |
60 |
ok( C4::SIP::Sip::Checksum::verify_cksum($testdata.$something), "Checksum: $something is valid."); |
54 |
'verify_cksum: no sum detected', |
61 |
|
55 |
'verify_cksum prints the expected warning for an invalid checksum'; |
62 |
my $invalidTest; |
56 |
is($invalidTest, 0, "Checksum: 1234567 is invalid as expected"); |
63 |
warning_is { $invalidTest = C4::SIP::Sip::Checksum::verify_cksum("1234567") } |
|
|
64 |
'verify_cksum: no sum detected', |
65 |
'verify_cksum prints the expected warning for an invalid checksum'; |
66 |
is($invalidTest, 0, "Checksum: 1234567 is invalid as expected"); |
67 |
} |
68 |
|
69 |
sub test_get_timeout { |
70 |
my $server = { policy => { timeout => 1 }, |
71 |
config => { timeout => 2 }, |
72 |
service => { |
73 |
timeout => 3, |
74 |
client_timeout => 4, |
75 |
}, |
76 |
}; |
77 |
|
78 |
is( get_timeout(), 30, "Default fallback" ); |
79 |
is( get_timeout( undef, { fallback => 25 } ), 25, "Fallback parameter" ); |
80 |
is( get_timeout( $server, { transport => 1 } ), 3, "Transport value" ); |
81 |
is( get_timeout( $server, { client => 1 } ), 4, "Client value" ); |
82 |
is( get_timeout( $server, { policy => 1 } ), '001', "Policy value" ); |
83 |
|
84 |
delete $server->{policy}->{timeout}; |
85 |
is( get_timeout( $server, { policy => 1 } ), '000', "No policy" ); |
86 |
|
87 |
$server->{service}->{client_timeout} = '0'; |
88 |
is( get_timeout( $server, { client => 1 } ), 0, "Client zero" ); |
89 |
$server->{service}->{client_timeout} = 'no'; |
90 |
is( get_timeout( $server, { client => 1 } ), 0, "Client no" ); |
91 |
delete $server->{service}->{client_timeout}; |
92 |
is( get_timeout( $server, { client => 1 } ), 3, "Fallback to service" ); |
93 |
|
94 |
delete $server->{service}->{timeout}; |
95 |
is( get_timeout( $server, { transport => 1 } ), 2, "Back to old config" ); |
96 |
delete $server->{config}->{timeout}; |
97 |
is( get_timeout( $server, { transport => 1 } ), 30, "Fallback again" ); |
98 |
} |
57 |
|
99 |
|
58 |
1; |
100 |
1; |
59 |
- |
|
|