Add client support for outgoing maximum packet size.
parent
1877f8a326
commit
b9b8e0ff2a
@ -0,0 +1,59 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
# Test whether a client publishing an oversize packet correctly.
|
||||
# The client should try to publish a message that is too big, then the one below which is ok.
|
||||
# It should also try to subscribe with a too large topic
|
||||
|
||||
from mosq_test_helper import *
|
||||
|
||||
port = mosq_test.get_lib_port()
|
||||
|
||||
rc = 1
|
||||
keepalive = 60
|
||||
connect_packet = mosq_test.gen_connect("publish-qos0-test", keepalive=keepalive, proto_ver=5)
|
||||
props = mqtt5_props.gen_uint32_prop(mqtt5_props.PROP_MAXIMUM_PACKET_SIZE, 30)
|
||||
connack_packet = mosq_test.gen_connack(rc=0, proto_ver=5, properties=props)
|
||||
|
||||
bad_publish_packet = mosq_test.gen_publish("pub/test", qos=0, payload="0123456789012345678", proto_ver=5)
|
||||
publish_packet = mosq_test.gen_publish("pub/test", qos=0, payload="012345678901234567", proto_ver=5)
|
||||
|
||||
disconnect_packet = mosq_test.gen_disconnect()
|
||||
|
||||
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
sock.settimeout(10)
|
||||
sock.bind(('', port))
|
||||
sock.listen(5)
|
||||
|
||||
client_args = sys.argv[1:]
|
||||
env = dict(os.environ)
|
||||
env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp'
|
||||
try:
|
||||
pp = env['PYTHONPATH']
|
||||
except KeyError:
|
||||
pp = ''
|
||||
env['PYTHONPATH'] = '../../lib/python:'+pp
|
||||
client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port)
|
||||
|
||||
try:
|
||||
(conn, address) = sock.accept()
|
||||
conn.settimeout(10)
|
||||
|
||||
if mosq_test.expect_packet(conn, "connect", connect_packet):
|
||||
conn.send(connack_packet)
|
||||
|
||||
if mosq_test.expect_packet(conn, "publish", publish_packet):
|
||||
if mosq_test.expect_packet(conn, "disconnect", disconnect_packet):
|
||||
rc = 0
|
||||
|
||||
conn.close()
|
||||
finally:
|
||||
client.terminate()
|
||||
client.wait()
|
||||
if rc:
|
||||
(stdo, stde) = client.communicate()
|
||||
print(stde)
|
||||
sock.close()
|
||||
|
||||
exit(rc)
|
@ -0,0 +1,72 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <mosquitto.h>
|
||||
|
||||
static int run = -1;
|
||||
static int sent_mid = -1;
|
||||
|
||||
void on_connect(struct mosquitto *mosq, void *obj, int rc)
|
||||
{
|
||||
if(rc){
|
||||
exit(1);
|
||||
}else{
|
||||
rc = mosquitto_subscribe(mosq, NULL, "0123456789012345678901234567890", 0);
|
||||
if(rc != MOSQ_ERR_OVERSIZE_PACKET){
|
||||
printf("Fail on subscribe\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
rc = mosquitto_unsubscribe(mosq, NULL, "0123456789012345678901234567890");
|
||||
if(rc != MOSQ_ERR_OVERSIZE_PACKET){
|
||||
printf("Fail on unsubscribe\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
rc = mosquitto_publish(mosq, &sent_mid, "pub/test", strlen("0123456789012345678"), "0123456789012345678", 0, false);
|
||||
if(rc != MOSQ_ERR_OVERSIZE_PACKET){
|
||||
printf("Fail on publish 1\n");
|
||||
exit(1);
|
||||
}
|
||||
rc = mosquitto_publish(mosq, &sent_mid, "pub/test", strlen("012345678901234567"), "012345678901234567", 0, false);
|
||||
if(rc != MOSQ_ERR_SUCCESS){
|
||||
printf("Fail on publish 2\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void on_publish(struct mosquitto *mosq, void *obj, int mid)
|
||||
{
|
||||
if(mid == sent_mid){
|
||||
mosquitto_disconnect(mosq);
|
||||
run = 0;
|
||||
}else{
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int rc;
|
||||
struct mosquitto *mosq;
|
||||
|
||||
int port = atoi(argv[1]);
|
||||
|
||||
mosquitto_lib_init();
|
||||
|
||||
mosq = mosquitto_new("publish-qos0-test", true, NULL);
|
||||
mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5);
|
||||
mosquitto_connect_callback_set(mosq, on_connect);
|
||||
mosquitto_publish_callback_set(mosq, on_publish);
|
||||
|
||||
rc = mosquitto_connect(mosq, "localhost", port, 60);
|
||||
|
||||
while(run == -1){
|
||||
rc = mosquitto_loop(mosq, -1, 1);
|
||||
}
|
||||
|
||||
mosquitto_lib_cleanup();
|
||||
return run;
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
#include <CUnit/CUnit.h>
|
||||
#include <CUnit/Basic.h>
|
||||
|
||||
#include <mosquitto_internal.h>
|
||||
#include <util_mosq.h>
|
||||
|
||||
|
||||
static void TEST_maximum_packet_size(void)
|
||||
{
|
||||
struct mosquitto mosq;
|
||||
int rc;
|
||||
|
||||
memset(&mosq, 0, sizeof(struct mosquitto));
|
||||
|
||||
mosq.maximum_packet_size = 5;
|
||||
rc = mosquitto_publish(&mosq, NULL, "topic/oversize", strlen("payload"), "payload", 0, 0);
|
||||
CU_ASSERT_EQUAL(rc, MOSQ_ERR_OVERSIZE_PACKET);
|
||||
}
|
||||
|
||||
/* ========================================================================
|
||||
* TEST SUITE SETUP
|
||||
* ======================================================================== */
|
||||
|
||||
int init_publish_tests(void)
|
||||
{
|
||||
CU_pSuite test_suite = NULL;
|
||||
|
||||
test_suite = CU_add_suite("Publish", NULL, NULL);
|
||||
if(!test_suite){
|
||||
printf("Error adding CUnit Publish test suite.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(0
|
||||
|| !CU_add_test(test_suite, "v5: Maximum packet size", TEST_maximum_packet_size)
|
||||
){
|
||||
|
||||
printf("Error adding Publish CUnit tests.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue