1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2003-2016. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20%%
21-module(test_partial_incomplete_decode).
22
23-export([test/1]).
24
25-include_lib("common_test/include/ct.hrl").
26
27test(Config) ->
28    FMsg = msg('F'),
29    Bytes1 = roundtrip('PartialDecSeq', 'F', FMsg),
30    {ok,IncFMsg} = 'PartialDecSeq':decode_F_fb_incomplete(Bytes1),
31    decode_parts('F', IncFMsg),
32    {ok,IncF2Msg} = 'PartialDecSeq':decode_F_fb_exclusive2(Bytes1),
33    decode_parts('F2', IncF2Msg),
34
35    DMsg = msg('D'),
36    Bytes2 = roundtrip('PartialDecSeq', 'D', DMsg),
37    {ok,IncDMsg} = 'PartialDecSeq':decode_D_incomplete(Bytes2),
38    decode_parts('D', IncDMsg),
39
40    F3Msg = msg('F3'),
41    BytesF3 = roundtrip('PartialDecSeq', 'F', F3Msg),
42    {ok,IncF3Msg} = 'PartialDecSeq':decode_F_fb_exclusive3(BytesF3),
43    decode_parts('F3', IncF3Msg),
44
45    AMsg = msg('A'),
46    Bytes3 = roundtrip('PartialDecSeq2', 'A', AMsg),
47    {ok,IncFMsg3} = 'PartialDecSeq2':decode_A_c_b_incomplete(Bytes3),
48    decode_parts('A', IncFMsg3),
49
50    MyHTTPMsg = msg('GetRequest'),
51    Bytes4 = roundtrip('PartialDecMyHTTP', 'GetRequest', MyHTTPMsg),
52    {ok,IncFMsg4} = 'PartialDecMyHTTP':decode_GetRequest_incomplete(Bytes4),
53    decode_parts('GetRequest', IncFMsg4),
54
55    MsgS1_1 = msg('S1_1'),
56    Bytes5 = roundtrip('PartialDecSeq3', 'S1', MsgS1_1),
57    {ok,IncFMsg5} = 'PartialDecSeq3':decode_S1_incomplete(Bytes5),
58    decode_parts('S1_1', IncFMsg5),
59
60    MsgS1_2 = msg('S1_2'),
61    Bytes6 = roundtrip('PartialDecSeq3', 'S1', MsgS1_2),
62    {ok,IncFMsg6} = 'PartialDecSeq3':decode_S1_incomplete(Bytes6),
63    decode_parts('S1_2', IncFMsg6),
64
65    %% test of MEDIA-GATEWAY-CONTROL
66    test_megaco(Config),
67    ok.
68
69test_megaco(Config) ->
70    DataDir = proplists:get_value(data_dir, Config),
71    Files = filelib:wildcard(filename:join([DataDir,megacomessages,"*.val"])),
72    Mod = 'MEDIA-GATEWAY-CONTROL',
73    lists:foreach(fun(File) ->
74			  {ok,Bin} = file:read_file(File),
75			  V = binary_to_term(Bin),
76			  T = element(1, V),
77			  Enc = roundtrip(Mod, T, V),
78			  exclusive_decode(Enc, File)
79		  end, Files).
80
81exclusive_decode(Bin,F) ->
82    Mod='MEDIA-GATEWAY-CONTROL',
83    io:format("Encoding message: ~p~n",[F]),
84    {ok,{_,_,{_,_VsnNo,{MsgMidKey,MsgMid},{MsgMBodyKey,MsgMBody}}}}=
85	Mod:decode_MegacoMessage_exclusive(Bin),
86    {ok,_} = Mod:decode_part(MsgMidKey,MsgMid),
87    {ok,_} = Mod:decode_part(MsgMBodyKey,MsgMBody),
88    ok.
89
90decode_parts('F',PartDecMsg) ->
91    {fb,{'E',35,{NameE_b,ListBinE_b},false,{NameE_d,BinE_d}}} = PartDecMsg,
92    {ok,[{'D',3,true}|_]} = 'PartialDecSeq':decode_part(NameE_b,ListBinE_b),
93    {ok,{'D',3,true}} = 'PartialDecSeq':decode_part(NameE_b,
94							  hd(ListBinE_b)),
95    {ok,{da,[{'A',16,{'D',17,true}}]}} =
96	'PartialDecSeq':decode_part(NameE_d,BinE_d),
97    ok;
98decode_parts('F2',PartDecMsg) ->
99    {fb,{'E',35,{E_bkey,E_b},false,{da,{E_d_akey,E_d_a}}}} = PartDecMsg,
100    {ok,[{'D',3,true},{'D',4,false},{'D',5,true},{'D',6,true},{'D',7,false},{'D',8,true},{'D',9,true},{'D',10,false},{'D',11,true},{'D',12,true},{'D',13,false},{'D',14,true}]} = 'PartialDecSeq':decode_part(E_bkey,E_b),
101    {ok,[{'A',16,{'D',17,true}}]} = 'PartialDecSeq':decode_part(E_d_akey,E_d_a);
102
103decode_parts('F3',PartDecMsg) ->
104    {fb,{'E',10,{E_bkey,E_b},false,{dc,{'E_d_dc',13,true,{E_d_dc_dcckey,E_d_dc_dcc}}}}} = PartDecMsg,
105    {ok,[{'D',11,true},{'D',12,false}]} = 'PartialDecSeq':decode_part(E_bkey,E_b),
106    {ok,{'E_d_dc_dcc',14,15}} = 'PartialDecSeq':decode_part(E_d_dc_dcckey,E_d_dc_dcc);
107
108
109decode_parts('D',PartDecMsg) ->
110    {'D',{NameD_a,BinD_a},true} = PartDecMsg,
111    {ok,123} = 'PartialDecSeq':decode_part(NameD_a,BinD_a),
112    ok;
113decode_parts('A',PartDecMsg) ->
114    {'A',12,{c,{'S',true,false}},{b,{NameA_c_b,BinA_c_b}}} = PartDecMsg,
115    {ok,{'A_c_b',false,false}} =
116	'PartialDecSeq2':decode_part(NameA_c_b,BinA_c_b),
117    ok;
118decode_parts('GetRequest',PartDecMsg) ->
119    {'GetRequest',true,false,
120	   {'AcceptTypes',[html,'plain-text',gif,jpeg],
121	    {NameAcceptTypes_others,ListBinAcceptTypes_others}},
122	   "IamfineThankYOu"} = PartDecMsg,
123    {ok,["hell","othe","reho","peyo","uare","fine"]} =
124	'PartialDecMyHTTP':decode_part(NameAcceptTypes_others,
125				       ListBinAcceptTypes_others),
126    {ok,"hell"} =
127	'PartialDecMyHTTP':decode_part(NameAcceptTypes_others,
128				       hd(ListBinAcceptTypes_others)),
129    ok;
130decode_parts('S1_1',PartDecMsg) ->
131    {'S1',14,{'S2',false,12,{NameS2c,BinS2c}},
132	   {_,{NameS1c_a,ListBinS1c_a}},{NameS1d,BinS1d}} = PartDecMsg,
133    {ok,[{'S3',10,"PrintableString","OCTETSTRING",
134		[one,two,three,four]}|_Rest1]} =
135	'PartialDecSeq3':decode_part(NameS2c,BinS2c),
136    {ok,[{'S3',10,"PrintableString","OCTETSTRING",
137		[one,two,three,four]}|_Rest2]} =
138	'PartialDecSeq3':decode_part(NameS1c_a,ListBinS1c_a),
139    {ok,{'S3',10,"PrintableString","OCTETSTRING",
140	       [one,two,three,four]}} =
141	'PartialDecSeq3':decode_part(NameS1c_a,hd(ListBinS1c_a)),
142    {ok,[{'Name',"Hans","HCA","Andersen"}|_Rest3]} =
143	'PartialDecSeq3':decode_part(NameS1d,BinS1d),
144    ok;
145decode_parts('S1_2',PartDecMsg) ->
146    {'S1',14,{'S2',false,12,_S2c},S1c_b,{NameS1d,BinS1d}} = PartDecMsg,
147    {b,{'C1_b',11,true,
148	      {'S4',{'Name',"Hans","HCA","Andersen"},"MSc"}}}=S1c_b,
149    {ok,[{'Name',"Hans","HCA","Andersen"}|_Rest3]} =
150	'PartialDecSeq3':decode_part(NameS1d,BinS1d),
151    ok.
152
153
154
155msg('F') ->
156    {fb,{'E',35,[{'D',3,true},{'D',4,false},{'D',5,true},{'D',6,true},{'D',7,false},{'D',8,true},{'D',9,true},{'D',10,false},{'D',11,true},{'D',12,true},{'D',13,false},{'D',14,true}],false,{da,[{'A',16,{'D',17,true}}]}}};
157
158msg('F3') ->
159    {fb,{'E',10,[{'D',11,true},{'D',12,false}],false,{dc,{'E_d_dc',13,true,{'E_d_dc_dcc',14,15}}}}};
160
161msg('D') ->
162    {'D',123,true};
163
164msg('A') ->
165    {'A',12,{c,{'S',true,false}},{b,{'A_c_b',false,false}}};
166
167msg('GetRequest') ->
168    {'GetRequest',true,false,
169     {'AcceptTypes',[html,'plain-text',gif,jpeg],
170      ["hell","othe","reho","peyo","uare","fine"]},
171     "IamfineThankYOu"};
172
173msg('S1_1') ->
174    {'S1',14,msg('S2'),msg('C1_a'),msg('SO1')};
175msg('S1_2') ->
176    {'S1',14,msg('S2'),msg('C1_b'),msg('SO1')};
177msg('S2') ->
178    {'S2',false,12,[msg('S3'),msg('S3'),msg('S3')]};
179msg('C1_a') ->
180    {a,[msg('S3'),msg('S3'),msg('S3')]};
181msg('C1_b') ->
182    {b,{'C1_b',11,true,msg('S4')}};
183msg('S3') ->
184    {'S3',10,"PrintableString","OCTETSTRING",[one,two,three,four]};
185msg('S4') ->
186    {'S4',msg('Name'),"MSc"};
187msg('SO1') ->
188    [msg('Name'),msg('Name'),msg('Name')];
189msg('Name') ->
190    {'Name',"Hans","HCA","Andersen"}.
191
192roundtrip(M, T, V) ->
193    asn1_test_lib:roundtrip_enc(M, T, V).
194