1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2003-2020. 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%%----------------------------------------------------------------------
22%% Purpose:
23%%----------------------------------------------------------------------
24-module(snmp_pdus_SUITE).
25
26
27%%----------------------------------------------------------------------
28%% Include files
29%%----------------------------------------------------------------------
30-include_lib("common_test/include/ct.hrl").
31-include("snmp_test_lib.hrl").
32-include_lib("snmp/include/snmp_types.hrl").
33
34
35%%----------------------------------------------------------------------
36%% External exports
37%%----------------------------------------------------------------------
38
39-export([
40         suite/0, all/0, groups/0,
41         init_per_suite/1,    end_per_suite/1,
42         init_per_group/2,    end_per_group/2,
43         init_per_testcase/2, end_per_testcase/2,
44
45	 otp7575/1,
46	 otp8563/1,
47	 otp9022/1,
48	 otp10132/1
49	]).
50
51
52%%======================================================================
53%% Common Test interface functions
54%%======================================================================
55
56suite() ->
57    [{ct_hooks, [ts_install_cth]}].
58
59all() ->
60    [{group, tickets}].
61
62groups() ->
63    [{tickets, [], tickets_cases()}].
64
65tickets_cases() ->
66    [
67     otp7575,
68     otp8563,
69     otp9022,
70     otp10132
71    ].
72
73
74%%
75%% -----
76%%
77
78init_per_suite(Config0) when is_list(Config0) ->
79    ?IPRINT("init_per_suite -> entry with"
80            "~n   Config: ~p", [Config0]),
81
82    case ?LIB:init_per_suite(Config0) of
83        {skip, _} = SKIP ->
84            SKIP;
85
86        Config1 when is_list(Config1) ->
87            %% We need a monitor on this node also
88            snmp_test_sys_monitor:start(),
89
90            ?IPRINT("init_per_suite -> end when"
91                    "~n      Config1: ~p", [Config1]),
92
93            Config1
94    end.
95
96end_per_suite(Config0) when is_list(Config0) ->
97    ?IPRINT("end_per_suite -> entry with"
98            "~n   Config: ~p", [Config0]),
99
100    snmp_test_sys_monitor:stop(),
101    Config1 = ?LIB:end_per_suite(Config0),
102
103    ?IPRINT("end_per_suite -> end"),
104
105    Config1.
106
107
108
109%%
110%% -----
111%%
112
113init_per_group(_GroupName, Config) ->
114    Config.
115
116end_per_group(_GroupName, Config) ->
117    Config.
118
119
120
121%%
122%% -----
123%%
124
125init_per_testcase(_Case, Config) when is_list(Config) ->
126    ?IPRINT("init_per_testcase -> entry with"
127            "~n   Config: ~p", [Config]),
128
129    snmp_test_global_sys_monitor:reset_events(),
130
131    ?IPRINT("init_per_testcase -> end"),
132
133    Config.
134
135end_per_testcase(_Case, Config) when is_list(Config) ->
136
137    ?IPRINT("end_per_testcase -> entry with"
138            "~n   Config: ~p",
139            [Config]),
140
141    ?IPRINT("system events during test: ~p",
142            [snmp_test_global_sys_monitor:events()]),
143
144    Config.
145
146
147
148%%======================================================================
149%% Test functions
150%%======================================================================
151
152otp7575(suite) -> [];
153otp7575(doc) -> ["OTP-7575 - Message version"];
154otp7575(Config) when is_list(Config) ->
155    ?IPRINT("attempt to decode message with valid version"),
156    MsgWithOkVersion = <<48,39,2,1,0,4,6,112,117,98,108,105,99,160,26,2,2,1,49,2,1,0,2,1,0,48,14,48,12,6,8,43,6,1,2,1,1,5,0,5,0>>,
157    case (catch dec_message(MsgWithOkVersion)) of
158	Msg when is_record(Msg, message) ->
159	    ok;
160	Unexpected1 ->
161	    exit({unexpected_decode_result, 1, Unexpected1})
162    end,
163
164    ?IPRINT("attempt to decode message with bad version"),
165    MsgWithBadVersion = <<48,48,2,10,1,1,1,1,1,1,1,1,1,1,4,6,112,117,98,108,105,99,160,26,2,2,1,49,2,1,0,2,1,0,48,14,48,12,6,8,43,6,1,2,1,1,5,0,5,0>>,
166    case (catch dec_message(MsgWithBadVersion)) of
167	{'EXIT', {bad_version, BadVersion}} when is_integer(BadVersion) ->
168	    ok;
169	Unexpected2 ->
170	    exit({unexpected_decode_result, 2, Unexpected2})
171    end,
172
173    ?IPRINT("attempt to decode message with very bad version"),
174    MsgWithVeryBadVersion = <<48,49,2,11,1,1,1,1,1,1,1,1,1,1,1,4,6,112,117,98,108,105,99,160,26,2,2,1,49,2,1,0,2,1,0,48,14,48,12,6,8,43,6,1,2,1,1,5,0,5,0>>,
175    case (catch dec_message(MsgWithVeryBadVersion)) of
176	{'EXIT', {bad_version, {VersionSize, MaxVersionSize}}} when (VersionSize > MaxVersionSize) ->
177	    ok;
178	Unexpected3 ->
179	    exit({unexpected_decode_result, 3, Unexpected3})
180    end,
181    ?IPRINT("done"),
182    ok.
183
184
185otp8563(suite) -> [];
186otp8563(doc) -> ["OTP-8563 - Counter64"];
187otp8563(Config) when is_list(Config) ->
188    Val1 = 16#7fffffffffffffff,
189    ?IPRINT("try encode and decode value 1: ~w (0x~.16b)", [Val1, Val1]),
190    Enc1 = snmp_pdus:enc_value('Counter64', Val1),
191    ?IPRINT("  => ~w", [Enc1]),
192    {{'Counter64', Val1}, []} = snmp_pdus:dec_value(Enc1),
193
194    Val2 = Val1 + 1,
195    ?IPRINT("try encode and decode value 2: ~w (0x~.16b)", [Val2, Val2]),
196    Enc2 = snmp_pdus:enc_value('Counter64', Val2),
197    ?IPRINT("  => ~w", [Enc2]),
198    {{'Counter64', Val2}, []} = snmp_pdus:dec_value(Enc2),
199
200    Val3 = Val2 + 1,
201    ?IPRINT("try encode and decode value 3: ~w (0x~.16b)", [Val3, Val3]),
202    Enc3 = snmp_pdus:enc_value('Counter64', Val3),
203    ?IPRINT("  => ~w", [Enc3]),
204    {{'Counter64', Val3}, []} = snmp_pdus:dec_value(Enc3),
205
206    Val4 = 16#fffffffffffffffe,
207    ?IPRINT("try encode and decode value 4: ~w (0x~.16b)", [Val4, Val4]),
208    Enc4 = snmp_pdus:enc_value('Counter64', Val4),
209    ?IPRINT("  => ~w", [Enc4]),
210    {{'Counter64', Val4}, []} = snmp_pdus:dec_value(Enc4),
211
212    Val5 = Val4 + 1,
213    ?IPRINT("try encode and decode value 5: ~w (0x~.16b)", [Val5, Val5]),
214    Enc5 = snmp_pdus:enc_value('Counter64', Val5),
215    ?IPRINT("  => ~w", [Enc5]),
216    {{'Counter64', Val5}, []} = snmp_pdus:dec_value(Enc5),
217
218    Val6 = 16#ffffffffffffffff + 1,
219    ?IPRINT("try and fail to encode value 6: ~w (0x~.16b)", [Val6, Val6]),
220    case (catch snmp_pdus:enc_value('Counter64', Val6)) of
221	{'EXIT', {error, {bad_counter64, Val6}}} ->
222	    ok;
223	Unexpected6 ->
224	    ?IPRINT("  => ~w", [Unexpected6]),
225	    exit({unexpected_encode_result, Unexpected6, Val6})
226    end,
227
228    Val7 = -1,
229    ?IPRINT("try and fail to encode value 7: ~w", [Val7]),
230    case (catch snmp_pdus:enc_value('Counter64', Val7)) of
231	{'EXIT', {error, {bad_counter64, Val7}}} ->
232	    ok;
233	Unexpected7 ->
234	    ?IPRINT("  => ~w", [Unexpected7]),
235	    exit({unexpected_encode_result, Unexpected7, Val7})
236    end,
237
238    ok.
239
240
241otp9022(suite) -> [];
242otp9022(doc) -> ["OTP-9022 - Counter32"];
243otp9022(Config) when is_list(Config) ->
244    Val0 = 2908389204,
245    ?IPRINT("try encode and decode value 0: ~w (0x~.16b)", [Val0, Val0]),
246    Enc0 = snmp_pdus:enc_value('Counter32', Val0),
247    ?IPRINT("  => ~w", [Enc0]),
248    {{'Counter32', Val0}, []} = snmp_pdus:dec_value(Enc0),
249
250    Val1 = 0,
251    ?IPRINT("try encode and decode value 1: ~w (0x~.16b)", [Val1, Val1]),
252    Enc1 = snmp_pdus:enc_value('Counter32', Val1),
253    ?IPRINT("  => ~w", [Enc1]),
254    {{'Counter32', Val1}, []} = snmp_pdus:dec_value(Enc1),
255
256    Val2 = Val1 + 1,
257    ?IPRINT("try encode and decode value 2: ~w (0x~.16b)", [Val2, Val2]),
258    Enc2 = snmp_pdus:enc_value('Counter32', Val2),
259    ?IPRINT("  => ~w", [Enc2]),
260    {{'Counter32', Val2}, []} = snmp_pdus:dec_value(Enc2),
261
262    Val3 = 16#7ffffffe,
263    ?IPRINT("try encode and decode value 3: ~w (0x~.16b)", [Val3, Val3]),
264    Enc3 = snmp_pdus:enc_value('Counter32', Val3),
265    ?IPRINT("  => ~w", [Enc3]),
266    {{'Counter32', Val3}, []} = snmp_pdus:dec_value(Enc3),
267
268    Val4 = Val3 + 1,
269    ?IPRINT("try encode and decode value 4: ~w (0x~.16b)", [Val4, Val4]),
270    Enc4 = snmp_pdus:enc_value('Counter32', Val4),
271    ?IPRINT("  => ~w", [Enc4]),
272    {{'Counter32', Val4}, []} = snmp_pdus:dec_value(Enc4),
273
274    Val5 = Val4 + 1,
275    ?IPRINT("try encode and decode value 5: ~w (0x~.16b)", [Val5, Val5]),
276    Enc5 = snmp_pdus:enc_value('Counter32', Val5),
277    ?IPRINT("  => ~w", [Enc5]),
278    {{'Counter32', Val5}, []} = snmp_pdus:dec_value(Enc5),
279
280    Val6 = 16#fffffffe,
281    ?IPRINT("try encode and decode value 6: ~w (0x~.16b)", [Val6, Val6]),
282    Enc6 = snmp_pdus:enc_value('Counter32', Val6),
283    ?IPRINT("  => ~w", [Enc6]),
284    {{'Counter32', Val6}, []} = snmp_pdus:dec_value(Enc6),
285
286    Val7 = Val6 + 1,
287    ?IPRINT("try encode and decode value 7: ~w (0x~.16b)", [Val7, Val7]),
288    Enc7 = snmp_pdus:enc_value('Counter32', Val7),
289    ?IPRINT("  => ~w", [Enc7]),
290    {{'Counter32', Val7}, []} = snmp_pdus:dec_value(Enc7),
291
292    Val8 = 16#ffffffff + 1,
293    ?IPRINT("try and fail to encode value 8: ~w (0x~.16b)", [Val8, Val8]),
294    case (catch snmp_pdus:enc_value('Counter32', Val8)) of
295	{'EXIT', {error, {bad_counter32, Val8}}} ->
296	    ok;
297	Unexpected8 ->
298	    ?IPRINT("  => ~w", [Unexpected8]),
299	    exit({unexpected_encode_result, Unexpected8, Val8})
300    end,
301
302    Val9 = -1,
303    ?IPRINT("try and fail to encode value 9: ~w", [Val9]),
304    case (catch snmp_pdus:enc_value('Counter32', Val9)) of
305	{'EXIT', {error, {bad_counter32, Val9}}} ->
306	    ok;
307	Unexpected9 ->
308	    ?IPRINT("  => ~w", [Unexpected9]),
309	    exit({unexpected_encode_result, Unexpected9, Val9})
310    end,
311
312    ok.
313
314
315otp10132(suite) -> [];
316otp10132(doc) -> ["OTP-10132 - TimeTicks"];
317otp10132(Config) when is_list(Config) ->
318    Val0 = 2159001034,
319    ?IPRINT("try encode and decode value 0: ~w (0x~.16b)", [Val0, Val0]),
320    Enc0 = snmp_pdus:enc_value('TimeTicks', Val0),
321    ?IPRINT("  => ~w", [Enc0]),
322    {{'TimeTicks', Val0}, []} = snmp_pdus:dec_value(Enc0),
323
324    Val1 = 0,
325    ?IPRINT("try encode and decode value 1: ~w (0x~.16b)", [Val1, Val1]),
326    Enc1 = snmp_pdus:enc_value('TimeTicks', Val1),
327    ?IPRINT("  => ~w", [Enc1]),
328    {{'TimeTicks', Val1}, []} = snmp_pdus:dec_value(Enc1),
329
330    Val2 = Val1 + 1,
331    ?IPRINT("try encode and decode value 2: ~w (0x~.16b)", [Val2, Val2]),
332    Enc2 = snmp_pdus:enc_value('TimeTicks', Val2),
333    ?IPRINT("  => ~w", [Enc2]),
334    {{'TimeTicks', Val2}, []} = snmp_pdus:dec_value(Enc2),
335
336    Val3 = 16#7ffffffe,
337    ?IPRINT("try encode and decode value 3: ~w (0x~.16b)", [Val3, Val3]),
338    Enc3 = snmp_pdus:enc_value('TimeTicks', Val3),
339    ?IPRINT("  => ~w", [Enc3]),
340    {{'TimeTicks', Val3}, []} = snmp_pdus:dec_value(Enc3),
341
342    Val4 = Val3 + 1,
343    ?IPRINT("try encode and decode value 4: ~w (0x~.16b)", [Val4, Val4]),
344    Enc4 = snmp_pdus:enc_value('TimeTicks', Val4),
345    ?IPRINT("  => ~w", [Enc4]),
346    {{'TimeTicks', Val4}, []} = snmp_pdus:dec_value(Enc4),
347
348    Val5 = Val4 + 1,
349    ?IPRINT("try encode and decode value 5: ~w (0x~.16b)", [Val5, Val5]),
350    Enc5 = snmp_pdus:enc_value('TimeTicks', Val5),
351    ?IPRINT("  => ~w", [Enc5]),
352    {{'TimeTicks', Val5}, []} = snmp_pdus:dec_value(Enc5),
353
354    Val6 = 16#fffffffe,
355    ?IPRINT("try encode and decode value 6: ~w (0x~.16b)", [Val6, Val6]),
356    Enc6 = snmp_pdus:enc_value('TimeTicks', Val6),
357    ?IPRINT("  => ~w", [Enc6]),
358    {{'TimeTicks', Val6}, []} = snmp_pdus:dec_value(Enc6),
359
360    Val7 = Val6 + 1,
361    ?IPRINT("try encode and decode value 7: ~w (0x~.16b)", [Val7, Val7]),
362    Enc7 = snmp_pdus:enc_value('TimeTicks', Val7),
363    ?IPRINT("  => ~w", [Enc7]),
364    {{'TimeTicks', Val7}, []} = snmp_pdus:dec_value(Enc7),
365
366    Val8 = Val7 + 1,
367    ?IPRINT("try and fail to encode value 8: ~w (0x~.16b)", [Val8, Val8]),
368    case (catch snmp_pdus:enc_value('TimeTicks', Val8)) of
369	{'EXIT', {error, {bad_timeticks, Val8}}} ->
370	    ok;
371	Unexpected8 ->
372	    ?IPRINT("  => ~w", [Unexpected8]),
373	    exit({unexpected_encode_result, Unexpected8, Val8})
374    end,
375
376    Val9 = -1,
377    ?IPRINT("try and fail to encode value 9: ~w", [Val9]),
378    case (catch snmp_pdus:enc_value('TimeTicks', Val9)) of
379	{'EXIT', {error, {bad_timeticks, Val9}}} ->
380	    ok;
381	Unexpected9 ->
382	    ?IPRINT("  => ~w", [Unexpected9]),
383	    exit({unexpected_encode_result, Unexpected9, Val9})
384    end,
385
386    ?IPRINT("done"),
387    ok.
388
389
390%%======================================================================
391%% Internal functions
392%%======================================================================
393
394dec_message(B) when is_binary(B) ->
395    L = binary_to_list(B),
396    snmp_pdus:dec_message(L).
397