int test10(struct Options options) { int rc = 0; unsigned char buf[100]; int buflen = sizeof(buf); unsigned short packetid = 255, packetid2 = 0; unsigned short topicid = 233, topicid2 = 0; unsigned char return_code = 127, return_code2 = 0; fprintf(xml, "<testcase classname=\"test10\" name=\"de/serialization\""); global_start_time = start_clock(); failures = 0; MyLog(LOGA_INFO, "Starting test 10 - serialization of regack and back"); rc = MQTTSNSerialize_regack(buf, buflen, topicid, packetid, return_code); assert("good rc from serialize regack", rc > 0, "rc was %d\n", rc); rc = MQTTSNDeserialize_regack(&topicid2, &packetid2, &return_code2, buf, buflen); assert("good rc from deserialize regack", rc == 1, "rc was %d\n", rc); assert("packetids should be the same", packetid == packetid2, "packetids were different %d\n", packetid2); assert("topicids should be the same", topicid == topicid2, "topicids were different %d\n", topicid2); assert("return codes should be the same", return_code == return_code2, "return_codes were different %d\n", return_code2); /* exit: */ MyLog(LOGA_INFO, "TEST10: test %s. %d tests run, %d failures.", (failures == 0) ? "passed" : "failed", tests, failures); write_test_result(); return failures; }
int test11(struct Options options) { int rc = 0; unsigned char buf[100]; int buflen = sizeof(buf); unsigned char gatewayid = 255, gatewayid2 = 0; unsigned short duration = 3233, duration2 = 0; fprintf(xml, "<testcase classname=\"test11\" name=\"de/serialization\""); global_start_time = start_clock(); failures = 0; MyLog(LOGA_INFO, "Starting test 11 - serialization of advertise and back"); rc = MQTTSNSerialize_advertise(buf, buflen, gatewayid, duration); assert("good rc from serialize advertise", rc > 0, "rc was %d\n", rc); rc = MQTTSNDeserialize_advertise(&gatewayid2, &duration2, buf, buflen); assert("good rc from deserialize advertise", rc == 1, "rc was %d\n", rc); assert("gatewayids should be the same", gatewayid == gatewayid2, "gatewayids were different %d\n", gatewayid2); assert("return codes should be the same", duration == duration2, "return_codes were different %d\n", duration2); /* exit: */ MyLog(LOGA_INFO, "TEST11: test %s. %d tests run, %d failures.", (failures == 0) ? "passed" : "failed", tests, failures); write_test_result(); return failures; }
int test12(struct Options options) { int rc = 0; unsigned char buf[100]; int buflen = sizeof(buf); unsigned char radius = 255, radius2 = 0; fprintf(xml, "<testcase classname=\"test12\" name=\"de/serialization\""); global_start_time = start_clock(); failures = 0; MyLog(LOGA_INFO, "Starting test 12 - serialization of searchgw and back"); rc = MQTTSNSerialize_searchgw(buf, buflen, radius); assert("good rc from serialize searchgw", rc > 0, "rc was %d\n", rc); rc = MQTTSNDeserialize_searchgw(&radius2, buf, buflen); assert("good rc from deserialize searchgw", rc == 1, "rc was %d\n", rc); assert("radiuss should be the same", radius == radius2, "radiuss were different %d\n", radius2); /* exit: */ MyLog(LOGA_INFO, "TEST12: test %s. %d tests run, %d failures.", (failures == 0) ? "passed" : "failed", tests, failures); write_test_result(); return failures; }
int test13(struct Options options) { int rc = 0; unsigned char buf[100]; int buflen = sizeof(buf); unsigned char gatewayid = 255, gatewayid2 = 0; unsigned short gatewayaddress_len = 16, gatewayaddress_len2 = 0; unsigned char gatewayaddress[40] = "a gateway address", *gatewayaddress2 = NULL; fprintf(xml, "<testcase classname=\"test11\" name=\"de/serialization\""); global_start_time = start_clock(); failures = 0; MyLog(LOGA_INFO, "Starting test 13 - serialization of gwinfo and back"); rc = MQTTSNSerialize_gwinfo(buf, buflen, gatewayid, gatewayaddress_len, gatewayaddress); assert("good rc from serialize gwinfo", rc > 0, "rc was %d\n", rc); rc = MQTTSNDeserialize_gwinfo(&gatewayid2, &gatewayaddress_len2, &gatewayaddress2, buf, buflen); assert("good rc from deserialize gwinfo", rc == 1, "rc was %d\n", rc); assert("gatewayids should be the same", gatewayid == gatewayid2, "gatewayids were different %d\n", gatewayid2); assert("gateway lengths should be the same", gatewayaddress_len == gatewayaddress_len2, "gateway lengths were different %d\n", gatewayaddress_len2); assert("gateway addresses should be the same", memcmp(gatewayaddress, gatewayaddress2, gatewayaddress_len) == 0, "gateway addresses were different %.10s\n", gatewayaddress2); /* exit: */ MyLog(LOGA_INFO, "TEST13: test %s. %d tests run, %d failures.", (failures == 0) ? "passed" : "failed", tests, failures); write_test_result(); return failures; }
int test6(struct Options options) { int rc = 0; unsigned char buf[100]; int buflen = sizeof(buf); int connack_rc = 77; int connack_rc2 = 0; fprintf(xml, "<testcase classname=\"test6\" name=\"de/serialization\""); global_start_time = start_clock(); failures = 0; MyLog(LOGA_INFO, "Starting test 6 - serialization of connack and back"); rc = MQTTSNSerialize_connack(buf, buflen, connack_rc); assert("good rc from serialize connack", rc > 0, "rc was %d\n", rc); rc = MQTTSNDeserialize_connack(&connack_rc2, buf, buflen); assert("good rc from deserialize connack", rc == 1, "rc was %d\n", rc); /* data after should be the same as data before */ assert("connack rcs should be the same", connack_rc == connack_rc2, "connack rcs were different %d\n", connack_rc2); /* exit: */ MyLog(LOGA_INFO, "TEST6: test %s. %d tests run, %d failures.", (failures == 0) ? "passed" : "failed", tests, failures); write_test_result(); return failures; }
/********************************************************************* Test2c: Mutual SSL Authentication - Client does not have Server cert *********************************************************************/ int test2c(struct Options options) { char* testname = "test2c"; char* test_topic = "C client test2c"; int subsqos = 2; MQTTClient c; MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer; MQTTClient_willOptions wopts = MQTTClient_willOptions_initializer; MQTTClient_SSLOptions sslopts = MQTTClient_SSLOptions_initializer; int rc = 0; failures = 0; MyLog(LOGA_INFO, "Starting test 2c - connection to SSL MQTT server, server auth enabled but unknown cert"); fprintf(xml, "<testcase classname=\"test3\" name=\"test 2c\""); global_start_time = start_clock(); rc = MQTTClient_create(&c, options.mutual_auth_connection, "test2c", MQTTCLIENT_PERSISTENCE_DEFAULT, persistenceStore); if (!(assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc))) goto exit; opts.keepAliveInterval = 20; opts.cleansession = 1; opts.username = "******"; opts.password = "******"; if (options.haconnections != NULL) { opts.serverURIs = options.haconnections; opts.serverURIcount = options.hacount; } opts.ssl = &sslopts; //if (options.server_key_file) // opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/ opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/ if (options.client_key_pass) opts.ssl->privateKeyPassword = options.client_key_pass; if (options.client_private_key_file) opts.ssl->privateKey = options.client_private_key_file; //opts.ssl->enabledCipherSuites = "DEFAULT"; //opts.ssl->enabledServerCertAuth = 0; MyLog(LOGA_DEBUG, "Connecting"); rc = MQTTClient_connect(c, &opts); if (!(assert("Good rc from connect", rc == MQTTCLIENT_FAILURE, "rc was %d", rc))) goto exit; exit: MQTTClient_destroy(&c); MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.", (failures == 0) ? "passed" : "failed", testname, tests, failures); write_test_result(); return failures; }
/******************************************************************** Test5: Connack return codes *********************************************************************/ int test5(struct Options options) { int subsqos = 2; MQTTAsync c; MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer; MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer; int rc = 0; char* test_topic = "C client test1"; test_finished = failures = 0; MyLog(LOGA_INFO, "Starting test 5 - connack return codes"); fprintf(xml, "<testcase classname=\"test4\" name=\"connack return codes\""); global_start_time = start_clock(); rc = MQTTAsync_create(&c, options.connection, "a clientid that is too long to be accepted", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL); assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc); if (rc != MQTTASYNC_SUCCESS) { MQTTAsync_destroy(&c); goto exit; } rc = MQTTAsync_setCallbacks(c, c, NULL, test1_messageArrived, NULL); assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); opts.onSuccess = test5_onConnect; opts.onFailure = test5_onConnectFailure; opts.context = c; MyLog(LOGA_DEBUG, "Connecting"); rc = MQTTAsync_connect(c, &opts); rc = 0; assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); if (rc != MQTTASYNC_SUCCESS) goto exit; while (!test_finished) #if defined(WIN32) Sleep(100); #else usleep(10000L); #endif MQTTAsync_destroy(&c); exit: MyLog(LOGA_INFO, "TEST5: test %s. %d tests run, %d failures.", (failures == 0) ? "passed" : "failed", tests, failures); write_test_result(); return failures; }
/********************************************************************* Test1: SSL connection to non SSL MQTT server *********************************************************************/ int test1(struct Options options) { char* testname = "test1"; char* test_topic = "C client SSL test1"; int subsqos = 2; MQTTClient c; MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer; MQTTClient_willOptions wopts = MQTTClient_willOptions_initializer; MQTTClient_SSLOptions sslopts = MQTTClient_SSLOptions_initializer; int rc = 0; failures = 0; MyLog(LOGA_INFO, "Starting SSL test 1 - connection to nonSSL MQTT server"); fprintf(xml, "<testcase classname=\"test3\" name=\"SSL connect fail to nonSSL MQTT server\""); global_start_time = start_clock(); rc = MQTTClient_create(&c, options.connection, "test1", MQTTCLIENT_PERSISTENCE_DEFAULT, persistenceStore); if (!(assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d \n", rc))) goto exit; opts.keepAliveInterval = 20; opts.cleansession = 1; opts.username = "******"; opts.password = "******"; if (options.haconnections != NULL) { opts.serverURIs = options.haconnections; opts.serverURIcount = options.hacount; } opts.ssl = &sslopts; if (options.server_key_file != NULL) opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/ MyLog(LOGA_DEBUG, "Connecting"); rc = MQTTClient_connect(c, &opts); if (!(assert("Connect should fail", rc == MQTTCLIENT_FAILURE, "rc was %d ", rc))) goto exit; exit: MQTTClient_destroy(&c); MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.", (failures == 0) ? "passed" : "failed", testname, tests, failures); write_test_result(); return failures; }
int test4(struct Options options) { int rc = 0; unsigned char buf[100]; size_t buflen = sizeof(buf); unsigned char dup = 0; unsigned short packetid = 23; MQTTSN_topicid topicFilter; int req_qos = 2; unsigned char dup2 = 1; unsigned short packetid2 = 2223; MQTTSN_topicid topicFilter2; int req_qos2 = 0; fprintf(xml, "<testcase classname=\"test4\" name=\"de/serialization\""); global_start_time = start_clock(); failures = 0; MyLog(LOGA_INFO, "Starting test 4 - serialization of subscribe and back"); memset(&topicFilter, '\0', sizeof(topicFilter)); memset(&topicFilter2, '\0', sizeof(topicFilter2)); topicFilter.type = MQTTSN_TOPIC_TYPE_NORMAL; topicFilter.data.long_.name = "mytopic"; topicFilter.data.long_.len = strlen(topicFilter.data.long_.name); rc = MQTTSNSerialize_subscribe(buf, buflen, dup, req_qos, packetid, &topicFilter); assert("good rc from serialize subscribe", rc > 0, "rc was %d\n", rc); rc = MQTTSNDeserialize_subscribe(&dup2, &req_qos2, &packetid2, &topicFilter2, buf, buflen); assert("good rc from deserialize subscribe", rc == 1, "rc was %d\n", rc); /* data after should be the same as data before */ assert("dups should be the same", dup == dup2, "dups were different %d\n", dup2); assert("msgids should be the same", packetid == packetid2, "packetids were different %d\n", packetid2); assert("topics should be the same", checkMQTTSNTopics(topicFilter, topicFilter2), "topics were different %s\n", ""); assert("qoss should be the same", req_qos == req_qos2, "qoss were different %d\n", req_qos2); /*exit:*/ MyLog(LOGA_INFO, "TEST4: test %s. %d tests run, %d failures.", (failures == 0) ? "passed" : "failed", tests, failures); write_test_result(); return failures; }
int test5a(struct Options options) { char* testname = "test5a"; char* test_topic = "C client SSL test5a"; int subsqos = 2; MQTTClient c; MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer; MQTTClient_willOptions wopts = MQTTClient_willOptions_initializer; MQTTClient_SSLOptions sslopts = MQTTClient_SSLOptions_initializer; int rc = 0; failures = 0; MyLog(LOGA_INFO, "Starting SSL test 5a - Anonymous ciphers - server authentication disabled"); fprintf(xml, "<testcase classname=\"test3\" name=\"test 5a\""); global_start_time = start_clock(); rc = MQTTClient_create(&c, options.anon_connection, "test5a", MQTTCLIENT_PERSISTENCE_DEFAULT, persistenceStore); if (!(assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc))) goto exit; opts.keepAliveInterval = 20; opts.cleansession = 1; opts.username = "******"; opts.password = "******"; if (options.haconnections != NULL) { opts.serverURIs = options.haconnections; opts.serverURIcount = options.hacount; } opts.ssl = &sslopts; opts.ssl->enabledCipherSuites = "aNULL"; opts.ssl->enableServerCertAuth = 0; MyLog(LOGA_DEBUG, "Connecting"); rc = MQTTClient_connect(c, &opts); if (!(assert("Good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc))) goto exit; rc = MQTTClient_subscribe(c, test_topic, subsqos); if (!(assert("Good rc from subscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc))) goto exit; singleThread_sendAndReceive(c, 0, test_topic); singleThread_sendAndReceive(c, 1, test_topic); singleThread_sendAndReceive(c, 2, test_topic); MyLog(LOGA_DEBUG, "Stopping\n"); rc = MQTTClient_unsubscribe(c, test_topic); if (!(assert("Unsubscribe successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc))) goto exit; rc = MQTTClient_disconnect(c, 0); if (!(assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc))) goto exit; /* Just to make sure we can connect again */ rc = MQTTClient_connect(c, &opts); if (!(assert("Connect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc))) goto exit; rc = MQTTClient_disconnect(c, 0); if (!(assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc))) goto exit; exit: MQTTClient_destroy(&c); MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.", (failures == 0) ? "passed" : "failed", testname, tests, failures); write_test_result(); return failures; }
int test3(struct Options options) { char* testname = "test3"; int subsqos = 2; MQTTAsync c, d; MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer; MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer; MQTTAsync_createOptions createOptions = MQTTAsync_createOptions_initializer; int rc = 0; int count = 0; char clientidc[50]; char clientidd[50]; int i = 0; MQTTAsync_token *tokens; sprintf(willTopic, "paho-test9-3-%s", unique); sprintf(clientidc, "paho-test9-3-c-%s", unique); sprintf(clientidd, "paho-test9-3-d-%s", unique); sprintf(test_topic, "paho-test9-3-test topic %s", unique); test3Finished = 0; failures = 0; MyLog(LOGA_INFO, "Starting Offline buffering 3 - messages while disconnected"); fprintf(xml, "<testcase classname=\"test3\" name=\"%s\"", testname); global_start_time = start_clock(); createOptions.sendWhileDisconnected = 1; rc = MQTTAsync_createWithOptions(&c, options.proxy_connection, clientidc, MQTTCLIENT_PERSISTENCE_DEFAULT, NULL, &createOptions); assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d \n", rc); if (rc != MQTTASYNC_SUCCESS) { MQTTAsync_destroy(&c); goto exit; } rc = MQTTAsync_create(&d, options.connection, clientidd, MQTTCLIENT_PERSISTENCE_DEFAULT, NULL); assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d \n", rc); if (rc != MQTTASYNC_SUCCESS) { MQTTAsync_destroy(&c); goto exit; } opts.keepAliveInterval = 20; opts.cleansession = 1; //opts.username = "******"; //opts.password = "******"; rc = MQTTAsync_setCallbacks(d, d, NULL, test3_messageArrived, NULL); assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); opts.will = NULL; /* don't need will for this client, as it's going to be connected all the time */ opts.context = d; opts.onSuccess = test3dOnConnect; opts.onFailure = test3dOnFailure; MyLog(LOGA_DEBUG, "Connecting client d"); rc = MQTTAsync_connect(d, &opts); assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d ", rc); if (rc != MQTTASYNC_SUCCESS) { failures++; goto exit; } /* wait until d is ready: connected and subscribed */ count = 0; while (!test3dReady && ++count < 10000) MySleep(100); assert("Count should be less than 10000", count < 10000, "count was %d", count); /* wrong */ rc = MQTTAsync_setConnected(c, c, test3cConnected); assert("Good rc from setConnectedCallback", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); /* let client c go: connect, and send disconnect command to proxy */ opts.will = &wopts; opts.will->message = "will message"; opts.will->qos = 1; opts.will->retained = 0; opts.will->topicName = willTopic; opts.onSuccess = test3cOnConnect; opts.onFailure = test3cOnFailure; opts.context = c; opts.cleansession = 0; opts.automaticReconnect = 1; MyLog(LOGA_DEBUG, "Connecting client c"); rc = MQTTAsync_connect(c, &opts); assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d ", rc); if (rc != MQTTASYNC_SUCCESS) { failures++; goto exit; } /* wait for will message */ while (!test3_will_message_received && ++count < 10000) MySleep(100); MyLog(LOGA_DEBUG, "Now we can send some messages to be buffered"); test3c_connected = 0; /* send some messages. Then reconnect (check connected callback), and check that those messages are received */ for (i = 0; i < 3; ++i) { char buf[50]; MQTTAsync_message pubmsg = MQTTAsync_message_initializer; MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer; sprintf(buf, "QoS %d message", i); pubmsg.payload = buf; pubmsg.payloadlen = strlen(pubmsg.payload) + 1; pubmsg.qos = i; pubmsg.retained = 0; rc = MQTTAsync_sendMessage(c, test_topic, &pubmsg, &opts); assert("Good rc from sendMessage", rc == MQTTASYNC_SUCCESS, "rc was %d ", rc); } rc = MQTTAsync_getPendingTokens(c, &tokens); assert("Good rc from getPendingTokens", rc == MQTTASYNC_SUCCESS, "rc was %d ", rc); i = 0; if (tokens) { while (tokens[i] != -1) ++i; MQTTAsync_free(tokens); } assert("Number of getPendingTokens should be 3", i == 3, "i was %d ", i); /* wait for client to be reconnected */ while (!test3c_connected == 0 && ++count < 10000) MySleep(100); /* wait for success or failure callback */ while (test3_messages_received < 3 && ++count < 10000) MySleep(100); rc = MQTTAsync_getPendingTokens(c, &tokens); assert("Good rc from getPendingTokens", rc == MQTTASYNC_SUCCESS, "rc was %d ", rc); i = 0; if (tokens) { while (tokens[i] != -1) ++i; MQTTAsync_free(tokens); } assert("Number of getPendingTokens should be 0", i == 0, "i was %d ", i); rc = MQTTAsync_disconnect(c, NULL); assert("Good rc from disconnect", rc == MQTTASYNC_SUCCESS, "rc was %d ", rc); rc = MQTTAsync_disconnect(d, NULL); assert("Good rc from disconnect", rc == MQTTASYNC_SUCCESS, "rc was %d ", rc); exit: MQTTAsync_destroy(&c); MQTTAsync_destroy(&d); MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.", (failures == 0) ? "passed" : "failed", testname, tests, failures); write_test_result(); return failures; }
int test2(struct Options options) { char* testname = "test2"; int subsqos = 2; MQTTClient c; MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer; int rc = 0; char* test_topic = "C client test2"; char* topics[2] = {"test_topic", "nosubscribe"}; int qoss[2] = {2, 2}; fprintf(xml, "<testcase classname=\"test1\" name=\"bad return code from subscribe\""); MyLog(LOGA_INFO, "Starting test 2 - bad return code from subscribe"); global_start_time = start_clock(); failures = 0; MQTTClient_create(&c, options.connection, "multi_threaded_sample", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL); opts.keepAliveInterval = 20; opts.cleansession = 1; opts.MQTTVersion = 4; if (options.haconnections != NULL) { opts.serverURIs = options.haconnections; opts.serverURIcount = options.hacount; } rc = MQTTClient_setCallbacks(c, NULL, NULL, test2_messageArrived, test2_deliveryComplete); assert("Good rc from setCallbacks", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc); MyLog(LOGA_DEBUG, "Connecting"); rc = MQTTClient_connect(c, &opts); assert("Good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc); if (rc != MQTTCLIENT_SUCCESS) goto exit; assert("Correct serverURI returned", strcmp(opts.returned.serverURI, options.connection) == 0, "serverURI was %s", opts.returned.serverURI); assert("Correct MQTTVersion returned", opts.returned.MQTTVersion == 4, "MQTTVersion was %d", opts.returned.MQTTVersion); assert("Correct sessionPresent returned", opts.returned.sessionPresent == 0, "sessionPresent was %d", opts.returned.sessionPresent); rc = MQTTClient_subscribe(c, test_topic, subsqos); assert("Good rc from subscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc); rc = MQTTClient_subscribe(c, "nosubscribe", 2); assert("0x80 from subscribe", rc == 0x80, "rc was %d", rc); rc = MQTTClient_subscribeMany(c, 2, topics, qoss); assert("Good rc from subscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc); assert("Correct returned qos from subscribe", qoss[0] == 2, "qos 0 was %d", qoss[0]); assert("Correct returned qos from subscribe", qoss[1] == 0x80, "qos 0 was %d", qoss[0]); rc = MQTTClient_unsubscribe(c, test_topic); assert("Unsubscribe successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc); rc = MQTTClient_disconnect(c, 0); assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc); MQTTClient_destroy(&c); exit: MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.", (failures == 0) ? "passed" : "failed", testname, tests, failures); write_test_result(); return failures; }
/********************************************************************* Test1: sessionPresent *********************************************************************/ int test1(struct Options options) { MQTTClient c; MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer; MQTTClient_willOptions wopts = MQTTClient_willOptions_initializer; int rc = 0; char* test_topic = "C client test1"; fprintf(xml, "<testcase classname=\"test1\" name=\"sessionPresent\""); global_start_time = start_clock(); failures = 0; MyLog(LOGA_INFO, "Starting test 1 - sessionPresent"); rc = MQTTClient_create(&c, options.connection, "sesssionPresent", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL); assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc); if (rc != MQTTCLIENT_SUCCESS) { MQTTClient_destroy(&c); goto exit; } opts.keepAliveInterval = 20; opts.username = "******"; opts.password = "******"; opts.MQTTVersion = 4; if (options.haconnections != NULL) { opts.serverURIs = options.haconnections; opts.serverURIcount = options.hacount; } opts.will = &wopts; opts.will->message = "will message"; opts.will->qos = 1; opts.will->retained = 0; opts.will->topicName = "will topic"; opts.will = NULL; /* Connect cleansession */ opts.cleansession = 1; MyLog(LOGA_DEBUG, "Connecting"); rc = MQTTClient_connect(c, &opts); assert("Good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc); if (rc != MQTTCLIENT_SUCCESS) goto exit; assert("Correct serverURI returned", strcmp(opts.returned.serverURI, options.connection) == 0, "serverURI was %s", opts.returned.serverURI); assert("Correct MQTTVersion returned", opts.returned.MQTTVersion == 4, "MQTTVersion was %d", opts.returned.MQTTVersion); assert("Correct sessionPresent returned", opts.returned.sessionPresent == 0, "sessionPresent was %d", opts.returned.sessionPresent); rc = MQTTClient_disconnect(c, 0); assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc); /* Connect again, non-cleansession */ opts.cleansession = 0; rc = MQTTClient_connect(c, &opts); assert("Connect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc); assert("Correct serverURI returned", strcmp(opts.returned.serverURI, options.connection) == 0, "serverURI was %s", opts.returned.serverURI); assert("Correct MQTTVersion returned", opts.returned.MQTTVersion == 4, "MQTTVersion was %d", opts.returned.MQTTVersion); assert("Correct sessionPresent returned", opts.returned.sessionPresent == 0, "sessionPresent was %d", opts.returned.sessionPresent); rc = MQTTClient_disconnect(c, 0); assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc); /* Connect again, non-cleansession */ opts.cleansession = 0; rc = MQTTClient_connect(c, &opts); assert("Connect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc); assert("Correct serverURI returned", strcmp(opts.returned.serverURI, options.connection) == 0, "serverURI was %s", opts.returned.serverURI); assert("Correct MQTTVersion returned", opts.returned.MQTTVersion == 4, "MQTTVersion was %d", opts.returned.MQTTVersion); assert("Correct sessionPresent returned", opts.returned.sessionPresent == 1, "sessionPresent was %d", opts.returned.sessionPresent); rc = MQTTClient_disconnect(c, 0); assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc); MQTTClient_destroy(&c); exit: MyLog(LOGA_INFO, "TEST1: test %s. %d tests run, %d failures.", (failures == 0) ? "passed" : "failed", tests, failures); write_test_result(); return failures; }
int test3(struct Options options) { int rc = 0; unsigned char buf[100]; int buflen = sizeof(buf); fprintf(xml, "<testcase classname=\"test3\" name=\"de/serialization\""); global_start_time = start_clock(); failures = 0; MyLog(LOGA_INFO, "Starting test 3 - will messages"); rc = MQTTSNSerialize_willtopicreq(buf, buflen); assert("good rc from serialize willtopicreq", rc > 0, "rc was %d\n", rc); rc = MQTTSNDeserialize_willtopicreq(buf, buflen); assert("good rc from deserialize willtopicreq", rc == 1, "rc was %d\n", rc); rc = MQTTSNSerialize_willmsgreq(buf, buflen); assert("good rc from serialize willmsgreq", rc > 0, "rc was %d\n", rc); rc = MQTTSNDeserialize_willmsgreq(buf, rc); assert("good rc from deserialize willmsgreq", rc == 1, "rc was %d\n", rc); memset(buf, '\0', sizeof(buf)); int willQoS = 1, willQoS1 = 0; unsigned char willRetain = 1, willRetain1 = 0; MQTTSNString willTopic = MQTTSNString_initializer, willTopic1 = MQTTSNString_initializer; willTopic.cstring = "a will topic"; rc = MQTTSNSerialize_willtopic(buf, buflen, willQoS, willRetain, willTopic); assert("good rc from serialize willtopic", rc > 0, "rc was %d\n", rc); rc = MQTTSNDeserialize_willtopic(&willQoS1, &willRetain1, &willTopic1, buf, rc); assert("good rc from deserialize willtopic", rc == 1, "rc was %d\n", rc); assert("willQoSs are the same", willQoS == willQoS1, "willQoS1 was %d\n", willQoS1); assert("willRetains are the same", willRetain == willRetain1, "willRetain1 was %d\n", willRetain1); assert("willTopics are the same", checkMQTTSNStrings(willTopic, willTopic1), "willTopic1 was %.s\n", willTopic1.lenstring.data); memset(buf, '\0', sizeof(buf)); willQoS = 2; willRetain = 1; willQoS1 = 0; willRetain1 = 0; MQTTSNString initTopic = MQTTSNString_initializer; memcpy(&willTopic, &initTopic, sizeof(initTopic)); memcpy(&willTopic1, &initTopic, sizeof(initTopic)); willTopic.cstring = "a will topic update"; rc = MQTTSNSerialize_willtopicupd(buf, buflen, willQoS, willRetain, willTopic); assert("good rc from serialize willtopicupd", rc > 0, "rc was %d\n", rc); rc = MQTTSNDeserialize_willtopicupd(&willQoS1, &willRetain1, &willTopic1, buf, rc); assert("good rc from deserialize willtopicupd", rc == 1, "rc was %d\n", rc); assert("willQoSs are the same", willQoS == willQoS1, "willQoS1 was %d\n", willQoS1); assert("willRetains are the same", willRetain == willRetain1, "willRetain1 was %d\n", willRetain1); assert("willTopics are the same", checkMQTTSNStrings(willTopic, willTopic1), "willTopic1 was %.s\n", willTopic1.lenstring.data); memset(buf, '\0', sizeof(buf)); MQTTSNString willMsg = MQTTSNString_initializer, willMsg1 = MQTTSNString_initializer; willMsg.cstring = "a will message"; rc = MQTTSNSerialize_willmsg(buf, buflen, willMsg); assert("good rc from serialize willmsg", rc > 0, "rc was %d\n", rc); rc = MQTTSNDeserialize_willmsg(&willMsg1, buf, rc); assert("good rc from deserialize willmsg", rc == 1, "rc was %d\n", rc); assert("willMsgs are the same", checkMQTTSNStrings(willMsg, willMsg1), "willMsg1 was %.s\n", willMsg1.lenstring.data); memset(buf, '\0', sizeof(buf)); memcpy(&willMsg, &initTopic, sizeof(initTopic)); memcpy(&willMsg1, &initTopic, sizeof(initTopic)); willMsg.cstring = "a will message"; rc = MQTTSNSerialize_willmsgupd(buf, buflen, willMsg); assert("good rc from serialize willmsgupd", rc > 0, "rc was %d\n", rc); rc = MQTTSNDeserialize_willmsgupd(&willMsg1, buf, rc); assert("good rc from deserialize willmsgupd", rc == 1, "rc was %d\n", rc); assert("willMsgs are the same", checkMQTTSNStrings(willMsg, willMsg1), "willMsg1 was %.s\n", willMsg1.lenstring.data); int resp_rc = 33, resp_rc2 = 0; rc = MQTTSNSerialize_willmsgresp(buf, buflen, resp_rc); assert("good rc from serialize willmsgresp", rc > 0, "rc was %d\n", rc); rc = MQTTSNDeserialize_willmsgresp(&resp_rc2, buf, buflen); assert("good rc from deserialize willmsgresp", rc == 1, "rc was %d\n", rc); assert("resp rcs should be the same", resp_rc == resp_rc2, "resp rcs were different %d\n", resp_rc2); resp_rc = 67, resp_rc2 = 0; rc = MQTTSNSerialize_willtopicresp(buf, buflen, resp_rc); assert("good rc from serialize willmsgresp", rc > 0, "rc was %d\n", rc); rc = MQTTSNDeserialize_willtopicresp(&resp_rc2, buf, buflen); assert("good rc from deserialize willmsgresp", rc == 1, "rc was %d\n", rc); assert("resp rcs should be the same", resp_rc == resp_rc2, "resp rcs were different %d\n", resp_rc2); /* exit: */ MyLog(LOGA_INFO, "TEST3: test %s. %d tests run, %d failures.", (failures == 0) ? "passed" : "failed", tests, failures); write_test_result(); return failures; }
int test2(struct Options options) { int rc = 0; unsigned char buf[100]; int buflen = sizeof(buf); unsigned char dup = 0; int qos = 2; unsigned char retained = 0; unsigned short msgid = 23; MQTTSN_topicid topic; unsigned char *payload = (unsigned char*)"kkhkhkjkj jkjjk jk jk "; int payloadlen = strlen((char*)payload); unsigned char dup2 = 1; int qos2 = 1; unsigned char retained2 = 1; unsigned short msgid2 = 3243; MQTTSN_topicid topic2; unsigned char *payload2 = NULL; int payloadlen2 = 0; unsigned char acktype; unsigned char returncode = 3, returncode2 = -99; fprintf(xml, "<testcase classname=\"test1\" name=\"de/serialization\""); global_start_time = start_clock(); failures = 0; MyLog(LOGA_INFO, "Starting test 2 - serialization of publish and back"); memset(&topic, 0, sizeof(topic)); memset(&topic2, 0, sizeof(topic2)); topic.type = MQTTSN_TOPIC_TYPE_SHORT; memcpy(topic.data.short_name, "my", 2); rc = MQTTSNSerialize_publish(buf, buflen, dup, qos, retained, msgid, topic, payload, payloadlen); assert("good rc from serialize publish", rc > 0, "rc was %d\n", rc); rc = MQTTSNDeserialize_publish(&dup2, &qos2, &retained2, &msgid2, &topic2, &payload2, &payloadlen2, buf, buflen); assert("good rc from deserialize publish", rc == 1, "rc was %d\n", rc); /* data after should be the same as data before */ assert("dups should be the same", dup == dup2, "dups were different %d\n", dup2); assert("qoss should be the same", qos == qos2, "qoss were different %d\n", qos2); assert("retaineds should be the same", retained == retained2, "retaineds were different %d\n", retained2); assert("msgids should be the same", msgid == msgid2, "msgids were different %d\n", msgid2); assert("topics should be the same", checkMQTTSNTopics(topic, topic2), "topics were different %s\n", ""); assert("payload lengths should be the same", payloadlen == payloadlen2, "payload lengths were different %d\n", payloadlen2); assert("payloads should be the same", memcmp(payload, payload2, payloadlen) == 0, "payloads were different %s\n", ""); rc = MQTTSNSerialize_puback(buf, buflen, topic.data.id, msgid, returncode); assert("good rc from serialize puback", rc > 0, "rc was %d\n", rc); rc = MQTTSNDeserialize_puback(&topic2.data.id, &msgid2, &returncode2, buf, buflen); assert("good rc from deserialize puback", rc > 0, "rc was %d\n", rc); assert("msgids should be the same", msgid == msgid2, "msgids were different %d\n", msgid2); assert("return codes should be the same", returncode == returncode2, "return codes were different %d\n", returncode2); rc = MQTTSNSerialize_pubrec(buf, buflen, msgid); assert("good rc from serialize pubrec", rc > 0, "rc was %d\n", rc); rc = MQTTSNDeserialize_ack(&acktype, &msgid2, buf, buflen); assert("good rc from deserialize pubrec", rc == 1, "rc was %d\n", rc); assert("Acktype should be MQTTSN_PUBREC", acktype == MQTTSN_PUBREC, "acktype was %d\n", acktype); assert("msgids should be the same", msgid == msgid2, "msgids were different %d\n", msgid2); /*exit:*/ MyLog(LOGA_INFO, "TEST2: test %s. %d tests run, %d failures.", (failures == 0) ? "passed" : "failed", tests, failures); write_test_result(); return failures; }
int test1(struct Options options) { MQTTSNPacket_connectData data = MQTTSNPacket_connectData_initializer; MQTTSNPacket_connectData data_after = MQTTSNPacket_connectData_initializer; int rc = 0; unsigned char buf[100]; int buflen = sizeof(buf); MQTTSNString clientid = MQTTSNString_initializer, clientid_after = MQTTSNString_initializer; int duration_after = -1; fprintf(xml, "<testcase classname=\"test1\" name=\"de/serialization\""); global_start_time = start_clock(); failures = 0; MyLog(LOGA_INFO, "Starting test 1 - serialization of connect and back"); data.clientID.cstring = "me too"; data.duration = 20; data.cleansession = 1; data.willFlag = 1; rc = MQTTSNSerialize_connect(buf, buflen, &data); assert("good rc from serialize connect", rc > 0, "rc was %d\n", rc); rc = MQTTSNDeserialize_connect(&data_after, buf, buflen); assert("good rc from deserialize connect", rc == 1, "rc was %d\n", rc); /* data after should be the same as data before */ rc = checkConnectPackets(&data, &data_after); assert("packets should be the same", rc == 1, "packets were different\n", rc); /* Pingreq without clientid */ rc = MQTTSNSerialize_pingreq(buf, buflen, clientid); assert("good rc from serialize pingreq", rc > 0, "rc was %d\n", rc); rc = MQTTSNDeserialize_pingreq(&clientid_after, buf, buflen); assert("good rc from deserialize pingreq", rc == 1, "rc was %d\n", rc); /* data after should be the same as data before */ assert("ClientIDs should be the same", checkMQTTSNStrings(clientid, clientid_after), "ClientIDs were different\n", rc); /* Pingreq with clientid */ clientid.cstring = "this is me"; rc = MQTTSNSerialize_pingreq(buf, buflen, clientid); assert("good rc from serialize pingreq", rc > 0, "rc was %d\n", rc); rc = MQTTSNDeserialize_pingreq(&clientid_after, buf, buflen); assert("good rc from deserialize pingreq", rc == 1, "rc was %d\n", rc); /* data after should be the same as data before */ assert("ClientIDs should be the same", checkMQTTSNStrings(clientid, clientid_after), "ClientIDs were different\n", rc); rc = MQTTSNSerialize_pingresp(buf, buflen); assert("good rc from serialize pingresp", rc > 0, "rc was %d\n", rc); rc = MQTTSNDeserialize_pingresp(buf, buflen); assert("good rc from deserialize pingresp", rc == 1, "rc was %d\n", rc); /* Disconnect without duration */ rc = MQTTSNSerialize_disconnect(buf, buflen, 0); assert("good rc from serialize disconnect", rc > 0, "rc was %d\n", rc); rc = MQTTSNDeserialize_disconnect(&duration_after, buf, buflen); assert("good rc from deserialize disconnect", rc == 1, "rc was %d\n", rc); /* data after should be the same as data before */ assert("durations should be the same", 0 == duration_after, "durations were different\n", rc); /* Disconnect with duration */ rc = MQTTSNSerialize_disconnect(buf, buflen, 33); assert("good rc from serialize disconnect", rc > 0, "rc was %d\n", rc); rc = MQTTSNDeserialize_disconnect(&duration_after, buf, buflen); assert("good rc from deserialize disconnect", rc == 1, "rc was %d\n", rc); /* data after should be the same as data before */ assert("durations should be the same", 33 == duration_after, "durations were different\n", rc); /* Pingreq with clientid */ clientid.cstring = "this is me"; rc = MQTTSNSerialize_pingreq(buf, buflen, clientid); assert("good rc from serialize pingreq", rc > 0, "rc was %d\n", rc); rc = MQTTSNDeserialize_pingreq(&clientid_after, buf, buflen); assert("good rc from deserialize pingreq", rc == 1, "rc was %d\n", rc); /* exit: */ MyLog(LOGA_INFO, "TEST1: test %s. %d tests run, %d failures.", (failures == 0) ? "passed" : "failed", tests, failures); write_test_result(); return failures; }
/********************************************************************* Test3: More than one client object - simultaneous working. *********************************************************************/ int test3(struct Options options) { #define num_clients 10 int subsqos = 2; MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer; MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer; int rc = 0; int i; client_data clientdata[num_clients]; test_finished = 0; MyLog(LOGA_INFO, "Starting test 3 - multiple connections"); fprintf(xml, "<testcase classname=\"test4\" name=\"multiple connections\""); global_start_time = start_clock(); for (i = 0; i < num_clients; ++i) { sprintf(clientdata[i].clientid, "async_test3_num_%d", i); sprintf(clientdata[i].test_topic, "async test3 topic num %d", i); clientdata[i].index = i; clientdata[i].message_count = 0; rc = MQTTAsync_create(&(clientdata[i].c), options.connection, clientdata[i].clientid, MQTTCLIENT_PERSISTENCE_NONE, NULL); assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc); rc = MQTTAsync_setCallbacks(clientdata[i].c, &clientdata[i], NULL, test3_messageArrived, NULL); assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); opts.keepAliveInterval = 20; opts.cleansession = 1; opts.username = "******"; opts.password = "******"; opts.MQTTVersion = options.MQTTVersion; opts.will = &wopts; opts.will->message = "will message"; opts.will->qos = 1; opts.will->retained = 0; opts.will->topicName = "will topic"; opts.onSuccess = test3_onConnect; opts.onFailure = test3_onFailure; opts.context = &clientdata[i]; MyLog(LOGA_DEBUG, "Connecting"); rc = MQTTAsync_connect(clientdata[i].c, &opts); assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); } while (test_finished < num_clients) { MyLog(LOGA_DEBUG, "num_clients %d test_finished %d\n", num_clients, test_finished); #if defined(WIN32) Sleep(100); #else usleep(10000L); #endif } MyLog(LOGA_DEBUG, "TEST3: destroying clients"); for (i = 0; i < num_clients; ++i) MQTTAsync_destroy(&clientdata[i].c); //exit: MyLog(LOGA_INFO, "TEST3: test %s. %d tests run, %d failures.", (failures == 0) ? "passed" : "failed", tests, failures); write_test_result(); return failures; }
int test2a_s(struct Options options) { char* testname = "test2a_s"; char* test_topic = "C client test2a_s"; int subsqos = 2; MQTTClient c; MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer; MQTTClient_willOptions wopts = MQTTClient_willOptions_initializer; MQTTClient_SSLOptions sslopts = MQTTClient_SSLOptions_initializer; int rc = 0; failures = 0; MyLog(LOGA_INFO, "Starting test 2a_s - Mutual SSL authentication - single threaded client using receive"); fprintf(xml, "<testcase classname=\"test3\" name=\"test 2a_s\""); global_start_time = start_clock(); rc = MQTTClient_create(&c, options.server_auth_connection, "test2a_s", MQTTCLIENT_PERSISTENCE_DEFAULT, persistenceStore); if (!(assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc))) goto exit; opts.keepAliveInterval = 20; opts.cleansession = 1; opts.username = "******"; opts.password = "******"; if (options.haconnections != NULL) { opts.serverURIs = options.haconnections; opts.serverURIcount = options.hacount; } opts.ssl = &sslopts; if (options.server_key_file) opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/ opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/ if (options.client_key_pass) opts.ssl->privateKeyPassword = options.client_key_pass; if (options.client_private_key_file) opts.ssl->privateKey = options.client_private_key_file; MyLog(LOGA_DEBUG, "Connecting"); rc = MQTTClient_connect(c, &opts); if (!(assert("Good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc))) goto exit; rc = MQTTClient_subscribe(c, test_topic, subsqos); if (!(assert("Good rc from subscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc))) goto exit; singleThread_sendAndReceive(c, 0, test_topic); singleThread_sendAndReceive(c, 1, test_topic); singleThread_sendAndReceive(c, 2, test_topic); MyLog(LOGA_DEBUG, "Stopping\n"); rc = MQTTClient_unsubscribe(c, test_topic); if (!(assert("Unsubscribe successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc))) goto exit; rc = MQTTClient_disconnect(c, 0); if (!(assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc))) goto exit; /* Just to make sure we can connect again */ rc = MQTTClient_connect(c, &opts); if (!(assert("Connect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc))) goto exit; rc = MQTTClient_disconnect(c, 0); if (!(assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc))) goto exit; exit: MQTTClient_destroy(&c); MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.", (failures == 0) ? "passed" : "failed", testname, tests, failures); write_test_result(); return failures; }
/********************************************************************* Test4: Send and receive big messages *********************************************************************/ int test4(struct Options options) { int subsqos = 2; MQTTAsync c; MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer; MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer; int rc = 0; char* test_topic = "C client test4"; test_finished = failures = 0; MyLog(LOGA_INFO, "Starting test 4 - big messages"); fprintf(xml, "<testcase classname=\"test4\" name=\"big messages\""); global_start_time = start_clock(); rc = MQTTAsync_create(&c, options.connection, "async_test_4", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL); assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc); if (rc != MQTTASYNC_SUCCESS) { MQTTAsync_destroy(&c); goto exit; } rc = MQTTAsync_setCallbacks(c, c, NULL, test4_messageArrived, NULL); assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); opts.keepAliveInterval = 20; opts.cleansession = 1; opts.username = "******"; opts.password = "******"; opts.MQTTVersion = options.MQTTVersion; opts.will = &wopts; opts.will->message = "will message"; opts.will->qos = 1; opts.will->retained = 0; opts.will->topicName = "will topic"; opts.will = NULL; opts.onSuccess = test4_onConnect; opts.onFailure = NULL; opts.context = c; MyLog(LOGA_DEBUG, "Connecting"); rc = MQTTAsync_connect(c, &opts); rc = 0; assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); if (rc != MQTTASYNC_SUCCESS) goto exit; while (!test_finished) #if defined(WIN32) Sleep(100); #else usleep(1000L); #endif MQTTAsync_destroy(&c); exit: MyLog(LOGA_INFO, "TEST4: test %s. %d tests run, %d failures.", (failures == 0) ? "passed" : "failed", tests, failures); write_test_result(); return failures; }
int test3a_m(struct Options options) { char* testname = "test3a_m"; char* test_topic = "C client test3a_m"; int subsqos = 2; MQTTClient c; MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer; MQTTClient_willOptions wopts = MQTTClient_willOptions_initializer; MQTTClient_SSLOptions sslopts = MQTTClient_SSLOptions_initializer; int rc = 0; failures = 0; MyLog(LOGA_INFO, "Starting test 3a_m - Server authentication - multi-threaded client using callbacks"); fprintf(xml, "<testcase classname=\"test3\" name=\"test 3a_m\""); global_start_time = start_clock(); rc = MQTTClient_create(&c, options.server_auth_connection, "test3a_m", MQTTCLIENT_PERSISTENCE_DEFAULT, persistenceStore); if (!(assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc))) goto exit; opts.keepAliveInterval = 20; opts.cleansession = 1; opts.username = "******"; opts.password = "******"; if (options.haconnections != NULL) { opts.serverURIs = options.haconnections; opts.serverURIcount = options.hacount; } opts.ssl = &sslopts; if (options.server_key_file != NULL) opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/ rc = MQTTClient_setCallbacks(c, NULL, NULL, multiThread_messageArrived, multiThread_deliveryComplete); if (!(assert("Good rc from setCallbacks", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc))) goto exit; MyLog(LOGA_DEBUG, "Connecting"); rc = MQTTClient_connect(c, &opts); if (!(assert("Good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc))) goto exit; rc = MQTTClient_subscribe(c, test_topic, subsqos); if (!(assert("Good rc from subscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc))) goto exit; multiThread_sendAndReceive(c, 0, test_topic); multiThread_sendAndReceive(c, 1, test_topic); multiThread_sendAndReceive(c, 2, test_topic); MyLog(LOGA_DEBUG, "Stopping\n"); rc = MQTTClient_unsubscribe(c, test_topic); if (!(assert("Unsubscribe successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc))) goto exit; rc = MQTTClient_disconnect(c, 0); if (!(assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc))) goto exit; exit: MQTTClient_destroy(&c); MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.", (failures == 0) ? "passed" : "failed", testname, tests, failures); write_test_result(); return failures; }
/******************************************************************** Test6: HA connections *********************************************************************/ int test6(struct Options options) { int subsqos = 2; test6_client_info cinfo; MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer; MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer; int rc = 0; char* test_topic = "C client test1"; char* uris[2] = {options.connection, options.connection}; failures = 0; MyLog(LOGA_INFO, "Starting test 7 - HA connections"); fprintf(xml, "<testcase classname=\"test4\" name=\"HA connections\""); global_start_time = start_clock(); test_finished = 0; cinfo.should_fail = 1; /* fail to connect */ rc = MQTTAsync_create(&cinfo.c, "tcp://rubbish:1883", "async ha connection", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL); assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc); if (rc != MQTTASYNC_SUCCESS) { MQTTAsync_destroy(&cinfo.c); goto exit; } rc = MQTTAsync_setCallbacks(cinfo.c, cinfo.c, NULL, test1_messageArrived, NULL); assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); opts.onSuccess = test6_onConnect; opts.onFailure = test6_onConnectFailure; opts.context = &cinfo; opts.MQTTVersion = options.MQTTVersion; MyLog(LOGA_DEBUG, "Connecting"); rc = MQTTAsync_connect(cinfo.c, &opts); rc = 0; assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); if (rc != MQTTASYNC_SUCCESS) goto exit; while (!test_finished) #if defined(WIN32) Sleep(100); #else usleep(10000L); #endif test_finished = 0; cinfo.should_fail = 0; /* should connect */ rc = MQTTAsync_create(&cinfo.c, "tcp://rubbish:1883", "async ha connection", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL); assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc); if (rc != MQTTASYNC_SUCCESS) { MQTTAsync_destroy(&cinfo.c); goto exit; } rc = MQTTAsync_setCallbacks(cinfo.c, cinfo.c, NULL, test1_messageArrived, NULL); assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); opts.onSuccess = test6_onConnect; opts.onFailure = test6_onConnectFailure; opts.context = &cinfo; opts.serverURIs = uris; opts.serverURIcount = 2; MyLog(LOGA_DEBUG, "Connecting"); rc = MQTTAsync_connect(cinfo.c, &opts); rc = 0; assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); if (rc != MQTTASYNC_SUCCESS) goto exit; while (!test_finished) #if defined(WIN32) Sleep(100); #else usleep(10000L); #endif MQTTAsync_destroy(&cinfo.c); exit: MyLog(LOGA_INFO, "TEST6: test %s. %d tests run, %d failures.", (failures == 0) ? "passed" : "failed", tests, failures); write_test_result(); return failures; }