/
ControlPacketFormatTests.java
325 lines (294 loc) · 13.8 KB
/
ControlPacketFormatTests.java
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.artemis.tests.integration.mqtt5.spec;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import io.netty.handler.codec.mqtt.MqttMessageIdAndPropertiesVariableHeader;
import io.netty.handler.codec.mqtt.MqttMessageType;
import io.netty.handler.codec.mqtt.MqttPubAckMessage;
import io.netty.handler.codec.mqtt.MqttPubReplyMessageVariableHeader;
import io.netty.handler.codec.mqtt.MqttPublishMessage;
import io.netty.handler.codec.mqtt.MqttSubscribeMessage;
import io.netty.handler.codec.mqtt.MqttUnsubscribeMessage;
import org.apache.activemq.artemis.core.protocol.mqtt.MQTTInterceptor;
import org.apache.activemq.artemis.tests.integration.mqtt5.MQTT5TestSupport;
import org.apache.activemq.artemis.tests.util.RandomUtil;
import org.apache.activemq.artemis.tests.util.Wait;
import org.eclipse.paho.mqttv5.client.MqttClient;
import org.eclipse.paho.mqttv5.common.MqttMessage;
import org.junit.Test;
/**
* Fulfilled by client or Netty codec (i.e. not explicitly tested here):
*
* [MQTT-2.1.3-1] Where a flag bit is marked as “Reserved” it is reserved for future use and MUST be set to the value listed.
* [MQTT-2.2.2-1] If there are no properties, this MUST be indicated by including a Property Length of zero.
* [MQTT-2.2.1-3] Each time a Client sends a new SUBSCRIBE, UNSUBSCRIBE,or PUBLISH (where QoS > 0) MQTT Control Packet it MUST assign it a non-zero Packet Identifier that is currently unused.
*/
public class ControlPacketFormatTests extends MQTT5TestSupport {
/*
* [MQTT-2.2.1-2] A PUBLISH packet MUST NOT contain a Packet Identifier if its QoS value is set to 0.
*/
@Test(timeout = DEFAULT_TIMEOUT)
public void testPacketIdQoSZero() throws Exception {
final String TOPIC = this.getTopicName();
final String CONSUMER_CLIENT_ID = "consumer";
final int MESSAGE_COUNT = 100;
final CountDownLatch latch = new CountDownLatch(MESSAGE_COUNT);
MqttClient consumer = createPahoClient(CONSUMER_CLIENT_ID);
consumer.setCallback(new DefaultMqttCallback() {
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
assertEquals(0, message.getId());
assertEquals(0, message.getQos());
latch.countDown();
}
});
consumer.connect();
consumer.subscribe(TOPIC, 0);
MqttClient producer = createPahoClient("producer");
producer.connect();
for (int i = 0; i < MESSAGE_COUNT; i++) {
producer.publish(TOPIC, ("foo" + i).getBytes(), 0, false);
}
Wait.assertEquals(MESSAGE_COUNT, () -> getSubscriptionQueue(TOPIC, CONSUMER_CLIENT_ID).getMessagesAdded());
producer.disconnect();
producer.close();
assertTrue(latch.await(3, TimeUnit.SECONDS));
consumer.disconnect();
consumer.close();
}
/*
* [MQTT-2.2.1-4] Each time a Server sends a new PUBLISH (with QoS > 0) MQTT Control Packet it MUST assign it a non
* zero Packet Identifier that is currently unused.
*/
@Test(timeout = DEFAULT_TIMEOUT)
public void testPacketIdQoSGreaterThanZero() throws Exception {
final String CONSUMER_ID = RandomUtil.randomString();
final String TOPIC = this.getTopicName();
final int MESSAGE_COUNT = 10;
final List IDS = new ArrayList();
final Object lock = new Object();
final CountDownLatch latch = new CountDownLatch(MESSAGE_COUNT);
MqttClient consumer = createPahoClient(CONSUMER_ID);
consumer.setCallback(new DefaultMqttCallback() {
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
synchronized (lock) {
System.out.println(message.getId());
assertFalse(IDS.contains(message.getId()));
IDS.add(message.getId());
latch.countDown();
}
}
});
consumer.connect();
consumer.subscribe(TOPIC, 2);
Wait.assertTrue(() -> getSubscriptionQueue(TOPIC, CONSUMER_ID) != null);
Wait.assertEquals(1, () -> getSubscriptionQueue(TOPIC, CONSUMER_ID).getConsumerCount());
MqttClient producer = createPahoClient("producer");
producer.connect();
for (int i = 0; i < MESSAGE_COUNT; i++) {
producer.publish(TOPIC, ("foo" + i).getBytes(), (RandomUtil.randomPositiveInt() % 2) + 1, false);
}
Wait.assertEquals(MESSAGE_COUNT, () -> getSubscriptionQueue(TOPIC, CONSUMER_ID).getMessagesAdded());
producer.disconnect();
producer.close();
assertTrue(latch.await(3, TimeUnit.SECONDS));
consumer.disconnect();
consumer.close();
}
/*
* [MQTT-2.2.1-5] A PUBACK, PUBREC, PUBREL, or PUBCOMP packet MUST contain the same Packet Identifier as the PUBLISH
* packet that was originally sent.
*/
@Test(timeout = DEFAULT_TIMEOUT)
public void testPacketIdPubAckQoS2() throws Exception {
AtomicInteger id = new AtomicInteger(0);
AtomicBoolean failed = new AtomicBoolean(false);
AtomicInteger packetCount = new AtomicInteger(0);
MQTTInterceptor incomingInterceptor = (packet, connection) -> {
if (packet.fixedHeader().messageType() == MqttMessageType.PUBLISH) {
id.set(((MqttPublishMessage)packet).variableHeader().packetId());
packetCount.incrementAndGet();
}
if (packet.fixedHeader().messageType() == MqttMessageType.PUBREL || packet.fixedHeader().messageType() == MqttMessageType.PUBREC || packet.fixedHeader().messageType() == MqttMessageType.PUBCOMP) {
if (((MqttPubReplyMessageVariableHeader)packet.variableHeader()).messageId() != id.get()) {
failed.set(true);
}
packetCount.incrementAndGet();
}
return true;
};
MQTTInterceptor outgoingInterceptor = (packet, connection) -> {
if (packet.fixedHeader().messageType() == MqttMessageType.PUBLISH) {
if (((MqttPublishMessage)packet).variableHeader().packetId() != id.get()) {
failed.set(true);
}
packetCount.incrementAndGet();
}
if (packet.fixedHeader().messageType() == MqttMessageType.PUBREL || packet.fixedHeader().messageType() == MqttMessageType.PUBREC || packet.fixedHeader().messageType() == MqttMessageType.PUBCOMP) {
if (((MqttPubAckMessage)packet).variableHeader().messageId() != id.get()) {
failed.set(true);
}
packetCount.incrementAndGet();
}
return true;
};
server.getRemotingService().addIncomingInterceptor(incomingInterceptor);
server.getRemotingService().addOutgoingInterceptor(outgoingInterceptor);
final String TOPIC = this.getTopicName();
final CountDownLatch latch = new CountDownLatch(1);
final String CONSUMER_ID = "consumer";
MqttClient consumer = createPahoClient(CONSUMER_ID);
consumer.setCallback(new DefaultMqttCallback() {
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
latch.countDown();
}
});
consumer.connect();
consumer.subscribe(TOPIC, 2);
MqttClient producer = createPahoClient("producer");
producer.connect();
producer.publish(TOPIC, "foo".getBytes(StandardCharsets.UTF_8), 2, false);
Wait.assertEquals((long) 1, () -> getSubscriptionQueue(TOPIC, CONSUMER_ID).getMessagesAdded(), 2000, 100);
producer.disconnect();
producer.close();
Wait.assertEquals(1L, () -> getSubscriptionQueue(TOPIC, CONSUMER_ID).getMessagesAcknowledged(), 15000, 100);
assertTrue(latch.await(15, TimeUnit.SECONDS));
Wait.assertFalse(() -> failed.get(), 2000, 100);
Wait.assertEquals(8, () -> packetCount.get());
consumer.disconnect();
consumer.close();
}
/*
* [MQTT-2.2.1-5] A PUBACK, PUBREC, PUBREL, or PUBCOMP packet MUST contain the same Packet Identifier as the PUBLISH
* packet that was originally sent.
*/
@Test(timeout = DEFAULT_TIMEOUT)
public void testPacketIdPubAckQoS1() throws Exception {
AtomicInteger id = new AtomicInteger(0);
AtomicBoolean failed = new AtomicBoolean(false);
AtomicInteger packetCount = new AtomicInteger(0);
MQTTInterceptor incomingInterceptor = (packet, connection) -> {
if (packet.fixedHeader().messageType() == MqttMessageType.PUBLISH) {
id.set(((MqttPublishMessage)packet).variableHeader().packetId());
packetCount.incrementAndGet();
}
if (packet.fixedHeader().messageType() == MqttMessageType.PUBACK) {
if (((MqttPubReplyMessageVariableHeader)packet.variableHeader()).messageId() != id.get()) {
failed.set(true);
}
packetCount.incrementAndGet();
}
return true;
};
MQTTInterceptor outgoingInterceptor = (packet, connection) -> {
if (packet.fixedHeader().messageType() == MqttMessageType.PUBLISH) {
if (((MqttPublishMessage)packet).variableHeader().packetId() != id.get()) {
failed.set(true);
}
packetCount.incrementAndGet();
}
if (packet.fixedHeader().messageType() == MqttMessageType.PUBACK) {
if (((MqttPubAckMessage)packet).variableHeader().messageId() != id.get()) {
failed.set(true);
}
packetCount.incrementAndGet();
}
return true;
};
server.getRemotingService().addIncomingInterceptor(incomingInterceptor);
server.getRemotingService().addOutgoingInterceptor(outgoingInterceptor);
final String TOPIC = this.getTopicName();
final CountDownLatch latch = new CountDownLatch(1);
final String CONSUMER_ID = "consumer";
MqttClient consumer = createPahoClient(CONSUMER_ID);
consumer.setCallback(new DefaultMqttCallback() {
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
latch.countDown();
}
});
consumer.connect();
consumer.subscribe(TOPIC, 1);
MqttClient producer = createPahoClient("producer");
producer.connect();
producer.publish(TOPIC, "foo".getBytes(StandardCharsets.UTF_8), 1, false);
Wait.assertEquals((long) 1, () -> getSubscriptionQueue(TOPIC, CONSUMER_ID).getMessagesAdded(), 2000, 100);
producer.disconnect();
producer.close();
Wait.assertEquals(1L, () -> getSubscriptionQueue(TOPIC, CONSUMER_ID).getMessagesAcknowledged(), 15000, 100);
assertTrue(latch.await(15, TimeUnit.SECONDS));
Wait.assertFalse(() -> failed.get(), 2000, 100);
Wait.assertEquals(4, () -> packetCount.get());
consumer.disconnect();
consumer.close();
}
/*
* [MQTT-2.2.1-6] A SUBACK and UNSUBACK MUST contain the Packet Identifier that was used in the corresponding
* SUBSCRIBE and UNSUBSCRIBE packet respectively.
*/
@Test(timeout = DEFAULT_TIMEOUT)
public void testPacketIdSubAckAndUnsubAck() throws Exception {
AtomicInteger subId = new AtomicInteger(0);
AtomicInteger unsubId = new AtomicInteger(0);
AtomicInteger packetCount = new AtomicInteger(0);
AtomicBoolean failed = new AtomicBoolean(false);
MQTTInterceptor incomingInterceptor = (packet, connection) -> {
if (packet.fixedHeader().messageType() == MqttMessageType.SUBSCRIBE) {
subId.set(((MqttSubscribeMessage)packet).variableHeader().messageId());
packetCount.incrementAndGet();
}
if (packet.fixedHeader().messageType() == MqttMessageType.UNSUBSCRIBE) {
unsubId.set(((MqttUnsubscribeMessage)packet).variableHeader().messageId());
packetCount.incrementAndGet();
}
return true;
};
MQTTInterceptor outgoingInterceptor = (packet, connection) -> {
if (packet.fixedHeader().messageType() == MqttMessageType.SUBACK) {
if (((MqttMessageIdAndPropertiesVariableHeader)packet.variableHeader()).messageId() != subId.get()) {
failed.set(true);
}
packetCount.incrementAndGet();
} else if (packet.fixedHeader().messageType() == MqttMessageType.UNSUBACK) {
if (((MqttMessageIdAndPropertiesVariableHeader)packet.variableHeader()).messageId() != unsubId.get()) {
failed.set(true);
}
packetCount.incrementAndGet();
}
return true;
};
server.getRemotingService().addIncomingInterceptor(incomingInterceptor);
server.getRemotingService().addOutgoingInterceptor(outgoingInterceptor);
final String TOPIC = this.getTopicName();
MqttClient consumer = createPahoClient("consumer");
consumer.connect();
consumer.subscribe(TOPIC, 1);
consumer.unsubscribe(TOPIC);
Wait.assertFalse(() -> failed.get(), 2000, 100);
Wait.assertEquals(4, () -> packetCount.get());
consumer.disconnect();
consumer.close();
}
}