1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2001-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(testCompactBitString).
22
23-export([compact_bit_string/1, bit_string_unnamed/1,otp_4869/1,
24	 ticket_7734/1]).
25
26compact_bit_string(Rules) ->
27
28    %%==========================================================
29    %% Bs1 ::= BIT STRING
30    %%==========================================================
31
32    roundtrip('Bs1', 0, {0,<<>>}),
33    roundtrip('Bs1', 4, {5,<<2#00100000>>}),
34    roundtrip('Bs1', 15, {4,<<2#11110000>>}),
35    roundtrip('Bs1', 255, {0,<<2#11111111>>}),
36    roundtrip('Bs1', 256, {7,<<16#00,16#80>>}),
37    roundtrip('Bs1', 257, {7,<<16#80,16#80>>}),
38    roundtrip('Bs1', 444, {7,<<16#3D,16#80>>}),
39    roundtrip('Bs1', 12345678901234567890,
40	      {0,<<75,80,248,215,49,149,42,213>>}),
41
42    roundtrip('Bs1', [1,1,1,1,1,1,1,1], {0,<<255>>}),
43
44    (Rules =/= jer) andalso roundtrip('Bs1', [0,1,0,0,1,0], {2,<<16#48>>}),
45    (Rules =/= jer) andalso roundtrip('Bs1', [1,0,0,0,0,0,0,0,0], {7,<<16#80,0>>}),
46    roundtrip('Bs1', [0,1,0,0,1,0,1,1,1,1,1,0,0,0,1,0,0,1,1], {5,<<75,226,96>>}),
47
48    case Rules of
49	ber ->
50	    {ok,{4,<<73,32>>}} =
51		'PrimStrings':decode('Bs1', <<35,8,3,2,0,73,3,2,4,32>>),
52	    {ok,{7,<<234,156,0>>}} =
53		'PrimStrings':decode('Bs1', <<35,9,3,2,0,234,3,3,7,156,0>>),
54	    {ok,{4,<<73,32>>}} =
55		'PrimStrings':decode('Bs1', <<35,128,3,2,0,73,3,2,4,32,0,0>>),
56	    {ok,{7,<<234,156,0>>}} =
57		'PrimStrings':decode('Bs1',
58				     <<35,128,3,2,0,234,3,3,7,156,0,0,0>>);
59	_ ->
60	    ok
61    end,
62
63    %% Test OTP-4200
64    roundtrip('Bs1',  {0,<<0,0,1,1>>}),
65
66    %%==========================================================
67    %% Bs2 ::= BIT STRING {su(0), mo(1), tu(2), we(3), th(4), fr(5), sa(6) } (SIZE (7))
68    %%==========================================================
69
70    roundtrip('Bs2', [mo,tu,fr]),
71    roundtrip('Bs2', [0,1,1,0,0,1,0], [mo,tu,fr]),
72
73    %%==========================================================
74    %% Bs3 ::= BIT STRING {su(0), mo(1), tu(2), we(3), th(4), fr(5), sa(6) } (SIZE (1..7))
75    %%==========================================================
76
77    roundtrip('Bs3', [mo,tu,fr]),
78    (Rules =/= jer) andalso roundtrip('Bs3', [0,1,1,0,0,1,0], [mo,tu,fr]),
79
80    %%==========================================================
81    %% BsPri ::= [PRIVATE 61] BIT STRING
82    %%==========================================================
83
84    roundtrip('BsPri', 45, {2,<<180>>}),
85    roundtrip('BsPri', 211, {0,<<203>>}),
86
87    case Rules of
88	ber ->
89	    {ok,{5,<<75,226,96>>}} =
90		'PrimStrings':decode('BsPri',
91				     <<223,61,4,5,75,226,96>>),
92
93	    {ok,{5,<<75,226,96>>}} =
94		'PrimStrings':decode('BsPri',
95				     <<255,61,128,3,4,5,75,226,96,0,0>>),
96
97	    {ok,{5,<<75,226,96>>}} =
98		'PrimStrings':decode('BsPri',
99				     <<255,61,9,3,2,0,75,3,3,5,226,96>>),
100
101	    {ok,{5,<<75,226,96>>}} =
102		'PrimStrings':decode('BsPri',
103				     <<255,61,128,3,2,0,75,3,3,5,226,96,0,0>>),
104	    ok;
105	_ ->
106	    ok
107    end,
108
109
110    %%==========================================================
111    %% BsExpPri ::= [PRIVATE 61] EXPLICIT BIT STRING
112    %%==========================================================
113
114    roundtrip('BsExpPri', 45, {2,<<180>>}),
115    roundtrip('BsExpPri', 211, {0,<<203>>}),
116
117    case Rules of
118	ber ->
119	    {ok,{5,<<75,226,96>>}} =
120		'PrimStrings':decode('BsExpPri', <<255,61,6,3,4,5,75,226,96>>);
121	_ ->
122	    ok
123    end,
124
125    ok.
126
127ticket_7734(_) ->
128    BS = {0,list_to_binary(lists:duplicate(128, 0))},
129    roundtrip('BS1024', BS).
130
131bit_string_unnamed(jer) -> ok;
132bit_string_unnamed(_Rules) ->
133    roundtrip('TransportLayerAddress', [0,1,1,0], {4,<<96>>}).
134
135otp_4869(per) ->
136    Val1 = {'IP',[0],{0,<<62,235,90,50,0,0,0,0,0,0,0,0,0,0,0,0>>},asn1_NOVALUE},
137    Val2 = {'IP',[0],[0,0,1,1,1,1,1,0,1,1,1,0,1,0,1,1,0,1,0,1,1,0,
138		      1,0,0,0,1,1,0,0,1,0] ++
139	    lists:duplicate(128 - 32, 0),asn1_NOVALUE},
140    {ok,Encoded} = 'Constraints':encode('IP', Val1),
141    {ok,Encoded} = 'Constraints':encode('IP', Val2),
142    ok;
143otp_4869(_) ->
144    ok.
145
146roundtrip(Type, Val) ->
147    roundtrip_1('PrimStrings', Type, Val, Val).
148
149roundtrip(Type, Val1, Val2) ->
150    roundtrip_1('PrimStrings', Type, Val1, Val2).
151
152roundtrip_1(Mod, Type, In, Out) ->
153    {ok,Encoded} = Mod:encode(Type, In),
154    {ok,Out} = Mod:decode(Type, Encoded),
155    %% Test that compact BIT STRINGs can be encoded.
156    {ok,Encoded} = Mod:encode(Type, Out),
157    ok.
158