Skip to content

Commit e0de5f7

Browse files
justinboswellJonathanHenson
authored andcommitted
Tests n fixes (aws#16)
* Fixed memory leaks after structure changes in aws-c-io * Added IoT service unit test * Current state of IoT service test * Revert "Fixed memory leaks after structure changes in aws-c-io" This reverts commit 1b5ed8b961542c04721631bb7f7247e9af341720. * Updated to mqtt version v0.3.2 * Made service test optional if certs don't exist * clang-format
1 parent 13aeaff commit e0de5f7

File tree

3 files changed

+164
-1
lines changed

3 files changed

+164
-1
lines changed

aws-common-runtime/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ set(AWS_C_IO_SHA "v0.2.4")
3131
include(BuildAwsCIO)
3232

3333
set(AWS_C_MQTT_URL "https://github.com/awslabs/aws-c-mqtt.git")
34-
set(AWS_C_MQTT_SHA "v0.3.1")
34+
set(AWS_C_MQTT_SHA "v0.3.2")
3535
include(BuildAwsCMqtt)
3636

3737
set(AWS_C_CAL_URL "https://github.com/awslabs/aws-c-cal.git")

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ add_test_case(BasicJsonParsing)
1717
add_test_case(SHA256ResourceSafety)
1818
add_test_case(MD5ResourceSafety)
1919
add_test_case(SHA256HMACResourceSafety)
20+
add_test_case(IotPublishSubscribe)
2021

2122
generate_cpp_test_driver(${TEST_BINARY_NAME})
2223

tests/IotServiceTest.cpp

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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

Comments
 (0)