File Coverage

blib/lib/Protocol/TLS/Extension.pm
Criterion Covered Total %
statement 33 43 76.7
branch 4 8 50.0
condition n/a
subroutine 7 8 87.5
pod 0 3 0.0
total 44 62 70.9


line stmt bran cond sub pod time code
1             package Protocol::TLS::Extension;
2 2     2   10 use strict;
  2         2  
  2         69  
3 2     2   9 use warnings;
  2         4  
  2         50  
4 2     2   8 use Carp;
  2         3  
  2         216  
5 2     2   1161 use Module::Runtime qw(require_module);
  2         3390  
  2         40  
6 2     2   112 use Protocol::TLS::Trace qw(tracer);
  2         3  
  2         768  
7              
8             sub ext_decode {
9 1     1 0 4 my ( $ctx, $result_ref, $buf_ref, $buf_offset, $length ) = @_;
10              
11             # Length error
12 1 50       5 if ( $length < 2 ) {
13 0         0 tracer->debug("Extensions length error: MUST be at least 2 bytes\n");
14 0         0 $ctx->error();
15 0         0 return undef;
16             }
17              
18 1         5 my $ext_l = unpack 'n', substr $$buf_ref, $buf_offset, 2;
19 1         3 my $offset = 2;
20              
21             # Length error
22 1 50       10 if ( $offset + $ext_l > $length ) {
23 0         0 tracer->debug("Extensions length error: $ext_l\n");
24 0         0 $ctx->error();
25 0         0 return undef;
26             }
27              
28 1         5 while ( $offset + 4 < $length ) {
29 1         5 my ( $type, $l ) = unpack 'n2', substr $$buf_ref,
30             $buf_offset + $offset, 4;
31 1         2 $offset += 4;
32              
33 1 50       105 if ( $offset + $l > $length ) {
34 0         0 tracer->debug("Extension $type length error: $l\n");
35 0         0 $ctx->error();
36 0         0 return undef;
37             }
38              
39 1 50       11 if ( exists $ctx->{extensions}->{$type} ) {
40 1         10 $ctx->{extensions}->{$type}->decode( $ctx, \$$result_ref->{$type},
41             $buf_ref, $buf_offset + $offset, $l );
42             }
43 1         5 $offset += $l;
44             }
45              
46 1         5 return $offset;
47             }
48              
49             sub ext_encode {
50 0     0 0 0 croak "not implemented";
51             }
52              
53             sub load_extensions {
54 4     4 0 10 my $ctx = shift;
55 4         10 for my $ext (@_) {
56 4         45 my $m = 'Protocol::TLS::Extension::' . $ext;
57 4         16 require_module($m);
58 4         82 $ctx->{extensions}->{ $m->type } = $m->new;
59             }
60             }
61              
62             1