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
|
%%
%% %CopyrightBegin%
%%
%% SPDX-License-Identifier: Apache-2.0
%%
%% Copyright Ericsson AB 2013-2025. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
%%
%% Tests of transport_opt() length_errors.
%%
-module(diameter_length_SUITE).
%% testcases, no common_test dependency
-export([run/0,
run/1]).
%% common_test wrapping
-export([suite/0,
all/0,
parallel/1]).
%% diameter callbacks
-export([peer_up/3,
peer_down/3,
pick_peer/5,
prepare_request/4,
handle_answer/5,
handle_error/5,
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_SUCCESS').
-define(MISSING_AVP,
?'DIAMETER_BASE_RESULT-CODE_MISSING_AVP').
-define(INVALID_MESSAGE_LENGTH,
?'DIAMETER_BASE_RESULT-CODE_INVALID_MESSAGE_LENGTH').
-define(LOGOUT,
?'DIAMETER_BASE_TERMINATION-CAUSE_LOGOUT').
-define(L, atom_to_list).
%% ===========================================================================
suite() ->
[{timetrap, {seconds, 120}}].
all() ->
[parallel].
parallel(_Config) ->
run().
%% ===========================================================================
origin(exit) -> 0;
origin(handle) -> 1;
origin(discard) -> 2;
origin(0) -> exit;
origin(1) -> handle;
origin(2) -> discard.
%% ===========================================================================
run() ->
run([exit, handle, discard]).
run(List)
when is_list(List) ->
ok = diameter:start(),
try
?util:run([[{[fun run/1, T], 30000} || T <- List]])
after
ok = diameter:stop()
end;
run(Group) ->
start(Group),
send(Group),
stop().
%% start/1
start(Group) ->
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/0
stop() ->
ok = diameter:remove_transport(?SERVER, true),
ok = diameter:remove_transport(?CLIENT, 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).
%% ===========================================================================
call(Delta) ->
diameter:call(?CLIENT,
?DICT,
#diameter_base_STR
{'Termination-Cause' = ?LOGOUT,
'Auth-Application-Id' = ?DIAMETER_APP_ID_COMMON,
'Origin-State-Id' = [7]},
[{extra, [Delta]}]).
%% ===========================================================================
%% diameter callbacks
%% peer_up/3
peer_up(_SvcName, _Peer, State) ->
State.
%% peer_down/3
peer_down(_SvcName, _Peer, State) ->
State.
%% pick_peer/5
pick_peer([Peer], _, ?CLIENT, _State, _Delta) ->
{ok, Peer}.
%% prepare_request/4
prepare_request(Pkt, ?CLIENT, {_Ref, Caps}, 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/5
handle_answer(Pkt, _Req, ?CLIENT, _Peer, _Delta) ->
Pkt#diameter_packet.msg.
%% handle_error/5
handle_error(Reason, _Req, ?CLIENT, _Peer, _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);
%% No errors: just answer.
answer(_, [], Ans) ->
{reply, Ans};
%% Otherwise an invalid length should only reach the callback if
%% length_errors = handle.
answer(Group, [RC|_], Ans)
when RC == ?INVALID_MESSAGE_LENGTH, Group == handle;
RC /= ?INVALID_MESSAGE_LENGTH ->
{reply, Ans}.
|