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-module(snmp_agent_conf_SUITE).
22
23%%----------------------------------------------------------------------
24%% Include files
25%%----------------------------------------------------------------------
26
27-include("snmp_test_lib.hrl").
28-include_lib("common_test/include/ct.hrl").
29
30-export([
31         suite/0, all/0, groups/0,
32         init_per_suite/1,    end_per_suite/1,
33         init_per_group/2,    end_per_group/2,
34         init_per_testcase/2, end_per_testcase/2,
35
36         check_agent/1,
37         check_usm/1,
38         check_vacm/1
39        ]).
40
41
42
43%%======================================================================
44%% Common Test interface functions
45%%======================================================================
46
47suite() ->
48    [{ct_hooks, [ts_install_cth]}].
49
50all() ->
51    [
52     check_agent,
53     check_usm,
54     check_vacm
55    ].
56
57
58groups() ->
59    [].
60
61
62%%
63%% -----
64%%
65
66init_per_suite(Config0) ->
67    case ?LIB:init_per_suite(Config0) of
68        {skip, _} = SKIP ->
69            SKIP;
70        Config1 when is_list(Config1) ->
71            snmp_test_sys_monitor:start(),
72            PrivDir = ?config(priv_dir, Config1),
73            PrivSubdir = filename:join(PrivDir, "snmp_agent_conf_test"),
74            ok = filelib:ensure_dir(filename:join(PrivSubdir, "dummy")),
75            [{priv_subdir, PrivSubdir} | Config1]
76    end.
77
78end_per_suite(Config) ->
79    snmp_test_sys_monitor:stop(),
80    ?LIB:end_per_suite(Config).
81
82
83
84%%
85%% -----
86%%
87
88init_per_group(_GroupName, Config) ->
89    Config.
90
91end_per_group(_GroupName, Config) ->
92    Config.
93
94
95%%
96%% -----
97%%
98
99init_per_testcase(_Case, Config) when is_list(Config) ->
100    snmp_test_global_sys_monitor:reset_events(),
101    Config.
102
103end_per_testcase(_Case, Config) when is_list(Config) ->
104    ?IPRINT("system events during test: "
105            "~n   ~p", [snmp_test_global_sys_monitor:events()]),
106    Config.
107
108
109
110
111%%======================================================================
112%% Test data
113%%======================================================================
114
115engine_ids() -> [
116    "plain eid",
117    "here\"eid",
118    "comes\neid",
119    "trouble\0eid",
120    binary_to_list(<<"中国引擎标识符"/utf8>>)
121].
122
123snmp_admin_strings() -> [
124    "plain string",
125    "heres\"eid",
126    "trouble\neid",
127    binary_to_list(<<"中国引擎标识符"/utf8>>)
128].
129
130
131%%======================================================================
132%% Test functions
133%%======================================================================
134
135
136check_agent(Config) ->
137    Dir = ?config(priv_subdir, Config),
138    lists:foreach(
139        fun(EngineId) -> check_agent_by_engineid(Dir, EngineId) end,
140        engine_ids()
141    ),
142    ok.
143
144check_agent_by_engineid(Dir, EngineId) ->
145    WEntries = [
146        snmpa_conf:agent_entry(intAgentIpAddress, {0,0,0,0}),
147        snmpa_conf:agent_entry(intAgentUDPPort, 161),
148        snmpa_conf:agent_entry(snmpEngineMaxMessageSize, 484),
149        snmpa_conf:agent_entry(snmpEngineID, EngineId)
150    ],
151
152    ok = snmpa_conf:write_agent_config(Dir, WEntries),
153    {ok, REntries} = snmpa_conf:read_agent_config(Dir),
154
155    true = is_subset(WEntries, REntries),
156    ok.
157
158%%======================================================================
159
160check_usm(Config) ->
161    Dir = ?config(priv_subdir, Config),
162    EngineId = hd(engine_ids()),
163    UserName = hd(snmp_admin_strings()),
164    SecName  = hd(snmp_admin_strings()),
165
166    %% vary engine id
167    lists:foreach(
168        fun(EngineId_) -> check_usm_by_params(Dir, EngineId_, UserName, SecName) end,
169        engine_ids()
170    ),
171
172    %% vary user name
173    lists:foreach(
174        fun(UserName_) -> check_usm_by_params(Dir, EngineId, UserName_, SecName) end,
175        snmp_admin_strings()
176    ),
177
178    %% vary sec name
179    lists:foreach(
180        fun(SecName_) -> check_usm_by_params(Dir, EngineId, UserName, SecName_) end,
181        snmp_admin_strings()
182    ),
183
184    ok.
185
186check_usm_by_params(Dir, EngineId, UserName, SecName) ->
187    WEntries = [
188        snmpa_conf:usm_entry(
189            EngineId,
190            UserName,
191            SecName,
192            zeroDotZero,
193            usmNoAuthProtocol, % authproto
194            "", "",
195            usmNoPrivProtocol, % privproto
196            "", "", "",
197            [], %AuthKey
198            [])  %PrivKey
199    ],
200
201    ok = snmpa_conf:write_usm_config(Dir, WEntries),
202    {ok, REntries} = snmpa_conf:read_usm_config(Dir),
203
204    true = is_subset(WEntries, REntries),
205    ok.
206
207%%======================================================================
208
209check_vacm(Config) ->
210    Dir = ?config(priv_subdir, Config),
211
212    %% vary sec name
213    lists:foreach(
214        fun(SecName_) -> check_vacm_by_params(Dir, SecName_) end,
215        snmp_admin_strings()
216    ),
217
218    ok.
219
220
221check_vacm_by_params(Dir, SecName) ->
222    WEntries = [
223        %%                      SecModel, SecName, GroupName
224        snmpa_conf:vacm_s2g_entry(usm, SecName,  SecName),
225        %%                     GroupName,Prefix,SecModel,SecLevel,Match,ReadView,WriteView,NotifyView
226        snmpa_conf:vacm_acc_entry(SecName,  "", any, noAuthNoPriv, exact, "all", "all", "all")
227    ],
228
229    ok = snmpa_conf:write_vacm_config(Dir, WEntries),
230    {ok, REntries} = snmpa_conf:read_vacm_config(Dir),
231
232    true = is_subset(WEntries, REntries),
233    ok.
234
235
236
237%%======================================================================
238
239
240%% additional tests needed:
241% check_context()
242% check_community()
243% check_standard()
244% check_target_addr()
245% check_target_params()
246% check_notify()
247
248
249%%======================================================================
250%% Local utility functions
251%%======================================================================
252
253is_subset(List1, List2) ->
254    io:format("Check ~p is subset of ~p\n", [List1, List2]),
255    sets:is_subset(
256        sets:from_list(List1),
257        sets:from_list(List2)
258    ).
259