Skip to content

Commit 57a078c

Browse files
committed
Update the test cases due to the change in address formats
1 parent 50fc2c7 commit 57a078c

File tree

4 files changed

+25
-18
lines changed

4 files changed

+25
-18
lines changed

application/commands/address.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ const (
6161
HostNameAddr AddressType = 0x03
6262
)
6363

64+
// Address size limits
65+
const (
66+
MaxHostNameLen = 255
67+
)
68+
6469
// Address data
6570
type Address struct {
6671
port uint16
@@ -197,7 +202,7 @@ func (a Address) Marshal(b []byte) (int, error) {
197202
}
198203
b[0] = byte(a.port >> 8)
199204
b[1] = byte(a.port)
200-
b[2] = byte(LoopbackAddr << 6)
205+
b[2] = byte(LoopbackAddr)
201206
return 3, nil
202207

203208
case IPv4Addr:
@@ -206,7 +211,7 @@ func (a Address) Marshal(b []byte) (int, error) {
206211
}
207212
b[0] = byte(a.port >> 8)
208213
b[1] = byte(a.port)
209-
b[2] = byte(IPv4Addr << 6)
214+
b[2] = byte(IPv4Addr)
210215
copy(b[3:], a.data)
211216
return 7, nil
212217

@@ -216,25 +221,24 @@ func (a Address) Marshal(b []byte) (int, error) {
216221
}
217222
b[0] = byte(a.port >> 8)
218223
b[1] = byte(a.port)
219-
b[2] = byte(IPv6Addr << 6)
224+
b[2] = byte(IPv6Addr)
220225
copy(b[3:], a.data)
221-
222226
return 19, nil
223227

224228
case HostNameAddr:
225229
hLen := len(a.data)
226-
if hLen > 0x3f {
230+
if hLen > MaxHostNameLen {
227231
panic("Host name cannot longer than 0x3f")
228232
}
229-
if bLen < hLen+3 {
233+
if bLen < hLen+4 {
230234
return 0, ErrAddressMarshalBufferTooSmall
231235
}
232236
b[0] = byte(a.port >> 8)
233237
b[1] = byte(a.port)
234-
b[2] = byte(HostNameAddr << 6)
235-
b[2] |= byte(hLen)
236-
copy(b[3:], a.data)
237-
return hLen + 3, nil
238+
b[2] = byte(HostNameAddr)
239+
b[3] = byte(hLen)
240+
copy(b[4:], a.data)
241+
return hLen + 4, nil
238242

239243
default:
240244
return 0, ErrAddressInvalidAddressType

application/commands/address_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func TestParseAddress(t *testing.T) {
9595
testParseAddress(
9696
t,
9797
[]byte{
98-
0x04, 0x1e, 0x40,
98+
0x04, 0x1e, byte(IPv4Addr),
9999
0x7f, 0x00, 0x00, 0x01,
100100
},
101101
make([]byte, 4), IPv4Addr, []byte{0x7f, 0x00, 0x00, 0x01}, 1054,
@@ -104,7 +104,7 @@ func TestParseAddress(t *testing.T) {
104104
testParseAddress(
105105
t,
106106
[]byte{
107-
0x04, 0x1e, 0x80,
107+
0x04, 0x1e, byte(IPv6Addr),
108108
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
109109
0x00, 0x7f, 0x00, 0x00, 0x01,
110110
},
@@ -116,7 +116,8 @@ func TestParseAddress(t *testing.T) {
116116
testParseAddress(
117117
t,
118118
[]byte{
119-
0x04, 0x1e, 0xff,
119+
0x04, 0x1e, byte(HostNameAddr), 73,
120+
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
120121
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
121122
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
122123
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
@@ -125,7 +126,8 @@ func TestParseAddress(t *testing.T) {
125126
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
126127
'1', '2', '3',
127128
},
128-
make([]byte, 63), HostNameAddr, []byte{
129+
make([]byte, 73), HostNameAddr, []byte{
130+
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
129131
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
130132
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
131133
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
@@ -134,5 +136,5 @@ func TestParseAddress(t *testing.T) {
134136
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
135137
'1', '2', '3',
136138
}, 1054,
137-
strings.Repeat("ABCDEFGHIJ", 6)+"123:1054")
139+
strings.Repeat("ABCDEFGHIJ", 7)+"123:1054")
138140
}

ui/commands/address.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class Address {
4646
portNum <<= 8;
4747
portNum |= readed[1];
4848

49-
addrType = readed[2] >> 6;
49+
addrType = readed[2];
5050

5151
switch (addrType) {
5252
case LOOPBACK:
@@ -61,7 +61,8 @@ export class Address {
6161
break;
6262

6363
case HOSTNAME:
64-
addrData = await reader.readN(rd, 0x3f & readed[2]);
64+
addrData = await reader.readN(rd, 1);
65+
addrData = await reader.readN(rd, addrData[0]);
6566
break;
6667

6768
default:

ui/commands/address_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ describe("Address", () => {
8282
it("Address HostName", async () => {
8383
let addr = new address.Address(
8484
address.HOSTNAME,
85-
new Uint8Array(["v", "a", "g", "u", "l", "1", "2", "3"]),
85+
new Uint8Array(['n', 'i', 'r', 'u', 'i', 'o', 'r', 'g', 1, 2, 3]),
8686
8080,
8787
),
8888
buf = addr.buffer();

0 commit comments

Comments
 (0)