Skip to content

Commit c330de6

Browse files
Add more proto error tests; skip before ZMQ 4.3.
1 parent 52a6f59 commit c330de6

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

src/observer.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ static inline const char* EventName(uint32_t val) {
6565
return "handshake:error:auth";
6666
#endif
6767

68-
/* <---- Insert new events here. */
68+
/* <---- Insert new events here. */
6969

70-
/* Fallback if the event was unknown. */
7170
default:
71+
/* Fallback if the event was unknown, which should not happen. */
7272
return "unknown";
7373
}
7474
}
@@ -83,6 +83,7 @@ static inline const char* AuthError(uint32_t val) {
8383
case 500:
8484
return "Internal error";
8585
default:
86+
/* Fallback if the auth error was unknown, which should not happen. */
8687
return "Unknown error";
8788
}
8889
}
@@ -116,6 +117,7 @@ static inline std::pair<const char*, const char*> ProtoError(uint32_t val) {
116117
PROTO_ERROR_CASE(ZAP, INVALID_STATUS_CODE);
117118
PROTO_ERROR_CASE(ZAP, INVALID_METADATA);
118119
default:
120+
/* Fallback if the proto error was unknown, which should not happen. */
119121
return std::make_pair("Unknown error", "ERR_UNKNOWN");
120122
}
121123
}

test/unit/socket-zap-test.ts

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as semver from "semver"
12
import * as zmq from "../../src"
23

34
import {assert} from "chai"
@@ -15,7 +16,7 @@ for (const proto of testProtos("tcp", "ipc")) {
1516
})
1617

1718
afterEach(function() {
18-
handler.stop()
19+
if (handler) handler.stop()
1920
sockA.close()
2021
sockB.close()
2122
global.gc()
@@ -49,6 +50,9 @@ for (const proto of testProtos("tcp", "ipc")) {
4950
})
5051

5152
it("should report authentication error", async function() {
53+
/* ZMQ < 4.3.0 does not have these event details. */
54+
if (semver.satisfies(zmq.version, "< 4.3.0")) this.skip()
55+
5256
handler = new ValidatingZapHandler({
5357
domain: "test",
5458
mechanism: "PLAIN",
@@ -86,7 +90,10 @@ for (const proto of testProtos("tcp", "ipc")) {
8690
assert.equal(eventB.error.status, 400)
8791
})
8892

89-
it("should report protocol error", async function() {
93+
it("should report protocol version error", async function() {
94+
/* ZMQ < 4.3.0 does not have these event details. */
95+
if (semver.satisfies(zmq.version, "< 4.3.0")) this.skip()
96+
9097
handler = new CustomZapHandler(
9198
([path, delim, version, id, ...rest]) => {
9299
return [path, delim, "9.9", id, "200", "OK", null, null]
@@ -97,7 +104,6 @@ for (const proto of testProtos("tcp", "ipc")) {
97104
sockA.zapDomain = "test"
98105

99106
sockB.plainUsername = "user"
100-
sockB.plainPassword = "BAD PASS"
101107

102108
const address = uniqAddress(proto)
103109
await sockA.bind(address)
@@ -110,6 +116,52 @@ for (const proto of testProtos("tcp", "ipc")) {
110116
assert.equal(eventA.error.message, "ZAP protocol error")
111117
assert.equal(eventA.error.code, "ERR_ZAP_BAD_VERSION")
112118
})
119+
120+
it("should report protocol format error", async function() {
121+
/* ZMQ < 4.3.0 does not have these event details. */
122+
if (semver.satisfies(zmq.version, "< 4.3.0")) this.skip()
123+
124+
handler = new CustomZapHandler(
125+
([path, delim, ...rest]) => {
126+
return [path, delim, null, null]
127+
},
128+
)
129+
130+
sockA.plainServer = true
131+
sockA.zapDomain = "test"
132+
133+
sockB.plainUsername = "user"
134+
135+
const address = uniqAddress(proto)
136+
await sockA.bind(address)
137+
await sockB.connect(address)
138+
139+
const eventA = await captureEvent(sockA, "handshake:error:protocol")
140+
assert.equal(eventA.type, "handshake:error:protocol")
141+
assert.equal(eventA.address, address)
142+
assert.instanceOf(eventA.error, Error)
143+
assert.equal(eventA.error.message, "ZAP protocol error")
144+
assert.equal(eventA.error.code, "ERR_ZAP_MALFORMED_REPLY")
145+
})
146+
147+
it("should report mechanism mismatch error", async function() {
148+
/* ZMQ < 4.3.0 does not have these event details. */
149+
if (semver.satisfies(zmq.version, "< 4.3.0")) this.skip()
150+
151+
sockA.plainServer = true
152+
sockB.curveServer = true
153+
154+
const address = uniqAddress(proto)
155+
await sockA.bind(address)
156+
await sockB.connect(address)
157+
158+
const eventA = await captureEvent(sockA, "handshake:error:protocol")
159+
assert.equal(eventA.type, "handshake:error:protocol")
160+
assert.equal(eventA.address, address)
161+
assert.instanceOf(eventA.error, Error)
162+
assert.equal(eventA.error.message, "ZMTP protocol error")
163+
assert.equal(eventA.error.code, "ERR_ZMTP_MECHANISM_MISMATCH")
164+
})
113165
})
114166
})
115167
}

0 commit comments

Comments
 (0)