-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
76 lines (67 loc) · 2.57 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import mqtt from "mqtt";
import { describe, test } from "node:test";
import assert from "node:assert/strict";
import { randomWords } from "@nordicsemiconductor/random-words";
import { ipv4, ipv6 } from "./ip";
import fs from "node:fs/promises";
const hostname = process.env.HOSTNAME ?? "localhost";
const ipv = process.env.IPV ?? "ipv4";
const rejectUnauthorized = process.env.VALIDATE_TLS_CERT !== "0";
const mqttPort = process.env.MQTT_PORT ?? "1883";
const mqttsPort = process.env.MQTTS_PORT ?? "8883";
const certPath = process.env.CERT_PATH;
console.log(`hostname:`, JSON.stringify(hostname));
console.log(`ipv:`, JSON.stringify(ipv));
console.log(`rejectUnauthorized:`, JSON.stringify(rejectUnauthorized));
const addr =
ipv === "ipv6" ? `[${await ipv6(hostname)}]` : await ipv4(hostname);
describe("MQTT server", async () => {
await Promise.all(
[`mqtt://${addr}:${mqttPort}`, `mqtts://${addr}:${mqttsPort}`].map(
(endpoint) =>
describe(endpoint, async () => {
test("the MQTT server should allow to publish and subscribe", async () => {
const msg = `Hello World (${Date.now()})!`;
const topic = randomWords().join("-");
const client = mqtt.connect(endpoint, {
rejectUnauthorized,
servername: hostname,
// Use the server certificate as the CA so the client can validate the server
ca:
certPath === undefined
? undefined
: await fs.readFile(certPath, "utf-8"),
});
const received = await new Promise<string>((resolve, reject) => {
const t = setTimeout(() => {
reject(new Error(`Timeout!`));
client.end();
}, 5000);
client.on("message", (topic, message) => {
clearTimeout(t);
console.log(`<`, topic);
console.log(`<`, message.toString());
resolve(message.toString());
client.end();
});
console.log(`>`, topic);
console.log(`>`, msg);
client.on("connect", () => {
client.subscribe(topic, (err) => {
if (!err) {
client.publish(topic, msg);
}
});
});
client.on("error", (err) => {
clearTimeout(t);
console.error(err);
reject(err);
});
});
assert.equal(received, msg);
});
}),
),
);
});