/[cwmp]/google/trunk/t/30-server.t
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /google/trunk/t/30-server.t

Parent Directory Parent Directory | Revision Log Revision Log


Revision 238 - (hide annotations)
Mon Nov 26 00:28:54 2007 UTC (16 years, 5 months ago) by dpavlin
File MIME type: application/x-troff
File size: 3378 byte(s)
 r287@brr:  dpavlin | 2007-11-26 01:28:00 +0100
 implemented parsing into $state->{ParameterAttribute}

1 dpavlin 41 #!/usr/bin/perl
2     use strict;
3     use warnings;
4    
5     my $debug = shift @ARGV;
6    
7 dpavlin 238 use Test::More tests => 153;
8 dpavlin 41 use Data::Dump qw/dump/;
9 dpavlin 77 use Cwd qw/abs_path/;
10 dpavlin 234 use File::Find;
11     use File::Slurp;
12     use File::Path qw/rmtree mkpath/;
13     use YAML::Syck;
14 dpavlin 41 use blib;
15    
16     BEGIN {
17     use_ok('Net::HTTP');
18     use_ok('CWMP::Server');
19 dpavlin 85 use_ok('CWMP::Store');
20 dpavlin 234 use_ok('CWMP::Request');
21 dpavlin 41 }
22    
23     my $port = 4242;
24    
25     eval {
26     $SIG{ALRM} = sub { die; };
27     alarm 30;
28     };
29    
30 dpavlin 77 ok(my $abs_path = abs_path($0), "abs_path");
31     $abs_path =~ s!/[^/]*$!/!; #!fix-vim
32    
33 dpavlin 163 my $store_path = "$abs_path/var/";
34     my $store_module = 'YAML';
35 dpavlin 84
36 dpavlin 234 rmtree $store_path if -e $store_path;
37     ok( mkpath $store_path, "mkpath $store_path" );
38    
39 dpavlin 77 ok( my $server = CWMP::Server->new({
40     debug => $debug,
41     port => $port,
42 dpavlin 205 session => {
43     store => {
44     module => $store_module,
45     path => $store_path,
46 dpavlin 234 # clean => 1,
47 dpavlin 205 },
48     create_dump => 0,
49     }
50 dpavlin 77 }), 'new' );
51 dpavlin 41 isa_ok( $server, 'CWMP::Server' );
52    
53     my $pid;
54    
55     if ( $pid = fork ) {
56     ok( $pid, 'fork ');
57     diag "forked $pid";
58     } elsif (defined($pid)) {
59     # child
60     $server->run;
61     exit;
62     } else {
63     die "can't fork";
64     }
65    
66 dpavlin 84 sleep 1; # so server can start
67    
68 dpavlin 234 my $s;
69 dpavlin 41
70 dpavlin 234 sub cpe_connect {
71     return $s if $s;
72     diag "CPE connect";
73     ok( $s = Net::HTTP->new(Host => "localhost:$port"), 'CPE client' );
74     $s->keep_alive( 1 );
75     }
76 dpavlin 41
77 dpavlin 234 sub cpe_disconnect {
78     return unless $s;
79     diag "CPE disconnect";
80     $s->keep_alive( 0 );
81     ok( $s->write_request(
82     POST => '/',
83     'SOAPAction' => '',
84     'Content-Type' => 'text/xml',
85     ), 'write_request' );
86     my ($code, $mess, %h) = $s->read_response_headers;
87     undef $s;
88     diag "$code $mess";
89     return $code == 200 ? 1 : 0;
90 dpavlin 41 }
91    
92 dpavlin 234 sub test_request {
93     my $path = shift;
94 dpavlin 41
95 dpavlin 234 ok( -e $path, $path );
96 dpavlin 85
97 dpavlin 234 cpe_disconnect if $path =~ m/Inform/;
98     cpe_connect;
99 dpavlin 85
100 dpavlin 234 ok( $s->write_request(
101     POST => '/',
102     'Transfer-Encoding' => 'chunked',
103     'SOAPAction' => '',
104     'Content-Type' => 'text/xml',
105     ), 'write_request' );
106 dpavlin 85
107 dpavlin 234 my $xml = read_file( $path );
108     $xml =~ s/^.+?</</s;
109 dpavlin 223
110 dpavlin 234 my $chunk_size = 5000;
111    
112     foreach my $part ( 0 .. int( length($xml) / $chunk_size ) ) {
113     my $chunk = substr( $xml, $part * $chunk_size, $chunk_size );
114     ok( $s->write_chunk( $chunk ), "chunk $part " . length($chunk) . " bytes" );
115     }
116     ok( $s->write_chunk_eof, 'write_chunk_eof' );
117    
118     my($code, $mess, %h) = $s->read_response_headers;
119     diag "$code $mess";
120     while (1) {
121     my $buf;
122     my $n = $s->read_entity_body($buf, 1024);
123     die "read failed: $!" unless defined $n;
124     last unless $n;
125     diag $buf;
126     }
127    
128     ok( my $store = CWMP::Store->new({ module => $store_module, path => $store_path, debug => $debug }), 'another store' );
129    
130     my $state = LoadFile( "$path.yml" );
131    
132     $path =~ s!/[^/]+$!!; #!vim
133     ok( my $uid = $store->state_to_uid( LoadFile( "$path/Inform.yml" ) ), 'state_to_uid' );
134    
135     ok( my $store_state = $store->current_store->get_state( $uid ), 'get_state' );
136    
137     my $s;
138    
139     # ignore
140     foreach my $k ( keys %$state ) {
141     if ( defined( $store_state->{$k} ) ) {
142     $s->{$k} = $store_state->{$k};
143     } else {
144     die "store_state doesn't have $k: ",dump( $store_state );
145     }
146     }
147    
148     is_deeply( $s, $state, 'store->current_store->get_state' );
149    
150     }
151    
152     find({
153     no_chdir => 1,
154     wanted => sub {
155     my $path = $File::Find::name;
156     return unless -f $path;
157     return if $path =~ m/\.yml$/;
158     eval {
159     test_request( $path );
160     };
161     ok( ! $@, "request $path" );
162     }
163     },'t/dump/');
164    
165     ok( cpe_disconnect, 'cpe_disconnect' );
166    
167 dpavlin 197 diag "shutdown server";
168    
169 dpavlin 84 ok( kill(9,$pid), 'kill ' . $pid );
170    
171 dpavlin 41 ok( waitpid($pid,0), 'waitpid' );
172 dpavlin 205

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26