Skip to content

Commit

Permalink
Add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeuz committed Aug 30, 2024
1 parent 3105d62 commit f3f9e48
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 16 deletions.
56 changes: 43 additions & 13 deletions user/tests/integration/communication/events/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,37 @@
namespace {

String eventName;
String eventData;
Variant eventData;
ContentType eventContentType = ContentType();
bool eventReceived = false;

void clearReceivedEvent() {
eventName = String();
eventData = Variant();
eventContentType = ContentType();
eventReceived = false;
}

void eventHandler(const char* name, const char* data, size_t size, ContentType type) {
clearReceivedEvent();
eventName = name;
eventData = String(data, size);
eventData = Buffer(data, size);
eventContentType = type;
eventReceived = true;
}

void clearReceivedEvent() {
eventName = String();
eventData = String();
eventContentType = ContentType();
eventReceived = false;
void eventHandler2(const char* name, Variant data) {
clearReceivedEvent();
eventName = name;
eventData = std::move(data);
eventReceived = true;
}

} // namespace

test(connect) {
Particle.subscribe(System.deviceID() + "/my_event", eventHandler);
Particle.subscribe(System.deviceID() + "/my_event2", eventHandler2);
Particle.connect();
assertTrue(waitFor(Particle.connected, 60000));
}
Expand All @@ -46,9 +55,16 @@ test(verify_max_event_data_size) {
assertTrue((bool)Particle.publish("my_event", data, PRIVATE | WITH_ACK));
}

test(device_to_cloud_event_with_content_type) {
uint8_t data[] = { 0x92, 0xb2, 0xd1, 0x00, 0x23, 0x8c, 0x49, 0x0b, 0xd6, 0xe7, 0xa8, 0x90, 0x50, 0xdb, 0xc5, 0xbb, 0xe5, 0x3f, 0xf3, 0x29, 0xb2, 0x4c, 0xef, 0xad, 0xad, 0xd8, 0x10, 0xd9, 0xc3, 0xa4, 0xf1, 0xb0, 0xe7, 0x74, 0x8e, 0x7e, 0x2e, 0xcf, 0x48, 0xbe, 0x4d, 0xd3, 0xae, 0x08, 0x36, 0x8f, 0x76, 0xa8, 0xd5, 0x50, 0xec, 0x13, 0x9d, 0x5b, 0xca, 0x62, 0x4e, 0x3c, 0x6b, 0x3c, 0xbc, 0x75, 0x85, 0x65, 0x35, 0x6c, 0x00, 0xaf, 0xee, 0x12, 0xf2, 0xbd, 0x3f, 0xf2, 0x27, 0x00, 0xdc, 0x4c, 0xc6, 0xfa, 0x02, 0x16, 0x8e, 0xe5, 0xa1, 0xe6, 0xe9, 0x40, 0x4a, 0x71, 0xd1, 0x7d, 0xa5, 0xa6, 0xb7, 0x8d, 0x7d, 0x47, 0x5f, 0xf2 };
assertTrue((bool)Particle.publish("my_event", (const char*)data, sizeof(data), ContentType::BINARY, WITH_ACK));
test(publish_device_to_cloud_event_with_content_type) {
auto data = Buffer::fromHex("92b2d100238c490bd6e7a89050dbc5bbe53ff329b24cefadadd810d9c3a4f1b0e7748e7e2ecf48be4dd3ae08368f76a8d550ec139d5bca624e3c6b3cbc758565356c00afee12f2bd3ff22700dc4cc6fa02168ee5a1e6e9404a71d17da5a6b78d7d475ff2");
assertTrue((bool)Particle.publish("my_event", data.data(), data.size(), ContentType::BINARY, WITH_ACK));
}

test(publish_device_to_cloud_event_as_variant) {
Variant v;
v["a"] = 123;
v["b"] = Buffer::fromHex("6e7200463f1bb774472f");
assertTrue((bool)Particle.publish("my_event", v, WITH_ACK));
}

test(publish_cloud_to_device_event_with_content_type) {
Expand All @@ -59,9 +75,23 @@ test(validate_cloud_to_device_event_with_content_type) {
assertTrue(waitFor([]() {
return eventReceived;
}, 10000));
uint8_t expectedData[] = { 0xcb, 0xdf, 0x43, 0x83, 0x00, 0x86, 0x31, 0x72, 0x8b, 0xaf, 0x35, 0xc2, 0xaa, 0xae, 0x5d, 0x2e, 0x77, 0x76, 0x91, 0xb1, 0x31, 0xa5, 0xf1, 0x05, 0x1d, 0x7f, 0xc1, 0x47, 0xa8, 0x1f, 0x2a, 0x90, 0xe5, 0x75, 0x30, 0x9d, 0xdc, 0x28, 0x90, 0x68, 0x8b, 0xb8, 0x6e, 0x6e, 0x85, 0x14, 0x0d, 0x95, 0xc0, 0x64, 0xfd, 0xf3, 0xce, 0x3d, 0xfb, 0x45, 0xa3, 0xa7, 0xfe, 0x3d, 0xcf, 0x94, 0xd8, 0x69, 0xcb, 0x21, 0x39, 0x2c, 0x9a, 0xc9, 0xbb, 0x5b, 0xcb, 0x2d, 0xd9, 0x43, 0xb5, 0xbe, 0x21, 0xdd, 0x3b, 0xe1, 0x8c, 0x87, 0x64, 0x68, 0x00, 0x4d, 0x98, 0x1e, 0x6a, 0xe0, 0x2a, 0x42, 0xe8, 0x05, 0xb9, 0x89, 0xef, 0xcd };
auto expectedData = Buffer::fromHex("cbdf4383008631728baf35c2aaae5d2e777691b131a5f1051d7fc147a81f2a90e575309ddc2890688bb86e6e85140d95c064fdf3ce3dfb45a3a7fe3dcf94d869cb21392c9ac9bb5bcb2dd943b5be21dd3be18c876468004d981e6ae02a42e805b989efcd");
assertTrue(eventName == System.deviceID() + "/my_event");
assertTrue(eventData.length() == sizeof(expectedData));
assertTrue(std::memcmp(eventData.c_str(), expectedData, sizeof(expectedData)) == 0);
assertTrue(eventData.asBuffer() == expectedData);
assertTrue(eventContentType == ContentType::BINARY);
}

test(publish_cloud_to_device_event_with_cbor_data) {
clearReceivedEvent();
}

test(validate_cloud_to_device_event_with_cbor_data) {
assertTrue(waitFor([]() {
return eventReceived;
}, 10000));
assertTrue(eventName == System.deviceID() + "/my_event2");
Variant v;
v["c"] = 456;
v["d"] = Buffer::fromHex("921bff008d91814e789b");
assertTrue(eventData == v);
}
23 changes: 20 additions & 3 deletions user/tests/integration/communication/events/events.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,24 @@ test('verify_max_event_data_size', async function() {
expect(data).to.equal(str.slice(0, maxEventDataSize));
});

test('device_to_cloud_event_with_content_type', async function() {
const expectedData = Buffer.from([0x92, 0xb2, 0xd1, 0x00, 0x23, 0x8c, 0x49, 0x0b, 0xd6, 0xe7, 0xa8, 0x90, 0x50, 0xdb, 0xc5, 0xbb, 0xe5, 0x3f, 0xf3, 0x29, 0xb2, 0x4c, 0xef, 0xad, 0xad, 0xd8, 0x10, 0xd9, 0xc3, 0xa4, 0xf1, 0xb0, 0xe7, 0x74, 0x8e, 0x7e, 0x2e, 0xcf, 0x48, 0xbe, 0x4d, 0xd3, 0xae, 0x08, 0x36, 0x8f, 0x76, 0xa8, 0xd5, 0x50, 0xec, 0x13, 0x9d, 0x5b, 0xca, 0x62, 0x4e, 0x3c, 0x6b, 0x3c, 0xbc, 0x75, 0x85, 0x65, 0x35, 0x6c, 0x00, 0xaf, 0xee, 0x12, 0xf2, 0xbd, 0x3f, 0xf2, 0x27, 0x00, 0xdc, 0x4c, 0xc6, 0xfa, 0x02, 0x16, 0x8e, 0xe5, 0xa1, 0xe6, 0xe9, 0x40, 0x4a, 0x71, 0xd1, 0x7d, 0xa5, 0xa6, 0xb7, 0x8d, 0x7d, 0x47, 0x5f, 0xf2]);
test('publish_device_to_cloud_event_with_content_type', async function() {
const expectedData = Buffer.from('92b2d100238c490bd6e7a89050dbc5bbe53ff329b24cefadadd810d9c3a4f1b0e7748e7e2ecf48be4dd3ae08368f76a8d550ec139d5bca624e3c6b3cbc758565356c00afee12f2bd3ff22700dc4cc6fa02168ee5a1e6e9404a71d17da5a6b78d7d475ff2', 'hex');
let d = await this.particle.receiveEvent('my_event');
d = parseDataUri(d);
expect(d.type).to.equal('application/octet-stream');
expect(d.data.equals(expectedData)).to.be.true;
});

test('publish_device_to_cloud_event_as_variant', async function() {
const expectedData = Buffer.from('a26161187b61624a6e7200463f1bb774472f', 'hex');
let d = await this.particle.receiveEvent('my_event');
d = parseDataUri(d);
expect(d.type).to.equal('application/cbor');
expect(d.data.equals(expectedData)).to.be.true;
});

test('publish_cloud_to_device_event_with_content_type', async function() {
const data = Buffer.from([0xcb, 0xdf, 0x43, 0x83, 0x00, 0x86, 0x31, 0x72, 0x8b, 0xaf, 0x35, 0xc2, 0xaa, 0xae, 0x5d, 0x2e, 0x77, 0x76, 0x91, 0xb1, 0x31, 0xa5, 0xf1, 0x05, 0x1d, 0x7f, 0xc1, 0x47, 0xa8, 0x1f, 0x2a, 0x90, 0xe5, 0x75, 0x30, 0x9d, 0xdc, 0x28, 0x90, 0x68, 0x8b, 0xb8, 0x6e, 0x6e, 0x85, 0x14, 0x0d, 0x95, 0xc0, 0x64, 0xfd, 0xf3, 0xce, 0x3d, 0xfb, 0x45, 0xa3, 0xa7, 0xfe, 0x3d, 0xcf, 0x94, 0xd8, 0x69, 0xcb, 0x21, 0x39, 0x2c, 0x9a, 0xc9, 0xbb, 0x5b, 0xcb, 0x2d, 0xd9, 0x43, 0xb5, 0xbe, 0x21, 0xdd, 0x3b, 0xe1, 0x8c, 0x87, 0x64, 0x68, 0x00, 0x4d, 0x98, 0x1e, 0x6a, 0xe0, 0x2a, 0x42, 0xe8, 0x05, 0xb9, 0x89, 0xef, 0xcd]);
const data = Buffer.from('cbdf4383008631728baf35c2aaae5d2e777691b131a5f1051d7fc147a81f2a90e575309ddc2890688bb86e6e85140d95c064fdf3ce3dfb45a3a7fe3dcf94d869cb21392c9ac9bb5bcb2dd943b5be21dd3be18c876468004d981e6ae02a42e805b989efcd', 'hex');
await this.particle.apiClient.instance.publishEvent({
name: `${deviceId}/my_event`,
data: `data:application/octet-stream;base64,${data.toString('base64')}`,
Expand All @@ -70,3 +78,12 @@ test('publish_cloud_to_device_event_with_content_type', async function() {
test('validate_cloud_to_device_event_with_content_type', async function() {
// See events.cpp
});

test('publish_cloud_to_device_event_with_cbor_data', async function() {
const data = Buffer.from('a261631901c861644a921bff008d91814e789b', 'hex');
await this.particle.apiClient.instance.publishEvent({
name: `${deviceId}/my_event2`,
data: `data:application/octet-stream;base64,${data.toString('base64')}`,
auth: this.particle.apiClient.token
});
});
36 changes: 36 additions & 0 deletions user/tests/integration/communication/ledger/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,40 @@ test(07_validate_cloud_to_device_sync_large_size) {
}
}

test(08_sync_device_to_cloud_binary_data) {
g_synced = false;
auto ledger = Particle.ledger(DEVICE_TO_CLOUD_LEDGER);
ledger.onSync([](Ledger /* ledger */) {
g_synced = true;
});
SCOPE_GUARD({
ledger.onSync(nullptr);
});
LedgerData d = { { "b", Buffer::fromHex("92b3409f87002be21dd272b5a4a5c9f4a960cdf08994dc50b4ab3b2ba27727abcfdde8a6e81566b711b912f2f4256716224d") } };
ledger.set(d);
waitFor([]() {
return g_synced;
}, 60000);
assertTrue(g_synced);
}

test(09_update_cloud_to_device_binary_data) {
g_synced = false;
auto ledger = Particle.ledger(CLOUD_TO_DEVICE_LEDGER);
ledger.onSync([](Ledger /* ledger */) {
g_synced = true;
});
}

test(10_validate_cloud_to_device_sync_binary_data) {
waitFor([]() {
return g_synced;
}, 60000);
auto ledger = Particle.ledger(CLOUD_TO_DEVICE_LEDGER);
ledger.onSync(nullptr);
assertTrue(g_synced);
auto d = ledger.get();
assertTrue((d == LedgerData{ { "c", Buffer::fromHex("b7b5e8af68d10051c80f6e401d78aeea3421abac61983ab267f40534ae21fe6699347124afa89e1b3887ec71f5d9042acd28") } }));
}

#endif // Wiring_Ledger
25 changes: 25 additions & 0 deletions user/tests/integration/communication/ledger/ledger.spec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f3f9e48

Please sign in to comment.