Skip to content

Commit 21b0fd7

Browse files
committed
fix: 🐛 correctly set will flags
1 parent c5dad81 commit 21b0fd7

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

src/packets/__tests__/connect.spec.ts

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PROPERTY } from '../../../es6/enums';
1+
import {PROPERTY} from '../../../es6/enums';
22
import {PACKET_TYPE} from '../../enums';
33
import {PacketConnect} from '../connect';
44

@@ -140,3 +140,77 @@ test('can remove will', () => {
140140
expect(packet.wt).toBe(undefined);
141141
expect(packet.wp).toBe(undefined);
142142
});
143+
144+
test('can manipulate multiple properties simultaneously', () => {
145+
const packet = PacketConnect.create(5, 0, 30, {}, 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx');
146+
147+
expect(packet.qualityOfService()).toBe(0);
148+
packet.setQualityOfService(2);
149+
expect(packet.qualityOfService()).toBe(2);
150+
151+
expect(packet.usr).toBe(undefined);
152+
packet.setUserName('foo');
153+
expect(packet.qualityOfService()).toBe(2);
154+
expect(packet.usr).toBe('foo');
155+
156+
expect(packet.pwd).toBe(undefined);
157+
packet.setPassword(Buffer.from([1, 2]));
158+
expect(packet.pwd).toEqual(Buffer.from([1, 2]));
159+
expect(packet.qualityOfService()).toBe(2);
160+
expect(packet.usr).toBe('foo');
161+
162+
expect(packet.cleanStart()).toBe(false);
163+
packet.setCleanStart(true);
164+
expect(packet.cleanStart()).toBe(true);
165+
expect(packet.pwd).toEqual(Buffer.from([1, 2]));
166+
expect(packet.qualityOfService()).toBe(2);
167+
expect(packet.usr).toBe('foo');
168+
169+
expect(packet.willFlag()).toBe(false);
170+
expect(packet.willRetain()).toBe(false);
171+
expect(packet.willQos()).toBe(0);
172+
packet.setWill(Buffer.from([25]), 't', {}, 0, false);
173+
expect(packet.willFlag()).toBe(true);
174+
expect(packet.willRetain()).toBe(false);
175+
expect(packet.willQos()).toBe(0);
176+
expect(packet.cleanStart()).toBe(true);
177+
expect(packet.pwd).toEqual(Buffer.from([1, 2]));
178+
expect(packet.qualityOfService()).toBe(2);
179+
expect(packet.usr).toBe('foo');
180+
181+
packet.removePassword();
182+
expect(packet.willFlag()).toBe(true);
183+
expect(packet.willRetain()).toBe(false);
184+
expect(packet.willQos()).toBe(0);
185+
expect(packet.cleanStart()).toBe(true);
186+
expect(packet.pwd).toBe(undefined);
187+
expect(packet.qualityOfService()).toBe(2);
188+
expect(packet.usr).toBe('foo');
189+
190+
packet.removeUserName();
191+
expect(packet.willFlag()).toBe(true);
192+
expect(packet.willRetain()).toBe(false);
193+
expect(packet.willQos()).toBe(0);
194+
expect(packet.cleanStart()).toBe(true);
195+
expect(packet.pwd).toBe(undefined);
196+
expect(packet.qualityOfService()).toBe(2);
197+
expect(packet.usr).toBe(undefined);
198+
199+
packet.removeWill()
200+
expect(packet.willFlag()).toBe(false);
201+
expect(packet.willRetain()).toBe(false);
202+
expect(packet.willQos()).toBe(0);
203+
expect(packet.cleanStart()).toBe(true);
204+
expect(packet.pwd).toBe(undefined);
205+
expect(packet.qualityOfService()).toBe(2);
206+
expect(packet.usr).toBe(undefined);
207+
208+
packet.setCleanStart(false);
209+
expect(packet.willFlag()).toBe(false);
210+
expect(packet.willRetain()).toBe(false);
211+
expect(packet.willQos()).toBe(0);
212+
expect(packet.cleanStart()).toBe(false);
213+
expect(packet.pwd).toBe(undefined);
214+
expect(packet.qualityOfService()).toBe(2);
215+
expect(packet.usr).toBe(undefined);
216+
});

src/packets/connect.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ export class PacketConnect extends Packet implements PacketConnectData {
106106
this.w = will;
107107
this.wt = willTopic;
108108
this.wp = willProps;
109-
this.f = (((((willRetain ? 1 : 0) << 2) | (willQualityOfService & 0b11)) << 1) | 1) << 2;
109+
const bits = ((((willRetain ? 1 : 0) << 2) | (willQualityOfService & 0b11)) << 1) | 1;
110+
this.f = (this.f & ~0b111100) | (bits << 2);
110111
}
111112

112113
public removeWill() {

0 commit comments

Comments
 (0)