mirror of
https://github.com/cesanta/mongoose.git
synced 2025-08-06 13:37:34 +08:00
Add MQTT test for MIP
This commit is contained in:
parent
3256218fd2
commit
b4c645f703
@ -14,7 +14,9 @@
|
|||||||
#include <net/if_types.h>
|
#include <net/if_types.h>
|
||||||
#endif
|
#endif
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
|
|
||||||
#include "mongoose.c"
|
#include "mongoose.c"
|
||||||
|
|
||||||
#include "driver_mock.c"
|
#include "driver_mock.c"
|
||||||
|
|
||||||
static int s_num_tests = 0;
|
static int s_num_tests = 0;
|
||||||
@ -108,10 +110,11 @@ char *fetch(struct mg_mgr *mgr, const char *url, const char *fn_data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mg_mgr_poll(mgr, 0);
|
mg_mgr_poll(mgr, 0);
|
||||||
if (!post_reply.http_responses_received)
|
if (!post_reply.http_responses_received) {
|
||||||
return 0;
|
return 0;
|
||||||
else
|
} else {
|
||||||
return (char *) post_reply.http_response;
|
return (char *) post_reply.http_response;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns server's HTTP response code
|
// Returns server's HTTP response code
|
||||||
@ -127,14 +130,86 @@ int get_response_code(char *http_msg_raw) {
|
|||||||
return http_status;
|
return http_status;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_http_fetch(void) {
|
static void test_http_fetch(struct mg_mgr *mgr) {
|
||||||
|
char *http_feedback = (char *) "";
|
||||||
|
const bool ipv6 = 0;
|
||||||
|
if (ipv6) {
|
||||||
|
http_feedback = fetch(mgr, "ipv6.google.com",
|
||||||
|
"GET/ HTTP/1.0\r\nHost: ipv6.google.com\r\n\r\n");
|
||||||
|
} else {
|
||||||
|
http_feedback =
|
||||||
|
fetch(mgr, "http://cesanta.com",
|
||||||
|
"GET //robots.txt HTTP/1.0\r\nHost: cesanta.com\r\n\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
ASSERT(http_feedback != NULL &&
|
||||||
|
*http_feedback != '\0'); // HTTP response received ?
|
||||||
|
|
||||||
|
int http_status = get_response_code(http_feedback);
|
||||||
|
// printf("Server response HTTP status code: %d\n",http_status);
|
||||||
|
ASSERT(http_status != 0);
|
||||||
|
ASSERT(http_status == 301); // OK: Permanently moved (HTTP->HTTPS redirect)
|
||||||
|
|
||||||
|
if (http_feedback) {
|
||||||
|
free(http_feedback);
|
||||||
|
http_feedback = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct mg_connection *s_conn;
|
||||||
|
static char s_topic[16];
|
||||||
|
|
||||||
|
static void mqtt_fn(struct mg_connection *c, int ev, void *ev_data,
|
||||||
|
void *fn_data) {
|
||||||
|
if (ev == MG_EV_MQTT_OPEN) {
|
||||||
|
// MQTT connect is successful
|
||||||
|
struct mg_mqtt_opts sub_opts;
|
||||||
|
memset(&sub_opts, 0, sizeof(sub_opts));
|
||||||
|
sub_opts.topic = mg_str(mg_random_str(s_topic, sizeof(s_topic)));
|
||||||
|
sub_opts.qos = 1;
|
||||||
|
mg_mqtt_sub(c, &sub_opts);
|
||||||
|
struct mg_mqtt_opts pub_opts;
|
||||||
|
memset(&pub_opts, 0, sizeof(pub_opts));
|
||||||
|
pub_opts.topic = sub_opts.topic;
|
||||||
|
pub_opts.message = mg_str("hi");
|
||||||
|
pub_opts.qos = 1, pub_opts.retain = false;
|
||||||
|
mg_mqtt_pub(c, &pub_opts);
|
||||||
|
} else if (ev == MG_EV_MQTT_MSG) {
|
||||||
|
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
|
||||||
|
if (mm->topic.len != strlen(s_topic) || strcmp(mm->topic.ptr, s_topic))
|
||||||
|
ASSERT(0);
|
||||||
|
if (mm->data.len != 2 || strcmp(mm->data.ptr, "hi")) ASSERT(0);
|
||||||
|
mg_mqtt_disconnect(c, NULL);
|
||||||
|
*(bool *) fn_data = true;
|
||||||
|
} else if (ev == MG_EV_CLOSE) {
|
||||||
|
s_conn = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_mqtt_connsubpub(struct mg_mgr *mgr) {
|
||||||
|
const char *url = "mqtt://broker.hivemq.com:1883";
|
||||||
|
bool passed = false;
|
||||||
|
struct mg_mqtt_opts opts;
|
||||||
|
memset(&opts, 0, sizeof(opts));
|
||||||
|
opts.clean = true, opts.version = 4;
|
||||||
|
s_conn = mg_mqtt_connect(mgr, url, &opts, mqtt_fn, &passed);
|
||||||
|
ASSERT(s_conn != NULL);
|
||||||
|
for (int i = 0; i < 500 && s_conn != NULL && !s_conn->is_closing; i++) {
|
||||||
|
mg_mgr_poll(mgr, 0);
|
||||||
|
usleep(10000); // 10 ms. Slow down poll loop to ensure packets transit
|
||||||
|
}
|
||||||
|
ASSERT(passed);
|
||||||
|
mg_mgr_poll(mgr, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
// Setup interface
|
// Setup interface
|
||||||
const char *iface = "tap0"; // Network iface
|
const char *iface = "tap0"; // Network iface
|
||||||
const char *mac = "00:00:01:02:03:78"; // MAC address
|
const char *mac = "00:00:01:02:03:78"; // MAC address
|
||||||
#ifndef __OpenBSD__
|
#ifndef __OpenBSD__
|
||||||
const char* tuntap_device = "/dev/net/tun";
|
const char *tuntap_device = "/dev/net/tun";
|
||||||
#else
|
#else
|
||||||
const char* tuntap_device = "/dev/tap0";
|
const char *tuntap_device = "/dev/tap0";
|
||||||
#endif
|
#endif
|
||||||
int fd = open(tuntap_device, O_RDWR);
|
int fd = open(tuntap_device, O_RDWR);
|
||||||
struct ifreq ifr;
|
struct ifreq ifr;
|
||||||
@ -147,7 +222,7 @@ static void test_http_fetch(void) {
|
|||||||
abort(); // return EXIT_FAILURE;
|
abort(); // return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
ifr.ifr_flags = (short)(IFF_UP | IFF_BROADCAST | IFF_MULTICAST);
|
ifr.ifr_flags = (short) (IFF_UP | IFF_BROADCAST | IFF_MULTICAST);
|
||||||
if (ioctl(fd, TUNSIFMODE, (void *) &ifr) < 0) {
|
if (ioctl(fd, TUNSIFMODE, (void *) &ifr) < 0) {
|
||||||
MG_ERROR(("Failed to setup TAP interface: %s", ifr.ifr_name));
|
MG_ERROR(("Failed to setup TAP interface: %s", ifr.ifr_name));
|
||||||
abort(); // return EXIT_FAILURE;
|
abort(); // return EXIT_FAILURE;
|
||||||
@ -177,7 +252,7 @@ static void test_http_fetch(void) {
|
|||||||
|
|
||||||
#if MG_USING_DHCP == 1
|
#if MG_USING_DHCP == 1
|
||||||
#else
|
#else
|
||||||
mif.ip = mg_htonl(MG_U32(192, 168, 32, 2)); // Triggering a network failure
|
mif.ip = mg_htonl(MG_U32(192, 168, 32, 2)); // Triggering a network failure
|
||||||
mif.mask = mg_htonl(MG_U32(255, 255, 255, 0));
|
mif.mask = mg_htonl(MG_U32(255, 255, 255, 0));
|
||||||
mif.gw = mg_htonl(MG_U32(192, 168, 32, 1));
|
mif.gw = mg_htonl(MG_U32(192, 168, 32, 1));
|
||||||
#endif
|
#endif
|
||||||
@ -189,61 +264,31 @@ static void test_http_fetch(void) {
|
|||||||
MG_INFO(("Init done, starting main loop"));
|
MG_INFO(("Init done, starting main loop"));
|
||||||
|
|
||||||
// Stack initialization, Network configuration (DHCP lease, ...)
|
// Stack initialization, Network configuration (DHCP lease, ...)
|
||||||
{
|
|
||||||
#if MG_USING_DHCP == 0
|
#if MG_USING_DHCP == 0
|
||||||
MG_INFO(("MIF configuration: Static IP"));
|
MG_INFO(("MIF configuration: Static IP"));
|
||||||
ASSERT(mif.ip != 0); // Check we have a satic IP assigned
|
ASSERT(mif.ip != 0); // Check we have a satic IP assigned
|
||||||
mg_mgr_poll(&mgr, 100); // For initialisation
|
mg_mgr_poll(&mgr, 100); // For initialisation
|
||||||
#else
|
#else
|
||||||
MG_INFO(("MIF configuration: DHCP"));
|
MG_INFO(("MIF configuration: DHCP"));
|
||||||
MG_INFO(("Opened TAP interface: %s", iface));
|
ASSERT(!mif.ip); // Check we are set for DHCP
|
||||||
ASSERT(!mif.ip); // Check we are set for DHCP
|
int pc = 500; // Timout on DHCP lease 500 ~ approx 5s (typical delay <1s)
|
||||||
int pc = 500; // Timout on DHCP lease 500 ~ approx 5s (typical delay <1s)
|
while (((pc--) > 0) && !mif.ip) {
|
||||||
while (((pc--) > 0) && !mif.ip) {
|
mg_mgr_poll(&mgr, 100);
|
||||||
mg_mgr_poll(&mgr, 100);
|
usleep(10000); // 10 ms
|
||||||
usleep(10000); // 10 ms
|
}
|
||||||
}
|
if (!mif.ip) MG_ERROR(("No ip assigned (DHCP lease may have failed).\n"));
|
||||||
if (!mif.ip) MG_ERROR(("No ip assigned (DHCP lease may have failed).\n"));
|
ASSERT(mif.ip); // We have an IP (lease or static)
|
||||||
ASSERT(mif.ip); // We have an IP (lease or static)
|
|
||||||
#endif
|
#endif
|
||||||
}
|
|
||||||
|
|
||||||
// Simple HTTP fetch
|
// RUN TESTS
|
||||||
{
|
test_http_fetch(&mgr);
|
||||||
char *http_feedback = (char *) "";
|
test_mqtt_connsubpub(&mgr);
|
||||||
const bool ipv6 = 0;
|
printf("SUCCESS. Total tests: %d\n", s_num_tests);
|
||||||
if (ipv6) {
|
|
||||||
http_feedback = fetch(&mgr, "ipv6.google.com",
|
|
||||||
"GET/ HTTP/1.0\r\nHost: ipv6.google.com\r\n\r\n");
|
|
||||||
} else {
|
|
||||||
http_feedback =
|
|
||||||
fetch(&mgr, "http://cesanta.com",
|
|
||||||
"GET //robots.txt HTTP/1.0\r\nHost: cesanta.com\r\n\r\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
ASSERT(http_feedback != NULL &&
|
|
||||||
*http_feedback != '\0'); // HTTP response received ?
|
|
||||||
|
|
||||||
int http_status = get_response_code(http_feedback);
|
|
||||||
// printf("Server response HTTP status code: %d\n",http_status);
|
|
||||||
ASSERT(http_status != 0);
|
|
||||||
ASSERT(http_status == 301); // OK: Permanently moved (HTTP->HTTPS redirect)
|
|
||||||
|
|
||||||
if (http_feedback) {
|
|
||||||
free(http_feedback);
|
|
||||||
http_feedback = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clear
|
// Clear
|
||||||
mg_mgr_free(&mgr);
|
mg_mgr_free(&mgr);
|
||||||
mg_tcpip_free(&mif); // Release after mg_mgr
|
mg_tcpip_free(&mif); // Release after mg_mgr
|
||||||
ASSERT(mgr.conns == NULL); // Deconstruction OK
|
ASSERT(mgr.conns == NULL); // Deconstruction OK
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
|
||||||
|
|
||||||
int main(void) {
|
|
||||||
test_http_fetch();
|
|
||||||
printf("SUCCESS. Total tests: %d\n", s_num_tests);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user