Merge pull request #1792 from huangdan/master
Add test cases for mqtt5 connect packet
This commit is contained in:
commit
cb058143a5
|
@ -17,8 +17,6 @@
|
||||||
-compile(export_all).
|
-compile(export_all).
|
||||||
-compile(nowarn_export_all).
|
-compile(nowarn_export_all).
|
||||||
|
|
||||||
-include("emqx_mqtt.hrl").
|
|
||||||
|
|
||||||
-define(APP, emqx).
|
-define(APP, emqx).
|
||||||
|
|
||||||
-include_lib("eunit/include/eunit.hrl").
|
-include_lib("eunit/include/eunit.hrl").
|
||||||
|
@ -52,9 +50,7 @@
|
||||||
-define(PUBPACKET, ?PUBLISH_PACKET(?PUBQOS, <<"sub/topic">>, ?PACKETID, <<"publish">>)).
|
-define(PUBPACKET, ?PUBLISH_PACKET(?PUBQOS, <<"sub/topic">>, ?PACKETID, <<"publish">>)).
|
||||||
|
|
||||||
all() ->
|
all() ->
|
||||||
[{group, connect}%,
|
[{group, connect}].
|
||||||
% {group, cleanSession}
|
|
||||||
].
|
|
||||||
|
|
||||||
groups() ->
|
groups() ->
|
||||||
[{connect, [non_parallel_tests],
|
[{connect, [non_parallel_tests],
|
||||||
|
@ -64,11 +60,7 @@ groups() ->
|
||||||
mqtt_connect_with_ssl_oneway,
|
mqtt_connect_with_ssl_oneway,
|
||||||
mqtt_connect_with_ssl_twoway,
|
mqtt_connect_with_ssl_twoway,
|
||||||
mqtt_connect_with_ws
|
mqtt_connect_with_ws
|
||||||
]},
|
]}].
|
||||||
{cleanSession, [sequence],
|
|
||||||
[cleanSession_validate]
|
|
||||||
}
|
|
||||||
].
|
|
||||||
|
|
||||||
init_per_suite(Config) ->
|
init_per_suite(Config) ->
|
||||||
emqx_ct_broker_helpers:run_setup_steps(),
|
emqx_ct_broker_helpers:run_setup_steps(),
|
||||||
|
@ -109,15 +101,17 @@ mqtt_connect_with_ssl_oneway(_) ->
|
||||||
emqx_ct_broker_helpers:change_opts(ssl_oneway),
|
emqx_ct_broker_helpers:change_opts(ssl_oneway),
|
||||||
emqx:start(),
|
emqx:start(),
|
||||||
ClientSsl = emqx_ct_broker_helpers:client_ssl(),
|
ClientSsl = emqx_ct_broker_helpers:client_ssl(),
|
||||||
{ok, #ssl_socket{tcp = Sock, ssl = SslSock}}
|
{ok, #ssl_socket{tcp = _Sock1, ssl = SslSock} = Sock}
|
||||||
= emqx_client_sock:connect("127.0.0.1", 8883, [{ssl_opts, ClientSsl}], 3000),
|
= emqx_client_sock:connect("127.0.0.1", 8883, [{ssl_opts, ClientSsl}], 3000),
|
||||||
%% Packet = raw_send_serialise(?CLIENT),
|
Packet = raw_send_serialise(?CLIENT),
|
||||||
%% ssl:send(SslSock, Packet),
|
emqx_client_sock:setopts(Sock, [{active, once}]),
|
||||||
%% receive Data ->
|
emqx_client_sock:send(Sock, Packet),
|
||||||
%% ct:log("Data:~p~n", [Data])
|
?assert(
|
||||||
%% after 30000 ->
|
receive {ssl, _, ConAck}->
|
||||||
%% ok
|
{ok, ?CONNACK_PACKET(?CONNACK_ACCEPT), _} = raw_recv_pase(ConAck), true
|
||||||
%% end,
|
after 1000 ->
|
||||||
|
false
|
||||||
|
end),
|
||||||
ssl:close(SslSock).
|
ssl:close(SslSock).
|
||||||
|
|
||||||
mqtt_connect_with_ssl_twoway(_Config) ->
|
mqtt_connect_with_ssl_twoway(_Config) ->
|
||||||
|
@ -131,11 +125,12 @@ mqtt_connect_with_ssl_twoway(_Config) ->
|
||||||
emqx_client_sock:setopts(Sock, [{active, once}]),
|
emqx_client_sock:setopts(Sock, [{active, once}]),
|
||||||
emqx_client_sock:send(Sock, Packet),
|
emqx_client_sock:send(Sock, Packet),
|
||||||
timer:sleep(500),
|
timer:sleep(500),
|
||||||
|
?assert(
|
||||||
receive {ssl, _, Data}->
|
receive {ssl, _, Data}->
|
||||||
{ok, ?CONNACK_PACKET(?CONNACK_ACCEPT), _} = raw_recv_pase(Data)
|
{ok, ?CONNACK_PACKET(?CONNACK_ACCEPT), _} = raw_recv_pase(Data), true
|
||||||
after 1000 ->
|
after 1000 ->
|
||||||
ok
|
false
|
||||||
end,
|
end),
|
||||||
emqx_client_sock:close(Sock).
|
emqx_client_sock:close(Sock).
|
||||||
|
|
||||||
mqtt_connect_with_ws(_Config) ->
|
mqtt_connect_with_ws(_Config) ->
|
||||||
|
@ -162,32 +157,6 @@ mqtt_connect_with_ws(_Config) ->
|
||||||
{close, _} = rfc6455_client:close(WS),
|
{close, _} = rfc6455_client:close(WS),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
cleanSession_validate(_) ->
|
|
||||||
{ok, C1} = emqttc:start_link([{host, "localhost"},
|
|
||||||
{port, 1883},
|
|
||||||
{client_id, <<"c1">>},
|
|
||||||
{clean_sess, false}]),
|
|
||||||
timer:sleep(10),
|
|
||||||
emqttc:subscribe(C1, <<"topic">>, qos0),
|
|
||||||
emqttc:disconnect(C1),
|
|
||||||
{ok, Pub} = emqttc:start_link([{host, "localhost"},
|
|
||||||
{port, 1883},
|
|
||||||
{client_id, <<"pub">>}]),
|
|
||||||
|
|
||||||
emqttc:publish(Pub, <<"topic">>, <<"m1">>, [{qos, 0}]),
|
|
||||||
timer:sleep(10),
|
|
||||||
{ok, C11} = emqttc:start_link([{host, "localhost"},
|
|
||||||
{port, 1883},
|
|
||||||
{client_id, <<"c1">>},
|
|
||||||
{clean_sess, false}]),
|
|
||||||
timer:sleep(100),
|
|
||||||
receive {publish, _Topic, M1} ->
|
|
||||||
?assertEqual(<<"m1">>, M1)
|
|
||||||
after 1000 -> false
|
|
||||||
end,
|
|
||||||
emqttc:disconnect(Pub),
|
|
||||||
emqttc:disconnect(C11).
|
|
||||||
|
|
||||||
raw_send_serialise(Packet) ->
|
raw_send_serialise(Packet) ->
|
||||||
emqx_frame:serialize(Packet).
|
emqx_frame:serialize(Packet).
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,117 @@
|
||||||
|
%%%===================================================================
|
||||||
|
%%% Copyright (c) 2013-2018 EMQ Inc. 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.
|
||||||
|
%%%===================================================================
|
||||||
|
|
||||||
|
-module(emqx_mqtt_packet_SUITE).
|
||||||
|
|
||||||
|
-compile(export_all).
|
||||||
|
-compile(nowarn_export_all).
|
||||||
|
|
||||||
|
-import(emqx_frame, [serialize/1]).
|
||||||
|
|
||||||
|
-include("emqx_mqtt.hrl").
|
||||||
|
|
||||||
|
-include_lib("eunit/include/eunit.hrl").
|
||||||
|
|
||||||
|
-define(INVALID_RESERVED, 1).
|
||||||
|
|
||||||
|
-define(CONNECT_INVALID_PACKET(Var),
|
||||||
|
#mqtt_packet{header = #mqtt_packet_header{type = ?INVALID_RESERVED},
|
||||||
|
variable = Var}).
|
||||||
|
|
||||||
|
-define(CASE1_PROTOCOL_NAME, ?CONNECT_PACKET(#mqtt_packet_connect{
|
||||||
|
proto_name = <<"MQTC">>,
|
||||||
|
client_id = <<"mqtt_protocol_name">>,
|
||||||
|
username = <<"admin">>,
|
||||||
|
password = <<"public">>})).
|
||||||
|
|
||||||
|
-define(CASE2_PROTOCAL_VER, ?CONNECT_PACKET(#mqtt_packet_connect{
|
||||||
|
client_id = <<"mqtt_client">>,
|
||||||
|
proto_ver = 6,
|
||||||
|
username = <<"admin">>,
|
||||||
|
password = <<"public">>})).
|
||||||
|
|
||||||
|
-define(CASE3_PROTOCAL_INVALID_RESERVED, ?CONNECT_INVALID_PACKET(#mqtt_packet_connect{
|
||||||
|
client_id = <<"mqtt_client">>,
|
||||||
|
proto_ver = 5,
|
||||||
|
username = <<"admin">>,
|
||||||
|
password = <<"public">>})).
|
||||||
|
|
||||||
|
-define(PROTOCOL5, ?CONNECT_PACKET(#mqtt_packet_connect{
|
||||||
|
proto_ver = 5,
|
||||||
|
keepalive = 60,
|
||||||
|
properties = #{'Message-Expiry-Interval' => 3600},
|
||||||
|
client_id = <<"mqtt_client">>,
|
||||||
|
will_topic = <<"will_tipic">>,
|
||||||
|
will_payload = <<"will message">>,
|
||||||
|
username = <<"admin">>,
|
||||||
|
password = <<"public">>})).
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
all() -> [{group, connect}].
|
||||||
|
|
||||||
|
groups() -> [{connect, [sequence],
|
||||||
|
[case1_protocol_name,
|
||||||
|
case2_protocol_ver%,
|
||||||
|
%TOTO case3_invalid_reserved
|
||||||
|
]}].
|
||||||
|
|
||||||
|
init_per_suite(Config) ->
|
||||||
|
emqx_ct_broker_helpers:run_setup_steps(),
|
||||||
|
Config.
|
||||||
|
|
||||||
|
end_per_suite(_Config) ->
|
||||||
|
emqx_ct_broker_helpers:run_teardown_steps().
|
||||||
|
|
||||||
|
init_per_group(_Group, Config) ->
|
||||||
|
Config.
|
||||||
|
|
||||||
|
end_per_group(_Group, _Config) ->
|
||||||
|
ok.
|
||||||
|
|
||||||
|
case1_protocol_name(_) ->
|
||||||
|
{ok, Sock} = emqx_client_sock:connect({127,0,0,1}, 1883, [binary, {packet, raw}, {active, false}], 3000),
|
||||||
|
MqttPacket = serialize(?CASE1_PROTOCOL_NAME),
|
||||||
|
emqx_client_sock:send(Sock, MqttPacket),
|
||||||
|
{ok, Data} = gen_tcp:recv(Sock, 0),
|
||||||
|
{ok, ?CONNACK_PACKET(?CONNACK_PROTO_VER), _} = raw_recv_pase(Data),
|
||||||
|
Disconnect = gen_tcp:recv(Sock, 0),
|
||||||
|
?assertEqual({error, closed}, Disconnect).
|
||||||
|
|
||||||
|
case2_protocol_ver(_) ->
|
||||||
|
{ok, Sock} = emqx_client_sock:connect({127,0,0,1}, 1883, [binary, {packet, raw}, {active, false}], 3000),
|
||||||
|
Packet = serialize(?CASE2_PROTOCAL_VER),
|
||||||
|
emqx_client_sock:send(Sock, Packet),
|
||||||
|
{ok, Data} = gen_tcp:recv(Sock, 0),
|
||||||
|
%% case1 Unacceptable protocol version
|
||||||
|
{ok, ?CONNACK_PACKET(?CONNACK_PROTO_VER), _} = raw_recv_pase(Data),
|
||||||
|
Disconnect = gen_tcp:recv(Sock, 0),
|
||||||
|
?assertEqual({error, closed}, Disconnect).
|
||||||
|
|
||||||
|
case3_invalid_reserved(_) ->
|
||||||
|
{ok, Sock} = emqx_client_sock:connect({127,0,0,1}, 1883, [binary, {packet, raw}, {active, false}], 3000),
|
||||||
|
Packet = serialize(?CASE3_PROTOCAL_INVALID_RESERVED),
|
||||||
|
emqx_client_sock:send(Sock, Packet),
|
||||||
|
{ok, Data} = gen_tcp:recv(Sock, 0),
|
||||||
|
%% case1 Unacceptable protocol version
|
||||||
|
ct:log("Data:~p~n", [raw_recv_pase(Data)]),
|
||||||
|
{ok, ?CONNACK_PACKET(?CONNACK_PROTO_VER), _} = raw_recv_pase(Data),
|
||||||
|
Disconnect = gen_tcp:recv(Sock, 0),
|
||||||
|
?assertEqual({error, closed}, Disconnect).
|
||||||
|
|
||||||
|
raw_recv_pase(P) ->
|
||||||
|
emqx_frame:parse(P, {none, #{max_packet_size => ?MAX_PACKET_SIZE,
|
||||||
|
version => ?MQTT_PROTO_V4} }).
|
Loading…
Reference in New Issue