d8bcbd4497b35156f62774c3e69d2ba2806c1662
[perl-cwmp.git] / t / 30-server.t
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 my $debug = shift @ARGV;
6
7 use Test::More tests => 19;
8 use Data::Dump qw/dump/;
9 use Cwd qw/abs_path/;
10 use blib;
11
12 BEGIN {
13         use_ok('Net::HTTP');
14         use_ok('CWMP::Server');
15         use_ok('CWMP::Store');
16 }
17
18 my $port = 4242;
19
20 eval {
21         $SIG{ALRM} = sub { die; };
22         alarm 30;
23 };
24
25 ok(my $abs_path = abs_path($0), "abs_path");
26 $abs_path =~ s!/[^/]*$!/!;      #!fix-vim
27
28 my $store_path = "$abs_path/var/";
29 #my $store_module = 'DBMDeep';
30 my $store_module = 'YAML';
31
32 ok( my $server = CWMP::Server->new({
33         debug => $debug,
34         port => $port,
35         store => {
36                 module => $store_module,
37                 path => $store_path,
38                 clean => 1,
39         },
40 }), 'new' );
41 isa_ok( $server, 'CWMP::Server' );
42
43 my $pid;
44
45 if ( $pid = fork ) {
46         ok( $pid, 'fork ');
47         diag "forked $pid";
48 } elsif (defined($pid)) {
49         # child
50         $server->run;
51         exit;
52 } else {
53         die "can't fork";
54 }
55
56 sleep 1;        # so server can start
57
58 ok( my $s = Net::HTTP->new(Host => "localhost:$port"), 'client' );
59 $s->keep_alive( 1 );
60
61 ok( $s->write_request(
62         POST => '/',
63         'Transfer-Encoding' => 'chunked',
64         'SOAPAction' => '',
65         'Content-Type' => 'text/xml',
66 ), 'write_request' );
67
68 foreach my $chunk (qq{
69
70 <soapenv:Envelope soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cwmp="urn:dslforum-org:cwmp-1-0">
71  <soapenv:Header>
72 <cwmp:ID soapenv:mustUnderstand="1">1_THOM_TR69_ID</cwmp:ID>
73  </soapenv:Header>
74  <soapenv:Body>
75 <cwmp:Inform>
76 <DeviceId>
77  <Manufacturer>THOMSON</Manufacturer>
78  <OUI>00147F</OUI>
79  <ProductClass>SpeedTouch 780</ProductClass>
80  <SerialNumber>CP0644JTHJ4</SerialNumber>
81 </DeviceId>
82 <Event soap:arrayType="cwmp:EventStruct[03]">
83 <EventStruct>
84  <EventCode>0 BOOTSTRAP</EventCode>
85  <CommandKey></CommandKey>
86 </EventStruct>
87 <EventStruct>
88  <Event},qq{Code>1 BOOT</EventCode>
89  <CommandKey></CommandKey>
90 </EventStruct>
91 <EventStruct>
92  <EventCode>4 VALUE CHANGE</EventCode>
93  <CommandKey></CommandKey>
94 </EventStruct>
95 </Event>
96 <MaxEnvelopes>2</MaxEnvelopes>
97 <CurrentTime>1970-01-01T00:04:33Z</CurrentTime>
98 <RetryCount>01</RetryCount>},qq{
99 <ParameterList soap:arrayType="cwmp:ParameterValueStruct[12]">
100 <ParameterValueStruct>
101  <Name>InternetGatewayDevice.DeviceSummary</Name>
102  <Value xsi:type="xsd:string">InternetGatewayDevice:1.1[] (Baseline:1, EthernetLAN:1, ADSLWAN:1, Bridging:1, Time:1, WiFiLAN:1)</Value>
103 </ParameterValueStruct>
104 <ParameterValueStruct>
105  <Name>}, qq{
106 InternetGatewayDevice.DeviceInfo.SpecVersion</Name>
107  <Value xsi:type="xsd:string">1.1</Value>
108 </ParameterValueStruct>
109 <ParameterValueStruct>
110  <Name>InternetGatewayDevice.DeviceInfo.HardwareVersion</Name>
111  <Value xsi:type="xsd:string">BANT-R</Value>
112 </ParameterValueStruct>
113 <ParameterValueStruct>
114  <Name>InternetGatewayDevice.DeviceInfo.SoftwareVersion</Name>
115  <Value xsi:type="xsd:string">6.2.15.5</Value>
116 </ParameterValueStruct>
117 <ParameterValueStruct>
118  <Name>InternetGatewayDevice.DeviceInfo.ProvisioningCode</Name>
119  <Value xsi:type="xsd:string"></Value>
120 </ParameterValueStruct>
121 <ParameterValueStruct>
122  <Name>InternetGatewayDevice.DeviceInfo.VendorConfigFile.1.Name</Name>
123  <Value xsi:type="xsd:string">Routed PPPoE on 0/35 and 8/35</Value>
124 </ParameterValueStruct>
125 <ParameterValueStruct>
126  <Name>InternetGatewayDevice.DeviceInfo.VendorConfigFile.1.Version</Name>
127  <Value xsi:type="xsd:string"></Value>
128 </ParameterValueStruct>
129 <ParameterValueStruct>
130  <Name>InternetGatewayDevice.DeviceInfo.VendorConfigFile.1.Date</Name>
131  <Value xsi:type="xsd:dateTime">0000-00-00T00:00:00</Value>
132 </ParameterValueStruct>
133 <ParameterValueStruct>
134  <Name>InternetGatewayDevice.DeviceInfo.VendorConfigFile.1.Description</Name>
135  <Value xsi:type="xsd:string">Factory Defaults</Value>
136 </ParameterValueStruct>
137 <ParameterValueStruct>
138  <Name>InternetGatewayDevice.ManagementServer.ConnectionRequestURL</Name>
139  <Value}, qq{ xsi:type="xsd:string">http://192.168.1.254:51005/</Value>
140 </ParameterValueStruct>
141 <ParameterValueStruct>
142  <Name>InternetGatewayDevice.ManagementServer.ParameterKey</Name>
143  <Value xsi:type="xsd:string"></Value>
144 </ParameterValueStruct>
145 <ParameterValueStruct>
146  <Name>.ExternalIPAddress</Name>
147  <Value xsi:type="xsd:string">192.168.1.254</Value>
148 </ParameterValueStruct>
149 </ParameterList>
150 </cwmp:Inform>
151  </soapenv:Body>
152 </soapenv:Envelope>
153 } ) {
154         ok( $s->write_chunk( $chunk ), "chunk " . length($chunk) . " bytes" );
155 }
156 ok( $s->write_chunk_eof, 'write_chunk_eof' );
157
158 sleep 1;
159
160 ok( my $store = CWMP::Store->new({ module => $store_module, path => $store_path, debug => $debug }), 'another store' );
161
162 my $state = {
163   CurrentTime    => "1970-01-01T00:04:33Z",
164   DeviceID       => {
165                       Manufacturer => "THOMSON",
166                       OUI => "00147F",
167                       ProductClass => "SpeedTouch 780",
168                       SerialNumber => "CP0644JTHJ4",
169                     },
170   EventStruct    => ["0 BOOTSTRAP", "1 BOOT", "4 VALUE CHANGE"],
171   ID             => "1_THOM_TR69_ID",
172   MaxEnvelopes   => 2,
173 #  NoMoreRequests => undef,
174   Parameter      => {
175                       "\nInternetGatewayDevice.DeviceInfo.SpecVersion"                  => "1.1",
176                       ".ExternalIPAddress"                                              => "192.168.1.254",
177                       "InternetGatewayDevice.DeviceInfo.HardwareVersion"                => "BANT-R",
178                       "InternetGatewayDevice.DeviceInfo.ProvisioningCode"               => undef,
179                       "InternetGatewayDevice.DeviceInfo.SoftwareVersion"                => "6.2.15.5",
180                       "InternetGatewayDevice.DeviceInfo.VendorConfigFile.1.Date"        => "0000-00-00T00:00:00",
181                       "InternetGatewayDevice.DeviceInfo.VendorConfigFile.1.Description" => "Factory Defaults",
182                       "InternetGatewayDevice.DeviceInfo.VendorConfigFile.1.Name"        => "Routed PPPoE on 0/35 and 8/35",
183                       "InternetGatewayDevice.DeviceInfo.VendorConfigFile.1.Version"     => undef,
184                       "InternetGatewayDevice.DeviceSummary"                             => "InternetGatewayDevice:1.1[] (Baseline:1, EthernetLAN:1, ADSLWAN:1, Bridging:1, Time:1, WiFiLAN:1)",
185                       "InternetGatewayDevice.ManagementServer.ConnectionRequestURL"     => "http://192.168.1.254:51005/",
186                       "InternetGatewayDevice.ManagementServer.ParameterKey"             => undef,
187                     },
188   RetryCount     => "01",
189   _dispatch      => "InformResponse",
190 };
191
192 is_deeply( $store->current_store->get_state( 'CP0644JTHJ4' ), $state, 'store->current_store->get_state' );
193
194 ok( kill(9,$pid), 'kill ' . $pid );
195
196 ok( waitpid($pid,0), 'waitpid' );