1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2004-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: Various (snmp manager) user related tests
23%%----------------------------------------------------------------------
24-module(snmp_manager_user_SUITE).
25
26
27%%----------------------------------------------------------------------
28%% Include files
29%%----------------------------------------------------------------------
30-include_lib("common_test/include/ct.hrl").
31-include("snmp_test_lib.hrl").
32
33
34%%----------------------------------------------------------------------
35%% External exports
36%%----------------------------------------------------------------------
37
38-export([
39         suite/0, all/0, groups/0,
40	 init_per_suite/1,    end_per_suite/1,
41	 init_per_group/2,    end_per_group/2,
42	 init_per_testcase/2, end_per_testcase/2,
43
44
45	 simple_register_and_unregister1/1,
46	 simple_register_and_unregister2/1,
47	 simple_register_and_unregister3/1,
48	 register_and_crash1/1,
49	 register_and_crash2/1,
50	 register_and_crash3/1,
51	 register_request_and_crash1/1,
52	 register_request_and_crash2/1,
53	 register_request_and_crash3/1,
54	 simple_register_monitor_and_unregister1/1,
55	 simple_register_monitor_and_unregister2/1,
56	 simple_register_monitor_and_unregister3/1,
57	 register_monitor_and_crash1/1,
58	 register_monitor_and_crash2/1,
59	 register_monitor_and_crash3/1,
60	 register_monitor_and_crash4/1,
61	 register_monitor_and_crash5/1,
62	 register_monitor_request_and_crash1/1,
63	 register_monitor_request_and_crash2/1,
64	 register_monitor_request_and_crash3/1,
65	 register_monitor_request_and_crash4/1,
66
67
68	 otp7902/1
69
70	]).
71
72
73%%----------------------------------------------------------------------
74%% Internal exports
75%%----------------------------------------------------------------------
76-export([
77        ]).
78
79
80%%----------------------------------------------------------------------
81%% Macros
82%%----------------------------------------------------------------------
83
84
85%%----------------------------------------------------------------------
86%% Records
87%%----------------------------------------------------------------------
88
89
90%%======================================================================
91%% Common Test interface functions
92%%======================================================================
93
94suite() ->
95    [{ct_hooks, [ts_install_cth]}].
96
97
98all() ->
99    [
100     {group, register_user},
101     {group, tickets}
102    ].
103
104groups() ->
105    [
106     {register_user, [], register_user_cases()},
107     {tickets,       [], tickets_cases()}
108    ].
109
110register_user_cases() ->
111    [
112     simple_register_and_unregister1,
113     simple_register_and_unregister2,
114     simple_register_and_unregister3, register_and_crash1,
115     register_and_crash2, register_and_crash3,
116     register_request_and_crash1,
117     register_request_and_crash2,
118     register_request_and_crash3,
119     simple_register_monitor_and_unregister1,
120     simple_register_monitor_and_unregister2,
121     simple_register_monitor_and_unregister3,
122     register_monitor_and_crash1,
123     register_monitor_and_crash2,
124     register_monitor_and_crash3,
125     register_monitor_and_crash4,
126     register_monitor_and_crash5,
127     register_monitor_request_and_crash1,
128     register_monitor_request_and_crash2,
129     register_monitor_request_and_crash3,
130     register_monitor_request_and_crash4
131    ].
132
133tickets_cases() ->
134    [
135     otp7902
136    ].
137
138
139
140%%
141%% -----
142%%
143
144init_per_suite(Config0) when is_list(Config0) ->
145
146    ?IPRINT("init_per_suite -> entry with"
147            "~n   Config0: ~p", [Config0]),
148
149    case ?LIB:init_per_suite(Config0) of
150        {skip, _} = SKIP ->
151            SKIP;
152        Config1 when is_list(Config1) ->
153            snmp_test_sys_monitor:start(),
154            Config2 = snmp_test_lib:init_suite_top_dir(?MODULE, Config1),
155
156            ?IPRINT("init_per_suite -> done when"
157                    "~n   Config: ~p", [Config2]),
158
159            Config2
160    end.
161
162end_per_suite(Config) when is_list(Config) ->
163
164    ?IPRINT("end_per_suite -> entry with"
165            "~n   Config: ~p", [Config]),
166
167    snmp_test_sys_monitor:stop(),
168    Config1 = ?LIB:end_per_suite(Config),
169
170    ?IPRINT("end_per_suite -> end"),
171
172    Config1.
173
174
175
176%%
177%% -----
178%%
179
180init_per_group(_GroupName, Config) ->
181	Config.
182
183end_per_group(_GroupName, Config) ->
184	Config.
185
186
187
188%%
189%% -----
190%%
191
192init_per_testcase(Case, Config) when is_list(Config) ->
193    ?IPRINT("init_per_testcase -> entry with"
194            "~n   Config: ~p", [Config]),
195
196    snmp_test_global_sys_monitor:reset_events(),
197
198    SuiteTopDir = ?config(snmp_suite_top_dir, Config),
199    CaseTopDir  = filename:join(SuiteTopDir, atom_to_list(Case)),
200    ?line ok    = file:make_dir(CaseTopDir),
201    ?IPRINT("init_per_testcase -> CaseTopDir: ~p", [CaseTopDir]),
202    MgrTopDir   = filename:join(CaseTopDir, "manager/"),
203    ?line ok    = file:make_dir(MgrTopDir),
204    MgrConfDir  = filename:join(MgrTopDir, "conf/"),
205    ?line ok    = file:make_dir(MgrConfDir),
206    MgrDbDir    = filename:join(MgrTopDir, "db/"),
207    ?line ok    = file:make_dir(MgrDbDir),
208    MgrLogDir   = filename:join(MgrTopDir,   "log/"),
209    ?line ok    = file:make_dir(MgrLogDir),
210
211    Config1 = [{case_top_dir,     CaseTopDir},
212               {manager_dir,      MgrTopDir},
213               {manager_conf_dir, MgrConfDir},
214               {manager_db_dir,   MgrDbDir},
215               {manager_log_dir,  MgrLogDir} | Config],
216
217    ?IPRINT("init_per_testcase -> done when"
218            "~n   Config: ~p", [Config1]),
219
220    Config1.
221
222
223end_per_testcase(_Case, Config) when is_list(Config) ->
224
225    ?IPRINT("end_per_testcase -> entry with"
226            "~n   Config: ~p",
227            [Config]),
228
229    ?IPRINT("system events during test: ~p",
230            [snmp_test_global_sys_monitor:events()]),
231
232    Config.
233
234
235
236%%======================================================================
237%% Test functions
238%%======================================================================
239
240simple_register_and_unregister1(suite) -> [];
241simple_register_and_unregister1(doc) ->
242    "Start a user, register and unregister the user.";
243simple_register_and_unregister1(Conf) when is_list(Conf) ->
244    put(tname,srar1),
245    ?IPRINT("start"),
246    process_flag(trap_exit, true),
247
248    ConfDir = ?config(manager_conf_dir, Conf),
249    DbDir = ?config(manager_db_dir, Conf),
250
251    write_manager_conf(ConfDir),
252
253    Opts = [{server, [{verbosity, trace}]},
254            {net_if, [{verbosity, trace}]},
255            {note_store, [{verbosity, trace}]},
256            {config, [{verbosity, trace}, {dir, ConfDir}, {db_dir, DbDir}]}],
257
258    ?IPRINT("try starting manager"),
259    ok = snmpm:start_link(Opts),
260
261    ?SLEEP(1000),
262
263    Id = make_ref(),
264
265    ?line Pid = start_user(),
266
267    ?line [] = Users1 = which_users(),
268    ?IPRINT("Users1: ~p", [Users1]),
269
270    ?line ok = register_user(Pid, Id),
271
272    ?line [Id] = Users2 = which_users(),
273    ?IPRINT("Users2: ~p", [Users2]),
274
275    ?line ok = unregister_user(Pid),
276
277    ?line [] = Users3 = which_users(),
278    ?IPRINT("Users3: ~p", [Users3]),
279
280    ?line stop_user(Pid),
281
282    ?line ok = snmpm:stop(),
283
284    ?IPRINT("end"),
285    ok.
286
287
288%% ------------------------------------------------------------------
289
290simple_register_and_unregister2(suite) -> [];
291simple_register_and_unregister2(doc) ->
292    "Start a single user process, "
293	"register 2 users (per that process) and unregister the 2 users.";
294simple_register_and_unregister2(Conf) when is_list(Conf) ->
295    put(tname,srar2),
296    ?IPRINT("start"),
297    process_flag(trap_exit, true),
298
299    ConfDir = ?config(manager_conf_dir, Conf),
300    DbDir = ?config(manager_db_dir, Conf),
301
302    write_manager_conf(ConfDir),
303
304    Opts = [{server, [{verbosity, trace}]},
305            {net_if, [{verbosity, trace}]},
306            {note_store, [{verbosity, trace}]},
307            {config, [{verbosity, trace}, {dir, ConfDir}, {db_dir, DbDir}]}],
308
309    ?IPRINT("try starting manager"),
310    ok = snmpm:start_link(Opts),
311
312    ?SLEEP(1000),
313
314    Id1 = make_ref(),
315    Id2 = make_ref(),
316
317    ?line Pid = start_user(),
318
319    ?line [] = Users1 = which_users(),
320    ?IPRINT("Users1: ~p", [Users1]),
321
322    ?line ok = register_user(Pid, Id1),
323    ?line ok = register_user(Pid, Id2),
324
325    ?line Users2 = case which_users() of
326		       [Id1, Id2] = U1 ->
327			   U1;
328		       [Id2, Id1] = U2 ->
329			   U2;
330		       Else ->
331			   ?FAIL({invalid_users, Else})
332		   end,
333    ?IPRINT("Users2: ~p", [Users2]),
334
335
336    ?line ok = unregister_user(Pid, Id1),
337    ?line ok = unregister_user(Pid, Id2),
338
339    ?line [] = Users3 = which_users(),
340    ?IPRINT("Users3: ~p", [Users3]),
341
342    ?line stop_user(Pid),
343
344    ?line ok = snmpm:stop(),
345
346    ?IPRINT("end"),
347    ok.
348
349
350%% ------------------------------------------------------------------
351
352simple_register_and_unregister3(suite) -> [];
353simple_register_and_unregister3(doc) ->
354    "Start 2 user processes, "
355	"register one users per process and unregister the 2 users.";
356simple_register_and_unregister3(Conf) when is_list(Conf) ->
357    put(tname,srar2),
358    ?IPRINT("start"),
359    process_flag(trap_exit, true),
360
361    ConfDir = ?config(manager_conf_dir, Conf),
362    DbDir = ?config(manager_db_dir, Conf),
363
364    write_manager_conf(ConfDir),
365
366    Opts = [{server, [{verbosity, trace}]},
367            {net_if, [{verbosity, trace}]},
368            {note_store, [{verbosity, trace}]},
369            {config, [{verbosity, trace}, {dir, ConfDir}, {db_dir, DbDir}]}],
370
371    ?IPRINT("try starting manager"),
372    ok = snmpm:start_link(Opts),
373
374    ?SLEEP(1000),
375
376    Id1 = make_ref(),
377    Id2 = make_ref(),
378
379    ?line Pid1 = start_user(),
380    ?line Pid2 = start_user(),
381
382    ?line [] = Users1 = which_users(),
383    ?IPRINT("Users1: ~p", [Users1]),
384
385    ?line ok = register_user(Pid1, Id1),
386    ?line ok = register_user(Pid2, Id2),
387
388    ?line Users2 = case which_users() of
389		       [Id1, Id2] = U1 ->
390			   U1;
391		       [Id2, Id1] = U2 ->
392			   U2;
393		       Else ->
394			   ?FAIL({invalid_users, Else})
395		   end,
396    ?IPRINT("Users2: ~p", [Users2]),
397
398
399    ?line ok = unregister_user(Pid1, Id1),
400    ?line ok = unregister_user(Pid2, Id2),
401
402    ?line [] = Users3 = which_users(),
403    ?IPRINT("Users3: ~p", [Users3]),
404
405    ?line stop_user(Pid1),
406    ?line stop_user(Pid2),
407
408    ?line ok = snmpm:stop(),
409
410    ?IPRINT("end"),
411    ok.
412
413
414%% ------------------------------------------------------------------
415
416register_and_crash1(suite) -> [];
417register_and_crash1(doc) ->
418    "Start a user, register and crash user.";
419register_and_crash1(Conf) when is_list(Conf) ->
420    put(tname,racau1),
421    ?IPRINT("start"),
422    process_flag(trap_exit, true),
423
424    ConfDir = ?config(manager_conf_dir, Conf),
425    DbDir = ?config(manager_db_dir, Conf),
426
427    write_manager_conf(ConfDir),
428
429    Opts = [{server, [{verbosity, trace}]},
430            {net_if, [{verbosity, trace}]},
431            {note_store, [{verbosity, trace}]},
432            {config, [{verbosity, trace}, {dir, ConfDir}, {db_dir, DbDir}]}],
433
434    ?IPRINT("try starting manager"),
435    ?line ok = snmpm:start_link(Opts),
436
437    ?SLEEP(1000),
438
439    Id = make_ref(),
440
441    ?line Pid = start_user(),
442
443    ?line [] = Users1 = which_users(),
444    ?IPRINT("Users1: ~p", [Users1]),
445
446    ?line ok = register_user(Pid, Id),
447
448    ?line [Id] = Users2 = which_users(),
449    ?IPRINT("Users2: ~p", [Users2]),
450
451    ?line ok = simulate_crash(Pid),
452
453    ?line [Id] = Users3 = which_users(),
454    ?IPRINT("Users3: ~p", [Users3]),
455
456    ?IPRINT("stop manager"),
457    ?line ok = snmpm:stop(),
458
459    ?IPRINT("end"),
460    ok.
461
462
463%% ------------------------------------------------------------------
464
465register_and_crash2(suite) -> [];
466register_and_crash2(doc) ->
467    "Start a single user process, "
468	"register 2 users (per that process) and crash the process.";
469register_and_crash2(Conf) when is_list(Conf) ->
470    put(tname,racau2),
471    ?IPRINT("start"),
472    process_flag(trap_exit, true),
473
474    ConfDir = ?config(manager_conf_dir, Conf),
475    DbDir = ?config(manager_db_dir, Conf),
476
477    write_manager_conf(ConfDir),
478
479    Opts = [{server, [{verbosity, trace}]},
480            {net_if, [{verbosity, trace}]},
481            {note_store, [{verbosity, trace}]},
482            {config, [{verbosity, trace}, {dir, ConfDir}, {db_dir, DbDir}]}],
483
484    ?IPRINT("try starting manager"),
485    ?line ok = snmpm:start_link(Opts),
486
487    ?SLEEP(1000),
488
489    Id1 = make_ref(),
490    Id2 = make_ref(),
491
492    ?line Pid = start_user(),
493
494    ?line [] = Users1 = which_users(),
495    ?IPRINT("Users1: ~p", [Users1]),
496
497    ?line ok = register_user(Pid, Id1),
498    ?line ok = register_user(Pid, Id2),
499
500    ?line Users2 = case which_users() of
501		       [Id1, Id2] = U1 ->
502			   U1;
503		       [Id2, Id1] = U2 ->
504			   U2;
505		       Else1 ->
506			   ?FAIL({invalid_users, Else1})
507		   end,
508    ?IPRINT("Users2: ~p", [Users2]),
509
510    ?line ok = simulate_crash(Pid),
511
512    ?line Users3 = case which_users() of
513		       [Id1, Id2] = U3 ->
514			   U3;
515		       [Id2, Id1] = U4 ->
516			   U4;
517		       Else2 ->
518			   ?FAIL({invalid_users, Else2})
519		   end,
520    ?IPRINT("Users3: ~p", [Users3]),
521
522    ?IPRINT("stop manager"),
523    ?line ok = snmpm:stop(),
524
525    ?IPRINT("end"),
526    ok.
527
528
529%% ------------------------------------------------------------------
530
531register_and_crash3(suite) -> [];
532register_and_crash3(doc) ->
533    "Start 2 user processes, "
534	"register one user per process and "
535	"crash the first user process.";
536register_and_crash3(Conf) when is_list(Conf) ->
537    %%     put(tname,rac3),
538    %%     ?IPRINT("start"),
539    %%     process_flag(trap_exit, true),
540    ?SKIP(not_yet_implemented).
541
542
543%% ------------------------------------------------------------------
544
545register_request_and_crash1(suite) -> [];
546register_request_and_crash1(doc) ->
547    "Start a single user process, "
548	"register user, send request and crash user.";
549register_request_and_crash1(Conf) when is_list(Conf) ->
550    %%     put(tname,rrac1),
551    %%     ?IPRINT("start"),
552    %%     process_flag(trap_exit, true),
553    ?SKIP(not_yet_implemented).
554
555
556%% ------------------------------------------------------------------
557
558register_request_and_crash2(suite) -> [];
559register_request_and_crash2(doc) ->
560    "Start a single user process, "
561	"register 2 users (per that single user process), "
562	"send a request for each user and crash the single user process.";
563register_request_and_crash2(Conf) when is_list(Conf) ->
564    %%     put(tname,rrac2),
565    %%     ?IPRINT("start"),
566    %%     process_flag(trap_exit, true),
567    ?SKIP(not_yet_implemented).
568
569
570%% ------------------------------------------------------------------
571
572register_request_and_crash3(suite) -> [];
573register_request_and_crash3(doc) ->
574    "Start 2 user processes, "
575	"register one user per process, "
576	"send a request for each user and crash the first user process.";
577register_request_and_crash3(Conf) when is_list(Conf) ->
578    %%     put(tname,rrac3),
579    %%     ?IPRINT("start"),
580    %%     process_flag(trap_exit, true),
581    ?SKIP(not_yet_implemented).
582
583
584%% ------------------------------------------------------------------
585
586simple_register_monitor_and_unregister1(suite) -> [];
587simple_register_monitor_and_unregister1(doc) ->
588    "Start a user, register-link and unregister the user.";
589simple_register_monitor_and_unregister1(Conf) when is_list(Conf) ->
590    put(tname,srlau1),
591    ?IPRINT("start"),
592    process_flag(trap_exit, true),
593
594    ConfDir = ?config(manager_conf_dir, Conf),
595    DbDir = ?config(manager_db_dir, Conf),
596
597    write_manager_conf(ConfDir),
598
599    Opts = [{server, [{verbosity, trace}]},
600            {net_if, [{verbosity, trace}]},
601            {note_store, [{verbosity, trace}]},
602            {config, [{verbosity, trace}, {dir, ConfDir}, {db_dir, DbDir}]}],
603
604    ?IPRINT("try starting manager"),
605    ok = snmpm:start_link(Opts),
606
607    ?SLEEP(1000),
608
609    Id = make_ref(),
610
611    ?IPRINT("start user"),
612    ?line Pid = start_user(),
613
614    ?IPRINT("get users (=0)"),
615    ?line [] = Users1 = which_users(),
616    ?IPRINT("Users1: ~p", [Users1]),
617
618    ?IPRINT("register monitored user"),
619    ?line ok = register_user_monitor(Pid, Id),
620
621    ?IPRINT("get users (=1)"),
622    ?line [Id] = Users2 = which_users(),
623    ?IPRINT("Users2: ~p", [Users2]),
624
625    ?IPRINT("unregister monitored user"),
626    ?line unregister_user(Pid),
627
628    ?IPRINT("get users (=0)"),
629    ?line [] = Users3 = which_users(),
630    ?IPRINT("Users3: ~p", [Users3]),
631
632    ?IPRINT("start user"),
633    ?line stop_user(Pid),
634
635    ?IPRINT("stop manager"),
636    ?line ok = snmpm:stop(),
637
638    ?IPRINT("end"),
639    ok.
640
641
642%% ------------------------------------------------------------------
643
644simple_register_monitor_and_unregister2(suite) -> [];
645simple_register_monitor_and_unregister2(doc) ->
646    "Start a single user process, "
647	"register-link 2 users (per that process) and "
648	"unregister the 2 users.";
649simple_register_monitor_and_unregister2(Conf) when is_list(Conf) ->
650    put(tname,srlau2),
651    ?IPRINT("start"),
652    process_flag(trap_exit, true),
653
654    ConfDir = ?config(manager_conf_dir, Conf),
655    DbDir = ?config(manager_db_dir, Conf),
656
657    write_manager_conf(ConfDir),
658
659    Opts = [{server, [{verbosity, trace}]},
660            {net_if, [{verbosity, trace}]},
661            {note_store, [{verbosity, trace}]},
662            {config, [{verbosity, trace}, {dir, ConfDir}, {db_dir, DbDir}]}],
663
664    ?IPRINT("try starting manager"),
665    ok = snmpm:start_link(Opts),
666
667    ?SLEEP(1000),
668
669    Id1 = make_ref(),
670    Id2 = make_ref(),
671
672    ?line Pid = start_user(),
673
674    ?line [] = Users1 = which_users(),
675    ?IPRINT("Users1: ~p", [Users1]),
676
677    ?line ok = register_user_monitor(Pid, Id1),
678    ?line ok = register_user_monitor(Pid, Id2),
679
680    ?line Users2 = case which_users() of
681		       [Id1, Id2] = U1 ->
682			   U1;
683		       [Id2, Id1] = U2 ->
684			   U2;
685		       Else ->
686			   ?FAIL({invalid_users, Else})
687		   end,
688    ?IPRINT("Users2: ~p", [Users2]),
689
690    ?line ok = unregister_user(Pid, Id1),
691    ?line ok = unregister_user(Pid, Id2),
692
693    ?line [] = Users3 = which_users(),
694    ?IPRINT("Users3: ~p", [Users3]),
695
696    ?line stop_user(Pid),
697
698    ?line ok = snmpm:stop(),
699
700    ?IPRINT("end"),
701    ok.
702
703
704%% ------------------------------------------------------------------
705
706simple_register_monitor_and_unregister3(suite) -> [];
707simple_register_monitor_and_unregister3(doc) ->
708    "Start a single user process, "
709	"register one user and register-monitor one user "
710	"(per that process) and "
711	"unregister the 2 users.";
712simple_register_monitor_and_unregister3(Conf) when is_list(Conf) ->
713    put(tname,srlau3),
714    ?IPRINT("start"),
715    process_flag(trap_exit, true),
716
717    ConfDir = ?config(manager_conf_dir, Conf),
718    DbDir = ?config(manager_db_dir, Conf),
719
720    write_manager_conf(ConfDir),
721
722    Opts = [{server, [{verbosity, trace}]},
723            {net_if, [{verbosity, trace}]},
724            {note_store, [{verbosity, trace}]},
725            {config, [{verbosity, trace}, {dir, ConfDir}, {db_dir, DbDir}]}],
726
727    ?IPRINT("try starting manager"),
728    ok = snmpm:start_link(Opts),
729
730    ?SLEEP(1000),
731
732    Id1 = make_ref(),
733    Id2 = make_ref(),
734
735    ?line Pid = start_user(),
736
737    ?line [] = Users1 = which_users(),
738    ?IPRINT("Users1: ~p", [Users1]),
739
740    ?line ok = register_user(Pid, Id1),
741    ?line ok = register_user_monitor(Pid, Id2),
742
743    ?line Users2 = case which_users() of
744		       [Id1, Id2] = U1 ->
745			   U1;
746		       [Id2, Id1] = U2 ->
747			   U2;
748		       Else ->
749			   ?FAIL({invalid_users, Else})
750		   end,
751    ?IPRINT("Users2: ~p", [Users2]),
752
753    ?line unregister_user(Pid),
754
755    ?line [] = Users3 = which_users(),
756    ?IPRINT("Users3: ~p", [Users3]),
757
758    ?line stop_user(Pid),
759
760    ?line ok = snmpm:stop(),
761
762    ?IPRINT("end"),
763    ok.
764
765
766%% ------------------------------------------------------------------
767
768register_monitor_and_crash1(suite) -> [];
769register_monitor_and_crash1(doc) ->
770    "Start a user, register-monitor and crash the user.";
771register_monitor_and_crash1(Conf) when is_list(Conf) ->
772    put(tname,rlac1),
773    ?IPRINT("start"),
774    process_flag(trap_exit, true),
775
776    ConfDir = ?config(manager_conf_dir, Conf),
777    DbDir = ?config(manager_db_dir, Conf),
778
779    write_manager_conf(ConfDir),
780
781    Opts = [{server, [{verbosity, trace}]},
782            {net_if, [{verbosity, trace}]},
783            {note_store, [{verbosity, trace}]},
784            {config, [{verbosity, trace}, {dir, ConfDir}, {db_dir, DbDir}]}],
785
786    ?IPRINT("try starting manager"),
787    ?line ok = snmpm:start_link(Opts),
788
789    ?SLEEP(1000),
790
791    Id = make_ref(),
792
793    ?line Pid = start_user(),
794
795    ?line [] = Users1 = which_users(),
796    ?IPRINT("Users1: ~p", [Users1]),
797
798    ?line ok = register_user_monitor(Pid, Id),
799
800    ?line [Id] = Users2 = which_users(),
801    ?IPRINT("Users2: ~p", [Users2]),
802
803    ?line ok = simulate_crash(Pid),
804
805    ?SLEEP(1000),
806
807    ?line [] = Users3 = which_users(),
808    ?IPRINT("Users3: ~p", [Users3]),
809
810    ?IPRINT("stop manager"),
811    ?line ok = snmpm:stop(),
812
813    ?IPRINT("end"),
814    ok.
815
816
817%% ------------------------------------------------------------------
818
819register_monitor_and_crash2(suite) -> [];
820register_monitor_and_crash2(doc) ->
821    "Start a single user process, "
822	"register-monitor 2 users (per that process) "
823	"and crash the single user process.";
824register_monitor_and_crash2(Conf) when is_list(Conf) ->
825    put(tname,rlac2),
826    ?IPRINT("start"),
827    process_flag(trap_exit, true),
828
829    ConfDir = ?config(manager_conf_dir, Conf),
830    DbDir = ?config(manager_db_dir, Conf),
831
832    write_manager_conf(ConfDir),
833
834    Opts = [{server, [{verbosity, trace}]},
835            {net_if, [{verbosity, trace}]},
836            {note_store, [{verbosity, trace}]},
837            {config, [{verbosity, trace}, {dir, ConfDir}, {db_dir, DbDir}]}],
838
839    ?IPRINT("try starting manager"),
840    ?line ok = snmpm:start_link(Opts),
841
842    ?SLEEP(1000),
843
844    Id1 = make_ref(),
845    Id2 = make_ref(),
846
847    ?line Pid = start_user(),
848
849    ?line [] = Users1 = which_users(),
850    ?IPRINT("Users1: ~p", [Users1]),
851
852    ?line ok = register_user_monitor(Pid, Id1),
853    ?line ok = register_user_monitor(Pid, Id2),
854
855    ?line Users2 = case which_users() of
856		       [Id1, Id2] = U1 ->
857			   U1;
858		       [Id2, Id1] = U2 ->
859			   U2;
860		       Else ->
861			   ?FAIL({invalid_users, Else})
862		   end,
863    ?IPRINT("Users2: ~p", [Users2]),
864
865    ?line ok = simulate_crash(Pid),
866
867    ?SLEEP(1000),
868
869    ?line [] = Users3 = which_users(),
870    ?IPRINT("Users3: ~p", [Users3]),
871
872    ?IPRINT("stop manager"),
873    ?line ok = snmpm:stop(),
874
875    ?IPRINT("end"),
876    ok.
877
878
879%% ------------------------------------------------------------------
880
881register_monitor_and_crash3(suite) -> [];
882register_monitor_and_crash3(doc) ->
883    "Start a single user process, "
884	"register-monitor one user and register one user, "
885	"crash the single user process.";
886register_monitor_and_crash3(Conf) when is_list(Conf) ->
887    process_flag(trap_exit, true),
888    put(tname,rlac3),
889
890    %% <CONDITIONAL-SKIP>
891    LinuxVersionVerify =
892	fun() ->
893                case os:version() of
894                    V when V > {2, 6, 16} ->
895                        ?IPRINT("(Linux) kernel version check: "
896                                "~p > {2, 6, 16} => *NO* SKIP", [V]),
897                        false;
898                    V ->
899                        ?IPRINT("(Linux) kernel version check: "
900                                "~p =< {2, 6, 16}  => *SKIP*", [V]),
901                        true
902		end
903	end,
904    Skippable = [{unix, [{linux, LinuxVersionVerify}]}],
905    Condition = fun() -> ?OS_BASED_SKIP(Skippable) end,
906    ?NON_PC_TC_MAYBE_SKIP(Conf, Condition),
907    %% </CONDITIONAL-SKIP>
908
909    ?IPRINT("start"),
910
911    ConfDir = ?config(manager_conf_dir, Conf),
912    DbDir = ?config(manager_db_dir, Conf),
913
914    write_manager_conf(ConfDir),
915
916    Opts = [{server,     [{verbosity, trace}]},
917            {net_if,     [{verbosity, trace}]},
918            {note_store, [{verbosity, trace}]},
919            {config,     [{verbosity, trace}, {dir, ConfDir}, {db_dir, DbDir}]}],
920
921    ?IPRINT("try starting manager"),
922    ?line ok = snmpm:start_link(Opts),
923
924    ?SLEEP(1000),
925
926    Id1 = make_ref(),
927    Id2 = make_ref(),
928
929    ?line Pid = start_user(),
930
931    ?line [] = Users1 = which_users(),
932    ?IPRINT("Users1: ~p", [Users1]),
933
934    ?line ok = register_user(Pid, Id1),
935    ?line ok = register_user_monitor(Pid, Id2),
936
937    ?line Users2 = case which_users() of
938		       [Id1, Id2] = U1 ->
939			   U1;
940		       [Id2, Id1] = U2 ->
941			   U2;
942		       Else ->
943			   ?FAIL({invalid_users, Else})
944		   end,
945    ?IPRINT("Users2: ~p", [Users2]),
946
947    ?line ok = simulate_crash(Pid),
948
949    ?SLEEP(1000),
950
951    ?line [Id1] = Users3 = which_users(),
952    ?IPRINT("Users3: ~p", [Users3]),
953
954    ?IPRINT("stop manager"),
955    ?line ok = snmpm:stop(),
956
957    ?IPRINT("end"),
958    ok.
959
960
961%% ------------------------------------------------------------------
962
963register_monitor_and_crash4(suite) -> [];
964register_monitor_and_crash4(doc) ->
965    "Start 2 user processes, "
966	"register-monitor one user per process "
967	"and crash the first user process.";
968register_monitor_and_crash4(Conf) when is_list(Conf) ->
969    put(tname,rlac4),
970    ?IPRINT("start"),
971    process_flag(trap_exit, true),
972
973    ConfDir = ?config(manager_conf_dir, Conf),
974    DbDir = ?config(manager_db_dir, Conf),
975
976    write_manager_conf(ConfDir),
977
978    Opts = [{server, [{verbosity, trace}]},
979            {net_if, [{verbosity, trace}]},
980            {note_store, [{verbosity, trace}]},
981            {config, [{verbosity, trace}, {dir, ConfDir}, {db_dir, DbDir}]}],
982
983    ?IPRINT("start manager"),
984    ?line ok = snmpm:start_link(Opts),
985
986    ?SLEEP(1000),
987
988    Id1 = make_ref(),
989    Id2 = make_ref(),
990
991    ?IPRINT("start user processes"),
992    ?line Pid1 = start_user(),
993    ?line Pid2 = start_user(),
994
995    ?line [] = Users1 = which_users(),
996    ?IPRINT("Users1: ~p", [Users1]),
997
998    ?line ok = register_user_monitor(Pid1, Id1),
999    ?line ok = register_user_monitor(Pid2, Id2),
1000
1001    ?line Users2 = case which_users() of
1002		       [Id1, Id2] = U1 ->
1003			   U1;
1004		       [Id2, Id1] = U2 ->
1005			   U2;
1006		       Else ->
1007			   ?FAIL({invalid_users, Else})
1008		   end,
1009    ?IPRINT("Users2: ~p", [Users2]),
1010
1011    ?line ok = simulate_crash(Pid1),
1012
1013    ?SLEEP(1000),
1014
1015    ?line [Id2] = Users3 = which_users(),
1016    ?IPRINT("Users3: ~p", [Users3]),
1017
1018    ?line stop_user(Pid2),
1019
1020    ?IPRINT("stop manager"),
1021    ?line ok = snmpm:stop(),
1022
1023    ?IPRINT("end"),
1024    ok.
1025
1026
1027%% ------------------------------------------------------------------
1028
1029register_monitor_and_crash5(suite) -> [];
1030register_monitor_and_crash5(doc) ->
1031    "OTP-7961: "
1032	"Start 2 user processes, "
1033	"register-monitor a user for per process, "
1034	"let each user register an agent "
1035	"and crash the first user process.";
1036register_monitor_and_crash5(Conf) when is_list(Conf) ->
1037    put(tname,rlac4),
1038    ?IPRINT("start"),
1039    process_flag(trap_exit, true),
1040
1041    ConfDir = ?config(manager_conf_dir, Conf),
1042    DbDir = ?config(manager_db_dir, Conf),
1043
1044    write_manager_conf(ConfDir),
1045
1046    Opts = [{server, [{verbosity, trace}]},
1047            {net_if, [{verbosity, trace}]},
1048            {note_store, [{verbosity, trace}]},
1049            {config, [{verbosity, trace}, {dir, ConfDir}, {db_dir, DbDir}]}],
1050
1051    ?IPRINT("start manager"),
1052    ?line ok = snmpm:start_link(Opts),
1053
1054    ?SLEEP(1000),
1055
1056    Id1 = gurka, %% make_ref(),
1057    Id2 = tomat, %% make_ref(),
1058
1059    ?IPRINT("start user processes"),
1060    ?line Pid1 = start_user(),
1061    ?line Pid2 = start_user(),
1062
1063    ?line [] = Users1 = which_users(),
1064    ?IPRINT("Users1: ~p", [Users1]),
1065
1066    ?line ok = register_user_monitor(Pid1, Id1),
1067    ?line ok = register_user_monitor(Pid2, Id2),
1068
1069    LocalHost = snmp_test_lib:localhost(),
1070
1071    TargetName1 = "kalle1",
1072    Address1    = LocalHost,
1073    Port1       = 5001,
1074    EngineId1   = "agentEngineId-1",
1075
1076    TargetName2 = "kalle2",
1077    Address2 = LocalHost,
1078    Port2       = 5002,
1079    EngineId2   = "agentEngineId-2",
1080
1081    ?line ok = register_agent(Pid1,
1082			      Id1, TargetName1, [{address,   Address1},
1083						 {port,      Port1},
1084						 {engine_id, EngineId1}]),
1085    ?line ok = register_agent(Pid2,
1086			      Id2, TargetName2, [{address,   Address2},
1087						 {port,      Port2},
1088						 {engine_id, EngineId2}]),
1089
1090    ?line Users2 = case which_users() of
1091		       [Id1, Id2] = U1 ->
1092			   U1;
1093		       [Id2, Id1] = U2 ->
1094			   U2;
1095		       U3 ->
1096			   ?FAIL({invalid_users, U3})
1097		   end,
1098    ?IPRINT("Users2: ~p", [Users2]),
1099
1100    ?IPRINT("verify all agent(s): expect 2"),
1101    ?line Agents1 = case which_agents() of
1102			[TargetName1, TargetName2] = A1 ->
1103			    A1;
1104			[TargetName2, TargetName1] = A2 ->
1105			    A2;
1106		       A3 ->
1107			   ?FAIL({invalid_agents, A3})
1108		   end,
1109    ?IPRINT("Agents1: ~p", [Agents1]),
1110
1111    ?line ok = simulate_crash(Pid1),
1112
1113    ?IPRINT("wait some time"),
1114    ?SLEEP(1000),
1115
1116    ?line [Id2] = Users3 = which_users(),
1117    ?IPRINT("Users3: ~p", [Users3]),
1118
1119    ?line [TargetName2] = Agents2 = which_agents(),
1120    ?IPRINT("Agents2: ~p", [Agents2]),
1121
1122    ?line stop_user(Pid2),
1123
1124    ?IPRINT("stop manager"),
1125    ?line ok = snmpm:stop(),
1126
1127    ?IPRINT("end"),
1128    ok.
1129
1130
1131%% ------------------------------------------------------------------
1132
1133register_monitor_request_and_crash1(suite) -> [];
1134register_monitor_request_and_crash1(doc) ->
1135    "Start a single user process, "
1136	"register-monitor one user, "
1137	"send request and crash the user.";
1138register_monitor_request_and_crash1(Conf) when is_list(Conf) ->
1139    %% put(tname,rlrac1),
1140    %% ?IPRINT("start"),
1141    %% process_flag(trap_exit, true),
1142    ?SKIP(not_yet_implemented).
1143
1144
1145%% ------------------------------------------------------------------
1146
1147register_monitor_request_and_crash2(suite) -> [];
1148register_monitor_request_and_crash2(doc) ->
1149    "Start a single user process, "
1150	"register-monitor 2 user (per that one process), "
1151	"send a request for each user and crash the single user process.";
1152register_monitor_request_and_crash2(Conf) when is_list(Conf) ->
1153    %% put(tname,rlrac2),
1154    %% ?IPRINT("start"),
1155    %% process_flag(trap_exit, true),
1156    ?SKIP(not_yet_implemented).
1157
1158
1159%% ------------------------------------------------------------------
1160
1161register_monitor_request_and_crash3(suite) -> [];
1162register_monitor_request_and_crash3(doc) ->
1163    "Start a single user process, "
1164	"register-monitor one user and register one user, "
1165	"send a request for each user and crash the single user process.";
1166register_monitor_request_and_crash3(Conf) when is_list(Conf) ->
1167    %% put(tname,rlrac3),
1168    %% ?IPRINT("start"),
1169    %% process_flag(trap_exit, true),
1170    ?SKIP(not_yet_implemented).
1171
1172
1173%% ------------------------------------------------------------------
1174
1175register_monitor_request_and_crash4(suite) -> [];
1176register_monitor_request_and_crash4(doc) ->
1177    "Start 2 user processes, "
1178	"register-monitor one user and register one user on the "
1179	"first user process and do the same for the other user process, "
1180	"then for each user, send a request and "
1181	"crash the first user process.";
1182register_monitor_request_and_crash4(Conf) when is_list(Conf) ->
1183    %% put(tname,rlrac4),
1184    %% ?IPRINT("start"),
1185    %% process_flag(trap_exit, true),
1186    ?SKIP(not_yet_implemented).
1187
1188
1189
1190%% ------------------------------------------------------------------
1191
1192otp7902(suite) -> [];
1193otp7902(doc) ->
1194    "OTP-7902 - Start old user and make sure it wors.";
1195otp7902(Conf) when is_list(Conf) ->
1196    put(tname, otp7902),
1197    ?IPRINT("start"),
1198    process_flag(trap_exit, true),
1199
1200    ConfDir = ?config(manager_conf_dir, Conf),
1201    DbDir = ?config(manager_db_dir, Conf),
1202
1203    write_manager_conf(ConfDir),
1204
1205    Opts = [{server, [{verbosity, trace}]},
1206            {net_if, [{verbosity, trace}]},
1207            {note_store, [{verbosity, trace}]},
1208            {config, [{verbosity, trace}, {dir, ConfDir}, {db_dir, DbDir}]}],
1209
1210    ?IPRINT("try starting manager"),
1211    ok = snmpm:start_link(Opts),
1212
1213    ?SLEEP(1000),
1214
1215    ?line [] = Users1 = which_users(),
1216    ?IPRINT("Users1: ~p", [Users1]),
1217
1218    ?line ok = snmp_manager_user_old:start(),
1219
1220    ?line [_] = Users2 = which_users(),
1221    ?IPRINT("Users2: ~p", [Users2]),
1222
1223    ?line ok = snmp_manager_user_old:stop(),
1224
1225    ?line [] = Users3 = which_users(),
1226    ?IPRINT("Users3: ~p", [Users3]),
1227
1228    ?line ok = snmpm:stop(),
1229
1230    ?IPRINT("end"),
1231    ok.
1232
1233
1234%% ------------------------------------------------------------------
1235
1236start_user() ->
1237    {ok, Pid} = snmp_manager_user_test_lib:start_link(),
1238    Pid.
1239
1240stop_user(Pid) ->
1241    snmp_manager_user_test_lib:stop(Pid).
1242
1243simulate_crash(Pid) ->
1244    snmp_manager_user_test_lib:simulate_crash(Pid, simulate_crash),
1245    receive
1246	{'EXIT', Pid, simulate_crash} ->
1247	    ok;
1248	{'EXIT', Pid, Whatever} ->
1249	    {ok, Whatever}
1250    after 5000 ->
1251	    {error, timeout}
1252    end.
1253
1254register_user(Pid, Id) ->
1255    snmp_manager_user_test_lib:register(Pid, Id).
1256
1257register_user_monitor(Pid, Id) ->
1258    snmp_manager_user_test_lib:register_monitor(Pid, Id).
1259
1260unregister_user(Pid) ->
1261    case snmp_manager_user_test_lib:unregister(Pid) of
1262	{ok, Res} ->
1263	    case [R || R <- Res, R =/= ok] of
1264		[] ->
1265		    ok;
1266		Errs ->
1267		    {error, Errs}
1268	    end;
1269	Error ->
1270	    Error
1271    end.
1272
1273unregister_user(Pid, Id) ->
1274    snmp_manager_user_test_lib:unregister(Pid, Id).
1275
1276
1277register_agent(Pid, Id, TargetName, Config) ->
1278    snmp_manager_user_test_lib:register_agent(Pid, Id, TargetName, Config).
1279
1280
1281%% ------
1282
1283which_users() ->
1284    snmpm:which_users().
1285
1286which_agents() ->
1287    snmpm:which_agents().
1288
1289
1290%% ------
1291
1292
1293write_manager_conf(Dir) ->
1294    Port = "5000",
1295    MMS  = "484",
1296    EngineID = "\"mgrEngine\"",
1297    Str = lists:flatten(
1298            io_lib:format("%% Minimum manager config file\n"
1299                          "{port,             ~s}.\n"
1300                          "{max_message_size, ~s}.\n"
1301                          "{engine_id,        ~s}.\n",
1302                          [Port, MMS, EngineID])),
1303    write_manager_conf(Dir, Str).
1304
1305write_manager_conf(Dir, Str) ->
1306    write_conf_file(Dir, "manager.conf", Str).
1307
1308
1309write_conf_file(Dir, File, Str) ->
1310    ?line {ok, Fd} = file:open(filename:join(Dir, File), write),
1311    ?line ok = io:format(Fd, "~s", [Str]),
1312    file:close(Fd).
1313
1314