| … | … | @@ -0,0 +1,380 @@ |
| 1 | // Wire-message suite — round-trips for all 14 message types, plus |
| 2 | // back-compat decoding for the appended-last fields (`from` on |
| 3 | // contentRequest, `more` on chunkHashes). |
| 4 | import test from 'brittle' |
| 5 | import c from 'compact-encoding' |
| 6 | import * as m from '../lib/messages.js' |
| 7 | import crypto from 'hypercore-crypto' |
| 8 | |
| 9 | function roundTrip (t, codec, value) { |
| 10 | const state = { start: 0, end: 0, buffer: null } |
| 11 | codec.preencode(state, value) |
| 12 | state.buffer = Buffer.alloc(state.end) |
| 13 | codec.encode(state, value) |
| 14 | state.start = 0 |
| 15 | const decoded = codec.decode(state) |
| 16 | t.alike(decoded, value, 'round-trip') |
| 17 | return decoded |
| 18 | } |
| 19 | |
| 20 | const fakeHash = 'a'.repeat(64) |
| 21 | const fakeHash2 = 'b'.repeat(64) |
| 22 | const fakeHash3 = 'c'.repeat(64) |
| 23 | |
| 24 | // ── Handshake ───────────────────────────────────────────────── |
| 25 | |
| 26 | test('handshake round-trip', (t) => { |
| 27 | roundTrip(t, m.handshake, { version: 2, capabilities: 3 }) |
| 28 | }) |
| 29 | |
| 30 | // ── Sync messages ───────────────────────────────────────────── |
| 31 | |
| 32 | test('syncState round-trip', (t) => { |
| 33 | roundTrip(t, m.syncState, { |
| 34 | feedKey: fakeHash, |
| 35 | localSeq: 42, |
| 36 | remoteSeq: 15 |
| 37 | }) |
| 38 | }) |
| 39 | |
| 40 | test('fileOffer round-trip (put)', (t) => { |
| 41 | roundTrip(t, m.fileOffer, { |
| 42 | path: '/docs/report.pdf', |
| 43 | contentHash: fakeHash, |
| 44 | size: 524288, |
| 45 | mtime: 1712345678, |
| 46 | op: 0 |
| 47 | }) |
| 48 | }) |
| 49 | |
| 50 | test('fileOffer round-trip (delete)', (t) => { |
| 51 | roundTrip(t, m.fileOffer, { |
| 52 | path: '/docs/old.txt', |
| 53 | contentHash: '0'.repeat(64), |
| 54 | size: 0, |
| 55 | mtime: 0, |
| 56 | op: 1 |
| 57 | }) |
| 58 | }) |
| 59 | |
| 60 | test('fileRequest round-trip (no chunks)', (t) => { |
| 61 | // port note: with compact-encoding >= 3.1 an empty buffer decodes to a |
| 62 | // zero-length buffer, not null. |
| 63 | // Overlay never sends a non-null chunksHave, so the distinction is immaterial. |
| 64 | roundTrip(t, m.fileRequest, { |
| 65 | path: '/docs/report.pdf', |
| 66 | contentHash: fakeHash, |
| 67 | chunksHave: Buffer.alloc(0) |
| 68 | }) |
| 69 | }) |
| 70 | |
| 71 | test('fileRequest round-trip (with chunks)', (t) => { |
| 72 | roundTrip(t, m.fileRequest, { |
| 73 | path: '/docs/report.pdf', |
| 74 | contentHash: fakeHash, |
| 75 | chunksHave: Buffer.from([0x01, 0x02, 0x03]) |
| 76 | }) |
| 77 | }) |
| 78 | |
| 79 | // ── Transfer messages ───────────────────────────────────────── |
| 80 | |
| 81 | test('chunkHashes round-trip', (t) => { |
| 82 | roundTrip(t, m.chunkHashes, { |
| 83 | path: '/docs/report.pdf', |
| 84 | tier: 1, |
| 85 | chunks: [ |
| 86 | { hash: fakeHash, length: 16384 }, |
| 87 | { hash: fakeHash2, length: 12288 }, |
| 88 | { hash: fakeHash3, length: 8192 } |
| 89 | ], |
| 90 | more: 0 |
| 91 | }) |
| 92 | }) |
| 93 | |
| 94 | test('chunkHashes round-trip (empty)', (t) => { |
| 95 | roundTrip(t, m.chunkHashes, { |
| 96 | path: '/empty', |
| 97 | tier: 0, |
| 98 | chunks: [], |
| 99 | more: 0 |
| 100 | }) |
| 101 | }) |
| 102 | |
| 103 | // Paging flag round-trips, and a pre-paging frame (no trailing |
| 104 | // `more` byte) decodes to more:0 so a mixed-version swarm stays compatible. |
| 105 | test('chunkHashes round-trip (more:1 — a non-final page)', (t) => { |
| 106 | roundTrip(t, m.chunkHashes, { |
| 107 | path: '/big', |
| 108 | tier: 3, |
| 109 | chunks: [{ hash: fakeHash, length: 1048576 }], |
| 110 | more: 1 |
| 111 | }) |
| 112 | }) |
| 113 | |
| 114 | test('chunkHashes — omitted more encodes/decodes as 0', (t) => { |
| 115 | const value = { path: '/docs/report.pdf', tier: 1, chunks: [{ hash: fakeHash, length: 16384 }] } |
| 116 | const state = { start: 0, end: 0, buffer: null } |
| 117 | m.chunkHashes.preencode(state, value) |
| 118 | state.buffer = Buffer.alloc(state.end) |
| 119 | m.chunkHashes.encode(state, value) |
| 120 | state.start = 0 |
| 121 | const decoded = m.chunkHashes.decode(state) |
| 122 | t.is(decoded.more, 0, 'omitted more → 0 (single, complete page)') |
| 123 | t.alike(decoded.chunks, value.chunks) |
| 124 | }) |
| 125 | |
| 126 | test('chunkHashes — a pre-paging frame (no trailing byte) decodes to more:0', (t) => { |
| 127 | // Encode with the current codec (which appends more:0), then strip the single |
| 128 | // trailing more byte to reproduce exactly what an older peer puts on the wire. |
| 129 | const value = { path: '/legacy', tier: 2, chunks: [{ hash: fakeHash, length: 8192 }], more: 0 } |
| 130 | const state = { start: 0, end: 0, buffer: null } |
| 131 | m.chunkHashes.preencode(state, value) |
| 132 | state.buffer = Buffer.alloc(state.end) |
| 133 | m.chunkHashes.encode(state, value) |
| 134 | |
| 135 | // Old frame = our encoding minus the appended uint8 more (always 1 byte, last). |
| 136 | const oldFrame = state.buffer.subarray(0, state.buffer.length - 1) |
| 137 | const dstate = { start: 0, end: oldFrame.length, buffer: oldFrame } |
| 138 | const decoded = m.chunkHashes.decode(dstate) |
| 139 | t.is(decoded.more, 0, 'missing trailing byte → more:0') |
| 140 | t.is(decoded.path, '/legacy') |
| 141 | t.alike(decoded.chunks, [{ hash: fakeHash, length: 8192 }]) |
| 142 | }) |
| 143 | |
| 144 | test('chunkNeed round-trip', (t) => { |
| 145 | roundTrip(t, m.chunkNeed, { |
| 146 | path: '/docs/report.pdf', |
| 147 | indices: [0, 2, 5, 10, 99] |
| 148 | }) |
| 149 | }) |
| 150 | |
| 151 | test('chunkNeed round-trip (empty)', (t) => { |
| 152 | roundTrip(t, m.chunkNeed, { |
| 153 | path: '/file', |
| 154 | indices: [] |
| 155 | }) |
| 156 | }) |
| 157 | |
| 158 | test('chunkData round-trip', (t) => { |
| 159 | const data = crypto.randomBytes(16384) |
| 160 | roundTrip(t, m.chunkData, { |
| 161 | path: '/docs/report.pdf', |
| 162 | index: 7, |
| 163 | data |
| 164 | }) |
| 165 | }) |
| 166 | |
| 167 | test('chunkCancel round-trip', (t) => { |
| 168 | roundTrip(t, m.chunkCancel, { |
| 169 | path: '/docs/report.pdf' |
| 170 | }) |
| 171 | }) |
| 172 | |
| 173 | test('transferComplete round-trip', (t) => { |
| 174 | roundTrip(t, m.transferComplete, { |
| 175 | path: '/docs/report.pdf', |
| 176 | contentHash: fakeHash |
| 177 | }) |
| 178 | }) |
| 179 | |
| 180 | test('conflict round-trip', (t) => { |
| 181 | roundTrip(t, m.conflict, { |
| 182 | path: '/docs/report.pdf', |
| 183 | myHash: fakeHash, |
| 184 | theirHash: fakeHash2, |
| 185 | ancestorHash: fakeHash3 |
| 186 | }) |
| 187 | }) |
| 188 | |
| 189 | test('conflict with null ancestor', (t) => { |
| 190 | const result = roundTrip(t, m.conflict, { |
| 191 | path: '/new-file.txt', |
| 192 | myHash: fakeHash, |
| 193 | theirHash: fakeHash2, |
| 194 | ancestorHash: '0'.repeat(64) |
| 195 | }) |
| 196 | t.is(result.ancestorHash, '0'.repeat(64)) |
| 197 | }) |
| 198 | |
| 199 | // ── Large payloads ──────────────────────────────────────────── |
| 200 | |
| 201 | test('chunkHashes with 100 chunks', (t) => { |
| 202 | const chunks = [] |
| 203 | for (let i = 0; i < 100; i++) { |
| 204 | chunks.push({ hash: crypto.randomBytes(32).toString('hex'), length: 16384 + i }) |
| 205 | } |
| 206 | roundTrip(t, m.chunkHashes, { path: '/large', tier: 2, chunks, more: 0 }) |
| 207 | }) |
| 208 | |
| 209 | test('chunkNeed with 500 indices', (t) => { |
| 210 | const indices = [] |
| 211 | for (let i = 0; i < 500; i++) indices.push(i * 2) |
| 212 | roundTrip(t, m.chunkNeed, { path: '/large', indices }) |
| 213 | }) |
| 214 | |
| 215 | test('chunkData with 1MB payload', (t) => { |
| 216 | roundTrip(t, m.chunkData, { |
| 217 | path: '/big', |
| 218 | index: 0, |
| 219 | data: crypto.randomBytes(1048576) |
| 220 | }) |
| 221 | }) |
| 222 | |
| 223 | // ── Tree messages (0.5a) ────────────────────────────────────── |
| 224 | |
| 225 | test('treeRequest round-trip', (t) => { |
| 226 | roundTrip(t, m.treeRequest, { hash: fakeHash, nonce: 0 }) |
| 227 | }) |
| 228 | |
| 229 | test('treeRequest round-trip (with nonce)', (t) => { |
| 230 | roundTrip(t, m.treeRequest, { hash: fakeHash, nonce: 42 }) |
| 231 | }) |
| 232 | |
| 233 | test('treeRequest — omitted nonce decodes to 0', (t) => { |
| 234 | const value = { hash: fakeHash } |
| 235 | const state = { start: 0, end: 0, buffer: null } |
| 236 | m.treeRequest.preencode(state, value) |
| 237 | state.buffer = Buffer.alloc(state.end) |
| 238 | m.treeRequest.encode(state, value) |
| 239 | state.start = 0 |
| 240 | const decoded = m.treeRequest.decode(state) |
| 241 | t.is(decoded.nonce, 0, 'omitted nonce → 0') |
| 242 | t.is(decoded.hash, fakeHash) |
| 243 | }) |
| 244 | |
| 245 | test('treeResponse round-trip — empty entries', (t) => { |
| 246 | roundTrip(t, m.treeResponse, { hash: fakeHash, entries: [], more: 0, nonce: 0 }) |
| 247 | }) |
| 248 | |
| 249 | test('treeResponse round-trip — mixed file/dir/symlink entries', (t) => { |
| 250 | roundTrip(t, m.treeResponse, { |
| 251 | hash: fakeHash, |
| 252 | entries: [ |
| 253 | { kind: 0, exec: 0, name: 'a.js', childHash: fakeHash2, size: 1024 }, |
| 254 | { kind: 0, exec: 1, name: 'run.sh', childHash: fakeHash3, size: 256 }, |
| 255 | { kind: 1, exec: 0, name: 'lib', childHash: fakeHash, size: 50000 }, |
| 256 | { kind: 2, exec: 0, name: 'current', childHash: fakeHash2, size: 12 } |
| 257 | ], |
| 258 | more: 0, |
| 259 | nonce: 0 |
| 260 | }) |
| 261 | }) |
| 262 | |
| 263 | test('treeResponse round-trip — many entries', (t) => { |
| 264 | const entries = [] |
| 265 | for (let i = 0; i < 500; i++) { |
| 266 | entries.push({ |
| 267 | kind: 0, |
| 268 | exec: 0, |
| 269 | name: 'file-' + i + '.js', |
| 270 | childHash: crypto.randomBytes(32).toString('hex'), |
| 271 | size: i * 100 |
| 272 | }) |
| 273 | } |
| 274 | roundTrip(t, m.treeResponse, { hash: fakeHash, entries, more: 0, nonce: 0 }) |
| 275 | }) |
| 276 | |
| 277 | test('treeResponse preserves UTF-8 names', (t) => { |
| 278 | roundTrip(t, m.treeResponse, { |
| 279 | hash: fakeHash, |
| 280 | entries: [ |
| 281 | { kind: 0, exec: 0, name: 'café.txt', childHash: fakeHash2, size: 10 }, |
| 282 | { kind: 0, exec: 0, name: '日本.md', childHash: fakeHash3, size: 20 } |
| 283 | ], |
| 284 | more: 0, |
| 285 | nonce: 0 |
| 286 | }) |
| 287 | }) |
| 288 | |
| 289 | // Paging flag + nonce round-trip, and a pre-paging frame (no trailing bytes) |
| 290 | // decodes to more:0/nonce:0 so a mixed-version swarm stays compatible. |
| 291 | test('treeResponse round-trip (more:1 + nonce — a non-final page)', (t) => { |
| 292 | roundTrip(t, m.treeResponse, { |
| 293 | hash: fakeHash, |
| 294 | entries: [{ kind: 0, exec: 0, name: 'a.js', childHash: fakeHash2, size: 1024 }], |
| 295 | more: 1, |
| 296 | nonce: 7 |
| 297 | }) |
| 298 | }) |
| 299 | |
| 300 | test('treeResponse — omitted more/nonce encode/decode as 0', (t) => { |
| 301 | const value = { hash: fakeHash, entries: [{ kind: 1, exec: 0, name: 'lib', childHash: fakeHash2, size: 9 }] } |
| 302 | const state = { start: 0, end: 0, buffer: null } |
| 303 | m.treeResponse.preencode(state, value) |
| 304 | state.buffer = Buffer.alloc(state.end) |
| 305 | m.treeResponse.encode(state, value) |
| 306 | state.start = 0 |
| 307 | const decoded = m.treeResponse.decode(state) |
| 308 | t.is(decoded.more, 0, 'omitted more → 0 (single, complete page)') |
| 309 | t.is(decoded.nonce, 0, 'omitted nonce → 0') |
| 310 | t.alike(decoded.entries, value.entries) |
| 311 | }) |
| 312 | |
| 313 | test('treeResponse — a pre-paging frame (no trailing more/nonce) decodes to 0/0', (t) => { |
| 314 | const value = { hash: fakeHash, entries: [{ kind: 0, exec: 0, name: 'x', childHash: fakeHash2, size: 3 }], more: 0, nonce: 0 } |
| 315 | const state = { start: 0, end: 0, buffer: null } |
| 316 | m.treeResponse.preencode(state, value) |
| 317 | state.buffer = Buffer.alloc(state.end) |
| 318 | m.treeResponse.encode(state, value) |
| 319 | |
| 320 | // Old upstream frame = our encoding minus the two appended uints (more, nonce), each 1 byte for 0. |
| 321 | const oldFrame = state.buffer.subarray(0, state.buffer.length - 2) |
| 322 | const dstate = { start: 0, end: oldFrame.length, buffer: oldFrame } |
| 323 | const decoded = m.treeResponse.decode(dstate) |
| 324 | t.is(decoded.more, 0, 'missing trailing bytes → more:0') |
| 325 | t.is(decoded.nonce, 0, 'missing trailing bytes → nonce:0') |
| 326 | t.is(decoded.hash, fakeHash) |
| 327 | t.alike(decoded.entries, value.entries) |
| 328 | }) |
| 329 | |
| 330 | test('contentRequest round-trip — with chunksHave + from', (t) => { |
| 331 | roundTrip(t, m.contentRequest, { |
| 332 | contentHash: fakeHash, |
| 333 | chunksHave: crypto.randomBytes(128), |
| 334 | from: 'a1b2c3d4e5f6' |
| 335 | }) |
| 336 | }) |
| 337 | |
| 338 | test('contentRequest round-trip — empty chunksHave', (t) => { |
| 339 | // port note: see fileRequest no-chunks — empty buffer round-trips as |
| 340 | // a zero-length buffer under compact-encoding@3.1.0, not null. |
| 341 | roundTrip(t, m.contentRequest, { |
| 342 | contentHash: fakeHash, |
| 343 | chunksHave: Buffer.alloc(0), |
| 344 | from: 'deadbeef' |
| 345 | }) |
| 346 | }) |
| 347 | |
| 348 | // ── Transfer-control (message 12) ───────────────────────────── |
| 349 | |
| 350 | test('transferControl round-trip — paused', (t) => { |
| 351 | roundTrip(t, m.transferControl, { contentHash: fakeHash, state: 0 }) |
| 352 | }) |
| 353 | |
| 354 | test('transferControl round-trip — stopped', (t) => { |
| 355 | roundTrip(t, m.transferControl, { contentHash: fakeHash2, state: 1 }) |
| 356 | }) |
| 357 | |
| 358 | // The `from` field is appended last; a sender that omits it |
| 359 | // encodes '' and the decoder yields from:'' (never undefined, never crashes). |
| 360 | test('contentRequest — omitted from decodes to empty string', (t) => { |
| 361 | const state = { start: 0, end: 0, buffer: null } |
| 362 | const value = { contentHash: fakeHash, chunksHave: null } |
| 363 | m.contentRequest.preencode(state, value) |
| 364 | state.buffer = Buffer.alloc(state.end) |
| 365 | m.contentRequest.encode(state, value) |
| 366 | state.start = 0 |
| 367 | const decoded = m.contentRequest.decode(state) |
| 368 | t.is(decoded.from, '', 'omitted from → empty string') |
| 369 | t.is(decoded.contentHash, fakeHash) |
| 370 | }) |
| 371 | |
| 372 | // ── Transfer-progress (message 13) ──────────────────────────── |
| 373 | |
| 374 | test('transferProgress round-trip — have baseline', (t) => { |
| 375 | roundTrip(t, m.transferProgress, { contentHash: fakeHash, have: 123456789 }) |
| 376 | }) |
| 377 | |
| 378 | test('transferProgress round-trip — zero have', (t) => { |
| 379 | roundTrip(t, m.transferProgress, { contentHash: fakeHash2, have: 0 }) |
| 380 | }) |