Skip to content

Commit

Permalink
Add test for duplicate CONNECT.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralight committed Dec 19, 2018
1 parent ec11788 commit dab6452
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/handle_connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ int handle__connect(struct mosquitto_db *db, struct mosquitto *context)

/* Don't accept multiple CONNECT commands. */
if(context->state != mosq_cs_new){
log__printf(NULL, MOSQ_LOG_NOTICE, "Bad client %s sending multiple CONNECT messages.", context->id);
rc = MOSQ_ERR_PROTOCOL;
goto handle_connect_error;
}
Expand Down
32 changes: 32 additions & 0 deletions test/broker/01-connect-duplicate-v5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python

# Test whether a duplicate CONNECT is rejected. MQTT v5

from mosq_test_helper import *

rc = 1
keepalive = 10
connect_packet = mosq_test.gen_connect("connect-test", keepalive=keepalive, proto_ver=5)
connack_packet = mosq_test.gen_connack(rc=0, proto_ver=5)

port = mosq_test.get_port()
broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port)

try:
sock = mosq_test.do_client_connect(connect_packet, connack_packet, port=port)
sock.settimeout(3)
sock.send(connect_packet)
data = sock.recv(1)
if len(data) == 0:
rc = 0
except socket.error:
rc = 0
finally:
broker.terminate()
broker.wait()
(stdo, stde) = broker.communicate()
if rc:
print(stde)

exit(rc)

32 changes: 32 additions & 0 deletions test/broker/01-connect-duplicate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python

# Test whether a duplicate CONNECT is rejected.

from mosq_test_helper import *

rc = 1
keepalive = 10
connect_packet = mosq_test.gen_connect("connect-test", keepalive=keepalive)
connack_packet = mosq_test.gen_connack(rc=0)

port = mosq_test.get_port()
broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port)

try:
sock = mosq_test.do_client_connect(connect_packet, connack_packet, port=port)
sock.settimeout(3)
sock.send(connect_packet)
data = sock.recv(1)
if len(data) == 0:
rc = 0
except socket.error:
rc = 0
finally:
broker.terminate()
broker.wait()
(stdo, stde) = broker.communicate()
if rc:
print(stde)

exit(rc)

2 changes: 2 additions & 0 deletions test/broker/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ ptest : test-compile
test : test-compile 01 02 03 04 05 06 07 08 09 10 11 12

01 :
./01-connect-duplicate.py
./01-connect-duplicate-v5.py
./01-connect-success.py
./01-connect-success-v5.py
./01-connect-invalid-protonum.py
Expand Down
2 changes: 2 additions & 0 deletions test/broker/ptest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
max_running = 10
tests = [
#(ports required, 'path'),
(1, './01-connect-duplicate.py'),
(1, './01-connect-duplicate-v5.py'),
(1, './01-connect-success.py'),
(1, './01-connect-success-v5.py'),
(1, './01-connect-invalid-protonum.py'),
Expand Down

0 comments on commit dab6452

Please sign in to comment.