X-Git-Url: http://git.rot13.org/?p=perl-cwmp.git;a=blobdiff_plain;f=t%2F30-server.t;h=236058445ee14a2193b24578034f5809ade18bb7;hp=4b7747c44613f24c030f693d4a3f2fb31d83ca0a;hb=ab3c8b6b75a3c7fb9ddc5a0c474fb1aa7269bc83;hpb=fec1acf5d1dfce1a661535d2b40b0e9d97de6005 diff --git a/t/30-server.t b/t/30-server.t index 4b7747c..2360584 100755 --- a/t/30-server.t +++ b/t/30-server.t @@ -4,15 +4,20 @@ use warnings; my $debug = shift @ARGV; -use Test::More tests => 19; +use Test::More tests => 153; use Data::Dump qw/dump/; use Cwd qw/abs_path/; +use File::Find; +use File::Slurp; +use File::Path qw/rmtree mkpath/; +use YAML::Syck; use blib; BEGIN { use_ok('Net::HTTP'); use_ok('CWMP::Server'); use_ok('CWMP::Store'); + use_ok('CWMP::Request'); } my $port = 4242; @@ -26,17 +31,22 @@ ok(my $abs_path = abs_path($0), "abs_path"); $abs_path =~ s!/[^/]*$!/!; #!fix-vim my $store_path = "$abs_path/var/"; -#my $store_module = 'DBMDeep'; my $store_module = 'YAML'; +rmtree $store_path if -e $store_path; +ok( mkpath $store_path, "mkpath $store_path" ); + ok( my $server = CWMP::Server->new({ debug => $debug, port => $port, - store => { - module => $store_module, - path => $store_path, - clean => 1, - }, + session => { + store => { + module => $store_module, + path => $store_path, +# clean => 1, + }, + create_dump => 0, + } }), 'new' ); isa_ok( $server, 'CWMP::Server' ); @@ -55,144 +65,108 @@ if ( $pid = fork ) { sleep 1; # so server can start -ok( my $s = Net::HTTP->new(Host => "localhost:$port"), 'client' ); -$s->keep_alive( 1 ); - -ok( $s->write_request( - POST => '/', - 'Transfer-Encoding' => 'chunked', - 'SOAPAction' => '', - 'Content-Type' => 'text/xml', -), 'write_request' ); - -foreach my $chunk (qq{ - - - -1_THOM_TR69_ID - - - - - THOMSON - 00147F - SpeedTouch 780 - CP0644JTHJ4 - - - - 0 BOOTSTRAP - - - - 1 BOOT - - - - 4 VALUE CHANGE - - - -2 -1970-01-01T00:04:33Z -01},qq{ - - - InternetGatewayDevice.DeviceSummary - InternetGatewayDevice:1.1[] (Baseline:1, EthernetLAN:1, ADSLWAN:1, Bridging:1, Time:1, WiFiLAN:1) - - - }, qq{ -InternetGatewayDevice.DeviceInfo.SpecVersion - 1.1 - - - InternetGatewayDevice.DeviceInfo.HardwareVersion - BANT-R - - - InternetGatewayDevice.DeviceInfo.SoftwareVersion - 6.2.15.5 - - - InternetGatewayDevice.DeviceInfo.ProvisioningCode - - - - InternetGatewayDevice.DeviceInfo.VendorConfigFile.1.Name - Routed PPPoE on 0/35 and 8/35 - - - InternetGatewayDevice.DeviceInfo.VendorConfigFile.1.Version - - - - InternetGatewayDevice.DeviceInfo.VendorConfigFile.1.Date - 0000-00-00T00:00:00 - - - InternetGatewayDevice.DeviceInfo.VendorConfigFile.1.Description - Factory Defaults - - - InternetGatewayDevice.ManagementServer.ConnectionRequestURL - http://192.168.1.254:51005/ - - - InternetGatewayDevice.ManagementServer.ParameterKey - - - - .ExternalIPAddress - 192.168.1.254 - - - - - -} ) { - ok( $s->write_chunk( $chunk ), "chunk " . length($chunk) . " bytes" ); +my $s; + +sub cpe_connect { + return $s if $s; + diag "CPE connect"; + ok( $s = Net::HTTP->new(Host => "localhost:$port"), 'CPE client' ); + $s->keep_alive( 1 ); } -ok( $s->write_chunk_eof, 'write_chunk_eof' ); - -sleep 1; - -ok( my $store = CWMP::Store->new({ module => $store_module, path => $store_path, debug => $debug }), 'another store' ); - -my $state = { - CurrentTime => "1970-01-01T00:04:33Z", - DeviceID => { - Manufacturer => "THOMSON", - OUI => "00147F", - ProductClass => "SpeedTouch 780", - SerialNumber => "CP0644JTHJ4", - }, - EventStruct => ["0 BOOTSTRAP", "1 BOOT", "4 VALUE CHANGE"], - ID => "1_THOM_TR69_ID", - MaxEnvelopes => 2, -# NoMoreRequests => undef, - Parameter => { - "\nInternetGatewayDevice.DeviceInfo.SpecVersion" => "1.1", - ".ExternalIPAddress" => "192.168.1.254", - "InternetGatewayDevice.DeviceInfo.HardwareVersion" => "BANT-R", - "InternetGatewayDevice.DeviceInfo.ProvisioningCode" => undef, - "InternetGatewayDevice.DeviceInfo.SoftwareVersion" => "6.2.15.5", - "InternetGatewayDevice.DeviceInfo.VendorConfigFile.1.Date" => "0000-00-00T00:00:00", - "InternetGatewayDevice.DeviceInfo.VendorConfigFile.1.Description" => "Factory Defaults", - "InternetGatewayDevice.DeviceInfo.VendorConfigFile.1.Name" => "Routed PPPoE on 0/35 and 8/35", - "InternetGatewayDevice.DeviceInfo.VendorConfigFile.1.Version" => undef, - "InternetGatewayDevice.DeviceSummary" => "InternetGatewayDevice:1.1[] (Baseline:1, EthernetLAN:1, ADSLWAN:1, Bridging:1, Time:1, WiFiLAN:1)", - "InternetGatewayDevice.ManagementServer.ConnectionRequestURL" => "http://192.168.1.254:51005/", - "InternetGatewayDevice.ManagementServer.ParameterKey" => undef, - }, - RetryCount => "01", - _dispatch => "InformResponse", -}; -is_deeply( $store->current_store->get_state( 'CP0644JTHJ4' ), $state, 'store->current_store->get_state' ); +sub cpe_disconnect { + return unless $s; + diag "CPE disconnect"; + $s->keep_alive( 0 ); + ok( $s->write_request( + POST => '/', + 'SOAPAction' => '', + 'Content-Type' => 'text/xml', + ), 'write_request' ); + my ($code, $mess, %h) = $s->read_response_headers; + undef $s; + diag "$code $mess"; + return $code == 200 ? 1 : 0; +} + +sub test_request { + my $path = shift; + + ok( -e $path, $path ); + + cpe_disconnect if $path =~ m/Inform/; + cpe_connect; + + ok( $s->write_request( + POST => '/', + 'Transfer-Encoding' => 'chunked', + 'SOAPAction' => '', + 'Content-Type' => 'text/xml', + ), 'write_request' ); + + my $xml = read_file( $path ); + $xml =~ s/^.+?write_chunk( $chunk ), "chunk $part " . length($chunk) . " bytes" ); + } + ok( $s->write_chunk_eof, 'write_chunk_eof' ); + + my($code, $mess, %h) = $s->read_response_headers; + diag "$code $mess"; + while (1) { + my $buf; + my $n = $s->read_entity_body($buf, 1024); + die "read failed: $!" unless defined $n; + last unless $n; + diag $buf; + } + + ok( my $store = CWMP::Store->new({ module => $store_module, path => $store_path, debug => $debug }), 'another store' ); + + my $state = LoadFile( "$path.yml" ); + + $path =~ s!/[^/]+$!!; #!vim + ok( my $uid = $store->state_to_uid( LoadFile( "$path/Inform.yml" ) ), 'state_to_uid' ); + + ok( my $store_state = $store->current_store->get_state( $uid ), 'get_state' ); + + my $s; + + # ignore + foreach my $k ( keys %$state ) { + if ( defined( $store_state->{$k} ) ) { + $s->{$k} = $store_state->{$k}; + } else { + die "store_state doesn't have $k: ",dump( $store_state ); + } + } + + is_deeply( $s, $state, 'store->current_store->get_state' ); + +} + +find({ + no_chdir => 1, + wanted => sub { + my $path = $File::Find::name; + return unless -f $path; + return if $path =~ m/\.yml$/; + eval { + test_request( $path ); + }; + ok( ! $@, "request $path" ); + } +},'t/dump/'); + +ok( cpe_disconnect, 'cpe_disconnect' ); diag "shutdown server"; ok( kill(9,$pid), 'kill ' . $pid ); ok( waitpid($pid,0), 'waitpid' ); +