check-message-interop.mjs (10520B)
1 import assert from 'node:assert/strict'; 2 import { createHash } from 'node:crypto'; 3 import { 4 lstat, 5 realpath, 6 readFile, 7 writeFile, 8 mkdtemp, 9 rm 10 } from 'node:fs/promises'; 11 import { tmpdir } from 'node:os'; 12 import { join, dirname } from 'node:path'; 13 import { fileURLToPath } from 'node:url'; 14 import { spawnSync } from 'node:child_process'; 15 import { 16 messageReplay, 17 actualWebMessage 18 } from '../tests/conformance/message-cases.ts'; 19 import { qualifiedMessageReplay } from '../tests/conformance/message-qualified-matrix.ts'; 20 const args = process.argv.slice(2); 21 assert.ok( 22 args.length === 0 || 23 (args.length === 1 && ['--write', '--write-qualified'].includes(args[0])), 24 'expected no arguments or explicit --write or --write-qualified' 25 ); 26 assert.equal(process.versions.node, '24.21.0'); 27 const root = fileURLToPath(new URL('../../', import.meta.url)); 28 const folder = new URL('../../contracts/interop/message/', import.meta.url); 29 const revision = '189c49b74b4bafc142b00b76b296477931139e72'; 30 const digest = (bytes) => createHash('sha256').update(bytes).digest('hex'); 31 const cargo = [ 32 '+1.99.0', 33 'run', 34 '--manifest-path', 35 'contracts/interop/message/oracle/Cargo.toml', 36 '--locked', 37 '--offline', 38 '--quiet', 39 '--' 40 ]; 41 function success(command, argv, cwd = root) { 42 // Compiler and cache waits on a busy host have no outer elapsed cutoff. 43 const r = spawnSync(command, argv, { 44 cwd, 45 encoding: 'utf8', 46 maxBuffer: 1024 * 1024 47 }); 48 assert.ifError(r.error); 49 assert.equal(r.signal, null); 50 assert.equal(r.status, 0, r.stderr || r.stdout); 51 return r.stdout; 52 } 53 async function owned(name) { 54 const path = new URL(name, folder), 55 info = await lstat(path); 56 assert.ok( 57 info.isFile() && 58 !info.isSymbolicLink() && 59 info.size > 0 && 60 info.size <= 1024 * 1024 61 ); 62 const bytes = await readFile(path); 63 assert.equal(bytes.length, info.size); 64 return bytes; 65 } 66 const profile = JSON.parse((await owned('source_profile.v1.json')).toString()); 67 assert.equal(profile.revision, revision); 68 await owned('oracle/Cargo.toml'); 69 await owned('oracle/Cargo.lock'); 70 await owned('oracle/src/main.rs'); 71 // Offline actual Cargo/cache identity and exact immutable source, never sibling Lib. 72 const metadata = JSON.parse( 73 success('cargo', [ 74 '+1.99.0', 75 'metadata', 76 '--manifest-path', 77 'contracts/interop/message/oracle/Cargo.toml', 78 '--locked', 79 '--offline', 80 '--format-version', 81 '1' 82 ]) 83 ); 84 const gitPackages = metadata.packages.filter( 85 (p) => typeof p.source === 'string' && p.source.startsWith('git+') 86 ); 87 assert.equal(gitPackages.length, 6); 88 const roots = new Set(); 89 for (const p of gitPackages) { 90 assert.equal( 91 p.source, 92 'git+https://github.com/radrootslabs/lib?rev=' + revision + '#' + revision 93 ); 94 roots.add( 95 success( 96 'git', 97 ['rev-parse', '--show-toplevel'], 98 dirname(p.manifest_path) 99 ).trim() 100 ); 101 } 102 assert.equal(roots.size, 1); 103 const cache = [...roots][0]; 104 assert.equal(success('git', ['rev-parse', 'HEAD'], cache).trim(), revision); 105 assert.equal(success('git', ['diff', '--raw'], cache), ''); 106 assert.equal(success('git', ['diff', '--cached', '--raw'], cache), ''); 107 const untracked = success( 108 'git', 109 ['ls-files', '--others', '--exclude-standard'], 110 cache 111 ).trim(); 112 assert.ok(untracked === '' || untracked === '.cargo-ok'); 113 if (untracked) { 114 const marker = await lstat(join(cache, untracked)); 115 assert.ok(marker.isFile() && !marker.isSymbolicLink() && marker.size === 0); 116 } 117 assert.deepEqual( 118 Object.keys(profile.pinned_lib_source_sha256).sort(), 119 [ 120 'crates/event/src/message.rs', 121 'crates/event_codec/src/message/decode.rs', 122 'crates/event_codec/src/message/encode.rs', 123 'crates/event_codec/src/message/tags.rs' 124 ].sort() 125 ); 126 for (const [name, hash] of Object.entries(profile.pinned_lib_source_sha256)) { 127 const path = join(cache, name), 128 info = await lstat(path); 129 assert.equal(await realpath(path), path); 130 assert.ok( 131 info.isFile() && 132 !info.isSymbolicLink() && 133 info.size > 0 && 134 info.size <= 1024 * 1024 135 ); 136 assert.equal(digest(await readFile(path)), hash, name); 137 } 138 const emitted = success('cargo', [...cargo, '--emit']); 139 const corpus = JSON.parse(emitted); 140 assert.equal(corpus.revision, revision); 141 assert.equal( 142 corpus.source_sha256, 143 digest(await owned('source_profile.v1.json')) 144 ); 145 if (args[0] === '--write') { 146 for (const name of ['corpus.v1.json', 'provenance.v1.json']) { 147 try { 148 const info = await lstat(new URL(name, folder)); 149 assert.ok(info.isFile() && !info.isSymbolicLink()); 150 } catch (error) { 151 if (!( 152 error && 153 typeof error === 'object' && 154 'code' in error && 155 error.code === 'ENOENT' 156 )) 157 throw error; 158 } 159 } 160 await writeFile(new URL('corpus.v1.json', folder), emitted); 161 /** @type {Record<string,string>} */ const inputs = {}; 162 for (const name of [ 163 'source_profile.v1.json', 164 'corpus.v1.json', 165 'oracle/Cargo.toml', 166 'oracle/Cargo.lock', 167 'oracle/src/main.rs' 168 ]) 169 inputs[name] = digest(await owned(name)); 170 await writeFile( 171 new URL('provenance.v1.json', folder), 172 JSON.stringify( 173 { 174 schema_version: 1, 175 revision, 176 inputs, 177 pinned_lib_source_sha256: profile.pinned_lib_source_sha256, 178 qualification: 179 'Generated by actual pinned Rust Message codec; explicit canonical/budget/schema web differences retained. No signature, room, extension, relay or send qualification.' 180 }, 181 null, 182 2 183 ) + '\n' 184 ); 185 } 186 assert.equal((await owned('corpus.v1.json')).toString(), emitted); 187 const provenance = JSON.parse((await owned('provenance.v1.json')).toString()); 188 assert.equal(provenance.revision, revision); 189 assert.deepEqual( 190 provenance.pinned_lib_source_sha256, 191 profile.pinned_lib_source_sha256 192 ); 193 assert.deepEqual( 194 Object.keys(provenance.inputs).sort(), 195 [ 196 'source_profile.v1.json', 197 'corpus.v1.json', 198 'oracle/Cargo.toml', 199 'oracle/Cargo.lock', 200 'oracle/src/main.rs' 201 ].sort() 202 ); 203 for (const [name, hash] of Object.entries(provenance.inputs)) 204 assert.equal(digest(await owned(name)), hash); 205 success('cargo', [...cargo, '--check']); 206 for (const vector of corpus.vectors) { 207 const actual = actualWebMessage(vector); 208 assert.equal(actual.status, vector.web_status, vector.id); 209 if (vector.web_status === 'supported') { 210 const { error, ...expected } = vector.expected; 211 assert.equal(error, undefined); 212 assert.deepEqual(actual, expected, vector.id); 213 } else if (vector.expected.status === 'supported') 214 assert.ok(vector.policy_difference, vector.id); 215 } 216 const temporary = await mkdtemp( 217 join(tmpdir(), 'harvestcircle-message-interop-') 218 ); 219 try { 220 async function consume(replay, batch) { 221 assert.ok(replay.length > 0); 222 const file = join(temporary, batch + '.json'); 223 await writeFile(file, JSON.stringify(replay)); 224 const result = JSON.parse(success('cargo', [...cargo, '--consume', file])); 225 assert.equal(result.revision, revision); 226 assert.equal(result.consumed.length, replay.length); 227 assert.deepEqual( 228 result.consumed, 229 replay.map((v) => ({ id: v.id, message: v.expected })) 230 ); 231 return result; 232 } 233 const original = messageReplay(); 234 assert.equal(original.length, 11); 235 await consume(original, 'original'); 236 assert.equal(qualifiedMessageReplay.length, 16); 237 const qualified = await consume(qualifiedMessageReplay, 'qualified'); 238 const recipePath = new URL( 239 '../tests/conformance/message-qualified-matrix.ts', 240 import.meta.url 241 ); 242 const receiptPath = new URL( 243 '../tests/conformance/message-qualified-writer-rust.v1.json', 244 import.meta.url 245 ); 246 const inputs = { 247 'message-qualified-matrix.ts': digest(await readFile(recipePath)) 248 }; 249 for (const name of [ 250 'source_profile.v1.json', 251 'corpus.v1.json', 252 'oracle/src/main.rs' 253 ]) 254 inputs[name] = digest(await owned(name)); 255 const receipt = { 256 schema_version: 1, 257 revision, 258 recipe_sha256: digest(JSON.stringify(qualifiedMessageReplay)), 259 inputs, 260 consumed: qualified.consumed, 261 qualification: 262 'Actual pinned Rust99 consumes separate browser-owned unsigned kind 14 Message recipes; not independent-client receive/reply, encryption, extension, relay or publication qualification.' 263 }; 264 const existing = await lstat(receiptPath); 265 assert.ok(existing.isFile() && !existing.isSymbolicLink()); 266 if (args[0] === '--write' || args[0] === '--write-qualified') 267 await writeFile(receiptPath, JSON.stringify(receipt, null, 2) + '\n'); 268 assert.deepEqual(JSON.parse(await readFile(receiptPath, 'utf8')), receipt); 269 const controls = [ 270 [ 271 'standalone-seal-kind', 272 0, 273 (row) => { 274 row.wire_parts.kind = 13; 275 } 276 ], 277 [ 278 'missing-recipient', 279 0, 280 (row) => { 281 row.wire_parts.tags = []; 282 } 283 ], 284 [ 285 'changed-reply-parent', 286 2, 287 (row) => { 288 row.wire_parts.tags[1][1] = '4'.repeat(64); 289 } 290 ], 291 [ 292 'changed-subject', 293 4, 294 (row) => { 295 row.wire_parts.tags[1][1] = 'Different subject'; 296 } 297 ] 298 ]; 299 for (const [mutation, index, change] of controls) { 300 const originalRow = qualifiedMessageReplay[index]; 301 const row = { 302 ...originalRow, 303 wire_parts: { 304 ...originalRow.wire_parts, 305 tags: originalRow.wire_parts.tags.map((tag) => [...tag]) 306 } 307 }; 308 change(row); 309 const file = join(temporary, mutation + '.json'); 310 await writeFile(file, JSON.stringify([row])); 311 const result = spawnSync('cargo', [...cargo, '--consume', file], { 312 cwd: root, 313 encoding: 'utf8', 314 maxBuffer: 1024 * 1024 315 }); 316 assert.ifError(result.error); 317 assert.equal(result.signal, null); 318 assert.equal(result.status, 101, result.stderr || result.stdout); 319 assert.match(result.stderr, /assertion .* failed/); 320 console.log( 321 JSON.stringify({ 322 direction: 'WEB_TO_RUST_NEGATIVE', 323 mutation, 324 exit_code: result.status, 325 actual_stderr: result.stderr 326 }) 327 ); 328 } 329 console.log( 330 JSON.stringify({ 331 result: 'PASS', 332 scope: 'SOURCE_ORACLE_NOT_INDEPENDENT_CLIENT_QUALIFICATION', 333 revision, 334 rust_reader_cases: corpus.vectors.length, 335 original_writer_cases: original.length, 336 qualified_writer_cases: qualifiedMessageReplay.length, 337 negative_controls: controls.length, 338 keys: 'NO_KEYS' 339 }) 340 ); 341 } finally { 342 await rm(temporary, { recursive: true, force: true }); 343 }