aboutsummaryrefslogtreecommitdiffstats
path: root/lib/asn1/test/testPrim.erl
blob: a5d34f5f08332799ea94b5463b99a321febbb3c2 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1997-2017. 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(testPrim).

-export([bool/1]).
-export([int/1]).
-export([enum/1]).
-export([obj_id/1]).
-export([rel_oid/1]).
-export([null/1]).
-export([real/1]).

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

bool(Rules) ->
    Types = ['Bool','BoolCon','BoolPri','BoolApp',
	     'BoolExpCon','BoolExpPri','BoolExpApp'],
    [roundtrip(T, V) || T <- Types, V <- [true,false]],
    Tag = case Rules of
              ber -> encode_boolean;
              _ -> illegal_boolean
          end,
    [{Tag,517} = enc_error(T, 517) || T <- Types],
    ok.


int(Rules) ->
    %% OTP-2666: encoding should use minimum number of octets; x.690 8.3.2
    Bytes0 = roundtrip('Int', -128),
    case Rules of
	ber ->
	    <<_,1,128>> = Bytes0;
	_ ->
	    ok
    end,

    Values = [0,2,3,4,127,128,254,255,256,257,444,
	      16383,16384,16385,65534,65535,65536,65537,
	      123456789,12345678901234567890,
	      -1,-2,-3,-4,-100,-127,-255,-256,-257,
	      -1234567890,-2147483648],
    Types = ['Int','IntCon','IntPri','IntApp',
             'IntExpCon','IntExpPri','IntExpApp'],
    _ = [roundtrip(T, V) || T <- Types, V <- [1|Values]],
    Tag = case Rules of
              ber -> encode_integer;
              _ -> illegal_integer
          end,
    _ = [{Tag,V} = enc_error(T, V) ||
            T <- Types, V <- [atom,42.0,{a,b,c}]],
    case Rules of
        ber ->
            ok;
        _ ->
            _ = [{Tag,V} = enc_error('IntConstrained', V) ||
                    V <- [atom,-1,256,42.0]]
    end,

    %%==========================================================
    %% IntEnum ::=  INTEGER {first(1),last(31)} 
    %%==========================================================

    [roundtrip('IntEnum', V) || V <- Values],

    roundtrip('IntEnum', first),
    roundtrip('IntEnum', last),

    roundtrip('ASeq', {'ASeq',true,254,false,255,true,256,true,68789}),
    roundtrip('ASeq', {'ASeq',false,250,true,200,true,199,true,77788}),
    roundtrip('ASeq', {'ASeq',true,0,false,0,true,0,true,68789}),

    %%==========================================================
    %% Longitude ::= INTEGER {
    %%  oneMicrodegreeEast(10),
    %%  oneMicrodegreeWest(-10),
    %%  unavailable(1800000001)
    %% } (-1799999999..1800000001)
    %%==========================================================

    Enc10 = encoding(Rules, oneMicrodegreeEast),
    Enc10 = roundtrip('Longitude', oneMicrodegreeEast),
    Enc10 = roundtrip('Longitude', 10, oneMicrodegreeEast),

    Enc20 = encoding(Rules, oneMicrodegreeWest),
    Enc20 = roundtrip('Longitude', oneMicrodegreeWest),
    Enc20 = roundtrip('Longitude', -10, oneMicrodegreeWest),

    Enc30 = roundtrip('Longitude', unavailable),
    Enc30 = roundtrip('Longitude', 1800000001, unavailable),

    ok.

encoding(Rules, Type) ->
    asn1_test_lib:hex_to_bin(encoding_1(Rules, Type)).

encoding_1(ber, oneMicrodegreeEast) -> "02010A";
encoding_1(per, oneMicrodegreeEast) -> "C06B49D2 09";
encoding_1(uper, oneMicrodegreeEast) -> "6B49D209";

encoding_1(ber, oneMicrodegreeWest) -> "0201F6";
encoding_1(per, oneMicrodegreeWest) -> "C06B49D1 F5";
encoding_1(uper, oneMicrodegreeWest) -> "6B49D1F5".

enum(Rules) ->

    %%==========================================================
    %% Enum ::=  ENUMERATED {monday(1),tuesday(2),wednesday(3),thursday(4),
    %%                       friday(5),saturday(6),sunday(7)}
    %%==========================================================

    roundtrip('Enum', monday),
    roundtrip('Enum', thursday),
    Tag = case Rules of
              ber -> enumerated_not_in_range;
              _ -> illegal_enumerated
          end,
    {Tag,4} = enc_error('Enum', 4),

    case Rules of
	Per when Per =:= per; Per =:= uper ->
	    <<0>> = roundtrip('SingleEnumVal', true),
	    <<0>> = roundtrip('SingleEnumValExt', true);
	ber ->
	    ok
    end,

    roundtrip('NegEnumVal', neg),
    roundtrip('NegEnumVal', zero),
    roundtrip('EnumVal128', val),

    ok.


obj_id(_) ->

    %%==========================================================
    %% ObjId ::= OBJECT IDENTIFIER
    %%==========================================================

    [roundtrip('ObjId', V) ||
	V <- [{0,22,3},{1,39,3},{2,100,3},{2,16303,3},{2,16304,3}]],
    ok.

rel_oid(_Rules) ->
    %%==========================================================
    %% RelOid ::= RELATIVE-OID
    %%==========================================================

    [roundtrip('RelOid', V) ||
	V <- [{0,22,3},{1,39,3},{2,100,3},{2,16303,3},
	      {2,16304,3},{8,16304,16#ffff}]],
    ok.


null(_Rules) ->

    %%==========================================================
    %% Null ::= NULL
    %%==========================================================
    roundtrip('Null', monday, 'NULL'),
    ok.

roundtrip(T, V) ->
    roundtrip(T, V, V).

roundtrip(Type, Value, ExpectedValue) ->
    case get(no_ok_wrapper) of
	false ->
	    asn1_test_lib:roundtrip_enc('Prim', Type, Value, ExpectedValue);
	true ->
	    M = 'Prim',
	    Enc = M:encode(Type, Value),
	    ExpectedValue = M:decode(Type, Enc),
	    Enc
    end.

enc_error(T, V) ->
    case get(no_ok_wrapper) of
	false ->
	    {error,{asn1,{Reason,Stk}}} = 'Prim':encode(T, V),
            [{_,_,_,_}|_] = Stk,
            Reason;
	true ->
	    try 'Prim':encode(T, V) of
		_ ->
		    ?t:fail()
	    catch
		_:{error,{asn1,Reason}} ->
		    Reason
	    end
    end.

real(_Rules) ->
    %%==========================================================
    %% AngleInRadians ::= REAL
    %%==========================================================
    
    %% Base 2
    real_roundtrip('AngleInRadians', {1,2,1}),
    real_roundtrip('AngleInRadians', {129,2,1}),
    real_roundtrip('AngleInRadians', {128,2,1}, {1,2,8}),
    real_roundtrip('AngleInRadians', {128,2,-7}, {1,2,0}),
    real_roundtrip('AngleInRadians', {16#f1f1f1,2,128}),

    %% Base 10, tuple format
    real_roundtrip('AngleInRadians', {1,10,1}, "1.E1"),
    real_roundtrip('AngleInRadians', {100,10,1}, "1.E3"),
    real_roundtrip('AngleInRadians', {-100,10,1}, "-1.E3"),
    real_roundtrip('AngleInRadians', {2,10,1}, "2.E1"),
    real_roundtrip('AngleInRadians', {123000,10,0}, "123.E3"),
    real_roundtrip('AngleInRadians', {123456789,10,123456789},
		   "123456789.E123456789" ),
    real_roundtrip('AngleInRadians', {-12345,10,-12345}, "-12345.E-12345"),

    %% Base 10, string format NR3

    real_roundtrip('AngleInRadians', "123.123E123", "123123.E120"),
    real_roundtrip('AngleInRadians', "0.0E0", "0.E+0"),
    real_roundtrip('AngleInRadians', "0.0123", "123.E-4"),
    real_roundtrip('AngleInRadians', "0", "0.E+0"),
    real_roundtrip('AngleInRadians', "-123.45", "-12345.E-2"),
    real_roundtrip('AngleInRadians', "123456789E123456789",
		   "123456789.E123456789"),
    real_roundtrip('AngleInRadians', "01.000E1", "1.E1"),
    real_roundtrip('AngleInRadians', "120.0001", "1200001.E-4"),

    ok.

real_roundtrip(T, V) ->
    real_roundtrip(T, V, V).

real_roundtrip(Type, Value, ExpectedValue) ->
    case get(no_ok_wrapper) of
	false ->
	    asn1_test_lib:roundtrip('Real', Type, Value, ExpectedValue);
	true ->
	    M = 'Real',
	    ExpectedValue = M:decode(Type, M:encode(Type, Value)),
	    ok
    end.