blib/lib/SMB/Command.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 19 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 108 SCAR 24.8
CC
3
Cov
0%
dump
12.0
new
2.0
is
2.0
CRAP 12.0
1 # SMB-Perl library, Copyright (C) 2014 Mikhael Goikhman, migo@cpan.org
2 #
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15  
16 package SMB::Command;
17  
18 use strict;
19 use warnings;
20  
21 use parent 'SMB';
22  
23 sub new ($$$$%) {
24 my $class = shift;
25 my $smb = shift || die "No smb parameter in $class constructor\n";
26 my $name = shift || die "No name parameter in $class constructor\n";
27 my $header = shift || die "No header parameter in $class constructor\n";
28 my %options = @_;
29  
30 my $self = $class->SUPER::new(
31 %options,
32 smb => $smb,
33 name => $name,
34 header => $header,
35 );
36  
37 $self->init;
38  
39 return $self;
40 }
41  
42 sub is ($$) {
43 my $self = shift;
44 my $name = shift || '';
45  
46 return $self->name eq $name;
47 }
48  
49 sub is_response ($) {
50 my $self = shift;
51  
52 return $self->header->is_response;
53 }
54  
55 sub is_response_to ($$) {
56 my $self = shift;
57 my $request = shift || die;
58  
59 my $header1 = $request->header;
60 my $header2 = $self->header;
61  
62 return
63 !$header1->is_response &&
64 $header2->is_response &&
65 $header1->code == $header2->code &&
66 $header1->mid == $header2->mid;
67 }
68  
69 sub is_smb1 ($) { $_[0]->smb <= 1 }
70 sub is_smb2 ($) { $_[0]->smb >= 2 }
71  
72 sub status ($) { $_[0]->header->status }
73 sub set_status ($$) { $_[0]->header->status($_[1]); }
74 sub is_success ($) { $_[0]->status == 0 }
75 sub is_error ($) { $_[0]->status != 0 }
76  
77 # stub methods to be overloaded
78  
79 sub parse ($$%) {
80 my $self = shift;
81 my $parser = shift;
82  
83 return $self;
84 }
85  
86 sub pack ($$%) {
87 my $self = shift;
88 my $packer = shift;
89  
90 return $self;
91 }
92  
93 sub init ($) {
94 }
95  
96 # end of stub methods
97  
98 sub set ($%) {
99 my $self = shift;
100 my %values = @_;
101  
102 $self->{$_} = $values{$_} for keys %values;
103 }
104  
105 sub abort_pack ($$) {
106 my $self = shift;
107 my $packer = shift;
108 my $status = shift;
109  
110 $self->set_status($status);
111 $packer->jump('status')->uint32($status)->jump('command-start');
112  
113 return $self;
114 }
115  
116 sub dump ($) {
117 my $self = shift;
118  
119 return sprintf "SMB%d [%s %s] mid=%u uid=%x tid=%02x%s",
120 $self->smb, $self->name,
121 $self->is_response ? "Response" : "Request ",
122 $self->header->mid,
123 $self->header->uid,
124 $self->header->tid,
125 $self->status ? sprintf " status=%x", $self->status : '',
126 }
127  
128 1;