File Coverage

blib/lib/RFID/Reader/TestBase.pm
Criterion Covered Total %
statement 42 51 82.3
branch 6 20 30.0
condition 2 6 33.3
subroutine 10 10 100.0
pod 0 1 0.0
total 60 88 68.1


line stmt bran cond sub pod time code
1             package RFID::Reader::TestBase;
2 6     6   303530 use RFID::Reader qw(ref_tainted); $VERSION=$RFID::Reader::VERSION;
  6         15  
  6         545  
3             @ISA=();
4              
5             # Written by Scott Gifford
6             # Copyright (C) 2004-2006 The Regents of the University of Michigan.
7             # See the file LICENSE included with the distribution for license
8             # information.
9              
10             =head1 NAME
11              
12             RFID::Reader::TestBase - Provide basic methods for writing an RFID reader test module
13              
14             =head1 SYNOPSIS
15              
16             Provides fake backend methods to test out L
17             without having access to a real reader.
18              
19             =cut
20              
21              
22 6     6   4616 use IO::Select;
  6         8180  
  6         320  
23 6     6   1101 use IO::Handle;
  6         8674  
  6         250  
24 6     6   39 use Carp;
  6         12  
  6         4612  
25              
26             sub _init
27             {
28 4     4   484 my $self = shift;
29              
30 4         48 $self->{_readbuf}='';
31 4         15 $self->{_writebuf}='';
32 4         14 $self;
33             }
34              
35             sub _add_output
36             {
37 3     3   36 my $self = shift;
38 3         17 $self->{_writebuf} .= join('',@_);
39             }
40              
41             sub _writebytes
42             {
43 5     5   2257 my $self = shift;
44 5         21 my $wb = join("",@_);
45 5 100       36 if (ref_tainted(\$wb)) { die "Attempt to send tainted data to reader"; }
  2         18  
46 3         41 $self->debug("WRITEBYTES: $wb\n");
47 3         22 $self->{_readbuf} = $self->_process_input($self->{_readbuf}.$wb);
48 3         31 return length($wb);
49             }
50              
51             sub _readbytes
52             {
53 3     3   7 my $self = shift;
54 3         10 my($wantbytes)=@_;
55              
56 3         14 my $rb = substr($self->{_writebuf},0,$wantbytes,'');
57              
58 3         17 $self->debug("READBYTES: $rb\n");
59 3         29 $rb;
60             }
61              
62             sub _readuntil
63             {
64 2     2   4 my $self = shift;
65 2         3 my($delim)=@_;
66              
67 2 50       52 if ($self->{_writebuf} =~ s/^(.*?)$delim//s)
68             {
69 2         24 $self->debug("READUNTIL: $1\n");
70 2         13 return $1;
71             }
72             else
73             {
74 0         0 croak "Attempt to read with no data!";
75             }
76             }
77              
78             sub run
79             {
80 1     1 0 1005746 my $self = shift;
81 1 50 33     54 my $readh = shift || IO::Handle->new_from_fd(fileno(STDIN),"r")
82             or die "Couldn't get read filehandle: $!\n";
83 1 50 33     38 my $writeh = shift || shift || IO::Handle->new_from_fd(fileno(STDOUT),"w")
84             or die "Couldn't get write filehandle: $!\n";
85              
86 1         30 my $readsel = IO::Select->new($readh);
87 1         193 my $writesel = IO::Select->new($writeh);
88            
89 1         156 while (1)
90             {
91 1 50       36 my($readable,$writable,undef) = IO::Select->select($readsel, $self->{_writebuf}?$writesel:undef, undef)
    0          
92             or last;
93 0 0         if (@$readable)
94             {
95 0           my $readbuf;
96 0 0         sysread($readable->[0],$readbuf,8192)
97             or die "Couldn't read: $!\n";
98             # This is just for testing, so untaint it blindly.
99 0           $readbuf =~ /^(.*)$/s;
100 0           $self->_writebytes($1);
101             }
102 0 0         if (@$writable)
103             {
104 0 0         my $wrote = syswrite($writable->[0],$self->{_writebuf})
105             or die "Couldn't write: $!\n";
106 0           substr($self->{_writebuf},0,$wrote)='';
107             }
108             }
109             }
110              
111             =head1 SEE ALSO
112              
113             L.
114              
115             =head1 AUTHOR
116              
117             Scott Gifford Egifford@umich.eduE, Esgifford@suspectclass.comE
118              
119             Copyright (C) 2004-2006 The Regents of the University of Michigan.
120              
121             See the file LICENSE included with the distribution for license
122             information.
123              
124             =cut
125              
126             1;