aboutsummaryrefslogtreecommitdiffstats
path: root/lib/asn1/test/testSeqExtension.erl
blob: f7885cb002efbd6ed5ba8c504a6a549a1c93b8ab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1997-2016. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
%%
-module(testSeqExtension).

-include("External.hrl").
-export([main/3]).

-include_lib("common_test/include/ct.hrl").

-record('SeqExt1',{}).
-record('SeqExt2',{bool, int}).
-record('SeqExt3',{bool, int}).
-record('SeqExt4',{bool, int}).
-record('SeqExt5',{name, shoesize}).
-record('SeqExt6',{i1,i2,i3,i4,i5,i6,i7}).
-record('SuperSeq',{s1,s2,s3,s4,s5,s6,i}).

main(Erule, DataDir, Opts) ->
    roundtrip('SeqExt1', #'SeqExt1'{}),

    roundtrip('SeqExt2', #'SeqExt2'{bool=true,int=99}),
    roundtrip('SeqExt2', #'SeqExt2'{bool=false,int=42}),

    roundtrip('SeqExt3', #'SeqExt3'{bool=true,int=-77777}),
    roundtrip('SeqExt3', #'SeqExt3'{bool=false,int=-42000}),

    roundtrip('SeqExt4', #'SeqExt4'{bool=true,int=12345}),
    roundtrip('SeqExt4', #'SeqExt4'{bool=false,int=123456}),

    roundtrip('SeqExt5', #'SeqExt5'{name = <<"Arne">>,shoesize=47}),

    %% Encode a value with this version of the specification.
    BigInt = 128638468966,
    SuperSeq = #'SuperSeq'{s1=#'SeqExt1'{},
			   s2=#'SeqExt2'{bool=true,int=2345},
			   s3=#'SeqExt3'{bool=false,int=17},
			   s4=#'SeqExt4'{bool=true,int=38739739},
			   s5=#'SeqExt5'{name = <<"Arne">>,shoesize=47},
			   s6=#'SeqExt6'{i1=531,i2=601,i3=999,
					 i4=777,i5=11953,
					 i6=13553,i7=77777},
			   i=BigInt
			  },
    {ok,SuperSeqEnc} = 'SeqExtension':encode('SuperSeq', SuperSeq),
    {ok,SuperSeq} = 'SeqExtension':decode('SuperSeq', SuperSeqEnc),

    %% Remove all extensions from the ASN.1 specification and compile it.
    CaseDir = filename:dirname(code:which('SeqExtension')),
    Asn1SrcBase = "SeqExtension.asn1",
    Asn1SrcFile0 = filename:join(DataDir, Asn1SrcBase),
    {ok,Src0} = file:read_file(Asn1SrcFile0),
    %% Remove all declarations following "...," up to the end
    %% of the SEQUENCE.
    Src1 = re:replace(Src0, "[.][.][.],[^}]*", "...\n",
		      [global,{return,binary}]),
    %% Remove the last double bracket group in the SEQUENCE.
    Src = re:replace(Src1, ",\\s*\\[\\[.*?\\]\\]\\s*\\}", "\n}",
		     [global,{return,binary}]),
    io:format("~s\n\n", [Src]),
    Asn1SrcFile = filename:join(CaseDir, Asn1SrcBase),
    ok = file:write_file(Asn1SrcFile, Src),
    ok = asn1ct:compile(Asn1SrcFile,
			[{i,DataDir},{outdir,CaseDir}|Opts]),

    %% Decode the encoded sequence with the version of the spec
    %% with no extensions following the extension marks
    %% (except in SeqExt6). The integer 'i' at the end
    %% of the sequence must still be the correct integer (otherwise
    %% some extension has not been skipped correctly).
    {ok,DecodedSuperSeq} = 'SeqExtension':decode('SuperSeq', SuperSeqEnc),
    #'SuperSeq'{s1={'SeqExt1'},
		s2=#'SeqExt2'{bool=true,int=2345},
		s3={'SeqExt3'},
		s4={'SeqExt4',true},
		s5={'SeqExt5'},
		s6={'SeqExt6',531,601,999,777,11953},
		i=BigInt} = DecodedSuperSeq,


    %% Test more than 64 extensions.
    roundtrip2('SeqExt66',
	       list_to_tuple(['SeqExt66'|lists:seq(0, 65)])),
    v_roundtrip2(Erule, 'SeqExt66',
		 list_to_tuple(['SeqExt66'|
				lists:duplicate(65, asn1_NOVALUE)++[125]])),
    roundtrip2('SeqExt130',
	       list_to_tuple(['SeqExt130'|lists:seq(0, 129)])),
    v_roundtrip2(Erule, 'SeqExt130',
		 list_to_tuple(['SeqExt130'|
				lists:duplicate(129, asn1_NOVALUE)++[199]])),
    ok.

roundtrip(Type, Value) ->
    asn1_test_lib:roundtrip('SeqExtension', Type, Value).

v_roundtrip2(Erule, Type, Value) ->
    Encoded = asn1_test_lib:hex_to_bin(v(Erule, Type)),
    Encoded = roundtrip2(Type, Value).

roundtrip2(Type, Value) ->
    asn1_test_lib:roundtrip_enc('SeqExtension2', Type, Value).

v(ber, 'SeqExt66') ->  "30049F41 017D";
v(per, 'SeqExt66') ->  "C0420000 00000000 00004001 FA";
v(uper, 'SeqExt66') -> "D0800000 00000000 00101FA0";
v(ber, 'SeqExt130') ->  "30069F81 010200C7";
v(per, 'SeqExt130') ->  "C0808200 00000000 00000000 00000000 00000040 01C7";
v(uper, 'SeqExt130') -> "E0208000 00000000 00000000 00000000 0000101C 70".