aws-iot-device-sdk-v2
Version:
NodeJS API for the AWS IoT service
260 lines • 11.4 kB
JavaScript
;
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const aws_crt_1 = require("aws-crt");
const events_1 = require("events");
const uuid_1 = require("uuid");
const client_iot_1 = require("@aws-sdk/client-iot");
const iotidentityclientv2_1 = require("./iotidentityclientv2");
const fs_1 = __importDefault(require("fs"));
jest.setTimeout(10000);
function hasTestEnvironment() {
if (process.env.AWS_TEST_IOT_CORE_PROVISIONING_HOST === undefined) {
return false;
}
if (process.env.AWS_TEST_IOT_CORE_PROVISIONING_CERTIFICATE_PATH === undefined) {
return false;
}
if (process.env.AWS_TEST_IOT_CORE_PROVISIONING_KEY_PATH === undefined) {
return false;
}
if (process.env.AWS_TEST_IOT_CORE_PROVISIONING_TEMPLATE_NAME === undefined) {
return false;
}
if (process.env.AWS_TEST_IOT_CORE_PROVISIONING_CSR_PATH === undefined) {
return false;
}
return true;
}
const conditional_test = (condition) => condition ? it : it.skip;
function build_protocol_client_mqtt5() {
let builder = aws_crt_1.iot.AwsIotMqtt5ClientConfigBuilder.newDirectMqttBuilderWithMtlsFromPath(
// @ts-ignore
process.env.AWS_TEST_IOT_CORE_PROVISIONING_HOST, process.env.AWS_TEST_IOT_CORE_PROVISIONING_CERTIFICATE_PATH, process.env.AWS_TEST_IOT_CORE_PROVISIONING_KEY_PATH);
builder.withConnectProperties({
clientId: `test-${(0, uuid_1.v4)()}`,
keepAliveIntervalSeconds: 1200,
});
return new aws_crt_1.mqtt5.Mqtt5Client(builder.build());
}
function build_protocol_client_mqtt311() {
// @ts-ignore
let builder = aws_crt_1.iot.AwsIotMqttConnectionConfigBuilder.new_mtls_builder_from_path(process.env.AWS_TEST_IOT_CORE_PROVISIONING_CERTIFICATE_PATH, process.env.AWS_TEST_IOT_CORE_PROVISIONING_KEY_PATH);
// @ts-ignore
builder.with_endpoint(process.env.AWS_TEST_IOT_CORE_PROVISIONING_HOST);
builder.with_client_id(`test-${(0, uuid_1.v4)()}`);
let client = new aws_crt_1.mqtt.MqttClient();
return client.new_connection(builder.build());
}
var ProtocolVersion;
(function (ProtocolVersion) {
ProtocolVersion[ProtocolVersion["Mqtt311"] = 0] = "Mqtt311";
ProtocolVersion[ProtocolVersion["Mqtt5"] = 1] = "Mqtt5";
})(ProtocolVersion || (ProtocolVersion = {}));
class IdentityTestingContext {
startProtocolClient() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.protocolStarted) {
this.protocolStarted = true;
if (this.mqtt5Client) {
let connected = (0, events_1.once)(this.mqtt5Client, aws_crt_1.mqtt5.Mqtt5Client.CONNECTION_SUCCESS);
this.mqtt5Client.start();
yield connected;
}
if (this.mqtt311Client) {
yield this.mqtt311Client.connect();
}
}
});
}
stopProtocolClient() {
return __awaiter(this, void 0, void 0, function* () {
if (this.protocolStarted) {
this.protocolStarted = false;
if (this.mqtt5Client) {
let stopped = (0, events_1.once)(this.mqtt5Client, aws_crt_1.mqtt5.Mqtt5Client.STOPPED);
this.mqtt5Client.stop();
yield stopped;
this.mqtt5Client.close();
}
if (this.mqtt311Client) {
yield this.mqtt311Client.disconnect();
}
}
});
}
constructor(options) {
var _a, _b;
this.protocolStarted = false;
if (options.version == ProtocolVersion.Mqtt5) {
this.mqtt5Client = build_protocol_client_mqtt5();
let rrOptions = {
maxRequestResponseSubscriptions: 6,
maxStreamingSubscriptions: 2,
operationTimeoutInSeconds: (_a = options.timeoutSeconds) !== null && _a !== void 0 ? _a : 60,
};
this.client = iotidentityclientv2_1.IotIdentityClientv2.newFromMqtt5(this.mqtt5Client, rrOptions);
}
else {
this.mqtt311Client = build_protocol_client_mqtt311();
let rrOptions = {
maxRequestResponseSubscriptions: 6,
maxStreamingSubscriptions: 2,
operationTimeoutInSeconds: (_b = options.timeoutSeconds) !== null && _b !== void 0 ? _b : 60,
};
this.client = iotidentityclientv2_1.IotIdentityClientv2.newFromMqtt311(this.mqtt311Client, rrOptions);
}
}
open() {
return __awaiter(this, void 0, void 0, function* () {
yield this.startProtocolClient();
});
}
close() {
return __awaiter(this, void 0, void 0, function* () {
this.client.close();
yield this.stopProtocolClient();
});
}
}
function doCreateDestroyTest(version) {
return __awaiter(this, void 0, void 0, function* () {
let context = new IdentityTestingContext({
version: version
});
yield context.open();
yield context.close();
});
}
conditional_test(hasTestEnvironment())('identityv2 - create destroy mqtt5', () => __awaiter(void 0, void 0, void 0, function* () {
yield doCreateDestroyTest(ProtocolVersion.Mqtt5);
}));
conditional_test(hasTestEnvironment())('identityv2 - create destroy mqtt311', () => __awaiter(void 0, void 0, void 0, function* () {
yield doCreateDestroyTest(ProtocolVersion.Mqtt311);
}));
//@ts-ignore
let identityResources = {};
beforeEach(() => __awaiter(void 0, void 0, void 0, function* () {
identityResources = {};
}));
afterEach(() => __awaiter(void 0, void 0, void 0, function* () {
const client = new client_iot_1.IoTClient({});
if (identityResources.thingName) {
yield new Promise(r => setTimeout(r, 1000));
const command = new client_iot_1.DeleteThingCommand({
thingName: identityResources.thingName
});
try {
yield client.send(command);
}
catch (e) { }
}
if (identityResources.certificateId) {
yield new Promise(r => setTimeout(r, 1000));
const deactivateCommand = new client_iot_1.UpdateCertificateCommand({
certificateId: identityResources.certificateId,
newStatus: client_iot_1.CertificateStatus.INACTIVE
});
try {
yield client.send(deactivateCommand);
}
catch (e) { }
yield new Promise(r => setTimeout(r, 1000));
const deleteCommand = new client_iot_1.DeleteCertificateCommand({
certificateId: identityResources.certificateId,
});
try {
yield client.send(deleteCommand);
}
catch (e) { }
}
identityResources = {};
}));
function doProvisioningTest(version) {
return __awaiter(this, void 0, void 0, function* () {
let context = new IdentityTestingContext({
version: version
});
yield context.open();
let createKeysResponse = yield context.client.createKeysAndCertificate({});
identityResources.certificateId = createKeysResponse.certificateId;
expect(createKeysResponse.certificateId).toBeDefined();
expect(createKeysResponse.certificatePem).toBeDefined();
expect(createKeysResponse.privateKey).toBeDefined();
expect(createKeysResponse.certificateOwnershipToken).toBeDefined();
const params = JSON.parse(`{"SerialNumber":"${(0, uuid_1.v4)()}"}`);
let registerThingResponse = yield context.client.registerThing({
// @ts-ignore
templateName: process.env.AWS_TEST_IOT_CORE_PROVISIONING_TEMPLATE_NAME,
certificateOwnershipToken: createKeysResponse.certificateOwnershipToken,
parameters: params
});
identityResources.thingName = registerThingResponse.thingName;
expect(registerThingResponse.thingName).toBeDefined();
context.client.close();
yield context.close();
});
}
conditional_test(hasTestEnvironment())('identityv2 provisioning mqtt5', () => __awaiter(void 0, void 0, void 0, function* () {
yield doProvisioningTest(ProtocolVersion.Mqtt5);
}));
conditional_test(hasTestEnvironment())('identityv2 provisioning mqtt311', () => __awaiter(void 0, void 0, void 0, function* () {
yield doProvisioningTest(ProtocolVersion.Mqtt311);
}));
function doCsrProvisioningTest(version) {
return __awaiter(this, void 0, void 0, function* () {
let csr = "";
try {
csr = fs_1.default.readFileSync(process.env.AWS_TEST_IOT_CORE_PROVISIONING_CSR_PATH, 'utf8');
}
catch (e) {
if (e instanceof Error) {
console.log('Error reading CSR PEM file:', e.stack);
}
}
let context = new IdentityTestingContext({
version: version
});
yield context.open();
let createCertificateFromCsrResponse = yield context.client.createCertificateFromCsr({
certificateSigningRequest: csr
});
identityResources.certificateId = createCertificateFromCsrResponse.certificateId;
expect(createCertificateFromCsrResponse.certificateId).toBeDefined();
expect(createCertificateFromCsrResponse.certificatePem).toBeDefined();
expect(createCertificateFromCsrResponse.certificateOwnershipToken).toBeDefined();
const params = JSON.parse(`{"SerialNumber":"${(0, uuid_1.v4)()}"}`);
let registerThingResponse = yield context.client.registerThing({
// @ts-ignore
templateName: process.env.AWS_TEST_IOT_CORE_PROVISIONING_TEMPLATE_NAME,
certificateOwnershipToken: createCertificateFromCsrResponse.certificateOwnershipToken,
parameters: params
});
identityResources.thingName = registerThingResponse.thingName;
expect(registerThingResponse.thingName).toBeDefined();
context.client.close();
yield context.close();
});
}
conditional_test(hasTestEnvironment())('identityv2 CSR provisioning mqtt5', () => __awaiter(void 0, void 0, void 0, function* () {
yield doCsrProvisioningTest(ProtocolVersion.Mqtt5);
}));
conditional_test(hasTestEnvironment())('identityv2 CSR provisioning mqtt311', () => __awaiter(void 0, void 0, void 0, function* () {
yield doCsrProvisioningTest(ProtocolVersion.Mqtt311);
}));
//# sourceMappingURL=iotidentityclientv2.spec.js.map