Length.pm untested

filter: Statementstmt 0.0% 0 / 78 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 129 CC 3 SCAR 11.0
CC
3
Cov
0%
summarize
6.0
test
6.0
default_parameters
2.0
CRAP 3.0
1 package IDS::Algorithm::Length;
2 use base qw(IDS::Algorithm);
3 $IDS::Algorithm::Length::VERSION = "1.0";
4  
5 =head1 NAME
6  
7 IDS::Algorithm::Length - learn or test the length of a string;
8 based on section 4.1 in the Kruegel and Vigna paper (L</SEE ALSO>).
9  
10 =head1 SYNOPSIS
11  
12 A usage synopsis would go here. Since it is not here, read on.
13  
14 The learning and testing are based on section 4.1 in kruegel2003anomaly.
15  
16 =head1 DESCRIPTION
17  
18 Someday more will be here.
19  
20 =cut
21  
22 use strict;
23 use warnings;
24 use Statistics::Lite qw(:all);
25 use Carp qw(cluck carp confess);
26 use IDS::Utils qw(to_fh);
27  
28 sub default_parameters {
29 my $self = shift;
30  
31 %{$self->{"params"}} = (
32 "verbose" => 0,
33 "state_file" => 0,
34 );
35 }
36  
37 sub param_options {
38 my $self = shift;
39  
40 return (
41 "length_verbose=i" => \${$self->{"params"}}{"verbose"},
42 "ids_state=s" => \${$self->{"params"}}{"state_file"},
43 );
44 }
45  
46 sub initialize {
47 my $self = shift;
48  
49  
50 $self->{"lengths"} = []; # a list of attribute lengths
51 $self->{"stats"} = {}; # length statistics from Statistics::Lite
52 $self->{"summarized"} = 0;
53 }
54  
55 sub summarize {
56 my $self = shift;
57 my $verbose = ${$self->{"params"}}{"verbose"};
58  
59 $self->{"stats"} = { statshash(@{$self->{"lengths"}}) };
60  
61 print join(" ", %{$self->{"stats"}}), "\n" if $verbose;
62 }
63  
64 sub save {
65 my $self = shift;
66 my $fname = $self->find_fname(shift);
67 defined($fname) && $fname or
68 confess *save{PACKAGE} . "::save missing filename";
69 my $fh = to_fh($fname, ">");
70  
71 my ($chars, $c);
72  
73 defined($self->{"summarized"}) && $self->{"summarized"} or
74 $self->summarize;
75  
76 print $fh join(" ", %{$self->{"stats"}}), "\n";
77 }
78  
79 sub load {
80 my $self = shift;
81 my $fname = $self->find_fname(shift);
82 $fname or
83 confess *load{PACKAGE} . "::load missing filename";
84 my $fh = to_fh($fname);
85  
86 my ($line);
87  
88 $line = <$fh>;
89 chomp $line;
90 %{$self->{"stats"}} = split(/ /, $line);
91 }
92  
93 # eqn 3 in kruegel2003anomaly
94 sub test {
95 my $self = shift;
96 my $tref = shift; # not used
97 my $q = shift;
98 defined($q) or
99 confess "bug: missing q to ", *test{PACKAGE} . "::test";
100 my $instance = shift; # not used
101  
102 my $var = ${$self->{"stats"}}{"variance"};
103 my $mean = ${$self->{"stats"}}{"mean"};
104 my $p = $var/(length($q) - $mean) **2;
105 $p = $p > 1 ? 1 : $p;
106 return $p;
107 }
108  
109 sub add {
110 my $self = shift;
111 my $tref = shift; # not used
112 my $str = shift; # 0-length is OK
113 defined($str) or confess "bug: missing q to ", *add{PACKAGE} . "::add";
114 my $instance = shift; # not used
115  
116 push @{$self->{"lengths"}}, length($str);
117 }
118  
119 =head1 AUTHOR INFORMATION
120  
121 Copyright 2005-2007, Kenneth Ingham. All rights reserved.
122  
123 This library is free software; you can redistribute it and/or modify
124 it under the same terms as Perl itself.
125  
126 Address bug reports and comments to: ids_test at i-pi.com. When
127 sending bug reports, please provide the versions of IDS::Test.pm,
128 IDS::Algorithm.pm, IDS::DataSource.pm, the version of Perl, and the
129 name and version of the operating system you are using. Since Kenneth
130 is a PhD student, the speed of the response depends on how the research
131 is proceeding.
132  
133 =head1 BUGS
134  
135 Please report them.
136  
137 =head1 SEE ALSO
138  
139 L<IDS::Test>, L<IDS::DataSource>, L<IDS::Algorithm>
140  
141 "Anomaly detection of web-based attacks" by Christopher Kruegel and
142 Giovanni Vigna, pages 251--261 in Proceedings of the 10th ACM conference
143 on computer and communications security, ACM Press, 2003, ISBN 1-58113-738-9.
144 http://doi.acm.org/10.1145/948109.948144
145  
146 =cut
147  
148 1;