|
| 1 | +/* |
| 2 | + * Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | +#include <aws/crt/Api.h> |
| 16 | + |
| 17 | +#include <aws/testing/aws_test_harness.h> |
| 18 | +#include <utility> |
| 19 | + |
| 20 | +#include <condition_variable> |
| 21 | +#include <fstream> |
| 22 | +#include <mutex> |
| 23 | + |
| 24 | +#include <aws/io/logging.h> |
| 25 | + |
| 26 | +#define TEST_CERTIFICATE "/tmp/certificate.pem" |
| 27 | +#define TEST_PRIVATEKEY "/tmp/privatekey.pem" |
| 28 | +#define TEST_ROOTCA "/tmp/AmazonRootCA1.pem" |
| 29 | + |
| 30 | +static int s_TestIotPublishSubscribe(Aws::Crt::Allocator *allocator, void *ctx) |
| 31 | +{ |
| 32 | + using namespace Aws::Crt; |
| 33 | + using namespace Aws::Crt::Io; |
| 34 | + using namespace Aws::Crt::Mqtt; |
| 35 | + |
| 36 | + const char *credentialFiles[] = {TEST_CERTIFICATE, TEST_PRIVATEKEY, TEST_ROOTCA}; |
| 37 | + |
| 38 | + for (int fileIdx = 0; fileIdx < AWS_ARRAY_SIZE(credentialFiles); ++fileIdx) |
| 39 | + { |
| 40 | + std::ifstream file; |
| 41 | + file.open(credentialFiles[fileIdx]); |
| 42 | + if (!file.is_open()) |
| 43 | + { |
| 44 | + printf("Required credential file %s is missing or unreadable, skipping test\n", credentialFiles[fileIdx]); |
| 45 | + return AWS_ERROR_SUCCESS; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + (void)ctx; |
| 50 | + Aws::Crt::ApiHandle apiHandle(allocator); |
| 51 | + |
| 52 | + Aws::Crt::Io::TlsContextOptions tlsCtxOptions = |
| 53 | + Aws::Crt::Io::TlsContextOptions::InitClientWithMtls(TEST_CERTIFICATE, TEST_PRIVATEKEY); |
| 54 | + tlsCtxOptions.OverrideDefaultTrustStore(nullptr, TEST_ROOTCA); |
| 55 | + Aws::Crt::Io::TlsContext tlsContext(tlsCtxOptions, Aws::Crt::Io::TlsMode::CLIENT, allocator); |
| 56 | + ASSERT_TRUE(tlsContext); |
| 57 | + |
| 58 | + Aws::Crt::Io::SocketOptions socketOptions; |
| 59 | + AWS_ZERO_STRUCT(socketOptions); |
| 60 | + socketOptions.type = AWS_SOCKET_STREAM; |
| 61 | + socketOptions.domain = AWS_SOCKET_IPV4; |
| 62 | + socketOptions.connect_timeout_ms = 3000; |
| 63 | + |
| 64 | + Aws::Crt::Io::EventLoopGroup eventLoopGroup(0, allocator); |
| 65 | + ASSERT_TRUE(eventLoopGroup); |
| 66 | + |
| 67 | + Aws::Crt::Io::ClientBootstrap clientBootstrap(eventLoopGroup, allocator); |
| 68 | + ASSERT_TRUE(allocator); |
| 69 | + |
| 70 | + Aws::Crt::Mqtt::MqttClient mqttClient(clientBootstrap, allocator); |
| 71 | + ASSERT_TRUE(mqttClient); |
| 72 | + |
| 73 | + int tries = 0; |
| 74 | + while (tries++ < 10) |
| 75 | + { |
| 76 | + auto mqttConnection = mqttClient.NewConnection( |
| 77 | + "a16523t7iy5uyg-ats.iot.us-east-1.amazonaws.com", 8883, socketOptions, tlsContext.NewConnectionOptions()); |
| 78 | + |
| 79 | + std::mutex mutex; |
| 80 | + std::condition_variable cv; |
| 81 | + bool connected = false; |
| 82 | + bool subscribed = false; |
| 83 | + bool published = false; |
| 84 | + bool received = false; |
| 85 | + auto onConnectionCompleted = |
| 86 | + [&](MqttConnection &connection, int errorCode, ReturnCode returnCode, bool sessionPresent) { |
| 87 | + printf( |
| 88 | + "%s errorCode=%d returnCode=%d sessionPresent=%d\n", |
| 89 | + (errorCode == 0) ? "CONNECTED" : "COMPLETED", |
| 90 | + errorCode, |
| 91 | + (int)returnCode, |
| 92 | + (int)sessionPresent); |
| 93 | + connected = true; |
| 94 | + cv.notify_one(); |
| 95 | + }; |
| 96 | + auto onDisconnect = [&](MqttConnection &connection) { |
| 97 | + printf("DISCONNECTED\n"); |
| 98 | + connected = false; |
| 99 | + cv.notify_one(); |
| 100 | + }; |
| 101 | + auto onTest = [&](MqttConnection &connection, const String &topic, const ByteBuf &payload) { |
| 102 | + printf("GOT MESSAGE topic=%s payload=" PRInSTR "\n", topic.c_str(), AWS_BYTE_BUF_PRI(payload)); |
| 103 | + received = true; |
| 104 | + cv.notify_one(); |
| 105 | + }; |
| 106 | + auto onSubAck = |
| 107 | + [&](MqttConnection &connection, uint16_t packetId, const String &topic, QOS qos, int errorCode) { |
| 108 | + printf("SUBACK id=%d topic=%s qos=%d\n", packetId, topic.c_str(), qos); |
| 109 | + subscribed = true; |
| 110 | + cv.notify_one(); |
| 111 | + }; |
| 112 | + auto onPubAck = [&](MqttConnection &connection, uint16_t packetId, int errorCode) { |
| 113 | + printf("PUBLISHED id=%d\n", packetId); |
| 114 | + published = true; |
| 115 | + cv.notify_one(); |
| 116 | + }; |
| 117 | + |
| 118 | + mqttConnection->OnConnectionCompleted = onConnectionCompleted; |
| 119 | + mqttConnection->OnDisconnect = onDisconnect; |
| 120 | + char clientId[32]; |
| 121 | + sprintf(clientId, "aws-crt-cpp-v2-%d", tries); |
| 122 | + mqttConnection->Connect(clientId, true); |
| 123 | + |
| 124 | + { |
| 125 | + std::unique_lock<std::mutex> lock(mutex); |
| 126 | + cv.wait(lock, [&]() { return connected; }); |
| 127 | + } |
| 128 | + |
| 129 | + mqttConnection->Subscribe("/publish/me/senpai", QOS::AWS_MQTT_QOS_AT_LEAST_ONCE, onTest, onSubAck); |
| 130 | + |
| 131 | + { |
| 132 | + std::unique_lock<std::mutex> lock(mutex); |
| 133 | + cv.wait(lock, [&]() { return subscribed; }); |
| 134 | + } |
| 135 | + |
| 136 | + Aws::Crt::ByteBuf payload = Aws::Crt::ByteBufFromCString("notice me pls"); |
| 137 | + mqttConnection->Publish("/publish/me/senpai", QOS::AWS_MQTT_QOS_AT_LEAST_ONCE, false, payload, onPubAck); |
| 138 | + |
| 139 | + // wait for publish |
| 140 | + { |
| 141 | + std::unique_lock<std::mutex> lock(mutex); |
| 142 | + cv.wait(lock, [&]() { return published; }); |
| 143 | + } |
| 144 | + |
| 145 | + // wait for message received callback |
| 146 | + { |
| 147 | + std::unique_lock<std::mutex> lock(mutex); |
| 148 | + cv.wait(lock, [&]() { return received; }); |
| 149 | + } |
| 150 | + |
| 151 | + mqttConnection->Disconnect(); |
| 152 | + { |
| 153 | + std::unique_lock<std::mutex> lock(mutex); |
| 154 | + cv.wait(lock, [&]() { return !connected; }); |
| 155 | + } |
| 156 | + ASSERT_TRUE(mqttConnection); |
| 157 | + } |
| 158 | + |
| 159 | + return AWS_ERROR_SUCCESS; |
| 160 | +} |
| 161 | + |
| 162 | +AWS_TEST_CASE(IotPublishSubscribe, s_TestIotPublishSubscribe) |
0 commit comments