aboutsummaryrefslogtreecommitdiffstats
path: root/lib/diameter/test/diameter_length_SUITE.erl
blob: 4e413e6a42d42551d4aa031855f649a8cac1d8d8 (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2013. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%

%%
%% Tests of transport_opt() length_errors.
%%

-module(diameter_length_SUITE).

-export([suite/0,
         all/0,
         groups/0,
         init_per_suite/1,
         end_per_suite/1,
         init_per_group/2,
         end_per_group/2,
         init_per_testcase/2,
         end_per_testcase/2]).

%% testcases
-export([start/1,
         send/1,
         stop/1]).

%% diameter callbacks
-export([peer_up/3,
         peer_down/3,
         pick_peer/6,
         prepare_request/5,
         handle_answer/6,
         handle_error/6,
         handle_request/3]).

-include("diameter.hrl").
-include("diameter_gen_base_rfc3588.hrl").

%% ===========================================================================

-define(util, diameter_util).

-define(CLIENT, "CLIENT").
-define(SERVER, "SERVER").
-define(REALM, "erlang.org").
-define(HOST(Host, Realm), Host ++ [$.|Realm]).
-define(DICT, diameter_gen_base_rfc3588).

%% Config for diameter:start_service/2.
-define(SERVICE(Name),
        [{'Origin-Host', Name ++ "." ++ ?REALM},
         {'Origin-Realm', ?REALM},
         {'Host-IP-Address', [{127,0,0,1}]},
         {'Vendor-Id', 12345},
         {'Product-Name', "OTP/diameter"},
         {'Auth-Application-Id', [?DIAMETER_APP_ID_COMMON]},
         {application, [{dictionary, ?DICT},
                        {module, ?MODULE},
                        {answer_errors, callback}]}]).

-define(SUCCESS,
        ?'DIAMETER_BASE_RESULT-CODE_DIAMETER_SUCCESS').
-define(MISSING_AVP,
        ?'DIAMETER_BASE_RESULT-CODE_DIAMETER_MISSING_AVP').
-define(INVALID_MESSAGE_LENGTH,
        ?'DIAMETER_BASE_RESULT-CODE_DIAMETER_INVALID_MESSAGE_LENGTH').

-define(LOGOUT,
        ?'DIAMETER_BASE_TERMINATION-CAUSE_DIAMETER_LOGOUT').

-define(GROUPS, [exit, handle, discard]).

-define(L, atom_to_list).

%% ===========================================================================

suite() ->
    [{timetrap, {seconds, 60}}].

all() ->
    [{group, G} || G <- ?GROUPS].

groups() ->
    [{G, [], [start, send, stop]} || G <- ?GROUPS].

init_per_suite(Config) ->
    ok = diameter:start(),
    Config.

end_per_suite(_Config) ->
    ok = diameter:stop().

init_per_group(Group, Config) ->
    [{group, Group} | Config].

end_per_group(_, _) ->
    ok.

init_per_testcase(_Name, Config) ->
    Config.

end_per_testcase(_, _) ->
    ok.

origin(exit)    -> 0;
origin(handle)  -> 1;
origin(discard) -> 2;

origin(0) -> exit;
origin(1) -> handle;
origin(2) -> discard.

%% ===========================================================================

%% start/1

start(Config) ->
    Group = proplists:get_value(group, Config),
    ok = diameter:start_service(?SERVER, ?SERVICE(?L(Group))),
    ok = diameter:start_service(?CLIENT, ?SERVICE(?CLIENT)),
    LRef = ?util:listen(?SERVER,
                        tcp,
                        [{length_errors, Group}]),
    ?util:connect(?CLIENT,
                  tcp,
                  LRef,
                  [{capabilities, [{'Origin-State-Id', origin(Group)}]}]).

%% stop/1

stop(_Config) ->
    ok = diameter:remove_transport(?CLIENT, true),
    ok = diameter:remove_transport(?SERVER, true),
    ok = diameter:stop_service(?SERVER),
    ok = diameter:stop_service(?CLIENT).

%% send/1

%% Server transport exits on messages of insuffient length.
send(exit) ->
    %% Transport exit is followed by failover but there's only one
    %% transport to choose from.
    {error, failover} = call(4);

%% Server transport receives messages of insufficient length.
send(handle) ->
    %% Message Length too large: diameter_tcp flushes the request
    %% when no additional bytes arrive.
    #diameter_base_STA{'Result-Code' = ?INVALID_MESSAGE_LENGTH}
        = call(4),
    %% Another request answered as it should.
    #diameter_base_STA{'Result-Code' = ?SUCCESS}
        = call(0),
    %% Message Length conveniently small: the trailing optional
    %% Origin-State-Id isn't included in the received request.
    #diameter_base_STA{'Result-Code' = ?SUCCESS}
        = call(-12),
    %% Server receives Origin-State-Id AVP as the first 12 bytes of
    %% the next request: AVP <<Code:32, Flags:8, Len:24, Data:32>> is
    %% interpreted as header <<Version:8, Len:24, Flags:8, Code:24,
    %% ApplId: 32>>. In particular, the AVP Length 12 = 00001100 is
    %% interpreted as Command Flags, so R=0 and the request is
    %% interpreted as an unsolicited answer. Increase Message Length
    %% to have the server receive all bytes sent thusfar.
    {error, timeout}
        = call(12),
    %% Another request answered as it should.
    #diameter_base_STA{'Result-Code' = ?SUCCESS}
        = call(0),
    %% Shorten Message Length so much that that the server doesn't
    %% receive the required Termination-Cause AVP.
    #diameter_base_STA{'Result-Code' = ?MISSING_AVP}
        = call(-24);

%% Server transport discards message of insufficient length.
send(discard) ->
    %% First request times out when the server discards it but a
    %% second succeeds since the transport remains up.
    {error, timeout}
        = call(4),
    #diameter_base_STA{'Result-Code' = ?SUCCESS}
        = call(0);

send(Config) ->
    Group = proplists:get_value(group, Config),
    put({?MODULE, group}, Group),
    send(Group).

%% ===========================================================================

call(Delta) ->
    Group = get({?MODULE, group}),
    diameter:call(?CLIENT,
                  ?DICT,
                  #diameter_base_STR
                  {'Termination-Cause' = ?LOGOUT,
                   'Auth-Application-Id' = ?DIAMETER_APP_ID_COMMON,
                   'Origin-State-Id' = [7]},
                  [{extra, [Group, Delta]}]).

%% ===========================================================================
%% diameter callbacks

%% peer_up/3

peer_up(_SvcName, _Peer, State) ->
    State.

%% peer_down/3

peer_down(_SvcName, _Peer, State) ->
    State.

%% pick_peer/6

pick_peer([Peer], _, ?CLIENT, _State, _Group, _Delta) ->
    {ok, Peer}.

%% prepare_request/5

prepare_request(Pkt, ?CLIENT, {_Ref, Caps}, _Group, Delta) ->
    {send, resize(Delta, prepare(Pkt, Caps))}.

prepare(#diameter_packet{msg = Req0} = Pkt, Caps) ->
    #diameter_caps{origin_host  = {OH, _},
                   origin_realm = {OR, DR}}
        = Caps,
    Req = Req0#diameter_base_STR{'Session-Id' = diameter:session_id(OH),
                                 'Origin-Host' = OH,
                                 'Origin-Realm' = OR,
                                 'Destination-Realm' = DR},
    diameter_codec:encode(?DICT, Pkt#diameter_packet{msg = Req}).

resize(0, Pkt) ->
    Pkt;
resize(Delta, #diameter_packet{bin = Bin} = Pkt) ->
    Pkt#diameter_packet{bin = resize(Delta, Bin)};

resize(Delta, <<V, Len:24, T/binary>>) ->
    <<V, (Len + Delta):24, T/binary>>.

%% handle_answer/6

handle_answer(Pkt, _Req, ?CLIENT, _Peer, _Group, _Delta) ->
    Pkt#diameter_packet.msg.

%% handle_error/6

handle_error(Reason, _Req, ?CLIENT, _Peer, _Group, _Delta) ->
    {error, Reason}.

%% handle_request/3

handle_request(Pkt, ?SERVER, {_Ref, Caps}) ->
    #diameter_caps{origin_host  = {OH, _},
                   origin_realm = {OR, _},
                   origin_state_id = {_,[Id]}}
        = Caps,
    answer(origin(Id),
           Pkt,
           #diameter_base_STA{'Result-Code' = ?SUCCESS,
                              'Session-Id' = diameter:session_id(OH),
                              'Origin-Host' = OH,
                              'Origin-Realm' = OR}).

answer(Group, #diameter_packet{errors = Es}, Ans) ->
    answer(Group, Es, Ans);

answer(_, [], Ans) ->
    {reply, Ans};
answer(Group, [RC|_], Ans)
  when RC == ?INVALID_MESSAGE_LENGTH, Group == handle;
       RC /= ?INVALID_MESSAGE_LENGTH ->
    {reply, Ans}.